diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 07069ba6a..ad59206b9 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -3275,6 +3275,42 @@ public Set getTransactions(boolean includeDead) { } } + public Set getTransactionsOpt(boolean includeDead) { + lock.lock(); + try { + int capacity = unspent.size() + spent.size() + pending.size() + (includeDead ? dead.size(): 0); + Set all = new HashSet<>(capacity); + all.addAll(unspent.values()); + all.addAll(spent.values()); + all.addAll(pending.values()); + if (includeDead) + all.addAll(dead.values()); + return all; + } finally { + lock.unlock(); + } + } + + /** + * Returns a list of all transactions in the wallet. + * @param includeDead If true, transactions that were overridden by a double spend are included. + */ + public Collection getTransactionList(boolean includeDead) { + lock.lock(); + try { + int capacity = unspent.size() + spent.size() + pending.size() + (includeDead ? dead.size(): 0); + ArrayList all = new ArrayList<>(capacity); + all.addAll(unspent.values()); + all.addAll(spent.values()); + all.addAll(pending.values()); + if (includeDead) + all.addAll(dead.values()); + return all; + } finally { + lock.unlock(); + } + } + /** * Returns a set of all WalletTransactions in the wallet. */ diff --git a/core/src/test/java/org/bitcoinj/wallet/WalletBenchmarkTest.java b/core/src/test/java/org/bitcoinj/wallet/WalletBenchmarkTest.java new file mode 100644 index 000000000..372f60aa9 --- /dev/null +++ b/core/src/test/java/org/bitcoinj/wallet/WalletBenchmarkTest.java @@ -0,0 +1,44 @@ +package org.bitcoinj.wallet; + +import com.google.common.base.Stopwatch; +import org.bitcoinj.core.Transaction; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + +public class WalletBenchmarkTest { + @Test + public void getTransactionsTest() throws UnreadableWalletException, IOException { + Set set = null; + Collection list = null; + int count = 1; + try (InputStream stream = getClass().getResourceAsStream("coinjoin-testnet-big.wallet")) { + WalletEx coinJoinWallet = (WalletEx) new WalletProtobufSerializer().readWallet(stream); + Stopwatch watch = Stopwatch.createStarted(); + for (int i = 0; i < count; ++i) + set = coinJoinWallet.getTransactions(true); + System.out.println("getTransactions = " + watch); + } + try (InputStream stream = getClass().getResourceAsStream("coinjoin-testnet-big.wallet")) { + WalletEx coinJoinWallet = (WalletEx) new WalletProtobufSerializer().readWallet(stream); + Stopwatch watch = Stopwatch.createStarted(); + for (int i = 0; i < count; ++i) + set = coinJoinWallet.getTransactionsOpt(true); + System.out.println("getTransactions(optimized) = " + watch); + } + try (InputStream stream = getClass().getResourceAsStream("coinjoin-testnet-big.wallet")) { + WalletEx coinJoinWallet = (WalletEx) new WalletProtobufSerializer().readWallet(stream); + Stopwatch watch = Stopwatch.createStarted(); + for (int i = 0; i < count; ++i) + list = coinJoinWallet.getTransactionList(true); + System.out.println("getTransactionList = " + watch); + } + assertEquals(set.size(), list.size()); + } + +}