Coverage Summary for Class: Node (org.ethereum.net.rlpx)
Class |
Class, %
|
Method, %
|
Line, %
|
Node |
0%
(0/1)
|
0%
(0/13)
|
0%
(0/52)
|
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.rlpx;
21
22 import co.rsk.net.NodeID;
23 import org.bouncycastle.util.encoders.Hex;
24 import org.ethereum.util.ByteUtil;
25 import org.ethereum.util.RLP;
26 import org.ethereum.util.RLPList;
27
28 import java.io.Serializable;
29 import java.net.InetAddress;
30 import java.net.InetSocketAddress;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.nio.charset.Charset;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Arrays;
36 import java.util.Objects;
37
38 import static org.ethereum.util.ByteUtil.byteArrayToInt;
39
40 public class Node implements Serializable {
41 private static final long serialVersionUID = -4267600517925770636L;
42
43 private final byte[] id;
44 private final String host;
45 private final int port;
46
47 public Node(String enodeURL) {
48 try {
49 URI uri = new URI(enodeURL);
50 if (!uri.getScheme().equals("enode")) {
51 throw new RuntimeException("expecting URL in the format enode://PUBKEY@HOST:PORT");
52 }
53 this.id = Hex.decode(uri.getUserInfo());
54 this.host = uri.getHost();
55 this.port = uri.getPort();
56 } catch (URISyntaxException e) {
57 throw new RuntimeException("expecting URL in the format enode://PUBKEY@HOST:PORT", e);
58 }
59 }
60
61 public Node(byte[] id, String host, int port) {
62 this.id = id;
63 this.host = host;
64 this.port = port;
65 }
66
67 public Node(byte[] rlp) {
68 RLPList nodeRLP = (RLPList)RLP.decode2(rlp).get(0);
69
70 byte[] hostB = nodeRLP.get(0).getRLPData();
71 byte[] portB = nodeRLP.get(1).getRLPData();
72 byte[] idB;
73
74 //Check getRLP()
75 if (nodeRLP.size() > 3) {
76 idB = nodeRLP.get(3).getRLPData();
77 } else {
78 idB = nodeRLP.get(2).getRLPData();
79 }
80
81 String host = new String(hostB, Charset.forName("UTF-8"));
82 int port = byteArrayToInt(portB);
83
84 this.id = idB;
85 this.host = host;
86 this.port = port;
87 }
88
89
90 public NodeID getId() {
91 return new NodeID(id);
92 }
93
94 public String getHexId() {
95 return ByteUtil.toHexString(id);
96 }
97
98 public String getHost() {
99 return host;
100 }
101
102 public int getPort() {
103 return port;
104 }
105
106 public byte[] getRLP() {
107 byte[] rlphost = RLP.encodeElement(host.getBytes(StandardCharsets.UTF_8));
108 byte[] rlpTCPPort = RLP.encodeInt(port);
109 byte[] rlpUDPPort = RLP.encodeInt(port);
110 byte[] rlpId = RLP.encodeElement(id);
111
112 return RLP.encodeList(rlphost, rlpUDPPort, rlpTCPPort, rlpId);
113 }
114
115 public InetSocketAddress getAddress() {
116 return new InetSocketAddress(this.getHost(), this.getPort());
117 }
118
119 public String getAddressAsString() {
120 InetSocketAddress address = this.getAddress();
121 InetAddress addr = address.getAddress();
122 // addr == null if the hostname can't be resolved
123 return (addr == null ? address.getHostString() : addr.getHostAddress()) + ":" + address.getPort();
124 }
125
126
127 @Override
128 public String toString() {
129 return "Node{" +
130 " host='" + host + '\'' +
131 ", port=" + port +
132 ", id=" + getHexId() +
133 '}';
134 }
135
136 @Override
137 public int hashCode() {
138 return Objects.hash(host, port, id);
139 }
140
141 @Override
142 public boolean equals(Object o) {
143 if (o == null) {
144 return false;
145 }
146
147 if (o == this) {
148 return true;
149 }
150
151 if (!(o instanceof Node)) {
152 return false;
153 }
154
155 // TODO(mc): do we need to check host and port too?
156 return Arrays.equals(id, ((Node) o).id);
157 }
158 }