Skip to content

Commit 78295e9

Browse files
author
MarcoFalke
committed
Merge #15788: test: Unify testing setups for fuzz, bench, and unit tests
faf4000 scripted-diff: Bump copyright headers in test, bench (MarcoFalke) fa82190 scripted-diff: Rename test_bitcoin to test/setup_common (MarcoFalke) fa8685d test: Use test_bitcoin setup in bench, Add test utils (MarcoFalke) 666696b test: Have segwit always active in (Basic)TestingSetup (MarcoFalke) Pull request description: Now that the fuzz tests can use the BasicTestingSetup [1], do the same for bench. Also move some duplicate code to a common "test/util" module. [1]: fuzz: Link BasicTestingSetup (shared with unit tests) #15504 ACKs for commit faf400: jonatack: ACK bitcoin/bitcoin@faf4000 Tree-SHA512: 8ac5692e72cf50e460958f291643ae6b8bb04d5c1331ed50dce9eb4e9457e5a925144c532c42b360a26707e11eeece74aab27db8c76ab9a429b9dd7167e7cdc4
2 parents 2209b3b + faf4000 commit 78295e9

File tree

94 files changed

+316
-297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+316
-297
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\setup_common.h" />
26+
<ClCompile Include="..\..\src\test\setup_common.cpp" />
2327
<ClCompile Include="..\..\src\bench\base58.cpp" />
2428
<ClCompile Include="..\..\src\bench\bech32.cpp" />
2529
<ClCompile Include="..\..\src\bench\bench.cpp" />

build_msvc/test_bitcoin/test_bitcoin.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ClCompile Include="..\..\src\test\*_properties.cpp" />
2525
<ClCompile Include="..\..\src\test\gen\*_gen.cpp" />
2626
<ClCompile Include="..\..\src\wallet\test\*_tests.cpp" />
27-
<ClCompile Include="..\..\src\test\test_bitcoin.cpp" />
27+
<ClCompile Include="..\..\src\test\setup_common.cpp" />
2828
<ClCompile Include="..\..\src\test\main.cpp" />
2929
<ClCompile Include="..\..\src\wallet\test\*_fixture.cpp" />
3030
</ItemGroup>

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/setup_common.h \
37+
test/setup_common.cpp \
38+
test/util.h \
39+
test/util.cpp
3640

3741
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)
3842

src/Makefile.qttest.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ TEST_QT_H = \
3333
qt/test/wallettests.h
3434

3535
TEST_BITCOIN_CPP = \
36-
test/test_bitcoin.cpp
36+
test/setup_common.cpp
3737

3838
TEST_BITCOIN_H = \
39-
test/test_bitcoin.h
39+
test/setup_common.h
4040

4141
qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
4242
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)

src/Makefile.test.include

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.r
5252

5353
BITCOIN_TEST_SUITE = \
5454
test/main.cpp \
55-
test/test_bitcoin.h \
56-
test/test_bitcoin.cpp
55+
test/setup_common.h \
56+
test/setup_common.cpp
5757

5858
FUZZ_SUITE = \
59-
test/test_bitcoin.h \
60-
test/test_bitcoin.cpp \
59+
test/setup_common.h \
60+
test/setup_common.cpp \
6161
test/fuzz/fuzz.cpp \
6262
test/fuzz/fuzz.h
6363

src/bench/bench.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
// Copyright (c) 2015-2018 The Bitcoin Core developers
1+
// Copyright (c) 2015-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
66

7+
#include <chainparams.h>
8+
#include <test/setup_common.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: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
// Copyright (c) 2015-2018 The Bitcoin Core developers
1+
// Copyright (c) 2015-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
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: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,18 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
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: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -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/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <qt/test/addressbooktests.h>
22
#include <qt/test/util.h>
3-
#include <test/test_bitcoin.h>
3+
#include <test/setup_common.h>
44

55
#include <interfaces/chain.h>
66
#include <interfaces/node.h>

0 commit comments

Comments
 (0)