From e8491bbd8842d7e1759fdfec51d04861153ab5d0 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Tue, 5 Sep 2023 19:20:08 -0700 Subject: [PATCH] KeyTimeCoinSelector: use a stream in `select()` Note that this removes the warning about exceeding the limit. --- .../bitcoinj/wallet/KeyTimeCoinSelector.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/KeyTimeCoinSelector.java b/core/src/main/java/org/bitcoinj/wallet/KeyTimeCoinSelector.java index 05cda68b9..5e84e40b5 100644 --- a/core/src/main/java/org/bitcoinj/wallet/KeyTimeCoinSelector.java +++ b/core/src/main/java/org/bitcoinj/wallet/KeyTimeCoinSelector.java @@ -19,6 +19,7 @@ package org.bitcoinj.wallet; import org.bitcoinj.base.Coin; import org.bitcoinj.base.ScriptType; +import org.bitcoinj.base.internal.StreamUtils; import org.bitcoinj.crypto.ECKey; import org.bitcoinj.core.Transaction; import org.bitcoinj.core.TransactionConfidence; @@ -34,6 +35,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; /** * 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 public CoinSelection select(Coin target, List candidates) { try { - LinkedList gathered = new LinkedList<>(); - for (TransactionOutput output : candidates) { - if (ignorePending && !isConfirmed(output)) - continue; - if (!isKeyBeforeCutoff(output)) - 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); + return candidates.stream() + .filter(output -> !ignorePending || isConfirmed(output)) + .filter(this::isKeyBeforeCutoff) + .limit(MAX_SIMULTANEOUS_INPUTS) // TODO: log a warning if limit exceeded? + .collect(Collectors.collectingAndThen(StreamUtils.toUnmodifiableList(), CoinSelection::new)); } catch (ScriptException e) { throw new RuntimeException(e); // We should never have problems understanding scripts in our wallet. }