diff --git a/core/src/main/java/org/bitcoinj/core/Wallet.java b/core/src/main/java/org/bitcoinj/core/Wallet.java index 7fd561a0..9e609f8a 100644 --- a/core/src/main/java/org/bitcoinj/core/Wallet.java +++ b/core/src/main/java/org/bitcoinj/core/Wallet.java @@ -2474,6 +2474,25 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha } } + /** + * Prepares the wallet for a blockchain replay. Removes all transactions (as they would get in the way of the + * replay) and makes the wallet think it has never seen a block. {@link WalletEventListener#onWalletChanged()} will + * be fired. + */ + public void reset() { + lock.lock(); + try { + clearTransactions(); + lastBlockSeenHash = null; + lastBlockSeenHeight = -1; // Magic value for 'never'. + lastBlockSeenTimeSecs = 0; + saveLater(); + maybeQueueOnWalletChanged(); + } finally { + lock.unlock(); + } + } + /** * Deletes transactions which appeared above the given block height from the wallet, but does not touch the keys. * This is useful if you have some keys and wish to replay the block chain into the wallet in order to pick them up. @@ -2483,11 +2502,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha lock.lock(); try { if (fromHeight == 0) { - unspent.clear(); - spent.clear(); - pending.clear(); - dead.clear(); - transactions.clear(); + clearTransactions(); saveLater(); } else { throw new UnsupportedOperationException(); @@ -2497,6 +2512,14 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha } } + private void clearTransactions() { + unspent.clear(); + spent.clear(); + pending.clear(); + dead.clear(); + transactions.clear(); + } + /** * Returns all the outputs that match addresses or scripts added via {@link #addWatchedAddress(Address)} or * {@link #addWatchedScripts(java.util.List)}. diff --git a/core/src/main/java/org/bitcoinj/core/WalletEventListener.java b/core/src/main/java/org/bitcoinj/core/WalletEventListener.java index 370a0a8e..3759bb80 100644 --- a/core/src/main/java/org/bitcoinj/core/WalletEventListener.java +++ b/core/src/main/java/org/bitcoinj/core/WalletEventListener.java @@ -107,7 +107,8 @@ public interface WalletEventListener extends KeyChainEventListener { *
When this is called you can refresh the UI contents from the wallet contents. It's more efficient to use