Coverage Summary for Class: HandshakeMessage (org.ethereum.net.rlpx)
Class |
Class, %
|
Method, %
|
Line, %
|
HandshakeMessage |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/34)
|
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 org.ethereum.net.client.Capability;
23 import org.ethereum.util.*;
24
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import static org.ethereum.util.ByteUtil.longToBytes;
30
31 /**
32 * Created by devrandom on 2015-04-12.
33 */
34 public class HandshakeMessage {
35 public static final int HANDSHAKE_MESSAGE_TYPE = 0x00;
36 long version;
37 String name;
38 List<Capability> caps;
39 long listenPort;
40 byte[] nodeId;
41
42 public static final int NODE_ID_BITS = 512;
43
44 public HandshakeMessage(long version, String name, List<Capability> caps, long listenPort, byte[] nodeId) {
45 this.version = version;
46 this.name = name;
47 this.caps = caps;
48 this.listenPort = listenPort;
49 this.nodeId = nodeId;
50 }
51
52 HandshakeMessage() {
53 }
54
55 static HandshakeMessage parse(byte[] wire) {
56 RLPList list = (RLPList) RLP.decode2(wire).get(0);
57 HandshakeMessage message = new HandshakeMessage();
58 message.version = ByteUtil.byteArrayToInt(list.get(0).getRLPData()); // FIXME long
59 message.name = new String(list.get(1).getRLPData(), StandardCharsets.UTF_8);
60 // caps
61 message.caps = new ArrayList<>();
62
63 RLPList capElements = (RLPList)list.get(2);
64
65 for (int k = 0; k < capElements.size(); k++) {
66 RLPElement capEl = capElements.get(k);
67 RLPList capElList = (RLPList)capEl;
68 String name = new String(capElList.get(0).getRLPData(), StandardCharsets.UTF_8);
69 long version = ByteUtil.byteArrayToInt(capElList.get(1).getRLPData());
70
71 message.caps.add(new Capability(name, (byte)version)); // FIXME long
72 }
73
74 message.listenPort = ByteUtil.byteArrayToInt(list.get(3).getRLPData());
75 message.nodeId = list.get(4).getRLPData();
76
77 return message;
78 }
79
80 public byte[] encode() {
81 List<byte[]> capsItemBytes = new ArrayList<>();
82 for (Capability cap : caps) {
83 capsItemBytes.add(RLP.encodeList(
84 RLP.encodeElement(cap.getName().getBytes(StandardCharsets.UTF_8)),
85 RLP.encodeElement(ByteUtil.stripLeadingZeroes(longToBytes(cap.getVersion())))
86 ));
87 }
88 return RLP.encodeList(
89 RLP.encodeElement(ByteUtil.stripLeadingZeroes(longToBytes(version))),
90 RLP.encodeElement(name.getBytes(StandardCharsets.UTF_8)),
91 RLP.encodeList(capsItemBytes.toArray(new byte[0][])),
92 RLP.encodeElement(ByteUtil.stripLeadingZeroes(longToBytes(listenPort))),
93 RLP.encodeElement(nodeId)
94 );
95 }
96 }