Skip to content

Commit

Permalink
Port bench
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Feb 13, 2024
1 parent 4b9e9b6 commit 5408367
Show file tree
Hide file tree
Showing 34 changed files with 282 additions and 63 deletions.
18 changes: 17 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ BITCOIN_CORE_H = \
util/ui_change_type.h \
util/vector.h \
util/convert.h \
util/signstr.h \
util/contractabi.h \
util/tokenstr.h \
validation.h \
validationinterface.h \
versionbits.h \
Expand All @@ -361,6 +364,7 @@ BITCOIN_CORE_H = \
wallet/receive.h \
wallet/rpc/util.h \
wallet/rpc/wallet.h \
wallet/rpc/addresses.h \
wallet/salvage.h \
wallet/scriptpubkeyman.h \
wallet/spend.h \
Expand All @@ -377,7 +381,13 @@ BITCOIN_CORE_H = \
zmq/zmqnotificationinterface.h \
zmq/zmqpublishnotifier.h \
zmq/zmqrpc.h \
zmq/zmqutil.h
zmq/zmqutil.h \
qtum/posutils.h \
qtum/qtumtransaction.h \
qtum/qtumutils.h \
qtum/qtumtoken.h \
qtum/qtumledger.h \
qtum/delegationutils.h


obj/build.h: FORCE
Expand Down Expand Up @@ -479,6 +489,7 @@ libbitcoin_node_a_SOURCES = \
validation.cpp \
validationinterface.cpp \
versionbits.cpp \
qtum/qtumledger.cpp \
$(BITCOIN_CORE_H)

if ENABLE_WALLET
Expand Down Expand Up @@ -721,6 +732,10 @@ libbitcoin_common_a_SOURCES = \
script/signingprovider.cpp \
script/solver.cpp \
warnings.cpp \
qtum/qtumutils.cpp \
qtum/qtumtoken.cpp \
qtum/delegationutils.cpp \
util/contractabi.cpp \
libff/libff/algebra/curves/public_params.hpp \
libff/libff/algebra/curves/curve_utils.hpp \
evmone/include/evmone/evmone.h \
Expand Down Expand Up @@ -960,6 +975,7 @@ libqtum_util_a_SOURCES = \
util/string.cpp \
util/time.cpp \
util/tokenpipe.cpp \
util/tokenstr.cpp \
$(BITCOIN_CORE_H)
#

Expand Down
2 changes: 1 addition & 1 deletion src/bench/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bench_bitcoin
bench_qtum
10 changes: 5 additions & 5 deletions src/bench/bench_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ int main(int argc, char** argv)
}

if (HelpRequested(argsman)) {
std::cout << "Usage: bench_bitcoin [options]\n"
std::cout << "Usage: bench_qtum [options]\n"
"\n"
<< argsman.GetHelpMessage()
<< "Description:\n"
"\n"
" bench_bitcoin executes microbenchmarks. The quality of the benchmark results\n"
" bench_qtum executes microbenchmarks. The quality of the benchmark results\n"
" highly depend on the stability of the machine. It can sometimes be difficult\n"
" to get stable, repeatable results, so here are a few tips:\n"
"\n"
Expand All @@ -88,20 +88,20 @@ int main(int argc, char** argv)
" * If results are still not reliable, increase runtime with e.g.\n"
" -min-time=5000 to let a benchmark run for at least 5 seconds.\n"
"\n"
" * bench_bitcoin uses nanobench [3] for which there is extensive\n"
" * bench_qtum uses nanobench [3] for which there is extensive\n"
" documentation available online.\n"
"\n"
"Environment Variables:\n"
"\n"
" To attach a profiler you can run a benchmark in endless mode. This can be\n"
" done with the environment variable NANOBENCH_ENDLESS. E.g. like so:\n"
"\n"
" NANOBENCH_ENDLESS=MuHash ./bench_bitcoin -filter=MuHash\n"
" NANOBENCH_ENDLESS=MuHash ./bench_qtum -filter=MuHash\n"
"\n"
" In rare cases it can be useful to suppress stability warnings. This can be\n"
" done with the environment variable NANOBENCH_SUPPRESS_WARNINGS, e.g:\n"
"\n"
" NANOBENCH_SUPPRESS_WARNINGS=1 ./bench_bitcoin\n"
" NANOBENCH_SUPPRESS_WARNINGS=1 ./bench_qtum\n"
"\n"
"Notes:\n"
"\n"
Expand Down
9 changes: 5 additions & 4 deletions src/bench/block_assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ static void AssembleBlock(benchmark::Bench& bench)
witness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);

// Collect some loose transactions that spend the coinbases of our mined blocks
constexpr size_t NUM_BLOCKS{200};
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
constexpr size_t NUM_BLOCKS{2100};
constexpr size_t coinbaseMaturity = 2000;
std::array<CTransactionRef, NUM_BLOCKS - coinbaseMaturity + 1> txs;
for (size_t b{0}; b < NUM_BLOCKS; ++b) {
CMutableTransaction tx;
tx.vin.emplace_back(MineBlock(test_setup->m_node, P2WSH_OP_TRUE));
tx.vin.back().scriptWitness = witness;
tx.vout.emplace_back(1337, P2WSH_OP_TRUE);
if (NUM_BLOCKS - b >= COINBASE_MATURITY)
tx.vout.emplace_back(101337, P2WSH_OP_TRUE);
if (NUM_BLOCKS - b >= coinbaseMaturity)
txs.at(b) = MakeTransactionRef(tx);
}
{
Expand Down
3 changes: 3 additions & 0 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <streams.h>
#include <util/chaintype.h>
#include <validation.h>
#include <test/util/setup_common.h>

// These are the two major time-sinks which happen after we have fully received
// a block off the wire, but before we can relay the block on to peers using
Expand Down Expand Up @@ -38,6 +39,8 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)

ArgsManager bench_args;
const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN);
const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
Chainstate& chainstate = testing_setup->m_node.chainman->ActiveChainstate();

bench.unit("block").run([&] {
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
Expand Down
3 changes: 2 additions & 1 deletion src/bench/duplicate_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static void DuplicateInputs(benchmark::Bench& bench)
block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
block.nNonce = 0;
auto nHeight = pindexPrev->nHeight + 1;
Chainstate& chainstate = testing_setup->m_node.chainman->ActiveChainstate();

// Make a coinbase TX
coinbaseTx.vin.resize(1);
Expand All @@ -45,7 +46,7 @@ static void DuplicateInputs(benchmark::Bench& bench)
naughtyTx.vout[0].nValue = 0;
naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;

uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
uint64_t n_inputs = (((dgpMaxBlockSerSize / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bench/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void Logging(benchmark::Bench& bench, const std::vector<const char*>& ext
LogInstance().DisableCategory(BCLog::LogFlags::ALL);

TestingSetup test_setup{
ChainType::REGTEST,
ChainType::UNITTEST,
extra_args,
};

Expand Down
4 changes: 2 additions & 2 deletions src/bench/mempool_stress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ static void ComplexMemPool(benchmark::Bench& bench)
static void MempoolCheck(benchmark::Bench& bench)
{
FastRandomContext det_rand{true};
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {"-checkmempool=1"});
auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::UNITTEST, {"-checkmempool=1"});
CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
LOCK2(cs_main, pool.cs);
testing_setup->PopulateMempool(det_rand, 400, true);
const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();

bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
// Bump up the spendheight so we don't hit premature coinbase spend errors.
pool.check(coins_tip, /*spendheight=*/300);
pool.check(coins_tip, /*spendheight=*/4100);
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/bench/wallet_balance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <validationinterface.h>
#include <wallet/receive.h>
#include <wallet/wallet.h>
#include <chainparams.h>

#include <optional>

Expand All @@ -35,7 +36,8 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b

const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};

for (int i = 0; i < 100; ++i) {
int blockCount = Params().GetConsensus().CoinbaseMaturity(0) + 100;
for (int i = 0; i < blockCount; ++i) {
generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
}
Expand Down
14 changes: 8 additions & 6 deletions src/bench/wallet_create_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void generateFakeBlock(const CChainParams& params,
coinbase_tx.vin[0].prevout.SetNull();
coinbase_tx.vout.resize(2);
coinbase_tx.vout[0].scriptPubKey = coinbase_out_script;
coinbase_tx.vout[0].nValue = 49 * COIN;
coinbase_tx.vout[0].nValue = 19999 * COIN;
coinbase_tx.vin[0].scriptSig = CScript() << ++tip.tip_height << OP_0;
coinbase_tx.vout[1].scriptPubKey = coinbase_out_script; // extra output
coinbase_tx.vout[1].nValue = 1 * COIN;
Expand Down Expand Up @@ -106,7 +106,8 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type

// Check available balance
auto bal = WITH_LOCK(wallet.cs_wallet, return wallet::AvailableCoins(wallet).GetTotalAmount()); // Cache
assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY));
constexpr size_t coinbaseMaturity = 2000;
assert(bal == 20000 * COIN * (chain_size - coinbaseMaturity));

wallet::CCoinControl coin_control;
coin_control.m_allow_other_inputs = allow_other_inputs;
Expand All @@ -126,7 +127,7 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type
}

// If automatic coin selection is enabled, add the value of another UTXO to the target
if (coin_control.m_allow_other_inputs) target += 50 * COIN;
if (coin_control.m_allow_other_inputs) target += 20000 * COIN;
std::vector<wallet::CRecipient> recipients = {{dest, target, true}};

bench.epochIterations(5).run([&] {
Expand Down Expand Up @@ -158,7 +159,7 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType

// Generate chain; each coinbase will have two outputs to fill-up the wallet
const auto& params = Params();
unsigned int chain_size = 1000;
unsigned int chain_size = 3000;
for (unsigned int i = 0; i < chain_size / dest_wallet.size(); ++i) {
for (const auto& dest : dest_wallet) {
generateFakeBlock(params, test_setup->m_node, wallet, dest);
Expand All @@ -167,12 +168,13 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType

// Check available balance
auto bal = WITH_LOCK(wallet.cs_wallet, return wallet::AvailableCoins(wallet).GetTotalAmount()); // Cache
assert(bal == 50 * COIN * (chain_size - COINBASE_MATURITY));
constexpr size_t coinbaseMaturity = 2000;
assert(bal == 20000 * COIN * (chain_size - coinbaseMaturity));

bench.epochIterations(2).run([&] {
LOCK(wallet.cs_wallet);
const auto& res = wallet::AvailableCoins(wallet);
assert(res.All().size() == (chain_size - COINBASE_MATURITY) * 2);
assert(res.All().size() == (chain_size - coinbaseMaturity) * 2);
});
}

Expand Down
48 changes: 41 additions & 7 deletions src/common/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <utility>
#include <variant>

const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_CONF_FILENAME = "qtum.conf";
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";

ArgsManager gArgs;
Expand Down Expand Up @@ -685,12 +685,12 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message

fs::path GetDefaultDataDir()
{
// Windows: C:\Users\Username\AppData\Roaming\Bitcoin
// macOS: ~/Library/Application Support/Bitcoin
// Unix-like: ~/.bitcoin
// Windows: C:\Users\Username\AppData\Roaming\Qtum
// macOS: ~/Library/Application Support/Qtum
// Unix-like: ~/.qtum
#ifdef WIN32
// Windows
return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
return GetSpecialFolderPath(CSIDL_APPDATA) / "Qtum";
#else
fs::path pathRet;
char* pszHome = getenv("HOME");
Expand All @@ -700,10 +700,10 @@ fs::path GetDefaultDataDir()
pathRet = fs::path(pszHome);
#ifdef MAC_OSX
// macOS
return pathRet / "Library/Application Support/Bitcoin";
return pathRet / "Library/Application Support/Qtum";
#else
// Unix-like
return pathRet / ".bitcoin";
return pathRet / ".qtum";
#endif
#endif
}
Expand Down Expand Up @@ -812,6 +812,40 @@ void ArgsManager::LogArgs() const
logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
}

std::map<std::string, std::vector<std::string>> ArgsManager::getArgsList(const std::vector<std::string>& paramListType) const
{
LOCK(cs_args);
// Get argument list
std::map<std::string, bool> args;
for (const auto& arg : m_settings.forced_settings) {
args[arg.first] = true;
}
for (const auto& arg : m_settings.command_line_options) {
args[arg.first] = true;
}
for (const auto& arg : m_settings.ro_config) {
for(const auto& confArg : arg.second)
args[confArg.first] = true;
}

// Fill argument list with values
std::map<std::string, std::vector<std::string>> ret;
for (const auto& arg : args) {
std::string paramName = '-' + arg.first;
std::vector<std::string> paramValue;
bool isList = std::find(std::begin(paramListType), std::end(paramListType), paramName) != std::end(paramListType);
if(isList) {
paramValue = GetArgs(paramName);
}
else {
paramValue.push_back(GetArg(paramName, ""));
}
ret[arg.first] = paramValue;
}

return ret;
}

namespace common {
#ifdef WIN32
WinCmdLineArgs::WinCmdLineArgs()
Expand Down
5 changes: 5 additions & 0 deletions src/common/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ class ArgsManager
*/
void LogArgs() const;

/**
* Return config arguments
*/
std::map<std::string, std::vector<std::string>> getArgsList(const std::vector<std::string>& paramListType) const;

private:
/**
* Get data directory path
Expand Down
9 changes: 9 additions & 0 deletions src/common/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#else
#include <codecvt>
#endif
#include <iomanip>

#ifdef HAVE_MALLOPT_ARENA_MAX
#include <malloc.h>
Expand Down Expand Up @@ -100,6 +101,14 @@ int GetNumCores()
return std::thread::hardware_concurrency();
}

bool CheckHex(const std::string& str) {
size_t data=0;
if(str.size() > 2 && (str.compare(0, 2, "0x") == 0 || str.compare(0, 2, "0X") == 0)){
data=2;
}
return str.size() > data && str.find_first_not_of("0123456789abcdefABCDEF", data) == std::string::npos;
}

// Obtain the application startup time (used for uptime calculation)
int64_t GetStartupTime()
{
Expand Down
Loading

0 comments on commit 5408367

Please sign in to comment.