Skip to content

Commit 640eb77

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25064: [kernel 2b/n] Add ChainstateManager::m_adjusted_time_callback
53494bc validation: Have ChainstateManager own m_chainparams (Carl Dong) 04c31c1 Add ChainstateManager::m_adjusted_time_callback (Carl Dong) dbe45c3 Add ChainstateManagerOpts, using as ::Options (Carl Dong) Pull request description: ``` 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. ``` Top commit has no ACKs. Tree-SHA512: a093ec6ecacdc659d656574f05bd31ade6a6cdb64d5a97684f94ae7e55c0e360b78177553d4d1ef40280192674464d029a0d68e96caf8711d9274011172f1330
2 parents aac99fa + 53494bc commit 640eb77

File tree

11 files changed

+69
-20
lines changed

11 files changed

+69
-20
lines changed

src/Makefile.am

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ BITCOIN_CORE_H = \
171171
interfaces/ipc.h \
172172
interfaces/node.h \
173173
interfaces/wallet.h \
174+
kernel/chainstatemanager_opts.h \
174175
key.h \
175176
key_io.h \
176177
logging.h \
@@ -876,7 +877,6 @@ libbitcoinkernel_la_SOURCES = \
876877
init/common.cpp \
877878
key.cpp \
878879
logging.cpp \
879-
netaddress.cpp \
880880
node/blockstorage.cpp \
881881
node/chainstate.cpp \
882882
node/coinstats.cpp \
@@ -905,11 +905,9 @@ libbitcoinkernel_la_SOURCES = \
905905
support/lockedpool.cpp \
906906
sync.cpp \
907907
threadinterrupt.cpp \
908-
timedata.cpp \
909908
txdb.cpp \
910909
txmempool.cpp \
911910
uint256.cpp \
912-
util/asmap.cpp \
913911
util/bytevectorhash.cpp \
914912
util/check.cpp \
915913
util/getuniquepath.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ int main(int argc, char* argv[])
7070

7171

7272
// SETUP: Chainstate
73-
ChainstateManager chainman{chainparams};
73+
const ChainstateManager::Options chainman_opts{
74+
chainparams,
75+
static_cast<int64_t(*)()>(GetTime),
76+
};
77+
ChainstateManager chainman{chainman_opts};
7478

7579
auto rv = node::LoadChainstate(false,
7680
std::ref(chainman),

src/init.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14211421
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
14221422
node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get(), mempool_check_ratio);
14231423

1424-
node.chainman = std::make_unique<ChainstateManager>(chainparams);
1424+
const ChainstateManager::Options chainman_opts{
1425+
chainparams,
1426+
GetAdjustedTime,
1427+
};
1428+
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
14251429
ChainstateManager& chainman = *node.chainman;
14261430

14271431
const bool fReset = fReindex;

src/kernel/chainstatemanager_opts.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
6+
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
7+
8+
#include <cstdint>
9+
#include <functional>
10+
11+
class CChainParams;
12+
13+
/**
14+
* An options struct for `ChainstateManager`, more ergonomically referred to as
15+
* `ChainstateManager::Options` due to the using-declaration in
16+
* `ChainstateManager`.
17+
*/
18+
struct ChainstateManagerOpts {
19+
const CChainParams& chainparams;
20+
const std::function<int64_t()> adjusted_time_callback{nullptr};
21+
};
22+
23+
#endif // BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
167167
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
168168

169169
BlockValidationState state;
170-
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, false, false)) {
170+
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, GetAdjustedTime, false, false)) {
171171
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
172172
}
173173
int64_t nTime2 = GetTimeMicros();

src/rpc/mining.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <script/script.h>
2828
#include <script/signingprovider.h>
2929
#include <shutdown.h>
30+
#include <timedata.h>
3031
#include <txmempool.h>
3132
#include <univalue.h>
3233
#include <util/strencodings.h>
@@ -371,7 +372,7 @@ static RPCHelpMan generateblock()
371372
LOCK(cs_main);
372373

373374
BlockValidationState state;
374-
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
375+
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), GetAdjustedTime, false, false)) {
375376
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
376377
}
377378
}
@@ -640,7 +641,7 @@ static RPCHelpMan getblocktemplate()
640641
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
641642
return "inconclusive-not-best-prevblk";
642643
BlockValidationState state;
643-
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true);
644+
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, GetAdjustedTime, false, true);
644645
return BIP22ValidationResult(state);
645646
}
646647

src/test/miner_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <node/miner.h>
1111
#include <policy/policy.h>
1212
#include <script/standard.h>
13+
#include <timedata.h>
1314
#include <txmempool.h>
1415
#include <uint256.h>
1516
#include <util/strencodings.h>

src/test/util/setup_common.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <shutdown.h>
3030
#include <streams.h>
3131
#include <test/util/net.h>
32+
#include <timedata.h>
3233
#include <txdb.h>
3334
#include <util/strencodings.h>
3435
#include <util/string.h>
@@ -164,7 +165,11 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
164165

165166
m_cache_sizes = CalculateCacheSizes(m_args);
166167

167-
m_node.chainman = std::make_unique<ChainstateManager>(chainparams);
168+
const ChainstateManager::Options chainman_opts{
169+
chainparams,
170+
GetAdjustedTime,
171+
};
172+
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
168173
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
169174

170175
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.

src/test/validation_chainstate_tests.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
55
#include <chainparams.h>
6-
#include <random.h>
7-
#include <uint256.h>
86
#include <consensus/validation.h>
9-
#include <sync.h>
7+
#include <random.h>
108
#include <rpc/blockchain.h>
9+
#include <sync.h>
1110
#include <test/util/chainstate.h>
1211
#include <test/util/setup_common.h>
12+
#include <timedata.h>
13+
#include <uint256.h>
1314
#include <validation.h>
1415

1516
#include <vector>
@@ -22,8 +23,12 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup)
2223
//!
2324
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
2425
{
25-
const CChainParams& chainparams = Params();
26-
ChainstateManager manager(chainparams);
26+
const ChainstateManager::Options chainman_opts{
27+
Params(),
28+
GetAdjustedTime,
29+
};
30+
ChainstateManager manager{chainman_opts};
31+
2732
WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true));
2833
CTxMemPool mempool;
2934

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <script/sigcache.h>
3737
#include <shutdown.h>
3838
#include <signet.h>
39-
#include <timedata.h>
4039
#include <tinyformat.h>
4140
#include <txdb.h>
4241
#include <txmempool.h>
@@ -2006,7 +2005,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
20062005
// Also, currently the rule against blocks more than 2 hours in the future
20072006
// is enforced in ContextualCheckBlockHeader(); we wouldn't want to
20082007
// re-enforce that rule here (at least until we make it impossible for
2009-
// GetAdjustedTime() to go backward).
2008+
// m_adjusted_time_callback() to go backward).
20102009
if (!CheckBlock(block, state, m_params.GetConsensus(), !fJustCheck, !fJustCheck)) {
20112010
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
20122011
// We don't write down blocks to disk if they may have been
@@ -3613,7 +3612,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
36133612
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
36143613
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
36153614
}
3616-
if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev, GetAdjustedTime())) {
3615+
if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev, m_adjusted_time_callback())) {
36173616
LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
36183617
return false;
36193618
}
@@ -3837,6 +3836,7 @@ bool TestBlockValidity(BlockValidationState& state,
38373836
CChainState& chainstate,
38383837
const CBlock& block,
38393838
CBlockIndex* pindexPrev,
3839+
const std::function<int64_t()>& adjusted_time_callback,
38403840
bool fCheckPOW,
38413841
bool fCheckMerkleRoot)
38423842
{
@@ -3850,7 +3850,7 @@ bool TestBlockValidity(BlockValidationState& state,
38503850
indexDummy.phashBlock = &block_hash;
38513851

38523852
// NOTE: CheckBlockHeader is called by CheckBlock
3853-
if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev, GetAdjustedTime()))
3853+
if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev, adjusted_time_callback()))
38543854
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.ToString());
38553855
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
38563856
return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString());

0 commit comments

Comments
 (0)