From 6dc76205b022579605163cf46ce8fa1254e21aa4 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 5 Dec 2017 15:19:36 -0500 Subject: [PATCH] Refactor BsqBlockchainManager dependencies. Add BsqTxProvider interface. --- .../core/btc/wallet/BsqWalletService.java | 39 ++++++++----------- .../main/java/io/bisq/core/dao/DaoModule.java | 1 + .../dao/blockchain/BsqBlockchainManager.java | 3 ++ .../dao/blockchain/parse/BsqChainState.java | 14 ++++++- .../dao/blockchain/parse/BsqTxProvider.java | 28 +++++++++++++ 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqTxProvider.java diff --git a/core/src/main/java/io/bisq/core/btc/wallet/BsqWalletService.java b/core/src/main/java/io/bisq/core/btc/wallet/BsqWalletService.java index dee2f80e19..0bb5533710 100644 --- a/core/src/main/java/io/bisq/core/btc/wallet/BsqWalletService.java +++ b/core/src/main/java/io/bisq/core/btc/wallet/BsqWalletService.java @@ -25,6 +25,7 @@ import io.bisq.core.btc.exceptions.TransactionVerificationException; import io.bisq.core.btc.exceptions.WalletException; import io.bisq.core.dao.blockchain.BsqChainStateListener; import io.bisq.core.dao.blockchain.parse.BsqChainState; +import io.bisq.core.dao.blockchain.parse.BsqTxProvider; import io.bisq.core.dao.blockchain.vo.Tx; import io.bisq.core.dao.blockchain.vo.TxOutput; import io.bisq.core.provider.fee.FeeService; @@ -40,10 +41,7 @@ import org.bitcoinj.wallet.Wallet; import org.bitcoinj.wallet.listeners.AbstractWalletEventListener; import javax.inject.Inject; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.Function; import java.util.stream.Collectors; @@ -54,7 +52,7 @@ import static org.bitcoinj.core.TransactionConfidence.ConfidenceType.PENDING; @Slf4j public class BsqWalletService extends WalletService implements BsqChainStateListener { private final BsqCoinSelector bsqCoinSelector; - private final BsqChainState bsqChainState; + private final BsqTxProvider bsqTxProvider; private final ObservableList walletTransactions = FXCollections.observableArrayList(); private final CopyOnWriteArraySet bsqBalanceListeners = new CopyOnWriteArraySet<>(); private Coin availableBsqBalance = Coin.ZERO; @@ -68,7 +66,7 @@ public class BsqWalletService extends WalletService implements BsqChainStateList @Inject public BsqWalletService(WalletsSetup walletsSetup, BsqCoinSelector bsqCoinSelector, - BsqChainState bsqChainState, + BsqTxProvider bsqTxProvider, Preferences preferences, FeeService feeService) { super(walletsSetup, @@ -76,7 +74,7 @@ public class BsqWalletService extends WalletService implements BsqChainStateList feeService); this.bsqCoinSelector = bsqCoinSelector; - this.bsqChainState = bsqChainState; + this.bsqTxProvider = bsqTxProvider; if (BisqEnvironment.isBaseCurrencySupportingBsq()) { walletsSetup.addSetupCompletedHandler(() -> { @@ -131,8 +129,10 @@ public class BsqWalletService extends WalletService implements BsqChainStateList @Override public void onBsqChainStateChanged() { - updateBsqWalletTransactions(); - updateBsqBalance(); + if (isWalletReady()) { + updateBsqWalletTransactions(); + updateBsqBalance(); + } } @@ -204,7 +204,7 @@ public class BsqWalletService extends WalletService implements BsqChainStateList private Set getBsqWalletTransactions() { return getTransactions(false).stream() .filter(transaction -> transaction.getConfidence().getConfidenceType() == PENDING || - bsqChainState.containsTx(transaction.getHashAsString())) + bsqTxProvider.containsTx(transaction.getHashAsString())) .collect(Collectors.toSet()); } @@ -242,14 +242,11 @@ public class BsqWalletService extends WalletService implements BsqChainStateList if (isConfirmed) { final Transaction parentTransaction = connectedOutput.getParentTransaction(); if (parentTransaction != null) { - Tx tx = bsqChainState.getTxMap().get(parentTransaction.getHash().toString()); - if (tx == null) - tx = bsqChainState.getGenesisTx(); //todo put gen in txmap - if (tx != null) { - TxOutput txOutput = tx.getOutputs().get(connectedOutput.getIndex()); - if (txOutput.isVerified()) { + Optional txOptional = bsqTxProvider.findTx(parentTransaction.getHash().toString()); + if (txOptional.isPresent()) { + TxOutput txOutput = txOptional.get().getOutputs().get(connectedOutput.getIndex()); + if (txOutput.isVerified()) result = result.add(connectedOutput.getValue()); - } } } } else { @@ -268,11 +265,9 @@ public class BsqWalletService extends WalletService implements BsqChainStateList final boolean isConfirmed = output.getParentTransaction() != null && output.getParentTransaction().getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING; if (output.isMineOrWatched(wallet)) { if (isConfirmed) { - Tx tx = bsqChainState.getTxMap().get(txId); - if (tx == null) - tx = bsqChainState.getGenesisTx(); //todo put gen in txmap - if (tx != null) { - TxOutput txOutput = tx.getOutputs().get(i); + Optional txOptional = bsqTxProvider.findTx(txId); + if (txOptional.isPresent()) { + TxOutput txOutput = txOptional.get().getOutputs().get(i); if (txOutput.isVerified()) { result = result.add(output.getValue()); } diff --git a/core/src/main/java/io/bisq/core/dao/DaoModule.java b/core/src/main/java/io/bisq/core/dao/DaoModule.java index 33fcd67b93..c6594f72bc 100644 --- a/core/src/main/java/io/bisq/core/dao/DaoModule.java +++ b/core/src/main/java/io/bisq/core/dao/DaoModule.java @@ -47,6 +47,7 @@ public class DaoModule extends AppModule { bind(BsqLiteNode.class).in(Singleton.class); bind(BsqFullNode.class).in(Singleton.class); bind(BsqChainState.class).in(Singleton.class); + bind(BsqTxProvider.class).to(BsqChainState.class).in(Singleton.class); bind(BsqFullNodeExecutor.class).in(Singleton.class); bind(BsqLiteNodeExecutor.class).in(Singleton.class); bind(BsqParser.class).in(Singleton.class); diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java index e2ae5542cb..fd360f4603 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java @@ -22,6 +22,7 @@ import io.bisq.common.handlers.ErrorMessageHandler; import io.bisq.core.app.BisqEnvironment; import io.bisq.core.btc.wallet.BsqWalletService; import io.bisq.core.dao.DaoOptionKeys; +import io.bisq.core.dao.compensation.CompensationRequestManager; import lombok.extern.slf4j.Slf4j; import javax.inject.Named; @@ -38,10 +39,12 @@ public class BsqBlockchainManager { public BsqBlockchainManager(BsqLiteNode bsqLiteNode, BsqFullNode bsqFullNode, BsqWalletService bsqWalletService, + CompensationRequestManager compensationRequestManager, @Named(DaoOptionKeys.RPC_USER) String rpcUser) { bsqNode = rpcUser != null && !rpcUser.isEmpty() ? bsqFullNode : bsqLiteNode; bsqNode.addBsqChainStateListener(bsqWalletService); + bsqNode.addBsqChainStateListener(compensationRequestManager); } public void onAllServicesInitialized(ErrorMessageHandler errorMessageHandler) { diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqChainState.java b/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqChainState.java index 2f70861d03..9f06cc4f83 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqChainState.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqChainState.java @@ -45,7 +45,7 @@ import static com.google.common.base.Preconditions.checkNotNull; // Represents mutable state of BSQ chain data // We get accessed the data from different threads so we need to make sure it is thread safe. @Slf4j -public class BsqChainState implements PersistableEnvelope { +public class BsqChainState implements PersistableEnvelope, BsqTxProvider { /////////////////////////////////////////////////////////////////////////////////////////// // Static @@ -366,10 +366,22 @@ public class BsqChainState implements PersistableEnvelope { return lock.read(() -> getTx(txId).map(Tx::getTxType)); } + @Override public boolean containsTx(String txId) { return lock.read(() -> getTx(txId).isPresent()); } + @Override + public Optional findTx(String txId) { + Tx tx = getTxMap().get(txId); + if (tx == null) + tx = getGenesisTx(); //todo put gen in txmap + if (tx != null) + return Optional.of(tx); + else + return Optional.empty(); + } + public int getChainHeadHeight() { return lock.read(() -> chainHeadHeight); } diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqTxProvider.java b/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqTxProvider.java new file mode 100644 index 0000000000..d3588fe5a9 --- /dev/null +++ b/core/src/main/java/io/bisq/core/dao/blockchain/parse/BsqTxProvider.java @@ -0,0 +1,28 @@ +/* + * This file is part of bisq. + * + * bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with bisq. If not, see . + */ + +package io.bisq.core.dao.blockchain.parse; + +import io.bisq.core.dao.blockchain.vo.Tx; + +import java.util.Optional; + +public interface BsqTxProvider { + boolean containsTx(String txId); + + Optional findTx(String txId); +}