use the latest mempool data in the block summary service, and reset it on reconnecting
What changed, and why it matters
This commit changes how Sparrow Wallet caches and reuses recent Bitcoin mempool transactions when showing block summaries. It stores the most recently fetched mempool snapshot and only reuses it for up to 10 minutes and only if the blockchain tip has not advanced. It also clears that cached snapshot when the server reconnects. The change appears aimed at preventing stale or wrong mempool data from being shown to users after reconnections or tip changes.
Treat as a hardening/defensive fix. Review whether 600 seconds is an appropriate staleness window and confirm the cache is reset in all reconnection/error paths. No urgent user action required absent additional disclosure.
Security signals we found
Stale mempool data could be broadcast or displayed after reconnection or tip advance
New validation gate enforces freshness (600s) and height match before reusing cached mempool transactions
Cache reset on reconnect reduces risk of stale state persisting across sessions
No explicit security claim or CVE reference in commit
Evidence from the diff
ElectrumServer.BlockSummaryService now keeps a static volatile latestRecent field (a new RecentTransactions record storing height, fetch timestamp, and transaction list). When broadcasting recent transactions, it calls getBroadcastableRecent() to verify the cached data matches the current block height and is no older than RECENT_BROADCAST_MAX_DELAY_SECS (600 seconds). On server reconnection, latestRecent is reset to null. A unit test file verifies the age/height validation logic.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javaBlockSummaryServiceRecentTransactions recordsubscribeRecent / broadcastRecent logicInspect captured patch +69 / −3
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -160,6 +160,10 @@ public class ElectrumServer {
final static Map<String, BlockTransaction> confirmingRecent = new ConcurrentHashMap<>();
+ static final int RECENT_BROADCAST_MAX_DELAY_SECS = 600;
+
+ private static volatile RecentTransactions latestRecent;
+
static ElectrumServerRpc electrumServerRpc = new SimpleElectrumServerRpc();
private static Cormorant cormorant;
@@ -3453,6 +3457,7 @@ protected FeeRatesUpdatedEvent call() throws ServerException {
}
initializeTip(tip);
subscribedScriptHashes.clear();
+ latestRecent = null;
} else {
tip = new BlockHeaderTip();
}
@@ -4318,18 +4323,20 @@ private void subscribeRecent(ElectrumServer electrumServer, int currentHeight) {
}
}
+ latestRecent = new RecentTransactions(currentHeight, System.currentTimeMillis(), recentTransactions);
if(!recentTransactions.isEmpty()) {
- broadcastRecent(electrumServer, recentTransactions);
+ broadcastRecent(electrumServer);
}
}
- private void broadcastRecent(ElectrumServer electrumServer, List<BlockTransaction> recentTransactions) {
+ private void broadcastRecent(ElectrumServer electrumServer) {
ScheduledService<Void> broadcastService = new ScheduledService<>() {
@Override
protected Task<Void> createTask() {
return new Task<>() {
@Override
protected Void call() throws Exception {
+ List<BlockTransaction> recentTransactions = getBroadcastableRecent(latestRecent, AppServices.getCurrentBlockHeight(), System.currentTimeMillis());
if(!recentTransactions.isEmpty()) {
Random random = new Random();
if(random.nextBoolean()) {
@@ -4345,14 +4352,24 @@ protected Void call() throws Exception {
};
}
};
- broadcastService.setDelay(Duration.seconds(Math.random() * 60 * 10));
+ broadcastService.setDelay(Duration.seconds(Math.random() * RECENT_BROADCAST_MAX_DELAY_SECS));
broadcastService.setPeriod(Duration.hours(1));
broadcastService.setOnSucceeded(_ -> broadcastService.cancel());
broadcastService.setOnFailed(_ -> broadcastService.cancel());
broadcastService.start();
}
+
+ static List<BlockTransaction> getBroadcastableRecent(RecentTransactions recent, Integer currentHeight, long now) {
+ if(recent != null && currentHeight != null && recent.height() == currentHeight && now - recent.fetchedMillis() <= RECENT_BROADCAST_MAX_DELAY_SECS * 1000L) {
+ return recent.transactions();
+ }
+
+ return Collections.emptyList();
+ }
}
+ record RecentTransactions(int height, long fetchedMillis, List<BlockTransaction> transactions) {}
+
public static class WalletDiscoveryService extends Service<Optional<List<Wallet>>> {
private final List<Wallet> wallets;
### src/test/java/com/sparrowwallet/sparrow/net/RecentTransactionsTest.java
@@ -0,0 +1,49 @@
+package com.sparrowwallet.sparrow.net;
+
+import com.sparrowwallet.drongo.protocol.Sha256Hash;
+import com.sparrowwallet.drongo.wallet.BlockTransaction;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class RecentTransactionsTest {
+ private static final long FETCHED = 1_000_000_000L;
+
+ private static final List<BlockTransaction> TRANSACTIONS = List.of(new BlockTransaction(Sha256Hash.ZERO_HASH, 0, null, 0L, null));
+
+ @Test
+ public void testFetchedAtCurrentTipIsUsable() {
+ ElectrumServer.RecentTransactions recent = new ElectrumServer.RecentTransactions(100, FETCHED, TRANSACTIONS);
+ assertEquals(TRANSACTIONS, ElectrumServer.BlockSummaryService.getBroadcastableRecent(recent, 100, FETCHED + 60_000L));
+ }
+
+ @Test
+ public void testUsableUpToTheLongestDelay() {
+ ElectrumServer.RecentTransactions recent = new ElectrumServer.RecentTransactions(100, FETCHED, TRANSACTIONS);
+ long latest = FETCHED + ElectrumServer.RECENT_BROADCAST_MAX_DELAY_SECS * 1000L;
+ assertEquals(TRANSACTIONS, ElectrumServer.BlockSummaryService.getBroadcastableRecent(recent, 100, latest));
+ }
+
+ @Test
+ public void testFetchedBeforeCurrentTipIsNotUsable() {
+ ElectrumServer.RecentTransactions recent = new ElectrumServer.RecentTransactions(100, FETCHED, TRANSACTIONS);
+ assertTrue(ElectrumServer.BlockSummaryService.getBroadcastableRecent(recent, 101, FETCHED + 60_000L).isEmpty());
+ }
+
+ @Test
+ public void testOlderThanTheLongestDelayIsNotUsable() {
+ ElectrumServer.RecentTransactions recent = new ElectrumServer.RecentTransactions(100, FETCHED, TRANSACTIONS);
+ long late = FETCHED + ElectrumServer.RECENT_BROADCAST_MAX_DELAY_SECS * 1000L + 1;
+ assertTrue(ElectrumServer.BlockSummaryService.getBroadcastableRecent(recent, 100, late).isEmpty());
+ }
+
+ @Test
+ public void testNothingFetchedOrUnknownTipIsNotUsable() {
+ ElectrumServer.RecentTransactions recent = new ElectrumServer.RecentTransactions(100, FETCHED, TRANSACTIONS);
+ assertTrue(ElectrumServer.BlockSummaryService.getBroadcastableRecent(null, 100, FETCHED).isEmpty());
+ assertTrue(ElectrumServer.BlockSummaryService.getBroadcastableRecent(recent, null, FETCHED).isEmpty());
+ }
+}Why this scored 25/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.