3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-11 17:55:53 +00:00

Use ArrayList instead of HashSet in DefaultCoinSelector#select. Test

This commit is contained in:
Bennett Hoffman 2014-11-29 00:05:15 -08:00 committed by Mike Hearn
parent df00b1e27e
commit 552bf3fa4d
2 changed files with 20 additions and 1 deletions

View File

@ -20,7 +20,7 @@ public class DefaultCoinSelector implements CoinSelector {
@Override
public CoinSelection select(Coin biTarget, List<TransactionOutput> candidates) {
long target = biTarget.value;
HashSet<TransactionOutput> selected = new HashSet<TransactionOutput>();
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);

View File

@ -29,6 +29,7 @@ import org.junit.Test;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import static org.bitcoinj.core.Coin.*;
import static com.google.common.base.Preconditions.checkNotNull;
@ -113,4 +114,22 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
assertEquals(t1.getOutput(0), candidates.get(1));
assertEquals(t3.getOutput(0), candidates.get(2));
}
@Test
public void identicalInputs() throws Exception {
// Add four outputs to a transaction with same value and destination. Select them all.
Transaction t = new Transaction(params);
java.util.List<TransactionOutput> outputs = Arrays.asList(
new TransactionOutput(params, t, Coin.valueOf(30302787), myAddress),
new TransactionOutput(params, t, Coin.valueOf(30302787), myAddress),
new TransactionOutput(params, t, Coin.valueOf(30302787), myAddress),
new TransactionOutput(params, t, Coin.valueOf(30302787), myAddress)
);
t.getConfidence().setConfidenceType(TransactionConfidence.ConfidenceType.BUILDING);
DefaultCoinSelector selector = new DefaultCoinSelector();
CoinSelection selection = selector.select(COIN.multiply(2), outputs);
assertTrue(selection.gathered.size() == 4);
}
}