KeyTimeCoinSelector: use a stream in select()

Note that this removes the warning about exceeding the limit.
This commit is contained in:
Sean Gilligan 2023-09-05 19:20:08 -07:00 committed by Andreas Schildbach
parent 339e0d0450
commit e8491bbd88

View file

@ -19,6 +19,7 @@ package org.bitcoinj.wallet;
import org.bitcoinj.base.Coin; import org.bitcoinj.base.Coin;
import org.bitcoinj.base.ScriptType; import org.bitcoinj.base.ScriptType;
import org.bitcoinj.base.internal.StreamUtils;
import org.bitcoinj.crypto.ECKey; import org.bitcoinj.crypto.ECKey;
import org.bitcoinj.core.Transaction; import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionConfidence; import org.bitcoinj.core.TransactionConfidence;
@ -34,6 +35,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* A coin selector that takes all coins assigned to keys created before the given timestamp. * A coin selector that takes all coins assigned to keys created before the given timestamp.
@ -64,19 +66,11 @@ public class KeyTimeCoinSelector implements CoinSelector {
@Override @Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) { public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
try { try {
LinkedList<TransactionOutput> gathered = new LinkedList<>(); return candidates.stream()
for (TransactionOutput output : candidates) { .filter(output -> !ignorePending || isConfirmed(output))
if (ignorePending && !isConfirmed(output)) .filter(this::isKeyBeforeCutoff)
continue; .limit(MAX_SIMULTANEOUS_INPUTS) // TODO: log a warning if limit exceeded?
if (!isKeyBeforeCutoff(output)) .collect(Collectors.collectingAndThen(StreamUtils.toUnmodifiableList(), CoinSelection::new));
continue;
gathered.push(output);
if (gathered.size() >= MAX_SIMULTANEOUS_INPUTS) {
log.warn("Reached {} inputs, going further would yield a tx that is too large, stopping here.", gathered.size());
break;
}
}
return new CoinSelection(gathered);
} catch (ScriptException e) { } catch (ScriptException e) {
throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet. throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet.
} }