Merge bitcoin/bitcoin#33443: log: reduce excessive "rolling back/forward" messages during block replay

1fc7a81f1f log: reduce excessive messages during block replay (Lőrinc)

Pull request description:

  ### Summary
  After an incomplete reindex the blocks will need to be replayed.
  This results in excessive `Rolling back` and `Rolling forward` messages which quickly triggers the recently introduced log rate limiter.

  Change the logging strategy to:
  - Add single `LogInfo` messages showing the full range being replayed for both rollback and roll forward;
  - Log progress at `LogInfo` level only every 10,000 blocks to track the long operations.

  ### Reproducer:
  * Start a normal ibd, stop after some progress
  * Do a reindex, stop before it finishes
  * Restart the node normally without specifying the reindex parameter
  It should start rolling the blocks forward.

  Before this change the excessive logging would show:
  ```
  [*] Rolling forward 000000002f4f55aecfccc911076dc3f73ac0288c83dc1d79db0a026441031d40 (46245)
  [*] Rolling forward 0000000017ffcf34c8eac010c529670ba6745ea59cf1edf7b820928e3b40acf6 (46246)
  ```

  After the change it shows:
  ```
  Replaying blocks
  Rolling forward to 00000000000000001034012d7e4facaf16ca747ea94b8ea66743086cfe298ef8 (326223 to 340991)
  Rolling forward 00000000000000000faabab19f17c0178c754dbed023e6c871dcaf74159c5f02 (330000)
  Rolling forward 00000000000000000d9b2508615d569e18f00c034d71474fc44a43af8d4a5003 (340000)
  ...
  Rolled forward to 00000000000000001034012d7e4facaf16ca747ea94b8ea66743086cfe298ef8
  ```
  (similarly to rolling back)

ACKs for top commit:
  Crypt-iQ:
    crACK 1fc7a81f1f
  stickies-v:
    ACK 1fc7a81f1f
  achow101:
    ACK 1fc7a81f1f
  vasild:
    ACK 1fc7a81f1f
  hodlinator:
    Concept ACK 1fc7a81f1f

Tree-SHA512: 44ed1da8336de5a3d937e11a13e6f1789064e23eb70640a1c406fbb0074255344268f6eb6b06f036ca8d22bfeb4bdea319c3085a2139d848f6d36a4f8352b76a
This commit is contained in:
Ava Chow
2025-11-10 08:27:40 -08:00

View File

@@ -4918,35 +4918,47 @@ bool Chainstate::ReplayBlocks()
}
// Rollback along the old branch.
while (pindexOld != pindexFork) {
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
CBlock block;
if (!m_blockman.ReadBlock(block, *pindexOld)) {
LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
return false;
const int nForkHeight{pindexFork ? pindexFork->nHeight : 0};
if (pindexOld != pindexFork) {
LogInfo("Rolling back from %s (%i to %i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight, nForkHeight);
while (pindexOld != pindexFork) {
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
CBlock block;
if (!m_blockman.ReadBlock(block, *pindexOld)) {
LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
return false;
}
if (pindexOld->nHeight % 10'000 == 0) {
LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
}
DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
if (res == DISCONNECT_FAILED) {
LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
return false;
}
// If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
// overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
// applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
// the result is still a version of the UTXO set with the effects of that block undone.
}
LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
if (res == DISCONNECT_FAILED) {
LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
return false;
}
// If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
// overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
// applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
// the result is still a version of the UTXO set with the effects of that block undone.
pindexOld = pindexOld->pprev;
}
pindexOld = pindexOld->pprev;
LogInfo("Rolled back to %s", pindexFork->GetBlockHash().ToString());
}
// Roll forward from the forking point to the new tip.
int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
if (nForkHeight < pindexNew->nHeight) {
LogInfo("Rolling forward to %s (%i to %i)", pindexNew->GetBlockHash().ToString(), nForkHeight, pindexNew->nHeight);
for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
LogInfo("Rolling forward %s (%i)", pindex.GetBlockHash().ToString(), nHeight);
m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
if (!RollforwardBlock(&pindex, cache)) return false;
if (nHeight % 10'000 == 0) {
LogInfo("Rolling forward %s (%i)", pindex.GetBlockHash().ToString(), nHeight);
}
m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
if (!RollforwardBlock(&pindex, cache)) return false;
}
LogInfo("Rolled forward to %s", pindexNew->GetBlockHash().ToString());
}
cache.SetBestBlock(pindexNew->GetBlockHash());