Coverage Summary for Class: NeighborsPeerMessage (co.rsk.net.discovery.message)
Class |
Class, %
|
Method, %
|
Line, %
|
NeighborsPeerMessage |
0%
(0/1)
|
0%
(0/9)
|
0%
(0/43)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.net.discovery.message;
20
21 import co.rsk.net.discovery.PeerDiscoveryException;
22 import org.apache.commons.lang3.builder.ToStringBuilder;
23 import org.ethereum.crypto.ECKey;
24 import org.ethereum.net.rlpx.Node;
25 import org.ethereum.util.RLP;
26 import org.ethereum.util.RLPItem;
27 import org.ethereum.util.RLPList;
28
29 import java.nio.charset.Charset;
30 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.OptionalInt;
34
35 import static org.ethereum.util.ByteUtil.intToBytes;
36 import static org.ethereum.util.ByteUtil.stripLeadingZeroes;
37
38 /**
39 * Created by mario on 16/02/17.
40 */
41 public class NeighborsPeerMessage extends PeerDiscoveryMessage {
42 public static final String MORE_DATA = "NeighborsPeerMessage needs more data";
43 private List<Node> nodes;
44 private String messageId;
45
46 public NeighborsPeerMessage(byte[] wire, byte[] mdc, byte[] signature, byte[] type, byte[] data) {
47 super(wire, mdc, signature, type, data);
48 this.nodes = new ArrayList<>();
49 this.parse(data);
50 }
51
52 private NeighborsPeerMessage() {
53 }
54
55 @Override
56 public final void parse(byte[] data) {
57 RLPList list = (RLPList) RLP.decode2OneItem(data, 0);
58
59 if (list.size() < 2) {
60 throw new PeerDiscoveryException(MORE_DATA);
61 }
62
63 RLPList nodesRLP = (RLPList) list.get(0);
64
65 for (int i = 0; i < nodesRLP.size(); ++i) {
66 RLPList nodeRLP = (RLPList) nodesRLP.get(i);
67 Node node = new Node(nodeRLP.getRLPData());
68 nodes.add(node);
69 }
70
71 RLPItem chk = (RLPItem) list.get(1);
72
73 this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8"));
74
75 this.setNetworkIdWithRLP(list.size()>2?list.get(2):null);
76 }
77
78 public static NeighborsPeerMessage create(List<Node> nodes, String check, ECKey privKey, Integer networkId) {
79 byte[][] nodeRLPs = null;
80
81 if (nodes != null) {
82 nodeRLPs = new byte[nodes.size()][];
83 int i = 0;
84 for (Node node : nodes) {
85 nodeRLPs[i] = node.getRLP();
86 ++i;
87 }
88 }
89
90 byte[] rlpListNodes = RLP.encodeList(nodeRLPs);
91 byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8));
92
93 byte[] type = new byte[]{(byte) DiscoveryMessageType.NEIGHBORS.getTypeValue()};
94 byte[] data;
95 byte[] tmpNetworkId = intToBytes(networkId);
96 byte[] rlpNetworkId = RLP.encodeElement(stripLeadingZeroes(tmpNetworkId));
97 data = RLP.encodeList(rlpListNodes, rlpCheck, rlpNetworkId);
98
99 NeighborsPeerMessage neighborsMessage = new NeighborsPeerMessage();
100 neighborsMessage.encode(type, data, privKey);
101 neighborsMessage.setNetworkId(OptionalInt.of(networkId));
102 neighborsMessage.nodes = nodes;
103 neighborsMessage.messageId = check;
104
105 return neighborsMessage;
106 }
107
108 public List<Node> getNodes() {
109 return nodes;
110 }
111
112 public String getMessageId() {
113 return this.messageId;
114 }
115
116 @Override
117 public DiscoveryMessageType getMessageType() {
118 return DiscoveryMessageType.NEIGHBORS;
119 }
120
121 public int countNodes() {
122 return this.nodes.size();
123 }
124
125 @Override
126 public String toString() {
127 return new ToStringBuilder(this)
128 .append(this.nodes)
129 .append(this.messageId)
130 .append(this.getNetworkId()).toString();
131 }
132
133 }