Improve handling of p2pNetworkAndWalletReady

The p2pNetworkAndWalletReady MonadicBinding might be removed from GC
if its a local variable. I observed that in BisqSetup with a similar
setup. It might be an implementation weakness in MonadicBinding
(usage of weak references?). A tester reported that he does not see any
result, which might be cause that the service never gets the
onP2pNetworkAndWalletReady triggered if the MonadicBinding is not there
anymore.
By removing the listener we need at shutdown we need it anyway as class
field (so codacy does not complain anymore). As well added a check if
all is already complete to skip the MonadicBinding at all
(not expected case in onAllServicesInitialized).
This commit is contained in:
chimp1984 2020-09-04 13:46:15 -05:00 committed by Christoph Atteneder
parent 8949eaa7e2
commit dbefddf940
No known key found for this signature in database
GPG Key ID: CD5DC1C529CDFD3B

View File

@ -82,6 +82,8 @@ public class XmrTxProofService implements AssetTxProofService {
private Map<String, ChangeListener<Trade.State>> tradeStateListenerMap = new HashMap<>();
private ChangeListener<Number> btcPeersListener, btcBlockListener;
private BootstrapListener bootstrapListener;
private MonadicBinding<Boolean> p2pNetworkAndWalletReady;
private ChangeListener<Boolean> p2pNetworkAndWalletReadyListener;
///////////////////////////////////////////////////////////////////////////////////////////
@ -122,22 +124,34 @@ public class XmrTxProofService implements AssetTxProofService {
// As we might trigger the payout tx we want to be sure that we are well connected to the Bitcoin network.
// onAllServicesInitialized is called once we have received the initial data but we want to have our
// hidden service published and upDatedDataResponse received before we start.
MonadicBinding<Boolean> p2pNetworkAndWalletReady = EasyBind.combine(isP2pBootstrapped(), hasSufficientBtcPeers(), isBtcBlockDownloadComplete(),
(isP2pBootstrapped, hasSufficientBtcPeers, isBtcBlockDownloadComplete) -> {
log.info("isP2pBootstrapped={}, hasSufficientBtcPeers={} isBtcBlockDownloadComplete={}",
isP2pBootstrapped, hasSufficientBtcPeers, isBtcBlockDownloadComplete);
return isP2pBootstrapped && hasSufficientBtcPeers && isBtcBlockDownloadComplete;
});
BooleanProperty isP2pBootstrapped = isP2pBootstrapped();
BooleanProperty hasSufficientBtcPeers = hasSufficientBtcPeers();
BooleanProperty isBtcBlockDownloadComplete = isBtcBlockDownloadComplete();
if (isP2pBootstrapped.get() && hasSufficientBtcPeers.get() && isBtcBlockDownloadComplete.get()) {
onP2pNetworkAndWalletReady();
} else {
p2pNetworkAndWalletReady = EasyBind.combine(isP2pBootstrapped, hasSufficientBtcPeers, isBtcBlockDownloadComplete,
(bootstrapped, sufficientPeers, downloadComplete) -> {
log.info("isP2pBootstrapped={}, hasSufficientBtcPeers={} isBtcBlockDownloadComplete={}",
bootstrapped, sufficientPeers, downloadComplete);
return bootstrapped && sufficientPeers && downloadComplete;
});
p2pNetworkAndWalletReady.subscribe((observable, oldValue, newValue) -> {
if (newValue) {
onP2pNetworkAndWalletReady();
}
});
p2pNetworkAndWalletReadyListener = (observable, oldValue, newValue) -> {
if (newValue) {
onP2pNetworkAndWalletReady();
}
};
p2pNetworkAndWalletReady.subscribe(p2pNetworkAndWalletReadyListener);
}
}
@Override
public void shutDown() {
if (p2pNetworkAndWalletReady != null) {
p2pNetworkAndWalletReady.removeListener(p2pNetworkAndWalletReadyListener);
}
servicesByTradeId.values().forEach(XmrTxProofRequestsPerTrade::terminate);
servicesByTradeId.clear();
}