sync to the announced tip before serving a stored header when a reorg lengthens the chain, so a proof is not checked against a replaced block
What changed, and why it matters
This commit fixes a bug in how Sparrow Wallet verifies Bitcoin block headers during a blockchain reorganization (reorg). Previously, if a reorg made the chain longer, the wallet could serve an old, replaced block header to check a payment proof, instead of first syncing to the newly announced longer chain. This could let a malicious or mistaken server make a transaction proof look valid against the wrong block. The fix forces a sync to the announced tip before serving any stored header when the announced tip is newer or on a different branch than what is locally stored.
Treat this as a security-relevant correctness fix. Review whether released versions need a patch or advisory, especially because SPV/proof verification against wrong headers can lead to accepting invalid transactions. Users should upgrade to a release containing this commit. Wallet developers should audit other paths that serve stored headers before announced-tip reconciliation.
Security signals we found
Incorrect block header served during chain reorg
Proof verification could be checked against replaced block
Missing sync path when announced tip extends beyond local store
Fix includes regression tests for reorg handling
Logic change from else-if to independent if ensures syncHeadersTo runs after reorg sync
Evidence from the diff
In ElectrumServer.getVerifiedHeader(), the original condition only triggered a sync when the announced tip height was within the store’s current range AND its hash differed from the stored hash at that height. That missed the case where a reorg both replaces headers below the store tip and lengthens the chain (announced.height > store.tipHeight). In that case the store still contained replaced headers, and getVerifiedHeader() could return a stale header for proof verification. The patch changes the condition to sync whenever the announced tip is above the store tip OR its hash disagrees at the announced height, and it turns the subsequent height check into an independent if so syncHeadersTo(height) always runs when needed. New tests cover a lengthening reorg and an announced next-block extension.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javasrc/test/java/com/sparrowwallet/sparrow/net/HeaderSyncTest.javaElectrumServer.getVerifiedHeader()HeaderStore synchronization during reorgsInspect captured patch +39 / −5
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -1980,11 +1980,13 @@ public BlockHeader getVerifiedHeader(int height) throws ServerException {
try {
//One object written once per event: the height and the header read separately can straddle a new block
ChainTip announced = AppServices.getAnnouncedTip();
- if(announced != null && announced.height() >= store.getStartHeight() && announced.height() <= store.getTipHeight()
- && !announced.header().getHash().equals(store.getHash(announced.height()))) {
- //Never serve a stored header while the store tip disagrees with the announced tip: the tie form of a reorg
+ if(announced != null && announced.height() >= store.getStartHeight() && (announced.height() > store.getTipHeight()
+ || !announced.header().getHash().equals(store.getHash(announced.height())))) {
+ //Never serve a stored header until the store has reached the announced tip: a reorg replaces stored headers below the new tip
+ //whether it ties the store tip or lengthens the chain, and only reaching the tip shows which
syncHeaders(announced);
- } else if(height > store.getTipHeight()) {
+ }
+ if(height > store.getTipHeight()) {
syncHeadersTo(height); //the sync service has not caught up, so fetch on this thread
}
### src/test/java/com/sparrowwallet/sparrow/net/HeaderSyncTest.java
@@ -288,7 +288,7 @@ public void detectsAForkFromTheFirstHeaderOfAFetchedChunk() throws Exception {
BlockHeader verified = new ElectrumServer().getVerifiedHeader(11);
assertEquals(branch.get(10).getHash(), verified.getHash());
- assertEquals(11, store.getTipHeight());
+ assertEquals(12, store.getTipHeight());
assertEquals(branch.get(5).getHash(), store.getHeader(6).getHash());
assertEquals(5, ElectrumServer.lastReorgForkHeight);
assertEquals(5, listener.getForkHeight());
@@ -490,6 +490,38 @@ public void servesAStoredHeaderWhenTheAnnouncedTipIsAReplacedBlock() throws Exce
assertNull(listener.getForkHeight());
}
+ /**
+ * A reorg that also lengthens the chain announces a tip above the store, which still holds the replaced headers below it until the sync service
+ * catches up. A proof for a transaction in a replacing block has to be checked against that block, not the one it replaced.
+ */
+ @Test
+ public void reconcilesBeforeServingAStoredHeaderBelowAnAnnouncedTipOnAnotherBranch() throws Exception {
+ List<BlockHeader> chain = mineChain(Network.REGTEST.getGenesisHeader(), 10, CHAIN_TIME);
+ List<BlockHeader> branch = fork(chain, 8, 3);
+ HeaderStore store = seedStore(chain, 10);
+ serve(branch);
+ AppServices.setAnnouncedTip(new ChainTip(11, branch.getLast()));
+
+ BlockHeader verified = new ElectrumServer().getVerifiedHeader(10);
+
+ assertEquals(branch.get(9).getHash(), verified.getHash());
+ assertEquals(branch.getLast().getHash(), store.getTipHash());
+ assertEquals(8, listener.getForkHeight());
+ }
+
+ @Test
+ public void extendsTheStoreWithoutFetchingWhenTheAnnouncedTipIsTheNextBlock() throws Exception {
+ List<BlockHeader> chain = mineChain(Network.REGTEST.getGenesisHeader(), 11, CHAIN_TIME);
+ HeaderStore store = seedStore(chain, 10);
+ FakeElectrumServerRpc fake = serve(chain);
+ AppServices.setAnnouncedTip(new ChainTip(11, chain.getLast()));
+
+ assertEquals(chain.get(9).getHash(), new ElectrumServer().getVerifiedHeader(10).getHash());
+ assertEquals(11, store.getTipHeight());
+ assertEquals(0, fake.getChunkRequests());
+ assertNull(listener.getForkHeight());
+ }
+
@Test
public void servesAStoredHeaderWithoutFetchingWhileTheAnnouncedTipAgrees() throws Exception {
List<BlockHeader> chain = mineChain(Network.REGTEST.getGenesisHeader(), 10, CHAIN_TIME);Why this scored 69/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.