compare a re-announced chain tip by block when deciding whether to retry or refuse it
What changed, and why it matters
This commit fixes a logic bug in how Sparrow Wallet decides whether to retry or reject a newly announced blockchain tip from an Electrum server. Previously, the code compared tip objects using Java's == operator, which checks whether two variables point to the exact same object in memory rather than whether they represent the same block. Because a re-announced block is a new object with the same height and header hash, the old comparison could treat a repeated announcement of the same block as a different tip. That could cause the wallet to incorrectly retry failed header fetches or wrongly refuse a tip after a transient error, potentially leaving the wallet out of sync or stuck on a bad chain tip. The fix introduces an isSameBlock method that compares height and block hash instead of object identity.
Review whether the old reference-equality behavior could have caused observable sync failures or chain-tip refusal issues in production, and consider backporting the fix. Users relying on Electrum server connections should upgrade to a release containing this commit. No immediate emergency response is indicated, but the fix should be included in the next maintenance release.
Security signals we found
Logic bug: reference equality used where value equality (same block) was intended
Potential denial of wallet sync: repeated same-block announcements could bypass retry/refuse logic
No input validation changes; fix is localized to identity-vs-value comparison
New unit test added for the equality semantics
Evidence from the diff
ChainTip is a Java record holding a block height and BlockHeader. The old code in ElectrumServer used reference equality (tip != failedTip and tip != announcedTip) to detect duplicate chain-tip announcements. Since each server announcement creates a new ChainTip instance, reference equality fails for the same block announced twice. The patch adds ChainTip.isSameBlock(ChainTip), which compares height and header.getHash() (with null safety and identity short-circuit), and replaces the two reference-equality checks in ElectrumServer with calls to isSameBlock. A unit test verifies that two distinct ChainTip objects with the same height and header hash are considered the same block, while different nonces, heights, or null headers are not.
Changed components
com.sparrowwallet.sparrow.ChainTipcom.sparrowwallet.sparrow.net.ElectrumServerElectrum server chain-tip announcement handlingInspect captured patch +39 / −3
### src/main/java/com/sparrowwallet/sparrow/ChainTip.java
@@ -6,4 +6,12 @@
* The height and header of a chain tip, carried together so that a reader cannot take the height of one block with the header of another.
* Whether a tip is one a server has announced or one that has been verified is said by the accessor or the field holding it, not by this type.
*/
-public record ChainTip(int height, BlockHeader header) {}
+public record ChainTip(int height, BlockHeader header) {
+ /**
+ * Whether the other tip is the same block, announced again or not. The header has no value equality of its own, so a repeated announcement of
+ * a block is a different object that only its height and hash identify.
+ */
+ public boolean isSameBlock(ChainTip other) {
+ return this == other || (other != null && height == other.height && header != null && other.header != null && header.getHash().equals(other.header.getHash()));
+ }
+}
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -3719,7 +3719,7 @@ protected Void call() throws Exception {
} catch(ServerException | ElectrumServerRpcException e) {
//A failed call says nothing about the chain on its own, but a server answering every request for these headers with an error
//has not substantiated the tip any more than one serving the wrong headers
- if(failedTip != tip || !isConnected()) {
+ if(!tip.isSameBlock(failedTip) || !isConnected()) {
failedTip = tip;
throw e;
}
@@ -3742,7 +3742,7 @@ private void refuseAnnouncedTip(ChainTip tip, Exception e) throws ServerExceptio
ChainTip storeTip = getStoreTip();
Platform.runLater(() -> {
//A run overtaken by a later announcement has nothing left to correct
- if(tip != announcedTip) {
+ if(!tip.isSameBlock(announcedTip)) {
return;
}
### src/test/java/com/sparrowwallet/sparrow/ChainTipTest.java
@@ -0,0 +1,28 @@
+package com.sparrowwallet.sparrow;
+
+import com.sparrowwallet.drongo.protocol.BlockHeader;
+import com.sparrowwallet.drongo.protocol.Sha256Hash;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ChainTipTest {
+ private static BlockHeader getHeader(long nonce) {
+ return new BlockHeader(1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, null, 1700000000L, 0x207fffffL, nonce);
+ }
+
+ @Test
+ public void sameBlockAnnouncedAgain() {
+ ChainTip tip = new ChainTip(100, getHeader(1));
+ Assertions.assertTrue(tip.isSameBlock(new ChainTip(100, getHeader(1))));
+ Assertions.assertTrue(tip.isSameBlock(tip));
+ }
+
+ @Test
+ public void differentBlock() {
+ ChainTip tip = new ChainTip(100, getHeader(1));
+ Assertions.assertFalse(tip.isSameBlock(new ChainTip(100, getHeader(2))));
+ Assertions.assertFalse(tip.isSameBlock(new ChainTip(101, getHeader(1))));
+ Assertions.assertFalse(tip.isSameBlock(new ChainTip(100, null)));
+ Assertions.assertFalse(tip.isSameBlock(null));
+ }
+}Why this scored 42/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.