Coverage Summary for Class: StatusMessage (org.ethereum.net.eth.message)
Class |
Class, %
|
Method, %
|
Line, %
|
StatusMessage |
0%
(0/1)
|
0%
(0/15)
|
0%
(0/51)
|
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.eth.message;
21
22 import org.ethereum.util.ByteUtil;
23 import org.ethereum.util.RLP;
24 import org.ethereum.util.RLPList;
25
26 import java.math.BigInteger;
27
28 /**
29 * Wrapper for Ethereum STATUS message. <br>
30 *
31 * @see EthMessageCodes#STATUS
32 */
33 public class StatusMessage extends EthMessage {
34 private static final byte[] ZERO_BYTE_ARRAY = new byte[]{0};
35
36 protected byte protocolVersion;
37 protected int networkId;
38
39 /**
40 * Total difficulty of the best chain as found in block header.
41 */
42 protected byte[] totalDifficulty;
43 /**
44 * The hash of the best (i.e. highest TD) known block.
45 */
46 protected byte[] bestHash;
47 /**
48 * The hash of the Genesis block
49 */
50 protected byte[] genesisHash;
51
52 public StatusMessage(byte[] encoded) {
53 super(encoded);
54 }
55
56 public StatusMessage(byte protocolVersion, int networkId,
57 byte[] totalDifficulty, byte[] bestHash, byte[] genesisHash) {
58 this.protocolVersion = protocolVersion;
59 this.networkId = networkId;
60 this.totalDifficulty = totalDifficulty;
61 this.bestHash = bestHash;
62 this.genesisHash = genesisHash;
63 this.parsed = true;
64 }
65
66 protected void parse() {
67 RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
68
69 this.protocolVersion = paramsList.get(0).getRLPData()[0];
70 byte[] networkIdBytes = paramsList.get(1).getRLPData();
71 this.networkId = networkIdBytes == null ? 0 : ByteUtil.byteArrayToInt(networkIdBytes);
72
73 byte[] diff = paramsList.get(2).getRLPData();
74 this.totalDifficulty = (diff == null) ? ZERO_BYTE_ARRAY : diff;
75 this.bestHash = paramsList.get(3).getRLPData();
76 this.genesisHash = paramsList.get(4).getRLPData();
77
78 parsed = true;
79 }
80
81 protected void encode() {
82 byte[] protocolVersion = RLP.encodeByte(this.protocolVersion);
83 byte[] networkId = RLP.encodeInt(this.networkId);
84 byte[] totalDifficulty = RLP.encodeElement(this.totalDifficulty);
85 byte[] bestHash = RLP.encodeElement(this.bestHash);
86 byte[] genesisHash = RLP.encodeElement(this.genesisHash);
87
88 this.encoded = RLP.encodeList( protocolVersion, networkId,
89 totalDifficulty, bestHash, genesisHash);
90 }
91
92 @Override
93 public byte[] getEncoded() {
94 if (encoded == null) {
95 encode();
96 }
97 return encoded;
98 }
99
100 @Override
101 public Class<?> getAnswerMessage() {
102 return null;
103 }
104
105 public byte getProtocolVersion() {
106 if (!parsed) {
107 parse();
108 }
109 return protocolVersion;
110 }
111
112 public int getNetworkId() {
113 if (!parsed) {
114 parse();
115 }
116 return networkId;
117 }
118
119 public byte[] getTotalDifficulty() {
120 if (!parsed) {
121 parse();
122 }
123 return totalDifficulty;
124 }
125
126 public BigInteger getTotalDifficultyAsBigInt() {
127 return new BigInteger(1, getTotalDifficulty());
128 }
129
130 public byte[] getBestHash() {
131 if (!parsed) {
132 parse();
133 }
134 return bestHash;
135 }
136
137 public byte[] getGenesisHash() {
138 if (!parsed) {
139 parse();
140 }
141 return genesisHash;
142 }
143
144 @Override
145 public EthMessageCodes getCommand() {
146 return EthMessageCodes.STATUS;
147 }
148
149
150 @Override
151 public String toString() {
152 if (!parsed) {
153 parse();
154 }
155 return "[" + this.getCommand().name() +
156 " protocolVersion=" + this.protocolVersion +
157 " networkId=" + this.networkId +
158 " totalDifficulty=" + ByteUtil.toHexStringOrEmpty(this.totalDifficulty) +
159 " bestHash=" + ByteUtil.toHexString(this.bestHash) +
160 " genesisHash=" + ByteUtil.toHexString(this.genesisHash) +
161 "]";
162 }
163 }