From c366c5fa449ed679e8943995574584eb136f77d3 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 20 Sep 2013 16:47:42 +0200 Subject: [PATCH] Wallet: Split CoinSelection out into a top level interface in the wallet package. --- .../java/com/google/bitcoin/core/Wallet.java | 11 +-------- .../google/bitcoin/wallet/CoinSelection.java | 23 +++++++++++++++++++ .../google/bitcoin/wallet/CoinSelector.java | 5 ++-- .../bitcoin/wallet/KeyTimeCoinSelector.java | 4 ++-- 4 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 core/src/main/java/com/google/bitcoin/wallet/CoinSelection.java 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 09cf97ee..1d4e36ef 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -25,6 +25,7 @@ import com.google.bitcoin.store.UnreadableWalletException; import com.google.bitcoin.store.WalletProtobufSerializer; import com.google.bitcoin.utils.ListenerRegistration; import com.google.bitcoin.utils.Threading; +import com.google.bitcoin.wallet.CoinSelection; import com.google.bitcoin.wallet.CoinSelector; import com.google.bitcoin.wallet.KeyTimeCoinSelector; import com.google.bitcoin.wallet.WalletFiles; @@ -155,16 +156,6 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi private volatile long vKeyRotationTimestamp; private volatile boolean vKeyRotationEnabled; - /** Represents the results of a {@link CoinSelector#select(java.math.BigInteger, java.util.LinkedList)} operation */ - public static class CoinSelection { - public BigInteger valueGathered; - public Collection gathered; - public CoinSelection(BigInteger valueGathered, Collection gathered) { - this.valueGathered = valueGathered; - this.gathered = gathered; - } - } - /** * This class implements a {@link CoinSelector} which attempts to get the highest priority possible. This means that * the transaction is the most likely to get confirmed diff --git a/core/src/main/java/com/google/bitcoin/wallet/CoinSelection.java b/core/src/main/java/com/google/bitcoin/wallet/CoinSelection.java new file mode 100644 index 00000000..90104e8c --- /dev/null +++ b/core/src/main/java/com/google/bitcoin/wallet/CoinSelection.java @@ -0,0 +1,23 @@ +package com.google.bitcoin.wallet; + +import com.google.bitcoin.core.TransactionOutput; + +import java.math.BigInteger; +import java.util.Collection; + +/** + * Represents the results of a + * {@link com.google.bitcoin.wallet.CoinSelector#select(java.math.BigInteger, java.util.LinkedList)} operation. A + * coin selection represents a list of spendable transaction outputs that sum together to give valueGathered. + * Different coin selections could be produced by different coin selectors from the same input set, according + * to their varying policies. + */ +public class CoinSelection { + public BigInteger valueGathered; + public Collection gathered; + + public CoinSelection(BigInteger valueGathered, Collection gathered) { + this.valueGathered = valueGathered; + this.gathered = gathered; + } +} diff --git a/core/src/main/java/com/google/bitcoin/wallet/CoinSelector.java b/core/src/main/java/com/google/bitcoin/wallet/CoinSelector.java index 4742f40a..a68dcc58 100644 --- a/core/src/main/java/com/google/bitcoin/wallet/CoinSelector.java +++ b/core/src/main/java/com/google/bitcoin/wallet/CoinSelector.java @@ -1,7 +1,6 @@ package com.google.bitcoin.wallet; import com.google.bitcoin.core.TransactionOutput; -import com.google.bitcoin.core.Wallet; import java.math.BigInteger; import java.util.LinkedList; @@ -9,9 +8,9 @@ import java.util.LinkedList; /** * A CoinSelector is responsible for picking some outputs to spend, from the list of all spendable outputs. It * allows you to customize the policies for creation of transactions to suit your needs. The select operation - * may return a {@link com.google.bitcoin.core.Wallet.CoinSelection} that has a valueGathered lower than the requested target, if there's not + * may return a {@link CoinSelection} that has a valueGathered lower than the requested target, if there's not * enough money in the wallet. */ public interface CoinSelector { - public Wallet.CoinSelection select(BigInteger target, LinkedList candidates); + public CoinSelection select(BigInteger target, LinkedList candidates); } diff --git a/core/src/main/java/com/google/bitcoin/wallet/KeyTimeCoinSelector.java b/core/src/main/java/com/google/bitcoin/wallet/KeyTimeCoinSelector.java index 00fb4c0b..3e38b5c7 100644 --- a/core/src/main/java/com/google/bitcoin/wallet/KeyTimeCoinSelector.java +++ b/core/src/main/java/com/google/bitcoin/wallet/KeyTimeCoinSelector.java @@ -46,7 +46,7 @@ public class KeyTimeCoinSelector implements CoinSelector { } @Override - public Wallet.CoinSelection select(BigInteger target, LinkedList candidates) { + public CoinSelection select(BigInteger target, LinkedList candidates) { try { LinkedList gathered = Lists.newLinkedList(); BigInteger valueGathered = BigInteger.ZERO; @@ -74,7 +74,7 @@ public class KeyTimeCoinSelector implements CoinSelector { break; } } - return new Wallet.CoinSelection(valueGathered, gathered); + return new CoinSelection(valueGathered, gathered); } catch (ScriptException e) { throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet. }