|
1 | | -/* |
2 | | - * Copyright (c) [2016] [ <ether.camp> ] |
3 | | - * This file is part of the ethereumJ library. |
4 | | - * |
5 | | - * The ethereumJ library 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 | | - * The ethereumJ library 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 the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. |
17 | | - */ |
18 | 1 | package org.tron.common.overlay.discover.message; |
19 | 2 |
|
20 | | -import org.ethereum.crypto.ECKey; |
21 | | -import org.ethereum.util.ByteUtil; |
22 | | -import org.ethereum.util.RLP; |
23 | | -import org.ethereum.util.RLPItem; |
24 | | -import org.ethereum.util.RLPList; |
25 | | -import org.spongycastle.util.encoders.Hex; |
| 3 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 4 | +import org.tron.core.net.message.MessageTypes; |
| 5 | +import org.tron.protos.Message; |
26 | 6 |
|
27 | | -import static org.ethereum.util.ByteUtil.longToBytesNoLeadZeroes; |
| 7 | +public class FindNodeMessage extends DiscoverMessage { |
28 | 8 |
|
29 | | -public class FindNodeMessage extends Message { |
| 9 | + private Message.FindNeighbours findNeighbours; |
30 | 10 |
|
31 | | - byte[] target; |
32 | | - long expires; |
| 11 | + public FindNodeMessage(byte[] data) { |
| 12 | + super(data); |
| 13 | + } |
33 | 14 |
|
34 | | - @Override |
35 | | - public void parse(byte[] data) { |
36 | | - |
37 | | - RLPList list = (RLPList) RLP.decode2OneItem(data, 0); |
38 | | - |
39 | | - RLPItem target = (RLPItem) list.get(0); |
40 | | - RLPItem expires = (RLPItem) list.get(1); |
41 | | - |
42 | | - this.target = target.getRLPData(); |
43 | | - this.expires = ByteUtil.byteArrayToLong(expires.getRLPData()); |
| 15 | + private void unPack() { |
| 16 | + try { |
| 17 | + this.findNeighbours = Message.FindNeighbours.parseFrom(data); |
| 18 | + } catch (InvalidProtocolBufferException e) { |
| 19 | + e.printStackTrace(); |
44 | 20 | } |
| 21 | + unpacked = true; |
| 22 | + } |
45 | 23 |
|
| 24 | + private void pack() { |
| 25 | + this.data = this.findNeighbours.toByteArray(); |
| 26 | + } |
46 | 27 |
|
47 | | - public static FindNodeMessage create(byte[] target, ECKey privKey) { |
48 | | - |
49 | | - long expiration = 90 * 60 + System.currentTimeMillis() / 1000; |
50 | | - |
51 | | - /* RLP Encode data */ |
52 | | - byte[] rlpToken = RLP.encodeElement(target); |
53 | | - |
54 | | - byte[] rlpExp = longToBytesNoLeadZeroes(expiration); |
55 | | - rlpExp = RLP.encodeElement(rlpExp); |
56 | | - |
57 | | - byte[] type = new byte[]{3}; |
58 | | - byte[] data = RLP.encodeList(rlpToken, rlpExp); |
59 | | - |
60 | | - FindNodeMessage findNode = new FindNodeMessage(); |
61 | | - findNode.encode(type, data, privKey); |
62 | | - findNode.target = target; |
63 | | - findNode.expires = expiration; |
64 | | - |
65 | | - return findNode; |
66 | | - } |
67 | | - |
68 | | - public byte[] getTarget() { |
69 | | - return target; |
| 28 | + @Override |
| 29 | + public byte[] getData() { |
| 30 | + if (this.data == null) { |
| 31 | + this.pack(); |
70 | 32 | } |
| 33 | + return this.data; |
| 34 | + } |
71 | 35 |
|
72 | | - public long getExpires() { |
73 | | - return expires; |
74 | | - } |
| 36 | + @Override |
| 37 | + public String toString() { |
75 | 38 |
|
76 | | - @Override |
77 | | - public String toString() { |
| 39 | + long currTime = System.currentTimeMillis() / 1000; |
78 | 40 |
|
79 | | - long currTime = System.currentTimeMillis() / 1000; |
| 41 | + String out = String.format("[FindNodeMessage] \n"); |
80 | 42 |
|
81 | | - String out = String.format("[FindNodeMessage] \n target: %s \n expires in %d seconds \n %s\n", |
82 | | - Hex.toHexString(target), (expires - currTime), super.toString()); |
| 43 | + return out; |
| 44 | + } |
83 | 45 |
|
84 | | - return out; |
85 | | - } |
| 46 | + @Override |
| 47 | + public MessageTypes getType() { |
| 48 | + return null; |
| 49 | + } |
86 | 50 |
|
87 | 51 | } |
0 commit comments