show a transaction tab confirmation as unverified when the block the shown transaction carries has been replaced by a reorg
What changed, and why it matters
This commit fixes a display bug in Sparrow Wallet where a transaction could still be shown as 'confirmed' even after the Bitcoin block containing it was replaced by a blockchain reorganization (reorg). The wallet now checks whether the block it previously proved is still in its header store. If the block was orphaned by a reorg, the transaction is shown as unverified until it can be proven against the replacement block. This is a UI/verification correctness issue, not a direct loss-of-funds bug, but it could mislead a user into thinking a payment is settled when it no longer is.
Users and operators should upgrade to a Sparrow release containing this commit. Until then, after any known or suspected blockchain reorganization, users should not rely solely on the transaction tab's 'confirmed' status for finality; verify the transaction independently or wait for additional confirmations. Developers should review other places where cached BlockTransaction objects may carry stale block hashes.
Security signals we found
Blockchain reorg handling
Transaction confirmation status correctness
Block header verification
UI state desynchronization after reorg
New unit test covering reorg scenario
Evidence from the diff
The patch changes ElectrumServer.getProvenTransaction() to call isProvenAgainstOrphanedHeader() after isProven() succeeds. If the stored block header for the transaction’s height no longer matches the block hash carried by the BlockTransaction object, the method strips the block hash/date and returns the transaction as unproven. A new test, doesNotCarryABlockAReorgHasReplaced(), verifies that a pre-reorg BlockTransaction is demoted to unverified, and that after the replacement header is proven it carries the new block hash. isProvenAgainstOrphanedHeader() is also made static so it can be called from the static getProvenTransaction().
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javasrc/test/java/com/sparrowwallet/sparrow/net/TransactionProofTest.javaInspect captured patch +61 / −10
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -1332,7 +1332,7 @@ private void fetchSpentTransactions(Wallet wallet, Collection<Transaction> trans
* longer holds, which is the form a reorg takes when a transaction is re-included at the same height. Above the last fork point this session only,
* so an ordinary pass reads neither the store nor the disk.
*/
- private boolean isProvenAgainstOrphanedHeader(BlockTransaction existing) throws ServerException {
+ private static boolean isProvenAgainstOrphanedHeader(BlockTransaction existing) throws ServerException {
if(existing.getBlockHash() == null || existing.getHeight() <= lastReorgForkHeight) {
return false; //history from before this feature, or a height no reorg this session has reached
}
@@ -1492,13 +1492,27 @@ public static boolean isProven(BlockTransaction blockTransaction) {
/**
* The given transaction carrying the block it was proven to be in, where this session has proved its height, and the transaction itself where it
* has not. The proof outlives the objects a wallet or a fetch hands out, none of which carry what was established after they were built, so what
- * has been proven is asked here rather than read from whichever of them a form was last handed.
+ * has been proven is asked here rather than read from whichever of them a form was last handed. For the same reason a block one of them carries
+ * that a reorg has since replaced is not taken as proven: the object may have been handed out before the reorg reached the wallet that holds it.
*/
public static BlockTransaction getProvenTransaction(BlockTransaction blockTransaction) {
- if(blockTransaction.getHeight() <= 0 || isProven(blockTransaction)) {
+ if(blockTransaction.getHeight() <= 0) {
return blockTransaction;
}
+ if(isProven(blockTransaction)) {
+ try {
+ if(!isProvenAgainstOrphanedHeader(blockTransaction)) {
+ return blockTransaction;
+ }
+ } catch(ServerException e) {
+ log.warn("Could not compare the block of transaction " + blockTransaction.getHashAsString() + " with the header store", e);
+ return blockTransaction;
+ }
+
+ blockTransaction = new BlockTransaction(blockTransaction.getHash(), blockTransaction.getHeight(), null, blockTransaction.getFee(), blockTransaction.getTransaction(), null);
+ }
+
BlockHeader provenHeader = provenTransactionHeaders.get(blockTransaction.getHashAsString() + ":" + blockTransaction.getHeight());
if(provenHeader == null) {
return blockTransaction;
### src/test/java/com/sparrowwallet/sparrow/net/TransactionProofTest.java
@@ -327,6 +327,31 @@ public void carriesAProofOntoATransactionBuiltWithoutIt() throws Exception {
assertEquals(PROVEN_HEIGHT, proven.getHeight());
}
+ /**
+ * A transaction handed out before a reorg reached the wallet holding it still carries the block that was replaced, and is not shown as proven
+ * whichever object a form is redrawn from. Proven again against the block that replaced it, it carries that one. One carrying a block the store
+ * still holds is returned as it is, at a height a reorg this session has reached as well as below it.
+ */
+ @Test
+ public void doesNotCarryABlockAReorgHasReplaced() throws Exception {
+ Transaction transaction = blockTransactions.getFirst();
+ BlockHeader proven = chain.get(PROVEN_HEIGHT - 1);
+ BlockTransaction held = new BlockTransaction(transaction.getTxId(), PROVEN_HEIGHT, proven.getTimeAsDate(), 0L, transaction, proven.getHash());
+ assertSame(held, ElectrumServer.getProvenTransaction(held));
+ ElectrumServer.lastReorgForkHeight = PROVEN_HEIGHT - 1;
+ assertSame(held, ElectrumServer.getProvenTransaction(held));
+
+ List<BlockHeader> replacement = reorganiseStoreAbove(PROVEN_HEIGHT - 1);
+
+ BlockTransaction shown = ElectrumServer.getProvenTransaction(held);
+ assertFalse(ElectrumServer.isProven(shown));
+ assertEquals(PROVEN_HEIGHT, shown.getHeight());
+ assertNull(shown.getDate());
+
+ ElectrumServer.provenTransactionHeaders.put(transaction.getTxId() + ":" + PROVEN_HEIGHT, replacement.getFirst());
+ assertEquals(replacement.getFirst().getHash(), ElectrumServer.getProvenTransaction(held).getBlockHash());
+ }
+
/**
* Only what was proven is remembered. A server that would not prove it is not necessarily the server that will be asked next, and caching the
* refusal would outlast the connection that earned it. Refused for a whole budget here, since anything less is recovered from within one call.
@@ -1103,13 +1128,7 @@ private ElectrumServer reorganisingBeforeOutputs() {
@Override
public void calculateNodeHistory(Wallet calculatingWallet, Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap) {
try {
- HeaderStore store = ElectrumServer.getHeaderStore();
- store.truncate(PROVEN_HEIGHT - 1);
- BlockHeader previous = chain.get(PROVEN_HEIGHT - 2);
- for(int height = PROVEN_HEIGHT; height < CHAIN_LENGTH; height++) {
- previous = mineHeader(previous, Sha256Hash.ZERO_HASH, CHAIN_TIME + 100 + height);
- store.append(previous);
- }
+ reorganiseStoreAbove(PROVEN_HEIGHT - 1);
} catch(Exception e) {
throw new IllegalStateException(e);
}
@@ -1281,6 +1300,24 @@ public void refetchesADemotedNodeWhenSubscribingAfresh() throws Exception {
assertEquals(1, demoted.getTransactionOutputs().size()); //cache state only: the node's own outputs are untouched
}
+ /**
+ * Replaces the blocks the store holds above the given height with empty ones as far as it reached, returning those that replaced them.
+ */
+ private List<BlockHeader> reorganiseStoreAbove(int forkHeight) throws Exception {
+ HeaderStore store = ElectrumServer.getHeaderStore();
+ int tipHeight = store.getTipHeight();
+ store.truncate(forkHeight);
+ List<BlockHeader> replacement = new ArrayList<>();
+ BlockHeader previous = chain.get(forkHeight - 1);
+ for(int height = forkHeight + 1; height <= tipHeight; height++) {
+ previous = mineHeader(previous, Sha256Hash.ZERO_HASH, CHAIN_TIME + 100 + height);
+ store.append(previous);
+ replacement.add(previous);
+ }
+
+ return replacement;
+ }
+
private static Map<WalletNode, Set<BlockTransactionHash>> references(Wallet wallet, BlockTransactionHash... references) {
WalletNode node = wallet.getNode(KeyPurpose.RECEIVE).getChildren().iterator().next();
Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap = new LinkedHashMap<>();Why this scored 43/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.