restart the stale tip warning clock only when the server announces a block it has not announced before
What changed, and why it matters
This change fixes how Sparrow Wallet decides whether its connected Bitcoin server is still keeping up with the blockchain. Previously, any block announcement from the server—even a repeat of the same block—would reset the 'stale tip' timer, potentially hiding a server that had stopped following the chain. Now the timer only resets when the server announces a block with a new hash it hasn't announced before. This makes the stale-tip warning more reliable, which matters because a stale or malicious server could feed outdated or inconsistent transaction information to the wallet.
Review the broader tip-validation and warning logic to ensure the stale-tip warning is actually surfaced to users and not suppressed elsewhere. Consider whether a server could still evade detection by announcing alternating known blocks or by manipulating timestamps on public-server initialization. No urgent patch action is indicated beyond normal update adoption.
Security signals we found
stale tip warning clock reset logic changed from height-only to hash-deduplicated
new static volatile lastTipHash field introduced to track previously announced block
unit test added confirming repeated identical tip does not refresh staleness timer
public Electrum server staleness seeding remains based on tip timestamp
change is in network/server tip validation path, not transaction signing or key handling
Evidence from the diff
The patch changes ElectrumServer.updateTipReceived(int height) to updateTipReceived(BlockHeaderTip tip) and only updates lastTipReceivedAt and clears staleTipWarned when the announced block hash differs from lastTipHash. A new lastTipHash field tracks the previously announced block hash. initializeTip now resets lastTipHash to null. SubscriptionService passes the full BlockHeaderTip instead of just the height. A new unit test verifies that repeating the same tip does not refresh the clock, while a new block does. The change is defensive: it tightens the staleness heuristic but does not by itself prevent a malicious server from lying; it only makes the wallet warn sooner when the server appears stuck.
Changed components
com.sparrowwallet.sparrow.net.ElectrumServercom.sparrowwallet.sparrow.net.SubscriptionServiceElectrumServerTestInspect captured patch +42 / −11
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -132,7 +132,9 @@ public class ElectrumServer {
private static final long TIP_WARNING_INTERVAL_MILLIS = 60 * 1000;
- private static volatile long lastTipReceivedAt;
+ static volatile long lastTipReceivedAt;
+
+ private static volatile Sha256Hash lastTipHash;
private static volatile boolean staleTipWarned;
@@ -3207,18 +3209,24 @@ static void warnInvalidTip(String message) {
}
private static void initializeTip(BlockHeaderTip tip) {
- updateTipReceived(tip.height);
+ lastTipHash = null;
+ updateTipReceived(tip);
//Public servers are never mid-sync, so seed the staleness clock from the tip timestamp to warn promptly on an already stale server
if(Config.get().getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER) {
lastTipReceivedAt = Math.min(lastTipReceivedAt, tip.getBlockHeader().getTime() * 1000);
}
}
- static void updateTipReceived(int height) {
- lastTipReceivedAt = System.currentTimeMillis();
- staleTipWarned = false;
+ static void updateTipReceived(BlockHeaderTip tip) {
+ //Only a block not seen before shows the server is still following the chain
+ Sha256Hash tipHash = tip.getBlockHeader().getHash();
+ if(!tipHash.equals(lastTipHash)) {
+ lastTipHash = tipHash;
+ lastTipReceivedAt = System.currentTimeMillis();
+ staleTipWarned = false;
+ }
invalidTipWarned = false;
- if(height >= Network.get().getHeaderCheckpoints().getMaxHeight()) {
+ if(tip.height >= Network.get().getHeaderCheckpoints().getMaxHeight()) {
tipReachedCheckpoints = true;
}
}
### src/main/java/com/sparrowwallet/sparrow/net/SubscriptionService.java
@@ -27,7 +27,7 @@ public void newBlockHeaderTip(@JsonRpcParam("header") final BlockHeaderTip heade
return;
}
- ElectrumServer.updateTipReceived(header.height);
+ ElectrumServer.updateTipReceived(header);
ElectrumServer.updateRetrievedBlockHeaders(header.height, header.getBlockHeader());
Platform.runLater(() -> EventManager.get().post(new NewBlockEvent(header.height, header.getBlockHeader())));
}
### src/test/java/com/sparrowwallet/sparrow/net/ElectrumServerTest.java
@@ -264,7 +264,7 @@ public void verifiesAgainstAMandatoryServerHoweverLowItSaysItIs() {
public void rejectsAnAnnouncedTipRegressingBelowTheLastPin() {
int maxCheckpointHeight = Network.MAINNET.getHeaderCheckpoints().getMaxHeight();
try {
- ElectrumServer.updateTipReceived(maxCheckpointHeight);
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight, BLOCK_800000_HEADER_HEX));
assertNotNull(ElectrumServer.getAnnouncedTipValidationError(tip(maxCheckpointHeight - 1, BLOCK_800000_HEADER_HEX)));
//The regression that would go unnoticed: a genuine current header paired with a height a couple of thousand blocks back
@@ -286,12 +286,12 @@ public void acceptsAnAnnouncedTipFromAServerStillCatchingUp() {
ElectrumServer.tipReachedCheckpoints = false;
assertNull(ElectrumServer.getAnnouncedTipValidationError(tip(maxCheckpointHeight - 2000, BLOCK_800000_HEADER_HEX)));
- ElectrumServer.updateTipReceived(maxCheckpointHeight - 2000);
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight - 2000, BLOCK_800000_HEADER_HEX));
assertNull(ElectrumServer.getAnnouncedTipValidationError(tip(maxCheckpointHeight - 1999, BLOCK_800000_HEADER_HEX)));
assertNull(ElectrumServer.getAnnouncedTipValidationError(tip(maxCheckpointHeight, BLOCK_800000_HEADER_HEX)));
//An ordinary reorg above the pin is not a regression
- ElectrumServer.updateTipReceived(maxCheckpointHeight + 2);
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight + 2, BLOCK_800000_HEADER_HEX));
assertNull(ElectrumServer.getAnnouncedTipValidationError(tip(maxCheckpointHeight + 1, BLOCK_800000_HEADER_HEX)));
} finally {
ElectrumServer.tipReachedCheckpoints = false;
@@ -307,7 +307,7 @@ public void acceptsAnAnnouncedTipFromAServerStillCatchingUp() {
public void acceptsAnAnnouncedTipFromANewServerBelowTheOneBefore() {
int maxCheckpointHeight = Network.MAINNET.getHeaderCheckpoints().getMaxHeight();
try {
- ElectrumServer.updateTipReceived(maxCheckpointHeight + 100);
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight + 100, BLOCK_800000_HEADER_HEX));
AppServices.setAnnouncedTip(new ChainTip(maxCheckpointHeight + 100, Network.MAINNET.getGenesisHeader()));
//Connecting to a server still catching up, which clears the record where the reading thread is started
@@ -319,6 +319,29 @@ public void acceptsAnAnnouncedTipFromANewServerBelowTheOneBefore() {
}
}
+ /**
+ * The stale tip warning measures time since the chain last progressed, so a tip announced again leaves the clock where it was, and a new block
+ * restarts it.
+ */
+ @Test
+ public void refreshesTheStaleTipClockOnlyForANewBlock() {
+ int maxCheckpointHeight = Network.MAINNET.getHeaderCheckpoints().getMaxHeight();
+ long staleSince = System.currentTimeMillis() - 3 * 60 * 60 * 1000;
+ try {
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight, BLOCK_800000_HEADER_HEX));
+ ElectrumServer.lastTipReceivedAt = staleSince;
+
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight, BLOCK_800000_HEADER_HEX));
+ assertEquals(staleSince, ElectrumServer.lastTipReceivedAt);
+
+ ElectrumServer.updateTipReceived(tip(maxCheckpointHeight + 1, GENESIS_HEADER_HEX));
+ assertTrue(ElectrumServer.lastTipReceivedAt > staleSince);
+ } finally {
+ ElectrumServer.tipReachedCheckpoints = false;
+ ElectrumServer.lastTipReceivedAt = 0;
+ }
+ }
+
/**
* The escape hatch. It defaults on and has no user interface, and one answer has to cover the header sync, both write boundaries and the connect
* time enforcement - a public server rejected for lacking a call nothing is going to make would be no use.Why this scored 51/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.