From 628678450fddb66f919ffde04e1ed9942b2a6e53 Mon Sep 17 00:00:00 2001 From: wubin01 Date: Thu, 9 Feb 2023 18:21:03 +0800 Subject: [PATCH 1/2] feat(net): optimize network logs --- .../org/tron/core/net/messagehandler/BlockMsgHandler.java | 6 ++++++ .../main/java/org/tron/core/net/peer/PeerStatusCheck.java | 6 ++++++ .../tron/core/net/service/keepalive/KeepAliveService.java | 1 + 3 files changed, 13 insertions(+) diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java index 115e97db50d..6613ebfc581 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java @@ -104,14 +104,20 @@ private void check(PeerConnection peer, BlockMessage msg) throws P2pException { Item item = new Item(msg.getBlockId(), InventoryType.BLOCK); if (!peer.getSyncBlockRequested().containsKey(msg.getBlockId()) && !peer.getAdvInvRequest() .containsKey(item)) { + logger.error("Receive bad block {} from peer {}, with no request", + msg.getBlockId(), peer.getInetSocketAddress()); throw new P2pException(TypeEnum.BAD_MESSAGE, "no request"); } BlockCapsule blockCapsule = msg.getBlockCapsule(); if (blockCapsule.getInstance().getSerializedSize() > maxBlockSize) { + logger.error("Receive bad block {} from peer {}, block size over limit", + msg.getBlockId(), peer.getInetSocketAddress()); throw new P2pException(TypeEnum.BAD_MESSAGE, "block size over limit"); } long gap = blockCapsule.getTimeStamp() - System.currentTimeMillis(); if (gap >= BLOCK_PRODUCED_INTERVAL) { + logger.error("Receive bad block {} from peer {}, block time error", + msg.getBlockId(), peer.getInetSocketAddress()); throw new P2pException(TypeEnum.BAD_MESSAGE, "block time error"); } } diff --git a/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java b/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java index 993d9e94781..84cf59b9bd7 100644 --- a/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java +++ b/framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java @@ -53,11 +53,17 @@ public void statusCheck() { if (!isDisconnected) { isDisconnected = peer.getAdvInvRequest().values().stream() .anyMatch(time -> time < now - NetConstants.ADV_TIME_OUT); + if (isDisconnected) { + logger.warn("Peer {} get avd message timeout", peer.getInetAddress()); + } } if (!isDisconnected) { isDisconnected = peer.getSyncBlockRequested().values().stream() .anyMatch(time -> time < now - NetConstants.SYNC_TIME_OUT); + if (isDisconnected) { + logger.warn("Peer {} get sync message timeout", peer.getInetAddress()); + } } if (isDisconnected) { diff --git a/framework/src/main/java/org/tron/core/net/service/keepalive/KeepAliveService.java b/framework/src/main/java/org/tron/core/net/service/keepalive/KeepAliveService.java index 49ae692b6f1..07ab20c3954 100644 --- a/framework/src/main/java/org/tron/core/net/service/keepalive/KeepAliveService.java +++ b/framework/src/main/java/org/tron/core/net/service/keepalive/KeepAliveService.java @@ -35,6 +35,7 @@ public void init() { long lastSendTime = p.getChannel().getLastSendTime(); if (p.getChannel().waitForPong) { if (now - pingSent > PING_TIMEOUT) { + logger.warn("Peer {} receive pong timeout", p.getInetSocketAddress()); p.disconnect(Protocol.ReasonCode.TIME_OUT); } } else { From e1ef12711f74f1c1aa6fb1e333d836fc99d50591 Mon Sep 17 00:00:00 2001 From: wubin01 Date: Thu, 9 Feb 2023 18:55:23 +0800 Subject: [PATCH 2/2] feat(net): solve the unit test problem --- .../core/net/messagehandler/BlockMsgHandlerTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/BlockMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/BlockMsgHandlerTest.java index 5b2abfd0705..ff6203ee870 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/BlockMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/BlockMsgHandlerTest.java @@ -2,6 +2,8 @@ import com.google.common.collect.ImmutableList; import com.google.protobuf.ByteString; +import java.lang.reflect.Field; +import java.net.InetSocketAddress; import java.util.List; import org.junit.After; import org.junit.Assert; @@ -18,6 +20,7 @@ import org.tron.core.net.message.adv.BlockMessage; import org.tron.core.net.peer.Item; import org.tron.core.net.peer.PeerConnection; +import org.tron.p2p.connection.Channel; import org.tron.protos.Protocol.Inventory.InventoryType; import org.tron.protos.Protocol.Transaction; @@ -31,12 +34,18 @@ public class BlockMsgHandlerTest { * init context. */ @Before - public void init() { + public void init() throws Exception { Args.setParam(new String[]{"--output-directory", "output-directory", "--debug"}, Constant.TEST_CONF); context = new TronApplicationContext(DefaultConfig.class); handler = context.getBean(BlockMsgHandler.class); peer = context.getBean(PeerConnection.class); + Channel c1 = new Channel(); + InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100); + Field field = c1.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c1, a1.getAddress()); + peer.setChannel(c1); } @Test