skip reorganising the header store when an announced tip it has already synced past names a replaced block the server no longer serves
What changed, and why it matters
This commit fixes a bug in how Sparrow Wallet's Electrum server integration handles blockchain reorganizations. When the wallet had already switched to a newer chain branch, but then received an old announcement naming a block that was replaced, it could incorrectly try to reorganize its header store and fail to serve a header it actually had. The fix makes the wallet recognize when the server's chain at a given height already matches what it has stored, so it skips unnecessary reorganization and continues serving the stored header.
Review the reconcile() logic for other edge cases where stale announced tips interact with concurrent header store updates. Ensure the fix is included in the next release and consider whether the underlying race condition between announcement handling and history thread sync needs broader synchronization.
Security signals we found
Incorrect blockchain reorganization handling could cause denial of service in header verification
Race condition between header sync announcements and history thread updates
Potential for wallet to fail serving verified headers due to stale announced tip
Fix adds defensive early-return based on hash equality at synced height
Evidence from the diff
In ElectrumServer.reconcile(), the code previously walked back from an announced tip to find a fork point against the local HeaderStore. If a history thread had already synced the store onto a new branch before the announcement arrived, the announced tip could name a replaced block the server no longer serves. The patch adds an early-exit check: if the last header in the fetched candidate chunk matches the hash already stored at endHeight, the server’s chain to that height is the same as the store’s, so no reorganization is needed. A test verifies that getVerifiedHeader() returns the stored header and no fork is recorded when the announced tip is a replaced block.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javasrc/test/java/com/sparrowwallet/sparrow/net/HeaderSyncTest.javaInspect captured patch +28 / −0
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -1829,6 +1829,12 @@ private void reconcile(HeaderStore store, int tipHeight) throws ServerException,
throw new VerificationException("Server returned a malformed header chunk when reconciling to height " + endHeight, e);
}
+ //The server's chain to this height is the one the store holds, as when an announcement the store has already synced past names a block it
+ //replaced. Each header commits to the one below it, so a matching top header means there is nothing to reorganise
+ if(candidate.getLast().getHash().equals(store.getHash(endHeight))) {
+ return;
+ }
+
//Walk back from the announced tip, checking each header against the one below it, until one descends from a header the store already holds
int forkHeight = -1;
for(int i = count - 1; i >= 0; i--) {
### src/test/java/com/sparrowwallet/sparrow/net/HeaderSyncTest.java
@@ -40,6 +40,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -468,6 +469,27 @@ public void reconcilesBeforeServingAStoredHeaderTheAnnouncedTipContradicts() thr
assertEquals(9, listener.getForkHeight());
}
+ /**
+ * A history thread can sync the store onto a new branch before the announcement of that branch arrives, leaving the announced tip naming a block
+ * the store has replaced. The server's chain to that height is the store's, so the stored header is served rather than refused.
+ */
+ @Test
+ public void servesAStoredHeaderWhenTheAnnouncedTipIsAReplacedBlock() throws Exception {
+ List<BlockHeader> chain = mineChain(Network.REGTEST.getGenesisHeader(), 10, CHAIN_TIME);
+ List<BlockHeader> replaced = fork(chain, 8, 1);
+ HeaderStore store = seedStore(chain, 10);
+ serve(chain);
+ AppServices.setAnnouncedTip(new ChainTip(9, replaced.getLast()));
+
+ BlockHeader verified = new ElectrumServer().getVerifiedHeader(10);
+
+ assertNotNull(verified);
+ assertEquals(chain.getLast().getHash(), verified.getHash());
+ assertEquals(chain.getLast().getHash(), store.getTipHash());
+ assertEquals(Integer.MAX_VALUE, ElectrumServer.lastReorgForkHeight);
+ assertNull(listener.getForkHeight());
+ }
+
@Test
public void servesAStoredHeaderWithoutFetchingWhileTheAnnouncedTipAgrees() throws Exception {
List<BlockHeader> chain = mineChain(Network.REGTEST.getGenesisHeader(), 10, CHAIN_TIME);Why this scored 46/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.