From 57917677e6bf4e8fd7125d75b78d670d037cc8ba Mon Sep 17 00:00:00 2001 From: wubin01 Date: Wed, 24 Jan 2024 12:54:56 +0800 Subject: [PATCH 1/2] optimize the duplicate peer scenario logic in handshake --- .../business/handshake/HandshakeService.java | 14 ++++++++------ .../connection/message/handshake/HelloMessage.java | 4 ++-- .../java/org/tron/p2p/connection/MessageTest.java | 2 +- .../message/handshake/HelloMessageTest.java | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java b/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java index de9600b..760849a 100644 --- a/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java +++ b/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java @@ -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 @@ -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; @@ -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); } diff --git a/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java b/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java index 43b2680..fc42116 100644 --- a/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java +++ b/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java @@ -18,7 +18,7 @@ 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() @@ -26,7 +26,7 @@ public HelloMessage(DisconnectCode code) { .setNetworkId(Parameter.p2pConfig.getNetworkId()) .setCode(code.getValue()) .setVersion(Parameter.version) - .setTimestamp(System.currentTimeMillis()).build(); + .setTimestamp(time).build(); this.data = helloMessage.toByteArray(); } diff --git a/src/test/java/org/tron/p2p/connection/MessageTest.java b/src/test/java/org/tron/p2p/connection/MessageTest.java index 3c48699..17ca009 100644 --- a/src/test/java/org/tron/p2p/connection/MessageTest.java +++ b/src/test/java/org/tron/p2p/connection/MessageTest.java @@ -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); diff --git a/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java b/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java index ad3616d..41188f5 100644 --- a/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java +++ b/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java @@ -14,7 +14,7 @@ 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(Arrays.equals(p2pConfig.getNodeID(), m1.getFrom().getId())); From aa306b7d71b319255543fa7ef03325d42f499887 Mon Sep 17 00:00:00 2001 From: wubin01 Date: Wed, 24 Jan 2024 13:59:01 +0800 Subject: [PATCH 2/2] modify p2p version to 2.2.1 --- README.md | 4 ++-- build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 001a8e2..76b750d 100644 --- a/README.md +++ b/README.md @@ -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.2.0' + implementation group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.1' } ``` Or if you are using the jar files as your dependencies: @@ -66,7 +66,7 @@ dependencies { io.github.tronprotocol libp2p - 2.2.0 + 2.2.1 ``` diff --git a/build.gradle b/build.gradle index ca999fa..c0dd315 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'io.github.tronprotocol' -version '2.2.0' +version '2.2.1' buildscript { repositories {