Skip to content

Commit c4e52b1

Browse files
Add a message for disconnect message reasons (#9014)
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
1 parent bf6d2b4 commit c4e52b1

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/BlockRangeBroadcaster.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void handleBlockRangeUpdateMessage(final EthMessage message) {
8383
"Invalid block range update message received: earliest={}, latest={}",
8484
earliestBlockNumber,
8585
latestBlockNumber);
86-
handleInvalidMessage(message, DisconnectMessage.DisconnectReason.SUBPROTOCOL_TRIGGERED);
86+
handleInvalidMessage(
87+
message, DisconnectMessage.DisconnectReason.SUBPROTOCOL_TRIGGERED_INVALID_BLOCK_RANGE);
8788
}
8889
} catch (final RLPException e) {
8990
LOG.atTrace()

ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/BlockRangeBroadcasterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public void shouldNotDisconnectIfLatestBlockNumberIsEqualToEarliest() {
138138
public void shouldDisconnectIfLatestBlockNumberIsLessThanEarliest() {
139139
final EthPeer peer = mock(EthPeer.class);
140140
handleBlockRangeUpdateMessage(peer, 1L, 0L);
141-
verify(peer).disconnect(DisconnectMessage.DisconnectReason.SUBPROTOCOL_TRIGGERED);
141+
verify(peer)
142+
.disconnect(DisconnectMessage.DisconnectReason.SUBPROTOCOL_TRIGGERED_INVALID_BLOCK_RANGE);
142143
}
143144

144145
private void handleBlockRangeUpdateMessage(

ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/PeerDenylistManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onDisconnect(
5252
final boolean initiatedByPeer) {
5353
// we have a number of reasons that use the same code, but with different message strings
5454
// so here we use the code of the reason param to ensure we get the no-message version
55-
if (shouldBlock(DisconnectReason.forCode(reason.getValue()), initiatedByPeer)) {
55+
if (shouldBlock(DisconnectReason.forCode(reason.getCode()), initiatedByPeer)) {
5656
if (maintainedPeers.contains(connection.getPeer())) {
5757
LOG.debug(
5858
"Skip adding maintained peer {} to peer denylist for reason {}",

ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/wire/messages/DisconnectMessage.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.hyperledger.besu.ethereum.rlp.RLPInput;
2222
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
2323

24+
import java.util.HashMap;
25+
import java.util.Map;
2426
import java.util.Optional;
2527
import java.util.stream.Stream;
2628

@@ -70,7 +72,7 @@ public Data(final DisconnectReason reason) {
7072

7173
public void writeTo(final RLPOutput out) {
7274
out.startList();
73-
out.writeBytes(reason.getValue());
75+
out.writeBytes(reason.getCode());
7476
out.endList();
7577
}
7678

@@ -105,11 +107,10 @@ public DisconnectReason getReason() {
105107
* Transport Protocol</a>
106108
*/
107109
public enum DisconnectReason {
108-
UNKNOWN(null),
109-
REQUESTED((byte) 0x00),
110-
TCP_SUBSYSTEM_ERROR((byte) 0x01),
111-
112-
BREACH_OF_PROTOCOL((byte) 0x02),
110+
UNKNOWN(null, "Unknown reason"),
111+
REQUESTED((byte) 0x00, "Requested disconnection"),
112+
TCP_SUBSYSTEM_ERROR((byte) 0x01, "TCP subsystem error"),
113+
BREACH_OF_PROTOCOL((byte) 0x02, "Breach of protocol"),
113114
BREACH_OF_PROTOCOL_RECEIVED_OTHER_MESSAGE_BEFORE_STATUS(
114115
(byte) 0x02, "Message other than status received first"),
115116
BREACH_OF_PROTOCOL_UNSOLICITED_MESSAGE_RECEIVED((byte) 0x02, "Unsolicited message received"),
@@ -123,7 +124,7 @@ public enum DisconnectReason {
123124
(byte) 0x02, "A message was received before hello's exchanged"),
124125
BREACH_OF_PROTOCOL_INVALID_MESSAGE_RECEIVED_CAUGHT_EXCEPTION(
125126
(byte) 0x02, "An exception was caught decoding message"),
126-
USELESS_PEER((byte) 0x03),
127+
USELESS_PEER((byte) 0x03, "Useless peer"),
127128
USELESS_PEER_USELESS_RESPONSES((byte) 0x03, "Useless responses: exceeded threshold"),
128129
USELESS_PEER_TRAILING_PEER((byte) 0x03, "Trailing peer requirement"),
129130
USELESS_PEER_NO_SHARED_CAPABILITIES((byte) 0x03, "No shared capabilities"),
@@ -134,26 +135,27 @@ public enum DisconnectReason {
134135
USELESS_PEER_BY_REPUTATION((byte) 0x03, "Lowest reputation score"),
135136
USELESS_PEER_BY_CHAIN_COMPARATOR((byte) 0x03, "Lowest by chain height comparator"),
136137
USELESS_PEER_EXCEEDS_TRAILING_PEERS((byte) 0x03, "Adding peer would exceed max trailing peers"),
137-
TOO_MANY_PEERS((byte) 0x04),
138-
ALREADY_CONNECTED((byte) 0x05),
139-
INCOMPATIBLE_P2P_PROTOCOL_VERSION((byte) 0x06),
140-
NULL_NODE_ID((byte) 0x07),
141-
CLIENT_QUITTING((byte) 0x08),
142-
UNEXPECTED_ID((byte) 0x09),
143-
LOCAL_IDENTITY((byte) 0x0a),
144-
TIMEOUT((byte) 0x0b),
145-
SUBPROTOCOL_TRIGGERED((byte) 0x10),
138+
TOO_MANY_PEERS((byte) 0x04, "Too many peers"),
139+
ALREADY_CONNECTED((byte) 0x05, "Already connected"),
140+
INCOMPATIBLE_P2P_PROTOCOL_VERSION((byte) 0x06, "Incompatible P2P protocol version"),
141+
NULL_NODE_ID((byte) 0x07, "Null node ID"),
142+
CLIENT_QUITTING((byte) 0x08, "Client quitting"),
143+
UNEXPECTED_ID((byte) 0x09, "Unexpected ID"),
144+
LOCAL_IDENTITY((byte) 0x0a, "Local identity"),
145+
TIMEOUT((byte) 0x0b, "Timeout"),
146+
SUBPROTOCOL_TRIGGERED((byte) 0x10, "Sub protocol triggered"),
146147
SUBPROTOCOL_TRIGGERED_MISMATCHED_NETWORK((byte) 0x10, "Mismatched network id"),
147148
SUBPROTOCOL_TRIGGERED_MISMATCHED_FORKID((byte) 0x10, "Mismatched fork id"),
148149
SUBPROTOCOL_TRIGGERED_MISMATCHED_GENESIS_HASH((byte) 0x10, "Mismatched genesis hash"),
149150
SUBPROTOCOL_TRIGGERED_UNPARSABLE_STATUS((byte) 0x10, "Unparsable status message"),
150151
SUBPROTOCOL_TRIGGERED_POW_DIFFICULTY((byte) 0x10, "Peer has difficulty greater than POS TTD"),
151152
SUBPROTOCOL_TRIGGERED_POW_BLOCKS((byte) 0x10, "Peer sent blocks after POS transition"),
152-
SUBPROTOCOL_TRIGGERED_INVALID_STATUS_MESSAGE((byte) 0x10, "Peer sent invalid status message");
153+
SUBPROTOCOL_TRIGGERED_INVALID_STATUS_MESSAGE((byte) 0x10, "Peer sent invalid status message"),
154+
SUBPROTOCOL_TRIGGERED_INVALID_BLOCK_RANGE((byte) 0x10, "Invalid block range");
153155

154-
private static final DisconnectReason[] BY_ID;
156+
private static final Map<Byte, DisconnectReason> BY_ID;
155157
private final Optional<Byte> code;
156-
private final Optional<String> message;
158+
private final String message;
157159

158160
static {
159161
final int maxValue =
@@ -162,18 +164,18 @@ public enum DisconnectReason {
162164
.mapToInt(r -> (int) r.code.get())
163165
.max()
164166
.getAsInt();
165-
BY_ID = new DisconnectReason[maxValue + 1];
166-
Stream.of(DisconnectReason.values())
167-
.filter(r -> r.code.isPresent() && r.message.isEmpty())
168-
.forEach(r -> BY_ID[r.code.get()] = r);
167+
BY_ID = new HashMap<>(maxValue + 1);
168+
for (DisconnectReason reason : DisconnectReason.values()) {
169+
reason.code.ifPresent(code -> BY_ID.putIfAbsent(code, reason));
170+
}
169171
}
170172

171173
public static DisconnectReason forCode(final Byte code) {
172-
if (code == null || code >= BY_ID.length || code < 0 || BY_ID[code] == null) {
174+
if (code == null || code < 0 || !BY_ID.containsKey(code)) {
173175
// Be permissive and just return unknown if the disconnect reason is bad
174176
return UNKNOWN;
175177
}
176-
return BY_ID[code];
178+
return BY_ID.get(code);
177179
}
178180

179181
public static DisconnectReason forCode(final Bytes codeBytes) {
@@ -184,27 +186,22 @@ public static DisconnectReason forCode(final Bytes codeBytes) {
184186
}
185187
}
186188

187-
DisconnectReason(final Byte code) {
188-
this.code = Optional.ofNullable(code);
189-
this.message = Optional.empty();
190-
}
191-
192189
DisconnectReason(final Byte code, final String message) {
193190
this.code = Optional.ofNullable(code);
194-
this.message = Optional.of(message);
191+
this.message = message;
195192
}
196193

197-
public Bytes getValue() {
194+
public Bytes getCode() {
198195
return code.map(Bytes::of).orElse(Bytes.EMPTY);
199196
}
200197

201198
public String getMessage() {
202-
return message.orElse("");
199+
return message;
203200
}
204201

205202
@Override
206203
public String toString() {
207-
return getValue().toString() + " " + name() + " " + getMessage();
204+
return getCode().toString() + " " + name() + " " + getMessage();
208205
}
209206
}
210207
}

0 commit comments

Comments
 (0)