Skip to content

Commit 5f0303b

Browse files
committed
Merge bitcoin/bitcoin#33443: log: reduce excessive "rolling back/forward" messages during block replay
1fc7a81 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 1fc7a81 stickies-v: ACK 1fc7a81 achow101: ACK 1fc7a81 vasild: ACK 1fc7a81 hodlinator: Concept ACK 1fc7a81 Tree-SHA512: 44ed1da8336de5a3d937e11a13e6f1789064e23eb70640a1c406fbb0074255344268f6eb6b06f036ca8d22bfeb4bdea319c3085a2139d848f6d36a4f8352b76a
2 parents f4903dd + 1fc7a81 commit 5f0303b

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

src/validation.cpp

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4918,35 +4918,47 @@ bool Chainstate::ReplayBlocks()
49184918
}
49194919

49204920
// Rollback along the old branch.
4921-
while (pindexOld != pindexFork) {
4922-
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
4923-
CBlock block;
4924-
if (!m_blockman.ReadBlock(block, *pindexOld)) {
4925-
LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4926-
return false;
4927-
}
4928-
LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
4929-
DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
4930-
if (res == DISCONNECT_FAILED) {
4931-
LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4932-
return false;
4921+
const int nForkHeight{pindexFork ? pindexFork->nHeight : 0};
4922+
if (pindexOld != pindexFork) {
4923+
LogInfo("Rolling back from %s (%i to %i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight, nForkHeight);
4924+
while (pindexOld != pindexFork) {
4925+
if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
4926+
CBlock block;
4927+
if (!m_blockman.ReadBlock(block, *pindexOld)) {
4928+
LogError("RollbackBlock(): ReadBlock() failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4929+
return false;
4930+
}
4931+
if (pindexOld->nHeight % 10'000 == 0) {
4932+
LogInfo("Rolling back %s (%i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
4933+
}
4934+
DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
4935+
if (res == DISCONNECT_FAILED) {
4936+
LogError("RollbackBlock(): DisconnectBlock failed at %d, hash=%s\n", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4937+
return false;
4938+
}
4939+
// If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
4940+
// overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
4941+
// applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
4942+
// the result is still a version of the UTXO set with the effects of that block undone.
49334943
}
4934-
// If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
4935-
// overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
4936-
// applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
4937-
// the result is still a version of the UTXO set with the effects of that block undone.
4944+
pindexOld = pindexOld->pprev;
49384945
}
4939-
pindexOld = pindexOld->pprev;
4946+
LogInfo("Rolled back to %s", pindexFork->GetBlockHash().ToString());
49404947
}
49414948

49424949
// Roll forward from the forking point to the new tip.
4943-
int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
4944-
for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
4945-
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
4950+
if (nForkHeight < pindexNew->nHeight) {
4951+
LogInfo("Rolling forward to %s (%i to %i)", pindexNew->GetBlockHash().ToString(), nForkHeight, pindexNew->nHeight);
4952+
for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
4953+
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
49464954

4947-
LogInfo("Rolling forward %s (%i)", pindex.GetBlockHash().ToString(), nHeight);
4948-
m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
4949-
if (!RollforwardBlock(&pindex, cache)) return false;
4955+
if (nHeight % 10'000 == 0) {
4956+
LogInfo("Rolling forward %s (%i)", pindex.GetBlockHash().ToString(), nHeight);
4957+
}
4958+
m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
4959+
if (!RollforwardBlock(&pindex, cache)) return false;
4960+
}
4961+
LogInfo("Rolled forward to %s", pindexNew->GetBlockHash().ToString());
49504962
}
49514963

49524964
cache.SetBestBlock(pindexNew->GetBlockHash());

0 commit comments

Comments
 (0)