Changeset View
Changeset View
Standalone View
Standalone View
src/test/validation_block_tests.cpp
Show First 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering) { | ||||
bool ignored; | bool ignored; | ||||
BlockValidationState state; | BlockValidationState state; | ||||
std::vector<CBlockHeader> headers; | std::vector<CBlockHeader> headers; | ||||
std::transform( | std::transform( | ||||
blocks.begin(), blocks.end(), std::back_inserter(headers), | blocks.begin(), blocks.end(), std::back_inserter(headers), | ||||
[](std::shared_ptr<const CBlock> b) { return b->GetBlockHeader(); }); | [](std::shared_ptr<const CBlock> b) { return b->GetBlockHeader(); }); | ||||
// Process all the headers so we understand the toplogy of the chain | // Process all the headers so we understand the toplogy of the chain | ||||
BOOST_CHECK(Assert(m_node.chainman) | BOOST_CHECK( | ||||
->ProcessNewBlockHeaders(config, headers, true, state)); | Assert(m_node.chainman)->ProcessNewBlockHeaders(headers, true, state)); | ||||
// Connect the genesis block and drain any outstanding events | // Connect the genesis block and drain any outstanding events | ||||
BOOST_CHECK(Assert(m_node.chainman) | BOOST_CHECK(Assert(m_node.chainman) | ||||
->ProcessNewBlock( | ->ProcessNewBlock( | ||||
config, | |||||
std::make_shared<CBlock>(chainParams.GenesisBlock()), | std::make_shared<CBlock>(chainParams.GenesisBlock()), | ||||
true, true, &ignored)); | true, true, &ignored)); | ||||
SyncWithValidationInterfaceQueue(); | SyncWithValidationInterfaceQueue(); | ||||
// subscribe to events (this subscriber will validate event ordering) | // subscribe to events (this subscriber will validate event ordering) | ||||
const CBlockIndex *initial_tip = nullptr; | const CBlockIndex *initial_tip = nullptr; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
Show All 9 Lines | BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering) { | ||||
std::vector<std::thread> threads; | std::vector<std::thread> threads; | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
threads.emplace_back([&]() { | threads.emplace_back([&]() { | ||||
bool tlignored; | bool tlignored; | ||||
FastRandomContext insecure; | FastRandomContext insecure; | ||||
for (int j = 0; j < 1000; j++) { | for (int j = 0; j < 1000; j++) { | ||||
auto block = blocks[insecure.randrange(blocks.size() - 1)]; | auto block = blocks[insecure.randrange(blocks.size() - 1)]; | ||||
Assert(m_node.chainman) | Assert(m_node.chainman) | ||||
->ProcessNewBlock(config, block, true, true, &tlignored); | ->ProcessNewBlock(block, true, true, &tlignored); | ||||
} | } | ||||
// to make sure that eventually we process the full chain - do it | // to make sure that eventually we process the full chain - do it | ||||
// here | // here | ||||
for (auto block : blocks) { | for (auto block : blocks) { | ||||
if (block->vtx.size() == 1) { | if (block->vtx.size() == 1) { | ||||
bool processed = Assert(m_node.chainman) | bool processed = | ||||
->ProcessNewBlock(config, block, true, | Assert(m_node.chainman) | ||||
true, &tlignored); | ->ProcessNewBlock(block, true, true, &tlignored); | ||||
assert(processed); | assert(processed); | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
for (auto &t : threads) { | for (auto &t : threads) { | ||||
t.join(); | t.join(); | ||||
Show All 14 Lines | |||||
BOOST_AUTO_TEST_CASE(avalanche_finalization_bad_state) { | BOOST_AUTO_TEST_CASE(avalanche_finalization_bad_state) { | ||||
GlobalConfig config; | GlobalConfig config; | ||||
const CChainParams &chainParams = config.GetChainParams(); | const CChainParams &chainParams = config.GetChainParams(); | ||||
ChainstateManager &chainman = *Assert(m_node.chainman); | ChainstateManager &chainman = *Assert(m_node.chainman); | ||||
// Connect the genesis block | // Connect the genesis block | ||||
bool newBlock; | bool newBlock; | ||||
BOOST_CHECK(chainman.ProcessNewBlock( | BOOST_CHECK(chainman.ProcessNewBlock( | ||||
config, std::make_shared<CBlock>(chainParams.GenesisBlock()), true, | std::make_shared<CBlock>(chainParams.GenesisBlock()), true, true, | ||||
true, &newBlock)); | &newBlock)); | ||||
// Generate an invalid block with a valid header | // Generate an invalid block with a valid header | ||||
const std::shared_ptr<const CBlock> pblock = | const std::shared_ptr<const CBlock> pblock = | ||||
BadBlock(config, chainParams.GenesisBlock().GetHash()); | BadBlock(config, chainParams.GenesisBlock().GetHash()); | ||||
// Process the valid header | // Process the valid header | ||||
const CBlockIndex *pindexBadBlock; | const CBlockIndex *pindexBadBlock; | ||||
BlockValidationState state; | BlockValidationState state; | ||||
BOOST_CHECK(chainman.ProcessNewBlockHeaders( | BOOST_CHECK(chainman.ProcessNewBlockHeaders({pblock->GetBlockHeader()}, | ||||
config, {pblock->GetBlockHeader()}, true, state, &pindexBadBlock)); | true, state, &pindexBadBlock)); | ||||
// In order to force the invalid block to be finalized, we set the chain | // In order to force the invalid block to be finalized, we set the chain | ||||
// tip manually. This does not happen under normal conditions. Rewind | // tip manually. This does not happen under normal conditions. Rewind | ||||
// the chain tip immediately after finalizing. | // the chain tip immediately after finalizing. | ||||
CBlockIndex *pindex = const_cast<CBlockIndex *>(pindexBadBlock); | CBlockIndex *pindex = const_cast<CBlockIndex *>(pindexBadBlock); | ||||
Chainstate &activeChainstate = chainman.ActiveChainstate(); | Chainstate &activeChainstate = chainman.ActiveChainstate(); | ||||
// Set the tip to pindex because AvalancheFinalizeBlock checks it is in the | // Set the tip to pindex because AvalancheFinalizeBlock checks it is in the | ||||
// active chain. | // active chain. | ||||
activeChainstate.m_chain.SetTip(pindex); | activeChainstate.m_chain.SetTip(pindex); | ||||
BOOST_CHECK(activeChainstate.AvalancheFinalizeBlock(pindex)); | BOOST_CHECK(activeChainstate.AvalancheFinalizeBlock(pindex)); | ||||
activeChainstate.m_chain.SetTip(pindex->pprev); | activeChainstate.m_chain.SetTip(pindex->pprev); | ||||
// Process the block. It should be found invalid and finalization reverted. | // Process the block. It should be found invalid and finalization reverted. | ||||
bool processed = | bool processed = chainman.ProcessNewBlock(pblock, true, true, &newBlock); | ||||
chainman.ProcessNewBlock(config, pblock, true, true, &newBlock); | |||||
assert(processed); | assert(processed); | ||||
BOOST_CHECK(newBlock); | BOOST_CHECK(newBlock); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
BOOST_CHECK(pindex->nStatus.isInvalid()); | BOOST_CHECK(pindex->nStatus.isInvalid()); | ||||
} | } | ||||
BOOST_CHECK(!activeChainstate.IsBlockAvalancheFinalized(pindex)); | BOOST_CHECK(!activeChainstate.IsBlockAvalancheFinalized(pindex)); | ||||
BOOST_CHECK(activeChainstate.IsBlockAvalancheFinalized(pindex->pprev)); | BOOST_CHECK(activeChainstate.IsBlockAvalancheFinalized(pindex->pprev)); | ||||
Show All 19 Lines | |||||
*/ | */ | ||||
BOOST_AUTO_TEST_CASE(mempool_locks_reorg) { | BOOST_AUTO_TEST_CASE(mempool_locks_reorg) { | ||||
GlobalConfig config; | GlobalConfig config; | ||||
const CChainParams &chainParams = config.GetChainParams(); | const CChainParams &chainParams = config.GetChainParams(); | ||||
bool ignored; | bool ignored; | ||||
auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool { | auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool { | ||||
return Assert(m_node.chainman) | return Assert(m_node.chainman) | ||||
->ProcessNewBlock(config, block, /*force_processing=*/true, | ->ProcessNewBlock(block, /*force_processing=*/true, | ||||
/*min_pow_checked=*/true, | /*min_pow_checked=*/true, | ||||
/*new_block=*/&ignored); | /*new_block=*/&ignored); | ||||
}; | }; | ||||
// Process all mined blocks | // Process all mined blocks | ||||
BOOST_REQUIRE( | BOOST_REQUIRE( | ||||
ProcessBlock(std::make_shared<CBlock>(chainParams.GenesisBlock()))); | ProcessBlock(std::make_shared<CBlock>(chainParams.GenesisBlock()))); | ||||
auto last_mined = GoodBlock(config, chainParams.GenesisBlock().GetHash()); | auto last_mined = GoodBlock(config, chainParams.GenesisBlock().GetHash()); | ||||
▲ Show 20 Lines • Show All 114 Lines • Show Last 20 Lines |