What changed, and why it matters
This small patch fixes a wallet function that estimates the current blockchain height by looking at known past 'fork' events. Before the fix, if a fork was scheduled in the future, the code only added blocks when time had already passed the fork, but did nothing sensible when the fork was still ahead. The new code subtracts the remaining time from the estimate, and returns 0 with an error log if the calculation would go negative. This prevents the wallet from producing a nonsensical or underflowing height estimate, which could affect transaction creation, fee calculations, or lock-time checks.
Treat as a low-to-moderate hardening fix. Review callers of get_approximate_blockchain_height() to confirm they handle a 0 return safely, and consider whether any consensus-critical code paths depend on this approximation rather than the daemon's actual chain height.
Security signals we found
Potential unsigned integer underflow in blockchain height approximation
Incorrect future-fork handling could produce invalid height estimates
Defensive bounds check added with error logging and early return
May affect transaction validity, unlock_time, and fee estimation paths that rely on approximate height
Evidence from the diff
In wallet2::get_approximate_blockchain_height(), the original logic only adjusted approx_blockchain_height upward when now > fork_time. For future forks it left the estimate unchanged. The patch adds an else-if branch that subtracts (fork_time - now) / seconds_per_block from the approximate height, and an else branch that returns 0 after logging an error when the subtraction would underflow. This is a defensive hardening change against future fork schedules and unsigned-integer wraparound in the height estimate.
Changed components
src/wallet/wallet2.cppwallet2::get_approximate_blockchain_height()Inspect captured patch +7 / −0
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 5479aae..a06100f 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -12798,6 +12798,13 @@ uint64_t wallet2::get_approximate_blockchain_height() const
const time_t now = time(NULL);
if (now > fork_time)
approx_blockchain_height += (now - fork_time) / seconds_per_block;
+ else if (approx_blockchain_height > (fork_time - now) / seconds_per_block)
+ approx_blockchain_height -= (fork_time - now) / seconds_per_block;
+ else
+ {
+ LOG_ERROR("Failed to approximate blockchain height from future fork block: " << approx_blockchain_height);
+ return 0;
+ }
// testnet and stagenet got some huge rollbacks, so the estimation is way off
const uint64_t approximate_rolled_back_blocks = m_nettype == TESTNET ? 26600 : m_nettype == STAGENET ? 48600 : 33600;
if (approx_blockchain_height > approximate_rolled_back_blocks)
Why this scored 29/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.