Skip to content

Commit

Permalink
test: validation: add unittest for UpdateTip behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesob committed Apr 12, 2021
1 parent d7fc5c3 commit 3f1346a
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/test/validation_chainstate_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
#include <chainparams.h>
#include <random.h>
#include <uint256.h>
#include <consensus/validation.h>
#include <sync.h>
#include <rpc/blockchain.h>
#include <test/util/setup_common.h>
#include <test/util/chainstate.h>
#include <validation.h>

#include <vector>
Expand Down Expand Up @@ -72,4 +75,66 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
WITH_LOCK(::cs_main, manager.Unload());
}

//! Test UpdateTip behavior for both active and background chainstates.
//!
//! When run on the background chainstate, UpdateTip should do a subset
//! of what it does for the active chainstate.
BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
{
ChainstateManager& chainman = *Assert(m_node.chainman);
uint256 curr_tip = ::g_best_block;

// Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
// be found.
mineBlocks(10);

// After adding some blocks to the tip, best block should have changed.
BOOST_CHECK(::g_best_block != curr_tip);

BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(m_node, m_path_root));

// Ensure our active chain is the snapshot chainstate.
BOOST_CHECK(chainman.IsSnapshotActive());

curr_tip = ::g_best_block;

// Mine a new block on top of the activated snapshot chainstate.
mineBlocks(1); // Defined in TestChain100Setup.

// After adding some blocks to the snapshot tip, best block should have changed.
BOOST_CHECK(::g_best_block != curr_tip);

curr_tip = ::g_best_block;

CChainState& background_cs = m_node.chainman->ValidatedChainstate();
BOOST_CHECK(m_node.chainman->IsBackgroundIBD(&background_cs));

// Create a block to append to the validation chain.
std::vector<CMutableTransaction> noTxns;
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CBlock validation_block = this->CreateBlock(noTxns, scriptPubKey, &background_cs);
auto pblock = std::make_shared<const CBlock>(validation_block);
BlockValidationState state;
CBlockIndex* pindex = nullptr;
const CChainParams& chainparams = Params();
bool newblock = false;

// TODO: much of this is inlined from ProcessNewBlock(); just reuse PNB()
// once it is changed to support multiple chainstates.
{
LOCK(::cs_main);
bool checked = CheckBlock(*pblock, state, chainparams.GetConsensus());
BOOST_CHECK(checked);
bool accepted = background_cs.AcceptBlock(
pblock, state, chainparams, &pindex, true, nullptr, &newblock);
BOOST_CHECK(accepted);
}
bool block_added = background_cs.ActivateBestChain(state, chainparams, pblock);

// g_best_block should be unchanged after adding a block to the background
// validation chain.
BOOST_CHECK(block_added);
BOOST_CHECK_EQUAL(curr_tip, ::g_best_block);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 3f1346a

Please sign in to comment.