mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 02:02:42 +02:00
Merge bitcoin/bitcoin#21526: validation: UpdateTip/CheckBlockIndex assumeutxo support
673a5bd337test: validation: add unittest for UpdateTip behavior (James O'Beirne)2705570109test: refactor: separate CreateBlock in TestChain100Setup (James O'Beirne)298bf5d563test: refactor: declare NoMalleation const auto (James O'Beirne)071200993fmove-only: unittest: add test/util/chainstate.h (James O'Beirne)8f5710fd0avalidation: fix CheckBlockIndex for multiple chainstates (James O'Beirne)5a807736davalidation: insert assumed-valid block index entries into candidates (James O'Beirne)01a9b8fe71validation: set BLOCK_ASSUMED_VALID during snapshot load (James O'Beirne)42b2520db9chain: add BLOCK_ASSUMED_VALID for use with assumeutxo (James O'Beirne)b217020df7validation: change UpdateTip for multiple chainstates (James O'Beirne)665072a36ddoc: add comment for g_best_block (James O'Beirne)ac4051d891refactor: remove unused assumeutxo methods (James O'Beirne)9f6bb53935validation: add chainman ref to CChainState (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11) (parent PR: #15606) --- Modify UpdateTip and CheckBlockIndex for use with multiple chainstates. Includes a new unittest verifying `g_best_block` behavior (previously untested at the unit level) and various changes necessary for running and testing `ProcessNewBlock()`-like behavior on the background validation chainstate. This changeset introduces a new block index `nStatus` flag called `BLOCK_ASSUMED_VALID`, and it is applied to block index entries that are beneath the UTXO snapshot base block upon snapshot load. Once each block is validated (during async background validation), the flag is removed. This allows us to avoid (ab)using `BLOCK_VALID_*` flags for snapshot chain block entries, and preserves the original meaning of those flags. Note: this PR previously incorporated changes to `LoadBlockIndex()` and `RewindBlockIndex()` as noted in Russ' comments below, but once I generated the changes necessary to test the UpdateTip change, I decided to split this changes out into another PR due to the size of this one. ACKs for top commit: achow101: ACK673a5bd337jonatack: Code-review re-ACK673a5bd337reviewed diff, rebased to master/debug build/ran unit+functional tests naumenkogs: ACK673a5bd337fjahr: Code review ACK673a5bd337ariard: utACK673a5bd3ryanofsky: Code review ACK673a5bd337. Just linker fix and split commit changes mentioned https://github.com/bitcoin/bitcoin/pull/21526#issuecomment-921064563 since last review benthecarman: ACK673a5bd337Tree-SHA512: 0a6dc23d041b27ed9fd0ee1f3e5971b92fb1d2df2fc9b655d5dc48594235321ab1798d06de2ec55482ac3966a9ed56de8d56e9e29cae75bbe8690bafc2dda383
This commit is contained in:
54
src/test/util/chainstate.h
Normal file
54
src/test/util/chainstate.h
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
//
|
||||
#ifndef BITCOIN_TEST_UTIL_CHAINSTATE_H
|
||||
#define BITCOIN_TEST_UTIL_CHAINSTATE_H
|
||||
|
||||
#include <clientversion.h>
|
||||
#include <fs.h>
|
||||
#include <node/context.h>
|
||||
#include <node/utxo_snapshot.h>
|
||||
#include <rpc/blockchain.h>
|
||||
#include <validation.h>
|
||||
|
||||
#include <univalue.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
const auto NoMalleation = [](CAutoFile& file, SnapshotMetadata& meta){};
|
||||
|
||||
/**
|
||||
* Create and activate a UTXO snapshot, optionally providing a function to
|
||||
* malleate the snapshot.
|
||||
*/
|
||||
template<typename F = decltype(NoMalleation)>
|
||||
static bool
|
||||
CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleation = NoMalleation)
|
||||
{
|
||||
// Write out a snapshot to the test's tempdir.
|
||||
//
|
||||
int height;
|
||||
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
|
||||
fs::path snapshot_path = root / tfm::format("test_snapshot.%d.dat", height);
|
||||
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
|
||||
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
|
||||
|
||||
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
|
||||
BOOST_TEST_MESSAGE(
|
||||
"Wrote UTXO snapshot to " << snapshot_path.make_preferred().string() << ": " << result.write());
|
||||
|
||||
// Read the written snapshot in and then activate it.
|
||||
//
|
||||
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
|
||||
CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
|
||||
SnapshotMetadata metadata;
|
||||
auto_infile >> metadata;
|
||||
|
||||
malleation(auto_infile, metadata);
|
||||
|
||||
return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory*/ true);
|
||||
}
|
||||
|
||||
|
||||
#endif // BITCOIN_TEST_UTIL_CHAINSTATE_H
|
||||
@@ -234,11 +234,14 @@ void TestChain100Setup::mineBlocks(int num_blocks)
|
||||
}
|
||||
}
|
||||
|
||||
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
|
||||
CBlock TestChain100Setup::CreateBlock(
|
||||
const std::vector<CMutableTransaction>& txns,
|
||||
const CScript& scriptPubKey,
|
||||
CChainState& chainstate)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
CTxMemPool empty_pool;
|
||||
CBlock block = BlockAssembler(m_node.chainman->ActiveChainstate(), empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
||||
CBlock block = BlockAssembler(chainstate, empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
||||
|
||||
Assert(block.vtx.size() == 1);
|
||||
for (const CMutableTransaction& tx : txns) {
|
||||
@@ -248,6 +251,20 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
|
||||
|
||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
CBlock TestChain100Setup::CreateAndProcessBlock(
|
||||
const std::vector<CMutableTransaction>& txns,
|
||||
const CScript& scriptPubKey,
|
||||
CChainState* chainstate)
|
||||
{
|
||||
if (!chainstate) {
|
||||
chainstate = &Assert(m_node.chainman)->ActiveChainstate();
|
||||
}
|
||||
|
||||
const CChainParams& chainparams = Params();
|
||||
const CBlock block = this->CreateBlock(txns, scriptPubKey, *chainstate);
|
||||
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
|
||||
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
||||
|
||||
|
||||
@@ -119,9 +119,20 @@ struct TestChain100Setup : public RegTestingSetup {
|
||||
/**
|
||||
* Create a new block with just given transactions, coinbase paying to
|
||||
* scriptPubKey, and try to add it to the current chain.
|
||||
* If no chainstate is specified, default to the active.
|
||||
*/
|
||||
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
||||
const CScript& scriptPubKey);
|
||||
const CScript& scriptPubKey,
|
||||
CChainState* chainstate = nullptr);
|
||||
|
||||
/**
|
||||
* Create a new block with just given transactions, coinbase paying to
|
||||
* scriptPubKey.
|
||||
*/
|
||||
CBlock CreateBlock(
|
||||
const std::vector<CMutableTransaction>& txns,
|
||||
const CScript& scriptPubKey,
|
||||
CChainState& chainstate);
|
||||
|
||||
//! Mine a series of new blocks on the active chain.
|
||||
void mineBlocks(int num_blocks);
|
||||
|
||||
Reference in New Issue
Block a user