Skip to content

Commit fa8685d

Browse files
author
MarcoFalke
committed
test: Use test_bitcoin setup in bench, Add test utils
1 parent 666696b commit fa8685d

File tree

9 files changed

+151
-134
lines changed

9 files changed

+151
-134
lines changed

build_msvc/bench_bitcoin/bench_bitcoin.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
</ProjectConfiguration>
2121
</ItemGroup>
2222
<ItemGroup>
23+
<ClCompile Include="..\..\src\test\util.h" />
24+
<ClCompile Include="..\..\src\test\util.cpp" />
25+
<ClCompile Include="..\..\src\test\test_bitcoin.h" />
26+
<ClCompile Include="..\..\src\test\test_bitcoin.cpp" />
2327
<ClCompile Include="..\..\src\bench\base58.cpp" />
2428
<ClCompile Include="..\..\src\bench\bech32.cpp" />
2529
<ClCompile Include="..\..\src\bench\bench.cpp" />

src/Makefile.bench.include

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ bench_bench_bitcoin_SOURCES = \
3232
bench/bech32.cpp \
3333
bench/lockedpool.cpp \
3434
bench/poly1305.cpp \
35-
bench/prevector.cpp
35+
bench/prevector.cpp \
36+
test/test_bitcoin.h \
37+
test/test_bitcoin.cpp \
38+
test/util.h \
39+
test/util.cpp
3640

3741
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)
3842

src/bench/bench.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
#include <bench/bench.h>
66

7+
#include <chainparams.h>
8+
#include <test/test_bitcoin.h>
9+
#include <validation.h>
10+
11+
#include <algorithm>
712
#include <assert.h>
8-
#include <iostream>
913
#include <iomanip>
10-
#include <algorithm>
11-
#include <regex>
14+
#include <iostream>
1215
#include <numeric>
16+
#include <regex>
1317

1418
void benchmark::ConsolePrinter::header()
1519
{
@@ -108,6 +112,13 @@ void benchmark::BenchRunner::RunAll(Printer& printer, uint64_t num_evals, double
108112
printer.header();
109113

110114
for (const auto& p : benchmarks()) {
115+
TestingSetup test{CBaseChainParams::REGTEST};
116+
{
117+
assert(::chainActive.Height() == 0);
118+
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), Params().GetConsensus())};
119+
assert(witness_enabled);
120+
}
121+
111122
if (!std::regex_match(p.first, baseMatch, reFilter)) {
112123
continue;
113124
}

src/bench/bench_bitcoin.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
#include <crypto/sha256.h>
88
#include <key.h>
9-
#include <util/system.h>
109
#include <util/strencodings.h>
11-
#include <validation.h>
10+
#include <util/system.h>
1211

1312
#include <memory>
1413

15-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
16-
1714
static const int64_t DEFAULT_BENCH_EVALUATIONS = 5;
1815
static const char* DEFAULT_BENCH_FILTER = ".*";
1916
static const char* DEFAULT_BENCH_SCALING = "1.0";
@@ -36,14 +33,6 @@ static void SetupBenchArgs()
3633
gArgs.AddArg("-plot-height=<x>", strprintf("Plot height in pixel (default: %u)", DEFAULT_PLOT_HEIGHT), false, OptionsCategory::OPTIONS);
3734
}
3835

39-
static fs::path SetDataDir()
40-
{
41-
fs::path ret = fs::temp_directory_path() / "bench_bitcoin" / fs::unique_path();
42-
fs::create_directories(ret);
43-
gArgs.ForceSetArg("-datadir", ret.string());
44-
return ret;
45-
}
46-
4736
int main(int argc, char** argv)
4837
{
4938
SetupBenchArgs();
@@ -59,13 +48,6 @@ int main(int argc, char** argv)
5948
return EXIT_SUCCESS;
6049
}
6150

62-
// Set the datadir after parsing the bench options
63-
const fs::path bench_datadir{SetDataDir()};
64-
65-
SHA256AutoDetect();
66-
ECC_Start();
67-
SetupEnvironment();
68-
6951
int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
7052
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
7153
std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
@@ -88,9 +70,5 @@ int main(int argc, char** argv)
8870

8971
benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
9072

91-
fs::remove_all(bench_datadir);
92-
93-
ECC_Stop();
94-
9573
return EXIT_SUCCESS;
9674
}

src/bench/block_assemble.cpp

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,16 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <chainparams.h>
7-
#include <coins.h>
8-
#include <consensus/merkle.h>
96
#include <consensus/validation.h>
107
#include <crypto/sha256.h>
11-
#include <miner.h>
12-
#include <policy/policy.h>
13-
#include <pow.h>
14-
#include <scheduler.h>
15-
#include <txdb.h>
8+
#include <test/util.h>
169
#include <txmempool.h>
17-
#include <util/time.h>
1810
#include <validation.h>
19-
#include <validationinterface.h>
2011

21-
#include <boost/thread.hpp>
2212

2313
#include <list>
2414
#include <vector>
2515

26-
static std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
27-
{
28-
auto block = std::make_shared<CBlock>(
29-
BlockAssembler{Params()}
30-
.CreateNewBlock(coinbase_scriptPubKey)
31-
->block);
32-
33-
block->nTime = ::chainActive.Tip()->GetMedianTimePast() + 1;
34-
block->hashMerkleRoot = BlockMerkleRoot(*block);
35-
36-
return block;
37-
}
38-
39-
40-
static CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
41-
{
42-
auto block = PrepareBlock(coinbase_scriptPubKey);
43-
44-
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
45-
++block->nNonce;
46-
assert(block->nNonce);
47-
}
48-
49-
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
50-
assert(processed);
51-
52-
return CTxIn{block->vtx[0]->GetHash(), 0};
53-
}
54-
55-
5616
static void AssembleBlock(benchmark::State& state)
5717
{
5818
const std::vector<unsigned char> op_true{OP_TRUE};
@@ -64,32 +24,6 @@ static void AssembleBlock(benchmark::State& state)
6424

6525
const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};
6626

67-
// Switch to regtest so we can mine faster
68-
// Also segwit is active, so we can include witness transactions
69-
SelectParams(CBaseChainParams::REGTEST);
70-
71-
InitScriptExecutionCache();
72-
73-
boost::thread_group thread_group;
74-
CScheduler scheduler;
75-
{
76-
LOCK(cs_main);
77-
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
78-
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
79-
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
80-
}
81-
{
82-
const CChainParams& chainparams = Params();
83-
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
84-
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
85-
LoadGenesisBlock(chainparams);
86-
CValidationState state;
87-
ActivateBestChain(state, chainparams);
88-
assert(::chainActive.Tip() != nullptr);
89-
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
90-
assert(witness_enabled);
91-
}
92-
9327
// Collect some loose transactions that spend the coinbases of our mined blocks
9428
constexpr size_t NUM_BLOCKS{200};
9529
std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
@@ -114,11 +48,6 @@ static void AssembleBlock(benchmark::State& state)
11448
while (state.KeepRunning()) {
11549
PrepareBlock(SCRIPT_PUB);
11650
}
117-
118-
thread_group.interrupt_all();
119-
thread_group.join_all();
120-
GetMainSignals().FlushBackgroundCallbacks();
121-
GetMainSignals().UnregisterBackgroundSignalScheduler();
12251
}
12352

12453
BENCHMARK(AssembleBlock, 700);

src/bench/duplicate_inputs.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
#include <miner.h>
1111
#include <policy/policy.h>
1212
#include <pow.h>
13-
#include <scheduler.h>
14-
#include <txdb.h>
13+
#include <test/util.h>
1514
#include <txmempool.h>
16-
#include <util/time.h>
1715
#include <validation.h>
1816
#include <validationinterface.h>
1917

20-
#include <boost/thread.hpp>
21-
2218
#include <list>
2319
#include <vector>
2420

@@ -27,31 +23,7 @@ static void DuplicateInputs(benchmark::State& state)
2723
{
2824
const CScript SCRIPT_PUB{CScript(OP_TRUE)};
2925

30-
// Switch to regtest so we can mine faster
31-
// Also segwit is active, so we can include witness transactions
32-
SelectParams(CBaseChainParams::REGTEST);
33-
34-
InitScriptExecutionCache();
35-
36-
boost::thread_group thread_group;
37-
CScheduler scheduler;
3826
const CChainParams& chainparams = Params();
39-
{
40-
LOCK(cs_main);
41-
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
42-
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
43-
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
44-
}
45-
{
46-
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
47-
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
48-
LoadGenesisBlock(chainparams);
49-
CValidationState cvstate;
50-
ActivateBestChain(cvstate, chainparams);
51-
assert(::chainActive.Tip() != nullptr);
52-
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
53-
assert(witness_enabled);
54-
}
5527

5628
CBlock block{};
5729
CMutableTransaction coinbaseTx{};
@@ -92,11 +64,6 @@ static void DuplicateInputs(benchmark::State& state)
9264
assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
9365
assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
9466
}
95-
96-
thread_group.interrupt_all();
97-
thread_group.join_all();
98-
GetMainSignals().FlushBackgroundCallbacks();
99-
GetMainSignals().UnregisterBackgroundSignalScheduler();
10067
}
10168

10269
BENCHMARK(DuplicateInputs, 10);

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
3333
}
3434

3535
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
36-
: m_path_root(fs::temp_directory_path() / "test_bitcoin" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
36+
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
3737
{
3838
SHA256AutoDetect();
3939
ECC_Start();

src/test/util.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util.h>
6+
7+
#include <chainparams.h>
8+
#include <consensus/merkle.h>
9+
#include <consensus/validation.h>
10+
#include <key_io.h>
11+
#include <miner.h>
12+
#include <outputtype.h>
13+
#include <pow.h>
14+
#include <scheduler.h>
15+
#include <script/standard.h>
16+
#include <txdb.h>
17+
#include <validation.h>
18+
#include <validationinterface.h>
19+
#ifdef ENABLE_WALLET
20+
#include <wallet/wallet.h>
21+
#endif
22+
23+
#include <boost/thread.hpp>
24+
25+
#ifdef ENABLE_WALLET
26+
std::string getnewaddress(CWallet& w)
27+
{
28+
constexpr auto output_type = OutputType::BECH32;
29+
30+
CPubKey new_key;
31+
if (!w.GetKeyFromPool(new_key)) assert(false);
32+
33+
w.LearnRelatedScripts(new_key, output_type);
34+
const auto dest = GetDestinationForKey(new_key, output_type);
35+
36+
w.SetAddressBook(dest, /* label */ "", "receive");
37+
38+
return EncodeDestination(dest);
39+
}
40+
41+
void importaddress(CWallet& wallet, const std::string& address)
42+
{
43+
LOCK(wallet.cs_wallet);
44+
const auto dest = DecodeDestination(address);
45+
assert(IsValidDestination(dest));
46+
const auto script = GetScriptForDestination(dest);
47+
wallet.MarkDirty();
48+
assert(!wallet.HaveWatchOnly(script));
49+
if (!wallet.AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
50+
wallet.SetAddressBook(dest, /* label */ "", "receive");
51+
}
52+
#endif // ENABLE_WALLET
53+
54+
CTxIn generatetoaddress(const std::string& address)
55+
{
56+
const auto dest = DecodeDestination(address);
57+
assert(IsValidDestination(dest));
58+
const auto coinbase_script = GetScriptForDestination(dest);
59+
60+
return MineBlock(coinbase_script);
61+
}
62+
63+
CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
64+
{
65+
auto block = PrepareBlock(coinbase_scriptPubKey);
66+
67+
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
68+
++block->nNonce;
69+
assert(block->nNonce);
70+
}
71+
72+
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
73+
assert(processed);
74+
75+
return CTxIn{block->vtx[0]->GetHash(), 0};
76+
}
77+
78+
79+
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
80+
{
81+
auto block = std::make_shared<CBlock>(
82+
BlockAssembler{Params()}
83+
.CreateNewBlock(coinbase_scriptPubKey)
84+
->block);
85+
86+
block->nTime = ::chainActive.Tip()->GetMedianTimePast() + 1;
87+
block->hashMerkleRoot = BlockMerkleRoot(*block);
88+
89+
return block;
90+
}

0 commit comments

Comments
 (0)