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
18 changes: 1 addition & 17 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,7 @@ dependencies {
api 'org.aspectj:aspectjrt:1.9.8'
api 'org.aspectj:aspectjweaver:1.9.8'
api 'org.aspectj:aspectjtools:1.9.8'
api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.7',{
exclude group: 'io.grpc', module: 'grpc-context'
exclude group: 'io.grpc', module: 'grpc-core'
exclude group: 'io.grpc', module: 'grpc-netty'
exclude group: 'com.google.protobuf', module: 'protobuf-java'
exclude group: 'com.google.protobuf', module: 'protobuf-java-util'
// https://github.com/dom4j/dom4j/pull/116
// https://github.com/gradle/gradle/issues/13656
// https://github.com/dom4j/dom4j/issues/99
exclude group: 'jaxen', module: 'jaxen'
exclude group: 'javax.xml.stream', module: 'stax-api'
exclude group: 'net.java.dev.msv', module: 'xsdlib'
exclude group: 'pull-parser', module: 'pull-parser'
exclude group: 'xpp3', module: 'xpp3'
exclude group: 'org.bouncycastle', module: 'bcprov-jdk18on'
exclude group: 'org.bouncycastle', module: 'bcutil-jdk18on'
}
api project(':p2p')
api project(":protocol")
api project(":platform")
}
Expand Down
20 changes: 20 additions & 0 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ dependencies {
api project(":protocol")
api project(":actuator")
api project(":consensus")

// DNS-publish tests (moved here from :p2p per project convention).
// These SDKs are `implementation` scope in :p2p so not transitively visible;
// redeclared as testImplementation to keep the production runtime classpath
// unchanged.
//
// Excludes:
// - netty-nio-client: conflicts with grpc-netty transitively.
// - bcprov-jdk15on / bcpkix-jdk15on: conflict with bcprov-jdk18on.
// - pull-parser / xpp3: old XML parsers not registered in
// gradle/verification-metadata.xml; aliyun SDK works without them.
testImplementation('software.amazon.awssdk:route53:2.18.41') {
exclude group: 'software.amazon.awssdk', module: 'netty-nio-client'
}
testImplementation('com.aliyun:alidns20150109:3.0.1') {
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
exclude group: 'org.bouncycastle', module: 'bcpkix-jdk15on'
exclude group: 'pull-parser', module: 'pull-parser'
exclude group: 'xpp3', module: 'xpp3'
}
}

check.dependsOn 'lint'
Expand Down
77 changes: 77 additions & 0 deletions framework/src/test/java/org/tron/p2p/P2pConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.tron.p2p;

import org.junit.Assert;
import org.junit.Test;

public class P2pConfigTest {

@Test
public void testDefaultValues() {
P2pConfig config = new P2pConfig();
Assert.assertNotNull(config.getSeedNodes());
Assert.assertTrue(config.getSeedNodes().isEmpty());
Assert.assertNotNull(config.getActiveNodes());
Assert.assertTrue(config.getActiveNodes().isEmpty());
Assert.assertNotNull(config.getTrustNodes());
Assert.assertTrue(config.getTrustNodes().isEmpty());
Assert.assertNotNull(config.getNodeID());
Assert.assertEquals(64, config.getNodeID().length);
Assert.assertEquals(18888, config.getPort());
Assert.assertEquals(1, config.getNetworkId());
Assert.assertEquals(8, config.getMinConnections());
Assert.assertEquals(50, config.getMaxConnections());
Assert.assertEquals(2, config.getMinActiveConnections());
Assert.assertEquals(2, config.getMaxConnectionsWithSameIp());
Assert.assertTrue(config.isDiscoverEnable());
Assert.assertFalse(config.isDisconnectionPolicyEnable());
Assert.assertFalse(config.isNodeDetectEnable());
Assert.assertNotNull(config.getTreeUrls());
Assert.assertTrue(config.getTreeUrls().isEmpty());
Assert.assertNotNull(config.getPublishConfig());
}

@Test
public void testSettersAndGetters() {
P2pConfig config = new P2pConfig();

config.setPort(19999);
Assert.assertEquals(19999, config.getPort());

config.setNetworkId(42);
Assert.assertEquals(42, config.getNetworkId());

config.setMinConnections(10);
Assert.assertEquals(10, config.getMinConnections());

config.setMaxConnections(100);
Assert.assertEquals(100, config.getMaxConnections());

config.setMinActiveConnections(5);
Assert.assertEquals(5, config.getMinActiveConnections());

config.setMaxConnectionsWithSameIp(3);
Assert.assertEquals(3, config.getMaxConnectionsWithSameIp());

config.setDiscoverEnable(false);
Assert.assertFalse(config.isDiscoverEnable());

config.setDisconnectionPolicyEnable(true);
Assert.assertTrue(config.isDisconnectionPolicyEnable());

config.setNodeDetectEnable(true);
Assert.assertTrue(config.isNodeDetectEnable());

byte[] customId = new byte[64];
config.setNodeID(customId);
Assert.assertArrayEquals(customId, config.getNodeID());

config.setIp("10.0.0.1");
Assert.assertEquals("10.0.0.1", config.getIp());

config.setLanIp("192.168.0.1");
Assert.assertEquals("192.168.0.1", config.getLanIp());

config.setIpv6("::1");
Assert.assertEquals("::1", config.getIpv6());
}
}
104 changes: 104 additions & 0 deletions framework/src/test/java/org/tron/p2p/P2pServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.tron.p2p;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.p2p.base.Parameter;
import org.tron.p2p.exception.P2pException;

public class P2pServiceTest {

private P2pService p2pService;

@Before
public void init() {
p2pService = new P2pService();
// Reset handler state
Parameter.handlerList = new ArrayList<>();
Parameter.handlerMap = new HashMap<>();
}

@After
public void cleanup() {
try {
p2pService.close();
} catch (Exception e) {
// ignore cleanup errors
}
}

@Test
public void testGetVersion() {
Assert.assertEquals(Parameter.version, p2pService.getVersion());
}

@Test
public void testRegisterHandler() throws P2pException {
P2pEventHandler handler = new P2pEventHandler() {
{
Set<Byte> types = new HashSet<>();
types.add((byte) 0x50);
this.messageTypes = types;
}
};
p2pService.register(handler);
Assert.assertTrue(Parameter.handlerList.contains(handler));
Assert.assertEquals(handler, Parameter.handlerMap.get((byte) 0x50));
}

@Test(expected = P2pException.class)
public void testRegisterDuplicateTypeThrows() throws P2pException {
P2pEventHandler handler1 = new P2pEventHandler() {
{
Set<Byte> types = new HashSet<>();
types.add((byte) 0x60);
this.messageTypes = types;
}
};
P2pEventHandler handler2 = new P2pEventHandler() {
{
Set<Byte> types = new HashSet<>();
types.add((byte) 0x60);
this.messageTypes = types;
}
};
p2pService.register(handler1);
p2pService.register(handler2); // should throw
}

@Test
public void testRegisterHandlerWithNullMessageTypes() throws P2pException {
P2pEventHandler handler = new P2pEventHandler() {};
// messageTypes is null by default
p2pService.register(handler);
Assert.assertTrue(Parameter.handlerList.contains(handler));
}

@Test
public void testCloseIdempotent() throws Exception {
// Set up minimal config to allow close without NPE
// The close method checks isShutdown flag
Field isShutdownField = P2pService.class.getDeclaredField("isShutdown");
isShutdownField.setAccessible(true);

// First close
isShutdownField.set(p2pService, false);
// We can't call start() without real network, but we can test the idempotent close
isShutdownField.set(p2pService, true);
// Second close should be a no-op
p2pService.close();
Assert.assertTrue((boolean) isShutdownField.get(p2pService));
}

@Test
public void testGetP2pStats() {
// statsManager is initialized in constructor, getP2pStats should work
Assert.assertNotNull(p2pService.getP2pStats());
}
}
Loading
Loading