Skip to content

Commit

Permalink
Merge pull request #4977 from wubin01/net_log
Browse files Browse the repository at this point in the history
feat(net): optimize network logs
  • Loading branch information
xxo1shine authored Feb 10, 2023
2 parents 8493819 + e1ef127 commit 50ad05a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 50ad05a

Please sign in to comment.