https://github.com/bitcoin/bitcoin/pull/29647

Motivation

When computing the block synchronization percentage during IBD it could be the case that we end up performing a by-zero division, resulting in the reported percentage being wrong.

This is really unlikely to happen outside a testing environment (it was actually first detected in https://github.com/bitcoin/bitcoin/pull/29284, but #29647 fixes it in a more general way)

Thoughts/Questions

Not much to say about it, the fix ids pretty straight forward: if the computed value is negative, set it to zero so the division denominator can never be zero:

**blocks_left = std::max<int64_t>(0, blocks_left);**
progress{100.0 * last_accepted.nHeight / (last_accepted.nHeight + blocks_left)};

Review

I tested this against https://github.com/bitcoin/bitcoin/pull/29640 (which pick up bits from #29647), by removing the original fix and replacing by this (more generic) one.

Avoid divide-by-zero in header sync logs when NodeClock is behind by maflcko · Pull Request #29647 · bitcoin/bitcoin