Coverage Summary for Class: FindNodePeerMessage (co.rsk.net.discovery.message)
Class |
Class, %
|
Method, %
|
Line, %
|
FindNodePeerMessage |
0%
(0/1)
|
0%
(0/7)
|
0%
(0/28)
|
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.util.ByteUtil;
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.OptionalInt;
32
33 import static org.ethereum.util.ByteUtil.intToBytes;
34 import static org.ethereum.util.ByteUtil.stripLeadingZeroes;
35
36 /**
37 * Created by mario on 16/02/17.
38 */
39 public class FindNodePeerMessage extends PeerDiscoveryMessage {
40
41 public static final String MORE_DATA = "FindNodePeerMessage needs more data";
42 private byte[] nodeId;
43 private String messageId;
44
45 public FindNodePeerMessage(byte[] wire, byte[] mdc, byte[] signature, byte[] type, byte[] data) {
46 super(wire, mdc, signature, type, data);
47 this.parse(data);
48 }
49
50 private FindNodePeerMessage() {
51 }
52
53 public static FindNodePeerMessage create(byte[] nodeId, String check, ECKey privKey, Integer networkId) {
54
55 /* RLP Encode data */
56 byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8));
57 byte[] rlpNodeId = RLP.encodeElement(nodeId);
58
59 byte[] type = new byte[]{(byte) DiscoveryMessageType.FIND_NODE.getTypeValue()};
60
61 byte[] data;
62 byte[] rlpNetworkId = RLP.encodeElement(stripLeadingZeroes(intToBytes(networkId)));
63 data = RLP.encodeList(rlpNodeId, rlpCheck, rlpNetworkId);
64
65 FindNodePeerMessage message = new FindNodePeerMessage();
66 message.encode(type, data, privKey);
67
68 message.messageId = check;
69 message.nodeId = nodeId;
70 message.setNetworkId(OptionalInt.of(networkId));
71
72 return message;
73 }
74
75 @Override
76 public final void parse(byte[] data) {
77 RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0);
78 if (dataList.size() < 2) {
79 throw new PeerDiscoveryException(MORE_DATA);
80 }
81 RLPItem chk = (RLPItem) dataList.get(1);
82
83 this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8"));
84
85 RLPItem nodeRlp = (RLPItem) dataList.get(0);
86
87 this.nodeId = nodeRlp.getRLPData();
88
89 this.setNetworkIdWithRLP(dataList.size()>2?dataList.get(2):null);
90 }
91
92
93 public String getMessageId() {
94 return this.messageId;
95 }
96
97 @Override
98 public DiscoveryMessageType getMessageType() {
99 return DiscoveryMessageType.FIND_NODE;
100 }
101
102 @Override
103 public String toString() {
104 return new ToStringBuilder(this)
105 .append(ByteUtil.toHexString(this.nodeId))
106 .append(this.getNetworkId())
107 .append(this.messageId).toString();
108 }
109
110 }