mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 10:15:52 +00:00
Make getBalance() significantly faster.
This commit is contained in:
parent
72f346c10f
commit
c9c107afaf
@ -143,8 +143,8 @@ public class Wallet implements Serializable, BlockChainListener {
|
|||||||
/** Represents the results of a {@link CoinSelector#select(java.math.BigInteger, java.util.LinkedList)} operation */
|
/** Represents the results of a {@link CoinSelector#select(java.math.BigInteger, java.util.LinkedList)} operation */
|
||||||
public static class CoinSelection {
|
public static class CoinSelection {
|
||||||
public BigInteger valueGathered;
|
public BigInteger valueGathered;
|
||||||
public List<TransactionOutput> gathered;
|
public Set<TransactionOutput> gathered;
|
||||||
public CoinSelection(BigInteger valueGathered, List<TransactionOutput> gathered) {
|
public CoinSelection(BigInteger valueGathered, Set<TransactionOutput> gathered) {
|
||||||
this.valueGathered = valueGathered;
|
this.valueGathered = valueGathered;
|
||||||
this.gathered = gathered;
|
this.gathered = gathered;
|
||||||
}
|
}
|
||||||
@ -170,36 +170,38 @@ public class Wallet implements Serializable, BlockChainListener {
|
|||||||
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;
|
long total = 0;
|
||||||
LinkedList<TransactionOutput> selected = Lists.newLinkedList();
|
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);
|
||||||
Collections.sort(sortedOutputs, new Comparator<TransactionOutput>() {
|
if (!biTarget.equals(NetworkParameters.MAX_MONEY)) {
|
||||||
public int compare(TransactionOutput a, TransactionOutput b) {
|
Collections.sort(sortedOutputs, new Comparator<TransactionOutput>() {
|
||||||
int depth1 = 0;
|
public int compare(TransactionOutput a, TransactionOutput b) {
|
||||||
int depth2 = 0;
|
int depth1 = 0;
|
||||||
TransactionConfidence conf1 = a.parentTransaction.getConfidence();
|
int depth2 = 0;
|
||||||
TransactionConfidence conf2 = b.parentTransaction.getConfidence();
|
TransactionConfidence conf1 = a.parentTransaction.getConfidence();
|
||||||
if (conf1.getConfidenceType() == ConfidenceType.BUILDING) depth1 = conf1.getDepthInBlocks();
|
TransactionConfidence conf2 = b.parentTransaction.getConfidence();
|
||||||
if (conf2.getConfidenceType() == ConfidenceType.BUILDING) depth2 = conf2.getDepthInBlocks();
|
if (conf1.getConfidenceType() == ConfidenceType.BUILDING) depth1 = conf1.getDepthInBlocks();
|
||||||
BigInteger aCoinDepth = a.getValue().multiply(BigInteger.valueOf(depth1));
|
if (conf2.getConfidenceType() == ConfidenceType.BUILDING) depth2 = conf2.getDepthInBlocks();
|
||||||
BigInteger bCoinDepth = b.getValue().multiply(BigInteger.valueOf(depth2));
|
BigInteger aCoinDepth = a.getValue().multiply(BigInteger.valueOf(depth1));
|
||||||
if (aCoinDepth.compareTo(bCoinDepth) < 0)
|
BigInteger bCoinDepth = b.getValue().multiply(BigInteger.valueOf(depth2));
|
||||||
return 1;
|
if (aCoinDepth.compareTo(bCoinDepth) < 0)
|
||||||
else if (bCoinDepth.compareTo(aCoinDepth) < 0)
|
return 1;
|
||||||
return -1;
|
else if (bCoinDepth.compareTo(aCoinDepth) < 0)
|
||||||
// The "coin*days" destroyed are equal, sort by value alone to get the lowest transaction size
|
return -1;
|
||||||
// (ie sort by reverse depth)
|
// The "coin*days" destroyed are equal, sort by value alone to get the lowest transaction size
|
||||||
if (depth1 < depth2)
|
// (ie sort by reverse depth)
|
||||||
return -1;
|
if (depth1 < depth2)
|
||||||
else if (depth2 < depth1)
|
return -1;
|
||||||
return 1;
|
else if (depth2 < depth1)
|
||||||
// They are entirely equivalent (possibly pending) so sort by hash to ensure a total ordering.
|
return 1;
|
||||||
BigInteger aHash = a.parentTransaction.getHash().toBigInteger();
|
// They are entirely equivalent (possibly pending) so sort by hash to ensure a total ordering.
|
||||||
BigInteger bHash = b.parentTransaction.getHash().toBigInteger();
|
BigInteger aHash = a.parentTransaction.getHash().toBigInteger();
|
||||||
return aHash.compareTo(bHash);
|
BigInteger bHash = b.parentTransaction.getHash().toBigInteger();
|
||||||
}
|
return aHash.compareTo(bHash);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
// 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).
|
||||||
for (TransactionOutput output : sortedOutputs) {
|
for (TransactionOutput output : sortedOutputs) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user