From 5eebca2c7dd7a5c33097a9f050587dcbad24d3a4 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 11 Apr 2013 18:15:14 +0200 Subject: [PATCH] Wallet: Add some convenience methods for spending unconfirmed transactions. --- .../java/com/google/bitcoin/core/Wallet.java | 31 +++++++++++++++++-- .../com/google/bitcoin/tools/WalletTool.java | 6 +--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index 4e51a698..0a052092 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -295,6 +295,24 @@ public class Wallet implements Serializable, BlockChainListener { } } + /** + * This coin selector will select any transaction at all, regardless of where it came from or whether it was + * confirmed yet. + */ + public static class AllowUnconfirmedCoinSelector extends DefaultCoinSelector { + @Override protected boolean shouldSelect(Transaction tx) { + return true; + } + + private static AllowUnconfirmedCoinSelector instance; + public static AllowUnconfirmedCoinSelector get() { + // This doesn't have to be thread safe as the object has no state, so discarded duplicates are harmless. + if (instance == null) + instance = new AllowUnconfirmedCoinSelector(); + return instance; + } + } + private transient CoinSelector coinSelector = new DefaultCoinSelector(); // The keyCrypter for the wallet. This specifies the algorithm used for encrypting and decrypting the private keys. @@ -1403,8 +1421,8 @@ public class Wallet implements Serializable, BlockChainListener { } /** - * Removes the given event listener object. Returns true if the listener was removed, - * false if that listener was never added. + * Removes the given event listener object. Returns true if the listener was removed, false if that listener + * was never added. */ public boolean removeEventListener(WalletEventListener listener) { return eventListeners.remove(listener); @@ -3048,6 +3066,15 @@ public class Wallet implements Serializable, BlockChainListener { } } + /** + * Convenience wrapper for setCoinSelector(Wallet.AllowUnconfirmedCoinSelector.get()). If this method + * is called on the wallet then transactions will be used for spending regardless of their confidence. This can + * be dangerous - only use this if you absolutely know what you're doing! + */ + public void allowSpendingUnconfirmedTransactions() { + setCoinSelector(Wallet.AllowUnconfirmedCoinSelector.get()); + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Boilerplate for running event listeners - unlocks the wallet, runs, re-locks. diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 064fcf85..8d66cb9c 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -426,11 +426,7 @@ public class WalletTool { Wallet.SendRequest req = Wallet.SendRequest.forTx(t); req.fee = fee; if (allowUnconfirmed) { - wallet.setCoinSelector(new Wallet.DefaultCoinSelector() { - @Override protected boolean shouldSelect(Transaction tx) { - return true; // Accept any transaction that's spendable. - } - }); + wallet.allowSpendingUnconfirmedTransactions(); } if (password != null) { if (!wallet.checkPassword(password)) {