mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-20 05:42:50 +02:00
miner: Add chainstate member to BlockAssembler
This commit is contained in:
parent
e62067e7bc
commit
7b8e976cd5
@ -55,9 +55,10 @@ BlockAssembler::Options::Options() {
|
|||||||
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
|
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options)
|
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options)
|
||||||
: chainparams(params),
|
: chainparams(params),
|
||||||
m_mempool(mempool)
|
m_mempool(mempool),
|
||||||
|
m_chainstate(chainstate)
|
||||||
{
|
{
|
||||||
blockMinFeeRate = options.blockMinFeeRate;
|
blockMinFeeRate = options.blockMinFeeRate;
|
||||||
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
|
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
|
||||||
@ -79,8 +80,8 @@ static BlockAssembler::Options DefaultOptions()
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params)
|
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params)
|
||||||
: BlockAssembler(mempool, params, DefaultOptions()) {}
|
: BlockAssembler(chainstate, mempool, params, DefaultOptions()) {}
|
||||||
|
|
||||||
void BlockAssembler::resetBlock()
|
void BlockAssembler::resetBlock()
|
||||||
{
|
{
|
||||||
@ -114,7 +115,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
|
|||||||
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
|
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
|
||||||
|
|
||||||
LOCK2(cs_main, m_mempool.cs);
|
LOCK2(cs_main, m_mempool.cs);
|
||||||
CBlockIndex* pindexPrev = ::ChainActive().Tip();
|
assert(std::addressof(*::ChainActive().Tip()) == std::addressof(*m_chainstate.m_chain.Tip()));
|
||||||
|
CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
|
||||||
assert(pindexPrev != nullptr);
|
assert(pindexPrev != nullptr);
|
||||||
nHeight = pindexPrev->nHeight + 1;
|
nHeight = pindexPrev->nHeight + 1;
|
||||||
|
|
||||||
@ -173,7 +175,8 @@ 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, ::ChainstateActive(), *pblock, pindexPrev, false, false)) {
|
assert(std::addressof(::ChainstateActive()) == std::addressof(m_chainstate));
|
||||||
|
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, 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();
|
||||||
|
@ -146,6 +146,7 @@ private:
|
|||||||
int64_t nLockTimeCutoff;
|
int64_t nLockTimeCutoff;
|
||||||
const CChainParams& chainparams;
|
const CChainParams& chainparams;
|
||||||
const CTxMemPool& m_mempool;
|
const CTxMemPool& m_mempool;
|
||||||
|
CChainState& m_chainstate;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct Options {
|
struct Options {
|
||||||
@ -154,8 +155,8 @@ public:
|
|||||||
CFeeRate blockMinFeeRate;
|
CFeeRate blockMinFeeRate;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params);
|
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params);
|
||||||
explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options);
|
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options);
|
||||||
|
|
||||||
/** Construct a new block template with coinbase to scriptPubKeyIn */
|
/** Construct a new block template with coinbase to scriptPubKeyIn */
|
||||||
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
|
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
|
||||||
@ -201,6 +202,7 @@ private:
|
|||||||
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
|
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
|
||||||
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
|
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
|
||||||
|
|
||||||
|
// TODO just accept a CBlockIndex*
|
||||||
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
|
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
|
||||||
void RegenerateCommitments(CBlock& block, BlockManager& blockman);
|
void RegenerateCommitments(CBlock& block, BlockManager& blockman);
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
|
|||||||
UniValue blockHashes(UniValue::VARR);
|
UniValue blockHashes(UniValue::VARR);
|
||||||
while (nHeight < nHeightEnd && !ShutdownRequested())
|
while (nHeight < nHeightEnd && !ShutdownRequested())
|
||||||
{
|
{
|
||||||
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(coinbase_script));
|
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
|
||||||
if (!pblocktemplate.get())
|
if (!pblocktemplate.get())
|
||||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
||||||
CBlock *pblock = &pblocktemplate->block;
|
CBlock *pblock = &pblocktemplate->block;
|
||||||
@ -358,7 +358,7 @@ static RPCHelpMan generateblock()
|
|||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
|
|
||||||
CTxMemPool empty_mempool;
|
CTxMemPool empty_mempool;
|
||||||
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(coinbase_script));
|
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(::ChainstateActive(), empty_mempool, chainparams).CreateNewBlock(coinbase_script));
|
||||||
if (!blocktemplate) {
|
if (!blocktemplate) {
|
||||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
||||||
}
|
}
|
||||||
@ -748,7 +748,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
|
|
||||||
// Create new block
|
// Create new block
|
||||||
CScript scriptDummy = CScript() << OP_TRUE;
|
CScript scriptDummy = CScript() << OP_TRUE;
|
||||||
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(scriptDummy);
|
pblocktemplate = BlockAssembler(::ChainstateActive(), mempool, Params()).CreateNewBlock(scriptDummy);
|
||||||
if (!pblocktemplate)
|
if (!pblocktemplate)
|
||||||
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
|
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
|
|||||||
const CScript& scriptPubKey)
|
const CScript& scriptPubKey)
|
||||||
{
|
{
|
||||||
const CChainParams& chainparams = Params();
|
const CChainParams& chainparams = Params();
|
||||||
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
|
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(::ChainstateActive(), *m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
|
||||||
CBlock& block = pblocktemplate->block;
|
CBlock& block = pblocktemplate->block;
|
||||||
block.hashPrevBlock = prev->GetBlockHash();
|
block.hashPrevBlock = prev->GetBlockHash();
|
||||||
block.nTime = prev->nTime + 1;
|
block.nTime = prev->nTime + 1;
|
||||||
|
@ -44,7 +44,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
|
|||||||
|
|
||||||
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
|
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
|
||||||
options.blockMinFeeRate = blockMinFeeRate;
|
options.blockMinFeeRate = blockMinFeeRate;
|
||||||
return BlockAssembler(*m_node.mempool, params, options);
|
return BlockAssembler(::ChainstateActive(), *m_node.mempool, params, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr static struct {
|
constexpr static struct {
|
||||||
|
@ -41,7 +41,7 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
|||||||
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
||||||
{
|
{
|
||||||
auto block = std::make_shared<CBlock>(
|
auto block = std::make_shared<CBlock>(
|
||||||
BlockAssembler{*Assert(node.mempool), Params()}
|
BlockAssembler{::ChainstateActive(), *Assert(node.mempool), Params()}
|
||||||
.CreateNewBlock(coinbase_scriptPubKey)
|
.CreateNewBlock(coinbase_scriptPubKey)
|
||||||
->block);
|
->block);
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
|
|||||||
{
|
{
|
||||||
const CChainParams& chainparams = Params();
|
const CChainParams& chainparams = Params();
|
||||||
CTxMemPool empty_pool;
|
CTxMemPool empty_pool;
|
||||||
CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
CBlock block = BlockAssembler(::ChainstateActive(), empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
||||||
|
|
||||||
Assert(block.vtx.size() == 1);
|
Assert(block.vtx.size() == 1);
|
||||||
for (const CMutableTransaction& tx : txns) {
|
for (const CMutableTransaction& tx : txns) {
|
||||||
|
@ -63,7 +63,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
|
|||||||
static int i = 0;
|
static int i = 0;
|
||||||
static uint64_t time = Params().GenesisBlock().nTime;
|
static uint64_t time = Params().GenesisBlock().nTime;
|
||||||
|
|
||||||
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
|
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
|
||||||
auto pblock = std::make_shared<CBlock>(ptemplate->block);
|
auto pblock = std::make_shared<CBlock>(ptemplate->block);
|
||||||
pblock->hashPrevBlock = prev_hash;
|
pblock->hashPrevBlock = prev_hash;
|
||||||
pblock->nTime = ++time;
|
pblock->nTime = ++time;
|
||||||
@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
|
|||||||
{
|
{
|
||||||
CScript pubKey;
|
CScript pubKey;
|
||||||
pubKey << 1 << OP_TRUE;
|
pubKey << 1 << OP_TRUE;
|
||||||
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
|
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(pubKey);
|
||||||
CBlock pblock = ptemplate->block;
|
CBlock pblock = ptemplate->block;
|
||||||
|
|
||||||
CTxOut witness;
|
CTxOut witness;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user