3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 10:45:51 +00:00

DefaultCoinSelector: no-op code simplification

This commit is contained in:
Mike Hearn 2015-04-16 14:14:10 +01:00
parent f2eaf4a562
commit e9bffdda18
2 changed files with 4 additions and 5 deletions

View File

@ -6,7 +6,7 @@ import org.bitcoinj.core.TransactionOutput;
import java.util.List; import java.util.List;
/** /**
* 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 possible 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 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.

View File

@ -18,22 +18,21 @@ import java.util.*;
*/ */
public class DefaultCoinSelector implements CoinSelector { public class DefaultCoinSelector implements CoinSelector {
@Override @Override
public CoinSelection select(Coin biTarget, List<TransactionOutput> candidates) { public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
long target = biTarget.value;
ArrayList<TransactionOutput> selected = new ArrayList<TransactionOutput>(); ArrayList<TransactionOutput> selected = new ArrayList<TransactionOutput>();
// Sort the inputs by age*value so we get the highest "coindays" spent. // Sort the inputs by age*value so we get the highest "coindays" spent.
// TODO: Consider changing the wallets internal format to track just outputs and keep them ordered. // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
ArrayList<TransactionOutput> sortedOutputs = new ArrayList<TransactionOutput>(candidates); ArrayList<TransactionOutput> sortedOutputs = new ArrayList<TransactionOutput>(candidates);
// When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting // When calculating the wallet balance, we may be asked to select all possible coins, if so, avoid sorting
// them in order to improve performance. // them in order to improve performance.
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) { if (!target.equals(NetworkParameters.MAX_MONEY)) {
sortOutputs(sortedOutputs); sortOutputs(sortedOutputs);
} }
// Now iterate over the sorted outputs until we have got as close to the target as possible or a little // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
// bit over (excessive value will be change). // bit over (excessive value will be change).
long total = 0; long total = 0;
for (TransactionOutput output : sortedOutputs) { for (TransactionOutput output : sortedOutputs) {
if (total >= target) break; if (total >= target.value) break;
// Only pick chain-included transactions, or transactions that are ours and pending. // Only pick chain-included transactions, or transactions that are ours and pending.
if (!shouldSelect(output.getParentTransaction())) continue; if (!shouldSelect(output.getParentTransaction())) continue;
selected.add(output); selected.add(output);