kernel: improve BlockChecked ownership semantics

Subscribers to the BlockChecked validation interface event may need
access to the block outside of the callback scope. Currently, this
is only possible by copying the block, which makes exposing this
validation interface event publicly either cumbersome or with significant
copy overhead.

By using shared_ptr, we make the shared ownership explicit and allow
users to safely use the block outside of the callback scope.
This commit is contained in:
stickies-v
2025-07-28 13:57:52 +01:00
parent 9ba1fff29e
commit 1d9f1cb4bd
8 changed files with 24 additions and 22 deletions

View File

@@ -513,7 +513,7 @@ public:
EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void BlockChecked(const CBlock& block, const BlockValidationState& state) override
void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
@@ -2103,11 +2103,11 @@ void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlock
* Handle invalid block rejection and consequent peer discouragement, maintain which
* peers announce compact blocks.
*/
void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationState& state)
void PeerManagerImpl::BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state)
{
LOCK(cs_main);
const uint256 hash(block.GetHash());
const uint256 hash(block->GetHash());
std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
// If the block failed validation, we know where it came from and we're still connected