mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-28 21:00:41 +02:00
Add ChainstateManager::m_adjusted_time_callback
This decouples validation.cpp from netaddress.cpp (transitively, timedata.cpp, and asmap.cpp). This is important for libbitcoinkernel as: - There is no reason for the consensus engine to be coupled with netaddress, timedata, and asmap - Users of libbitcoinkernel can now easily supply their own std::function that provides the adjusted time. See the src/Makefile.am changes for some satisfying removals.
This commit is contained in:
parent
dbe45c34f8
commit
04c31c1295
@ -877,7 +877,6 @@ libbitcoinkernel_la_SOURCES = \
|
|||||||
init/common.cpp \
|
init/common.cpp \
|
||||||
key.cpp \
|
key.cpp \
|
||||||
logging.cpp \
|
logging.cpp \
|
||||||
netaddress.cpp \
|
|
||||||
node/blockstorage.cpp \
|
node/blockstorage.cpp \
|
||||||
node/chainstate.cpp \
|
node/chainstate.cpp \
|
||||||
node/coinstats.cpp \
|
node/coinstats.cpp \
|
||||||
@ -906,11 +905,9 @@ libbitcoinkernel_la_SOURCES = \
|
|||||||
support/lockedpool.cpp \
|
support/lockedpool.cpp \
|
||||||
sync.cpp \
|
sync.cpp \
|
||||||
threadinterrupt.cpp \
|
threadinterrupt.cpp \
|
||||||
timedata.cpp \
|
|
||||||
txdb.cpp \
|
txdb.cpp \
|
||||||
txmempool.cpp \
|
txmempool.cpp \
|
||||||
uint256.cpp \
|
uint256.cpp \
|
||||||
util/asmap.cpp \
|
|
||||||
util/bytevectorhash.cpp \
|
util/bytevectorhash.cpp \
|
||||||
util/check.cpp \
|
util/check.cpp \
|
||||||
util/getuniquepath.cpp \
|
util/getuniquepath.cpp \
|
||||||
|
@ -72,6 +72,7 @@ int main(int argc, char* argv[])
|
|||||||
// SETUP: Chainstate
|
// SETUP: Chainstate
|
||||||
const ChainstateManager::Options chainman_opts{
|
const ChainstateManager::Options chainman_opts{
|
||||||
chainparams,
|
chainparams,
|
||||||
|
static_cast<int64_t(*)()>(GetTime),
|
||||||
};
|
};
|
||||||
ChainstateManager chainman{chainman_opts};
|
ChainstateManager chainman{chainman_opts};
|
||||||
|
|
||||||
|
@ -1423,6 +1423,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
|
|
||||||
const ChainstateManager::Options chainman_opts{
|
const ChainstateManager::Options chainman_opts{
|
||||||
chainparams,
|
chainparams,
|
||||||
|
GetAdjustedTime,
|
||||||
};
|
};
|
||||||
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
||||||
ChainstateManager& chainman = *node.chainman;
|
ChainstateManager& chainman = *node.chainman;
|
||||||
|
@ -17,6 +17,7 @@ class CChainParams;
|
|||||||
*/
|
*/
|
||||||
struct ChainstateManagerOpts {
|
struct ChainstateManagerOpts {
|
||||||
const CChainParams& chainparams;
|
const CChainParams& chainparams;
|
||||||
|
const std::function<int64_t()> adjusted_time_callback{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
|
#endif // BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
|
||||||
|
@ -167,7 +167,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
|
|||||||
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
|
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
|
||||||
|
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, false, false)) {
|
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, GetAdjustedTime, false, false)) {
|
||||||
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
|
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
|
||||||
}
|
}
|
||||||
int64_t nTime2 = GetTimeMicros();
|
int64_t nTime2 = GetTimeMicros();
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include <script/script.h>
|
#include <script/script.h>
|
||||||
#include <script/signingprovider.h>
|
#include <script/signingprovider.h>
|
||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
|
#include <timedata.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
@ -371,7 +372,7 @@ static RPCHelpMan generateblock()
|
|||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
|
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
|
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), GetAdjustedTime, false, false)) {
|
||||||
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
|
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -640,7 +641,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
|
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
|
||||||
return "inconclusive-not-best-prevblk";
|
return "inconclusive-not-best-prevblk";
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true);
|
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, GetAdjustedTime, false, true);
|
||||||
return BIP22ValidationResult(state);
|
return BIP22ValidationResult(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include <node/miner.h>
|
#include <node/miner.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <script/standard.h>
|
#include <script/standard.h>
|
||||||
|
#include <timedata.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <test/util/net.h>
|
#include <test/util/net.h>
|
||||||
|
#include <timedata.h>
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
@ -166,6 +167,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
|
|||||||
|
|
||||||
const ChainstateManager::Options chainman_opts{
|
const ChainstateManager::Options chainman_opts{
|
||||||
chainparams,
|
chainparams,
|
||||||
|
GetAdjustedTime,
|
||||||
};
|
};
|
||||||
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
||||||
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
|
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
|
||||||
|
@ -3,13 +3,14 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
//
|
//
|
||||||
#include <chainparams.h>
|
#include <chainparams.h>
|
||||||
#include <random.h>
|
|
||||||
#include <uint256.h>
|
|
||||||
#include <consensus/validation.h>
|
#include <consensus/validation.h>
|
||||||
#include <sync.h>
|
#include <random.h>
|
||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
|
#include <sync.h>
|
||||||
#include <test/util/chainstate.h>
|
#include <test/util/chainstate.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
|
#include <timedata.h>
|
||||||
|
#include <uint256.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -24,6 +25,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
|
|||||||
{
|
{
|
||||||
const ChainstateManager::Options chainman_opts{
|
const ChainstateManager::Options chainman_opts{
|
||||||
Params(),
|
Params(),
|
||||||
|
GetAdjustedTime,
|
||||||
};
|
};
|
||||||
ChainstateManager manager{chainman_opts};
|
ChainstateManager manager{chainman_opts};
|
||||||
|
|
||||||
|
@ -36,7 +36,6 @@
|
|||||||
#include <script/sigcache.h>
|
#include <script/sigcache.h>
|
||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
#include <signet.h>
|
#include <signet.h>
|
||||||
#include <timedata.h>
|
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
@ -2006,7 +2005,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
|||||||
// Also, currently the rule against blocks more than 2 hours in the future
|
// Also, currently the rule against blocks more than 2 hours in the future
|
||||||
// is enforced in ContextualCheckBlockHeader(); we wouldn't want to
|
// is enforced in ContextualCheckBlockHeader(); we wouldn't want to
|
||||||
// re-enforce that rule here (at least until we make it impossible for
|
// re-enforce that rule here (at least until we make it impossible for
|
||||||
// GetAdjustedTime() to go backward).
|
// m_adjusted_time_callback() to go backward).
|
||||||
if (!CheckBlock(block, state, m_params.GetConsensus(), !fJustCheck, !fJustCheck)) {
|
if (!CheckBlock(block, state, m_params.GetConsensus(), !fJustCheck, !fJustCheck)) {
|
||||||
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
|
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
|
||||||
// We don't write down blocks to disk if they may have been
|
// We don't write down blocks to disk if they may have been
|
||||||
@ -3613,7 +3612,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
|
|||||||
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
|
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
|
||||||
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
|
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
|
||||||
}
|
}
|
||||||
if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev, GetAdjustedTime())) {
|
if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev, m_adjusted_time_callback())) {
|
||||||
LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
|
LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -3837,6 +3836,7 @@ bool TestBlockValidity(BlockValidationState& state,
|
|||||||
CChainState& chainstate,
|
CChainState& chainstate,
|
||||||
const CBlock& block,
|
const CBlock& block,
|
||||||
CBlockIndex* pindexPrev,
|
CBlockIndex* pindexPrev,
|
||||||
|
const std::function<int64_t()>& adjusted_time_callback,
|
||||||
bool fCheckPOW,
|
bool fCheckPOW,
|
||||||
bool fCheckMerkleRoot)
|
bool fCheckMerkleRoot)
|
||||||
{
|
{
|
||||||
@ -3850,7 +3850,7 @@ bool TestBlockValidity(BlockValidationState& state,
|
|||||||
indexDummy.phashBlock = &block_hash;
|
indexDummy.phashBlock = &block_hash;
|
||||||
|
|
||||||
// NOTE: CheckBlockHeader is called by CheckBlock
|
// NOTE: CheckBlockHeader is called by CheckBlock
|
||||||
if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev, GetAdjustedTime()))
|
if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev, adjusted_time_callback()))
|
||||||
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.ToString());
|
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.ToString());
|
||||||
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
|
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
|
||||||
return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString());
|
return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString());
|
||||||
|
@ -362,6 +362,7 @@ bool TestBlockValidity(BlockValidationState& state,
|
|||||||
CChainState& chainstate,
|
CChainState& chainstate,
|
||||||
const CBlock& block,
|
const CBlock& block,
|
||||||
CBlockIndex* pindexPrev,
|
CBlockIndex* pindexPrev,
|
||||||
|
const std::function<int64_t()>& adjusted_time_callback,
|
||||||
bool fCheckPOW = true,
|
bool fCheckPOW = true,
|
||||||
bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
@ -837,6 +838,8 @@ private:
|
|||||||
|
|
||||||
const CChainParams& m_chainparams;
|
const CChainParams& m_chainparams;
|
||||||
|
|
||||||
|
const std::function<int64_t()> m_adjusted_time_callback;
|
||||||
|
|
||||||
//! Internal helper for ActivateSnapshot().
|
//! Internal helper for ActivateSnapshot().
|
||||||
[[nodiscard]] bool PopulateAndValidateSnapshot(
|
[[nodiscard]] bool PopulateAndValidateSnapshot(
|
||||||
CChainState& snapshot_chainstate,
|
CChainState& snapshot_chainstate,
|
||||||
@ -857,7 +860,8 @@ public:
|
|||||||
using Options = ChainstateManagerOpts;
|
using Options = ChainstateManagerOpts;
|
||||||
|
|
||||||
explicit ChainstateManager(const Options& opts)
|
explicit ChainstateManager(const Options& opts)
|
||||||
: m_chainparams(opts.chainparams) {};
|
: m_chainparams{opts.chainparams},
|
||||||
|
m_adjusted_time_callback{Assert(opts.adjusted_time_callback)} {};
|
||||||
|
|
||||||
const CChainParams& GetParams() const { return m_chainparams; }
|
const CChainParams& GetParams() const { return m_chainparams; }
|
||||||
const Consensus::Params& GetConsensus() const { return m_chainparams.GetConsensus(); }
|
const Consensus::Params& GetConsensus() const { return m_chainparams.GetConsensus(); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user