From 9ad77e89000bc616d0b1472815e1d369153bafaf Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:12:03 -0500 Subject: [PATCH 1/9] Fixes https://github.com/bisq-network/bisq/issues/5204 Remove SetupListener implementation from MailboxMessageService. Remove requestDataManager Add onNoSeedNodeAvailable method Call onNoSeedNodeAvailable from P2PService --- .../java/bisq/network/p2p/P2PService.java | 1 + .../p2p/mailbox/MailboxMessageService.java | 39 ++++++------------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index bd50e248552..b3e259022ad 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -258,6 +258,7 @@ public void onTorNodeReady() { if (!seedNodesAvailable) { isBootstrapped = true; + mailboxMessageService.onNoSeedNodeAvailable(); p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); } } diff --git a/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java b/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java index f1855ca0ed5..1b79ea29c1b 100644 --- a/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java +++ b/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java @@ -26,11 +26,9 @@ import bisq.network.p2p.messaging.DecryptedMailboxListener; import bisq.network.p2p.network.Connection; import bisq.network.p2p.network.NetworkNode; -import bisq.network.p2p.network.SetupListener; import bisq.network.p2p.peers.BroadcastHandler; import bisq.network.p2p.peers.Broadcaster; import bisq.network.p2p.peers.PeerManager; -import bisq.network.p2p.peers.getdata.RequestDataManager; import bisq.network.p2p.storage.HashMapChangedListener; import bisq.network.p2p.storage.P2PDataStorage; import bisq.network.p2p.storage.messages.AddDataMessage; @@ -112,14 +110,12 @@ */ @Singleton @Slf4j -public class MailboxMessageService implements SetupListener, HashMapChangedListener, - PersistedDataHost { +public class MailboxMessageService implements HashMapChangedListener, PersistedDataHost { private static final long REPUBLISH_DELAY_SEC = TimeUnit.MINUTES.toSeconds(2); private final NetworkNode networkNode; private final PeerManager peerManager; private final P2PDataStorage p2PDataStorage; - private final RequestDataManager requestDataManager; private final EncryptionService encryptionService; private final IgnoredMailboxService ignoredMailboxService; private final PersistenceManager persistenceManager; @@ -137,7 +133,6 @@ public class MailboxMessageService implements SetupListener, HashMapChangedListe public MailboxMessageService(NetworkNode networkNode, PeerManager peerManager, P2PDataStorage p2PDataStorage, - RequestDataManager requestDataManager, EncryptionService encryptionService, IgnoredMailboxService ignoredMailboxService, PersistenceManager persistenceManager, @@ -147,7 +142,6 @@ public MailboxMessageService(NetworkNode networkNode, this.networkNode = networkNode; this.peerManager = peerManager; this.p2PDataStorage = p2PDataStorage; - this.requestDataManager = requestDataManager; this.encryptionService = encryptionService; this.ignoredMailboxService = ignoredMailboxService; this.persistenceManager = persistenceManager; @@ -155,8 +149,6 @@ public MailboxMessageService(NetworkNode networkNode, this.clock = clock; this.republishMailboxEntries = republishMailboxEntries; - this.networkNode.addSetupListener(this); - this.persistenceManager.initialize(mailboxMessageList, PersistenceManager.Source.PRIVATE_LOW_PRIO); } @@ -236,6 +228,16 @@ public void onUpdatedDataReceived() { } } + public void onNoSeedNodeAvailable() { + if (!isBootstrapped) { + isBootstrapped = true; + // As we do not expect a updated data request response we start here with addHashMapChangedListenerAndApply + addHashMapChangedListenerAndApply(); + maybeRepublishMailBoxMessages(); + } + } + + public void sendEncryptedMailboxMessage(NodeAddress peer, PubKeyRing peersPubKeyRing, MailboxMessage mailboxMessage, @@ -343,25 +345,6 @@ public void addDecryptedMailboxListener(DecryptedMailboxListener listener) { } - /////////////////////////////////////////////////////////////////////////////////////////// - // SetupListener implementation - /////////////////////////////////////////////////////////////////////////////////////////// - @Override - public void onTorNodeReady() { - boolean seedNodesAvailable = requestDataManager.requestPreliminaryData(); - if (!seedNodesAvailable) { - isBootstrapped = true; - // As we do not expect a updated data request response we start here with addHashMapChangedListenerAndApply - addHashMapChangedListenerAndApply(); - maybeRepublishMailBoxMessages(); - } - } - - @Override - public void onHiddenServicePublished() { - } - - /////////////////////////////////////////////////////////////////////////////////////////// // HashMapChangedListener implementation for ProtectedStorageEntry items /////////////////////////////////////////////////////////////////////////////////////////// From 78f59b65533a63ce64c5f26cb184020f6b4cea5d Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:13:01 -0500 Subject: [PATCH 2/9] Call onNoSeedNodeAvailable on mailboxMessageService at onNoSeedNodeAvailable --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index b3e259022ad..43be1f63271 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -330,6 +330,7 @@ public void onUpdatedDataReceived() { @Override public void onNoSeedNodeAvailable() { + mailboxMessageService.onNoSeedNodeAvailable(); p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); } From cc203eb5d7a9accfce4c81cc1ce70dd86f074267 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:14:48 -0500 Subject: [PATCH 3/9] Merge onUpdatedDataReceived and onNoSeedNodeAvailable to onBootstrapped --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 6 +++--- .../network/p2p/mailbox/MailboxMessageService.java | 11 +---------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index 43be1f63271..e95cc8dff5f 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -258,7 +258,7 @@ public void onTorNodeReady() { if (!seedNodesAvailable) { isBootstrapped = true; - mailboxMessageService.onNoSeedNodeAvailable(); + mailboxMessageService.onBootstrapped(); p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); } } @@ -321,7 +321,7 @@ public void onUpdatedDataReceived() { // We don't use a listener at mailboxMessageService as we require the correct // order of execution. The p2pServiceListeners must be called after // mailboxMessageService.onUpdatedDataReceived. - mailboxMessageService.onUpdatedDataReceived(); + mailboxMessageService.onBootstrapped(); p2pServiceListeners.forEach(P2PServiceListener::onUpdatedDataReceived); p2PDataStorage.onBootstrapComplete(); @@ -330,7 +330,7 @@ public void onUpdatedDataReceived() { @Override public void onNoSeedNodeAvailable() { - mailboxMessageService.onNoSeedNodeAvailable(); + mailboxMessageService.onBootstrapped(); p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); } diff --git a/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java b/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java index 1b79ea29c1b..e505c457388 100644 --- a/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java +++ b/p2p/src/main/java/bisq/network/p2p/mailbox/MailboxMessageService.java @@ -218,7 +218,7 @@ public void readPersisted(Runnable completeHandler) { // We don't listen on requestDataManager directly as we require the correct // order of execution. The p2pService is handling the correct order of execution and we get called // directly from there. - public void onUpdatedDataReceived() { + public void onBootstrapped() { if (!isBootstrapped) { isBootstrapped = true; // Only now we start listening and processing. The p2PDataStorage is our cache for data we have received @@ -228,15 +228,6 @@ public void onUpdatedDataReceived() { } } - public void onNoSeedNodeAvailable() { - if (!isBootstrapped) { - isBootstrapped = true; - // As we do not expect a updated data request response we start here with addHashMapChangedListenerAndApply - addHashMapChangedListenerAndApply(); - maybeRepublishMailBoxMessages(); - } - } - public void sendEncryptedMailboxMessage(NodeAddress peer, PubKeyRing peersPubKeyRing, From 01e32ba3ebf37bbdfc94f06346dbe8894e96d11d Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:16:43 -0500 Subject: [PATCH 4/9] Call p2PDataStorage.onBootstrapComplete() before mailboxMessageService.onBootstrapped(); and onUpdatedDataReceived mailboxMessageService depends on p2PDataStorage so we make sure the p2PDataStorage is updated before we update the mailboxMessageService state. --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index e95cc8dff5f..f100240a023 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -318,13 +318,15 @@ public void onPreliminaryDataReceived() { public void onUpdatedDataReceived() { if (!isBootstrapped) { isBootstrapped = true; + + p2PDataStorage.onBootstrapComplete(); + // We don't use a listener at mailboxMessageService as we require the correct // order of execution. The p2pServiceListeners must be called after // mailboxMessageService.onUpdatedDataReceived. mailboxMessageService.onBootstrapped(); p2pServiceListeners.forEach(P2PServiceListener::onUpdatedDataReceived); - p2PDataStorage.onBootstrapComplete(); } } From 69db9a7a84480605b24595b85d32b6b92b674659 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:17:03 -0500 Subject: [PATCH 5/9] Refactor: Rename onBootstrapComplete to onBootstrapped --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 2 +- p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index f100240a023..a035a681311 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -319,7 +319,7 @@ public void onUpdatedDataReceived() { if (!isBootstrapped) { isBootstrapped = true; - p2PDataStorage.onBootstrapComplete(); + p2PDataStorage.onBootstrapped(); // We don't use a listener at mailboxMessageService as we require the correct // order of execution. The p2pServiceListeners must be called after diff --git a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java index 4607ccc9429..6114f74c16b 100644 --- a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java +++ b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java @@ -561,7 +561,7 @@ void removeExpiredEntries() { } } - public void onBootstrapComplete() { + public void onBootstrapped() { removeExpiredEntriesTimer = UserThread.runPeriodically(this::removeExpiredEntries, CHECK_TTL_INTERVAL_SEC); } From 5490c0a92d739f9ebe8c86dcc9aedd33448669d9 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:21:17 -0500 Subject: [PATCH 6/9] Use same behaviour in onNoSeedNodeAvailable as in onUpdatedDataReceived --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index a035a681311..e1ddaee16d3 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -332,8 +332,18 @@ public void onUpdatedDataReceived() { @Override public void onNoSeedNodeAvailable() { - mailboxMessageService.onBootstrapped(); - p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); + if (!isBootstrapped) { + isBootstrapped = true; + + p2PDataStorage.onBootstrapped(); + + // We don't use a listener at mailboxMessageService as we require the correct + // order of execution. The p2pServiceListeners must be called after + // mailboxMessageService.onUpdatedDataReceived. + mailboxMessageService.onBootstrapped(); + + p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); + } } @Override From 017ed50c16d982b87b15a579f9e12510d0ca909d Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:27:17 -0500 Subject: [PATCH 7/9] Refactor: Extract method from duplicated code --- .../java/bisq/network/p2p/P2PService.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index e1ddaee16d3..9f07f4155e0 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -70,6 +70,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -316,22 +317,25 @@ public void onPreliminaryDataReceived() { @Override public void onUpdatedDataReceived() { - if (!isBootstrapped) { - isBootstrapped = true; - - p2PDataStorage.onBootstrapped(); + applyIsBootstrapped(P2PServiceListener::onUpdatedDataReceived); + } - // We don't use a listener at mailboxMessageService as we require the correct - // order of execution. The p2pServiceListeners must be called after - // mailboxMessageService.onUpdatedDataReceived. - mailboxMessageService.onBootstrapped(); + @Override + public void onNoSeedNodeAvailable() { + applyIsBootstrapped(P2PServiceListener::onNoSeedNodeAvailable); + } - p2pServiceListeners.forEach(P2PServiceListener::onUpdatedDataReceived); - } + @Override + public void onNoPeersAvailable() { + p2pServiceListeners.forEach(P2PServiceListener::onNoPeersAvailable); } @Override - public void onNoSeedNodeAvailable() { + public void onDataReceived() { + p2pServiceListeners.forEach(P2PServiceListener::onDataReceived); + } + + private void applyIsBootstrapped(Consumer listenerHandler) { if (!isBootstrapped) { isBootstrapped = true; @@ -342,20 +346,11 @@ public void onNoSeedNodeAvailable() { // mailboxMessageService.onUpdatedDataReceived. mailboxMessageService.onBootstrapped(); - p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); + // Once we have applied the state in the P2P domain we notify our listeners + p2pServiceListeners.forEach(listenerHandler); } } - @Override - public void onNoPeersAvailable() { - p2pServiceListeners.forEach(P2PServiceListener::onNoPeersAvailable); - } - - @Override - public void onDataReceived() { - p2pServiceListeners.forEach(P2PServiceListener::onDataReceived); - } - /////////////////////////////////////////////////////////////////////////////////////////// // ConnectionListener implementation From 4877c1dc6cd78cba381ba3efe3eafa7e510e828a Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:29:27 -0500 Subject: [PATCH 8/9] Call onNoSeedNodeAvailable if return value from requestPreliminaryData is false We did not do all the calls before (like on p2pDataStorage), so that changes behaviour. --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index 9f07f4155e0..b22672217c4 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -258,9 +258,7 @@ public void onTorNodeReady() { p2pServiceListeners.forEach(SetupListener::onTorNodeReady); if (!seedNodesAvailable) { - isBootstrapped = true; - mailboxMessageService.onBootstrapped(); - p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable); + onNoSeedNodeAvailable(); } } From 1825f3308fcbd099a9d5a1ffa5cbfc0df6543c6b Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Thu, 18 Feb 2021 16:31:17 -0500 Subject: [PATCH 9/9] Remove boolean return value for requestPreliminaryData and call onNoSeedNodeAvailable instead. --- p2p/src/main/java/bisq/network/p2p/P2PService.java | 7 +------ .../bisq/network/p2p/peers/getdata/RequestDataManager.java | 5 ++--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index b22672217c4..0bac8ac70bf 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -252,14 +252,9 @@ private void doShutDown() { public void onTorNodeReady() { socks5ProxyProvider.setSocks5ProxyInternal(networkNode); - boolean seedNodesAvailable = requestDataManager.requestPreliminaryData(); - + requestDataManager.requestPreliminaryData(); keepAliveManager.start(); p2pServiceListeners.forEach(SetupListener::onTorNodeReady); - - if (!seedNodesAvailable) { - onNoSeedNodeAvailable(); - } } @Override diff --git a/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataManager.java b/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataManager.java index 7bf2d8fd361..b390706bc18 100644 --- a/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataManager.java +++ b/p2p/src/main/java/bisq/network/p2p/peers/getdata/RequestDataManager.java @@ -155,7 +155,7 @@ public void setListener(Listener listener) { this.listener = listener; } - public boolean requestPreliminaryData() { + public void requestPreliminaryData() { ArrayList nodeAddresses = new ArrayList<>(seedNodeAddresses); if (!nodeAddresses.isEmpty()) { ArrayList finalNodeAddresses = new ArrayList<>(nodeAddresses); @@ -169,9 +169,8 @@ public boolean requestPreliminaryData() { } isPreliminaryDataRequest = true; - return true; } else { - return false; + checkNotNull(listener).onNoSeedNodeAvailable(); } }