2121import org .hyperledger .besu .ethereum .rlp .RLPInput ;
2222import org .hyperledger .besu .ethereum .rlp .RLPOutput ;
2323
24+ import java .util .HashMap ;
25+ import java .util .Map ;
2426import java .util .Optional ;
2527import 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