Skip to content

Commit

Permalink
limit logging of invalid connection requests
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jul 9, 2024
1 parent 1fc8cff commit 94fd99c
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions p2p/src/main/java/haveno/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public static int getShutdownTimeout() {

private final Capabilities capabilities = new Capabilities();

// throttle logs of reported invalid requests
private static long lastLoggedInvalidRequestReport = 0;
private static int unloggedInvalidRequestReports = 0;
private static final long LOG_INVALID_REQUEST_REPORTS_INTERVAL_MS = 60000; // log invalid request reports once every 60s

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -606,35 +611,55 @@ public String printDetails() {
*/

public boolean reportInvalidRequest(RuleViolation ruleViolation) {
log.info("We got reported the ruleViolation {} at connection with address{} and uid {}", ruleViolation, this.getPeersNodeAddressProperty(), this.getUid());
int numRuleViolations;
numRuleViolations = ruleViolations.getOrDefault(ruleViolation, 0);
return Connection.reportInvalidRequest(this, ruleViolation);
}

numRuleViolations++;
ruleViolations.put(ruleViolation, numRuleViolations);
private static synchronized boolean reportInvalidRequest(Connection connection, RuleViolation ruleViolation) {

// determine if report should be logged to avoid spamming the logs
boolean logReport = System.currentTimeMillis() - lastLoggedInvalidRequestReport > LOG_INVALID_REQUEST_REPORTS_INTERVAL_MS;

// count the number of unlogged reports since last log entry
if (!logReport) unloggedInvalidRequestReports++;

// handle report
if (logReport) log.info("We got reported the ruleViolation {} at connection with address {} and uid {}", ruleViolation, connection.getPeersNodeAddressProperty(), connection.getUid());
int numRuleViolations;
numRuleViolations = connection.ruleViolations.getOrDefault(ruleViolation, 0);
numRuleViolations++;
connection.ruleViolations.put(ruleViolation, numRuleViolations);
if (numRuleViolations >= ruleViolation.maxTolerance) {
log.warn("We close connection as we received too many corrupt requests. " +
if (logReport) log.warn("We close connection as we received too many corrupt requests. " +
"ruleViolations={} " +
"connection with address{} and uid {}", ruleViolations, peersNodeAddressProperty, uid);
this.ruleViolation = ruleViolation;
"connection with address {} and uid {}", connection.ruleViolations, connection.peersNodeAddressProperty, connection.uid);
connection.ruleViolation = ruleViolation;
if (ruleViolation == RuleViolation.PEER_BANNED) {
log.debug("We close connection due RuleViolation.PEER_BANNED. peersNodeAddress={}", getPeersNodeAddressOptional());
shutDown(CloseConnectionReason.PEER_BANNED);
if (logReport) log.debug("We close connection due RuleViolation.PEER_BANNED. peersNodeAddress={}", connection.getPeersNodeAddressOptional());
connection.shutDown(CloseConnectionReason.PEER_BANNED);
} else if (ruleViolation == RuleViolation.INVALID_CLASS) {
log.warn("We close connection due RuleViolation.INVALID_CLASS");
shutDown(CloseConnectionReason.INVALID_CLASS_RECEIVED);
if (logReport) log.warn("We close connection due RuleViolation.INVALID_CLASS");
connection.shutDown(CloseConnectionReason.INVALID_CLASS_RECEIVED);
} else {
log.warn("We close connection due RuleViolation.RULE_VIOLATION");
shutDown(CloseConnectionReason.RULE_VIOLATION);
if (logReport) log.warn("We close connection due RuleViolation.RULE_VIOLATION");
connection.shutDown(CloseConnectionReason.RULE_VIOLATION);
}

resetReportedInvalidRequestsThrottle(logReport);
return true;
} else {
resetReportedInvalidRequestsThrottle(logReport);
return false;
}
}

private static synchronized void resetReportedInvalidRequestsThrottle(boolean logReport) {
if (logReport) {
if (unloggedInvalidRequestReports > 0) log.warn("We received {} other reports of invalid requests since the last log entry", unloggedInvalidRequestReports);
unloggedInvalidRequestReports = 0;
lastLoggedInvalidRequestReport = System.currentTimeMillis();
}
}

private void handleException(Throwable e) {
CloseConnectionReason closeConnectionReason;

Expand Down

0 comments on commit 94fd99c

Please sign in to comment.