Skip to content

Commit

Permalink
Add code to node/miner.cpp to get custom block
Browse files Browse the repository at this point in the history
Co-authored-by: Pieter Wuille <bitcoin-dev@wuille.net>
  • Loading branch information
2 people authored and ismaelsadeeq committed Feb 14, 2024
1 parent 86dcfff commit 5e249b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/node/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)

static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
{
// Limit weight to between 4K and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, 4000, DEFAULT_BLOCK_MAX_WEIGHT);
if (options.sanity_check_block_weight) {
// Limit weight to between 4K and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, 4000, DEFAULT_BLOCK_MAX_WEIGHT);
}
return options;
}

Expand Down Expand Up @@ -423,9 +425,23 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
}

++nPackagesSelected;
size_per_feerate[CFeeRate{packageFees, packageSize}] += packageSize;

// Update transactions that depend on each of these
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
}
}

std::map<CFeeRate, uint64_t> BlockAssembler::GetFeeRateStats()
{
return std::move(size_per_feerate);
}

std::map<CFeeRate, uint64_t> GetCustomBlockFeeRateHistogram(Chainstate& chainstate, const CTxMemPool* mempool, size_t block_weight)
{
BlockAssembler::Options options = {.nBlockMaxWeight = block_weight, .sanity_check_block_weight = false};
BlockAssembler assembler(chainstate, mempool, options);
assembler.CreateNewBlock(CScript{});
return assembler.GetFeeRateStats();
}
} // namespace node
10 changes: 10 additions & 0 deletions src/node/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class BlockAssembler
uint64_t nBlockSigOpsCost;
CAmount nFees;
std::unordered_set<Txid, SaltedTxidHasher> inBlock;
std::map<CFeeRate, uint64_t> size_per_feerate;

// Chain context for the block
int nHeight;
Expand All @@ -159,6 +160,8 @@ class BlockAssembler
CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
// Whether to call TestBlockValidity() at the end of CreateNewBlock().
bool test_block_validity{true};
// Whether we limit nBlockMaxWeight between 4k and DEFAULT_BLOCK_MAX_WEIGHT.
bool sanity_check_block_weight{true};
};

explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool);
Expand All @@ -170,6 +173,10 @@ class BlockAssembler
inline static std::optional<int64_t> m_last_block_num_txs{};
inline static std::optional<int64_t> m_last_block_weight{};

/** Return a map from feerates to vbyte, indicating how many vbytes were
* included in the block at which feerate. This can only be called once. */
std::map<CFeeRate, uint64_t> GetFeeRateStats();

private:
const Options m_options;

Expand Down Expand Up @@ -204,6 +211,9 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);

/** Get feerate statistics of a block weight from the mempool. */
std::map<CFeeRate, uint64_t> GetCustomBlockFeeRateHistogram(Chainstate& chainstate, const CTxMemPool* mempool, size_t block_weight);

/** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */
void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options);
} // namespace node
Expand Down

0 comments on commit 5e249b8

Please sign in to comment.