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

Speed up deposit and transactions view loads #5120

Merged
73 changes: 36 additions & 37 deletions core/src/main/java/bisq/core/btc/wallet/BsqWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
@Slf4j
public class BsqWalletService extends WalletService implements DaoStateListener {


public interface WalletTransactionsChangeListener {

void onWalletTransactionsChange();
Expand Down Expand Up @@ -140,42 +139,7 @@ public BsqWalletService(WalletsSetup walletsSetup,
wallet = walletsSetup.getBsqWallet();
if (wallet != null) {
wallet.setCoinSelector(bsqCoinSelector);

wallet.addCoinsReceivedEventListener(walletEventListener);
wallet.addCoinsSentEventListener(walletEventListener);
wallet.addReorganizeEventListener(walletEventListener);
wallet.addTransactionConfidenceEventListener(walletEventListener);

wallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> {
updateBsqWalletTransactions();
});
wallet.addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) -> {
updateBsqWalletTransactions();
});
wallet.addReorganizeEventListener(wallet -> {
log.warn("onReorganize ");
updateBsqWalletTransactions();
unconfirmedBsqChangeOutputListService.onReorganize();
});
wallet.addTransactionConfidenceEventListener((wallet, tx) -> {
// We are only interested in updates from unconfirmed txs and confirmed txs at the
// time when it gets into a block. Otherwise we would get called
// updateBsqWalletTransactions for each tx as the block depth changes for all.
if (tx != null && tx.getConfidence() != null && tx.getConfidence().getDepthInBlocks() <= 1 &&
daoStateService.isParseBlockChainComplete()) {
updateBsqWalletTransactions();
}
unconfirmedBsqChangeOutputListService.onTransactionConfidenceChanged(tx);
});
wallet.addKeyChainEventListener(keys -> {
updateBsqWalletTransactions();
});
wallet.addScriptsChangeEventListener((wallet, scripts, isAddingScripts) -> {
updateBsqWalletTransactions();
});
wallet.addChangeEventListener(wallet -> {
updateBsqWalletTransactions();
});
addListenersToWallet();
}

BlockChain chain = walletsSetup.getChain();
Expand All @@ -188,6 +152,41 @@ public BsqWalletService(WalletsSetup walletsSetup,
daoStateService.addDaoStateListener(this);
}

@Override
protected void addListenersToWallet() {
super.addListenersToWallet();

wallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) ->
updateBsqWalletTransactions()
);
wallet.addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) ->
updateBsqWalletTransactions()
);
wallet.addReorganizeEventListener(wallet -> {
log.warn("onReorganize ");
updateBsqWalletTransactions();
unconfirmedBsqChangeOutputListService.onReorganize();
});
wallet.addTransactionConfidenceEventListener((wallet, tx) -> {
// We are only interested in updates from unconfirmed txs and confirmed txs at the
// time when it gets into a block. Otherwise we would get called
// updateBsqWalletTransactions for each tx as the block depth changes for all.
if (tx != null && tx.getConfidence() != null && tx.getConfidence().getDepthInBlocks() <= 1 &&
daoStateService.isParseBlockChainComplete()) {
updateBsqWalletTransactions();
}
unconfirmedBsqChangeOutputListService.onTransactionConfidenceChanged(tx);
});
wallet.addKeyChainEventListener(keys ->
updateBsqWalletTransactions()
);
wallet.addScriptsChangeEventListener((wallet, scripts, isAddingScripts) ->
updateBsqWalletTransactions()
);
wallet.addChangeEventListener(wallet ->
updateBsqWalletTransactions()
);
}

///////////////////////////////////////////////////////////////////////////////////////////
// DaoStateListener
Expand Down
8 changes: 2 additions & 6 deletions core/src/main/java/bisq/core/btc/wallet/BtcWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ public BtcWalletService(WalletsSetup walletsSetup,

walletsSetup.addSetupCompletedHandler(() -> {
wallet = walletsSetup.getBtcWallet();
wallet.addCoinsReceivedEventListener(walletEventListener);
wallet.addCoinsSentEventListener(walletEventListener);
wallet.addReorganizeEventListener(walletEventListener);
wallet.addTransactionConfidenceEventListener(walletEventListener);
addListenersToWallet();

walletsSetup.getChain().addNewBestBlockListener(block -> chainHeightProperty.set(block.getHeight()));
chainHeightProperty.set(walletsSetup.getChain().getBestChainHeight());
Expand Down Expand Up @@ -676,8 +673,7 @@ public AddressEntry getFreshAddressEntry(boolean segwit) {
.filter(e -> {
boolean isSegwitOutputScriptType = Script.ScriptType.P2WPKH.equals(e.getAddress().getOutputScriptType());
// We need to ensure that we take only addressEntries which matches our segWit flag
boolean isMatchingOutputScriptType = isSegwitOutputScriptType == segwit;
return isMatchingOutputScriptType;
return isSegwitOutputScriptType == segwit;
})
.findAny();
return getOrCreateAddressEntry(context, addressEntry, segwit);
Expand Down
106 changes: 69 additions & 37 deletions core/src/main/java/bisq/core/btc/wallet/WalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@

import javax.inject.Inject;

import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.SetMultimap;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -83,9 +87,13 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -106,10 +114,13 @@ public abstract class WalletService {
protected final Preferences preferences;
protected final FeeService feeService;
protected final NetworkParameters params;
protected final BisqWalletListener walletEventListener = new BisqWalletListener();
protected final CopyOnWriteArraySet<AddressConfidenceListener> addressConfidenceListeners = new CopyOnWriteArraySet<>();
protected final CopyOnWriteArraySet<TxConfidenceListener> txConfidenceListeners = new CopyOnWriteArraySet<>();
protected final CopyOnWriteArraySet<BalanceListener> balanceListeners = new CopyOnWriteArraySet<>();
private final BisqWalletListener walletEventListener = new BisqWalletListener();
private final CopyOnWriteArraySet<AddressConfidenceListener> addressConfidenceListeners = new CopyOnWriteArraySet<>();
private final CopyOnWriteArraySet<TxConfidenceListener> txConfidenceListeners = new CopyOnWriteArraySet<>();
private final CopyOnWriteArraySet<BalanceListener> balanceListeners = new CopyOnWriteArraySet<>();
private final WalletChangeEventListener cacheInvalidationListener;
private final AtomicReference<Multiset<Address>> txOutputAddressCache = new AtomicReference<>();
private final AtomicReference<SetMultimap<Address, Transaction>> addressToMatchingTxSetCache = new AtomicReference<>();
@Getter
protected Wallet wallet;
@Getter
Expand All @@ -131,20 +142,33 @@ public abstract class WalletService {
this.feeService = feeService;

params = walletsSetup.getParams();

cacheInvalidationListener = wallet -> {
txOutputAddressCache.set(null);
addressToMatchingTxSetCache.set(null);
};
}


///////////////////////////////////////////////////////////////////////////////////////////
// Lifecycle
///////////////////////////////////////////////////////////////////////////////////////////

protected void addListenersToWallet() {
wallet.addCoinsReceivedEventListener(walletEventListener);
wallet.addCoinsSentEventListener(walletEventListener);
wallet.addReorganizeEventListener(walletEventListener);
wallet.addTransactionConfidenceEventListener(walletEventListener);
wallet.addChangeEventListener(Threading.SAME_THREAD, cacheInvalidationListener);
}

public void shutDown() {
if (wallet != null) {
//noinspection deprecation
wallet.removeCoinsReceivedEventListener(walletEventListener);
wallet.removeCoinsSentEventListener(walletEventListener);
wallet.removeReorganizeEventListener(walletEventListener);
wallet.removeTransactionConfidenceEventListener(walletEventListener);
wallet.removeChangeEventListener(cacheInvalidationListener);
}
}

Expand Down Expand Up @@ -371,15 +395,28 @@ public void broadcastTx(Transaction tx, TxBroadcaster.Callback callback, int tim
public TransactionConfidence getConfidenceForAddress(Address address) {
List<TransactionConfidence> transactionConfidenceList = new ArrayList<>();
if (wallet != null) {
Set<Transaction> transactions = wallet.getTransactions(false);
if (transactions != null) {
transactionConfidenceList.addAll(transactions.stream().map(tx ->
getTransactionConfidence(tx, address)).collect(Collectors.toList()));
}
Set<Transaction> transactions = getAddressToMatchingTxSetMultiset().get(address);
transactionConfidenceList.addAll(transactions.stream().map(tx ->
getTransactionConfidence(tx, address)).collect(Collectors.toList()));
}
return getMostRecentConfidence(transactionConfidenceList);
}

private SetMultimap<Address, Transaction> getAddressToMatchingTxSetMultiset() {
return addressToMatchingTxSetCache.updateAndGet(set -> set != null ? set : computeAddressToMatchingTxSetMultimap());
}

private SetMultimap<Address, Transaction> computeAddressToMatchingTxSetMultimap() {
return wallet.getTransactions(false).stream()
.collect(ImmutableSetMultimap.flatteningToImmutableSetMultimap(
Function.identity(),
(Function<Transaction, Stream<Address>>) (
t -> getOutputsWithConnectedOutputs(t).stream()
.map(WalletService::getAddressFromOutput)
.filter(Objects::nonNull))))
.inverse();
}

@Nullable
public TransactionConfidence getConfidenceForTxId(String txId) {
if (wallet != null) {
Expand All @@ -392,18 +429,18 @@ public TransactionConfidence getConfidenceForTxId(String txId) {
return null;
}

protected TransactionConfidence getTransactionConfidence(Transaction tx, Address address) {
List<TransactionConfidence> transactionConfidenceList = getOutputsWithConnectedOutputs(tx)
.stream()
.filter(WalletService::isOutputScriptConvertibleToAddress)
@Nullable
private TransactionConfidence getTransactionConfidence(Transaction tx, Address address) {
stejbac marked this conversation as resolved.
Show resolved Hide resolved
List<TransactionConfidence> transactionConfidenceList = getOutputsWithConnectedOutputs(tx).stream()
.filter(output -> address != null && address.equals(getAddressFromOutput(output)))
.map(o -> tx.getConfidence())
.flatMap(o -> Stream.ofNullable(o.getParentTransaction()))
.map(Transaction::getConfidence)
.collect(Collectors.toList());
return getMostRecentConfidence(transactionConfidenceList);
}


protected List<TransactionOutput> getOutputsWithConnectedOutputs(Transaction tx) {
private List<TransactionOutput> getOutputsWithConnectedOutputs(Transaction tx) {
List<TransactionOutput> transactionOutputs = tx.getOutputs();
List<TransactionOutput> connectedOutputs = new ArrayList<>();

Expand All @@ -423,7 +460,7 @@ protected List<TransactionOutput> getOutputsWithConnectedOutputs(Transaction tx)
}

@Nullable
protected TransactionConfidence getMostRecentConfidence(List<TransactionConfidence> transactionConfidenceList) {
private TransactionConfidence getMostRecentConfidence(List<TransactionConfidence> transactionConfidenceList) {
TransactionConfidence transactionConfidence = null;
for (TransactionConfidence confidence : transactionConfidenceList) {
if (confidence != null) {
Expand Down Expand Up @@ -490,16 +527,19 @@ public Coin getTxFeeForWithdrawalPerVbyte() {
///////////////////////////////////////////////////////////////////////////////////////////

public int getNumTxOutputsForAddress(Address address) {
List<TransactionOutput> transactionOutputs = new ArrayList<>();
wallet.getTransactions(false).forEach(t -> transactionOutputs.addAll(t.getOutputs()));
int outputs = 0;
for (TransactionOutput output : transactionOutputs) {
if (isOutputScriptConvertibleToAddress(output) &&
address != null &&
address.equals(getAddressFromOutput(output)))
outputs++;
}
return outputs;
return getTxOutputAddressMultiset().count(address);
}

private Multiset<Address> getTxOutputAddressMultiset() {
return txOutputAddressCache.updateAndGet(set -> set != null ? set : computeTxOutputAddressMultiset());
}

private Multiset<Address> computeTxOutputAddressMultiset() {
return wallet.getTransactions(false).stream()
.flatMap(t -> t.getOutputs().stream())
.map(WalletService::getAddressFromOutput)
.filter(Objects::nonNull)
.collect(ImmutableMultiset.toImmutableMultiset());
}

public boolean isAddressUnused(Address address) {
Expand Down Expand Up @@ -595,17 +635,13 @@ public void removeChangeEventListener(WalletChangeEventListener listener) {
wallet.removeChangeEventListener(listener);
}

@SuppressWarnings("deprecation")
public void addNewBestBlockListener(NewBestBlockListener listener) {
//noinspection deprecation
final BlockChain chain = walletsSetup.getChain();
if (isWalletReady() && chain != null)
chain.addNewBestBlockListener(listener);
}

@SuppressWarnings("deprecation")
public void removeNewBestBlockListener(NewBestBlockListener listener) {
//noinspection deprecation
final BlockChain chain = walletsSetup.getChain();
if (isWalletReady() && chain != null)
chain.removeNewBestBlockListener(listener);
Expand Down Expand Up @@ -786,7 +822,6 @@ public static Transaction maybeAddTxToWallet(Transaction transaction,
// bisqWalletEventListener
///////////////////////////////////////////////////////////////////////////////////////////

@SuppressWarnings("deprecation")
public class BisqWalletListener implements WalletCoinsReceivedEventListener, WalletCoinsSentEventListener, WalletReorganizeEventListener, TransactionConfidenceEventListener {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
Expand All @@ -806,11 +841,8 @@ public void onReorganize(Wallet wallet) {
@Override
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
for (AddressConfidenceListener addressConfidenceListener : addressConfidenceListeners) {
List<TransactionConfidence> transactionConfidenceList = new ArrayList<>();
transactionConfidenceList.add(getTransactionConfidence(tx, addressConfidenceListener.getAddress()));

TransactionConfidence transactionConfidence = getMostRecentConfidence(transactionConfidenceList);
addressConfidenceListener.onTransactionConfidenceChanged(transactionConfidence);
TransactionConfidence confidence = getTransactionConfidence(tx, addressConfidenceListener.getAddress());
addressConfidenceListener.onTransactionConfidenceChanged(confidence);
}
txConfidenceListeners.stream()
.filter(txConfidenceListener -> tx != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import javafx.collections.ObservableList;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand All @@ -53,6 +55,8 @@ public abstract class DisputeListService<T extends DisputeList<Dispute>> impleme
private final Map<String, Subscription> disputeIsClosedSubscriptionsMap = new HashMap<>();
@Getter
private final IntegerProperty numOpenDisputes = new SimpleIntegerProperty();
@Getter
private final Set<String> disputedTradeIds = new HashSet<>();


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -154,6 +158,7 @@ private void onDisputesChangeListener(List<? extends Dispute> addedList,
disputeIsClosedSubscriptionsMap.get(id).unsubscribe();
disputeIsClosedSubscriptionsMap.remove(id);
}
disputedTradeIds.remove(dispute.getTradeId());
});
}
addedList.forEach(dispute -> {
Expand All @@ -168,6 +173,7 @@ private void onDisputesChangeListener(List<? extends Dispute> addedList,
});
});
disputeIsClosedSubscriptionsMap.put(id, disputeStateSubscription);
disputedTradeIds.add(dispute.getTradeId());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -231,6 +232,9 @@ protected T getDisputeList() {
return disputeListService.getDisputeList();
}

public Set<String> getDisputedTradeIds() {
return disputeListService.getDisputedTradeIds();
}

///////////////////////////////////////////////////////////////////////////////////////////
// API
Expand Down
Loading