src/test/validation_block_tests.cpp
BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
{
    bool ignored;
    auto ProcessBlock = [&ignored](std::shared_ptr<const CBlock> block) -> bool {
        return ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
    };

    // Process all mined blocks
    BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(Params().GenesisBlock())));
    auto last_mined = GoodBlock(Params().GenesisBlock().GetHash());
    BOOST_REQUIRE(ProcessBlock(last_mined));

    // Run the test multiple times
    for (int test_runs = 3; test_runs > 0; --test_runs) {
        BOOST_CHECK_EQUAL(last_mined->GetHash(), ::ChainActive().Tip()->GetBlockHash());

        // Later on split from here
        const uint256 split_hash{last_mined->hashPrevBlock};

        // Create a bunch of transactions to spend the miner rewards of the
        // most recent blocks
        std::vector<CTransactionRef> txs;
        for (int num_txs = 22; num_txs > 0; --num_txs) {
            CMutableTransaction mtx;
            mtx.vin.push_back(CTxIn{COutPoint{last_mined->vtx[0]->GetHash(), 1}, CScript{}});
            mtx.vin[0].scriptWitness.stack.push_back(V_OP_TRUE);
            mtx.vout.push_back(last_mined->vtx[0]->vout[1]);
            mtx.vout[0].nValue -= 40000;
            txs.push_back(MakeTransactionRef(mtx));

            last_mined = GoodBlock(last_mined->GetHash());
            BOOST_REQUIRE(ProcessBlock(last_mined));
        }

        // Mature the inputs of the txs
        int coinbaseMaturity = Params().GetConsensus().CoinbaseMaturity(0);
        for (int j = coinbaseMaturity; j > 0; --j) {
            last_mined = GoodBlock(last_mined->GetHash());
            BOOST_REQUIRE(ProcessBlock(last_mined));
        }

        // Mine a reorg (and hold it back) before adding the txs to the mempool
        const uint256 tip_init{last_mined->GetHash()};

        std::vector<std::shared_ptr<const CBlock>> reorg;
        last_mined = GoodBlock(split_hash);
        reorg.push_back(last_mined);
        for (size_t j = coinbaseMaturity + txs.size() + 1; j > 0; --j) {
            last_mined = GoodBlock(last_mined->GetHash());
            reorg.push_back(last_mined);
        }

        // Add the txs to the tx pool
        {
            LOCK(cs_main);
            TxValidationState state;
            std::list<CTransactionRef> plTxnReplaced;
            for (const auto& tx : txs) {
                BOOST_REQUIRE(AcceptToMemoryPool(
                    *m_node.mempool,
                    state,
                    tx,
                    &plTxnReplaced,
                    /* bypass_limits */ false,
                    /* nAbsurdFee */ 0));
            }
        }

        // Check that all txs are in the pool
        {
            LOCK(m_node.mempool->cs);
            BOOST_CHECK_EQUAL(m_node.mempool->mapTx.size(), txs.size());
        }

        // Run a thread that simulates an RPC caller that is polling while
        // validation is doing a reorg
        std::thread rpc_thread{[&]() {
            // This thread is checking that the mempool either contains all of
            // the transactions invalidated by the reorg, or none of them, and
            // not some intermediate amount.
            while (true) {
                LOCK(m_node.mempool->cs);
                if (m_node.mempool->mapTx.size() == 0) {
                    // We are done with the reorg
                    break;
                }
                // Internally, we might be in the middle of the reorg, but
                // externally the reorg to the most-proof-of-work chain should
                // be atomic. So the caller assumes that the returned mempool
                // is consistent. That is, it has all txs that were there
                // before the reorg.
                assert(m_node.mempool->mapTx.size() == txs.size());
                continue;
            }
            LOCK(cs_main);
            // We are done with the reorg, so the tip must have changed
            assert(tip_init != ::ChainActive().Tip()->GetBlockHash());
        }};

        // Submit the reorg in this thread to invalidate and remove the txs from the tx pool
        for (const auto& b : reorg) {
            ProcessBlock(b);
        }
        // Check that the reorg was eventually successful
        BOOST_CHECK_EQUAL(last_mined->GetHash(), ::ChainActive().Tip()->GetBlockHash());

        // We can join the other thread, which returns when the reorg was successful
        rpc_thread.join();
    }
}
/**
 * Test that mempool updates happen atomically with reorgs.
 *
 * This prevents RPC clients, among others, from retrieving immediately-out-of-date mempool data
 * during large reorgs.
 *
 * The test verifies this by creating a chain of `num_txs` blocks, matures their coinbases, and then
 * submits txns spending from their coinbase to the mempool. A fork chain is then processed,
 * invalidating the txns and evicting them from the mempool.
 *
 * We verify that the mempool updates atomically by polling it continuously
 * from another thread during the reorg and checking that its size only changes
 * once. The size changing exactly once indicates that the polling thread's
 * view of the mempool is either consistent with the chain state before reorg,
 * or consistent with the chain state after the reorg, and not just consistent
 * with some intermediate state during the reorg.
 */
Test that mempool updates happen atomically with reorgs.
This prevents RPC clients, among others, from retrieving immediately-out-of-date mempool data
during large reorgs.

The test verifies this by creating a chain of `num_txs` blocks, matures their coinbases, and then
submits txns spending from their coinbase to the mempool. A fork chain is then processed,
invalidating the txns and evicting them from the mempool.

We verify that the mempool updates atomically by polling it continuously
from another thread during the reorg and checking that its size only changes
once. The size changing exactly once indicates that the polling thread's
view of the mempool is either consistent with the chain state before reorg,
or consistent with the chain state after the reorg, and not just consistent
with some intermediate state during the reorg.
Test that mempool updates happen atomically with reorgs . This prevents RPC clients among others from retrieving immediately - out - of - date mempool data during large reorgs . The test verifies this by creating a chain of ` num_txs ` blocks matures their coinbases and then submits txns spending from their coinbase to the mempool . A fork chain is then processed invalidating the txns and evicting them from the mempool . We verify that the mempool updates atomically by polling it continuously from another thread during the reorg and checking that its size only changes once . The size changing exactly once indicates that the polling thread ' s view of the mempool is either consistent with the chain state before reorg or consistent with the chain state after the reorg and not just consistent with some intermediate state during the reorg .
BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
{
    bool ignored;
    auto ProcessBlock = [&ignored](std::shared_ptr<const CBlock> block) -> bool {
        return ProcessNewBlock(Params(), block,  true,  &ignored);
    };
    BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(Params().GenesisBlock())));
    auto last_mined = GoodBlock(Params().GenesisBlock().GetHash());
    BOOST_REQUIRE(ProcessBlock(last_mined));
    for (int test_runs = 3; test_runs > 0; --test_runs) {
        BOOST_CHECK_EQUAL(last_mined->GetHash(), ::ChainActive().Tip()->GetBlockHash());
        const uint256 split_hash{last_mined->hashPrevBlock};
        std::vector<CTransactionRef> txs;
        for (int num_txs = 22; num_txs > 0; --num_txs) {
            CMutableTransaction mtx;
            mtx.vin.push_back(CTxIn{COutPoint{last_mined->vtx[0]->GetHash(), 1}, CScript{}});
            mtx.vin[0].scriptWitness.stack.push_back(V_OP_TRUE);
            mtx.vout.push_back(last_mined->vtx[0]->vout[1]);
            mtx.vout[0].nValue -= 40000;
            txs.push_back(MakeTransactionRef(mtx));
            last_mined = GoodBlock(last_mined->GetHash());
            BOOST_REQUIRE(ProcessBlock(last_mined));
        }
        int coinbaseMaturity = Params().GetConsensus().CoinbaseMaturity(0);
        for (int j = coinbaseMaturity; j > 0; --j) {
            last_mined = GoodBlock(last_mined->GetHash());
            BOOST_REQUIRE(ProcessBlock(last_mined));
        }
        const uint256 tip_init{last_mined->GetHash()};
        std::vector<std::shared_ptr<const CBlock>> reorg;
        last_mined = GoodBlock(split_hash);
        reorg.push_back(last_mined);
        for (size_t j = coinbaseMaturity + txs.size() + 1; j > 0; --j) {
            last_mined = GoodBlock(last_mined->GetHash());
            reorg.push_back(last_mined);
        }
        {
            LOCK(cs_main);
            TxValidationState state;
            std::list<CTransactionRef> plTxnReplaced;
            for (const auto& tx : txs) {
                BOOST_REQUIRE(AcceptToMemoryPool(
                    *m_node.mempool,
                    state,
                    tx,
                    &plTxnReplaced,
                     false,
                     0));
            }
        }
        {
            LOCK(m_node.mempool->cs);
            BOOST_CHECK_EQUAL(m_node.mempool->mapTx.size(), txs.size());
        }
        std::thread rpc_thread{[&]() {
            while (true) {
                LOCK(m_node.mempool->cs);
                if (m_node.mempool->mapTx.size() == 0) {
                    break;
                }
                assert(m_node.mempool->mapTx.size() == txs.size());
                continue;
            }
            LOCK(cs_main);
            , so the tip must have changed
            assert(tip_init != ::ChainActive().Tip()->GetBlockHash());
        }};
        for (const auto& b : reorg) {
            ProcessBlock(b);
        }
        BOOST_CHECK_EQUAL(last_mined->GetHash(), ::ChainActive().Tip()->GetBlockHash());
        rpc_thread.join();
    }
}
BOOST_AUTO_TEST_CASE ( mempool_locks_reorg ) { bool ignored ; auto ProcessBlock = [ & ignored ] ( std :: shared_ptr < const CBlock > block ) -> bool { return ProcessNewBlock ( Params ( ) , block , true , & ignored ) ; } ; BOOST_REQUIRE ( ProcessBlock ( std :: make_shared < CBlock > ( Params ( ) . GenesisBlock ( ) ) ) ) ; auto last_mined = GoodBlock ( Params ( ) . GenesisBlock ( ) . GetHash ( ) ) ; BOOST_REQUIRE ( ProcessBlock ( last_mined ) ) ; for ( int test_runs = 3 ; test_runs > 0 ; -- test_runs ) { BOOST_CHECK_EQUAL ( last_mined -> GetHash ( ) , :: ChainActive ( ) . Tip ( ) -> GetBlockHash ( ) ) ; const uint256 split_hash { last_mined -> hashPrevBlock } ; std :: vector < CTransactionRef > txs ; for ( int num_txs = 22 ; num_txs > 0 ; -- num_txs ) { CMutableTransaction mtx ; mtx . vin . push_back ( CTxIn { COutPoint { last_mined -> vtx [ 0 ] -> GetHash ( ) , 1 } , CScript { } } ) ; mtx . vin [ 0 ] . scriptWitness . stack . push_back ( V_OP_TRUE ) ; mtx . vout . push_back ( last_mined -> vtx [ 0 ] -> vout [ 1 ] ) ; mtx . vout [ 0 ] . nValue -= 40000 ; txs . push_back ( MakeTransactionRef ( mtx ) ) ; last_mined = GoodBlock ( last_mined -> GetHash ( ) ) ; BOOST_REQUIRE ( ProcessBlock ( last_mined ) ) ; } int coinbaseMaturity = Params ( ) . GetConsensus ( ) . CoinbaseMaturity ( 0 ) ; for ( int j = coinbaseMaturity ; j > 0 ; -- j ) { last_mined = GoodBlock ( last_mined -> GetHash ( ) ) ; BOOST_REQUIRE ( ProcessBlock ( last_mined ) ) ; } const uint256 tip_init { last_mined -> GetHash ( ) } ; std :: vector < std :: shared_ptr < const CBlock > > reorg ; last_mined = GoodBlock ( split_hash ) ; reorg . push_back ( last_mined ) ; for ( size_t j = coinbaseMaturity + txs . size ( ) + 1 ; j > 0 ; -- j ) { last_mined = GoodBlock ( last_mined -> GetHash ( ) ) ; reorg . push_back ( last_mined ) ; } { LOCK ( cs_main ) ; TxValidationState state ; std :: list < CTransactionRef > plTxnReplaced ; for ( const auto & tx : txs ) { BOOST_REQUIRE ( AcceptToMemoryPool ( * m_node . mempool , state , tx , & plTxnReplaced , false , 0 ) ) ; } } { LOCK ( m_node . mempool -> cs ) ; BOOST_CHECK_EQUAL ( m_node . mempool -> mapTx . size ( ) , txs . size ( ) ) ; } std :: thread rpc_thread { [ & ] ( ) { while ( true ) { LOCK ( m_node . mempool -> cs ) ; if ( m_node . mempool -> mapTx . size ( ) == 0 ) { break ; } assert ( m_node . mempool -> mapTx . size ( ) == txs . size ( ) ) ; continue ; } LOCK ( cs_main ) ; assert ( tip_init != :: ChainActive ( ) . Tip ( ) -> GetBlockHash ( ) ) ; } } ; for ( const auto & b : reorg ) { ProcessBlock ( b ) ; } BOOST_CHECK_EQUAL ( last_mined -> GetHash ( ) , :: ChainActive ( ) . Tip ( ) -> GetBlockHash ( ) ) ; rpc_thread . join ( ) ; } }
Test that mempool updates happen atomically with reorgs.
Test that mempool updates happen atomically with reorgs .
/* fForceProcessing */ /* fNewBlock */ // Process all mined blocks // Run the test multiple times // Later on split from here // Create a bunch of transactions to spend the miner rewards of the // most recent blocks // Mature the inputs of the txs // Mine a reorg (and hold it back) before adding the txs to the mempool // Add the txs to the tx pool /* bypass_limits */ /* nAbsurdFee */ // Check that all txs are in the pool // Run a thread that simulates an RPC caller that is polling while // validation is doing a reorg // This thread is checking that the mempool either contains all of // the transactions invalidated by the reorg, or none of them, and // not some intermediate amount. // We are done with the reorg // Internally, we might be in the middle of the reorg, but // externally the reorg to the most-proof-of-work chain should // be atomic. So the caller assumes that the returned mempool // is consistent. That is, it has all txs that were there // before the reorg. // We are done with the reorg, so the tip must have changed // Submit the reorg in this thread to invalidate and remove the txs from the tx pool // Check that the reorg was eventually successful // We can join the other thread, which returns when the reorg was successful