Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge main to develop #81

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repositories {
Then add the required packages as dependencies. Please add dependencies locally.
```bash
dependencies {
implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '2.1.0'
implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.1'
}
```
Or if you are using the jar files as your dependencies:
Expand All @@ -66,7 +66,7 @@ dependencies {
<dependency>
<groupId>io.github.tronprotocol</groupId>
<artifactId>libp2p</artifactId>
<version>2.1.0</version>
<version>2.2.1</version>
</dependency>
```

Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'io.github.tronprotocol'
version '2.1.0'
version '2.2.1'

buildscript {
repositories {
Expand Down Expand Up @@ -56,7 +56,7 @@ buildscript {
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'

implementation group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.8.4'
implementation group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.10.5'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.9'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.9'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: protobufVersion
Expand Down Expand Up @@ -86,7 +86,6 @@ dependencies {
exclude group: 'io.netty', module: 'netty-transport-native-unix-common'
})
implementation group: 'com.aliyun', name: 'alidns20150109', version: '3.0.1'
implementation group: 'xerces', name: 'xercesImpl', version: '2.11.0'
}

tasks.matching { it instanceof Test }.all {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/tron/p2p/P2pConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class P2pConfig {
private List<InetAddress> trustNodes = new CopyOnWriteArrayList<>();
private byte[] nodeID = NetUtil.getNodeId();
private String ip = NetUtil.getExternalIpV4();
private String lanIp = NetUtil.getLanIP();
private String ipv6 = NetUtil.getExternalIpV6();
private int port = 18888;
private int networkId = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class HandshakeService implements MessageProcess {
private final int networkId = Parameter.p2pConfig.getNetworkId();

public void startHandshake(Channel channel) {
sendHelloMsg(channel, DisconnectCode.NORMAL);
sendHelloMsg(channel, DisconnectCode.NORMAL, channel.getStartTime());
}

@Override
Expand All @@ -37,7 +37,9 @@ public void processMessage(Channel channel, Message message) {

DisconnectCode code = ChannelManager.processPeer(channel);
if (code != DisconnectCode.NORMAL) {
sendHelloMsg(channel, code);
if (!channel.isActive()) {
sendHelloMsg(channel, code, msg.getTimestamp());
}
logDisconnectReason(channel, getDisconnectReason(code));
channel.close();
return;
Expand Down Expand Up @@ -68,20 +70,20 @@ public void processMessage(Channel channel, Message message) {
if (msg.getNetworkId() != networkId) {
log.info("Peer {} different network id, peer->{}, me->{}",
channel.getInetSocketAddress(), msg.getNetworkId(), networkId);
sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION);
sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION, msg.getTimestamp());
logDisconnectReason(channel, DisconnectReason.DIFFERENT_VERSION);
channel.close();
return;
}
sendHelloMsg(channel, DisconnectCode.NORMAL);
sendHelloMsg(channel, DisconnectCode.NORMAL, msg.getTimestamp());
}
channel.setFinishHandshake(true);
channel.updateAvgLatency(System.currentTimeMillis() - channel.getStartTime());
Parameter.handlerList.forEach(h -> h.onConnect(channel));
}

private void sendHelloMsg(Channel channel, DisconnectCode code) {
HelloMessage helloMessage = new HelloMessage(code);
private void sendHelloMsg(Channel channel, DisconnectCode code, long time) {
HelloMessage helloMessage = new HelloMessage(code, time);
channel.send(helloMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public HelloMessage(byte[] data) throws Exception {
this.helloMessage = Connect.HelloMessage.parseFrom(data);
}

public HelloMessage(DisconnectCode code) {
public HelloMessage(DisconnectCode code, long time) {
super(MessageType.HANDSHAKE_HELLO, null);
Discover.Endpoint endpoint = Parameter.getHomeNode();
this.helloMessage = Connect.HelloMessage.newBuilder()
.setFrom(endpoint)
.setNetworkId(Parameter.p2pConfig.getNetworkId())
.setCode(code.getValue())
.setVersion(Parameter.version)
.setTimestamp(System.currentTimeMillis()).build();
.setTimestamp(time).build();
this.data = helloMessage.toByteArray();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.p2p.discover.socket;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -37,6 +38,10 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object
log.info("Parse msg failed, type {}, len {}, address {}", encoded[0], encoded.length,
packet.sender());
}
} catch (InvalidProtocolBufferException e) {
log.warn("An exception occurred while parsing the message, type {}, len {}, address {}, "
+ "data {}, cause: {}", encoded[0], encoded.length, packet.sender(),
ByteArray.toHexString(encoded), e.getMessage());
} catch (Exception e) {
log.error("An exception occurred while parsing the message, type {}, len {}, address {}, "
+ "data {}", encoded[0], encoded.length, packet.sender(),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/tron/p2p/utils/NetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -237,4 +238,14 @@ private static String getIp(List<String> multiSrcUrls) {
return result;
}

public static String getLanIP() {
String lanIP;
try (Socket s = new Socket("www.baidu.com", 80)) {
lanIP = s.getLocalAddress().getHostAddress();
} catch (IOException e) {
log.warn("Can't get lan IP. Fall back to 127.0.0.1: " + e);
lanIP = "127.0.0.1";
}
return lanIP;
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/tron/p2p/connection/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testPong() {

@Test
public void testHandShakeHello() {
HelloMessage helloMessage = new HelloMessage(DisconnectCode.NORMAL);
HelloMessage helloMessage = new HelloMessage(DisconnectCode.NORMAL, 0);
byte[] messageData = helloMessage.getSendData();
try {
Message message = Message.parse(messageData);
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/tron/p2p/connection/SocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.p2p.P2pConfig;
import org.tron.p2p.base.Parameter;
import org.tron.p2p.connection.message.Message;
import org.tron.p2p.connection.message.keepalive.PingMessage;
import org.tron.p2p.discover.Node;
import org.tron.p2p.discover.NodeManager;

public class SocketTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package org.tron.p2p.connection.message.handshake;

import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import static org.tron.p2p.base.Parameter.p2pConfig;

import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.tron.p2p.P2pConfig;
import org.tron.p2p.connection.business.handshake.DisconnectCode;
import org.tron.p2p.connection.message.MessageType;

import static org.tron.p2p.base.Parameter.p2pConfig;

public class HelloMessageTest {

@Test
public void testHelloMessage() throws Exception {
p2pConfig = new P2pConfig();
HelloMessage m1 = new HelloMessage(DisconnectCode.NORMAL);
HelloMessage m1 = new HelloMessage(DisconnectCode.NORMAL, 0);
Assert.assertEquals(0, m1.getCode());
Assert.assertTrue(ByteUtils.equals(p2pConfig.getNodeID(), m1.getFrom().getId()));

Assert.assertTrue(Arrays.equals(p2pConfig.getNodeID(), m1.getFrom().getId()));
Assert.assertEquals(p2pConfig.getPort(), m1.getFrom().getPort());
Assert.assertEquals(p2pConfig.getIp(), m1.getFrom().getHostV4());
Assert.assertEquals(p2pConfig.getNetworkId(), m1.getNetworkId());
Assert.assertEquals(MessageType.HANDSHAKE_HELLO, m1.getType());

HelloMessage m2 = new HelloMessage(m1.getData());
Assert.assertTrue(ByteUtils.equals(p2pConfig.getNodeID(), m2.getFrom().getId()));
Assert.assertTrue(Arrays.equals(p2pConfig.getNodeID(), m2.getFrom().getId()));
Assert.assertEquals(p2pConfig.getPort(), m2.getFrom().getPort());
Assert.assertEquals(p2pConfig.getIp(), m2.getFrom().getHostV4());
Assert.assertEquals(p2pConfig.getNetworkId(), m2.getNetworkId());
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/org/tron/p2p/discover/NodeTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.tron.p2p.discover;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/tron/p2p/utils/NetUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public void testGetIP() {
Assert.assertEquals(ip3, ip4);
}

@Test
public void testGetLanIP() {
String lanIpv4 = NetUtil.getLanIP();
Assert.assertNotNull(lanIpv4);
}

@Test
public void testIPv6Format() {
String std = "fe80:0:0:0:204:61ff:fe9d:f156";
Expand Down