Skip to content

Commit

Permalink
Merge pull request #54 from tronprotocol/release_v1.2.0
Browse files Browse the repository at this point in the history
merge release v1.2.0 into develop
  • Loading branch information
317787106 authored Jul 11, 2023
2 parents 61de534 + 4881240 commit f6fd9f2
Show file tree
Hide file tree
Showing 35 changed files with 546 additions and 238 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
libp2p is a p2p network SDK implemented in java language. The functional modules that have been implemented so far include
* node discovery by p2p, include ipv4 and ipv6,
* tcp connection management,
* publish nodes on dns domain and node discovery by dns.
* publish nodes on dns domain and node discovery by dns,
* support compressed message transmission among nodes

# Build
Building libp2p requires `git` and `Oracle JDK 1.8` to be installed, other JDK versions are not supported yet. Make sure you operate on `Linux` and `MacOS` operating systems.
Expand Down Expand Up @@ -42,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: '0.2.0'
implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '1.2.0'
}
```
Or if you are using the jar files as your dependencies:
Expand All @@ -65,7 +66,7 @@ dependencies {
<dependency>
<groupId>io.github.tronprotocol</groupId>
<artifactId>libp2p</artifactId>
<version>0.2.0</version>
<version>1.2.0</version>
</dependency>
```

Expand Down
89 changes: 18 additions & 71 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'io.github.tronprotocol'
version '0.2.0'
version '1.2.0'

buildscript {
repositories {
Expand All @@ -16,12 +16,8 @@ apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: "io.github.gradle-nexus.publish-plugin"

def protobufVersion = "3.19.2"

def grpcVersion = "1.14.0"

mainClassName = 'org.tron.p2p.example.StartApp'
Expand Down Expand Up @@ -62,22 +58,33 @@ dependencies {

implementation group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.8.4'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.9'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.9'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: protobufVersion
implementation group: 'com.google.protobuf', name: 'protobuf-java-util', version: protobufVersion
implementation group: 'io.grpc', name: 'grpc-netty', version: grpcVersion
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
implementation group: 'commons-cli', name: 'commons-cli', version: '1.5.0'

compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
testImplementation group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
testImplementation group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.12'

implementation group: 'dnsjava', name: 'dnsjava', version: '3.5.2'
implementation group: 'org.web3j', name: 'crypto', version: '5.0.0'
implementation group: 'software.amazon.awssdk', name: 'route53', version: '2.18.41'
implementation('software.amazon.awssdk:route53:2.18.41', {
exclude group: 'io.netty', module: 'netty-codec-http2'
exclude group: 'io.netty', module: 'netty-codec-http'
exclude group: 'io.netty', module: 'netty-common'
exclude group: 'io.netty', module: 'netty-buffer'
exclude group: 'io.netty', module: 'netty-transport'
exclude group: 'io.netty', module: 'netty-codec'
exclude group: 'io.netty', module: 'netty-handler'
exclude group: 'io.netty', module: 'netty-resolver'
exclude group: 'io.netty', module: 'netty-transport-classes-epoll'
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'
}
Expand Down Expand Up @@ -165,64 +172,4 @@ artifacts {
archives javadocJar, sourcesJar
}

publishing {

publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
name = 'libp2p'
description = 'libp2p is a p2p network SDK implemented in java language.'
url = 'https://github.com/tronprotocol/libp2p'
licenses {
license {
name = 'The Apache Software License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
name = 'chengtx01'
email = 'ctxhorse@gmail.com'
}
developer {
name = 'jiangyuanshu'
email = '317787106@qq.com'
}
developer {
name = 'wubin01'
email = 'wb_bupt@163.com'
}
}
scm {
url = 'https://github.com/tronprotocol/libp2p'
connection = 'scm:git:git://github.com/tronprotocol/libp2p.git'
developerConnection = 'scm:git:ssh://git@github.com:tronprotocol/libp2p.git'
}
}
}
}
}

nexusPublishing {
repositories {
sonatype {
//only for users registered in Sonatype after 24 Feb 2021
nexusUrl = uri("https://s01.oss.sonatype.org/service/local/")
snapshotRepositoryUrl = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
username = "${sonatypeUsername}"
password = "${sonatypePassword}"
}
}
}


signing {
// https://discuss.gradle.org/t/use-of-signing-plugin-with-gpg-agent-fails-with-gradle-5-x/38351/2
useGpgCmd()
sign publishing.publications.mavenJava
}


7 changes: 7 additions & 0 deletions src/main/java/org/tron/p2p/P2pService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tron.p2p;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -40,10 +42,15 @@ public void register(P2pEventHandler p2PEventHandler) throws P2pException {
Parameter.addP2pEventHandle(p2PEventHandler);
}

@Deprecated
public void connect(InetSocketAddress address) {
ChannelManager.connect(address);
}

public ChannelFuture connect(Node node, ChannelFutureListener future) {
return ChannelManager.connect(node, future);
}

public P2pStats getP2pStats() {
return statsManager.getP2pStats();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/tron/p2p/connection/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler;
Expand Down Expand Up @@ -96,7 +97,8 @@ public void processException(Throwable throwable) {
}
SocketAddress address = ctx.channel().remoteAddress();
if (throwable instanceof ReadTimeoutException
|| throwable instanceof IOException) {
|| throwable instanceof IOException
|| throwable instanceof CorruptedFrameException) {
log.warn("Close peer {}, reason: {}", address, throwable.getMessage());
} else if (baseThrowable instanceof P2pException) {
log.warn("Close peer {}, type: ({}), info: {}",
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/org/tron/p2p/connection/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
Expand All @@ -22,7 +24,9 @@
import org.tron.p2p.connection.message.Message;
import org.tron.p2p.connection.socket.PeerClient;
import org.tron.p2p.connection.socket.PeerServer;
import org.tron.p2p.discover.Node;
import org.tron.p2p.exception.P2pException;
import org.tron.p2p.exception.P2pException.TypeEnum;
import org.tron.p2p.utils.ByteArray;
import org.tron.p2p.utils.NetUtil;

Expand Down Expand Up @@ -50,9 +54,12 @@ public class ChannelManager {

@Getter
private static final Cache<InetAddress, Long> bannedNodes = CacheBuilder
.newBuilder().maximumSize(2000).build(); //ban timestamp
.newBuilder().maximumSize(2000).build(); //ban timestamp

private static boolean isInit = false;

public static void init() {
isInit = true;
peerServer = new PeerServer();
peerClient = new PeerClient();
keepAliveService = new KeepAliveService();
Expand All @@ -68,7 +75,11 @@ public static void init() {

public static void connect(InetSocketAddress address) {
peerClient.connect(address.getAddress().getHostAddress(), address.getPort(),
ByteArray.toHexString(NetUtil.getNodeId()));
ByteArray.toHexString(NetUtil.getNodeId()));
}

public static ChannelFuture connect(Node node, ChannelFutureListener future) {
return peerClient.connect(node, future);
}

public static void notifyDisconnect(Channel channel) {
Expand Down Expand Up @@ -99,7 +110,7 @@ public static synchronized DisconnectCode processPeer(Channel channel) {
if (!channel.isActive() && !channel.isTrustPeer()) {
InetAddress inetAddress = channel.getInetAddress();
if (bannedNodes.getIfPresent(inetAddress) != null
&& bannedNodes.getIfPresent(inetAddress) > System.currentTimeMillis()) {
&& bannedNodes.getIfPresent(inetAddress) > System.currentTimeMillis()) {
log.info("Peer {} recently disconnected", channel);
return DisconnectCode.TIME_BANNED;
}
Expand Down Expand Up @@ -138,12 +149,15 @@ public static synchronized DisconnectCode processPeer(Channel channel) {
public static void banNode(InetAddress inetAddress, Long banTime) {
long now = System.currentTimeMillis();
if (bannedNodes.getIfPresent(inetAddress) == null
|| bannedNodes.getIfPresent(inetAddress) < now) {
|| bannedNodes.getIfPresent(inetAddress) < now) {
bannedNodes.put(inetAddress, now + banTime);
}
}

public static void close() {
if (!isInit) {
return;
}
connPoolService.close();
keepAliveService.close();
peerServer.close();
Expand All @@ -152,6 +166,9 @@ public static void close() {
}

public static void processMessage(Channel channel, byte[] data) throws P2pException {
if (data == null || data.length == 0) {
throw new P2pException(TypeEnum.EMPTY_MESSAGE, "");
}
if (data[0] >= 0) {
handMessage(channel, data);
return;
Expand Down Expand Up @@ -182,9 +199,17 @@ private static void handMessage(Channel channel, byte[] data) throws P2pExceptio
if (handler == null) {
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type:" + data[0]);
}
if (channel.isDiscoveryMode()) {
channel.getCtx().close();
return;
}

if (!channel.isFinishHandshake()) {
channel.setFinishHandshake(true);
if (!DisconnectCode.NORMAL.equals(processPeer(channel))) {
channel.getCtx().close();
return;
}
Parameter.handlerList.forEach(h -> h.onConnect(channel));
}

Expand Down Expand Up @@ -218,4 +243,8 @@ public static synchronized void updateNodeId(Channel channel, String nodeId) {
c2.close();
}
}

public static void triggerConnect(InetSocketAddress address) {
connPoolService.triggerConnect(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NodeDetectService implements MessageProcess {

@Getter
private static final Cache<InetAddress, Long> badNodesCache = CacheBuilder
.newBuilder().maximumSize(2000).build();
.newBuilder().maximumSize(5000).expireAfterWrite(1, TimeUnit.HOURS).build();

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

Expand Down Expand Up @@ -77,7 +77,7 @@ public void work() {
return;
}

NodeStat nodeStat = getSortedNodeStats().get(0);
NodeStat nodeStat = nodeStats.get(0);
if (nodeStat.getLastDetectTime() > System.currentTimeMillis() - NODE_DETECT_MIN_THRESHOLD) {
return;
}
Expand Down
Loading

0 comments on commit f6fd9f2

Please sign in to comment.