Skip to content

Commit 7b8e976

Browse files
committed
miner: Add chainstate member to BlockAssembler
1 parent e62067e commit 7b8e976

File tree

8 files changed

+22
-17
lines changed

8 files changed

+22
-17
lines changed

src/miner.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ BlockAssembler::Options::Options() {
5555
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
5656
}
5757

58-
BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options)
58+
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options)
5959
: chainparams(params),
60-
m_mempool(mempool)
60+
m_mempool(mempool),
61+
m_chainstate(chainstate)
6162
{
6263
blockMinFeeRate = options.blockMinFeeRate;
6364
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
@@ -79,8 +80,8 @@ static BlockAssembler::Options DefaultOptions()
7980
return options;
8081
}
8182

82-
BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params)
83-
: BlockAssembler(mempool, params, DefaultOptions()) {}
83+
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params)
84+
: BlockAssembler(chainstate, mempool, params, DefaultOptions()) {}
8485

8586
void BlockAssembler::resetBlock()
8687
{
@@ -114,7 +115,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
114115
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
115116

116117
LOCK2(cs_main, m_mempool.cs);
117-
CBlockIndex* pindexPrev = ::ChainActive().Tip();
118+
assert(std::addressof(*::ChainActive().Tip()) == std::addressof(*m_chainstate.m_chain.Tip()));
119+
CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
118120
assert(pindexPrev != nullptr);
119121
nHeight = pindexPrev->nHeight + 1;
120122

@@ -173,7 +175,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
173175
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
174176

175177
BlockValidationState state;
176-
if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), *pblock, pindexPrev, false, false)) {
178+
assert(std::addressof(::ChainstateActive()) == std::addressof(m_chainstate));
179+
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, false, false)) {
177180
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
178181
}
179182
int64_t nTime2 = GetTimeMicros();

src/miner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class BlockAssembler
146146
int64_t nLockTimeCutoff;
147147
const CChainParams& chainparams;
148148
const CTxMemPool& m_mempool;
149+
CChainState& m_chainstate;
149150

150151
public:
151152
struct Options {
@@ -154,8 +155,8 @@ class BlockAssembler
154155
CFeeRate blockMinFeeRate;
155156
};
156157

157-
explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params);
158-
explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options);
158+
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params);
159+
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options);
159160

160161
/** Construct a new block template with coinbase to scriptPubKeyIn */
161162
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
@@ -201,6 +202,7 @@ class BlockAssembler
201202
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
202203
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
203204

205+
// TODO just accept a CBlockIndex*
204206
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
205207
void RegenerateCommitments(CBlock& block, BlockManager& blockman);
206208

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
150150
UniValue blockHashes(UniValue::VARR);
151151
while (nHeight < nHeightEnd && !ShutdownRequested())
152152
{
153-
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(coinbase_script));
153+
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
154154
if (!pblocktemplate.get())
155155
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
156156
CBlock *pblock = &pblocktemplate->block;
@@ -358,7 +358,7 @@ static RPCHelpMan generateblock()
358358
LOCK(cs_main);
359359

360360
CTxMemPool empty_mempool;
361-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(coinbase_script));
361+
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(::ChainstateActive(), empty_mempool, chainparams).CreateNewBlock(coinbase_script));
362362
if (!blocktemplate) {
363363
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
364364
}
@@ -748,7 +748,7 @@ static RPCHelpMan getblocktemplate()
748748

749749
// Create new block
750750
CScript scriptDummy = CScript() << OP_TRUE;
751-
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(scriptDummy);
751+
pblocktemplate = BlockAssembler(::ChainstateActive(), mempool, Params()).CreateNewBlock(scriptDummy);
752752
if (!pblocktemplate)
753753
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
754754

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
6262
const CScript& scriptPubKey)
6363
{
6464
const CChainParams& chainparams = Params();
65-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
65+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(::ChainstateActive(), *m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
6666
CBlock& block = pblocktemplate->block;
6767
block.hashPrevBlock = prev->GetBlockHash();
6868
block.nTime = prev->nTime + 1;

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
4444

4545
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
4646
options.blockMinFeeRate = blockMinFeeRate;
47-
return BlockAssembler(*m_node.mempool, params, options);
47+
return BlockAssembler(::ChainstateActive(), *m_node.mempool, params, options);
4848
}
4949

5050
constexpr static struct {

src/test/util/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
4141
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
4242
{
4343
auto block = std::make_shared<CBlock>(
44-
BlockAssembler{*Assert(node.mempool), Params()}
44+
BlockAssembler{::ChainstateActive(), *Assert(node.mempool), Params()}
4545
.CreateNewBlock(coinbase_scriptPubKey)
4646
->block);
4747

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
244244
{
245245
const CChainParams& chainparams = Params();
246246
CTxMemPool empty_pool;
247-
CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
247+
CBlock block = BlockAssembler(::ChainstateActive(), empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
248248

249249
Assert(block.vtx.size() == 1);
250250
for (const CMutableTransaction& tx : txns) {

src/test/validation_block_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
6363
static int i = 0;
6464
static uint64_t time = Params().GenesisBlock().nTime;
6565

66-
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
66+
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
6767
auto pblock = std::make_shared<CBlock>(ptemplate->block);
6868
pblock->hashPrevBlock = prev_hash;
6969
pblock->nTime = ++time;
@@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
325325
{
326326
CScript pubKey;
327327
pubKey << 1 << OP_TRUE;
328-
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
328+
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(pubKey);
329329
CBlock pblock = ptemplate->block;
330330

331331
CTxOut witness;

0 commit comments

Comments
 (0)