Skip to content

Commit

Permalink
Merge pull request #5205 from chimp1984/fix-multiple-requestPrelimina…
Browse files Browse the repository at this point in the history
…ryData-calls

Fix multiple request preliminary data calls
  • Loading branch information
ripcurlx authored Feb 19, 2021
2 parents 14ae0c3 + 1825f33 commit cc773b8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
38 changes: 20 additions & 18 deletions p2p/src/main/java/bisq/network/p2p/P2PService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -251,15 +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) {
isBootstrapped = true;
p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable);
}
}

@Override
Expand Down Expand Up @@ -315,21 +310,12 @@ public void onPreliminaryDataReceived() {

@Override
public void onUpdatedDataReceived() {
if (!isBootstrapped) {
isBootstrapped = true;
// 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();

p2pServiceListeners.forEach(P2PServiceListener::onUpdatedDataReceived);
p2PDataStorage.onBootstrapComplete();
}
applyIsBootstrapped(P2PServiceListener::onUpdatedDataReceived);
}

@Override
public void onNoSeedNodeAvailable() {
p2pServiceListeners.forEach(P2PServiceListener::onNoSeedNodeAvailable);
applyIsBootstrapped(P2PServiceListener::onNoSeedNodeAvailable);
}

@Override
Expand All @@ -342,6 +328,22 @@ public void onDataReceived() {
p2pServiceListeners.forEach(P2PServiceListener::onDataReceived);
}

private void applyIsBootstrapped(Consumer<P2PServiceListener> listenerHandler) {
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();

// Once we have applied the state in the P2P domain we notify our listeners
p2pServiceListeners.forEach(listenerHandler);
}
}


///////////////////////////////////////////////////////////////////////////////////////////
// ConnectionListener implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MailboxMessageList> persistenceManager;
Expand All @@ -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<MailboxMessageList> persistenceManager,
Expand All @@ -147,16 +142,13 @@ 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;
this.keyRing = keyRing;
this.clock = clock;
this.republishMailboxEntries = republishMailboxEntries;

this.networkNode.addSetupListener(this);

this.persistenceManager.initialize(mailboxMessageList, PersistenceManager.Source.PRIVATE_LOW_PRIO);
}

Expand Down Expand Up @@ -226,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
Expand All @@ -236,6 +228,7 @@ public void onUpdatedDataReceived() {
}
}


public void sendEncryptedMailboxMessage(NodeAddress peer,
PubKeyRing peersPubKeyRing,
MailboxMessage mailboxMessage,
Expand Down Expand Up @@ -343,25 +336,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
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void setListener(Listener listener) {
this.listener = listener;
}

public boolean requestPreliminaryData() {
public void requestPreliminaryData() {
ArrayList<NodeAddress> nodeAddresses = new ArrayList<>(seedNodeAddresses);
if (!nodeAddresses.isEmpty()) {
ArrayList<NodeAddress> finalNodeAddresses = new ArrayList<>(nodeAddresses);
Expand All @@ -169,9 +169,8 @@ public boolean requestPreliminaryData() {
}

isPreliminaryDataRequest = true;
return true;
} else {
return false;
checkNotNull(listener).onNoSeedNodeAvailable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ void removeExpiredEntries() {
}
}

public void onBootstrapComplete() {
public void onBootstrapped() {
removeExpiredEntriesTimer = UserThread.runPeriodically(this::removeExpiredEntries, CHECK_TTL_INTERVAL_SEC);
}

Expand Down

0 comments on commit cc773b8

Please sign in to comment.