validationinterface: Add signal for new block templates generated

This commit is contained in:
Luke Dashjr 2024-08-09 19:29:12 +00:00
parent 8ef56b3f03
commit 4505d469ec
13 changed files with 29 additions and 13 deletions

View File

@ -918,7 +918,7 @@ public:
std::shared_ptr<CBlockTemplate> createNewBlock2(const CScript& script_pub_key, const BlockCreateOptions& assemble_options) override
{
return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key);
return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options, m_node}.CreateNewBlock(script_pub_key);
}
NodeContext* context() override { return &m_node; }

View File

@ -16,6 +16,7 @@
#include <consensus/validation.h>
#include <deploymentstatus.h>
#include <logging.h>
#include <node/context.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <pow.h>
@ -23,6 +24,7 @@
#include <util/moneystr.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>
#include <algorithm>
#include <utility>
@ -82,10 +84,11 @@ BlockCreateOptions BlockCreateOptions::Clamped() const
return options;
}
BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options, const NodeContext& node)
: chainparams{chainstate.m_chainman.GetParams()},
m_mempool{options.use_mempool ? mempool : nullptr},
m_chainstate{chainstate},
m_node{node},
m_options{options.Clamped()}
{
// Whether we need to account for byte usage (in addition to weight usage)
@ -214,6 +217,8 @@ std::shared_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
Ticks<MillisecondsDouble>(time_2 - time_1),
Ticks<MillisecondsDouble>(time_2 - time_start));
if (m_node.validation_signals) m_node.validation_signals->NewBlockTemplate(pblocktemplate);
return std::move(pblocktemplate);
}

View File

@ -29,6 +29,7 @@ class Chainstate;
class ChainstateManager;
namespace Consensus { struct Params; };
namespace node { struct NodeContext; };
namespace node {
@ -139,7 +140,7 @@ class BlockAssembler
{
private:
// The constructed block template
std::unique_ptr<CBlockTemplate> pblocktemplate;
std::shared_ptr<CBlockTemplate> pblocktemplate;
bool fNeedSizeAccounting;
@ -158,11 +159,12 @@ private:
const CChainParams& chainparams;
const CTxMemPool* const m_mempool;
Chainstate& m_chainstate;
const NodeContext& m_node;
public:
using Options = BlockCreateOptions;
explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options, const NodeContext& node);
/** Construct a new block template with coinbase to scriptPubKeyIn */
std::shared_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);

View File

@ -68,7 +68,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
const CScript& scriptPubKey)
{
BlockAssembler::Options options;
std::shared_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey);
std::shared_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options, m_node}.CreateNewBlock(scriptPubKey);
CBlock& block = pblocktemplate->block;
block.hashPrevBlock = prev->GetBlockHash();
block.nTime = prev->nTime + 1;

View File

@ -175,7 +175,7 @@ FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
miner_options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
miner_options.test_block_validity = false;
node::BlockAssembler miner{g_setup->m_node.chainman->ActiveChainstate(), &pool, miner_options};
node::BlockAssembler miner{g_setup->m_node.chainman->ActiveChainstate(), &pool, miner_options, g_setup->m_node};
node::MiniMiner mini_miner{pool, outpoints};
assert(mini_miner.IsReadyToCalculate());

View File

@ -97,7 +97,7 @@ void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, Cha
BlockAssembler::Options options;
options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
auto assembler = BlockAssembler{chainstate, &tx_pool, options};
auto assembler = BlockAssembler{chainstate, &tx_pool, options, g_setup->m_node};
auto block_template = assembler.CreateNewBlock(CScript{} << OP_TRUE);
Assert(block_template->block.vtx.size() >= 1);
}

View File

@ -68,7 +68,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(CTxMemPool& tx_mempool)
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
options.nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
options.blockMinFeeRate = blockMinFeeRate;
return BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options};
return BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options, m_node};
}
constexpr static struct {

View File

@ -21,7 +21,7 @@ static void mineBlock(const node::NodeContext& node, std::chrono::seconds block_
auto curr_time = GetTime<std::chrono::seconds>();
SetMockTime(block_time); // update time so the block is created with it
node::BlockAssembler::Options options;
CBlock block = node::BlockAssembler{node.chainman->ActiveChainstate(), nullptr, options}.CreateNewBlock(CScript() << OP_TRUE)->block;
CBlock block = node::BlockAssembler{node.chainman->ActiveChainstate(), nullptr, options, node}.CreateNewBlock(CScript() << OP_TRUE)->block;
while (!CheckProofOfWork(block.GetHash(), block.nBits, node.chainman->GetConsensus())) ++block.nNonce;
block.fChecked = true; // little speedup
SetMockTime(curr_time); // process block at current time

View File

@ -112,7 +112,7 @@ std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coi
const BlockAssembler::Options& assembler_options)
{
auto block = std::make_shared<CBlock>(
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), Assert(node.mempool.get()), assembler_options}
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), Assert(node.mempool.get()), assembler_options, node}
.CreateNewBlock(coinbase_scriptPubKey)
->block);

View File

@ -389,7 +389,7 @@ CBlock TestChain100Setup::CreateBlock(
Chainstate& chainstate)
{
BlockAssembler::Options options;
CBlock block = BlockAssembler{chainstate, nullptr, options}.CreateNewBlock(scriptPubKey)->block;
CBlock block = BlockAssembler{chainstate, nullptr, options, m_node}.CreateNewBlock(scriptPubKey)->block;
Assert(block.vtx.size() == 1);
for (const CMutableTransaction& tx : txns) {

View File

@ -66,7 +66,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
static uint64_t time = Params().GenesisBlock().nTime;
BlockAssembler::Options options;
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(CScript{} << i++ << OP_TRUE);
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options, m_node}.CreateNewBlock(CScript{} << i++ << OP_TRUE);
auto pblock = std::make_shared<CBlock>(ptemplate->block);
pblock->hashPrevBlock = prev_hash;
pblock->nTime = ++time;
@ -331,7 +331,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
CScript pubKey;
pubKey << 1 << OP_TRUE;
BlockAssembler::Options options;
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(pubKey);
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options, m_node}.CreateNewBlock(pubKey);
CBlock pblock = ptemplate->block;
CTxOut witness;

View File

@ -260,3 +260,8 @@ void ValidationSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::s
LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
}
void ValidationSignals::NewBlockTemplate(const std::shared_ptr<node::CBlockTemplate>& blocktemplate) {
LOG_EVENT("%s", __func__);
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewBlockTemplate(blocktemplate); });
}

View File

@ -25,6 +25,7 @@ class BlockValidationState;
class CBlock;
class CBlockIndex;
struct CBlockLocator;
namespace node { struct CBlockTemplate; }
enum class MemPoolRemovalReason;
struct RemovedMempoolTransactionInfo;
struct NewMempoolTransactionInfo;
@ -157,6 +158,8 @@ protected:
* has been received and connected to the headers tree, though not validated yet.
*/
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
virtual void NewBlockTemplate(const std::shared_ptr<node::CBlockTemplate>& blocktemplate) {}
/**
* Notifies the validation interface that it is being unregistered
*/
@ -234,6 +237,7 @@ public:
void ChainStateFlushed(ChainstateRole, const CBlockLocator &);
void BlockChecked(const CBlock&, const BlockValidationState&);
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
void NewBlockTemplate(const std::shared_ptr<node::CBlockTemplate>& blocktemplate);
};
#endif // BITCOIN_VALIDATIONINTERFACE_H