Refactor BsqBlockchainManager dependencies. Add BsqTxProvider interface.

This commit is contained in:
Manfred Karrer 2017-12-05 15:19:36 -05:00
parent 57a019708d
commit 6dc76205b0
No known key found for this signature in database
GPG key ID: 401250966A6B2C46
5 changed files with 62 additions and 23 deletions

View file

@ -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<Transaction> walletTransactions = FXCollections.observableArrayList();
private final CopyOnWriteArraySet<BsqBalanceListener> 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,9 +129,11 @@ public class BsqWalletService extends WalletService implements BsqChainStateList
@Override
public void onBsqChainStateChanged() {
if (isWalletReady()) {
updateBsqWalletTransactions();
updateBsqBalance();
}
}
///////////////////////////////////////////////////////////////////////////////////////////
@ -204,7 +204,7 @@ public class BsqWalletService extends WalletService implements BsqChainStateList
private Set<Transaction> getBsqWalletTransactions() {
return getTransactions(false).stream()
.filter(transaction -> transaction.getConfidence().getConfidenceType() == PENDING ||
bsqChainState.containsTx(transaction.getHashAsString()))
bsqTxProvider.containsTx(transaction.getHashAsString()))
.collect(Collectors.toSet());
}
@ -242,16 +242,13 @@ 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<Tx> 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 {
result = result.add(connectedOutput.getValue());
}
@ -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<Tx> txOptional = bsqTxProvider.findTx(txId);
if (txOptional.isPresent()) {
TxOutput txOutput = txOptional.get().getOutputs().get(i);
if (txOutput.isVerified()) {
result = result.add(output.getValue());
}

View file

@ -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);

View file

@ -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) {

View file

@ -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<Tx> 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.<Tx>empty();
}
public int getChainHeadHeight() {
return lock.read(() -> chainHeadHeight);
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<Tx> findTx(String txId);
}