Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/src/main/java/org/tron/common/entity/NodeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Protocol.NodeInfo transferToProtoEntity() {
peerInfoBuilder.setLastBlockUpdateTime(peerInfo.getLastBlockUpdateTime());
peerInfoBuilder.setSyncFlag(peerInfo.isSyncFlag());
peerInfoBuilder.setHeadBlockTimeWeBothHave(peerInfo.getHeadBlockTimeWeBothHave());
peerInfoBuilder.setNeedSyncFromPeer(peerInfo.isSyncFlag());
peerInfoBuilder.setNeedSyncFromPeer(peerInfo.isNeedSyncFromPeer());
peerInfoBuilder.setNeedSyncFromUs(peerInfo.isNeedSyncFromUs());
peerInfoBuilder.setHost(peerInfo.getHost());
peerInfoBuilder.setPort(peerInfo.getPort());
Expand Down
58 changes: 58 additions & 0 deletions common/src/test/java/org/tron/common/entity/NodeInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.tron.common.entity;

import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.tron.protos.Protocol;

public class NodeInfoTest {

private PeerInfo newPeerInfo(boolean syncFlag, boolean needSyncFromPeer,
boolean needSyncFromUs) {
PeerInfo peerInfo = new PeerInfo();
peerInfo.setSyncFlag(syncFlag);
peerInfo.setNeedSyncFromPeer(needSyncFromPeer);
peerInfo.setNeedSyncFromUs(needSyncFromUs);
// string fields must be non-null, otherwise the protobuf setters throw NPE
peerInfo.setLastSyncBlock("");
peerInfo.setHost("127.0.0.1");
peerInfo.setNodeId("");
peerInfo.setHeadBlockWeBothHave("");
peerInfo.setLocalDisconnectReason("");
peerInfo.setRemoteDisconnectReason("");
return peerInfo;
}

/**
* The protobuf conversion must map each peer flag from its own source field. A previous
* copy-and-paste defect populated needSyncFromPeer from isSyncFlag(); distinct values for
* syncFlag and needSyncFromPeer are required so that such a mismatch is detected.
*/
@Test
public void testPeerFlagMappingIsIndependent() {
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setBlock("");
nodeInfo.setSolidityBlock("");
List<PeerInfo> peerList = new ArrayList<>();
// syncFlag != needSyncFromPeer so the two fields cannot be confused
peerList.add(newPeerInfo(false, true, false));
peerList.add(newPeerInfo(true, false, true));
nodeInfo.setPeerList(peerList);
nodeInfo.setCheatWitnessInfoMap(new java.util.HashMap<>());

Protocol.NodeInfo proto = nodeInfo.transferToProtoEntity();

Assert.assertEquals(2, proto.getPeerInfoListCount());

Protocol.NodeInfo.PeerInfo peer0 = proto.getPeerInfoList(0);
Assert.assertFalse(peer0.getSyncFlag());
Assert.assertTrue(peer0.getNeedSyncFromPeer());
Assert.assertFalse(peer0.getNeedSyncFromUs());

Protocol.NodeInfo.PeerInfo peer1 = proto.getPeerInfoList(1);
Assert.assertTrue(peer1.getSyncFlag());
Assert.assertFalse(peer1.getNeedSyncFromPeer());
Assert.assertTrue(peer1.getNeedSyncFromUs());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Protocol.MetricsInfo.NetInfo getNetProtoInfo() {
// udp
RateInfo udpInTraffic = net.getUdpInTraffic();
Protocol.MetricsInfo.RateInfo udpInTrafficInfo = udpInTraffic.toProtoEntity();
netInfo.setTcpOutTraffic(udpInTrafficInfo);
netInfo.setUdpInTraffic(udpInTrafficInfo);
RateInfo udpOutTraffic = net.getUdpOutTraffic();
Protocol.MetricsInfo.RateInfo udpOutTrafficInfo = udpOutTraffic.toProtoEntity();
netInfo.setUdpOutTraffic(udpOutTrafficInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected void afterInit() {

@Test
public void testProcessMessage() {
MetricsUtil.getMeter(MetricsKey.NET_TCP_IN_TRAFFIC).mark(1000);

@bladehan1 bladehan1 Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] Test isolation backlog

  1. Make the traffic sentinels independent of global meter history. MetricsUtil keeps a static MetricRegistry, while BaseMethodTest resets the Spring context and Args but not that registry. Adding fixed amounts therefore does not guarantee that the four final cumulative counts remain distinct; prior activity in the same test JVM can make one of the assertNotEquals checks fail even when the mapping is correct.

Suggestion: derive marks from captured baseline counts so the resulting values are deterministic and distinct, or swap in and restore a test-scoped registry instead of relying on cumulative global totals.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked how these meters are used in the test suite. Currently, no other tests write non-zero values to these four traffic meters. This test sets four distinct counts immediately after starting the application and reads the results before any actual network traffic can be generated. Therefore, although the MetricRegistry is static, there is no historical data in the current test execution path that could cause these counts to collide, so the fixed sentinel values remain deterministic and distinct. Based on this, I do not think the test needs any additional changes at this time.

MetricsUtil.getMeter(MetricsKey.NET_TCP_OUT_TRAFFIC).mark(2000);
MetricsUtil.getMeter(MetricsKey.NET_UDP_IN_TRAFFIC).mark(4000);
MetricsUtil.getMeter(MetricsKey.NET_UDP_OUT_TRAFFIC).mark(8000);

MetricsInfo m1 = metricsApiService.getMetricsInfo();

Expand Down Expand Up @@ -79,6 +83,20 @@ public void testProcessMessage() {
Assert.assertEquals(m1.getNet().getErrorProtoCount(), m2.getNet().getErrorProtoCount());
Assert
.assertEquals(m1.getNet().getValidConnectionCount(), m2.getNet().getValidConnectionCount());

long tcpIn = m1.getNet().getTcpInTraffic().getCount();
long tcpOut = m1.getNet().getTcpOutTraffic().getCount();
long udpIn = m1.getNet().getUdpInTraffic().getCount();
long udpOut = m1.getNet().getUdpOutTraffic().getCount();

Assert.assertNotEquals(tcpOut, udpIn);
Assert.assertNotEquals(tcpIn, tcpOut);
Assert.assertNotEquals(udpIn, udpOut);

Assert.assertEquals(tcpIn, m2.getNet().getTcpInTraffic().getCount());
Assert.assertEquals(tcpOut, m2.getNet().getTcpOutTraffic().getCount());
Assert.assertEquals(udpIn, m2.getNet().getUdpInTraffic().getCount());
Assert.assertEquals(udpOut, m2.getNet().getUdpOutTraffic().getCount());
}

}
Loading