Coverage Summary for Class: P2pMessageCodes (org.ethereum.net.p2p)
Class |
Class, %
|
Method, %
|
Line, %
|
P2pMessageCodes |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/16)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.net.p2p;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 /**
26 * A list of commands for the Ethereum network protocol.
27 * <br>
28 * The codes for these commands are the first byte in every packet.
29 * ÐΞV
30 *
31 * @see <a href="https://github.com/ethereum/wiki/wiki/%C3%90%CE%9EVp2p-Wire-Protocol">
32 * https://github.com/ethereum/wiki/wiki/ÐΞVp2p-Wire-Protocol</a>
33 */
34 public enum P2pMessageCodes {
35
36 /* P2P protocol */
37
38 /**
39 * [0x00, P2P_VERSION, CLIEND_ID, CAPS, LISTEN_PORT, CLIENT_ID] <br>
40 * First packet sent over the connection, and sent once by both sides.
41 * No other messages may be sent until a Hello is received.
42 */
43 HELLO(0x00),
44
45 /**
46 * [0x01, REASON] <br>Inform the peer that a disconnection is imminent;
47 * if received, a peer should disconnect immediately. When sending,
48 * well-behaved hosts give their peers a fighting chance (read: wait 2 seconds)
49 * to disconnect to before disconnecting themselves.
50 */
51 DISCONNECT(0x01),
52
53 /**
54 * [0x02] <br>Requests an immediate reply of Pong from the peer.
55 */
56 PING(0x02),
57
58 /**
59 * [0x03] <br>Reply to peer's Ping packet.
60 */
61 PONG(0x03),
62
63 /**
64 * [0x04] <br>Request the peer to enumerate some known peers
65 * for us to connect to. This should include the peer itself.
66 */
67 GET_PEERS(0x04),
68
69 /**
70 * [0x05, [IP1, Port1, Id1], [IP2, Port2, Id2], ... ] <br>
71 * Specifies a number of known peers. IP is a 4-byte array 'ABCD'
72 * that should be interpreted as the IP address A.B.C.D.
73 * Port is a 2-byte array that should be interpreted as a
74 * 16-bit big-endian integer. Id is the 512-bit hash that acts
75 * as the unique identifier of the node.
76 */
77 PEERS(0x05),
78
79
80 /**
81 *
82 */
83 USER(0x0F);
84
85
86 private final int cmd;
87
88 private static final Map<Integer, P2pMessageCodes> intToTypeMap = new HashMap<>();
89
90 static {
91 for (P2pMessageCodes type : P2pMessageCodes.values()) {
92 intToTypeMap.put(type.cmd, type);
93 }
94 }
95
96 private P2pMessageCodes(int cmd) {
97 this.cmd = cmd;
98 }
99
100 public static P2pMessageCodes fromByte(byte i) {
101 return intToTypeMap.get((int) i);
102 }
103
104 public static boolean inRange(byte code) {
105 return code >= HELLO.asByte() && code <= USER.asByte();
106 }
107
108 public byte asByte() {
109 return (byte) (cmd);
110 }
111
112 }