diff --git a/src/Makefile.test.include b/src/Makefile.test.include --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -124,6 +124,7 @@ test/undo_tests.cpp \ test/univalue_tests.cpp \ test/util_tests.cpp \ + test/validation_block_tests.cpp \ test/validation_tests.cpp \ test/work_comparator_tests.cpp \ rpc/test/server_tests.cpp diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -137,6 +137,7 @@ undo_tests.cpp univalue_tests.cpp util_tests.cpp + validation_block_tests.cpp validation_tests.cpp work_comparator_tests.cpp diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -152,4 +152,9 @@ return *this; } }; + +// define an implicit conversion here so that uint256 may be used directly in +// BOOST_CHECK_* +std::ostream &operator<<(std::ostream &os, const uint256 &num); + #endif diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -49,6 +49,11 @@ extern void noui_connect(); +std::ostream &operator<<(std::ostream &os, const uint256 &num) { + os << num.ToString(); + return os; +} + BasicTestingSetup::BasicTestingSetup(const std::string &chainName) { SHA256AutoDetect(); RandomInit(); diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp new file mode 100644 --- /dev/null +++ b/src/test/validation_block_tests.cpp @@ -0,0 +1,203 @@ +// Copyright (c) 2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct RegtestingSetup : public TestingSetup { + RegtestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {} +}; + +BOOST_FIXTURE_TEST_SUITE(validation_block_tests, RegtestingSetup) + +struct TestSubscriber : public CValidationInterface { + uint256 m_expected_tip; + + TestSubscriber(uint256 tip) : m_expected_tip(tip) {} + + void UpdatedBlockTip(const CBlockIndex *pindexNew, + const CBlockIndex *pindexFork, bool fInitialDownload) { + BOOST_CHECK_EQUAL(m_expected_tip, pindexNew->GetBlockHash()); + } + + void BlockConnected(const std::shared_ptr &block, + const CBlockIndex *pindex, + const std::vector &txnConflicted) { + BOOST_CHECK_EQUAL(m_expected_tip, block->hashPrevBlock); + BOOST_CHECK_EQUAL(m_expected_tip, pindex->pprev->GetBlockHash()); + + m_expected_tip = block->GetHash(); + } + + void BlockDisconnected(const std::shared_ptr &block) { + BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash()); + + m_expected_tip = block->hashPrevBlock; + } +}; + +std::shared_ptr Block(const Config &config, const uint256 &prev_hash) { + static int i = 0; + static uint64_t time = config.GetChainParams().GenesisBlock().nTime; + + CScript pubKey; + pubKey << i++ << OP_TRUE; + + auto ptemplate = BlockAssembler(config, g_mempool).CreateNewBlock(pubKey); + auto pblock = std::make_shared(ptemplate->block); + pblock->hashPrevBlock = prev_hash; + pblock->nTime = ++time; + + CMutableTransaction txCoinbase(*pblock->vtx[0]); + txCoinbase.vout.resize(1); + pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase)); + + return pblock; +} + +std::shared_ptr FinalizeBlock(const Config &config, + std::shared_ptr pblock) { + pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); + + while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, config)) { + ++(pblock->nNonce); + } + + return pblock; +} + +// construct a valid block +const std::shared_ptr GoodBlock(const Config &config, + const uint256 &prev_hash) { + return FinalizeBlock(config, Block(config, prev_hash)); +} + +// construct an invalid block (but with a valid header) +const std::shared_ptr BadBlock(const Config &config, + const uint256 &prev_hash) { + auto pblock = Block(config, prev_hash); + + CMutableTransaction coinbase_spend; + coinbase_spend.vin.push_back( + CTxIn(COutPoint(pblock->vtx[0]->GetHash(), 0), CScript(), 0)); + coinbase_spend.vout.push_back(pblock->vtx[0]->vout[0]); + + CTransactionRef tx = MakeTransactionRef(coinbase_spend); + pblock->vtx.push_back(tx); + + auto ret = FinalizeBlock(config, pblock); + return ret; +} + +void BuildChain(const Config &config, const uint256 &root, int height, + const unsigned int invalid_rate, const unsigned int branch_rate, + const unsigned int max_size, + std::vector> &blocks) { + if (height <= 0 || blocks.size() >= max_size) { + return; + } + + bool gen_invalid = GetRand(100) < invalid_rate; + bool gen_fork = GetRand(100) < branch_rate; + + const std::shared_ptr pblock = + gen_invalid ? BadBlock(config, root) : GoodBlock(config, root); + blocks.push_back(pblock); + if (!gen_invalid) { + BuildChain(config, pblock->GetHash(), height - 1, invalid_rate, + branch_rate, max_size, blocks); + } + + if (gen_fork) { + blocks.push_back(GoodBlock(config, root)); + BuildChain(config, blocks.back()->GetHash(), height - 1, invalid_rate, + branch_rate, max_size, blocks); + } +} + +BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering) { + GlobalConfig config; + const CChainParams &chainParams = config.GetChainParams(); + + // build a large-ish chain that's likely to have some forks + std::vector> blocks; + while (blocks.size() < 50) { + blocks.clear(); + BuildChain(config, chainParams.GenesisBlock().GetHash(), 100, 15, 10, + 500, blocks); + } + + bool ignored; + CValidationState state; + std::vector headers; + std::transform( + blocks.begin(), blocks.end(), std::back_inserter(headers), + [](std::shared_ptr b) { return b->GetBlockHeader(); }); + + // Process all the headers so we understand the toplogy of the chain + BOOST_CHECK(ProcessNewBlockHeaders(config, headers, state)); + + // Connect the genesis block and drain any outstanding events + ProcessNewBlock(config, + std::make_shared(chainParams.GenesisBlock()), true, + &ignored); + SyncWithValidationInterfaceQueue(); + + // subscribe to events (this subscriber will validate event ordering) + const CBlockIndex *initial_tip = nullptr; + { + LOCK(cs_main); + initial_tip = chainActive.Tip(); + } + TestSubscriber sub(initial_tip->GetBlockHash()); + RegisterValidationInterface(&sub); + + // create a bunch of threads that repeatedly process a block generated above + // at random this will create parallelism and randomness inside validation - + // the ValidationInterface will subscribe to events generated during block + // validation and assert on ordering invariance + boost::thread_group threads; + for (int i = 0; i < 10; i++) { + threads.create_thread([&config, &blocks]() { + bool tlignored; + for (int j = 0; j < 1000; j++) { + auto block = blocks[GetRand(blocks.size() - 1)]; + ProcessNewBlock(config, block, true, &tlignored); + } + + // to make sure that eventually we process the full chain - do it + // here + for (auto block : blocks) { + if (block->vtx.size() == 1) { + bool processed = + ProcessNewBlock(config, block, true, &tlignored); + assert(processed); + } + } + }); + } + + threads.join_all(); + while (GetMainSignals().CallbacksPending() > 0) { + MilliSleep(100); + } + + UnregisterValidationInterface(&sub); + + BOOST_CHECK_EQUAL(sub.m_expected_tip, chainActive.Tip()->GetBlockHash()); +} + +BOOST_AUTO_TEST_SUITE_END()