Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.5.4] Check for duplicate hashes at onBundleOfEnvelopes #5032

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions p2p/src/main/java/bisq/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.network.p2p.peers.getdata.messages.GetDataRequest;
import bisq.network.p2p.peers.getdata.messages.GetDataResponse;
import bisq.network.p2p.peers.keepalive.messages.KeepAliveMessage;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.messages.AddDataMessage;
import bisq.network.p2p.storage.messages.AddPersistableNetworkPayloadMessage;
import bisq.network.p2p.storage.payload.CapabilityRequiringPayload;
Expand Down Expand Up @@ -425,41 +426,40 @@ private boolean violatesThrottleLimit(long now, int seconds, int messageCountLim
@Override
public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
checkArgument(connection.equals(this));
int accountAgeWitnessEntries = 0;
if (networkEnvelope instanceof BundleOfEnvelopes) {
Map<String, List<NetworkEnvelope>> map = new HashMap<>();
Set<NetworkEnvelope> set = new HashSet<>();

List<NetworkEnvelope> networkEnvelopes = ((BundleOfEnvelopes) networkEnvelope).getEnvelopes();
for (NetworkEnvelope current : networkEnvelopes) {
String simpleName = current.getClass().getSimpleName();
boolean isAccountAgeWitness = false;
if (current instanceof AddPersistableNetworkPayloadMessage) {
PersistableNetworkPayload persistableNetworkPayload = ((AddPersistableNetworkPayloadMessage) current).getPersistableNetworkPayload();
simpleName = "AddPersistableNetworkPayloadMessage." + persistableNetworkPayload.getClass().getSimpleName();
if (simpleName.equals("AddPersistableNetworkPayloadMessage.AccountAgeWitness")) {
accountAgeWitnessEntries++;
isAccountAgeWitness = true;
}
}
map.putIfAbsent(simpleName, new ArrayList<>());
map.get(simpleName).add(current);
if (!isAccountAgeWitness || accountAgeWitnessEntries < 20) {
set.add(current);
}
}
map.forEach((key, value) -> log.info("BundleOfEnvelope with {} items of {}, from {}",
value.size(), key, connection.getPeersNodeAddressOptional()));

log.info("We forward {} items. All received items: {}", set.size(), networkEnvelopes.size());

set.forEach(envelope -> UserThread.execute(() ->
messageListeners.forEach(listener -> listener.onMessage(envelope, connection))));
onBundleOfEnvelopes((BundleOfEnvelopes) networkEnvelope, connection);
} else {
UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(networkEnvelope, connection)));
}
}

private void onBundleOfEnvelopes(BundleOfEnvelopes networkEnvelope, Connection connection) {
Map<P2PDataStorage.ByteArray, Set<NetworkEnvelope>> itemsByHash = new HashMap<>();
Set<NetworkEnvelope> envelopesToProcess = new HashSet<>();
List<NetworkEnvelope> networkEnvelopes = networkEnvelope.getEnvelopes();
for (NetworkEnvelope current : networkEnvelopes) {
if (current instanceof AddPersistableNetworkPayloadMessage) {
PersistableNetworkPayload persistableNetworkPayload = ((AddPersistableNetworkPayloadMessage) current).getPersistableNetworkPayload();
byte[] hash = persistableNetworkPayload.getHash();
String itemName = persistableNetworkPayload.getClass().getSimpleName();
P2PDataStorage.ByteArray byteArray = new P2PDataStorage.ByteArray(hash);
itemsByHash.putIfAbsent(byteArray, new HashSet<>());
Set<NetworkEnvelope> envelopesByHash = itemsByHash.get(byteArray);
if (!envelopesByHash.contains(current)) {
envelopesByHash.add(current);
envelopesToProcess.add(current);
} else {
log.debug("We got duplicated items for {}. We ignore the duplicates. Hash: {}",
itemName, Utilities.encodeToHex(hash));
}
} else {
envelopesToProcess.add(current);
}
}
envelopesToProcess.forEach(envelope -> UserThread.execute(() ->
messageListeners.forEach(listener -> listener.onMessage(envelope, connection))));
}


///////////////////////////////////////////////////////////////////////////////////////////
// Setters
Expand Down