Coverage Summary for Class: MessageType (co.rsk.net.messages)
Class |
Method, %
|
Line, %
|
MessageType |
0%
(0/5)
|
0%
(0/25)
|
MessageType$1 |
0%
(0/2)
|
0%
(0/10)
|
MessageType$10 |
0%
(0/2)
|
0%
(0/6)
|
MessageType$11 |
0%
(0/2)
|
0%
(0/7)
|
MessageType$12 |
0%
(0/2)
|
0%
(0/11)
|
MessageType$13 |
0%
(0/2)
|
0%
(0/6)
|
MessageType$14 |
0%
(0/2)
|
0%
(0/18)
|
MessageType$15 |
0%
(0/2)
|
0%
(0/7)
|
MessageType$16 |
0%
(0/2)
|
0%
(0/3)
|
MessageType$2 |
0%
(0/2)
|
0%
(0/2)
|
MessageType$3 |
0%
(0/2)
|
0%
(0/2)
|
MessageType$4 |
0%
(0/2)
|
0%
(0/2)
|
MessageType$5 |
0%
(0/2)
|
0%
(0/10)
|
MessageType$6 |
0%
(0/2)
|
0%
(0/7)
|
MessageType$7 |
0%
(0/2)
|
0%
(0/6)
|
MessageType$8 |
0%
(0/2)
|
0%
(0/8)
|
MessageType$9 |
0%
(0/2)
|
0%
(0/11)
|
Total |
0%
(0/37)
|
0%
(0/141)
|
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.messages;
20
21 import co.rsk.core.BlockDifficulty;
22 import co.rsk.net.Status;
23 import co.rsk.remasc.RemascTransaction;
24 import org.ethereum.core.*;
25 import org.ethereum.util.RLP;
26 import org.ethereum.util.RLPElement;
27 import org.ethereum.util.RLPList;
28 import org.bouncycastle.util.BigIntegers;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import static org.ethereum.util.ByteUtil.byteArrayToInt;
34
35 /**
36 * Created by mario on 16/02/17.
37 */
38 public enum MessageType {
39
40 STATUS_MESSAGE(1) {
41 @Override
42 public Message createMessage(BlockFactory blockFactory, RLPList list) {
43 byte[] rlpdata = list.get(0).getRLPData();
44 long number = rlpdata == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpdata).longValue();
45 byte[] hash = list.get(1).getRLPData();
46
47 if (list.size() == 2) {
48 return new StatusMessage(new Status(number, hash));
49 }
50
51 byte[] parentHash = list.get(2).getRLPData();
52 byte[] rlpTotalDifficulty = list.get(3).getRLPData();
53 BlockDifficulty totalDifficulty = rlpTotalDifficulty == null ? BlockDifficulty.ZERO : RLP.parseBlockDifficulty(rlpTotalDifficulty);
54
55 return new StatusMessage(new Status(number, hash, parentHash, totalDifficulty));
56 }
57 },
58 BLOCK_MESSAGE(2) {
59 @Override
60 public Message createMessage(BlockFactory blockFactory, RLPList list) {
61 return new BlockMessage(blockFactory.decodeBlock(list.get(0).getRLPData()));
62 }
63 },
64 GET_BLOCK_MESSAGE(3) {
65 @Override
66 public Message createMessage(BlockFactory blockFactory, RLPList list) {
67 return new GetBlockMessage(list.get(0).getRLPData());
68 }
69 },
70 NEW_BLOCK_HASHES(6) {
71 @Override
72 public Message createMessage(BlockFactory blockFactory, RLPList list) {
73 return new NewBlockHashesMessage(list.getRLPData());
74 }
75 },
76 TRANSACTIONS(7) {
77 @Override
78 public Message createMessage(BlockFactory blockFactory, RLPList list) {
79 List<Transaction> txs = new ArrayList<>();
80
81 for (int k = 0; k < list.size(); k++) {
82 RLPElement element = list.get(k);
83 byte[] data = element.getRLPData();
84
85 if (!MessageType.validTransactionLength(data)) {
86 continue;
87 }
88
89 ImmutableTransaction tx = new ImmutableTransaction(data);
90
91 txs.add(tx);
92 }
93
94 return new TransactionsMessage(txs);
95 }
96 },
97 BLOCK_HASH_REQUEST_MESSAGE(8) {
98 @Override
99 public Message createMessage(BlockFactory blockFactory, RLPList list) {
100 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
101 byte[] rlpId = list.get(0).getRLPData();
102 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
103 byte[] rlpHeight = message.get(0).getRLPData();
104 long height = rlpHeight == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpHeight).longValue();
105
106 return new BlockHashRequestMessage(id, height);
107 }
108 },
109 BLOCK_HASH_RESPONSE_MESSAGE(18) {
110 @Override
111 public Message createMessage(BlockFactory blockFactory, RLPList list) {
112 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
113 byte[] rlpId = list.get(0).getRLPData();
114 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
115 byte[] hash = message.get(0).getRLPData();
116
117 return new BlockHashResponseMessage(id, hash);
118 }
119 },
120 BLOCK_HEADERS_REQUEST_MESSAGE(9) {
121 @Override
122 public Message createMessage(BlockFactory blockFactory, RLPList list){
123 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
124 byte[] rlpId = list.get(0).getRLPData();
125 byte[] hash = message.get(0).getRLPData();
126 byte[] rlpCount = message.get(1).getRLPData();
127
128 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
129 int count = byteArrayToInt(rlpCount);
130
131 return new BlockHeadersRequestMessage(id, hash, count);
132 }
133 },
134 BLOCK_HEADERS_RESPONSE_MESSAGE(10) {
135 @Override
136 public Message createMessage(BlockFactory blockFactory, RLPList list) {
137 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
138 byte[] rlpId = list.get(0).getRLPData();
139 RLPList rlpHeaders = (RLPList)RLP.decode2(message.get(0).getRLPData()).get(0);
140 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
141
142 List<BlockHeader> headers = new ArrayList<>();
143
144 for (int k = 0; k < rlpHeaders.size(); k++) {
145 RLPElement element = rlpHeaders.get(k);
146 BlockHeader header = blockFactory.decodeHeader(element.getRLPData());
147 headers.add(header);
148 }
149
150 return new BlockHeadersResponseMessage(id, headers);
151 }
152 },
153 BLOCK_REQUEST_MESSAGE(11) {
154 @Override
155 public Message createMessage(BlockFactory blockFactory, RLPList list) {
156 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
157 byte[] rlpId = list.get(0).getRLPData();
158 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
159 byte[] hash = message.get(0).getRLPData();
160 return new BlockRequestMessage(id, hash);
161 }
162 },
163 BLOCK_RESPONSE_MESSAGE(12) {
164 @Override
165 public Message createMessage(BlockFactory blockFactory, RLPList list) {
166 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
167 byte[] rlpId = list.get(0).getRLPData();
168 byte[] rlpBlock = message.get(0).getRLPData();
169
170 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
171 Block block = blockFactory.decodeBlock(rlpBlock);
172
173 return new BlockResponseMessage(id, block);
174 }
175 },
176 SKELETON_RESPONSE_MESSAGE(13) {
177 @Override
178 public Message createMessage(BlockFactory blockFactory, RLPList list) {
179 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
180 byte[] rlpId = list.get(0).getRLPData();
181 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
182
183 RLPList paramsList = (RLPList)RLP.decode2(message.get(0).getRLPData()).get(0);
184
185 List<BlockIdentifier> blockIdentifiers = new ArrayList<>();
186
187 for (int k = 0; k < paramsList.size(); k++) {
188 RLPElement param = paramsList.get(k);
189 BlockIdentifier blockIdentifier = new BlockIdentifier((RLPList)param);
190 blockIdentifiers.add(blockIdentifier);
191 }
192
193 return new SkeletonResponseMessage(id, blockIdentifiers);
194 }
195 },
196 BODY_REQUEST_MESSAGE(14) {
197 @Override
198 public Message createMessage(BlockFactory blockFactory, RLPList list) {
199 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
200 byte[] rlpId = list.get(0).getRLPData();
201 byte[] hash = message.get(0).getRLPData();
202
203 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
204 return new BodyRequestMessage(id, hash);
205 }
206 },
207 BODY_RESPONSE_MESSAGE(15) {
208 @Override
209 public Message createMessage(BlockFactory blockFactory, RLPList list) {
210 RLPList message = (RLPList) RLP.decode2(list.get(1).getRLPData()).get(0);
211 byte[] rlpId = list.get(0).getRLPData();
212 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
213 RLPList rlpTransactions = (RLPList) RLP.decode2(message.get(0).getRLPData()).get(0);
214 RLPList rlpUncles = (RLPList) RLP.decode2(message.get(1).getRLPData()).get(0);
215
216 List<Transaction> transactions = new ArrayList<>();
217 for (int k = 0; k < rlpTransactions.size(); k++) {
218 byte[] txdata = rlpTransactions.get(k).getRLPData();
219 Transaction tx = new ImmutableTransaction(txdata);
220
221 if (tx.isRemascTransaction(k, rlpTransactions.size())) {
222 tx = new RemascTransaction(txdata);
223 }
224
225 transactions.add(tx);
226 }
227
228 List<BlockHeader> uncles = new ArrayList<>();
229
230 for (int j = 0; j < rlpUncles.size(); j++) {
231 RLPElement element = rlpUncles.get(j);
232 uncles.add(blockFactory.decodeHeader(element.getRLPData()));
233 }
234
235 return new BodyResponseMessage(id, transactions, uncles);
236 }
237 },
238 SKELETON_REQUEST_MESSAGE(16) {
239 @Override
240 public Message createMessage(BlockFactory blockFactory, RLPList list) {
241 RLPList message = (RLPList)RLP.decode2(list.get(1).getRLPData()).get(0);
242 byte[] rlpId = list.get(0).getRLPData();
243 long id = rlpId == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpId).longValue();
244 byte[] rlpStartNumber = message.get(0).getRLPData();
245 long startNumber = rlpStartNumber == null ? 0 : BigIntegers.fromUnsignedByteArray(rlpStartNumber).longValue();
246 return new SkeletonRequestMessage(id, startNumber);
247 }
248 },
249 NEW_BLOCK_HASH_MESSAGE(17) {
250 @Override
251 public Message createMessage(BlockFactory blockFactory, RLPList list) {
252 byte[] hash = list.get(0).getRLPData();
253 return new NewBlockHashMessage(hash);
254 }
255 };
256
257 private int type;
258
259 MessageType(int type) {
260 this.type = type;
261 }
262
263 public abstract Message createMessage(BlockFactory blockFactory, RLPList list);
264
265 public byte getTypeAsByte() {
266 return (byte) this.type;
267 }
268
269 public static MessageType valueOfType(int type) {
270 for(MessageType mt : MessageType.values()) {
271 if(mt.type == type) {
272 return mt;
273 }
274 }
275 throw new IllegalArgumentException(String.format("Invalid Message Type: %d", type));
276 }
277
278 private static boolean validTransactionLength(byte[] data) {
279 return data.length <= 1 << 19; /* 512KB */
280 }
281 }