Skip to content

Commit 04c31c1

Browse files
committed
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.
1 parent dbe45c3 commit 04c31c1

File tree

11 files changed

+24
-14
lines changed

11 files changed

+24
-14
lines changed

src/Makefile.am

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,6 @@ libbitcoinkernel_la_SOURCES = \
877877
init/common.cpp \
878878
key.cpp \
879879
logging.cpp \
880-
netaddress.cpp \
881880
node/blockstorage.cpp \
882881
node/chainstate.cpp \
883882
node/coinstats.cpp \
@@ -906,11 +905,9 @@ libbitcoinkernel_la_SOURCES = \
906905
support/lockedpool.cpp \
907906
sync.cpp \
908907
threadinterrupt.cpp \
909-
timedata.cpp \
910908
txdb.cpp \
911909
txmempool.cpp \
912910
uint256.cpp \
913-
util/asmap.cpp \
914911
util/bytevectorhash.cpp \
915912
util/check.cpp \
916913
util/getuniquepath.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int main(int argc, char* argv[])
7272
// SETUP: Chainstate
7373
const ChainstateManager::Options chainman_opts{
7474
chainparams,
75+
static_cast<int64_t(*)()>(GetTime),
7576
};
7677
ChainstateManager chainman{chainman_opts};
7778

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14231423

14241424
const ChainstateManager::Options chainman_opts{
14251425
chainparams,
1426+
GetAdjustedTime,
14261427
};
14271428
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
14281429
ChainstateManager& chainman = *node.chainman;

src/kernel/chainstatemanager_opts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CChainParams;
1717
*/
1818
struct ChainstateManagerOpts {
1919
const CChainParams& chainparams;
20+
const std::function<int64_t()> adjusted_time_callback{nullptr};
2021
};
2122

2223
#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: 2 additions & 0 deletions
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>
@@ -166,6 +167,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
166167

167168
const ChainstateManager::Options chainman_opts{
168169
chainparams,
170+
GetAdjustedTime,
169171
};
170172
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
171173
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);

src/test/validation_chainstate_tests.cpp

Lines changed: 5 additions & 3 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>
@@ -24,6 +25,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
2425
{
2526
const ChainstateManager::Options chainman_opts{
2627
Params(),
28+
GetAdjustedTime,
2729
};
2830
ChainstateManager manager{chainman_opts};
2931

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)