Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optimized Wallet.getTransaction functions #268

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2420,9 +2420,9 @@ private void receive(Transaction tx, StoredBlock block, BlockChain.NewBlockType
int diff = valueDifference.signum();
// We pick one callback based on the value difference, though a tx can of course both send and receive
// coins from the wallet.
if (diff > 0) {
if (diff >= 0) {
queueOnCoinsReceived(tx, prevBalance, newBalance);
} else if (diff < 0) {
} else {
queueOnCoinsSent(tx, prevBalance, newBalance);
}
}
Expand Down 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());
}

}
Loading