3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 23:03:04 +00:00

Wallet: Add some convenience methods for spending unconfirmed transactions.

This commit is contained in:
Mike Hearn 2013-04-11 18:15:14 +02:00
parent f1845dd552
commit 5eebca2c7d
2 changed files with 30 additions and 7 deletions

View File

@ -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(); private transient CoinSelector coinSelector = new DefaultCoinSelector();
// The keyCrypter for the wallet. This specifies the algorithm used for encrypting and decrypting the private keys. // 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, * Removes the given event listener object. Returns true if the listener was removed, false if that listener
* false if that listener was never added. * was never added.
*/ */
public boolean removeEventListener(WalletEventListener listener) { public boolean removeEventListener(WalletEventListener listener) {
return eventListeners.remove(listener); return eventListeners.remove(listener);
@ -3048,6 +3066,15 @@ public class Wallet implements Serializable, BlockChainListener {
} }
} }
/**
* Convenience wrapper for <tt>setCoinSelector(Wallet.AllowUnconfirmedCoinSelector.get())</tt>. 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. // Boilerplate for running event listeners - unlocks the wallet, runs, re-locks.

View File

@ -426,11 +426,7 @@ public class WalletTool {
Wallet.SendRequest req = Wallet.SendRequest.forTx(t); Wallet.SendRequest req = Wallet.SendRequest.forTx(t);
req.fee = fee; req.fee = fee;
if (allowUnconfirmed) { if (allowUnconfirmed) {
wallet.setCoinSelector(new Wallet.DefaultCoinSelector() { wallet.allowSpendingUnconfirmedTransactions();
@Override protected boolean shouldSelect(Transaction tx) {
return true; // Accept any transaction that's spendable.
}
});
} }
if (password != null) { if (password != null) {
if (!wallet.checkPassword(password)) { if (!wallet.checkPassword(password)) {