Skip to content

Commit

Permalink
feat: add optimized Wallet.getTransaction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Dec 27, 2024
1 parent 6a8ffcf commit 112e168
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
36 changes: 36 additions & 0 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3275,6 +3275,42 @@ public Set<Transaction> getTransactions(boolean includeDead) {
}
}

public Set<Transaction> getTransactionsOpt(boolean includeDead) {
lock.lock();
try {
int capacity = unspent.size() + spent.size() + pending.size() + (includeDead ? dead.size(): 0);
Set<Transaction> 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<Transaction> getTransactionList(boolean includeDead) {
lock.lock();
try {
int capacity = unspent.size() + spent.size() + pending.size() + (includeDead ? dead.size(): 0);
ArrayList<Transaction> 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.
*/
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/java/org/bitcoinj/wallet/WalletBenchmarkTest.java
Original file line number Diff line number Diff line change
@@ -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<Transaction> set = null;
Collection<Transaction> 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());
}

}

0 comments on commit 112e168

Please sign in to comment.