kernel: refactor: ConnectTip to pass block pointer by value

By passing by value, we can remove the need to allocate a new pointer if
the callsite uses move semantics. In the process, simplify naming.
This commit is contained in:
stickies-v committed 2025-07-28 13:45:15 +01:00
1 parent b08041cac8
commit 9ba1fff29e
2 files changed
+19 -12

No files matched your search

+13 -11
View File
@@ -3145,7 +3145,12 @@ public:
* *
* The block is added to connectTrace if connection succeeds. * The block is added to connectTrace if connection succeeds.
*/ */
bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) bool Chainstate::ConnectTip(
BlockValidationState& state,
CBlockIndex* pindexNew,
std::shared_ptr<const CBlock> block_to_connect,
ConnectTrace& connectTrace,
DisconnectedBlockTransactions& disconnectpool)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
if (m_mempool) AssertLockHeld(m_mempool->cs); if (m_mempool) AssertLockHeld(m_mempool->cs);
@@ -3153,18 +3158,15 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
assert(pindexNew->pprev == m_chain.Tip()); assert(pindexNew->pprev == m_chain.Tip());
// Read block from disk. // Read block from disk.
const auto time_1{SteadyClock::now()}; const auto time_1{SteadyClock::now()};
std::shared_ptr<const CBlock> pthisBlock; if (!block_to_connect) {
if (!pblock) {
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>(); std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
if (!m_blockman.ReadBlock(*pblockNew, *pindexNew)) { if (!m_blockman.ReadBlock(*pblockNew, *pindexNew)) {
return FatalError(m_chainman.GetNotifications(), state, _("Failed to read block.")); return FatalError(m_chainman.GetNotifications(), state, _("Failed to read block."));
} }
pthisBlock = pblockNew; block_to_connect = std::move(pblockNew);
} else { } else {
LogDebug(BCLog::BENCH, " - Using cached block\n"); LogDebug(BCLog::BENCH, " - Using cached block\n");
pthisBlock = pblock;
} }
const CBlock& blockConnecting = *pthisBlock;
// Apply the block atomically to the chain state. // Apply the block atomically to the chain state.
const auto time_2{SteadyClock::now()}; const auto time_2{SteadyClock::now()};
SteadyClock::time_point time_3; SteadyClock::time_point time_3;
@@ -3174,9 +3176,9 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
Ticks<MillisecondsDouble>(time_2 - time_1)); Ticks<MillisecondsDouble>(time_2 - time_1));
{ {
CCoinsViewCache view(&CoinsTip()); CCoinsViewCache view(&CoinsTip());
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view); bool rv = ConnectBlock(*block_to_connect, state, pindexNew, view);
if (m_chainman.m_options.signals) { if (m_chainman.m_options.signals) {
m_chainman.m_options.signals->BlockChecked(blockConnecting, state); m_chainman.m_options.signals->BlockChecked(*block_to_connect, state);
} }
if (!rv) { if (!rv) {
if (state.IsInvalid()) if (state.IsInvalid())
@@ -3212,8 +3214,8 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
Ticks<MillisecondsDouble>(m_chainman.time_chainstate) / m_chainman.num_blocks_total); Ticks<MillisecondsDouble>(m_chainman.time_chainstate) / m_chainman.num_blocks_total);
// Remove conflicting transactions from the mempool.; // Remove conflicting transactions from the mempool.;
if (m_mempool) { if (m_mempool) {
m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight); m_mempool->removeForBlock(block_to_connect->vtx, pindexNew->nHeight);
disconnectpool.removeForBlock(blockConnecting.vtx); disconnectpool.removeForBlock(block_to_connect->vtx);
} }
// Update m_chain & related variables. // Update m_chain & related variables.
m_chain.SetTip(*pindexNew); m_chain.SetTip(*pindexNew);
@@ -3239,7 +3241,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
m_chainman.MaybeCompleteSnapshotValidation(); m_chainman.MaybeCompleteSnapshotValidation();
} }
connectTrace.BlockConnected(pindexNew, std::move(pthisBlock)); connectTrace.BlockConnected(pindexNew, std::move(block_to_connect));
return true; return true;
} }
+6 -1
View File
@@ -787,7 +787,12 @@ public:
protected: protected:
bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs); bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
bool ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs); bool ConnectTip(
BlockValidationState& state,
CBlockIndex* pindexNew,
std::shared_ptr<const CBlock> block_to_connect,
ConnectTrace& connectTrace,
DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main); void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);