3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Wallet: Split CoinSelection out into a top level interface in the wallet package.

This commit is contained in:
Mike Hearn 2013-09-20 16:47:42 +02:00
parent c4405d3ee7
commit c366c5fa44
4 changed files with 28 additions and 15 deletions

View File

@ -25,6 +25,7 @@ import com.google.bitcoin.store.UnreadableWalletException;
import com.google.bitcoin.store.WalletProtobufSerializer; import com.google.bitcoin.store.WalletProtobufSerializer;
import com.google.bitcoin.utils.ListenerRegistration; import com.google.bitcoin.utils.ListenerRegistration;
import com.google.bitcoin.utils.Threading; import com.google.bitcoin.utils.Threading;
import com.google.bitcoin.wallet.CoinSelection;
import com.google.bitcoin.wallet.CoinSelector; import com.google.bitcoin.wallet.CoinSelector;
import com.google.bitcoin.wallet.KeyTimeCoinSelector; import com.google.bitcoin.wallet.KeyTimeCoinSelector;
import com.google.bitcoin.wallet.WalletFiles; import com.google.bitcoin.wallet.WalletFiles;
@ -155,16 +156,6 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
private volatile long vKeyRotationTimestamp; private volatile long vKeyRotationTimestamp;
private volatile boolean vKeyRotationEnabled; 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<TransactionOutput> gathered;
public CoinSelection(BigInteger valueGathered, Collection<TransactionOutput> gathered) {
this.valueGathered = valueGathered;
this.gathered = gathered;
}
}
/** /**
* This class implements a {@link CoinSelector} which attempts to get the highest priority possible. This means that * 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 * the transaction is the most likely to get confirmed

View File

@ -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<TransactionOutput> gathered;
public CoinSelection(BigInteger valueGathered, Collection<TransactionOutput> gathered) {
this.valueGathered = valueGathered;
this.gathered = gathered;
}
}

View File

@ -1,7 +1,6 @@
package com.google.bitcoin.wallet; package com.google.bitcoin.wallet;
import com.google.bitcoin.core.TransactionOutput; import com.google.bitcoin.core.TransactionOutput;
import com.google.bitcoin.core.Wallet;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.LinkedList; 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 * 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 * 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. * enough money in the wallet.
*/ */
public interface CoinSelector { public interface CoinSelector {
public Wallet.CoinSelection select(BigInteger target, LinkedList<TransactionOutput> candidates); public CoinSelection select(BigInteger target, LinkedList<TransactionOutput> candidates);
} }

View File

@ -46,7 +46,7 @@ public class KeyTimeCoinSelector implements CoinSelector {
} }
@Override @Override
public Wallet.CoinSelection select(BigInteger target, LinkedList<TransactionOutput> candidates) { public CoinSelection select(BigInteger target, LinkedList<TransactionOutput> candidates) {
try { try {
LinkedList<TransactionOutput> gathered = Lists.newLinkedList(); LinkedList<TransactionOutput> gathered = Lists.newLinkedList();
BigInteger valueGathered = BigInteger.ZERO; BigInteger valueGathered = BigInteger.ZERO;
@ -74,7 +74,7 @@ public class KeyTimeCoinSelector implements CoinSelector {
break; break;
} }
} }
return new Wallet.CoinSelection(valueGathered, gathered); return new CoinSelection(valueGathered, gathered);
} catch (ScriptException e) { } catch (ScriptException e) {
throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet. throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet.
} }