3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 19:25:51 +00:00

Make getBalance() significantly faster.

This commit is contained in:
Matt Corallo 2013-05-24 11:09:35 +02:00 committed by Mike Hearn
parent 72f346c10f
commit c9c107afaf

View File

@ -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,10 +170,11 @@ 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);
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) {
int depth1 = 0; int depth1 = 0;
@ -200,6 +201,7 @@ public class Wallet implements Serializable, BlockChainListener {
return aHash.compareTo(bHash); 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) {