Skip to content

Commit

Permalink
merge Release v2.2.0 to main (#77)
Browse files Browse the repository at this point in the history
* update dependency version

* set log level to warn when InvalidProtocolBufferException happens (#75)

* add column lanIp for P2pConfig

* update version to 2.2.0

---------

Co-authored-by: halibobo1205 <halibobo1205@gmail.com>
Co-authored-by: halibobo1205 <82020050+halibobo1205@users.noreply.github.com>
Co-authored-by: wubin01 <wb_bupt@163.com>
Co-authored-by: chengtx01 <allen_cheng1@163.com>
Co-authored-by: allen <56535423+jwrct@users.noreply.github.com>
  • Loading branch information
6 people committed Jan 23, 2024
1 parent b58571a commit c05b995
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
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.0'
}
```
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.0</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.0'

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
@@ -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;
}
}
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);
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

0 comments on commit c05b995

Please sign in to comment.