Skip to content

Commit

Permalink
merge bitcoin#15788: Unify testing setups for fuzz, bench, and unit t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
kwvg committed Oct 11, 2021
1 parent 919bdbe commit 92ff9c7
Show file tree
Hide file tree
Showing 105 changed files with 246 additions and 243 deletions.
4 changes: 3 additions & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ bench_bench_dash_SOURCES = \
bench/lockedpool.cpp \
bench/poly1305.cpp \
bench/prevector.cpp \
bench/string_cast.cpp
bench/string_cast.cpp \
test/util.cpp \
test/util.h

nodist_bench_bench_dash_SOURCES = $(GENERATED_BENCH_FILES)

Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.qttest.include
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ TEST_QT_H = \
qt/test/wallettests.h

TEST_BITCOIN_CPP = \
test/test_dash.cpp
test/setup_common.cpp

TEST_BITCOIN_H = \
test/test_dash.h
test/setup_common.h
qt_test_test_dash_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)

Expand Down
8 changes: 4 additions & 4 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.r

BITCOIN_TEST_SUITE = \
test/main.cpp \
test/test_dash.h \
test/test_dash.cpp
test/setup_common.h \
test/setup_common.cpp

FUZZ_SUITE = \
test/test_dash.h \
test/test_dash.cpp \
test/setup_common.h \
test/setup_common.cpp \
test/fuzz/fuzz.cpp \
test/fuzz/fuzz.h

Expand Down
13 changes: 13 additions & 0 deletions src/bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#include <bench/bench.h>

#include <chainparams.h>
#include <test/setup_common.h>
#include <validation.h>

#include <algorithm>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <regex>

namespace {
Expand Down Expand Up @@ -47,6 +53,13 @@ void benchmark::BenchRunner::RunAll(const Args& args)

std::vector<ankerl::nanobench::Result> benchmarkResults;
for (const auto& p : benchmarks()) {
TestingSetup test{CBaseChainParams::REGTEST};
{
assert(::chainActive.Height() == 0);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), Params().GetConsensus())};
assert(witness_enabled);
}

if (!std::regex_match(p.first, baseMatch, reFilter)) {
continue;
}
Expand Down
32 changes: 0 additions & 32 deletions src/bench/bench_dash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,18 @@
#include <stacktraces.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <validation.h>

#include <boost/lexical_cast.hpp>

#include <memory>

#include <bls/bls.h>

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;

static const char* DEFAULT_BENCH_FILTER = ".*";

void InitBLSTests();
void CleanupBLSTests();

static fs::path SetDataDir()
{
fs::path ret = fs::temp_directory_path() / "bench_dash" / fs::unique_path();
fs::create_directories(ret);
gArgs.ForceSetArg("-datadir", ret.string());
return ret;
}
static void SetupBenchArgs()
{
gArgs.AddArg("-?", "Print this help message and exit", false, OptionsCategory::OPTIONS);
Expand Down Expand Up @@ -72,21 +62,6 @@ int main(int argc, char** argv)
return EXIT_SUCCESS;
}

// Set the datadir after parsing the bench options
const fs::path bench_datadir{SetDataDir()};

SHA256AutoDetect();

RegisterPrettySignalHandlers();
RegisterPrettyTerminateHander();

ECC_Start();
ECCVerifyHandle verifyHandle;

BLSInit();
InitBLSTests();
SetupEnvironment();

benchmark::Args args;
args.regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
args.is_list_only = gArgs.GetBoolArg("-list", false);
Expand All @@ -96,12 +71,5 @@ int main(int argc, char** argv)

benchmark::BenchRunner::RunAll(args);

fs::remove_all(bench_datadir);

// need to be called before global destructors kick in (PoolAllocator is needed due to many BLSSecretKeys)
CleanupBLSTests();

ECC_Stop();

return EXIT_SUCCESS;
}
73 changes: 1 addition & 72 deletions src/bench/block_assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,16 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <crypto/sha256.h>
#include <miner.h>
#include <policy/policy.h>
#include <pow.h>
#include <scheduler.h>
#include <txdb.h>
#include <test/util.h>
#include <txmempool.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>

#include <boost/thread.hpp>

#include <list>
#include <vector>

static std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
{
auto block = std::make_shared<CBlock>(
BlockAssembler{Params()}
.CreateNewBlock(coinbase_scriptPubKey, /* fMineWitnessTx */ true)
->block);

block->nTime = ::chainActive.Tip()->GetMedianTimePast() + 1;
block->hashMerkleRoot = BlockMerkleRoot(*block);

return block;
}


static CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
{
auto block = PrepareBlock(coinbase_scriptPubKey);

while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
++block->nNonce;
assert(block->nNonce);
}

bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
assert(processed);

return CTxIn{block->vtx[0]->GetHash(), 0};
}


static void AssembleBlock(benchmark::State& state)
{
const std::vector<unsigned char> op_true{OP_TRUE};
Expand All @@ -64,32 +24,6 @@ static void AssembleBlock(benchmark::State& state)

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

// Switch to regtest so we can mine faster
// Also segwit is active, so we can include witness transactions
SelectParams(CBaseChainParams::REGTEST);

InitScriptExecutionCache();

boost::thread_group thread_group;
CScheduler scheduler;
{
LOCK(cs_main);
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
}
{
const CChainParams& chainparams = Params();
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
LoadGenesisBlock(chainparams);
CValidationState state;
ActivateBestChain(state, chainparams);
assert(::chainActive.Tip() != nullptr);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
assert(witness_enabled);
}

// 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;
Expand All @@ -114,11 +48,6 @@ static void AssembleBlock(benchmark::State& state)
while (state.KeepRunning()) {
PrepareBlock(SCRIPT_PUB);
}

thread_group.interrupt_all();
thread_group.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
}

BENCHMARK(AssembleBlock, 700);
35 changes: 1 addition & 34 deletions src/bench/duplicate_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
#include <miner.h>
#include <policy/policy.h>
#include <pow.h>
#include <scheduler.h>
#include <txdb.h>
#include <test/util.h>
#include <txmempool.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>

#include <boost/thread.hpp>

#include <list>
#include <vector>

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

// Switch to regtest so we can mine faster
// Also segwit is active, so we can include witness transactions
SelectParams(CBaseChainParams::REGTEST);

InitScriptExecutionCache();

boost::thread_group thread_group;
CScheduler scheduler;
const CChainParams& chainparams = Params();
{
LOCK(cs_main);
::pblocktree.reset(new CBlockTreeDB(1 << 20, true));
::pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
::pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
}
{
thread_group.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
LoadGenesisBlock(chainparams);
CValidationState cvstate;
ActivateBestChain(cvstate, chainparams);
assert(::chainActive.Tip() != nullptr);
const bool witness_enabled{IsWitnessEnabled(::chainActive.Tip(), chainparams.GetConsensus())};
assert(witness_enabled);
}

CBlock block{};
CMutableTransaction coinbaseTx{};
Expand Down Expand Up @@ -92,11 +64,6 @@ static void DuplicateInputs(benchmark::State& state)
assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
}

thread_group.interrupt_all();
thread_group.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
}

BENCHMARK(DuplicateInputs, 10);
2 changes: 1 addition & 1 deletion src/qt/test/addressbooktests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <qt/test/addressbooktests.h>
#include <qt/test/util.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

#include <interfaces/node.h>
#include <qt/addressbookpage.h>
Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/rpcnestedtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <rpc/register.h>
#include <rpc/server.h>
#include <qt/rpcconsole.h>
#include <test/test_dash.h>
#include <test/setup_common.h>
#include <univalue.h>
#include <util/system.h>

Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/wallettests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <qt/transactionview.h>
#include <qt/walletmodel.h>
#include <key_io.h>
#include <test/test_dash.h>
#include <test/setup_common.h>
#include <validation.h>
#include <wallet/wallet.h>
#include <qt/overviewpage.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ unit tests as possible).

The build system is setup to compile an executable called `test_dash`
that runs all of the unit tests. The main source file is called
test_dash.cpp. To add a new unit test file to our test suite you need
setup_common.cpp. To add a new unit test file to our test suite you need
to add the file to `src/Makefile.test.include`. The pattern is to create
one test file for each class or source file for which you want to create
unit tests. The file naming convention is `<source_filename>_tests.cpp`
Expand Down
2 changes: 1 addition & 1 deletion src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addrman.h>
#include <test/test_dash.h>
#include <test/setup_common.h>
#include <string>
#include <boost/test/unit_test.hpp>
#include <util/asmap.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/allocator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <util/system.h>

#include <support/allocators/secure.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

#include <memory>

Expand Down
2 changes: 1 addition & 1 deletion src/test/amount_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <amount.h>
#include <policy/feerate.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

#include <boost/test/unit_test.hpp>

Expand Down
2 changes: 1 addition & 1 deletion src/test/arith_uint256_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <arith_uint256.h>
#include <string>
#include <version.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup)

Expand Down
2 changes: 1 addition & 1 deletion src/test/base32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/strencodings.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

#include <boost/test/unit_test.hpp>

Expand Down
2 changes: 1 addition & 1 deletion src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <test/data/base58_encode_decode.json.h>

#include <base58.h>
#include <test/test_dash.h>
#include <test/setup_common.h>
#include <util/strencodings.h>

#include <univalue.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/base64_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/strencodings.h>
#include <test/test_dash.h>
#include <test/setup_common.h>

#include <boost/test/unit_test.hpp>

Expand Down
Loading

0 comments on commit 92ff9c7

Please sign in to comment.