Skip to content

Commit 9d2803e

Browse files
committed
Merge branch 'p2p' of https://github.com/tronprotocol/java-tron into p2p
2 parents 4c7c791 + 846bd2b commit 9d2803e

18 files changed

Lines changed: 255 additions & 667 deletions

‎src/main/java/org/tron/common/overlay/discover/DiscoveryEvent.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,23 @@
1818
package org.tron.common.overlay.discover;
1919

2020

21-
import org.ethereum.net.rlpx.Message;
22-
2321
import java.net.InetSocketAddress;
2422

2523
public class DiscoveryEvent {
26-
private Message message;
24+
25+
private org.ethereum.net.rlpx.DiscoverMessage message;
2726
private InetSocketAddress address;
2827

29-
public DiscoveryEvent(Message m, InetSocketAddress a) {
28+
public DiscoveryEvent(org.ethereum.net.rlpx.DiscoverMessage m, InetSocketAddress a) {
3029
message = m;
3130
address = a;
3231
}
3332

34-
public Message getMessage() {
33+
public org.ethereum.net.rlpx.DiscoverMessage getMessage() {
3534
return message;
3635
}
3736

38-
public void setMessage(Message message) {
37+
public void setMessage(org.ethereum.net.rlpx.DiscoverMessage message) {
3938
this.message = message;
4039
}
4140

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.tron.common.overlay.discover.message;
2+
3+
import org.tron.common.overlay.message.Message;
4+
5+
public abstract class DiscoverMessage extends Message {
6+
7+
public DiscoverMessage() {
8+
}
9+
10+
public DiscoverMessage(byte[] encoded) {
11+
super(encoded);
12+
}
13+
}
Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,51 @@
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-
*/
181
package org.tron.common.overlay.discover.message;
192

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;
266

27-
import static org.ethereum.util.ByteUtil.longToBytesNoLeadZeroes;
7+
public class FindNodeMessage extends DiscoverMessage {
288

29-
public class FindNodeMessage extends Message {
9+
private Message.FindNeighbours findNeighbours;
3010

31-
byte[] target;
32-
long expires;
11+
public FindNodeMessage(byte[] data) {
12+
super(data);
13+
}
3314

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();
4420
}
21+
unpacked = true;
22+
}
4523

24+
private void pack() {
25+
this.data = this.findNeighbours.toByteArray();
26+
}
4627

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();
7032
}
33+
return this.data;
34+
}
7135

72-
public long getExpires() {
73-
return expires;
74-
}
36+
@Override
37+
public String toString() {
7538

76-
@Override
77-
public String toString() {
39+
long currTime = System.currentTimeMillis() / 1000;
7840

79-
long currTime = System.currentTimeMillis() / 1000;
41+
String out = String.format("[FindNodeMessage] \n");
8042

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+
}
8345

84-
return out;
85-
}
46+
@Override
47+
public MessageTypes getType() {
48+
return null;
49+
}
8650

8751
}

‎src/main/java/org/tron/common/overlay/discover/message/Message.java‎

Lines changed: 0 additions & 173 deletions
This file was deleted.

0 commit comments

Comments
 (0)