From cc88c3a9d42c1afa5b342849d2cec22cf37256d3 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Wed, 9 Aug 2023 11:45:13 -0700 Subject: [PATCH] Wallet: use functional style in method `calcBloomOutPointsLocked()` --- .../main/java/org/bitcoinj/wallet/Wallet.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 5afeaf435..a95a219a2 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -139,6 +139,8 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.bitcoinj.base.internal.Preconditions.checkArgument; import static org.bitcoinj.base.internal.Preconditions.checkState; @@ -5205,21 +5207,15 @@ public class Wallet extends BaseTaggableObject private void calcBloomOutPointsLocked() { // TODO: This could be done once and then kept up to date. bloomOutPoints.clear(); - Set all = new HashSet<>(); - all.addAll(unspent.values()); - all.addAll(spent.values()); - all.addAll(pending.values()); - for (Transaction tx : all) { - for (TransactionOutput out : tx.getOutputs()) { - try { - if (isTxOutputBloomFilterable(out)) - bloomOutPoints.add(out.getOutPointFor()); - } catch (ScriptException e) { - // If it is ours, we parsed the script correctly, so this shouldn't happen. - throw new RuntimeException(e); - } - } - } + // Search unspent, spent, and pending for all TransactionOutputs that are bloom filterable and + // then collect a list of the corresponding TransactionOutPoints. + List outPoints = Stream.of(unspent.values(), spent.values(), pending.values()) + .flatMap(Collection::stream) + .flatMap(tx -> tx.getOutputs().stream()) + .filter(this::isTxOutputBloomFilterable) + .map(TransactionOutput::getOutPointFor) + .collect(Collectors.toList()); + bloomOutPoints.addAll(outPoints); } @Override @GuardedBy("keyChainGroupLock")