build(p2p): internalize libp2p as local module#6673
build(p2p): internalize libp2p as local module#6673barbatos2011 wants to merge 9 commits intotronprotocol:developfrom
Conversation
Copy source code from tronprotocol/libp2p v2.2.7 into a new p2p/ gradle submodule. This commit introduces the original source without modifications, preserving the org.tron.p2p package structure. - Add p2p/build.gradle with protobuf plugin and dependency declarations - Add p2p/lombok.config to override root lombok field name (log vs logger) - Move example code (StartApp) to src/test/java - Proto generated code is not committed (built by protobuf plugin) - Update gradle/verification-metadata.xml for new dependencies
Replace all @Slf4j-generated 'log' references with 'logger' to match java-tron's lombok.config (lombok.log.fieldName=logger). Remove the temporary p2p/lombok.config override added in the previous commit. Checkstyle is not yet enabled for the p2p module due to ~1900 violations inherited from the original libp2p codebase. This will be addressed in a follow-up PR.
Replace the Maven dependency on io.github.tronprotocol:libp2p:2.2.7 with a project dependency on the local :p2p module. This eliminates 11 exclude rules that were needed for the external artifact and removes the libp2p entry from dependency verification metadata. java-tron no longer depends on any external libp2p artifact.
Apply google-java-format for bulk formatting (indentation, imports, whitespace), then fix remaining violations manually: - Lambda indentation adjustments for checkstyle 8.7 compatibility - Line length wrapping for long strings and license headers - Empty catch blocks: add // expected comments - Star import replacement, multiple variable declaration splits Enable checkstyle plugin in p2p/build.gradle with proto-generated code excluded. Both checkstyleMain and checkstyleTest now pass.
- Replace BasicThreadFactory.builder() with new BasicThreadFactory.Builder() to use commons-lang3 3.4-compatible API, preventing implicit global version upgrade from 3.4 to 3.18.0 - Remove unused grpc-core dependency (p2p protos define only messages, no gRPC services); retain grpc-netty as Netty transport provider - Add p2p/.gitignore to exclude proto-generated code for all developers - Update verification-metadata.xml for changed dependency tree
- Replace Math.min/max with StrictMath.min/max in NodeEntry and NodeTable to satisfy CI check-math rule (integer operations, results are identical) - Rewrite NetUtilTest.testGetIP with mocked URLConnection instead of calling external IP services (checkip.amazonaws.com, ifconfig.me), covering: valid IP, connection failure, invalid IP, empty response - Remove testExternalIp (covered by mock tests) - Fix testGetLanIP to not depend on www.baidu.com connectivity
Add comprehensive unit tests to meet CI coverage gate requirements (changed files > 60%, overall delta < -0.1%). New test files (27): - connection/: Channel, ChannelManager, HandshakeService, KeepAlive, NodeDetect, ConnPoolService, UpgradeController, P2pProtobufDecoder, MessageHandler, PeerClient, P2pChannelInitializer, StatusMessage, P2pDisconnectMessage - dns/: DnsManager, AliClient, AwsClient, PublishService - discover/: FindNodeMessage, NeighborsMessage, Message, PacketDecoder, MessageHandler, DiscoverTask - utils/web3j: Numeric, Strings, Hash, ECKeyPair - stats: StatsManager - exception: DnsException - P2pService, P2pConfig Exclude proto-generated code from jacoco coverage calculation (consistent with checkstyle exclusion).
|
@barbatos2011 What is the primary motivation for integrating libp2p into java-tron at this stage? |
|
@barbatos2011 Should the unit tests related to the libp2p module be moved to the framework module? |
|
@xxo1shine Thanks for the questions. Let me address both: On the motivation for integrating nowThe main driver is reducing cross-repo maintenance friction, not adding features. Three concrete pain points with the current setup:
This follows the same pattern as go-ethereum, which keeps its On waiting for protocol stabilityFair concern, but the data doesn't support "active upgrade" framing:
If libp2p protocol upgrades do come later, they can be applied as normal PRs to the internalized module — in fact that will be easier than the current cross-repo flow. Waiting would mean indefinite deferral without a clear signal for what "stable" means here. Happy to discuss further if you see specific upgrade work that would be disrupted by this change. |
Addresses review feedback from @xxo1shine on tronprotocol#6673: > Should the unit tests related to the libp2p module be moved to the > framework module? java-tron's convention (crypto, common, chainbase, consensus, actuator, protocol) consolidates module unit tests under framework/src/test/. Align p2p with that layout, and give example reference code a clean place to live. Test migration -------------- - Move 53 unit test files from p2p/src/test/java to framework/src/test/java (org.tron.p2p.* + org.web3j.* packages preserved; 100% git renames, zero code changes). - Add testImplementation for AWS Route53 + Aliyun DNS SDKs in framework/build.gradle (required by 3 DNS-publish tests moved here; `implementation`-scope in :p2p so not transitively visible). Keep only the excludes needed for classpath conflict avoidance. - Fix pre-existing DnsManagerTest NPE by initializing Parameter.p2pConfig in @before (previously the test's result depended on what other tests had done to the static singleton). - Drop p2p's jacocoTestReport block: coverage is now collected by framework's tests and reported via :framework:jacocoTestReport. Example sourceSet ----------------- Preserve the reference usage examples from upstream libp2p v2.2.7 (StartApp, DnsExample1/2, ImportUsing + README) in a dedicated Gradle sourceSet at p2p/src/example/: - Compiles against main's output + implementation deps, so API changes in main surface as example compile errors. - NOT packaged in p2p.jar (verified: jar stays at 163 classes). - NOT executed as tests. - checkstyle disabled for example code (upstream style). - Lombok wired explicitly (root only wires main + test). Rationale: keeping the example sourceSet preserves p2p's passive usability as a standalone library reference, at zero cost to TRON runtime. Verified -------- - :p2p:compileJava / :p2p:compileExampleJava / :p2p:jar : ok - :framework:compileTestJava : ok - :framework:test --tests "org.tron.p2p.*" --tests "org.web3j.*" : 448 tests pass, 0 fail - :framework:buildFullNodeJar : p2p + web3j classes intact (163) - Spot-check of unrelated framework tests : ok
|
Done in 7858e43. What was moved
What stays in Only reference/example code from upstream libp2p v2.2.7, moved to a dedicated Gradle sourceSet: The Other changes bundled in
Verified
|
The previous commit added explicit `srcDir 'src/example/java'` and
`srcDir 'src/example/resources'` calls inside the example sourceSet.
Gradle already defaults custom sourceSet srcDirs to
`src/<name>/{java,resources}`, so the explicit calls duplicate those
entries. This caused `:p2p:processExampleResources` to fail with
"Entry README.md is a duplicate" on all CI build jobs.
Dropping the explicit srcDir calls relies on the defaults — same final
set of directories, no duplicates.
What
Internalize
io.github.tronprotocol:libp2p:2.2.7source code as a newp2p/gradle submodule, replacing the external Maven dependency. No functional changes to P2P behavior, protocol, or consensus.Why
The external libp2p repository had a 2.5-year development gap (2023.3 ~ 2025.10). Cross-repo maintenance creates friction:
p2p/package within the monorepo; this follows the same patternChanges
Commit 1:
chore(p2p): add libp2p v2.2.7 source as p2p moduleorg.web3jcrypto utils) + 2 proto files + 26 test files fromtronprotocol/libp2ptagv2.2.7p2p/build.gradlewith protobuf plugin configurationp2p/lombok.configto temporarily override root logger field name (logvslogger)StartApp) tosrc/test/java(not packaged in JAR, but available for reference)settings.gradle, updategradle/verification-metadata.xmlCommit 2:
style(p2p): replace log field with logger@Slf4j-generatedlogreferences withlogger(35 files) to match rootlombok.config(lombok.log.fieldName=logger)p2p/lombok.configoverrideCommit 3:
build(common): switch from external libp2p to local p2p modulecommon/build.gradle: replace 17 lines (Maven dependency + 11 exclude rules) with singleapi project(':p2p')io.github.tronprotocol:libp2p:2.2.7entry fromgradle/verification-metadata.xmlCommit 4:
style(p2p): fix checkstyle violations and enable checkstylegoogle-java-formatfor bulk formatting (indentation, imports, whitespace)exclude '**/protos/**')Commit 5:
fix(p2p): address review findingsBasicThreadFactory.builder()(requires commons-lang3 3.12+) withnew BasicThreadFactory.Builder()(available since 3.0) to prevent implicit global version upgrade from 3.4 to 3.18.0grpc-coredependency; retaingrpc-nettyas Netty transport provider (p2p uses 30+ Netty imports for TCP/UDP but zero gRPC imports)p2p/.gitignoreto exclude proto-generated code for all developersCommit 6:
fix(p2p): replace Math with StrictMath and fix flaky network testsMath.min/maxwithStrictMath.min/maxin 5 files to satisfy CIcheck-mathrule (integer operations, results identical)NetUtilTest.testGetIPwith mockedURLConnection— original test called 3 external IP services which failed in CI (libp2p's own CI never ran tests with-xtest)testExternalIp(covered by mock tests), fixtestGetLanIPto not depend on external connectivityKey Design Decisions
p2porg.tron.p2p, gethp2p/convention, project short-name style (common,crypto, etc.)protocol/; avoids couplingsrc/test/javabuilder()→new Builder()(3.0 API) instead of upgrading globallyservicedefinitions, zeroio.grpcimports in source**/protos/**Math→StrictMathcheck-mathrule; integer min/max results are identicaltestGetIPmock rewriteScope
org.tron.p2pare unchanged (package name preserved)git pulland rebuildKnown Issues (pre-existing in libp2p)
The following issues exist in the original libp2p v2.2.7 codebase and are NOT introduced by this PR. They will be addressed in follow-up PRs:
Critical:
java.util.Randominstead ofSecureRandom) — eclipse attack vectorHigh:
ChannelManager.handMessage(double onConnect)java.util.RandomAlgorithm.compressPubKeydrops leading zeros from public key X-coordinateMedium (from Cubic AI review):
Node.equalscompares byte[] vianew String(id)instead ofArrays.equalsChannel.hashCode()NPE wheninetSocketAddressis nullInterruptedExceptionswallowed in DiscoverServer and PeerClientTest
checkstyleMain+checkstyleTest: PASS for all modules including p2pcheck-math: PASS