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:
parent
f2eaf4a562
commit
e9bffdda18
@ -6,7 +6,7 @@ import org.bitcoinj.core.TransactionOutput;
|
||||
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
|
||||
* may return a {@link CoinSelection} that has a valueGathered lower than the requested target, if there's not
|
||||
* enough money in the wallet.
|
||||
|
@ -18,22 +18,21 @@ import java.util.*;
|
||||
*/
|
||||
public class DefaultCoinSelector implements CoinSelector {
|
||||
@Override
|
||||
public CoinSelection select(Coin biTarget, List<TransactionOutput> candidates) {
|
||||
long target = biTarget.value;
|
||||
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
|
||||
ArrayList<TransactionOutput> selected = new ArrayList<TransactionOutput>();
|
||||
// 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.
|
||||
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
|
||||
// them in order to improve performance.
|
||||
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) {
|
||||
if (!target.equals(NetworkParameters.MAX_MONEY)) {
|
||||
sortOutputs(sortedOutputs);
|
||||
}
|
||||
// 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).
|
||||
long total = 0;
|
||||
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.
|
||||
if (!shouldSelect(output.getParentTransaction())) continue;
|
||||
selected.add(output);
|
||||
|
Loading…
x
Reference in New Issue
Block a user