Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void handleEvent(UdpEvent udpEvent) {
logger.warn("Receive keep alive message from {} is not my member", sender.getHostString());
return;
}

logger.info("Receive keep alive message from {}", sender);

Choose a reason for hiding this comment

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

Maybe it should be sender.getHostString() to keep consistency with lines 121 and 126?

Copy link
Contributor Author

@317787106 317787106 Nov 27, 2025

Choose a reason for hiding this comment

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

Actually, it's more accurate to logging InetSocketAddress, the style is /IP:port. Line 126 only needs IP

lastKeepAliveTime = System.currentTimeMillis();

KeepAliveMessage keepAliveMessage = (KeepAliveMessage) msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public void statusCheck() {

long now = System.currentTimeMillis();

if (tronNetDelegate == null) {
//only occurs in mock test. TODO fix test
return;
}
tronNetDelegate.getActivePeer().forEach(peer -> {

boolean isDisconnected = false;
Expand Down
8 changes: 8 additions & 0 deletions framework/src/test/java/org/tron/common/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.core.exception.BalanceInsufficientException;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.net.peer.PeerManager;
import org.tron.core.store.AccountStore;
import org.tron.protos.Protocol;

Expand Down Expand Up @@ -68,6 +70,12 @@ public static void destroy() {
Args.clearParam();
}

public void closePeer() {
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
}

public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] privateKey) {
long blockTime = System.currentTimeMillis() / 3000 * 3000;
if (time != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.tron.common.BaseTest;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Constant;
Expand All @@ -20,12 +22,15 @@
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Inventory.InventoryType;

public class P2pEventHandlerImplTest {
public class P2pEventHandlerImplTest extends BaseTest {

@BeforeClass
public static void init() throws Exception {
Args.setParam(new String[] {"--output-directory", dbPath(), "--debug"}, Constant.TEST_CONF);
}

@Test
public void testProcessInventoryMessage() throws Exception {
String[] a = new String[0];
Args.setParam(a, Constant.TESTNET_CONF);
CommonParameter parameter = CommonParameter.getInstance();
parameter.setMaxTps(10);

Expand Down Expand Up @@ -114,9 +119,6 @@ public void testProcessInventoryMessage() throws Exception {

@Test
public void testUpdateLastInteractiveTime() throws Exception {
String[] a = new String[0];
Args.setParam(a, Constant.TESTNET_CONF);

PeerConnection peer = new PeerConnection();
P2pEventHandlerImpl p2pEventHandler = new P2pEventHandlerImpl();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import static org.mockito.Mockito.mock;

import com.google.protobuf.ByteString;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -63,14 +60,10 @@ public static void destroy() {
context.destroy();
}

@Before
@After
public void clearPeers() {
try {
Field field = PeerManager.class.getDeclaredField("peers");
field.setAccessible(true);
field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>()));
} catch (NoSuchFieldException | IllegalAccessException e) {
//ignore
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

import com.google.protobuf.ByteString;
import java.io.File;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import org.bouncycastle.util.encoders.Hex;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -65,14 +62,10 @@ public static void destroy() {
FileUtil.deleteDir(new File(dbPath));
}

@Before
@After
public void clearPeers() {
try {
Field field = PeerManager.class.getDeclaredField("peers");
field.setAccessible(true);
field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>()));
} catch (NoSuchFieldException | IllegalAccessException e) {
//ignore
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.tron.core.net.message.sync.BlockInventoryMessage;
import org.tron.core.net.message.sync.SyncBlockChainMessage;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.net.peer.PeerManager;
import org.tron.p2p.connection.Channel;

public class SyncBlockChainMsgHandlerTest {
Expand Down Expand Up @@ -109,6 +110,9 @@ public void testProcessMessage() throws Exception {

@AfterClass
public static void destroy() {
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
context.destroy();
Args.clearParam();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import java.util.LinkedList;
import java.util.List;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.tron.common.overlay.message.Message;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.Pair;
import org.tron.common.utils.ReflectUtils;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.net.message.adv.InventoryMessage;
import org.tron.core.net.message.handshake.HelloMessage;
import org.tron.core.net.message.keepalive.PingMessage;
Expand All @@ -26,6 +30,18 @@

public class PeerConnectionTest {

@BeforeClass
public static void initArgs() {
CommonParameter.getInstance().setRateLimiterSyncBlockChain(10);
CommonParameter.getInstance().setRateLimiterFetchInvData(10);
CommonParameter.getInstance().setRateLimiterDisconnect(10);
}

@AfterClass
public static void destroy() {
Args.clearParam();
}

@Test
public void testVariableDefaultValue() {
PeerConnection peerConnection = new PeerConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,40 @@
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.ReflectUtils;
import org.tron.core.config.args.Args;
import org.tron.p2p.connection.Channel;

public class PeerManagerTest {
List<InetSocketAddress> relayNodes = new ArrayList<>();

@BeforeClass
public static void initArgs() {
CommonParameter.getInstance().setRateLimiterSyncBlockChain(10);
CommonParameter.getInstance().setRateLimiterFetchInvData(10);
CommonParameter.getInstance().setRateLimiterDisconnect(10);
}

@AfterClass
public static void destroy() {
Args.clearParam();
}

@After
public void clearPeers() {
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
}

@Test
public void testAdd() throws Exception {
Field field = PeerManager.class.getDeclaredField("peers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static void init() throws IOException {

@AfterClass
public static void after() {
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
Args.clearParam();
context.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import static org.tron.core.net.message.handshake.HelloMessage.getEndpointFromNode;

import com.google.protobuf.ByteString;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -72,14 +69,10 @@ public static void destroy() {
context.destroy();
}

@Before
@After
public void clearPeers() {
try {
Field field = PeerManager.class.getDeclaredField("peers");
field.setAccessible(true);
field.set(PeerManager.class, Collections.synchronizedList(new ArrayList<>()));
} catch (NoSuchFieldException | IllegalAccessException e) {
//ignore
for (PeerConnection p : PeerManager.getPeers()) {
PeerManager.remove(p.getChannel());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -69,6 +70,11 @@ public static void init() {
Constant.TEST_CONF);
}

@After
public void clearPeers() {
closePeer();
}

@Test
public void test() throws Exception {
initWitness();
Expand Down
Loading