Skip to content

Commit 50ad05a

Browse files
authored
Merge pull request tronprotocol#4977 from wubin01/net_log
feat(net): optimize network logs
2 parents 8493819 + e1ef127 commit 50ad05a

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

framework/src/main/java/org/tron/core/net/messagehandler/BlockMsgHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,20 @@ private void check(PeerConnection peer, BlockMessage msg) throws P2pException {
104104
Item item = new Item(msg.getBlockId(), InventoryType.BLOCK);
105105
if (!peer.getSyncBlockRequested().containsKey(msg.getBlockId()) && !peer.getAdvInvRequest()
106106
.containsKey(item)) {
107+
logger.error("Receive bad block {} from peer {}, with no request",
108+
msg.getBlockId(), peer.getInetSocketAddress());
107109
throw new P2pException(TypeEnum.BAD_MESSAGE, "no request");
108110
}
109111
BlockCapsule blockCapsule = msg.getBlockCapsule();
110112
if (blockCapsule.getInstance().getSerializedSize() > maxBlockSize) {
113+
logger.error("Receive bad block {} from peer {}, block size over limit",
114+
msg.getBlockId(), peer.getInetSocketAddress());
111115
throw new P2pException(TypeEnum.BAD_MESSAGE, "block size over limit");
112116
}
113117
long gap = blockCapsule.getTimeStamp() - System.currentTimeMillis();
114118
if (gap >= BLOCK_PRODUCED_INTERVAL) {
119+
logger.error("Receive bad block {} from peer {}, block time error",
120+
msg.getBlockId(), peer.getInetSocketAddress());
115121
throw new P2pException(TypeEnum.BAD_MESSAGE, "block time error");
116122
}
117123
}

framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public void statusCheck() {
5353
if (!isDisconnected) {
5454
isDisconnected = peer.getAdvInvRequest().values().stream()
5555
.anyMatch(time -> time < now - NetConstants.ADV_TIME_OUT);
56+
if (isDisconnected) {
57+
logger.warn("Peer {} get avd message timeout", peer.getInetAddress());
58+
}
5659
}
5760

5861
if (!isDisconnected) {
5962
isDisconnected = peer.getSyncBlockRequested().values().stream()
6063
.anyMatch(time -> time < now - NetConstants.SYNC_TIME_OUT);
64+
if (isDisconnected) {
65+
logger.warn("Peer {} get sync message timeout", peer.getInetAddress());
66+
}
6167
}
6268

6369
if (isDisconnected) {

framework/src/main/java/org/tron/core/net/service/keepalive/KeepAliveService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void init() {
3535
long lastSendTime = p.getChannel().getLastSendTime();
3636
if (p.getChannel().waitForPong) {
3737
if (now - pingSent > PING_TIMEOUT) {
38+
logger.warn("Peer {} receive pong timeout", p.getInetSocketAddress());
3839
p.disconnect(Protocol.ReasonCode.TIME_OUT);
3940
}
4041
} else {

framework/src/test/java/org/tron/core/net/messagehandler/BlockMsgHandlerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.collect.ImmutableList;
44
import com.google.protobuf.ByteString;
5+
import java.lang.reflect.Field;
6+
import java.net.InetSocketAddress;
57
import java.util.List;
68
import org.junit.After;
79
import org.junit.Assert;
@@ -18,6 +20,7 @@
1820
import org.tron.core.net.message.adv.BlockMessage;
1921
import org.tron.core.net.peer.Item;
2022
import org.tron.core.net.peer.PeerConnection;
23+
import org.tron.p2p.connection.Channel;
2124
import org.tron.protos.Protocol.Inventory.InventoryType;
2225
import org.tron.protos.Protocol.Transaction;
2326

@@ -31,12 +34,18 @@ public class BlockMsgHandlerTest {
3134
* init context.
3235
*/
3336
@Before
34-
public void init() {
37+
public void init() throws Exception {
3538
Args.setParam(new String[]{"--output-directory", "output-directory", "--debug"},
3639
Constant.TEST_CONF);
3740
context = new TronApplicationContext(DefaultConfig.class);
3841
handler = context.getBean(BlockMsgHandler.class);
3942
peer = context.getBean(PeerConnection.class);
43+
Channel c1 = new Channel();
44+
InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100);
45+
Field field = c1.getClass().getDeclaredField("inetAddress");
46+
field.setAccessible(true);
47+
field.set(c1, a1.getAddress());
48+
peer.setChannel(c1);
4049
}
4150

4251
@Test

0 commit comments

Comments
 (0)