mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-07 23:03:04 +00:00
Some minor clarity improvements to DefaultCoinSelector.
This commit is contained in:
parent
0bdba0318a
commit
033e7e7aab
@ -169,11 +169,12 @@ public class Wallet implements Serializable, BlockChainListener {
|
|||||||
public static class DefaultCoinSelector implements CoinSelector {
|
public static class DefaultCoinSelector implements CoinSelector {
|
||||||
public CoinSelection select(BigInteger biTarget, LinkedList<TransactionOutput> candidates) {
|
public CoinSelection select(BigInteger biTarget, LinkedList<TransactionOutput> candidates) {
|
||||||
long target = biTarget.longValue();
|
long target = biTarget.longValue();
|
||||||
long total = 0;
|
|
||||||
HashSet<TransactionOutput> selected = new HashSet<TransactionOutput>();
|
HashSet<TransactionOutput> selected = new HashSet<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
|
||||||
|
// them in order to improve performance.
|
||||||
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) {
|
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) {
|
||||||
Collections.sort(sortedOutputs, new Comparator<TransactionOutput>() {
|
Collections.sort(sortedOutputs, new Comparator<TransactionOutput>() {
|
||||||
public int compare(TransactionOutput a, TransactionOutput b) {
|
public int compare(TransactionOutput a, TransactionOutput b) {
|
||||||
@ -183,18 +184,15 @@ public class Wallet implements Serializable, BlockChainListener {
|
|||||||
TransactionConfidence conf2 = b.parentTransaction.getConfidence();
|
TransactionConfidence conf2 = b.parentTransaction.getConfidence();
|
||||||
if (conf1.getConfidenceType() == ConfidenceType.BUILDING) depth1 = conf1.getDepthInBlocks();
|
if (conf1.getConfidenceType() == ConfidenceType.BUILDING) depth1 = conf1.getDepthInBlocks();
|
||||||
if (conf2.getConfidenceType() == ConfidenceType.BUILDING) depth2 = conf2.getDepthInBlocks();
|
if (conf2.getConfidenceType() == ConfidenceType.BUILDING) depth2 = conf2.getDepthInBlocks();
|
||||||
BigInteger aCoinDepth = a.getValue().multiply(BigInteger.valueOf(depth1));
|
BigInteger aValue = a.getValue();
|
||||||
BigInteger bCoinDepth = b.getValue().multiply(BigInteger.valueOf(depth2));
|
BigInteger bValue = b.getValue();
|
||||||
if (aCoinDepth.compareTo(bCoinDepth) < 0)
|
BigInteger aCoinDepth = aValue.multiply(BigInteger.valueOf(depth1));
|
||||||
return 1;
|
BigInteger bCoinDepth = bValue.multiply(BigInteger.valueOf(depth2));
|
||||||
else if (bCoinDepth.compareTo(aCoinDepth) < 0)
|
int c1 = bCoinDepth.compareTo(aCoinDepth);
|
||||||
return -1;
|
if (c1 != 0) return c1;
|
||||||
// The "coin*days" destroyed are equal, sort by value alone to get the lowest transaction size
|
// The "coin*days" destroyed are equal, sort by value alone to get the lowest transaction size.
|
||||||
// (ie sort by reverse depth)
|
int c2 = bValue.compareTo(aValue);
|
||||||
if (depth1 < depth2)
|
if (c2 != 0) return c2;
|
||||||
return -1;
|
|
||||||
else if (depth2 < depth1)
|
|
||||||
return 1;
|
|
||||||
// They are entirely equivalent (possibly pending) so sort by hash to ensure a total ordering.
|
// They are entirely equivalent (possibly pending) so sort by hash to ensure a total ordering.
|
||||||
BigInteger aHash = a.parentTransaction.getHash().toBigInteger();
|
BigInteger aHash = a.parentTransaction.getHash().toBigInteger();
|
||||||
BigInteger bHash = b.parentTransaction.getHash().toBigInteger();
|
BigInteger bHash = b.parentTransaction.getHash().toBigInteger();
|
||||||
@ -204,6 +202,7 @@ public class Wallet implements Serializable, BlockChainListener {
|
|||||||
}
|
}
|
||||||
// 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;
|
||||||
for (TransactionOutput output : sortedOutputs) {
|
for (TransactionOutput output : sortedOutputs) {
|
||||||
if (total >= target) break;
|
if (total >= target) 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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user