Filter out AccountAgeWitness data if it appears to be a dos attack or caused from a bug

This commit is contained in:
chimp1984 2020-12-30 06:13:57 -05:00
parent 510a941cb4
commit a6413e4c6f
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3

View File

@ -68,9 +68,13 @@ import java.io.OptionalDataException;
import java.io.StreamCorruptedException; import java.io.StreamCorruptedException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Queue; import java.util.Queue;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -421,11 +425,36 @@ public class Connection implements HasCapabilities, Runnable, MessageListener {
@Override @Override
public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) { public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
checkArgument(connection.equals(this)); checkArgument(connection.equals(this));
int accountAgeWitnessEntries = 0;
if (networkEnvelope instanceof BundleOfEnvelopes) { if (networkEnvelope instanceof BundleOfEnvelopes) {
for (NetworkEnvelope current : ((BundleOfEnvelopes) networkEnvelope).getEnvelopes()) { Map<String, List<NetworkEnvelope>> map = new HashMap<>();
UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(current, connection))); 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))));
} else { } else {
UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(networkEnvelope, connection))); UserThread.execute(() -> messageListeners.forEach(e -> e.onMessage(networkEnvelope, connection)));
} }
@ -718,7 +747,6 @@ public class Connection implements HasCapabilities, Runnable, MessageListener {
lastReadTimeStamp, now, elapsed); lastReadTimeStamp, now, elapsed);
Thread.sleep(20); Thread.sleep(20);
} }
// Reading the protobuffer message from the inputStream // Reading the protobuffer message from the inputStream
protobuf.NetworkEnvelope proto = protobuf.NetworkEnvelope.parseDelimitedFrom(protoInputStream); protobuf.NetworkEnvelope proto = protobuf.NetworkEnvelope.parseDelimitedFrom(protoInputStream);