From 735f85bf385bce25f06816fddee3e3605d739a31 Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 02:05:26 +0800 Subject: [PATCH 1/6] chore(p2p): add libp2p v2.2.7 source as p2p module 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 --- gradle/verification-metadata.xml | 77 +++ p2p/build.gradle | 93 ++++ p2p/lombok.config | 2 + p2p/src/main/java/org/tron/p2p/P2pConfig.java | 37 ++ .../java/org/tron/p2p/P2pEventHandler.java | 20 + .../main/java/org/tron/p2p/P2pService.java | 90 ++++ .../main/java/org/tron/p2p/base/Constant.java | 16 + .../java/org/tron/p2p/base/Parameter.java | 75 +++ .../java/org/tron/p2p/connection/Channel.java | 204 +++++++ .../tron/p2p/connection/ChannelManager.java | 298 +++++++++++ .../connection/business/MessageProcess.java | 8 + .../business/detect/NodeDetectService.java | 229 ++++++++ .../connection/business/detect/NodeStat.java | 26 + .../business/handshake/DisconnectCode.java | 30 ++ .../business/handshake/HandshakeService.java | 90 ++++ .../business/keepalive/KeepAliveService.java | 70 +++ .../business/pool/ConnPoolService.java | 339 ++++++++++++ .../business/upgrade/UpgradeController.java | 38 ++ .../tron/p2p/connection/message/Message.java | 78 +++ .../p2p/connection/message/MessageType.java | 41 ++ .../message/base/P2pDisconnectMessage.java | 39 ++ .../message/detect/StatusMessage.java | 61 +++ .../message/handshake/HelloMessage.java | 77 +++ .../message/keepalive/PingMessage.java | 33 ++ .../message/keepalive/PongMessage.java | 33 ++ .../p2p/connection/socket/MessageHandler.java | 90 ++++ .../socket/P2pChannelInitializer.java | 60 +++ .../P2pProtobufVarint32FrameDecoder.java | 98 ++++ .../p2p/connection/socket/PeerClient.java | 103 ++++ .../p2p/connection/socket/PeerServer.java | 80 +++ .../tron/p2p/discover/DiscoverService.java | 26 + .../main/java/org/tron/p2p/discover/Node.java | 187 +++++++ .../org/tron/p2p/discover/NodeManager.java | 47 ++ .../tron/p2p/discover/message/Message.java | 69 +++ .../p2p/discover/message/MessageType.java | 40 ++ .../discover/message/kad/FindNodeMessage.java | 55 ++ .../p2p/discover/message/kad/KadMessage.java | 35 ++ .../message/kad/NeighborsMessage.java | 80 +++ .../p2p/discover/message/kad/PingMessage.java | 59 ++ .../p2p/discover/message/kad/PongMessage.java | 53 ++ .../discover/protocol/kad/DiscoverTask.java | 87 +++ .../p2p/discover/protocol/kad/KadService.java | 219 ++++++++ .../discover/protocol/kad/NodeHandler.java | 238 ++++++++ .../kad/table/DistanceComparator.java | 26 + .../protocol/kad/table/KademliaOptions.java | 12 + .../protocol/kad/table/NodeBucket.java | 53 ++ .../protocol/kad/table/NodeEntry.java | 88 +++ .../protocol/kad/table/NodeTable.java | 114 ++++ .../protocol/kad/table/TimeComparator.java | 19 + .../p2p/discover/socket/DiscoverServer.java | 93 ++++ .../p2p/discover/socket/EventHandler.java | 12 + .../p2p/discover/socket/MessageHandler.java | 67 +++ .../p2p/discover/socket/P2pPacketDecoder.java | 51 ++ .../tron/p2p/discover/socket/UdpEvent.java | 32 ++ .../java/org/tron/p2p/dns/DnsManager.java | 79 +++ .../main/java/org/tron/p2p/dns/DnsNode.java | 96 ++++ .../org/tron/p2p/dns/lookup/LookUpTxt.java | 106 ++++ .../java/org/tron/p2p/dns/sync/Client.java | 188 +++++++ .../org/tron/p2p/dns/sync/ClientTree.java | 195 +++++++ .../java/org/tron/p2p/dns/sync/LinkCache.java | 82 +++ .../org/tron/p2p/dns/sync/RandomIterator.java | 126 +++++ .../org/tron/p2p/dns/sync/SubtreeSync.java | 75 +++ .../java/org/tron/p2p/dns/tree/Algorithm.java | 151 ++++++ .../org/tron/p2p/dns/tree/BranchEntry.java | 33 ++ .../java/org/tron/p2p/dns/tree/Entry.java | 10 + .../java/org/tron/p2p/dns/tree/LinkEntry.java | 54 ++ .../org/tron/p2p/dns/tree/NodesEntry.java | 40 ++ .../java/org/tron/p2p/dns/tree/RootEntry.java | 114 ++++ .../main/java/org/tron/p2p/dns/tree/Tree.java | 247 +++++++++ .../org/tron/p2p/dns/update/AliClient.java | 333 ++++++++++++ .../org/tron/p2p/dns/update/AwsClient.java | 506 ++++++++++++++++++ .../java/org/tron/p2p/dns/update/DnsType.java | 23 + .../java/org/tron/p2p/dns/update/Publish.java | 19 + .../tron/p2p/dns/update/PublishConfig.java | 25 + .../tron/p2p/dns/update/PublishService.java | 146 +++++ .../org/tron/p2p/exception/DnsException.java | 73 +++ .../org/tron/p2p/exception/P2pException.java | 59 ++ .../java/org/tron/p2p/stats/P2pStats.java | 15 + .../java/org/tron/p2p/stats/StatsManager.java | 17 + .../java/org/tron/p2p/stats/TrafficStats.java | 51 ++ .../java/org/tron/p2p/utils/ByteArray.java | 193 +++++++ .../org/tron/p2p/utils/CollectionUtils.java | 22 + .../main/java/org/tron/p2p/utils/NetUtil.java | 280 ++++++++++ .../java/org/tron/p2p/utils/ProtoUtil.java | 49 ++ .../java/org/web3j/crypto/ECDSASignature.java | 60 +++ .../main/java/org/web3j/crypto/ECKeyPair.java | 114 ++++ p2p/src/main/java/org/web3j/crypto/Hash.java | 138 +++++ p2p/src/main/java/org/web3j/crypto/Sign.java | 361 +++++++++++++ .../exceptions/MessageDecodingException.java | 24 + .../exceptions/MessageEncodingException.java | 24 + .../main/java/org/web3j/utils/Assertions.java | 29 + .../main/java/org/web3j/utils/Numeric.java | 252 +++++++++ .../main/java/org/web3j/utils/Strings.java | 58 ++ p2p/src/main/protos/Connect.proto | 60 +++ p2p/src/main/protos/Discover.proto | 50 ++ .../p2p/connection/ChannelManagerTest.java | 130 +++++ .../p2p/connection/ConnPoolServiceTest.java | 131 +++++ .../org/tron/p2p/connection/MessageTest.java | 88 +++ .../org/tron/p2p/connection/SocketTest.java | 77 +++ .../message/handshake/HelloMessageTest.java | 33 ++ .../tron/p2p/discover/NodeManagerTest.java | 25 + .../java/org/tron/p2p/discover/NodeTest.java | 87 +++ .../discover/protocol/kad/KadServiceTest.java | 54 ++ .../protocol/kad/NodeHandlerTest.java | 88 +++ .../protocol/kad/table/NodeEntryTest.java | 70 +++ .../protocol/kad/table/NodeTableTest.java | 202 +++++++ .../kad/table/TimeComparatorTest.java | 23 + .../java/org/tron/p2p/dns/AlgorithmTest.java | 106 ++++ .../java/org/tron/p2p/dns/AwsRoute53Test.java | 151 ++++++ .../java/org/tron/p2p/dns/DnsNodeTest.java | 53 ++ .../java/org/tron/p2p/dns/LinkCacheTest.java | 36 ++ .../java/org/tron/p2p/dns/RandomTest.java | 37 ++ .../test/java/org/tron/p2p/dns/SyncTest.java | 34 ++ .../test/java/org/tron/p2p/dns/TreeTest.java | 236 ++++++++ .../org/tron/p2p/example/DnsExample1.java | 110 ++++ .../org/tron/p2p/example/DnsExample2.java | 170 ++++++ .../org/tron/p2p/example/ImportUsing.java | 201 +++++++ .../test/java/org/tron/p2p/example/README.md | 421 +++++++++++++++ .../java/org/tron/p2p/example/StartApp.java | 386 +++++++++++++ .../org/tron/p2p/utils/ByteArrayTest.java | 26 + .../java/org/tron/p2p/utils/NetUtilTest.java | 205 +++++++ .../org/tron/p2p/utils/ProtoUtilTest.java | 30 ++ settings.gradle | 1 + 123 files changed, 12085 insertions(+) create mode 100644 p2p/build.gradle create mode 100644 p2p/lombok.config create mode 100644 p2p/src/main/java/org/tron/p2p/P2pConfig.java create mode 100644 p2p/src/main/java/org/tron/p2p/P2pEventHandler.java create mode 100644 p2p/src/main/java/org/tron/p2p/P2pService.java create mode 100644 p2p/src/main/java/org/tron/p2p/base/Constant.java create mode 100644 p2p/src/main/java/org/tron/p2p/base/Parameter.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/Channel.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/MessageProcess.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/handshake/DisconnectCode.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/Message.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java create mode 100644 p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/Node.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/NodeManager.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/Message.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/DistanceComparator.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeBucket.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/TimeComparator.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/socket/EventHandler.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java create mode 100644 p2p/src/main/java/org/tron/p2p/discover/socket/UdpEvent.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/DnsManager.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/DnsNode.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/sync/Client.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/Publish.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java create mode 100644 p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java create mode 100644 p2p/src/main/java/org/tron/p2p/exception/DnsException.java create mode 100644 p2p/src/main/java/org/tron/p2p/exception/P2pException.java create mode 100644 p2p/src/main/java/org/tron/p2p/stats/P2pStats.java create mode 100644 p2p/src/main/java/org/tron/p2p/stats/StatsManager.java create mode 100644 p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java create mode 100644 p2p/src/main/java/org/tron/p2p/utils/ByteArray.java create mode 100644 p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java create mode 100644 p2p/src/main/java/org/tron/p2p/utils/NetUtil.java create mode 100644 p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java create mode 100644 p2p/src/main/java/org/web3j/crypto/ECDSASignature.java create mode 100644 p2p/src/main/java/org/web3j/crypto/ECKeyPair.java create mode 100644 p2p/src/main/java/org/web3j/crypto/Hash.java create mode 100644 p2p/src/main/java/org/web3j/crypto/Sign.java create mode 100644 p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java create mode 100644 p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java create mode 100644 p2p/src/main/java/org/web3j/utils/Assertions.java create mode 100644 p2p/src/main/java/org/web3j/utils/Numeric.java create mode 100644 p2p/src/main/java/org/web3j/utils/Strings.java create mode 100644 p2p/src/main/protos/Connect.proto create mode 100644 p2p/src/main/protos/Discover.proto create mode 100644 p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/connection/MessageTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/connection/SocketTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/NodeManagerTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/NodeTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/RandomTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/SyncTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/dns/TreeTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/example/DnsExample1.java create mode 100644 p2p/src/test/java/org/tron/p2p/example/DnsExample2.java create mode 100644 p2p/src/test/java/org/tron/p2p/example/ImportUsing.java create mode 100644 p2p/src/test/java/org/tron/p2p/example/README.md create mode 100644 p2p/src/test/java/org/tron/p2p/example/StartApp.java create mode 100644 p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java create mode 100644 p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 4d0bf1013d6..f434248cddf 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -340,6 +340,14 @@ + + + + + + + + @@ -350,6 +358,11 @@ + + + + + @@ -394,6 +407,14 @@ + + + + + + + + @@ -512,6 +533,9 @@ + + + @@ -1209,6 +1233,22 @@ + + + + + + + + + + + + + + + + @@ -1280,6 +1320,19 @@ + + + + + + + + + + + + + @@ -1639,6 +1692,14 @@ + + + + + + + + @@ -1652,6 +1713,14 @@ + + + + + + + + @@ -2431,6 +2500,14 @@ + + + + + + + + diff --git a/p2p/build.gradle b/p2p/build.gradle new file mode 100644 index 00000000000..a6ac4fa1f67 --- /dev/null +++ b/p2p/build.gradle @@ -0,0 +1,93 @@ +apply plugin: 'com.google.protobuf' + +def protobufVersion = '3.25.8' +def grpcVersion = '1.75.0' + +sourceSets { + main { + proto { + srcDir 'src/main/protos' + } + java { + srcDir 'src/main/java' + } + } +} + +dependencies { + // protobuf & grpc (implementation scope: not leaked to consumers) + implementation "com.google.protobuf:protobuf-java:${protobufVersion}" + implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}" + implementation "io.grpc:grpc-netty:${grpcVersion}" + implementation "io.grpc:grpc-core:${grpcVersion}" + + // p2p-specific dependencies + implementation 'org.xerial.snappy:snappy-java:1.1.10.5' + implementation 'org.bouncycastle:bcpkix-jdk18on:1.79' + implementation 'dnsjava:dnsjava:3.6.2' + implementation 'commons-cli:commons-cli:1.5.0' + implementation('software.amazon.awssdk:route53:2.18.41') { + exclude group: 'io.netty', module: 'netty-codec-http2' + exclude group: 'io.netty', module: 'netty-codec-http' + exclude group: 'io.netty', module: 'netty-common' + exclude group: 'io.netty', module: 'netty-buffer' + exclude group: 'io.netty', module: 'netty-transport' + exclude group: 'io.netty', module: 'netty-codec' + exclude group: 'io.netty', module: 'netty-handler' + exclude group: 'io.netty', module: 'netty-resolver' + exclude group: 'io.netty', module: 'netty-transport-classes-epoll' + exclude group: 'io.netty', module: 'netty-transport-native-unix-common' + exclude group: 'software.amazon.awssdk', module: 'netty-nio-client' + } + implementation('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' + } + + // commons-lang3: libp2p uses BasicThreadFactory.builder() which requires 3.12+. + // Root build.gradle provides 3.4 globally but as 'implementation' (not on compile classpath). + // Declare 3.18.0 (matching original libp2p) to ensure API compatibility. + implementation 'org.apache.commons:commons-lang3:3.18.0' + + // provided by root build.gradle for all subprojects: + // slf4j-api, logback, bcprov-jdk18on, lombok, junit, mockito +} + +protobuf { + generatedFilesBaseDir = "$projectDir/src" + protoc { + artifact = "com.google.protobuf:protoc:${protobufVersion}" + } + plugins { + grpc { + artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.archInfo.requires.ProtocGenVersion}" + } + } + generateProtoTasks { + all().each { task -> + task.builtins { + java { outputSubDir = "java" } + } + } + all()*.plugins { + grpc { + outputSubDir = "java" + } + } + } +} + +clean.doFirst { + delete "src/main/java/org/tron/p2p/protos" +} + +processResources.dependsOn(generateProto) + +jacocoTestReport { + reports { + xml.enabled = true + html.enabled = true + } +} diff --git a/p2p/lombok.config b/p2p/lombok.config new file mode 100644 index 00000000000..012382f4b02 --- /dev/null +++ b/p2p/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.log.fieldName=log diff --git a/p2p/src/main/java/org/tron/p2p/P2pConfig.java b/p2p/src/main/java/org/tron/p2p/P2pConfig.java new file mode 100644 index 00000000000..9a3aef6b9df --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/P2pConfig.java @@ -0,0 +1,37 @@ +package org.tron.p2p; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import lombok.Data; +import org.tron.p2p.dns.update.PublishConfig; +import org.tron.p2p.utils.NetUtil; + +@Data +public class P2pConfig { + + private List seedNodes = new CopyOnWriteArrayList<>(); + private List activeNodes = new CopyOnWriteArrayList<>(); + private List trustNodes = new CopyOnWriteArrayList<>(); + private byte[] nodeID = NetUtil.getNodeId(); + private String ip = NetUtil.getExternalIpV4(); + private String lanIp = NetUtil.getLanIP(); + private String ipv6 = NetUtil.getExternalIpV6(); + private int port = 18888; + private int networkId = 1; + private int minConnections = 8; + private int maxConnections = 50; + private int minActiveConnections = 2; + private int maxConnectionsWithSameIp = 2; + private boolean discoverEnable = true; + private boolean disconnectionPolicyEnable = false; + private boolean nodeDetectEnable = false; + + //dns read config + private List treeUrls = new ArrayList<>(); + + //dns publish config + private PublishConfig publishConfig = new PublishConfig(); +} diff --git a/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java b/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java new file mode 100644 index 00000000000..7ca3f235049 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java @@ -0,0 +1,20 @@ +package org.tron.p2p; + +import java.util.Set; +import lombok.Getter; +import org.tron.p2p.connection.Channel; + +public abstract class P2pEventHandler { + + @Getter + protected Set messageTypes; + + public void onConnect(Channel channel) { + } + + public void onDisconnect(Channel channel) { + } + + public void onMessage(Channel channel, byte[] data) { + } +} diff --git a/p2p/src/main/java/org/tron/p2p/P2pService.java b/p2p/src/main/java/org/tron/p2p/P2pService.java new file mode 100644 index 00000000000..8173f40f4c6 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/P2pService.java @@ -0,0 +1,90 @@ +package org.tron.p2p; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.NodeManager; +import org.tron.p2p.dns.DnsManager; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.stats.P2pStats; +import org.tron.p2p.stats.StatsManager; + +@Slf4j(topic = "net") +public class P2pService { + + private StatsManager statsManager = new StatsManager(); + private volatile boolean isShutdown = false; + + public void start(P2pConfig p2pConfig) { + Parameter.p2pConfig = p2pConfig; + NodeManager.init(); + ChannelManager.init(); + DnsManager.init(); + log.info("P2p service started"); + + Runtime.getRuntime().addShutdownHook(new Thread(this::close)); + } + + public void close() { + if (isShutdown) { + return; + } + isShutdown = true; + DnsManager.close(); + NodeManager.close(); + ChannelManager.close(); + log.info("P2p service closed"); + } + + public void register(P2pEventHandler p2PEventHandler) throws P2pException { + Parameter.addP2pEventHandle(p2PEventHandler); + } + + @Deprecated + public void connect(InetSocketAddress address) { + ChannelManager.connect(address); + } + + public ChannelFuture connect(Node node, ChannelFutureListener future) { + return ChannelManager.connect(node, future); + } + + public P2pStats getP2pStats() { + return statsManager.getP2pStats(); + } + + public List getTableNodes() { + return NodeManager.getTableNodes(); + } + + public List getConnectableNodes() { + Set nodes = new HashSet<>(); + nodes.addAll(NodeManager.getConnectableNodes()); + nodes.addAll(DnsManager.getDnsNodes()); + return new ArrayList<>(nodes); + } + + public List getAllNodes() { + Set nodes = new HashSet<>(); + nodes.addAll(NodeManager.getAllNodes()); + nodes.addAll(DnsManager.getDnsNodes()); + return new ArrayList<>(nodes); + } + + public void updateNodeId(Channel channel, String nodeId) { + ChannelManager.updateNodeId(channel, nodeId); + } + + public int getVersion() { + return Parameter.version; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/base/Constant.java b/p2p/src/main/java/org/tron/p2p/base/Constant.java new file mode 100644 index 00000000000..b87cfa287f5 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/base/Constant.java @@ -0,0 +1,16 @@ +package org.tron.p2p.base; + +import java.util.Arrays; +import java.util.List; + +public class Constant { + + public static final int NODE_ID_LEN = 64; + public static final List ipV4Urls = Arrays.asList( + "http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/"); + public static final List ipV6Urls = Arrays.asList( + "https://v6.ident.me", "http://6.ipw.cn/", "https://api6.ipify.org", + "https://ipv6.icanhazip.com"); + public static final String ipV4Hex = "00000000"; //32 bit + public static final String ipV6Hex = "00000000000000000000000000000000"; //128 bit +} diff --git a/p2p/src/main/java/org/tron/p2p/base/Parameter.java b/p2p/src/main/java/org/tron/p2p/base/Parameter.java new file mode 100644 index 00000000000..50055949dc8 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/base/Parameter.java @@ -0,0 +1,75 @@ +package org.tron.p2p.base; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.protobuf.ByteString; +import lombok.Data; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pEventHandler; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.exception.P2pException.TypeEnum; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.utils.ByteArray; + +@Data +public class Parameter { + + public static int version = 1; + + public static final int TCP_NETTY_WORK_THREAD_NUM = 0; + + public static final int UDP_NETTY_WORK_THREAD_NUM = 1; + + public static final int CONN_MAX_QUEUE_SIZE = 10; + + public static final int NODE_CONNECTION_TIMEOUT = 2000; + + public static final int KEEP_ALIVE_TIMEOUT = 20_000; + + public static final int PING_TIMEOUT = 20_000; + + public static final int NETWORK_TIME_DIFF = 1000; + + public static final long DEFAULT_BAN_TIME = 60_000; + + public static final int MAX_MESSAGE_LENGTH = 5 * 1024 * 1024; + + public static volatile P2pConfig p2pConfig; + + public static volatile List handlerList = new ArrayList<>(); + + public static volatile Map handlerMap = new HashMap<>(); + + public static void addP2pEventHandle(P2pEventHandler p2PEventHandler) throws P2pException { + if (p2PEventHandler.getMessageTypes() != null) { + for (Byte type : p2PEventHandler.getMessageTypes()) { + if (handlerMap.get(type) != null) { + throw new P2pException(TypeEnum.TYPE_ALREADY_REGISTERED, "type:" + type); + } + } + for (Byte type : p2PEventHandler.getMessageTypes()) { + handlerMap.put(type, p2PEventHandler); + } + } + handlerList.add(p2PEventHandler); + } + + public static Discover.Endpoint getHomeNode() { + Discover.Endpoint.Builder builder = Discover.Endpoint.newBuilder() + .setNodeId(ByteString.copyFrom(Parameter.p2pConfig.getNodeID())) + .setPort(Parameter.p2pConfig.getPort()); + if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) { + builder.setAddress(ByteString.copyFrom( + ByteArray.fromString(Parameter.p2pConfig.getIp()))); + } + if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) { + builder.setAddressIpv6(ByteString.copyFrom( + ByteArray.fromString(Parameter.p2pConfig.getIpv6()))); + } + return builder.build(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/Channel.java b/p2p/src/main/java/org/tron/p2p/connection/Channel.java new file mode 100644 index 00000000000..d57259809e8 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/Channel.java @@ -0,0 +1,204 @@ +package org.tron.p2p.connection; + +import com.google.common.base.Throwables; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.CorruptedFrameException; +import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; +import io.netty.handler.timeout.ReadTimeoutException; +import io.netty.handler.timeout.ReadTimeoutHandler; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.upgrade.UpgradeController; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.handshake.HelloMessage; +import org.tron.p2p.connection.socket.MessageHandler; +import org.tron.p2p.connection.socket.P2pProtobufVarint32FrameDecoder; +import org.tron.p2p.discover.Node; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.stats.TrafficStats; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class Channel { + + public volatile boolean waitForPong = false; + public volatile long pingSent = System.currentTimeMillis(); + + @Getter + private HelloMessage helloMessage; + @Getter + private Node node; + @Getter + private int version; + @Getter + private ChannelHandlerContext ctx; + @Getter + private InetSocketAddress inetSocketAddress; + @Getter + private InetAddress inetAddress; + @Getter + private volatile long disconnectTime; + @Getter + @Setter + private volatile boolean isDisconnect = false; + @Getter + @Setter + private long lastSendTime = System.currentTimeMillis(); + @Getter + private final long startTime = System.currentTimeMillis(); + @Getter + private boolean isActive = false; + @Getter + private boolean isTrustPeer; + @Getter + @Setter + private volatile boolean finishHandshake; + @Getter + @Setter + private String nodeId; + @Setter + @Getter + private boolean discoveryMode; + @Getter + private long avgLatency; + private long count; + + public void init(ChannelPipeline pipeline, String nodeId, boolean discoveryMode) { + this.discoveryMode = discoveryMode; + this.nodeId = nodeId; + this.isActive = StringUtils.isNotEmpty(nodeId); + MessageHandler messageHandler = new MessageHandler(this); + pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60, TimeUnit.SECONDS)); + pipeline.addLast(TrafficStats.tcp); + pipeline.addLast("protoPrepend", new ProtobufVarint32LengthFieldPrepender()); + pipeline.addLast("protoDecode", new P2pProtobufVarint32FrameDecoder(this)); + pipeline.addLast("messageHandler", messageHandler); + } + + public void processException(Throwable throwable) { + Throwable baseThrowable = throwable; + try { + baseThrowable = Throwables.getRootCause(baseThrowable); + } catch (IllegalArgumentException e) { + baseThrowable = e.getCause(); + log.warn("Loop in causal chain detected"); + } + SocketAddress address = ctx.channel().remoteAddress(); + if (throwable instanceof ReadTimeoutException + || throwable instanceof IOException + || throwable instanceof CorruptedFrameException) { + log.warn("Close peer {}, reason: {}", address, throwable.getMessage()); + } else if (baseThrowable instanceof P2pException) { + log.warn("Close peer {}, type: ({}), info: {}", + address, ((P2pException) baseThrowable).getType(), baseThrowable.getMessage()); + } else { + log.error("Close peer {}, exception caught", address, throwable); + } + close(); + } + + public void setHelloMessage(HelloMessage helloMessage) { + this.helloMessage = helloMessage; + this.node = helloMessage.getFrom(); + this.nodeId = node.getHexId(); //update node id from handshake + this.version = helloMessage.getVersion(); + } + + public void setChannelHandlerContext(ChannelHandlerContext ctx) { + this.ctx = ctx; + this.inetSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress(); + this.inetAddress = inetSocketAddress.getAddress(); + this.isTrustPeer = Parameter.p2pConfig.getTrustNodes().contains(inetAddress); + } + + public void close(long banTime) { + this.isDisconnect = true; + this.disconnectTime = System.currentTimeMillis(); + ChannelManager.banNode(this.inetAddress, banTime); + ctx.close(); + } + + public void close() { + close(Parameter.DEFAULT_BAN_TIME); + } + + public void send(Message message) { + if (message.needToLog()) { + log.info("Send message to channel {}, {}", inetSocketAddress, message); + } else { + log.debug("Send message to channel {}, {}", inetSocketAddress, message); + } + send(message.getSendData()); + } + + public void send(byte[] data) { + try { + byte type = data[0]; + if (isDisconnect) { + log.warn("Send to {} failed as channel has closed, message-type:{} ", + ctx.channel().remoteAddress(), type); + return; + } + + if (finishHandshake) { + data = UpgradeController.codeSendData(version, data); + } + + ByteBuf byteBuf = Unpooled.wrappedBuffer(data); + ctx.writeAndFlush(byteBuf).addListener((ChannelFutureListener) future -> { + if (!future.isSuccess() && !isDisconnect) { + log.warn("Send to {} failed, message-type:{}, cause:{}", + ctx.channel().remoteAddress(), ByteArray.byte2int(type), + future.cause().getMessage()); + } + }); + setLastSendTime(System.currentTimeMillis()); + } catch (Exception e) { + log.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage()); + ctx.channel().close(); + } + } + + public void updateAvgLatency(long latency) { + long total = this.avgLatency * this.count; + this.count++; + this.avgLatency = (total + latency) / this.count; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Channel channel = (Channel) o; + return Objects.equals(inetSocketAddress, channel.inetSocketAddress); + } + + @Override + public int hashCode() { + return inetSocketAddress.hashCode(); + } + + @Override + public String toString() { + return String.format("%s | %s", inetSocketAddress, + StringUtils.isEmpty(nodeId) ? "" : nodeId); + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java new file mode 100644 index 00000000000..f781d8ddff6 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java @@ -0,0 +1,298 @@ +package org.tron.p2p.connection; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.bouncycastle.util.encoders.Hex; +import org.tron.p2p.P2pEventHandler; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.detect.NodeDetectService; +import org.tron.p2p.connection.business.handshake.DisconnectCode; +import org.tron.p2p.connection.business.handshake.HandshakeService; +import org.tron.p2p.connection.business.keepalive.KeepAliveService; +import org.tron.p2p.connection.business.pool.ConnPoolService; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.socket.PeerClient; +import org.tron.p2p.connection.socket.PeerServer; +import org.tron.p2p.discover.Node; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.exception.P2pException.TypeEnum; +import org.tron.p2p.protos.Connect.DisconnectReason; +import org.tron.p2p.utils.ByteArray; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class ChannelManager { + + @Getter + private static NodeDetectService nodeDetectService; + + private static PeerServer peerServer; + + @Getter + private static PeerClient peerClient; + + @Getter + private static ConnPoolService connPoolService; + + private static KeepAliveService keepAliveService; + + @Getter + private static HandshakeService handshakeService; + + @Getter + private static final Map channels = new ConcurrentHashMap<>(); + + @Getter + private static final Cache bannedNodes = CacheBuilder + .newBuilder().maximumSize(2000).build(); //ban timestamp + + private static boolean isInit = false; + public static volatile boolean isShutdown = false; + + public static void init() { + isInit = true; + peerServer = new PeerServer(); + peerClient = new PeerClient(); + keepAliveService = new KeepAliveService(); + connPoolService = new ConnPoolService(); + handshakeService = new HandshakeService(); + nodeDetectService = new NodeDetectService(); + peerServer.init(); + peerClient.init(); + keepAliveService.init(); + connPoolService.init(peerClient); + nodeDetectService.init(peerClient); + } + + public static void connect(InetSocketAddress address) { + peerClient.connect(address.getAddress().getHostAddress(), address.getPort(), + ByteArray.toHexString(NetUtil.getNodeId())); + } + + public static ChannelFuture connect(Node node, ChannelFutureListener future) { + return peerClient.connect(node, future); + } + + public static void notifyDisconnect(Channel channel) { + if (channel.getInetSocketAddress() == null) { + log.warn("Notify Disconnect peer has no address."); + return; + } + channels.remove(channel.getInetSocketAddress()); + Parameter.handlerList.forEach(h -> h.onDisconnect(channel)); + InetAddress inetAddress = channel.getInetAddress(); + if (inetAddress != null) { + banNode(inetAddress, Parameter.DEFAULT_BAN_TIME); + } + } + + public static int getConnectionNum(InetAddress inetAddress) { + int cnt = 0; + for (Channel channel : channels.values()) { + if (channel.getInetAddress().equals(inetAddress)) { + cnt++; + } + } + return cnt; + } + + public static synchronized DisconnectCode processPeer(Channel channel) { + + if (!channel.isActive() && !channel.isTrustPeer()) { + InetAddress inetAddress = channel.getInetAddress(); + if (bannedNodes.getIfPresent(inetAddress) != null + && bannedNodes.getIfPresent(inetAddress) > System.currentTimeMillis()) { + log.info("Peer {} recently disconnected", channel); + return DisconnectCode.TIME_BANNED; + } + + if (channels.size() >= Parameter.p2pConfig.getMaxConnections()) { + log.info("Too many peers, disconnected with {}", channel); + return DisconnectCode.TOO_MANY_PEERS; + } + + int num = getConnectionNum(channel.getInetAddress()); + if (num >= Parameter.p2pConfig.getMaxConnectionsWithSameIp()) { + log.info("Max connection with same ip {}", channel); + return DisconnectCode.MAX_CONNECTION_WITH_SAME_IP; + } + } + + if (StringUtils.isNotEmpty(channel.getNodeId())) { + for (Channel c : channels.values()) { + if (channel.getNodeId().equals(c.getNodeId())) { + if (c.getStartTime() > channel.getStartTime()) { + c.close(); + } else { + log.info("Duplicate peer {}, exist peer {}", channel, c); + return DisconnectCode.DUPLICATE_PEER; + } + } + } + } + + channels.put(channel.getInetSocketAddress(), channel); + + log.info("Add peer {}, total channels: {}", channel.getInetSocketAddress(), channels.size()); + return DisconnectCode.NORMAL; + } + + public static DisconnectReason getDisconnectReason(DisconnectCode code) { + DisconnectReason disconnectReason; + switch (code) { + case DIFFERENT_VERSION: + disconnectReason = DisconnectReason.DIFFERENT_VERSION; + break; + case TIME_BANNED: + disconnectReason = DisconnectReason.RECENT_DISCONNECT; + break; + case DUPLICATE_PEER: + disconnectReason = DisconnectReason.DUPLICATE_PEER; + break; + case TOO_MANY_PEERS: + disconnectReason = DisconnectReason.TOO_MANY_PEERS; + break; + case MAX_CONNECTION_WITH_SAME_IP: + disconnectReason = DisconnectReason.TOO_MANY_PEERS_WITH_SAME_IP; + break; + default: { + disconnectReason = DisconnectReason.UNKNOWN; + } + } + return disconnectReason; + } + + public static void logDisconnectReason(Channel channel, DisconnectReason reason) { + log.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name()); + } + + public static void banNode(InetAddress inetAddress, Long banTime) { + long now = System.currentTimeMillis(); + if (bannedNodes.getIfPresent(inetAddress) == null + || bannedNodes.getIfPresent(inetAddress) < now) { + bannedNodes.put(inetAddress, now + banTime); + } + } + + public static void close() { + if (!isInit || isShutdown) { + return; + } + isShutdown = true; + connPoolService.close(); + keepAliveService.close(); + peerServer.close(); + peerClient.close(); + nodeDetectService.close(); + } + + + public static void processMessage(Channel channel, byte[] data) throws P2pException { + if (data == null || data.length == 0) { + throw new P2pException(TypeEnum.EMPTY_MESSAGE, ""); + } + if (data[0] >= 0) { + handMessage(channel, data); + return; + } + + Message message = Message.parse(data); + + if (message.needToLog()) { + log.info("Receive message from channel: {}, {}", channel.getInetSocketAddress(), message); + } else { + log.debug("Receive message from channel {}, {}", channel.getInetSocketAddress(), message); + } + + switch (message.getType()) { + case KEEP_ALIVE_PING: + case KEEP_ALIVE_PONG: + keepAliveService.processMessage(channel, message); + break; + case HANDSHAKE_HELLO: + handshakeService.processMessage(channel, message); + break; + case STATUS: + nodeDetectService.processMessage(channel, message); + break; + case DISCONNECT: + channel.close(); + break; + default: + throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type:" + data[0]); + } + } + + private static void handMessage(Channel channel, byte[] data) throws P2pException { + P2pEventHandler handler = Parameter.handlerMap.get(data[0]); + if (handler == null) { + throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type:" + data[0]); + } + if (channel.isDiscoveryMode()) { + channel.send(new P2pDisconnectMessage(DisconnectReason.DISCOVER_MODE)); + channel.getCtx().close(); + return; + } + + if (!channel.isFinishHandshake()) { + channel.setFinishHandshake(true); + DisconnectCode code = processPeer(channel); + if (!DisconnectCode.NORMAL.equals(code)) { + DisconnectReason disconnectReason = getDisconnectReason(code); + channel.send(new P2pDisconnectMessage(disconnectReason)); + channel.getCtx().close(); + return; + } + Parameter.handlerList.forEach(h -> h.onConnect(channel)); + } + + handler.onMessage(channel, data); + } + + public static synchronized void updateNodeId(Channel channel, String nodeId) { + channel.setNodeId(nodeId); + if (nodeId.equals(Hex.toHexString(Parameter.p2pConfig.getNodeID()))) { + log.warn("Channel {} is myself", channel.getInetSocketAddress()); + channel.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); + channel.close(); + return; + } + + List list = new ArrayList<>(); + channels.values().forEach(c -> { + if (nodeId.equals(c.getNodeId())) { + list.add(c); + } + }); + if (list.size() <= 1) { + return; + } + Channel c1 = list.get(0); + Channel c2 = list.get(1); + if (c1.getStartTime() > c2.getStartTime()) { + log.info("Close channel {}, other channel {} is earlier", c1, c2); + c1.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); + c1.close(); + } else { + log.info("Close channel {}, other channel {} is earlier", c2, c1); + c2.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); + c2.close(); + } + } + + public static void triggerConnect(InetSocketAddress address) { + connPoolService.triggerConnect(address); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/MessageProcess.java b/p2p/src/main/java/org/tron/p2p/connection/business/MessageProcess.java new file mode 100644 index 00000000000..cf731e23398 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/MessageProcess.java @@ -0,0 +1,8 @@ +package org.tron.p2p.connection.business; + +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.message.Message; + +public interface MessageProcess { + void processMessage(Channel channel, Message message); +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java new file mode 100644 index 00000000000..3ef53b59a02 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java @@ -0,0 +1,229 @@ +package org.tron.p2p.connection.business.detect; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.business.MessageProcess; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.detect.StatusMessage; +import org.tron.p2p.connection.socket.PeerClient; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.NodeManager; + +@Slf4j(topic = "net") +public class NodeDetectService implements MessageProcess { + + private PeerClient peerClient; + + private Map nodeStatMap = new ConcurrentHashMap<>(); + + @Getter + private static final Cache badNodesCache = CacheBuilder + .newBuilder().maximumSize(5000).expireAfterWrite(1, TimeUnit.HOURS).build(); + + private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("nodeDetectService").build()); + + private final long NODE_DETECT_THRESHOLD = 5 * 60 * 1000; + + private final long NODE_DETECT_MIN_THRESHOLD = 30 * 1000; + + private final long NODE_DETECT_TIMEOUT = 2 * 1000; + + private final int MAX_NODE_SLOW_DETECT = 3; + + private final int MAX_NODE_NORMAL_DETECT = 10; + + private final int MAX_NODE_FAST_DETECT = 100; + + private final int MAX_NODES = 300; + + private final int MIN_NODES = 200; + + + public void init(PeerClient peerClient) { + if (!Parameter.p2pConfig.isNodeDetectEnable()) { + return; + } + this.peerClient = peerClient; + executor.scheduleWithFixedDelay(() -> { + try { + work(); + } catch (Exception t) { + log.warn("Exception in node detect worker, {}", t.getMessage()); + } + }, 1, 5, TimeUnit.SECONDS); + } + + public void close() { + executor.shutdown(); + } + + public void work() { + trimNodeMap(); + if (nodeStatMap.size() < MIN_NODES) { + loadNodes(); + } + + List nodeStats = getSortedNodeStats(); + if (nodeStats.size() == 0) { + return; + } + + NodeStat nodeStat = nodeStats.get(0); + if (nodeStat.getLastDetectTime() > System.currentTimeMillis() - NODE_DETECT_MIN_THRESHOLD) { + return; + } + + int n = MAX_NODE_NORMAL_DETECT; + if (nodeStat.getLastDetectTime() > System.currentTimeMillis() - NODE_DETECT_THRESHOLD) { + n = MAX_NODE_SLOW_DETECT; + } + + n = Math.min(n, nodeStats.size()); + + for (int i = 0; i < n; i++) { + detect(nodeStats.get(i)); + } + } + + public void trimNodeMap() { + long now = System.currentTimeMillis(); + nodeStatMap.forEach((k, v) -> { + if (!v.finishDetect() && v.getLastDetectTime() < now - NODE_DETECT_TIMEOUT) { + nodeStatMap.remove(k); + badNodesCache.put(k.getAddress(), System.currentTimeMillis()); + } + }); + } + + private void loadNodes() { + int size = nodeStatMap.size(); + int count = 0; + List nodes = NodeManager.getConnectableNodes(); + for (Node node : nodes) { + InetSocketAddress socketAddress = node.getPreferInetSocketAddress(); + if (socketAddress != null + && !nodeStatMap.containsKey(socketAddress) + && badNodesCache.getIfPresent(socketAddress.getAddress()) == null) { + NodeStat nodeStat = new NodeStat(node); + nodeStatMap.put(socketAddress, nodeStat); + detect(nodeStat); + count++; + if (count >= MAX_NODE_FAST_DETECT || count + size >= MAX_NODES) { + break; + } + } + } + } + + private void detect(NodeStat stat) { + try { + stat.setTotalCount(stat.getTotalCount() + 1); + setLastDetectTime(stat); + peerClient.connectAsync(stat.getNode(), true); + } catch (Exception e) { + log.warn("Detect node {} failed, {}", + stat.getNode().getPreferInetSocketAddress(), e.getMessage()); + nodeStatMap.remove(stat.getSocketAddress()); + } + } + + public synchronized void processMessage(Channel channel, Message message) { + StatusMessage statusMessage = (StatusMessage) message; + + if (!channel.isActive()) { + channel.setDiscoveryMode(true); + channel.send(new StatusMessage()); + channel.getCtx().close(); + return; + } + + InetSocketAddress socketAddress = channel.getInetSocketAddress(); + NodeStat nodeStat = nodeStatMap.get(socketAddress); + if (nodeStat == null) { + return; + } + + long cost = System.currentTimeMillis() - nodeStat.getLastDetectTime(); + if (cost > NODE_DETECT_TIMEOUT + || statusMessage.getRemainConnections() == 0) { + badNodesCache.put(socketAddress.getAddress(), cost); + nodeStatMap.remove(socketAddress); + } + + nodeStat.setLastSuccessDetectTime(nodeStat.getLastDetectTime()); + setStatusMessage(nodeStat, statusMessage); + + channel.getCtx().close(); + } + + public void notifyDisconnect(Channel channel) { + + if (!channel.isActive()) { + return; + } + + InetSocketAddress socketAddress = channel.getInetSocketAddress(); + if (socketAddress == null) { + return; + } + + NodeStat nodeStat = nodeStatMap.get(socketAddress); + if (nodeStat == null) { + return; + } + + if (nodeStat.getLastDetectTime() != nodeStat.getLastSuccessDetectTime()) { + badNodesCache.put(socketAddress.getAddress(), System.currentTimeMillis()); + nodeStatMap.remove(socketAddress); + } + } + + private synchronized List getSortedNodeStats() { + List nodeStats = new ArrayList<>(nodeStatMap.values()); + nodeStats.sort(Comparator.comparingLong(o -> o.getLastDetectTime())); + return nodeStats; + } + + private synchronized void setLastDetectTime(NodeStat nodeStat) { + nodeStat.setLastDetectTime(System.currentTimeMillis()); + } + + private synchronized void setStatusMessage(NodeStat nodeStat, StatusMessage message) { + nodeStat.setStatusMessage(message); + } + + public synchronized List getConnectableNodes() { + List stats = new ArrayList<>(); + List nodes = new ArrayList<>(); + nodeStatMap.values().forEach(stat -> { + if (stat.getStatusMessage() != null) { + stats.add(stat); + } + }); + + if (stats.isEmpty()) { + return nodes; + } + + stats.sort(Comparator.comparingInt(o -> -o.getStatusMessage().getRemainConnections())); + stats.forEach(stat -> nodes.add(stat.getNode())); + return nodes; + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java new file mode 100644 index 00000000000..2f2ef4a5ae7 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java @@ -0,0 +1,26 @@ +package org.tron.p2p.connection.business.detect; + +import lombok.Data; +import org.tron.p2p.connection.message.detect.StatusMessage; +import org.tron.p2p.discover.Node; + +import java.net.InetSocketAddress; + +@Data +public class NodeStat { + private int totalCount; + private long lastDetectTime; + private long lastSuccessDetectTime; + private StatusMessage statusMessage; + private Node node; + private InetSocketAddress socketAddress; + + public NodeStat(Node node) { + this.node = node; + this.socketAddress = node.getPreferInetSocketAddress(); + } + + public boolean finishDetect() { + return this.lastDetectTime == this.lastSuccessDetectTime; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/DisconnectCode.java b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/DisconnectCode.java new file mode 100644 index 00000000000..fc4c9224988 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/DisconnectCode.java @@ -0,0 +1,30 @@ +package org.tron.p2p.connection.business.handshake; + +public enum DisconnectCode { + NORMAL(0), + TOO_MANY_PEERS(1), + DIFFERENT_VERSION(2), + TIME_BANNED(3), + DUPLICATE_PEER(4), + MAX_CONNECTION_WITH_SAME_IP(5), + UNKNOWN(256); + + private final Integer value; + + DisconnectCode(Integer value) { + this.value = value; + } + + public Integer getValue() { + return value; + } + + public static DisconnectCode forNumber(int code) { + for (DisconnectCode disconnectCode : values()) { + if (disconnectCode.value == code) { + return disconnectCode; + } + } + return UNKNOWN; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java new file mode 100644 index 00000000000..760849a3a83 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java @@ -0,0 +1,90 @@ +package org.tron.p2p.connection.business.handshake; + +import static org.tron.p2p.connection.ChannelManager.getDisconnectReason; +import static org.tron.p2p.connection.ChannelManager.logDisconnectReason; + +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.connection.business.MessageProcess; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.message.handshake.HelloMessage; +import org.tron.p2p.protos.Connect.DisconnectReason; + +@Slf4j(topic = "net") +public class HandshakeService implements MessageProcess { + + private final int networkId = Parameter.p2pConfig.getNetworkId(); + + public void startHandshake(Channel channel) { + sendHelloMsg(channel, DisconnectCode.NORMAL, channel.getStartTime()); + } + + @Override + public void processMessage(Channel channel, Message message) { + HelloMessage msg = (HelloMessage) message; + + if (channel.isFinishHandshake()) { + log.warn("Close channel {}, handshake is finished", channel.getInetSocketAddress()); + channel.send(new P2pDisconnectMessage(DisconnectReason.DUP_HANDSHAKE)); + channel.close(); + return; + } + + channel.setHelloMessage(msg); + + DisconnectCode code = ChannelManager.processPeer(channel); + if (code != DisconnectCode.NORMAL) { + if (!channel.isActive()) { + sendHelloMsg(channel, code, msg.getTimestamp()); + } + logDisconnectReason(channel, getDisconnectReason(code)); + channel.close(); + return; + } + + ChannelManager.updateNodeId(channel, msg.getFrom().getHexId()); + if (channel.isDisconnect()) { + return; + } + + if (channel.isActive()) { + if (msg.getCode() != DisconnectCode.NORMAL.getValue() + || (msg.getNetworkId() != networkId && msg.getVersion() != networkId)) { + DisconnectCode disconnectCode = DisconnectCode.forNumber(msg.getCode()); + //v0.1 have version, v0.2 both have version and networkId + log.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}", + channel.getInetSocketAddress(), + msg.getCode(), + disconnectCode.name(), + msg.getNetworkId(), + msg.getVersion()); + logDisconnectReason(channel, getDisconnectReason(disconnectCode)); + channel.close(); + return; + } + } else { + + if (msg.getNetworkId() != networkId) { + log.info("Peer {} different network id, peer->{}, me->{}", + channel.getInetSocketAddress(), msg.getNetworkId(), networkId); + sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION, msg.getTimestamp()); + logDisconnectReason(channel, DisconnectReason.DIFFERENT_VERSION); + channel.close(); + return; + } + sendHelloMsg(channel, DisconnectCode.NORMAL, msg.getTimestamp()); + } + channel.setFinishHandshake(true); + channel.updateAvgLatency(System.currentTimeMillis() - channel.getStartTime()); + Parameter.handlerList.forEach(h -> h.onConnect(channel)); + } + + private void sendHelloMsg(Channel channel, DisconnectCode code, long time) { + HelloMessage helloMessage = new HelloMessage(code, time); + channel.send(helloMessage); + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java new file mode 100644 index 00000000000..19eea3b015a --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java @@ -0,0 +1,70 @@ +package org.tron.p2p.connection.business.keepalive; + +import static org.tron.p2p.base.Parameter.KEEP_ALIVE_TIMEOUT; +import static org.tron.p2p.base.Parameter.PING_TIMEOUT; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.connection.business.MessageProcess; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.message.keepalive.PingMessage; +import org.tron.p2p.connection.message.keepalive.PongMessage; +import org.tron.p2p.protos.Connect.DisconnectReason; + +@Slf4j(topic = "net") +public class KeepAliveService implements MessageProcess { + + private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("keepAlive").build()); + + public void init() { + executor.scheduleWithFixedDelay(() -> { + try { + long now = System.currentTimeMillis(); + ChannelManager.getChannels().values().stream() + .filter(p -> !p.isDisconnect()) + .forEach(p -> { + if (p.waitForPong) { + if (now - p.pingSent > KEEP_ALIVE_TIMEOUT) { + p.send(new P2pDisconnectMessage(DisconnectReason.PING_TIMEOUT)); + p.close(); + } + } else { + if (now - p.getLastSendTime() > PING_TIMEOUT && p.isFinishHandshake()) { + p.send(new PingMessage()); + p.waitForPong = true; + p.pingSent = now; + } + } + }); + } catch (Exception t) { + log.error("Exception in keep alive task", t); + } + }, 2, 2, TimeUnit.SECONDS); + } + + public void close() { + executor.shutdown(); + } + + @Override + public void processMessage(Channel channel, Message message) { + switch (message.getType()) { + case KEEP_ALIVE_PING: + channel.send(new PongMessage()); + break; + case KEEP_ALIVE_PONG: + channel.updateAvgLatency(System.currentTimeMillis() - channel.pingSent); + channel.waitForPong = false; + break; + default: + break; + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java new file mode 100644 index 00000000000..81523d6ed16 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java @@ -0,0 +1,339 @@ +package org.tron.p2p.connection.business.pool; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.bouncycastle.util.encoders.Hex; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pEventHandler; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.socket.PeerClient; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.NodeManager; +import org.tron.p2p.dns.DnsManager; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.protos.Connect.DisconnectReason; +import org.tron.p2p.utils.CollectionUtils; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class ConnPoolService extends P2pEventHandler { + + private final List activePeers = Collections.synchronizedList(new ArrayList<>()); + private final Cache peerClientCache = CacheBuilder.newBuilder() + .maximumSize(1000).expireAfterWrite(120, TimeUnit.SECONDS).recordStats().build(); + @Getter + private final AtomicInteger passivePeersCount = new AtomicInteger(0); + @Getter + private final AtomicInteger activePeersCount = new AtomicInteger(0); + @Getter + private final AtomicInteger connectingPeersCount = new AtomicInteger(0); + private final ScheduledThreadPoolExecutor poolLoopExecutor = new ScheduledThreadPoolExecutor(1, + BasicThreadFactory.builder().namingPattern("connPool").build()); + private final ScheduledExecutorService disconnectExecutor = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("randomDisconnect").build()); + + public P2pConfig p2pConfig = Parameter.p2pConfig; + private PeerClient peerClient; + private final List configActiveNodes = new ArrayList<>(); + + public ConnPoolService() { + this.messageTypes = new HashSet<>(); //no message type registers + try { + Parameter.addP2pEventHandle(this); + configActiveNodes.addAll(p2pConfig.getActiveNodes()); + } catch (P2pException e) { + //no exception will throw + } + } + + public void init(PeerClient peerClient) { + this.peerClient = peerClient; + poolLoopExecutor.scheduleWithFixedDelay(() -> { + try { + connect(false); + } catch (Exception t) { + log.error("Exception in poolLoopExecutor worker", t); + } + }, 200, 3600, TimeUnit.MILLISECONDS); + + if (p2pConfig.isDisconnectionPolicyEnable()) { + disconnectExecutor.scheduleWithFixedDelay(() -> { + try { + check(); + } catch (Exception t) { + log.error("Exception in disconnectExecutor worker", t); + } + }, 30, 30, TimeUnit.SECONDS); + } + } + + private void addNode(Set inetSet, Node node) { + if (node != null) { + if (node.getInetSocketAddressV4() != null) { + inetSet.add(node.getInetSocketAddressV4()); + } + if (node.getInetSocketAddressV6() != null) { + inetSet.add(node.getInetSocketAddressV6()); + } + } + } + + private void connect(boolean isFilterActiveNodes) { + List connectNodes = new ArrayList<>(); + + //collect already used nodes in channelManager + Set addressInUse = new HashSet<>(); + Set inetInUse = new HashSet<>(); + Set nodesInUse = new HashSet<>(); + nodesInUse.add(Hex.toHexString(p2pConfig.getNodeID())); + ChannelManager.getChannels().values().forEach(channel -> { + if (StringUtils.isNotEmpty(channel.getNodeId())) { + nodesInUse.add(channel.getNodeId()); + } + addressInUse.add(channel.getInetAddress()); + inetInUse.add(channel.getInetSocketAddress()); + addNode(inetInUse, channel.getNode()); + }); + + addNode(inetInUse, new Node(Parameter.p2pConfig.getNodeID(), Parameter.p2pConfig.getIp(), + Parameter.p2pConfig.getIpv6(), Parameter.p2pConfig.getPort())); + + p2pConfig.getActiveNodes().forEach(address -> { + if (!isFilterActiveNodes && !inetInUse.contains(address) && !addressInUse.contains( + address.getAddress())) { + addressInUse.add(address.getAddress()); + inetInUse.add(address); + Node node = new Node(address); //use a random NodeId for config activeNodes + if (node.getPreferInetSocketAddress() != null) { + connectNodes.add(node); + } + } + }); + + //calculate lackSize exclude config activeNodes + int activeLackSize = p2pConfig.getMinActiveConnections() - connectingPeersCount.get(); + int size = Math.max( + p2pConfig.getMinConnections() - connectingPeersCount.get() - passivePeersCount.get(), + activeLackSize); + if (p2pConfig.getMinConnections() <= activePeers.size() && activeLackSize <= 0) { + size = 0; + } + int lackSize = size; + if (lackSize > 0) { + List connectableNodes = ChannelManager.getNodeDetectService().getConnectableNodes(); + for (Node node : connectableNodes) { + // nodesInUse and inetInUse don't change in method `validNode` + if (validNode(node, nodesInUse, inetInUse, null)) { + connectNodes.add(node); + nodesInUse.add(node.getHexId()); + inetInUse.add(node.getPreferInetSocketAddress()); + lackSize -= 1; + if (lackSize <= 0) { + break; + } + } + } + } + + if (lackSize > 0) { + List connectableNodes = NodeManager.getConnectableNodes(); + // nodesInUse and inetInUse don't change in method `getNodes` + List newNodes = getNodes(nodesInUse, inetInUse, connectableNodes, lackSize); + connectNodes.addAll(newNodes); + for (Node node : newNodes) { + nodesInUse.add(node.getHexId()); + inetInUse.add(node.getPreferInetSocketAddress()); + } + lackSize -= newNodes.size(); + } + + if (lackSize > 0 && !p2pConfig.getTreeUrls().isEmpty()) { + List dnsNodes = DnsManager.getDnsNodes(); + List filtered = new ArrayList<>(); + Collections.shuffle(dnsNodes); + for (DnsNode node : dnsNodes) { + if (validNode(node, nodesInUse, inetInUse, null)) { + DnsNode copyNode = (DnsNode) node.clone(); + copyNode.setId(NetUtil.getNodeId()); + //for node1 {ipv4_1, ipv6}, node2 {ipv4_2, ipv6}, we will not connect it twice + addNode(inetInUse, node); + filtered.add(copyNode); + } + } + List newNodes = CollectionUtils.truncate(filtered, lackSize); + connectNodes.addAll(newNodes); + } + + log.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}", + size, connectNodes.size(), isFilterActiveNodes); + //establish tcp connection with chose nodes by peerClient + { + connectNodes.forEach(n -> { + log.info("Connect to peer {}", n.getPreferInetSocketAddress()); + peerClient.connectAsync(n, false); + peerClientCache.put(n.getPreferInetSocketAddress().getAddress(), + System.currentTimeMillis()); + if (!configActiveNodes.contains(n.getPreferInetSocketAddress())) { + connectingPeersCount.incrementAndGet(); + } + }); + } + } + + public List getNodes(Set nodesInUse, Set inetInUse, + List connectableNodes, int limit) { + List filtered = new ArrayList<>(); + Set dynamicInetInUse = new HashSet<>(inetInUse); + for (Node node : connectableNodes) { + if (validNode(node, nodesInUse, inetInUse, dynamicInetInUse)) { + filtered.add((Node) node.clone()); + addNode(dynamicInetInUse, node); + } + } + + filtered.sort(Comparator.comparingLong(node -> -node.getUpdateTime())); + return CollectionUtils.truncate(filtered, limit); + } + + private boolean validNode(Node node, Set nodesInUse, Set inetInUse, + Set dynamicInet) { + long now = System.currentTimeMillis(); + InetSocketAddress inetSocketAddress = node.getPreferInetSocketAddress(); + InetAddress inetAddress = inetSocketAddress.getAddress(); + Long forbiddenTime = ChannelManager.getBannedNodes().getIfPresent(inetAddress); + if ((forbiddenTime != null && now <= forbiddenTime) + || (ChannelManager.getConnectionNum(inetAddress) + >= p2pConfig.getMaxConnectionsWithSameIp()) + || (node.getId() != null && nodesInUse.contains(node.getHexId())) + || (peerClientCache.getIfPresent(inetAddress) != null) + || inetInUse.contains(inetSocketAddress) + || (dynamicInet != null && dynamicInet.contains(inetSocketAddress))) { + return false; + } + return true; + } + + private void check() { + if (ChannelManager.getChannels().size() < p2pConfig.getMaxConnections()) { + return; + } + + List channels = new ArrayList<>(activePeers); + Collection peers = channels.stream() + .filter(peer -> !peer.isDisconnect()) + .filter(peer -> !peer.isTrustPeer()) + .filter(peer -> !peer.isActive()) + .collect(Collectors.toList()); + + // if len(peers) >= 0, disconnect randomly + if (!peers.isEmpty()) { + List list = new ArrayList<>(peers); + Channel peer = list.get(new Random().nextInt(peers.size())); + log.info("Disconnect with peer randomly: {}", peer); + peer.send(new P2pDisconnectMessage(DisconnectReason.RANDOM_ELIMINATION)); + peer.close(); + } + } + + private synchronized void logActivePeers() { + log.info("Peer stats: channels {}, activePeers {}, active {}, passive {}", + ChannelManager.getChannels().size(), activePeers.size(), activePeersCount.get(), + passivePeersCount.get()); + } + + public void triggerConnect(InetSocketAddress address) { + if (configActiveNodes.contains(address)) { + return; + } + connectingPeersCount.decrementAndGet(); + if (poolLoopExecutor.getQueue().size() >= Parameter.CONN_MAX_QUEUE_SIZE) { + log.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE); + return; + } + try { + if (!ChannelManager.isShutdown) { + poolLoopExecutor.submit(() -> { + try { + connect(true); + } catch (Exception t) { + log.error("Exception in poolLoopExecutor worker", t); + } + }); + } + } catch (Exception e) { + log.warn("Submit task failed, message:{}", e.getMessage()); + } + } + + @Override + public synchronized void onConnect(Channel peer) { + if (!activePeers.contains(peer)) { + if (!peer.isActive()) { + passivePeersCount.incrementAndGet(); + } else { + activePeersCount.incrementAndGet(); + } + activePeers.add(peer); + } + logActivePeers(); + } + + @Override + public synchronized void onDisconnect(Channel peer) { + if (activePeers.contains(peer)) { + if (!peer.isActive()) { + passivePeersCount.decrementAndGet(); + } else { + activePeersCount.decrementAndGet(); + } + activePeers.remove(peer); + } + logActivePeers(); + } + + @Override + public void onMessage(Channel channel, byte[] data) { + //do nothing + } + + public void close() { + List channels = new ArrayList<>(activePeers); + try { + channels.forEach(p -> { + if (!p.isDisconnect()) { + p.send(new P2pDisconnectMessage(DisconnectReason.PEER_QUITING)); + p.close(); + } + }); + poolLoopExecutor.shutdownNow(); + disconnectExecutor.shutdownNow(); + } catch (Exception e) { + log.warn("Problems shutting down executor", e); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java b/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java new file mode 100644 index 00000000000..c49247e59cf --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java @@ -0,0 +1,38 @@ +package org.tron.p2p.connection.business.upgrade; + +import com.google.protobuf.InvalidProtocolBufferException; +import java.io.IOException; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.exception.P2pException.TypeEnum; +import org.tron.p2p.protos.Connect.CompressMessage; + +import org.tron.p2p.utils.ProtoUtil; + +public class UpgradeController { + + public static byte[] codeSendData(int version, byte[] data) throws IOException { + if (!supportCompress(version)) { + return data; + } + return ProtoUtil.compressMessage(data).toByteArray(); + } + + public static byte[] decodeReceiveData(int version, byte[] data) throws P2pException, IOException { + if (!supportCompress(version)) { + return data; + } + CompressMessage compressMessage; + try { + compressMessage = CompressMessage.parseFrom(data); + } catch (InvalidProtocolBufferException e) { + throw new P2pException(TypeEnum.PARSE_MESSAGE_FAILED, e); + } + return ProtoUtil.uncompressMessage(compressMessage); + } + + private static boolean supportCompress(int version) { + return Parameter.version >= 1 && version >= 1; + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/Message.java b/p2p/src/main/java/org/tron/p2p/connection/message/Message.java new file mode 100644 index 00000000000..38a860a54a1 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/Message.java @@ -0,0 +1,78 @@ +package org.tron.p2p.connection.message; + +import org.apache.commons.lang3.ArrayUtils; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.message.detect.StatusMessage; +import org.tron.p2p.connection.message.handshake.HelloMessage; +import org.tron.p2p.connection.message.keepalive.PingMessage; +import org.tron.p2p.connection.message.keepalive.PongMessage; +import org.tron.p2p.exception.P2pException; + +public abstract class Message { + + protected MessageType type; + protected byte[] data; + + public Message(MessageType type, byte[] data) { + this.type = type; + this.data = data; + } + + public MessageType getType() { + return this.type; + } + + public byte[] getData() { + return this.data; + } + + public byte[] getSendData() { + return ArrayUtils.add(this.data, 0, type.getType()); + } + + public abstract boolean valid(); + + public boolean needToLog() { + return type.equals(MessageType.DISCONNECT) || type.equals(MessageType.HANDSHAKE_HELLO); + } + + public static Message parse(byte[] encode) throws P2pException { + byte type = encode[0]; + try { + byte[] data = ArrayUtils.subarray(encode, 1, encode.length); + Message message; + switch (MessageType.fromByte(type)) { + case KEEP_ALIVE_PING: + message = new PingMessage(data); + break; + case KEEP_ALIVE_PONG: + message = new PongMessage(data); + break; + case HANDSHAKE_HELLO: + message = new HelloMessage(data); + break; + case STATUS: + message = new StatusMessage(data); + break; + case DISCONNECT: + message = new P2pDisconnectMessage(data); + break; + default: + throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type=" + type); + } + if (!message.valid()) { + throw new P2pException(P2pException.TypeEnum.BAD_MESSAGE, "type=" + type); + } + return message; + } catch (P2pException p2pException) { + throw p2pException; + } catch (Exception e) { + throw new P2pException(P2pException.TypeEnum.BAD_MESSAGE, "type:" + type); + } + } + + @Override + public String toString() { + return "type: " + getType() + ", "; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java b/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java new file mode 100644 index 00000000000..8109b18690a --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java @@ -0,0 +1,41 @@ +package org.tron.p2p.connection.message; + +import java.util.HashMap; +import java.util.Map; + +public enum MessageType { + + KEEP_ALIVE_PING((byte) 0xff), + + KEEP_ALIVE_PONG((byte) 0xfe), + + HANDSHAKE_HELLO((byte) 0xfd), + + STATUS((byte) 0xfc), + + DISCONNECT((byte) 0xfb), + + UNKNOWN((byte) 0x80); + + private final byte type; + + MessageType(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + + private static final Map map = new HashMap<>(); + + static { + for (MessageType value : values()) { + map.put(value.type, value); + } + } + public static MessageType fromByte(byte type) { + MessageType typeEnum = map.get(type); + return typeEnum == null ? UNKNOWN : typeEnum; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java new file mode 100644 index 00000000000..28460676dd4 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java @@ -0,0 +1,39 @@ +package org.tron.p2p.connection.message.base; + +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.protos.Connect; +import org.tron.p2p.protos.Connect.DisconnectReason; + + +public class P2pDisconnectMessage extends Message { + + private Connect.P2pDisconnectMessage p2pDisconnectMessage; + + public P2pDisconnectMessage(byte[] data) throws Exception { + super(MessageType.DISCONNECT, data); + this.p2pDisconnectMessage = Connect.P2pDisconnectMessage.parseFrom(data); + } + + public P2pDisconnectMessage(DisconnectReason disconnectReason) { + super(MessageType.DISCONNECT, null); + this.p2pDisconnectMessage = Connect.P2pDisconnectMessage.newBuilder() + .setReason(disconnectReason).build(); + this.data = p2pDisconnectMessage.toByteArray(); + } + + private DisconnectReason getReason() { + return p2pDisconnectMessage.getReason(); + } + + @Override + public boolean valid() { + return true; + } + + @Override + public String toString() { + return new StringBuilder().append(super.toString()).append("reason: ") + .append(getReason()).toString(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java new file mode 100644 index 00000000000..3cadb4620dc --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java @@ -0,0 +1,61 @@ +package org.tron.p2p.connection.message.detect; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.discover.Node; +import org.tron.p2p.protos.Connect; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.utils.NetUtil; + +public class StatusMessage extends Message { + private Connect.StatusMessage statusMessage; + + public StatusMessage(byte[] data) throws Exception { + super(MessageType.STATUS, data); + this.statusMessage = Connect.StatusMessage.parseFrom(data); + } + + public StatusMessage() { + super(MessageType.STATUS, null); + Discover.Endpoint endpoint = Parameter.getHomeNode(); + this.statusMessage = Connect.StatusMessage.newBuilder() + .setFrom(endpoint) + .setMaxConnections(Parameter.p2pConfig.getMaxConnections()) + .setCurrentConnections(ChannelManager.getChannels().size()) + .setNetworkId(Parameter.p2pConfig.getNetworkId()) + .setTimestamp(System.currentTimeMillis()).build(); + this.data = statusMessage.toByteArray(); + } + + public int getNetworkId() { + return this.statusMessage.getNetworkId(); + } + + public int getVersion() { + return this.statusMessage.getVersion(); + } + + public int getRemainConnections() { + return this.statusMessage.getMaxConnections() - this.statusMessage.getCurrentConnections(); + } + + public long getTimestamp() { + return this.statusMessage.getTimestamp(); + } + + public Node getFrom() { + return NetUtil.getNode(statusMessage.getFrom()); + } + + @Override + public String toString() { + return "[StatusMessage: " + statusMessage; + } + + @Override + public boolean valid() { + return NetUtil.validNode(getFrom()); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java new file mode 100644 index 00000000000..a3727d2d8c1 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java @@ -0,0 +1,77 @@ +package org.tron.p2p.connection.message.handshake; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.handshake.DisconnectCode; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.discover.Node; +import org.tron.p2p.protos.Connect; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.utils.ByteArray; +import org.tron.p2p.utils.NetUtil; + +public class HelloMessage extends Message { + + private Connect.HelloMessage helloMessage; + + public HelloMessage(byte[] data) throws Exception { + super(MessageType.HANDSHAKE_HELLO, data); + this.helloMessage = Connect.HelloMessage.parseFrom(data); + } + + public HelloMessage(DisconnectCode code, long time) { + super(MessageType.HANDSHAKE_HELLO, null); + Discover.Endpoint endpoint = Parameter.getHomeNode(); + this.helloMessage = Connect.HelloMessage.newBuilder() + .setFrom(endpoint) + .setNetworkId(Parameter.p2pConfig.getNetworkId()) + .setCode(code.getValue()) + .setVersion(Parameter.version) + .setTimestamp(time).build(); + this.data = helloMessage.toByteArray(); + } + + public int getNetworkId() { + return this.helloMessage.getNetworkId(); + } + + public int getVersion() { + return this.helloMessage.getVersion(); + } + + public int getCode() { + return this.helloMessage.getCode(); + } + + public long getTimestamp() { + return this.helloMessage.getTimestamp(); + } + + public Node getFrom() { + return NetUtil.getNode(helloMessage.getFrom()); + } + + @Override + public String toString() { + return "[HelloMessage: " + format(); + } + + @Override + public boolean valid() { + return NetUtil.validNode(getFrom()); + } + + public String format() { + String[] lines = helloMessage.toString().split("\n"); + StringBuilder sb = new StringBuilder(); + for (String line : lines) { + if (line.contains("nodeId")) { + String nodeId = ByteArray.toHexString(helloMessage.getFrom().getNodeId().toByteArray()); + line = " nodeId: \"" + nodeId + "\""; + } + sb.append(line).append("\n"); + } + return sb.toString(); + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java new file mode 100644 index 00000000000..8191b5e72f1 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java @@ -0,0 +1,33 @@ +package org.tron.p2p.connection.message.keepalive; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.protos.Connect; + +public class PingMessage extends Message { + + private Connect.KeepAliveMessage keepAliveMessage; + + public PingMessage(byte[] data) throws Exception { + super(MessageType.KEEP_ALIVE_PING, data); + this.keepAliveMessage = Connect.KeepAliveMessage.parseFrom(data); + } + + public PingMessage() { + super(MessageType.KEEP_ALIVE_PING, null); + this.keepAliveMessage = Connect.KeepAliveMessage.newBuilder() + .setTimestamp(System.currentTimeMillis()).build(); + this.data = this.keepAliveMessage.toByteArray(); + } + + public long getTimeStamp() { + return this.keepAliveMessage.getTimestamp(); + } + + @Override + public boolean valid() { + return getTimeStamp() > 0 + && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java new file mode 100644 index 00000000000..b3689bea93b --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java @@ -0,0 +1,33 @@ +package org.tron.p2p.connection.message.keepalive; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.protos.Connect; + +public class PongMessage extends Message { + + private Connect.KeepAliveMessage keepAliveMessage; + + public PongMessage(byte[] data) throws Exception { + super(MessageType.KEEP_ALIVE_PONG, data); + this.keepAliveMessage = Connect.KeepAliveMessage.parseFrom(data); + } + + public PongMessage() { + super(MessageType.KEEP_ALIVE_PONG, null); + this.keepAliveMessage = Connect.KeepAliveMessage.newBuilder() + .setTimestamp(System.currentTimeMillis()).build(); + this.data = this.keepAliveMessage.toByteArray(); + } + + public long getTimeStamp() { + return this.keepAliveMessage.getTimestamp(); + } + + @Override + public boolean valid() { + return getTimeStamp() > 0 + && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java new file mode 100644 index 00000000000..251b9a5590b --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java @@ -0,0 +1,90 @@ +package org.tron.p2p.connection.socket; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.connection.business.upgrade.UpgradeController; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.connection.message.detect.StatusMessage; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.protos.Connect.DisconnectReason; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class MessageHandler extends ByteToMessageDecoder { + + private final Channel channel; + + public MessageHandler(Channel channel) { + this.channel = channel; + } + + @Override + public void handlerAdded(ChannelHandlerContext ctx) { + } + + @Override + public void channelActive(ChannelHandlerContext ctx) { + log.debug("Channel active, {}", ctx.channel().remoteAddress()); + channel.setChannelHandlerContext(ctx); + if (channel.isActive()) { + if (channel.isDiscoveryMode()) { + channel.send(new StatusMessage()); + } else { + ChannelManager.getHandshakeService().startHandshake(channel); + } + } + } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List out) { + byte[] data = new byte[buffer.readableBytes()]; + buffer.readBytes(data); + try { + if (channel.isFinishHandshake()) { + data = UpgradeController.decodeReceiveData(channel.getVersion(), data); + } + ChannelManager.processMessage(channel, data); + } catch (Exception e) { + if (e instanceof P2pException) { + P2pException pe = (P2pException) e; + DisconnectReason disconnectReason; + switch (pe.getType()) { + case EMPTY_MESSAGE: + disconnectReason = DisconnectReason.EMPTY_MESSAGE; + break; + case BAD_PROTOCOL: + disconnectReason = DisconnectReason.BAD_PROTOCOL; + break; + case NO_SUCH_MESSAGE: + disconnectReason = DisconnectReason.NO_SUCH_MESSAGE; + break; + case BAD_MESSAGE: + case PARSE_MESSAGE_FAILED: + case MESSAGE_WITH_WRONG_LENGTH: + case TYPE_ALREADY_REGISTERED: + disconnectReason = DisconnectReason.BAD_MESSAGE; + break; + default: + disconnectReason = DisconnectReason.UNKNOWN; + } + channel.send(new P2pDisconnectMessage(disconnectReason)); + } + channel.processException(e); + } catch (Throwable t) { + log.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(), + ByteArray.toHexString(data)); + throw t; + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + channel.processException(cause); + } + +} \ No newline at end of file diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java new file mode 100644 index 00000000000..1bac43f11b9 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java @@ -0,0 +1,60 @@ +package org.tron.p2p.connection.socket; + +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.FixedRecvByteBufAllocator; +import io.netty.channel.socket.nio.NioSocketChannel; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.ChannelManager; + +@Slf4j(topic = "net") +public class P2pChannelInitializer extends ChannelInitializer { + + private final String remoteId; + + private boolean peerDiscoveryMode = false; //only be true when channel is activated by detect service + + private boolean trigger = true; + public P2pChannelInitializer(String remoteId, boolean peerDiscoveryMode, boolean trigger) { + this.remoteId = remoteId; + this.peerDiscoveryMode = peerDiscoveryMode; + this.trigger = trigger; + } + + @Override + public void initChannel(NioSocketChannel ch) { + try { + final Channel channel = new Channel(); + channel.init(ch.pipeline(), remoteId, peerDiscoveryMode); + + // limit the size of receiving buffer to 1024 + ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024)); + ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024); + ch.config().setOption(ChannelOption.SO_BACKLOG, 1024); + + // be aware of channel closing + ch.closeFuture().addListener((ChannelFutureListener) future -> { + channel.setDisconnect(true); + if (channel.isDiscoveryMode()) { + ChannelManager.getNodeDetectService().notifyDisconnect(channel); + } else { + try { + log.info("Close channel:{}", channel.getInetSocketAddress()); + ChannelManager.notifyDisconnect(channel); + } finally { + if (channel.getInetSocketAddress() != null && channel.isActive() && trigger) { + ChannelManager.triggerConnect(channel.getInetSocketAddress()); + } + } + } + }); + + } catch (Exception e) { + log.error("Unexpected initChannel error", e); + } + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java new file mode 100644 index 00000000000..6e04b1d2be7 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java @@ -0,0 +1,98 @@ +package org.tron.p2p.connection.socket; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.CorruptedFrameException; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.connection.message.base.P2pDisconnectMessage; +import org.tron.p2p.protos.Connect.DisconnectReason; + +@Slf4j(topic = "net") +public class P2pProtobufVarint32FrameDecoder extends ByteToMessageDecoder { + + private final Channel channel; + + public P2pProtobufVarint32FrameDecoder(Channel channel) { + this.channel = channel; + } + + private static int readRawVarint32(ByteBuf buffer) { + if (!buffer.isReadable()) { + return 0; + } + buffer.markReaderIndex(); + byte tmp = buffer.readByte(); + if (tmp >= 0) { + return tmp; + } else { + int result = tmp & 127; + if (!buffer.isReadable()) { + buffer.resetReaderIndex(); + return 0; + } + if ((tmp = buffer.readByte()) >= 0) { + result |= tmp << 7; + } else { + result |= (tmp & 127) << 7; + if (!buffer.isReadable()) { + buffer.resetReaderIndex(); + return 0; + } + if ((tmp = buffer.readByte()) >= 0) { + result |= tmp << 14; + } else { + result |= (tmp & 127) << 14; + if (!buffer.isReadable()) { + buffer.resetReaderIndex(); + return 0; + } + if ((tmp = buffer.readByte()) >= 0) { + result |= tmp << 21; + } else { + result |= (tmp & 127) << 21; + if (!buffer.isReadable()) { + buffer.resetReaderIndex(); + return 0; + } + result |= (tmp = buffer.readByte()) << 28; + if (tmp < 0) { + throw new CorruptedFrameException("malformed varint."); + } + } + } + } + return result; + } + } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { + in.markReaderIndex(); + int preIndex = in.readerIndex(); + int length = readRawVarint32(in); + if (length >= Parameter.MAX_MESSAGE_LENGTH) { + log.warn("Receive a big msg or not encoded msg, host : {}, msg length is : {}", + ctx.channel().remoteAddress(), length); + in.clear(); + channel.send(new P2pDisconnectMessage(DisconnectReason.BAD_MESSAGE)); + channel.close(); + return; + } + if (preIndex == in.readerIndex()) { + return; + } + if (length < 0) { + throw new CorruptedFrameException("negative length: " + length); + } + + if (in.readableBytes() < length) { + in.resetReaderIndex(); + } else { + out.add(in.readRetainedSlice(length)); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java new file mode 100644 index 00000000000..2f0bd943a41 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java @@ -0,0 +1,103 @@ +package org.tron.p2p.connection.socket; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelOption; +import io.netty.channel.DefaultMessageSizeEstimator; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.bouncycastle.util.encoders.Hex; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.ChannelManager; +import org.tron.p2p.discover.Node; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class PeerClient { + + private EventLoopGroup workerGroup; + + public void init() { + workerGroup = new NioEventLoopGroup(0, + BasicThreadFactory.builder().namingPattern("peerClient-%d").build()); + } + + public void close() { + workerGroup.shutdownGracefully(); + workerGroup.terminationFuture().syncUninterruptibly(); + } + + public void connect(String host, int port, String remoteId) { + try { + ChannelFuture f = connectAsync(host, port, remoteId, false, false); + if (f != null) { + f.sync().channel().closeFuture().sync(); + } + } catch (Exception e) { + log.warn("PeerClient can't connect to {}:{} ({})", host, port, e.getMessage()); + } + } + + public ChannelFuture connect(Node node, ChannelFutureListener future) { + ChannelFuture channelFuture = connectAsync( + node.getPreferInetSocketAddress().getAddress().getHostAddress(), + node.getPort(), + node.getId() == null ? Hex.toHexString(NetUtil.getNodeId()) : node.getHexId(), false, + false); + if (ChannelManager.isShutdown) { + return null; + } + if (channelFuture != null && future != null) { + channelFuture.addListener(future); + } + return channelFuture; + } + + public ChannelFuture connectAsync(Node node, boolean discoveryMode) { + ChannelFuture channelFuture = + connectAsync(node.getPreferInetSocketAddress().getAddress().getHostAddress(), + node.getPort(), + node.getId() == null ? Hex.toHexString(NetUtil.getNodeId()) : node.getHexId(), + discoveryMode, true); + if (ChannelManager.isShutdown) { + return null; + } + if (channelFuture != null) { + channelFuture.addListener((ChannelFutureListener) future -> { + if (!future.isSuccess()) { + log.warn("Connect to peer {} fail, cause:{}", node.getPreferInetSocketAddress(), + future.cause().getMessage()); + future.channel().close(); + if (!discoveryMode) { + ChannelManager.triggerConnect(node.getPreferInetSocketAddress()); + } + } + }); + } + return channelFuture; + } + + private ChannelFuture connectAsync(String host, int port, String remoteId, + boolean discoveryMode, boolean trigger) { + + P2pChannelInitializer p2pChannelInitializer = new P2pChannelInitializer(remoteId, + discoveryMode, trigger); + + Bootstrap b = new Bootstrap(); + b.group(workerGroup); + b.channel(NioSocketChannel.class); + b.option(ChannelOption.SO_KEEPALIVE, true); + b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Parameter.NODE_CONNECTION_TIMEOUT); + b.remoteAddress(host, port); + b.handler(p2pChannelInitializer); + if (ChannelManager.isShutdown) { + return null; + } + return b.connect(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java new file mode 100644 index 00000000000..8a1b7d9adf2 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java @@ -0,0 +1,80 @@ +package org.tron.p2p.connection.socket; + + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.DefaultMessageSizeEstimator; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LoggingHandler; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; + +@Slf4j(topic = "net") +public class PeerServer { + + private ChannelFuture channelFuture; + private boolean listening; + + public void init() { + int port = Parameter.p2pConfig.getPort(); + if (port > 0) { + new Thread(() -> start(port), "PeerServer").start(); + } + } + + public void close() { + if (listening && channelFuture != null && channelFuture.channel().isOpen()) { + try { + log.info("Closing TCP server..."); + channelFuture.channel().close().sync(); + } catch (Exception e) { + log.warn("Closing TCP server failed.", e); + } + } + } + + public void start(int port) { + EventLoopGroup bossGroup = new NioEventLoopGroup(1, + BasicThreadFactory.builder().namingPattern("peerBoss").build()); + //if threads = 0, it is number of core * 2 + EventLoopGroup workerGroup = new NioEventLoopGroup(Parameter.TCP_NETTY_WORK_THREAD_NUM, + BasicThreadFactory.builder().namingPattern("peerWorker-%d").build()); + P2pChannelInitializer p2pChannelInitializer = new P2pChannelInitializer("", false, true); + try { + ServerBootstrap b = new ServerBootstrap(); + + b.group(bossGroup, workerGroup); + b.channel(NioServerSocketChannel.class); + + b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Parameter.NODE_CONNECTION_TIMEOUT); + + b.handler(new LoggingHandler()); + b.childHandler(p2pChannelInitializer); + + // Start the client. + log.info("TCP listener started, bind port {}", port); + + channelFuture = b.bind(port).sync(); + + listening = true; + + // Wait until the connection is closed. + channelFuture.channel().closeFuture().sync(); + + log.info("TCP listener closed"); + + } catch (Exception e) { + log.error("Start TCP server failed", e); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + listening = false; + } + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java b/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java new file mode 100644 index 00000000000..fee40123c83 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java @@ -0,0 +1,26 @@ +package org.tron.p2p.discover; + +import org.tron.p2p.discover.socket.EventHandler; +import org.tron.p2p.discover.socket.UdpEvent; + +import java.util.List; + +public interface DiscoverService extends EventHandler { + + void init(); + + void close(); + + List getConnectableNodes(); + + List getTableNodes(); + + List getAllNodes(); + + Node getPublicHomeNode(); + + void channelActivated(); + + void handleEvent(UdpEvent event); + +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/Node.java b/p2p/src/main/java/org/tron/p2p/discover/Node.java new file mode 100644 index 00000000000..51156d2f462 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/Node.java @@ -0,0 +1,187 @@ +package org.tron.p2p.discover; + +import java.io.Serializable; +import java.net.Inet4Address; +import java.net.InetSocketAddress; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.bouncycastle.util.encoders.Hex; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class Node implements Serializable, Cloneable { + + private static final long serialVersionUID = -4267600517925770636L; + + @Setter + @Getter + private byte[] id; + + @Getter + protected String hostV4; + + @Getter + protected String hostV6; + + @Setter + @Getter + protected int port; + + @Setter + private int bindPort; + + @Setter + private int p2pVersion; + + @Getter + private long updateTime; + + public Node(InetSocketAddress address) { + this.id = NetUtil.getNodeId(); + if (address.getAddress() instanceof Inet4Address) { + this.hostV4 = address.getAddress().getHostAddress(); + } else { + this.hostV6 = address.getAddress().getHostAddress(); + } + this.port = address.getPort(); + this.bindPort = port; + this.updateTime = System.currentTimeMillis(); + formatHostV6(); + } + + public Node(byte[] id, String hostV4, String hostV6, int port) { + this.id = id; + this.hostV4 = hostV4; + this.hostV6 = hostV6; + this.port = port; + this.bindPort = port; + this.updateTime = System.currentTimeMillis(); + formatHostV6(); + } + + public Node(byte[] id, String hostV4, String hostV6, int port, int bindPort) { + this.id = id; + this.hostV4 = hostV4; + this.hostV6 = hostV6; + this.port = port; + this.bindPort = bindPort; + this.updateTime = System.currentTimeMillis(); + formatHostV6(); + } + + public void updateHostV4(String hostV4) { + if (StringUtils.isEmpty(this.hostV4) && StringUtils.isNotEmpty(hostV4)) { + log.info("update hostV4:{} with hostV6:{}", hostV4, this.hostV6); + this.hostV4 = hostV4; + } + } + + public void updateHostV6(String hostV6) { + if (StringUtils.isEmpty(this.hostV6) && StringUtils.isNotEmpty(hostV6)) { + log.info("update hostV6:{} with hostV4:{}", hostV6, this.hostV4); + this.hostV6 = hostV6; + } + } + + //use standard ipv6 format + private void formatHostV6() { + if (StringUtils.isNotEmpty(this.hostV6)) { + this.hostV6 = new InetSocketAddress(hostV6, port).getAddress().getHostAddress(); + } + } + + public boolean isConnectible(int argsP2PVersion) { + return port == bindPort && p2pVersion == argsP2PVersion; + } + + public InetSocketAddress getPreferInetSocketAddress() { + if (StringUtils.isNotEmpty(hostV4) && StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) { + return getInetSocketAddressV4(); + } else if (StringUtils.isNotEmpty(hostV6) && StringUtils.isNotEmpty( + Parameter.p2pConfig.getIpv6())) { + return getInetSocketAddressV6(); + } else { + return null; + } + } + + public String getHexId() { + return id == null ? null : Hex.toHexString(id); + } + + public String getHexIdShort() { + return getIdShort(getHexId()); + } + + public String getHostKey() { + return getPreferInetSocketAddress().getAddress().getHostAddress(); + } + + public String getIdString() { + if (id == null) { + return null; + } + return new String(id); + } + + public void touch() { + updateTime = System.currentTimeMillis(); + } + + @Override + public String toString() { + return "Node{" + " hostV4='" + hostV4 + '\'' + ", hostV6='" + hostV6 + '\'' + ", port=" + port + + ", id=\'" + (id == null ? "null" : Hex.toHexString(id)) + "\'}"; + } + + public String format() { + return "Node{" + " hostV4='" + hostV4 + '\'' + ", hostV6='" + hostV6 + '\'' + ", port=" + port + + '}'; + } + + @Override + public int hashCode() { + return this.format().hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o == null) { + return false; + } + + if (o == this) { + return true; + } + + if (o.getClass() == getClass()) { + return StringUtils.equals(getIdString(), ((Node) o).getIdString()); + } + + return false; + } + + private String getIdShort(String hexId) { + return hexId == null ? "" : hexId.substring(0, 8); + } + + public InetSocketAddress getInetSocketAddressV4() { + return StringUtils.isNotEmpty(hostV4) ? new InetSocketAddress(hostV4, port) : null; + } + + public InetSocketAddress getInetSocketAddressV6() { + return StringUtils.isNotEmpty(hostV6) ? new InetSocketAddress(hostV6, port) : null; + } + + @Override + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException ignored) { + } + return null; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java b/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java new file mode 100644 index 00000000000..e995ff6bb8e --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java @@ -0,0 +1,47 @@ +package org.tron.p2p.discover; + +import java.util.List; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.protocol.kad.KadService; +import org.tron.p2p.discover.socket.DiscoverServer; + +public class NodeManager { + + private static DiscoverService discoverService; + private static DiscoverServer discoverServer; + + public static void init() { + discoverService = new KadService(); + discoverService.init(); + if (Parameter.p2pConfig.isDiscoverEnable()) { + discoverServer = new DiscoverServer(); + discoverServer.init(discoverService); + } + } + + public static void close() { + if (discoverService != null) { + discoverService.close(); + } + if (discoverServer != null) { + discoverServer.close(); + } + } + + public static List getConnectableNodes() { + return discoverService.getConnectableNodes(); + } + + public static Node getHomeNode() { + return discoverService.getPublicHomeNode(); + } + + public static List getTableNodes() { + return discoverService.getTableNodes(); + } + + public static List getAllNodes() { + return discoverService.getAllNodes(); + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/Message.java b/p2p/src/main/java/org/tron/p2p/discover/message/Message.java new file mode 100644 index 00000000000..592b7b133be --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/Message.java @@ -0,0 +1,69 @@ +package org.tron.p2p.discover.message; + +import org.apache.commons.lang3.ArrayUtils; +import org.tron.p2p.discover.message.kad.FindNodeMessage; +import org.tron.p2p.discover.message.kad.NeighborsMessage; +import org.tron.p2p.discover.message.kad.PingMessage; +import org.tron.p2p.discover.message.kad.PongMessage; +import org.tron.p2p.exception.P2pException; + +public abstract class Message { + protected MessageType type; + protected byte[] data; + + protected Message(MessageType type, byte[] data) { + this.type = type; + this.data = data; + } + + public static Message parse(byte[] encode) throws Exception { + byte type = encode[0]; + byte[] data = ArrayUtils.subarray(encode, 1, encode.length); + Message message; + switch (MessageType.fromByte(type)) { + case KAD_PING: + message = new PingMessage(data); + break; + case KAD_PONG: + message = new PongMessage(data); + break; + case KAD_FIND_NODE: + message = new FindNodeMessage(data); + break; + case KAD_NEIGHBORS: + message = new NeighborsMessage(data); + break; + default: + throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, "type=" + type); + } + if (!message.valid()) { + throw new P2pException(P2pException.TypeEnum.BAD_MESSAGE, "type=" + type); + } + return message; + } + + public MessageType getType() { + return this.type; + } + + public byte[] getData() { + return this.data; + } + + public byte[] getSendData() { + return ArrayUtils.add(this.data, 0, type.getType()); + } + + public abstract boolean valid(); + + @Override + public String toString() { + return "[Message Type: " + getType() + ", len: " + (data == null ? 0 : data.length) + "]"; + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java b/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java new file mode 100644 index 00000000000..29dd0ca9a0e --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java @@ -0,0 +1,40 @@ +package org.tron.p2p.discover.message; + +import java.util.HashMap; +import java.util.Map; + +public enum MessageType { + + KAD_PING((byte) 0x01), + + KAD_PONG((byte) 0x02), + + KAD_FIND_NODE((byte) 0x03), + + KAD_NEIGHBORS((byte) 0x04), + + UNKNOWN((byte) 0xFF); + + private final byte type; + + MessageType(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + + private static final Map map = new HashMap<>(); + + static { + for (MessageType value : values()) { + map.put(value.type, value); + } + } + public static MessageType fromByte(byte type) { + MessageType typeEnum = map.get(type); + return typeEnum == null ? UNKNOWN : typeEnum; + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java new file mode 100644 index 00000000000..d3f812ded0f --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java @@ -0,0 +1,55 @@ +package org.tron.p2p.discover.message.kad; + +import com.google.protobuf.ByteString; +import org.tron.p2p.base.Constant; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.MessageType; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.utils.NetUtil; + +public class FindNodeMessage extends KadMessage { + + private Discover.FindNeighbours findNeighbours; + + public FindNodeMessage(byte[] data) throws Exception { + super(MessageType.KAD_FIND_NODE, data); + this.findNeighbours = Discover.FindNeighbours.parseFrom(data); + } + + public FindNodeMessage(Node from, byte[] targetId) { + super(MessageType.KAD_FIND_NODE, null); + Endpoint fromEndpoint = getEndpointFromNode(from); + this.findNeighbours = Discover.FindNeighbours.newBuilder() + .setFrom(fromEndpoint) + .setTargetId(ByteString.copyFrom(targetId)) + .setTimestamp(System.currentTimeMillis()) + .build(); + this.data = this.findNeighbours.toByteArray(); + } + + public byte[] getTargetId() { + return this.findNeighbours.getTargetId().toByteArray(); + } + + @Override + public long getTimestamp() { + return this.findNeighbours.getTimestamp(); + } + + @Override + public Node getFrom() { + return NetUtil.getNode(findNeighbours.getFrom()); + } + + @Override + public String toString() { + return "[findNeighbours: " + findNeighbours; + } + + @Override + public boolean valid() { + return NetUtil.validNode(getFrom()) + && getTargetId().length == Constant.NODE_ID_LEN; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java new file mode 100644 index 00000000000..d4506db5b78 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java @@ -0,0 +1,35 @@ +package org.tron.p2p.discover.message.kad; + +import com.google.protobuf.ByteString; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.Message; +import org.tron.p2p.discover.message.MessageType; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.utils.ByteArray; + +public abstract class KadMessage extends Message { + + protected KadMessage(MessageType type, byte[] data) { + super(type, data); + } + + public abstract Node getFrom(); + + public abstract long getTimestamp(); + + public static Endpoint getEndpointFromNode(Node node) { + Endpoint.Builder builder = Endpoint.newBuilder() + .setPort(node.getPort()); + if (node.getId() != null) { + builder.setNodeId(ByteString.copyFrom(node.getId())); + } + if (StringUtils.isNotEmpty(node.getHostV4())) { + builder.setAddress(ByteString.copyFrom(ByteArray.fromString(node.getHostV4()))); + } + if (StringUtils.isNotEmpty(node.getHostV6())) { + builder.setAddressIpv6(ByteString.copyFrom(ByteArray.fromString(node.getHostV6()))); + } + return builder.build(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java new file mode 100644 index 00000000000..37fd4d67271 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java @@ -0,0 +1,80 @@ +package org.tron.p2p.discover.message.kad; + +import java.util.ArrayList; +import java.util.List; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.MessageType; +import org.tron.p2p.discover.protocol.kad.table.KademliaOptions; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.protos.Discover.Neighbours; +import org.tron.p2p.protos.Discover.Neighbours.Builder; +import org.tron.p2p.utils.NetUtil; + +public class NeighborsMessage extends KadMessage { + + private Discover.Neighbours neighbours; + + public NeighborsMessage(byte[] data) throws Exception { + super(MessageType.KAD_NEIGHBORS, data); + this.neighbours = Discover.Neighbours.parseFrom(data); + } + + public NeighborsMessage(Node from, List neighbours, long sequence) { + super(MessageType.KAD_NEIGHBORS, null); + Builder builder = Neighbours.newBuilder() + .setTimestamp(sequence); + + neighbours.forEach(neighbour -> { + Endpoint endpoint = getEndpointFromNode(neighbour); + builder.addNeighbours(endpoint); + }); + + Endpoint fromEndpoint = getEndpointFromNode(from); + + builder.setFrom(fromEndpoint); + + this.neighbours = builder.build(); + + this.data = this.neighbours.toByteArray(); + } + + public List getNodes() { + List nodes = new ArrayList<>(); + neighbours.getNeighboursList().forEach(n -> nodes.add(NetUtil.getNode(n))); + return nodes; + } + + @Override + public long getTimestamp() { + return this.neighbours.getTimestamp(); + } + + @Override + public Node getFrom() { + return NetUtil.getNode(neighbours.getFrom()); + } + + @Override + public String toString() { + return "[neighbours: " + neighbours; + } + + @Override + public boolean valid() { + if (!NetUtil.validNode(getFrom())) { + return false; + } + if (getNodes().size() > 0) { + if (getNodes().size() > KademliaOptions.BUCKET_SIZE) { + return false; + } + for (Node node : getNodes()) { + if (!NetUtil.validNode(node)) { + return false; + } + } + } + return true; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java new file mode 100644 index 00000000000..e1cca85aa79 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java @@ -0,0 +1,59 @@ +package org.tron.p2p.discover.message.kad; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.MessageType; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.utils.NetUtil; + +public class PingMessage extends KadMessage { + + private Discover.PingMessage pingMessage; + + public PingMessage(byte[] data) throws Exception { + super(MessageType.KAD_PING, data); + this.pingMessage = Discover.PingMessage.parseFrom(data); + } + + public PingMessage(Node from, Node to) { + super(MessageType.KAD_PING, null); + Endpoint fromEndpoint = getEndpointFromNode(from); + Endpoint toEndpoint = getEndpointFromNode(to); + this.pingMessage = Discover.PingMessage.newBuilder() + .setVersion(Parameter.p2pConfig.getNetworkId()) + .setFrom(fromEndpoint) + .setTo(toEndpoint) + .setTimestamp(System.currentTimeMillis()) + .build(); + this.data = this.pingMessage.toByteArray(); + } + + public int getNetworkId() { + return this.pingMessage.getVersion(); + } + + public Node getTo() { + return NetUtil.getNode(this.pingMessage.getTo()); + } + + @Override + public long getTimestamp() { + return this.pingMessage.getTimestamp(); + } + + @Override + public Node getFrom() { + return NetUtil.getNode(pingMessage.getFrom()); + } + + @Override + public String toString() { + return "[pingMessage: " + pingMessage; + } + + @Override + public boolean valid() { + return NetUtil.validNode(getFrom()); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java new file mode 100644 index 00000000000..8ae80fbb0f6 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java @@ -0,0 +1,53 @@ +package org.tron.p2p.discover.message.kad; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.MessageType; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.utils.NetUtil; + +public class PongMessage extends KadMessage { + + private Discover.PongMessage pongMessage; + + public PongMessage(byte[] data) throws Exception { + super(MessageType.KAD_PONG, data); + this.pongMessage = Discover.PongMessage.parseFrom(data); + } + + public PongMessage(Node from) { + super(MessageType.KAD_PONG, null); + Endpoint toEndpoint = getEndpointFromNode(from); + this.pongMessage = Discover.PongMessage.newBuilder() + .setFrom(toEndpoint) + .setEcho(Parameter.p2pConfig.getNetworkId()) + .setTimestamp(System.currentTimeMillis()) + .build(); + this.data = this.pongMessage.toByteArray(); + } + + public int getNetworkId() { + return this.pongMessage.getEcho(); + } + + @Override + public long getTimestamp() { + return this.pongMessage.getTimestamp(); + } + + @Override + public Node getFrom() { + return NetUtil.getNode(pongMessage.getFrom()); + } + + @Override + public String toString() { + return "[pongMessage: " + pongMessage; + } + + @Override + public boolean valid() { + return NetUtil.validNode(getFrom()); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java new file mode 100644 index 00000000000..4ab23ec307c --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java @@ -0,0 +1,87 @@ +package org.tron.p2p.discover.protocol.kad; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.protocol.kad.table.KademliaOptions; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class DiscoverTask { + + private ScheduledExecutorService discoverer = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("discoverTask").build()); + + private KadService kadService; + + private int loopNum = 0; + private byte[] nodeId; + + public DiscoverTask(KadService kadService) { + this.kadService = kadService; + } + + public void init() { + discoverer.scheduleWithFixedDelay(() -> { + try { + loopNum++; + if (loopNum % KademliaOptions.MAX_LOOP_NUM == 0) { + loopNum = 0; + nodeId = kadService.getPublicHomeNode().getId(); + } else { + nodeId = NetUtil.getNodeId(); + } + discover(nodeId, 0, new ArrayList<>()); + } catch (Exception e) { + log.error("DiscoverTask fails to be executed", e); + } + }, 1, KademliaOptions.DISCOVER_CYCLE, TimeUnit.MILLISECONDS); + log.debug("DiscoverTask started"); + } + + private void discover(byte[] nodeId, int round, List prevTriedNodes) { + + List closest = kadService.getTable().getClosestNodes(nodeId); + List tried = new ArrayList<>(); + for (Node n : closest) { + if (!tried.contains(n) && !prevTriedNodes.contains(n)) { + try { + kadService.getNodeHandler(n).sendFindNode(nodeId); + tried.add(n); + } catch (Exception e) { + log.error("Unexpected Exception occurred while sending FindNodeMessage", e); + } + } + + if (tried.size() == KademliaOptions.ALPHA) { + break; + } + } + + try { + Thread.sleep(KademliaOptions.WAIT_TIME); + } catch (InterruptedException e) { + log.warn("Discover task interrupted"); + Thread.currentThread().interrupt(); + } + + if (tried.isEmpty()) { + return; + } + + if (++round == KademliaOptions.MAX_STEPS) { + return; + } + tried.addAll(prevTriedNodes); + discover(nodeId, round, tried); + } + + public void close() { + discoverer.shutdownNow(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java new file mode 100644 index 00000000000..1a137c4cbf8 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java @@ -0,0 +1,219 @@ +package org.tron.p2p.discover.protocol.kad; + +import java.net.Inet4Address; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.DiscoverService; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.kad.FindNodeMessage; +import org.tron.p2p.discover.message.kad.KadMessage; +import org.tron.p2p.discover.message.kad.NeighborsMessage; +import org.tron.p2p.discover.message.kad.PingMessage; +import org.tron.p2p.discover.message.kad.PongMessage; +import org.tron.p2p.discover.protocol.kad.table.NodeTable; +import org.tron.p2p.discover.socket.UdpEvent; + +@Slf4j(topic = "net") +public class KadService implements DiscoverService { + + private static final int MAX_NODES = 2000; + private static final int NODES_TRIM_THRESHOLD = 3000; + @Getter + @Setter + private static long pingTimeout = 15_000; + + private final List bootNodes = new ArrayList<>(); + + private volatile boolean inited = false; + + private final Map nodeHandlerMap = new ConcurrentHashMap<>(); + + private Consumer messageSender; + + private NodeTable table; + private Node homeNode; + + private ScheduledExecutorService pongTimer; + private DiscoverTask discoverTask; + + public void init() { + for (InetSocketAddress address : Parameter.p2pConfig.getSeedNodes()) { + bootNodes.add(new Node(address)); + } + for (InetSocketAddress address : Parameter.p2pConfig.getActiveNodes()) { + bootNodes.add(new Node(address)); + } + this.pongTimer = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("pongTimer").build()); + this.homeNode = new Node(Parameter.p2pConfig.getNodeID(), Parameter.p2pConfig.getIp(), + Parameter.p2pConfig.getIpv6(), Parameter.p2pConfig.getPort()); + this.table = new NodeTable(homeNode); + + if (Parameter.p2pConfig.isDiscoverEnable()) { + discoverTask = new DiscoverTask(this); + discoverTask.init(); + } + } + + public void close() { + try { + if (pongTimer != null) { + pongTimer.shutdownNow(); + } + + if (discoverTask != null) { + discoverTask.close(); + } + } catch (Exception e) { + log.error("Close nodeManagerTasksTimer or pongTimer failed", e); + throw e; + } + } + + public List getConnectableNodes() { + return getAllNodes().stream() + .filter(node -> node.isConnectible(Parameter.p2pConfig.getNetworkId())) + .filter(node -> node.getPreferInetSocketAddress() != null) + .collect(Collectors.toList()); + } + + public List getTableNodes() { + return table.getTableNodes(); + } + + public List getAllNodes() { + List nodeList = new ArrayList<>(); + for (NodeHandler nodeHandler : nodeHandlerMap.values()) { + nodeList.add(nodeHandler.getNode()); + } + return nodeList; + } + + @Override + public void setMessageSender(Consumer messageSender) { + this.messageSender = messageSender; + } + + @Override + public void channelActivated() { + if (!inited) { + inited = true; + + for (Node node : bootNodes) { + getNodeHandler(node); + } + } + } + + @Override + public void handleEvent(UdpEvent udpEvent) { + KadMessage m = (KadMessage) udpEvent.getMessage(); + + InetSocketAddress sender = udpEvent.getAddress(); + + Node n; + if (sender.getAddress() instanceof Inet4Address) { + n = new Node(m.getFrom().getId(), sender.getHostString(), m.getFrom().getHostV6(), + sender.getPort(), m.getFrom().getPort()); + } else { + n = new Node(m.getFrom().getId(), m.getFrom().getHostV4(), sender.getHostString(), + sender.getPort(), m.getFrom().getPort()); + } + + NodeHandler nodeHandler = getNodeHandler(n); + nodeHandler.getNode().setId(n.getId()); + nodeHandler.getNode().touch(); + + switch (m.getType()) { + case KAD_PING: + nodeHandler.handlePing((PingMessage) m); + break; + case KAD_PONG: + nodeHandler.handlePong((PongMessage) m); + break; + case KAD_FIND_NODE: + nodeHandler.handleFindNode((FindNodeMessage) m); + break; + case KAD_NEIGHBORS: + nodeHandler.handleNeighbours((NeighborsMessage) m, sender); + break; + default: + break; + } + } + + public NodeHandler getNodeHandler(Node n) { + NodeHandler ret = null; + InetSocketAddress inet4 = n.getInetSocketAddressV4(); + InetSocketAddress inet6 = n.getInetSocketAddressV6(); + if (inet4 != null) { + ret = nodeHandlerMap.get(inet4); + } + if (ret == null && inet6 != null) { + ret = nodeHandlerMap.get(inet6); + } + + if (ret == null) { + trimTable(); + ret = new NodeHandler(n, this); + if (n.getPreferInetSocketAddress() != null) { + nodeHandlerMap.put(n.getPreferInetSocketAddress(), ret); + } + } else { + ret.getNode().updateHostV4(n.getHostV4()); + ret.getNode().updateHostV6(n.getHostV6()); + } + return ret; + } + + public NodeTable getTable() { + return table; + } + + public Node getPublicHomeNode() { + return homeNode; + } + + public void sendOutbound(UdpEvent udpEvent) { + if (Parameter.p2pConfig.isDiscoverEnable() && messageSender != null) { + messageSender.accept(udpEvent); + } + } + + public ScheduledExecutorService getPongTimer() { + return pongTimer; + } + + private void trimTable() { + if (nodeHandlerMap.size() > NODES_TRIM_THRESHOLD) { + nodeHandlerMap.values().forEach(handler -> { + if (!handler.getNode().isConnectible(Parameter.p2pConfig.getNetworkId())) { + nodeHandlerMap.values().remove(handler); + } + }); + } + if (nodeHandlerMap.size() > NODES_TRIM_THRESHOLD) { + List sorted = new ArrayList<>(nodeHandlerMap.values()); + sorted.sort(Comparator.comparingLong(o -> o.getNode().getUpdateTime())); + for (NodeHandler handler : sorted) { + nodeHandlerMap.values().remove(handler); + if (nodeHandlerMap.size() <= MAX_NODES) { + break; + } + } + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java new file mode 100644 index 00000000000..de42c5f40d8 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java @@ -0,0 +1,238 @@ +package org.tron.p2p.discover.protocol.kad; + +import java.net.InetSocketAddress; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.Message; +import org.tron.p2p.discover.message.kad.FindNodeMessage; +import org.tron.p2p.discover.message.kad.NeighborsMessage; +import org.tron.p2p.discover.message.kad.PingMessage; +import org.tron.p2p.discover.message.kad.PongMessage; +import org.tron.p2p.discover.socket.UdpEvent; + +@Slf4j(topic = "net") +public class NodeHandler { + + private Node node; + private volatile State state; + private KadService kadService; + private NodeHandler replaceCandidate; + private AtomicInteger pingTrials = new AtomicInteger(3); + private volatile boolean waitForPong = false; + private volatile boolean waitForNeighbors = false; + + public NodeHandler(Node node, KadService kadService) { + this.node = node; + this.kadService = kadService; + // send ping only if IP stack is compatible + if (node.getPreferInetSocketAddress() != null) { + changeState(State.DISCOVERED); + } + } + + public Node getNode() { + return node; + } + + public void setNode(Node node) { + this.node = node; + } + + public State getState() { + return state; + } + + private void challengeWith(NodeHandler replaceCandidate) { + this.replaceCandidate = replaceCandidate; + changeState(State.EVICTCANDIDATE); + } + + // Manages state transfers + public void changeState(State newState) { + State oldState = state; + if (newState == State.DISCOVERED) { + sendPing(); + } + + if (newState == State.ALIVE) { + Node evictCandidate = kadService.getTable().addNode(this.node); + if (evictCandidate == null) { + newState = State.ACTIVE; + } else { + NodeHandler evictHandler = kadService.getNodeHandler(evictCandidate); + if (evictHandler.state != State.EVICTCANDIDATE) { + evictHandler.challengeWith(this); + } + } + } + if (newState == State.ACTIVE) { + if (oldState == State.ALIVE) { + // new node won the challenge + kadService.getTable().addNode(node); + } else if (oldState == State.EVICTCANDIDATE) { + // nothing to do here the node is already in the table + } else { + // wrong state transition + } + } + + if (newState == State.DEAD) { + if (oldState == State.EVICTCANDIDATE) { + // lost the challenge + // Removing ourselves from the table + kadService.getTable().dropNode(node); + // Congratulate the winner + replaceCandidate.changeState(State.ACTIVE); + } else if (oldState == State.ALIVE) { + // ok the old node was better, nothing to do here + } else { + // wrong state transition + } + } + + if (newState == State.EVICTCANDIDATE) { + // trying to survive, sending ping and waiting for pong + sendPing(); + } + state = newState; + } + + public void handlePing(PingMessage msg) { + if (!kadService.getTable().getNode().equals(node)) { + sendPong(); + } + node.setP2pVersion(msg.getNetworkId()); + if (!node.isConnectible(Parameter.p2pConfig.getNetworkId())) { + changeState(State.DEAD); + } else if (state.equals(State.DEAD)) { + changeState(State.DISCOVERED); + } + } + + public void handlePong(PongMessage msg) { + if (waitForPong) { + waitForPong = false; + node.setP2pVersion(msg.getNetworkId()); + if (!node.isConnectible(Parameter.p2pConfig.getNetworkId())) { + changeState(State.DEAD); + } else { + changeState(State.ALIVE); + } + } + } + + public void handleNeighbours(NeighborsMessage msg, InetSocketAddress sender) { + if (!waitForNeighbors) { + log.warn("Receive neighbors from {} without send find nodes", sender); + return; + } + waitForNeighbors = false; + for (Node n : msg.getNodes()) { + if (!kadService.getPublicHomeNode().getHexId().equals(n.getHexId())) { + kadService.getNodeHandler(n); + } + } + } + + public void handleFindNode(FindNodeMessage msg) { + List closest = kadService.getTable().getClosestNodes(msg.getTargetId()); + sendNeighbours(closest, msg.getTimestamp()); + } + + public void handleTimedOut() { + waitForPong = false; + if (pingTrials.getAndDecrement() > 0) { + sendPing(); + } else { + if (state == State.DISCOVERED || state == State.EVICTCANDIDATE) { + changeState(State.DEAD); + } else { + // TODO just influence to reputation + } + } + } + + public void sendPing() { + PingMessage msg = new PingMessage(kadService.getPublicHomeNode(), getNode()); + waitForPong = true; + sendMessage(msg); + + if (kadService.getPongTimer().isShutdown()) { + return; + } + kadService.getPongTimer().schedule(() -> { + try { + if (waitForPong) { + waitForPong = false; + handleTimedOut(); + } + } catch (Exception e) { + log.error("Unhandled exception in pong timer schedule", e); + } + }, KadService.getPingTimeout(), TimeUnit.MILLISECONDS); + } + + public void sendPong() { + Message pong = new PongMessage(kadService.getPublicHomeNode()); + sendMessage(pong); + } + + public void sendFindNode(byte[] target) { + waitForNeighbors = true; + FindNodeMessage msg = new FindNodeMessage(kadService.getPublicHomeNode(), target); + sendMessage(msg); + } + + public void sendNeighbours(List neighbours, long sequence) { + Message msg = new NeighborsMessage(kadService.getPublicHomeNode(), neighbours, sequence); + sendMessage(msg); + } + + private void sendMessage(Message msg) { + kadService.sendOutbound(new UdpEvent(msg, node.getPreferInetSocketAddress())); + } + + @Override + public String toString() { + return "NodeHandler[state: " + state + ", node: " + node.getHostKey() + ":" + node.getPort() + + "]"; + } + + public enum State { + /** + * The new node was just discovered either by receiving it with Neighbours message or by + * receiving Ping from a new node In either case we are sending Ping and waiting for Pong If the + * Pong is received the node becomes {@link #ALIVE} If the Pong was timed out the node becomes + * {@link #DEAD} + */ + DISCOVERED, + /** + * The node didn't send the Pong message back withing acceptable timeout This is the final + * state + */ + DEAD, + /** + * The node responded with Pong and is now the candidate for inclusion to the table If the table + * has bucket space for this node it is added to table and becomes {@link #ACTIVE} If the table + * bucket is full this node is challenging with the old node from the bucket if it wins then old + * node is dropped, and this node is added and becomes {@link #ACTIVE} else this node becomes + * {@link #DEAD} + */ + ALIVE, + /** + * The node is included in the table. It may become {@link #EVICTCANDIDATE} if a new node wants + * to become Active but the table bucket is full. + */ + ACTIVE, + /** + * This node is in the table but is currently challenging with a new Node candidate to survive + * in the table bucket If it wins then returns back to {@link #ACTIVE} state, else is evicted + * from the table and becomes {@link #DEAD} + */ + EVICTCANDIDATE + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/DistanceComparator.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/DistanceComparator.java new file mode 100644 index 00000000000..30da3b0fdab --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/DistanceComparator.java @@ -0,0 +1,26 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import java.util.Comparator; +import org.tron.p2p.discover.Node; + +public class DistanceComparator implements Comparator { + private byte[] targetId; + + DistanceComparator(byte[] targetId) { + this.targetId = targetId; + } + + @Override + public int compare(Node e1, Node e2) { + int d1 = NodeEntry.distance(targetId, e1.getId()); + int d2 = NodeEntry.distance(targetId, e2.getId()); + + if (d1 > d2) { + return 1; + } else if (d1 < d2) { + return -1; + } else { + return 0; + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java new file mode 100644 index 00000000000..b1362438af0 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java @@ -0,0 +1,12 @@ +package org.tron.p2p.discover.protocol.kad.table; + +public class KademliaOptions { + public static final int BUCKET_SIZE = 16; + public static final int ALPHA = 3; + public static final int BINS = 17; + public static final int MAX_STEPS = 8; + public static final int MAX_LOOP_NUM = 5; + + public static final long DISCOVER_CYCLE = 7200; //discovery cycle interval in millis + public static final long WAIT_TIME = 100; //wait time in millis +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeBucket.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeBucket.java new file mode 100644 index 00000000000..a79ade07986 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeBucket.java @@ -0,0 +1,53 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class NodeBucket { + private final int depth; + private List nodes = new ArrayList<>(); + + NodeBucket(int depth) { + this.depth = depth; + } + + public int getDepth() { + return depth; + } + + public synchronized NodeEntry addNode(NodeEntry e) { + if (!nodes.contains(e)) { + if (nodes.size() >= KademliaOptions.BUCKET_SIZE) { + return getLastSeen(); + } else { + nodes.add(e); + } + } + + return null; + } + + private NodeEntry getLastSeen() { + List sorted = nodes; + Collections.sort(sorted, new TimeComparator()); + return sorted.get(0); + } + + public synchronized void dropNode(NodeEntry entry) { + for (NodeEntry e : nodes) { + if (e.getId().equals(entry.getId())) { + nodes.remove(e); + break; + } + } + } + + public int getNodesCount() { + return nodes.size(); + } + + public List getNodes() { + return nodes; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java new file mode 100644 index 00000000000..b31e9ee1f55 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java @@ -0,0 +1,88 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import org.tron.p2p.discover.Node; + +public class NodeEntry { + private Node node; + private String entryId; + private int distance; + private long modified; + + public NodeEntry(byte[] ownerId, Node n) { + this.node = n; + entryId = n.getHostKey(); + distance = distance(ownerId, n.getId()); + touch(); + } + + public static int distance(byte[] ownerId, byte[] targetId) { + byte[] h1 = targetId; + byte[] h2 = ownerId; + + byte[] hash = new byte[Math.min(h1.length, h2.length)]; + + for (int i = 0; i < hash.length; i++) { + hash[i] = (byte) (h1[i] ^ h2[i]); + } + + int d = KademliaOptions.BINS; + + for (byte b : hash) { + if (b == 0) { + d -= 8; + } else { + int count = 0; + for (int i = 7; i >= 0; i--) { + boolean a = ((b & 0xff) & (1 << i)) == 0; + if (a) { + count++; + } else { + break; + } + } + + d -= count; + + break; + } + } + return d; + } + + public void touch() { + modified = System.currentTimeMillis(); + } + + public int getDistance() { + return distance; + } + + public String getId() { + return entryId; + } + + public Node getNode() { + return node; + } + + public long getModified() { + return modified; + } + + @Override + public boolean equals(Object o) { + boolean ret = false; + + if (o != null && this.getClass() == o.getClass()) { + NodeEntry e = (NodeEntry) o; + ret = this.getId().equals(e.getId()); + } + + return ret; + } + + @Override + public int hashCode() { + return this.entryId.hashCode(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java new file mode 100644 index 00000000000..274f5ca1800 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java @@ -0,0 +1,114 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.tron.p2p.discover.Node; + +public class NodeTable { + private final Node node; // our node + private transient NodeBucket[] buckets; + private transient Map nodes; + + public NodeTable(Node n) { + this.node = n; + initialize(); + } + + public Node getNode() { + return node; + } + + public final void initialize() { + nodes = new HashMap<>(); + buckets = new NodeBucket[KademliaOptions.BINS]; + for (int i = 0; i < KademliaOptions.BINS; i++) { + buckets[i] = new NodeBucket(i); + } + } + + public synchronized Node addNode(Node n) { + if (n.getHostKey().equals(node.getHostKey())) { + return null; + } + + NodeEntry entry = nodes.get(n.getHostKey()); + if (entry != null) { + entry.touch(); + return null; + } + + NodeEntry e = new NodeEntry(node.getId(), n); + NodeEntry lastSeen = buckets[getBucketId(e)].addNode(e); + if (lastSeen != null) { + return lastSeen.getNode(); + } + nodes.put(n.getHostKey(), e); + return null; + } + + public synchronized void dropNode(Node n) { + NodeEntry entry = nodes.get(n.getHostKey()); + if (entry != null) { + nodes.remove(n.getHostKey()); + buckets[getBucketId(entry)].dropNode(entry); + } + } + + public synchronized boolean contains(Node n) { + return nodes.containsKey(n.getHostKey()); + } + + public synchronized void touchNode(Node n) { + NodeEntry entry = nodes.get(n.getHostKey()); + if (entry != null) { + entry.touch(); + } + } + + public int getBucketsCount() { + int i = 0; + for (NodeBucket b : buckets) { + if (b.getNodesCount() > 0) { + i++; + } + } + return i; + } + + public int getBucketId(NodeEntry e) { + int id = e.getDistance() - 1; + return Math.max(id, 0); + } + + public synchronized int getNodesCount() { + return nodes.size(); + } + + public synchronized List getAllNodes() { + return new ArrayList<>(nodes.values()); + } + + public synchronized List getClosestNodes(byte[] targetId) { + List closestEntries = getAllNodes(); + List closestNodes = new ArrayList<>(); + for (NodeEntry e : closestEntries) { + closestNodes.add((Node) e.getNode().clone()); + } + Collections.sort(closestNodes, new DistanceComparator(targetId)); + if (closestNodes.size() > KademliaOptions.BUCKET_SIZE) { + closestNodes = closestNodes.subList(0, KademliaOptions.BUCKET_SIZE); + } + return closestNodes; + } + + public synchronized List getTableNodes() { + List nodeList = new ArrayList<>(); + for (NodeEntry nodeEntry : nodes.values()) { + nodeList.add(nodeEntry.getNode()); + } + return nodeList; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/TimeComparator.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/TimeComparator.java new file mode 100644 index 00000000000..7e2e94186e0 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/TimeComparator.java @@ -0,0 +1,19 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import java.util.Comparator; + +public class TimeComparator implements Comparator { + @Override + public int compare(NodeEntry e1, NodeEntry e2) { + long t1 = e1.getModified(); + long t2 = e2.getModified(); + + if (t1 < t2) { + return 1; + } else if (t1 > t2) { + return -1; + } else { + return 0; + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java new file mode 100644 index 00000000000..7b8ca97f2c9 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java @@ -0,0 +1,93 @@ +package org.tron.p2p.discover.socket; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioDatagramChannel; +import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; +import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.stats.TrafficStats; + +@Slf4j(topic = "net") +public class DiscoverServer { + + private Channel channel; + private EventHandler eventHandler; + + private final int SERVER_RESTART_WAIT = 5000; + private final int SERVER_CLOSE_WAIT = 10; + private final int port = Parameter.p2pConfig.getPort(); + private volatile boolean shutdown = false; + + public void init(EventHandler eventHandler) { + this.eventHandler = eventHandler; + new Thread(() -> { + try { + start(); + } catch (Exception e) { + log.error("Discovery server start failed", e); + } + }, "DiscoverServer").start(); + } + + public void close() { + log.info("Closing discovery server..."); + shutdown = true; + if (channel != null) { + try { + channel.close().await(SERVER_CLOSE_WAIT, TimeUnit.SECONDS); + } catch (Exception e) { + log.error("Closing discovery server failed", e); + } + } + } + + private void start() throws Exception { + NioEventLoopGroup group = new NioEventLoopGroup(Parameter.UDP_NETTY_WORK_THREAD_NUM, + new BasicThreadFactory.Builder().namingPattern("discoverServer").build()); + try { + while (!shutdown) { + Bootstrap b = new Bootstrap(); + b.group(group) + .channel(NioDatagramChannel.class) + .handler(new ChannelInitializer() { + @Override + public void initChannel(NioDatagramChannel ch) + throws Exception { + ch.pipeline().addLast(TrafficStats.udp); + ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); + ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); + ch.pipeline().addLast(new P2pPacketDecoder()); + MessageHandler messageHandler = new MessageHandler(ch, eventHandler); + eventHandler.setMessageSender(messageHandler); + ch.pipeline().addLast(messageHandler); + } + }); + + channel = b.bind(port).sync().channel(); + + log.info("Discovery server started, bind port {}", port); + + channel.closeFuture().sync(); + if (shutdown) { + log.info("Shutdown discovery server"); + break; + } + log.warn("Restart discovery server after 5 sec pause..."); + Thread.sleep(SERVER_RESTART_WAIT); + } + } catch (InterruptedException e) { + log.warn("Discover server interrupted"); + Thread.currentThread().interrupt(); + } catch (Exception e) { + log.error("Start discovery server with port {} failed", port, e); + } finally { + group.shutdownGracefully().sync(); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/EventHandler.java b/p2p/src/main/java/org/tron/p2p/discover/socket/EventHandler.java new file mode 100644 index 00000000000..a8223fd5d59 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/EventHandler.java @@ -0,0 +1,12 @@ +package org.tron.p2p.discover.socket; + +import java.util.function.Consumer; + +public interface EventHandler { + + void channelActivated(); + + void handleEvent(UdpEvent event); + + void setMessageSender(Consumer messageSender); +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java new file mode 100644 index 00000000000..502f1dbe30c --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java @@ -0,0 +1,67 @@ +package org.tron.p2p.discover.socket; + +import java.net.InetSocketAddress; +import java.util.function.Consumer; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.socket.DatagramPacket; +import io.netty.channel.socket.nio.NioDatagramChannel; +import lombok.extern.slf4j.Slf4j; + +@Slf4j(topic = "net") +public class MessageHandler extends SimpleChannelInboundHandler + implements Consumer { + + private Channel channel; + + private EventHandler eventHandler; + + public MessageHandler(NioDatagramChannel channel, EventHandler eventHandler) { + this.channel = channel; + this.eventHandler = eventHandler; + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + eventHandler.channelActivated(); + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, UdpEvent udpEvent) { + log.debug("Rcv udp msg type {}, len {} from {} ", + udpEvent.getMessage().getType(), + udpEvent.getMessage().getSendData().length, + udpEvent.getAddress()); + eventHandler.handleEvent(udpEvent); + } + + @Override + public void accept(UdpEvent udpEvent) { + log.debug("Send udp msg type {}, len {} to {} ", + udpEvent.getMessage().getType(), + udpEvent.getMessage().getSendData().length, + udpEvent.getAddress()); + InetSocketAddress address = udpEvent.getAddress(); + sendPacket(udpEvent.getMessage().getSendData(), address); + } + + void sendPacket(byte[] wire, InetSocketAddress address) { + DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address); + channel.write(packet); + channel.flush(); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) { + ctx.flush(); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + log.warn("Exception caught in udp message handler, {} {}", + ctx.channel().remoteAddress(), cause.getMessage()); + ctx.close(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java new file mode 100644 index 00000000000..5cfcf110e68 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java @@ -0,0 +1,51 @@ +package org.tron.p2p.discover.socket; + +import com.google.protobuf.InvalidProtocolBufferException; +import java.util.List; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.socket.DatagramPacket; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.discover.message.Message; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class P2pPacketDecoder extends MessageToMessageDecoder { + + private static final int MAXSIZE = 2048; + + @Override + public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List out) + throws Exception { + ByteBuf buf = packet.content(); + int length = buf.readableBytes(); + if (length <= 1 || length >= MAXSIZE) { + log.warn("UDP rcv bad packet, from {} length = {}", ctx.channel().remoteAddress(), length); + return; + } + byte[] encoded = new byte[length]; + buf.readBytes(encoded); + try { + UdpEvent event = new UdpEvent(Message.parse(encoded), packet.sender()); + out.add(event); + } catch (P2pException pe) { + if (pe.getType().equals(P2pException.TypeEnum.BAD_MESSAGE)) { + log.error("Message validation failed, type {}, len {}, address {}", encoded[0], + encoded.length, packet.sender()); + } else { + log.info("Parse msg failed, type {}, len {}, address {}", encoded[0], encoded.length, + packet.sender()); + } + } catch (InvalidProtocolBufferException e) { + log.warn("An exception occurred while parsing the message, type {}, len {}, address {}, " + + "data {}, cause: {}", encoded[0], encoded.length, packet.sender(), + ByteArray.toHexString(encoded), e.getMessage()); + } catch (Exception e) { + log.error("An exception occurred while parsing the message, type {}, len {}, address {}, " + + "data {}", encoded[0], encoded.length, packet.sender(), + ByteArray.toHexString(encoded), e); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/UdpEvent.java b/p2p/src/main/java/org/tron/p2p/discover/socket/UdpEvent.java new file mode 100644 index 00000000000..244d1c0b44f --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/UdpEvent.java @@ -0,0 +1,32 @@ +package org.tron.p2p.discover.socket; + +import java.net.InetSocketAddress; +import org.tron.p2p.discover.message.Message; + +public class UdpEvent { + private Message message; + //when receive UdpEvent, this is sender address + //when send UdpEvent, this is target address + private InetSocketAddress address; + + public UdpEvent(Message message, InetSocketAddress address) { + this.message = message; + this.address = address; + } + + public Message getMessage() { + return message; + } + + public void setMessage(Message message) { + this.message = message; + } + + public InetSocketAddress getAddress() { + return address; + } + + public void setAddress(InetSocketAddress address) { + this.address = address; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/DnsManager.java b/p2p/src/main/java/org/tron/p2p/dns/DnsManager.java new file mode 100644 index 00000000000..2f0adba9e00 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/DnsManager.java @@ -0,0 +1,79 @@ +package org.tron.p2p.dns; + + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.discover.Node; +import org.tron.p2p.dns.sync.Client; +import org.tron.p2p.dns.sync.RandomIterator; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.dns.update.PublishService; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class DnsManager { + + private static PublishService publishService; + private static Client syncClient; + private static RandomIterator randomIterator; + private static Set localIpSet; + + public static void init() { + publishService = new PublishService(); + syncClient = new Client(); + publishService.init(); + syncClient.init(); + randomIterator = syncClient.newIterator(); + localIpSet = NetUtil.getAllLocalAddress(); + } + + public static void close() { + if (publishService != null) { + publishService.close(); + } + if (syncClient != null) { + syncClient.close(); + } + if (randomIterator != null) { + randomIterator.close(); + } + } + + public static List getDnsNodes() { + Set nodes = new HashSet<>(); + for (Map.Entry entry : syncClient.getTrees().entrySet()) { + Tree tree = entry.getValue(); + int v4Size = 0, v6Size = 0; + List dnsNodes = tree.getDnsNodes(); + List ipv6Nodes = new ArrayList<>(); + for (DnsNode dnsNode : dnsNodes) { + //log.debug("DnsNode:{}", dnsNode); + if (dnsNode.getInetSocketAddressV4() != null) { + v4Size += 1; + } + if (dnsNode.getInetSocketAddressV6() != null) { + v6Size += 1; + ipv6Nodes.add(dnsNode); + } + } + List connectAbleNodes = dnsNodes.stream() + .filter(node -> node.getPreferInetSocketAddress() != null) + .filter(node -> !localIpSet.contains( + node.getPreferInetSocketAddress().getAddress().getHostAddress())) + .collect(Collectors.toList()); + log.debug("Tree {} node size:{}, v4 node size:{}, v6 node size:{}, connectable size:{}", + entry.getKey(), dnsNodes.size(), v4Size, v6Size, connectAbleNodes.size()); + nodes.addAll(connectAbleNodes); + } + return new ArrayList<>(nodes); + } + + public static Node getRandomNodes() { + return randomIterator.next(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java b/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java new file mode 100644 index 00000000000..1c11128c50f --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java @@ -0,0 +1,96 @@ +package org.tron.p2p.dns; + + +import static org.tron.p2p.discover.message.kad.KadMessage.getEndpointFromNode; + +import com.google.protobuf.InvalidProtocolBufferException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.base.Constant; +import org.tron.p2p.discover.Node; +import org.tron.p2p.dns.tree.Algorithm; +import org.tron.p2p.protos.Discover; +import org.tron.p2p.protos.Discover.EndPoints; +import org.tron.p2p.protos.Discover.EndPoints.Builder; +import org.tron.p2p.protos.Discover.Endpoint; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class DnsNode extends Node implements Comparable { + + private static final long serialVersionUID = 6689513341024130226L; + private String v4Hex = Constant.ipV4Hex; + private String v6Hex = Constant.ipV6Hex; + + public DnsNode(byte[] id, String hostV4, String hostV6, int port) throws UnknownHostException { + super(null, hostV4, hostV6, port); + if (StringUtils.isNotEmpty(hostV4)) { + this.v4Hex = ipToString(hostV4); + } + if (StringUtils.isNotEmpty(hostV6)) { + this.v6Hex = ipToString(hostV6); + } + } + + public static String compress(List nodes) { + Builder builder = Discover.EndPoints.newBuilder(); + nodes.forEach(node -> { + Endpoint endpoint = getEndpointFromNode(node); + builder.addNodes(endpoint); + }); + return Algorithm.encode64(builder.build().toByteArray()); + } + + public static List decompress(String base64Content) + throws InvalidProtocolBufferException, UnknownHostException { + byte[] data = Algorithm.decode64(base64Content); + EndPoints endPoints = EndPoints.parseFrom(data); + + List dnsNodes = new ArrayList<>(); + for (Endpoint endpoint : endPoints.getNodesList()) { + DnsNode dnsNode = new DnsNode(endpoint.getNodeId().toByteArray(), + new String(endpoint.getAddress().toByteArray()), + new String(endpoint.getAddressIpv6().toByteArray()), + endpoint.getPort()); + dnsNodes.add(dnsNode); + } + return dnsNodes; + } + + public String ipToString(String ip) throws UnknownHostException { + byte[] bytes = InetAddress.getByName(ip).getAddress(); + return ByteArray.toHexString(bytes); + } + + public int getNetworkA() { + if (StringUtils.isNotEmpty(hostV4)) { + return Integer.parseInt(hostV4.split("\\.")[0]); + } else { + return 0; + } + } + + @Override + public int compareTo(DnsNode o) { + if (this.v4Hex.compareTo(o.v4Hex) != 0) { + return this.v4Hex.compareTo(o.v4Hex); + } else if (this.v6Hex.compareTo(o.v6Hex) != 0) { + return this.v6Hex.compareTo(o.v6Hex); + } else { + return this.port - o.port; + } + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof DnsNode)) { + return false; + } + DnsNode other = (DnsNode) o; + return v4Hex.equals(other.v4Hex) && v6Hex.equals(other.v6Hex) && port == other.port; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java new file mode 100644 index 00000000000..2c58b4f852b --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java @@ -0,0 +1,106 @@ +package org.tron.p2p.dns.lookup; + + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.time.Duration; +import java.util.Random; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.base.Parameter; +import org.xbill.DNS.Lookup; +import org.xbill.DNS.Record; +import org.xbill.DNS.SimpleResolver; +import org.xbill.DNS.TXTRecord; +import org.xbill.DNS.TextParseException; +import org.xbill.DNS.Type; + +@Slf4j(topic = "net") +public class LookUpTxt { + + static String[] publicDnsV4 = new String[] { + "114.114.114.114", "114.114.115.115", //114 DNS + "223.5.5.5", "223.6.6.6", //AliDNS + //"180.76.76.76", //BaiduDNS slow + "119.29.29.29", //DNSPod DNS+ + // "182.254.116.116", //DNSPod DNS+ slow + //"1.2.4.8", "210.2.4.8", //CNNIC SDNS + "117.50.11.11", "117.50.22.22", //oneDNS + "101.226.4.6", "218.30.118.6", "123.125.81.6", "140.207.198.6", //DNS pai + "8.8.8.8", "8.8.4.4", //Google DNS + "9.9.9.9", //IBM Quad9 + //"208.67.222.222", "208.67.220.220", //OpenDNS slow + //"199.91.73.222", "178.79.131.110" //V2EX DNS + }; + + static String[] publicDnsV6 = new String[] { + "2606:4700:4700::1111", "2606:4700:4700::1001", //Cloudflare + "2400:3200::1", "2400:3200:baba::1", //AliDNS + //"2400:da00::6666", //BaiduDNS + "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff", //AdGuard + "2620:74:1b::1:1", "2620:74:1c::2:2", //Verisign + //"2a05:dfc7:5::53", "2a05:dfc7:5::5353", //OpenNIC + "2a02:6b8::feed:0ff", "2a02:6b8:0:1::feed:0ff", //Yandex + "2001:4860:4860::8888", "2001:4860:4860::8844", //Google DNS + "2620:fe::fe", "2620:fe::9", //IBM Quad9 + //"2620:119:35::35", "2620:119:53::53", //OpenDNS + "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff" //AdGuard + }; + + static int maxRetryTimes = 5; + static Random random = new Random(); + + public static TXTRecord lookUpTxt(String hash, String domain) + throws TextParseException, UnknownHostException { + return lookUpTxt(hash + "." + domain); + } + + // only get first Record. + // as dns server has dns cache, we may get the name's latest TXTRecord ttl later after it changes + public static TXTRecord lookUpTxt(String name) throws TextParseException, UnknownHostException { + TXTRecord txt = null; + log.info("LookUp name: {}", name); + Lookup lookup = new Lookup(name, Type.TXT); + int times = 0; + Record[] records = null; + long start = System.currentTimeMillis(); + while (times < maxRetryTimes) { + String publicDns; + if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) { + publicDns = publicDnsV4[random.nextInt(publicDnsV4.length)]; + } else { + publicDns = publicDnsV6[random.nextInt(publicDnsV6.length)]; + } + SimpleResolver simpleResolver = new SimpleResolver(InetAddress.getByName(publicDns)); + simpleResolver.setTimeout(Duration.ofMillis(1000)); + lookup.setResolver(simpleResolver); + long thisTime = System.currentTimeMillis(); + records = lookup.run(); + long end = System.currentTimeMillis(); + times += 1; + if (records != null) { + log.debug("Succeed to use dns: {}, cur cost: {}ms, total cost: {}ms", publicDns, + end - thisTime, end - start); + break; + } else { + log.debug("Failed to use dns: {}, cur cost: {}ms", publicDns, end - thisTime); + } + } + if (records == null) { + log.error("Failed to lookUp name:{}", name); + return null; + } + for (Record item : records) { + txt = (TXTRecord) item; + } + return txt; + } + + public static String joinTXTRecord(TXTRecord txtRecord) { + StringBuilder sb = new StringBuilder(); + for (String s : txtRecord.getStrings()) { + sb.append(s.trim()); + } + return sb.toString(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java new file mode 100644 index 00000000000..95781ec2c1a --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java @@ -0,0 +1,188 @@ +package org.tron.p2p.dns.sync; + + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.net.UnknownHostException; +import java.security.SignatureException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.dns.lookup.LookUpTxt; +import org.tron.p2p.dns.tree.Algorithm; +import org.tron.p2p.dns.tree.BranchEntry; +import org.tron.p2p.dns.tree.Entry; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.dns.tree.NodesEntry; +import org.tron.p2p.dns.tree.RootEntry; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import org.tron.p2p.utils.ByteArray; +import org.xbill.DNS.TXTRecord; +import org.xbill.DNS.TextParseException; + +@Slf4j(topic = "net") +public class Client { + + public static final int recheckInterval = 60 * 60; //seconds, should be smaller than rootTTL + public static final int cacheLimit = 2000; + public static final int randomRetryTimes = 10; + private Cache cache; + @Getter + private final Map trees = new ConcurrentHashMap<>(); + private final Map clientTrees = new HashMap<>(); + + private final ScheduledExecutorService syncer = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("dnsSyncer").build()); + + public Client() { + this.cache = CacheBuilder.newBuilder() + .maximumSize(cacheLimit) + .recordStats() + .build(); + } + + public void init() { + if (!Parameter.p2pConfig.getTreeUrls().isEmpty()) { + syncer.scheduleWithFixedDelay(this::startSync, 5, recheckInterval, + TimeUnit.SECONDS); + } + } + + public void startSync() { + for (String urlScheme : Parameter.p2pConfig.getTreeUrls()) { + ClientTree clientTree = clientTrees.getOrDefault(urlScheme, new ClientTree(this)); + Tree tree = trees.getOrDefault(urlScheme, new Tree()); + trees.put(urlScheme, tree); + clientTrees.put(urlScheme, clientTree); + try { + syncTree(urlScheme, clientTree, tree); + } catch (Exception e) { + log.error("SyncTree failed, url:" + urlScheme, e); + continue; + } + } + } + + public void syncTree(String urlScheme, ClientTree clientTree, Tree tree) throws Exception { + LinkEntry loc = LinkEntry.parseEntry(urlScheme); + if (clientTree == null) { + clientTree = new ClientTree(this); + } + if (clientTree.getLinkEntry() == null) { + clientTree.setLinkEntry(loc); + } + if (tree.getEntries().isEmpty()) { + // when sync tree first time, we can get the entries dynamically + clientTree.syncAll(tree.getEntries()); + } else { + Map tmpEntries = new HashMap<>(); + boolean[] isRootUpdate = clientTree.syncAll(tmpEntries); + if (!isRootUpdate[0]) { + tmpEntries.putAll(tree.getLinksMap()); + } + if (!isRootUpdate[1]) { + tmpEntries.putAll(tree.getNodesMap()); + } + // we update the entries after sync finishes, ignore branch difference + tree.setEntries(tmpEntries); + } + + tree.setRootEntry(clientTree.getRoot()); + log.info("SyncTree {} complete, LinkEntry size:{}, NodesEntry size:{}, node size:{}", + urlScheme, tree.getLinksEntry().size(), tree.getNodesEntry().size(), + tree.getDnsNodes().size()); + } + + public RootEntry resolveRoot(LinkEntry linkEntry) throws TextParseException, DnsException, + SignatureException, UnknownHostException { + //do not put root in cache + TXTRecord txtRecord = LookUpTxt.lookUpTxt(linkEntry.getDomain()); + if (txtRecord == null) { + throw new DnsException(TypeEnum.LOOK_UP_ROOT_FAILED, "domain: " + linkEntry.getDomain()); + } + for (String txt : txtRecord.getStrings()) { + if (txt.startsWith(Entry.rootPrefix)) { + return RootEntry.parseEntry(txt, linkEntry.getUnCompressHexPublicKey(), + linkEntry.getDomain()); + } + } + throw new DnsException(TypeEnum.NO_ROOT_FOUND, "domain: " + linkEntry.getDomain()); + } + + // resolveEntry retrieves an entry from the cache or fetches it from the network if it isn't cached. + public Entry resolveEntry(String domain, String hash) + throws DnsException, TextParseException, UnknownHostException { + Entry entry = cache.getIfPresent(hash); + if (entry != null) { + return entry; + } + entry = doResolveEntry(domain, hash); + if (entry != null) { + cache.put(hash, entry); + } + return entry; + } + + private Entry doResolveEntry(String domain, String hash) + throws DnsException, TextParseException, UnknownHostException { + try { + ByteArray.toHexString(Algorithm.decode32(hash)); + } catch (Exception e) { + throw new DnsException(TypeEnum.OTHER_ERROR, "invalid base32 hash: " + hash); + } + TXTRecord txtRecord = LookUpTxt.lookUpTxt(hash, domain); + if (txtRecord == null) { + return null; + } + String txt = LookUpTxt.joinTXTRecord(txtRecord); + + Entry entry = null; + if (txt.startsWith(Entry.branchPrefix)) { + entry = BranchEntry.parseEntry(txt); + } else if (txt.startsWith(Entry.linkPrefix)) { + entry = LinkEntry.parseEntry(txt); + } else if (txt.startsWith(Entry.nodesPrefix)) { + entry = NodesEntry.parseEntry(txt); + } + + if (entry == null) { + throw new DnsException(TypeEnum.NO_ENTRY_FOUND, + String.format("hash:%s, domain:%s, txt:%s", hash, domain, txt)); + } + + String wantHash = Algorithm.encode32AndTruncate(entry.toString()); + if (!wantHash.equals(hash)) { + throw new DnsException(TypeEnum.HASH_MISS_MATCH, + String.format("hash mismatch, want: [%s], really: [%s], content: [%s]", wantHash, hash, + entry)); + } + return entry; + } + + public RandomIterator newIterator() { + RandomIterator randomIterator = new RandomIterator(this); + for (String urlScheme : Parameter.p2pConfig.getTreeUrls()) { + try { + randomIterator.addTree(urlScheme); + } catch (DnsException e) { + log.error("AddTree failed " + urlScheme, e); + } + } + return randomIterator; + } + + public void close() { + if (syncer != null) { + syncer.shutdown(); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java new file mode 100644 index 00000000000..5c546ffd76f --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java @@ -0,0 +1,195 @@ +package org.tron.p2p.dns.sync; + + +import java.net.UnknownHostException; +import java.security.SignatureException; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.tree.Entry; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.dns.tree.NodesEntry; +import org.tron.p2p.dns.tree.RootEntry; +import org.tron.p2p.exception.DnsException; +import org.xbill.DNS.TextParseException; + +@Slf4j(topic = "net") +public class ClientTree { + + // used for construct + private final Client client; + @Getter + @Setter + private LinkEntry linkEntry; + private final LinkCache linkCache; + + // used for check + private long lastValidateTime; + private int lastSeq = -1; + + // used for sync + @Getter + @Setter + private RootEntry root; + private SubtreeSync enrSync; + private SubtreeSync linkSync; + + //all links in this tree + private Set curLinks; + private String linkGCRoot; + + private final Random random; + + public ClientTree(Client c) { + this.client = c; + this.linkCache = new LinkCache(); + random = new Random(); + } + + public ClientTree(Client c, LinkCache lc, LinkEntry loc) { + this.client = c; + this.linkCache = lc; + this.linkEntry = loc; + curLinks = new HashSet<>(); + random = new Random(); + } + + public boolean[] syncAll(Map entries) + throws DnsException, UnknownHostException, + SignatureException, TextParseException { + boolean[] isRootUpdate = updateRoot(); + linkSync.resolveAll(entries); + enrSync.resolveAll(entries); + return isRootUpdate; + } + + // retrieves a single entry of the tree. The Node return value is non-nil if the entry was a node. + public synchronized DnsNode syncRandom() + throws DnsException, SignatureException, TextParseException, UnknownHostException { + if (rootUpdateDue()) { + updateRoot(); + } + + // Link tree sync has priority, run it to completion before syncing ENRs. + if (!linkSync.done()) { + syncNextLink(); + return null; + } + gcLinks(); + + // Sync next random entry in ENR tree. Once every node has been visited, we simply + // start over. This is fine because entries are cached internally by the client LRU + // also by DNS resolvers. + if (enrSync.done()) { + enrSync = new SubtreeSync(client, linkEntry, root.getERoot(), false); + } + return syncNextRandomNode(); + } + + // checks if any meaningful action can be performed by syncRandom. + public boolean canSyncRandom() { + return rootUpdateDue() || !linkSync.done() || !enrSync.done() || enrSync.leaves == 0; + } + + // gcLinks removes outdated links from the global link cache. GC runs once when the link sync finishes. + public void gcLinks() { + if (!linkSync.done() || root.getLRoot().equals(linkGCRoot)) { + return; + } + linkCache.resetLinks(linkEntry.getRepresent(), curLinks); + linkGCRoot = root.getLRoot(); + } + + // traversal next link of missing + public void syncNextLink() throws DnsException, TextParseException, UnknownHostException { + String hash = linkSync.missing.peek(); + Entry entry = linkSync.resolveNext(hash); + linkSync.missing.poll(); + + if (entry instanceof LinkEntry) { + LinkEntry dest = (LinkEntry) entry; + linkCache.addLink(linkEntry.getRepresent(), dest.getRepresent()); + curLinks.add(dest.getRepresent()); + } + } + + // get one hash from enr missing randomly, then get random node from hash if hash is a leaf node + private DnsNode syncNextRandomNode() + throws DnsException, TextParseException, UnknownHostException { + int pos = random.nextInt(enrSync.missing.size()); + String hash = enrSync.missing.get(pos); + Entry entry = enrSync.resolveNext(hash); + enrSync.missing.remove(pos); + if (entry instanceof NodesEntry) { + NodesEntry nodesEntry = (NodesEntry) entry; + List nodeList = nodesEntry.getNodes(); + int size = nodeList.size(); + return nodeList.get(random.nextInt(size)); + } + log.info("Get branch or link entry in syncNextRandomNode"); + return null; + } + + // updateRoot ensures that the given tree has an up-to-date root. + private boolean[] updateRoot() + throws TextParseException, DnsException, SignatureException, UnknownHostException { + log.info("UpdateRoot {}", linkEntry.getDomain()); + lastValidateTime = System.currentTimeMillis(); + RootEntry rootEntry = client.resolveRoot(linkEntry); + if (rootEntry == null) { + return new boolean[] {false, false}; + } + if (rootEntry.getSeq() <= lastSeq) { + log.info("The seq of url doesn't change, url:[{}], seq:{}", linkEntry.getRepresent(), + lastSeq); + return new boolean[] {false, false}; + } + + root = rootEntry; + lastSeq = rootEntry.getSeq(); + + boolean updateLRoot = false; + boolean updateERoot = false; + if (linkSync == null || !rootEntry.getLRoot().equals(linkSync.root)) { + linkSync = new SubtreeSync(client, linkEntry, rootEntry.getLRoot(), true); + curLinks = new HashSet<>();//clear all links + updateLRoot = true; + } else { + // if lroot is not changed, wo do not to sync the link tree + log.info("The lroot of url doesn't change, url:[{}], lroot:[{}]", linkEntry.getRepresent(), + linkSync.root); + } + + if (enrSync == null || !rootEntry.getERoot().equals(enrSync.root)) { + enrSync = new SubtreeSync(client, linkEntry, rootEntry.getERoot(), false); + updateERoot = true; + } else { + // if eroot is not changed, wo do not to sync the enr tree + log.info("The eroot of url doesn't change, url:[{}], eroot:[{}]", linkEntry.getRepresent(), + enrSync.root); + } + return new boolean[] {updateLRoot, updateERoot}; + } + + private boolean rootUpdateDue() { + boolean scheduledCheck = System.currentTimeMillis() > nextScheduledRootCheck(); + if (scheduledCheck) { + log.info("Update root because of scheduledCheck, {}", linkEntry.getDomain()); + } + return root == null || scheduledCheck; + } + + public long nextScheduledRootCheck() { + return lastValidateTime + Client.recheckInterval * 1000L; + } + + public String toString() { + return linkEntry.toString(); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java b/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java new file mode 100644 index 00000000000..964fb0bcc99 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java @@ -0,0 +1,82 @@ +package org.tron.p2p.dns.sync; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j(topic = "net") +public class LinkCache { + + @Getter + Map> backrefs; + @Getter + @Setter + private boolean changed; //if data in backrefs changes, we need to rebuild trees + + public LinkCache() { + backrefs = new HashMap<>(); + changed = false; + } + + // check if the urlScheme occurs in other trees + public boolean isContainInOtherLink(String urlScheme) { + return backrefs.containsKey(urlScheme) && !backrefs.get(urlScheme).isEmpty(); + } + + /** + * add the reference to backrefs + * + * @param parent the url tree that contains url tree `children` + * @param children url tree + */ + public void addLink(String parent, String children) { + Set refs = backrefs.getOrDefault(children, new HashSet<>()); + if (!refs.contains(parent)) { + changed = true; + } + refs.add(parent); + backrefs.put(children, refs); + } + + /** + * clears all links of the given tree. + * + * @param from tree's urlScheme + * @param keep links contained in this tree + */ + public void resetLinks(String from, final Set keep) { + List stk = new ArrayList<>(); + stk.add(from); + + while (!stk.isEmpty()) { + int size = stk.size(); + String item = stk.get(size - 1); + stk = stk.subList(0, size - 1); + + Iterator>> it = backrefs.entrySet().iterator(); + while (it.hasNext()) { + Entry> entry = it.next(); + String r = entry.getKey(); + Set refs = entry.getValue(); + if ((keep != null && keep.contains(r)) || !refs.contains(item)) { + continue; + } + this.changed = true; + refs.remove(item); + if (refs.isEmpty()) { + it.remove(); + stk.add(r); + } + } + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java new file mode 100644 index 00000000000..35f4823415d --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java @@ -0,0 +1,126 @@ +package org.tron.p2p.dns.sync; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.exception.DnsException; + + +@Slf4j(topic = "net") +public class RandomIterator implements Iterator { + + private final Client client; + private Map clientTrees; + @Getter + private DnsNode cur; + private final LinkCache linkCache; + private final Random random; + + public RandomIterator(Client client) { + this.client = client; + clientTrees = new ConcurrentHashMap<>(); + linkCache = new LinkCache(); + random = new Random(); + } + + //syncs random tree entries until it finds a node. + @Override + public DnsNode next() { + int i = 0; + while (i < Client.randomRetryTimes) { + i += 1; + ClientTree clientTree = pickTree(); + if (clientTree == null) { + log.error("clientTree is null"); + return null; + } + log.info("Choose clientTree:{} from {} ClientTree", clientTree.getLinkEntry().getRepresent(), + clientTrees.size()); + DnsNode dnsNode; + try { + dnsNode = clientTree.syncRandom(); + } catch (Exception e) { + log.warn("Error in DNS random node sync, tree:{}, cause:[{}]", + clientTree.getLinkEntry().getDomain(), e.getMessage()); + continue; + } + if (dnsNode != null && dnsNode.getPreferInetSocketAddress() != null) { + return dnsNode; + } + } + return null; + } + + @Override + public boolean hasNext() { + this.cur = next(); + return this.cur != null; + } + + public void addTree(String url) throws DnsException { + LinkEntry linkEntry = LinkEntry.parseEntry(url); + linkCache.addLink("", linkEntry.getRepresent()); + } + + //the first random + private ClientTree pickTree() { + if (clientTrees == null) { + log.info("clientTrees is null"); + return null; + } + if (linkCache.isChanged()) { + rebuildTrees(); + linkCache.setChanged(false); + } + + int size = clientTrees.size(); + List allTrees = new ArrayList<>(clientTrees.values()); + + return allTrees.get(random.nextInt(size)); + } + + // rebuilds the 'trees' map. + // if urlScheme is not contain in any other link, wo delete it from clientTrees + // then create one ClientTree using this urlScheme, add it to clientTrees + private void rebuildTrees() { + log.info("rebuildTrees..."); + Iterator> it = clientTrees.entrySet().iterator(); + while (it.hasNext()) { + Entry entry = it.next(); + String urlScheme = entry.getKey(); + if (!linkCache.isContainInOtherLink(urlScheme)) { + log.info("remove tree from trees:{}", urlScheme); + it.remove(); + } + } + + for (Entry> entry : linkCache.backrefs.entrySet()) { + String urlScheme = entry.getKey(); + if (!clientTrees.containsKey(urlScheme)) { + try { + LinkEntry linkEntry = LinkEntry.parseEntry(urlScheme); + clientTrees.put(urlScheme, new ClientTree(client, linkCache, linkEntry)); + log.info("add tree to clientTrees:{}", urlScheme); + } catch (DnsException e) { + log.error("Parse LinkEntry failed", e); + } + } + } + log.info("Exist clientTrees: {}", StringUtils.join(clientTrees.keySet(), ",")); + } + + public void close() { + clientTrees = null; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java b/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java new file mode 100644 index 00000000000..eda6de84b31 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java @@ -0,0 +1,75 @@ +package org.tron.p2p.dns.sync; + + +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.dns.tree.BranchEntry; +import org.tron.p2p.dns.tree.Entry; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.dns.tree.NodesEntry; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import org.xbill.DNS.TextParseException; + +@Slf4j(topic = "net") +public class SubtreeSync { + + public Client client; + public LinkEntry linkEntry; + + public String root; + + public boolean link; + public int leaves; + + public LinkedList missing; + + public SubtreeSync(Client c, LinkEntry linkEntry, String root, boolean link) { + this.client = c; + this.linkEntry = linkEntry; + this.root = root; + this.link = link; + this.leaves = 0; + missing = new LinkedList<>(); + missing.add(root); + } + + public boolean done() { + return missing.isEmpty(); + } + + public void resolveAll(Map dest) + throws DnsException, UnknownHostException, TextParseException { + while (!done()) { + String hash = missing.peek(); + Entry entry = resolveNext(hash); + if (entry != null) { + dest.put(hash, entry); + } + missing.poll(); + } + } + + public Entry resolveNext(String hash) + throws DnsException, TextParseException, UnknownHostException { + Entry entry = client.resolveEntry(linkEntry.getDomain(), hash); + if (entry instanceof NodesEntry) { + if (link) { + throw new DnsException(TypeEnum.NODES_IN_LINK_TREE, ""); + } + leaves++; + } else if (entry instanceof LinkEntry) { + if (!link) { + throw new DnsException(TypeEnum.LINK_IN_NODES_TREE, ""); + } + leaves++; + } else if (entry instanceof BranchEntry) { + BranchEntry branchEntry = (BranchEntry) entry; + missing.addAll(Arrays.asList(branchEntry.getChildren())); + } + return entry; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java new file mode 100644 index 00000000000..0cc7ab6c1d6 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java @@ -0,0 +1,151 @@ +package org.tron.p2p.dns.tree; + + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.SignatureException; +import java.util.Arrays; +import java.util.Base64; +import org.apache.commons.lang3.StringUtils; +import org.bouncycastle.asn1.x9.X9ECParameters; +import org.bouncycastle.crypto.ec.CustomNamedCurves; +import org.bouncycastle.crypto.params.ECDomainParameters; +import org.bouncycastle.math.ec.ECPoint; +import org.bouncycastle.util.encoders.Base32; +import org.tron.p2p.utils.ByteArray; +import org.web3j.crypto.ECKeyPair; +import org.web3j.crypto.Hash; +import org.web3j.crypto.Sign; +import org.web3j.crypto.Sign.SignatureData; + +public class Algorithm { + + private static final int truncateLength = 26; + public static final String padding = "="; + + /** + * return compress public key with hex + */ + public static String compressPubKey(BigInteger pubKey) { + String pubKeyYPrefix = pubKey.testBit(0) ? "03" : "02"; + String pubKeyHex = pubKey.toString(16); + String pubKeyX = pubKeyHex.substring(0, 64); + String hexPub = pubKeyYPrefix + pubKeyX; + return hexPub; + } + + public static String decompressPubKey(String hexPubKey) { + X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1"); + ECDomainParameters CURVE = + new ECDomainParameters( + CURVE_PARAMS.getCurve(), + CURVE_PARAMS.getG(), + CURVE_PARAMS.getN(), + CURVE_PARAMS.getH()); + byte[] pubKey = ByteArray.fromHexString(hexPubKey); + ECPoint ecPoint = CURVE.getCurve().decodePoint(pubKey); + byte[] encoded = ecPoint.getEncoded(false); + BigInteger n = new BigInteger(1, Arrays.copyOfRange(encoded, 1, encoded.length)); + return ByteArray.toHexString(n.toByteArray()); + } + + public static ECKeyPair generateKeyPair(String privateKey) { + BigInteger privKey = new BigInteger(privateKey, 16); + BigInteger pubKey = Sign.publicKeyFromPrivate(privKey); + return new ECKeyPair(privKey, pubKey); + } + + /** + * The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1. + */ + public static byte[] sigData(String msg, String privateKey) { + ECKeyPair keyPair = generateKeyPair(privateKey); + Sign.SignatureData signature = Sign.signMessage(msg.getBytes(), keyPair, true); + byte[] data = new byte[65]; + System.arraycopy(signature.getR(), 0, data, 0, 32); + System.arraycopy(signature.getS(), 0, data, 32, 32); + data[64] = signature.getV()[0]; + return data; + } + + public static BigInteger recoverPublicKey(String msg, byte[] sig) throws SignatureException { + int recId = sig[64]; + if (recId < 27) { + recId += 27; + } + Sign.SignatureData signature = new SignatureData((byte) recId, ByteArray.subArray(sig, 0, 32), + ByteArray.subArray(sig, 32, 64)); + return Sign.signedMessageToKey(msg.getBytes(), signature); + } + + /** + * @param publicKey uncompress hex publicKey + * @param msg to be hashed message + */ + public static boolean verifySignature(String publicKey, String msg, byte[] sig) + throws SignatureException { + BigInteger pubKey = new BigInteger(publicKey, 16); + BigInteger pubKeyRecovered = recoverPublicKey(msg, sig); + return pubKey.equals(pubKeyRecovered); + } + + //we only use fix width hash + public static boolean isValidHash(String base32Hash) { + if (base32Hash == null || base32Hash.length() != truncateLength || base32Hash.contains("\r") + || base32Hash.contains("\n")) { + return false; + } + StringBuilder sb = new StringBuilder(base32Hash); + for (int i = 0; i < 32 - truncateLength; i++) { + sb.append(padding); + } + try { + Base32.decode(sb.toString()); + } catch (Exception e) { + return false; + } + return true; + } + + public static String encode64(byte[] content) { + String base64Content = new String(Base64.getUrlEncoder().encode(content), + StandardCharsets.UTF_8); + return StringUtils.stripEnd(base64Content, padding); + } + + // An Encoding is a radix 64 encoding/decoding scheme, defined by a + // 64-character alphabet. The most common encoding is the "base64" + // encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM + // (RFC 1421). RFC 4648 also defines an alternate encoding, which is + // the standard encoding with - and _ substituted for + and /. + public static byte[] decode64(String base64Content) { + return Base64.getUrlDecoder().decode(base64Content); + } + + public static String encode32(byte[] content) { + String base32Content = new String(Base32.encode(content), StandardCharsets.UTF_8); + return StringUtils.stripEnd(base32Content, padding); + } + + /** + * first get the hash of string, then get first 16 letter, last encode it with base32 + */ + public static String encode32AndTruncate(String content) { + return encode32(ByteArray.subArray(Hash.sha3(content.getBytes()), 0, 16)) + .substring(0, truncateLength); + } + + /** + * if content's length is not multiple of 8, we padding it + */ + public static byte[] decode32(String content) { + int left = content.length() % 8; + StringBuilder sb = new StringBuilder(content); + if (left > 0) { + for (int i = 0; i < 8 - left; i++) { + sb.append(padding); + } + } + return Base32.decode(sb.toString()); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java new file mode 100644 index 00000000000..9359349f144 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java @@ -0,0 +1,33 @@ +package org.tron.p2p.dns.tree; + + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +@Slf4j(topic = "net") +public class BranchEntry implements Entry { + + private static final String splitSymbol = ","; + @Getter + private String[] children; + + public BranchEntry(String[] children) { + this.children = children; + } + + public static BranchEntry parseEntry(String e) { + String content = e.substring(branchPrefix.length()); + if (StringUtils.isEmpty(content)) { + log.info("children size is 0, e:[{}]", e); + return new BranchEntry(new String[0]); + } else { + return new BranchEntry(content.split(splitSymbol)); + } + } + + @Override + public String toString() { + return branchPrefix + StringUtils.join(children, splitSymbol); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java new file mode 100644 index 00000000000..e3ea47b137e --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java @@ -0,0 +1,10 @@ +package org.tron.p2p.dns.tree; + + +public interface Entry { + + String rootPrefix = "tree-root-v1:"; + String linkPrefix = "tree://"; + String branchPrefix = "tree-branch:"; + String nodesPrefix = "nodes:"; +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java new file mode 100644 index 00000000000..b1f87490efa --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java @@ -0,0 +1,54 @@ +package org.tron.p2p.dns.tree; + + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class LinkEntry implements Entry { + + @Getter + private final String represent; + @Getter + private final String domain; + @Getter + private final String unCompressHexPublicKey; + + public LinkEntry(String represent, String domain, String unCompressHexPublicKey) { + this.represent = represent; + this.domain = domain; + this.unCompressHexPublicKey = unCompressHexPublicKey; + } + + public static LinkEntry parseEntry(String treeRepresent) throws DnsException { + if (!treeRepresent.startsWith(linkPrefix)) { + throw new DnsException(TypeEnum.INVALID_SCHEME_URL, + "scheme url must starts with :[" + Entry.linkPrefix + "], but get " + treeRepresent); + } + String[] items = treeRepresent.substring(linkPrefix.length()).split("@"); + if (items.length != 2) { + throw new DnsException(TypeEnum.NO_PUBLIC_KEY, "scheme url:" + treeRepresent); + } + String base32PublicKey = items[0]; + + try { + byte[] data = Algorithm.decode32(base32PublicKey); + String unCompressPublicKey = Algorithm.decompressPubKey(ByteArray.toHexString(data)); + return new LinkEntry(treeRepresent, items[1], unCompressPublicKey); + } catch (RuntimeException exception) { + throw new DnsException(TypeEnum.BAD_PUBLIC_KEY, "bad public key:" + base32PublicKey); + } + } + + public static String buildRepresent(String base32PubKey, String domain) { + return linkPrefix + base32PubKey + "@" + domain; + } + + @Override + public String toString() { + return represent; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java new file mode 100644 index 00000000000..83db1ca930c --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java @@ -0,0 +1,40 @@ +package org.tron.p2p.dns.tree; + + +import com.google.protobuf.InvalidProtocolBufferException; +import java.net.UnknownHostException; +import java.util.List; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; + +@Slf4j(topic = "net") +public class NodesEntry implements Entry { + + private final String represent; + @Getter + private final List nodes; + + public NodesEntry(String represent, List nodes) { + this.represent = represent; + this.nodes = nodes; + } + + public static NodesEntry parseEntry(String e) throws DnsException { + String content = e.substring(nodesPrefix.length()); + List nodeList; + try { + nodeList = DnsNode.decompress(content.replace("\"","")); + } catch (InvalidProtocolBufferException | UnknownHostException ex) { + throw new DnsException(TypeEnum.INVALID_NODES, ex); + } + return new NodesEntry(e, nodeList); + } + + @Override + public String toString() { + return represent; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java new file mode 100644 index 00000000000..65425c820e8 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java @@ -0,0 +1,114 @@ +package org.tron.p2p.dns.tree; + + +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import java.security.SignatureException; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import org.tron.p2p.protos.Discover.DnsRoot; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class RootEntry implements Entry { + + @Getter + private DnsRoot dnsRoot; + + public RootEntry(DnsRoot dnsRoot) { + this.dnsRoot = dnsRoot; + } + + public String getERoot() { + return new String(dnsRoot.getTreeRoot().getERoot().toByteArray()); + } + + public String getLRoot() { + return new String(dnsRoot.getTreeRoot().getLRoot().toByteArray()); + } + + public int getSeq() { + return dnsRoot.getTreeRoot().getSeq(); + } + + public void setSeq(int seq) { + DnsRoot.TreeRoot.Builder builder = dnsRoot.getTreeRoot().toBuilder(); + builder.setSeq(seq); + + DnsRoot.Builder dnsRootBuilder = dnsRoot.toBuilder(); + dnsRootBuilder.setTreeRoot(builder.build()); + + this.dnsRoot = dnsRootBuilder.build(); + } + + public byte[] getSignature() { + return Algorithm.decode64(new String(dnsRoot.getSignature().toByteArray())); + } + + public void setSignature(byte[] signature) { + DnsRoot.Builder dnsRootBuilder = dnsRoot.toBuilder(); + dnsRootBuilder.setSignature(ByteString.copyFrom(Algorithm.encode64(signature).getBytes())); + this.dnsRoot = dnsRootBuilder.build(); + } + + public RootEntry(String eRoot, String lRoot, int seq) { + DnsRoot.TreeRoot.Builder builder = DnsRoot.TreeRoot.newBuilder(); + builder.setERoot(ByteString.copyFrom(eRoot.getBytes())); + builder.setLRoot(ByteString.copyFrom(lRoot.getBytes())); + builder.setSeq(seq); + + DnsRoot.Builder dnsRootBuilder = DnsRoot.newBuilder(); + dnsRootBuilder.setTreeRoot(builder.build()); + this.dnsRoot = dnsRootBuilder.build(); + } + + public static RootEntry parseEntry(String e) throws DnsException { + String value = e.substring(rootPrefix.length()); + DnsRoot dnsRoot1; + try { + dnsRoot1 = DnsRoot.parseFrom(Algorithm.decode64(value)); + } catch (InvalidProtocolBufferException ex) { + throw new DnsException(TypeEnum.INVALID_ROOT, String.format("proto=[%s]", e), ex); + } + + byte[] signature = Algorithm.decode64(new String(dnsRoot1.getSignature().toByteArray())); + if (signature.length != 65) { + throw new DnsException(TypeEnum.INVALID_SIGNATURE, + String.format("signature's length(%d) != 65, signature: %s", signature.length, + ByteArray.toHexString(signature))); + } + + return new RootEntry(dnsRoot1); + } + + public static RootEntry parseEntry(String e, String publicKey, String domain) + throws SignatureException, DnsException { + log.info("Domain:{}, public key:{}", domain, publicKey); + RootEntry rootEntry = parseEntry(e); + boolean verify = Algorithm.verifySignature(publicKey, rootEntry.toString(), + rootEntry.getSignature()); + if (!verify) { + throw new DnsException(TypeEnum.INVALID_SIGNATURE, + String.format("verify signature failed! data:[%s], publicKey:%s, domain:%s", e, publicKey, + domain)); + } + if (!Algorithm.isValidHash(rootEntry.getERoot()) || !Algorithm.isValidHash( + rootEntry.getLRoot())) { + throw new DnsException(TypeEnum.INVALID_CHILD, + "eroot:" + rootEntry.getERoot() + " lroot:" + rootEntry.getLRoot()); + } + log.info("Get dnsRoot:[{}]", rootEntry.dnsRoot.toString()); + return rootEntry; + } + + @Override + public String toString() { + return dnsRoot.getTreeRoot().toString(); + } + + public String toFormat() { + return rootPrefix + Algorithm.encode64(dnsRoot.toByteArray()); + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java new file mode 100644 index 00000000000..c05204690e3 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java @@ -0,0 +1,247 @@ +package org.tron.p2p.dns.tree; + + +import com.google.protobuf.InvalidProtocolBufferException; +import java.math.BigInteger; +import java.net.UnknownHostException; +import java.security.SignatureException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.update.AliClient; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import org.tron.p2p.utils.ByteArray; + +@Slf4j(topic = "net") +public class Tree { + + public static final int HashAbbrevSize = 1 + 16 * 13 / 8; // Size of an encoded hash (plus comma) + public static final int MaxChildren = 370 / HashAbbrevSize; // 13 children + + @Getter + @Setter + private RootEntry rootEntry; + @Getter + private Map entries; + private String privateKey; + @Getter + private String base32PublicKey; + + public Tree() { + init(); + } + + private void init() { + this.entries = new ConcurrentHashMap<>(); + } + + private Entry build(List leafs) { + if (leafs.size() == 1) { + return leafs.get(0); + } + if (leafs.size() <= MaxChildren) { + String[] children = new String[leafs.size()]; + for (int i = 0; i < leafs.size(); i++) { + String subDomain = Algorithm.encode32AndTruncate(leafs.get(i).toString()); + children[i] = subDomain; + this.entries.put(subDomain, leafs.get(i)); + } + return new BranchEntry(children); + } + + //every batch size of leaf entry construct a branch + List subtrees = new ArrayList<>(); + while (!leafs.isEmpty()) { + int total = leafs.size(); + int n = Math.min(MaxChildren, total); + Entry branch = build(leafs.subList(0, n)); + + leafs = leafs.subList(n, total); + subtrees.add(branch); + + String subDomain = Algorithm.encode32AndTruncate(branch.toString()); + this.entries.put(subDomain, branch); + } + return build(subtrees); + } + + public void makeTree(int seq, List enrs, List links, String privateKey) + throws DnsException { + List nodesEntryList = new ArrayList<>(); + for (String enr : enrs) { + nodesEntryList.add(NodesEntry.parseEntry(enr)); + } + + List linkEntryList = new ArrayList<>(); + for (String link : links) { + linkEntryList.add(LinkEntry.parseEntry(link)); + } + + init(); + + Entry eRoot = build(nodesEntryList); + String eRootStr = Algorithm.encode32AndTruncate(eRoot.toString()); + entries.put(eRootStr, eRoot); + + Entry lRoot = build(linkEntryList); + String lRootStr = Algorithm.encode32AndTruncate(lRoot.toString()); + entries.put(lRootStr, lRoot); + + setRootEntry(new RootEntry(eRootStr, lRootStr, seq)); + + if (StringUtils.isNotEmpty(privateKey)) { + this.privateKey = privateKey; + sign(); + } + } + + public void sign() throws DnsException { + if (StringUtils.isEmpty(privateKey)) { + return; + } + byte[] sig = Algorithm.sigData(rootEntry.toString(), privateKey); //message don't include prefix + rootEntry.setSignature(sig); + + BigInteger publicKeyInt = Algorithm.generateKeyPair(privateKey).getPublicKey(); + String unCompressPublicKey = ByteArray.toHexString(publicKeyInt.toByteArray()); + + //verify ourselves + boolean verified; + try { + verified = Algorithm.verifySignature(unCompressPublicKey, rootEntry.toString(), + rootEntry.getSignature()); + } catch (SignatureException e) { + throw new DnsException(TypeEnum.INVALID_SIGNATURE, e); + } + if (!verified) { + throw new DnsException(TypeEnum.INVALID_SIGNATURE, ""); + } + String hexPub = Algorithm.compressPubKey(publicKeyInt); + this.base32PublicKey = Algorithm.encode32(ByteArray.fromHexString(hexPub)); + } + + public static List merge(List nodes, int maxMergeSize) { + Collections.sort(nodes); + List enrs = new ArrayList<>(); + int networkA = -1; + List sub = new ArrayList<>(); + for (DnsNode dnsNode : nodes) { + if ((networkA > -1 && dnsNode.getNetworkA() != networkA) || sub.size() >= maxMergeSize) { + enrs.add(Entry.nodesPrefix + DnsNode.compress(sub)); + sub.clear(); + } + sub.add(dnsNode); + networkA = dnsNode.getNetworkA(); + } + if (!sub.isEmpty()) { + enrs.add(Entry.nodesPrefix + DnsNode.compress(sub)); + } + return enrs; + } + + // hash => lower(hash).domain + public Map toTXT(String rootDomain) { + Map dnsRecords = new HashMap<>(); + if (StringUtils.isNoneEmpty(rootDomain)) { + dnsRecords.put(rootDomain, rootEntry.toFormat()); + } else { + dnsRecords.put(AliClient.aliyunRoot, rootEntry.toFormat()); + } + for (Map.Entry item : entries.entrySet()) { + String hash = item.getKey(); + String newKey = StringUtils.isNoneEmpty(rootDomain) ? hash + "." + rootDomain : hash; + dnsRecords.put(newKey.toLowerCase(), item.getValue().toString()); + } + return dnsRecords; + } + + public int getSeq() { + return rootEntry.getSeq(); + } + + public void setSeq(int seq) { + rootEntry.setSeq(seq); + } + + public List getLinksEntry() { + List linkList = new ArrayList<>(); + for (Entry entry : entries.values()) { + if (entry instanceof LinkEntry) { + LinkEntry linkEntry = (LinkEntry) entry; + linkList.add(linkEntry.toString()); + } + } + return linkList; + } + + public Map getLinksMap() { + Map linksMap = new HashMap<>(); + entries.entrySet().stream() + .filter(p -> p.getValue() instanceof LinkEntry) + .forEach(p -> linksMap.put(p.getKey(), p.getValue())); + return linksMap; + } + + public List getBranchesEntry() { + List branches = new ArrayList<>(); + for (Entry entry : entries.values()) { + if (entry instanceof BranchEntry) { + BranchEntry branchEntry = (BranchEntry) entry; + branches.add(branchEntry.toString()); + } + } + return branches; + } + + public List getNodesEntry() { + List nodesEntryList = new ArrayList<>(); + for (Entry entry : entries.values()) { + if (entry instanceof NodesEntry) { + NodesEntry nodesEntry = (NodesEntry) entry; + nodesEntryList.add(nodesEntry.toString()); + } + } + return nodesEntryList; + } + + public Map getNodesMap() { + Map nodesMap = new HashMap<>(); + entries.entrySet().stream() + .filter(p -> p.getValue() instanceof NodesEntry) + .forEach(p -> nodesMap.put(p.getKey(), p.getValue())); + return nodesMap; + } + + public void setEntries(Map entries) { + this.entries = entries; + } + + /** + * get nodes from entries dynamically. when sync first time, entries change as time + */ + public List getDnsNodes() { + List nodesEntryList = getNodesEntry(); + List nodes = new ArrayList<>(); + for (String nodesEntry : nodesEntryList) { + String joinStr = nodesEntry.substring(Entry.nodesPrefix.length()); + List subNodes; + try { + subNodes = DnsNode.decompress(joinStr); + } catch (InvalidProtocolBufferException | UnknownHostException e) { + log.error("", e); + continue; + } + nodes.addAll(subNodes); + } + return nodes; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java new file mode 100644 index 00000000000..795250763ed --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java @@ -0,0 +1,333 @@ +package org.tron.p2p.dns.update; + +import com.aliyun.alidns20150109.Client; +import com.aliyun.alidns20150109.models.*; +import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord; +import com.aliyun.teaopenapi.models.Config; +import java.text.NumberFormat; +import java.util.HashSet; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.dns.tree.NodesEntry; +import org.tron.p2p.dns.tree.RootEntry; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.exception.DnsException; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j(topic = "net") +public class AliClient implements Publish { + + private final Long domainRecordsPageSize = 20L; + private final int maxRetryCount = 3; + private final int successCode = 200; + private final long retryWaitTime = 30; + private final int treeNodeTTL = 24 * 60 * 60; + private int lastSeq = 0; + private Set serverNodes; + private final Client aliDnsClient; + private double changeThreshold; + public static final String aliyunRoot = "@"; + + public AliClient(String endpoint, String accessKeyId, String accessKeySecret, + double changeThreshold) throws Exception { + Config config = new Config(); + config.accessKeyId = accessKeyId; + config.accessKeySecret = accessKeySecret; + config.endpoint = endpoint; + this.changeThreshold = changeThreshold; + this.serverNodes = new HashSet<>(); + aliDnsClient = new Client(config); + } + + @Override + public void testConnect() throws Exception { + } + + @Override + public void deploy(String domainName, Tree t) throws DnsException { + try { + Map existing = collectRecords( + domainName); + log.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), + domainName); + String represent = LinkEntry.buildRepresent(t.getBase32PublicKey(), domainName); + log.info("Trying to publish {}", represent); + t.setSeq(this.lastSeq + 1); + t.sign(); //seq changed, wo need to sign again + Map records = t.toTXT(null); + + Set treeNodes = new HashSet<>(t.getDnsNodes()); + treeNodes.removeAll(serverNodes); // tree - dns + int addNodeSize = treeNodes.size(); + + Set set1 = new HashSet<>(serverNodes); + treeNodes = new HashSet<>(t.getDnsNodes()); + set1.removeAll(treeNodes); // dns - tree + int deleteNodeSize = set1.size(); + + if (serverNodes.isEmpty() + || (addNodeSize + deleteNodeSize) / (double) serverNodes.size() >= changeThreshold) { + String comment = String.format("Tree update of %s at seq %d", domainName, t.getSeq()); + log.info(comment); + submitChanges(domainName, records, existing); + } else { + NumberFormat nf = NumberFormat.getNumberInstance(); + nf.setMaximumFractionDigits(4); + double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); + log.info( + "Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", + nf.format(changePercent), changeThreshold); + } + serverNodes.clear(); + } catch (Exception e) { + throw new DnsException(DnsException.TypeEnum.DEPLOY_DOMAIN_FAILED, e); + } + } + + @Override + public boolean deleteDomain(String domainName) throws Exception { + DeleteSubDomainRecordsRequest request = new DeleteSubDomainRecordsRequest(); + request.setDomainName(domainName); + DeleteSubDomainRecordsResponse response = aliDnsClient.deleteSubDomainRecords(request); + return response.statusCode == successCode; + } + + // collects all TXT records below the given name. it also update lastSeq + @Override + public Map collectRecords( + String domain) throws Exception { + Map records = new HashMap<>(); + + String rootContent = null; + Set collectServerNodes = new HashSet<>(); + try { + DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest(); + request.setDomainName(domain); + request.setType("TXT"); + request.setPageSize(domainRecordsPageSize); + Long currentPageNum = 1L; + while (true) { + request.setPageNumber(currentPageNum); + DescribeDomainRecordsResponse response = aliDnsClient.describeDomainRecords(request); + if (response.statusCode == successCode) { + for (DescribeDomainRecordsResponseBodyDomainRecordsRecord r : response.getBody() + .getDomainRecords().getRecord()) { + String name = StringUtils.stripEnd(r.getRR(), "."); + records.put(name, r); + if (aliyunRoot.equalsIgnoreCase(name)) { + rootContent = r.value; + } + if (StringUtils.isNotEmpty(r.value) && r.value.startsWith( + org.tron.p2p.dns.tree.Entry.nodesPrefix)) { + NodesEntry nodesEntry; + try { + nodesEntry = NodesEntry.parseEntry(r.value); + List dnsNodes = nodesEntry.getNodes(); + collectServerNodes.addAll(dnsNodes); + } catch (DnsException e) { + //ignore + log.error("Parse nodeEntry failed: {}", e.getMessage()); + } + } + } + if (currentPageNum * domainRecordsPageSize >= response.getBody().getTotalCount()) { + break; + } + currentPageNum++; + } else { + throw new Exception("Failed to request domain records"); + } + } + } catch (Exception e) { + log.warn("Failed to collect domain records, error msg: {}", e.getMessage()); + throw e; + } + + if (rootContent != null) { + RootEntry rootEntry = RootEntry.parseEntry(rootContent); + this.lastSeq = rootEntry.getSeq(); + } + this.serverNodes = collectServerNodes; + return records; + } + + private void submitChanges(String domainName, + Map records, + Map existing) + throws Exception { + long ttl; + long addCount = 0; + long updateCount = 0; + long deleteCount = 0; + for (Map.Entry entry : records.entrySet()) { + boolean result = true; + ttl = treeNodeTTL; + if (entry.getKey().equals(aliyunRoot)) { + ttl = rootTTL; + } + if (!existing.containsKey(entry.getKey())) { + result = addRecord(domainName, entry.getKey(), entry.getValue(), ttl); + addCount++; + } else if (!entry.getValue().equals(existing.get(entry.getKey()).getValue()) || + existing.get(entry.getKey()).getTTL() != ttl) { + result = updateRecord(existing.get(entry.getKey()).getRecordId(), entry.getKey(), + entry.getValue(), ttl); + updateCount++; + } + + if (!result) { + throw new Exception("Adding or updating record failed"); + } + } + + for (String key : existing.keySet()) { + if (!records.containsKey(key)) { + deleteRecord(existing.get(key).getRecordId()); + deleteCount++; + } + } + log.info("Published successfully, add count:{}, update count:{}, delete count:{}", + addCount, updateCount, deleteCount); + } + + public boolean addRecord(String domainName, String RR, String value, long ttl) throws Exception { + AddDomainRecordRequest request = new AddDomainRecordRequest(); + request.setDomainName(domainName); + request.setRR(RR); + request.setType("TXT"); + request.setValue(value); + request.setTTL(ttl); + int retryCount = 0; + while (true) { + AddDomainRecordResponse response = aliDnsClient.addDomainRecord(request); + if (response.statusCode == successCode) { + break; + } else if (retryCount < maxRetryCount) { + retryCount++; + Thread.sleep(retryWaitTime); + } else { + return false; + } + } + return true; + } + + public boolean updateRecord(String recId, String RR, String value, long ttl) throws Exception { + UpdateDomainRecordRequest request = new UpdateDomainRecordRequest(); + request.setRecordId(recId); + request.setRR(RR); + request.setType("TXT"); + request.setValue(value); + request.setTTL(ttl); + int retryCount = 0; + while (true) { + UpdateDomainRecordResponse response = aliDnsClient.updateDomainRecord(request); + if (response.statusCode == successCode) { + break; + } else if (retryCount < maxRetryCount) { + retryCount++; + Thread.sleep(retryWaitTime); + } else { + return false; + } + } + return true; + } + + public boolean deleteRecord(String recId) throws Exception { + DeleteDomainRecordRequest request = new DeleteDomainRecordRequest(); + request.setRecordId(recId); + int retryCount = 0; + while (true) { + DeleteDomainRecordResponse response = aliDnsClient.deleteDomainRecord(request); + if (response.statusCode == successCode) { + break; + } else if (retryCount < maxRetryCount) { + retryCount++; + Thread.sleep(retryWaitTime); + } else { + return false; + } + } + return true; + } + + public String getRecId(String domainName, String RR) { + String recId = null; + try { + DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest(); + request.setDomainName(domainName); + request.setRRKeyWord(RR); + DescribeDomainRecordsResponse response = aliDnsClient.describeDomainRecords(request); + if (response.getBody().getTotalCount() > 0) { + List recs = + response.getBody().getDomainRecords().getRecord(); + for (DescribeDomainRecordsResponseBodyDomainRecordsRecord rec : recs) { + if (rec.getRR().equalsIgnoreCase(RR)) { + recId = rec.getRecordId(); + break; + } + } + } + } catch (Exception e) { + log.warn("Failed to get record id, error msg: {}", e.getMessage()); + } + return recId; + } + + public String update(String DomainName, String RR, String value, long ttl) { + String type = "TXT"; + String recId = null; + try { + String existRecId = getRecId(DomainName, RR); + if (existRecId == null || existRecId.isEmpty()) { + AddDomainRecordRequest request = new AddDomainRecordRequest(); + request.setDomainName(DomainName); + request.setRR(RR); + request.setType(type); + request.setValue(value); + request.setTTL(ttl); + AddDomainRecordResponse response = aliDnsClient.addDomainRecord(request); + recId = response.getBody().getRecordId(); + } else { + UpdateDomainRecordRequest request = new UpdateDomainRecordRequest(); + request.setRecordId(existRecId); + request.setRR(RR); + request.setType(type); + request.setValue(value); + request.setTTL(ttl); + UpdateDomainRecordResponse response = aliDnsClient.updateDomainRecord(request); + recId = response.getBody().getRecordId(); + } + } catch (Exception e) { + log.warn("Failed to update or add domain record, error mag: {}", e.getMessage()); + } + + return recId; + } + + public boolean deleteByRR(String domainName, String RR) { + try { + String recId = getRecId(domainName, RR); + if (recId != null && !recId.isEmpty()) { + DeleteDomainRecordRequest request = new DeleteDomainRecordRequest(); + request.setRecordId(recId); + DeleteDomainRecordResponse response = aliDnsClient.deleteDomainRecord(request); + if (response.statusCode != successCode) { + return false; + } + } + } catch (Exception e) { + log.warn("Failed to delete domain record, domain name: {}, RR: {}, error msg: {}", + domainName, RR, e.getMessage()); + return false; + } + return true; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java new file mode 100644 index 00000000000..133b294a795 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java @@ -0,0 +1,506 @@ +package org.tron.p2p.dns.update; + + +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.tree.LinkEntry; +import org.tron.p2p.dns.tree.NodesEntry; +import org.tron.p2p.dns.tree.RootEntry; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.exception.DnsException; +import org.tron.p2p.exception.DnsException.TypeEnum; +import software.amazon.awssdk.auth.credentials.AwsCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.route53.Route53Client; +import software.amazon.awssdk.services.route53.model.Change; +import software.amazon.awssdk.services.route53.model.ChangeAction; +import software.amazon.awssdk.services.route53.model.ChangeBatch; +import software.amazon.awssdk.services.route53.model.ChangeResourceRecordSetsRequest; +import software.amazon.awssdk.services.route53.model.ChangeResourceRecordSetsResponse; +import software.amazon.awssdk.services.route53.model.ChangeStatus; +import software.amazon.awssdk.services.route53.model.GetChangeRequest; +import software.amazon.awssdk.services.route53.model.GetChangeResponse; +import software.amazon.awssdk.services.route53.model.HostedZone; +import software.amazon.awssdk.services.route53.model.ListHostedZonesByNameRequest; +import software.amazon.awssdk.services.route53.model.ListHostedZonesByNameResponse; +import software.amazon.awssdk.services.route53.model.ListResourceRecordSetsRequest; +import software.amazon.awssdk.services.route53.model.ListResourceRecordSetsResponse; +import software.amazon.awssdk.services.route53.model.RRType; +import software.amazon.awssdk.services.route53.model.ResourceRecord; +import software.amazon.awssdk.services.route53.model.ResourceRecordSet; + +@Slf4j(topic = "net") +public class AwsClient implements Publish { + + // Route53 limits change sets to 32k of 'RDATA size'. Change sets are also limited to + // 1000 items. UPSERTs count double. + // https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DNSLimitations.html#limits-api-requests-changeresourcerecordsets + public static final int route53ChangeSizeLimit = 32000; + public static final int route53ChangeCountLimit = 1000; + public static final int maxRetryLimit = 60; + private int lastSeq = 0; + private Route53Client route53Client; + private String zoneId; + private Set serverNodes; + private static final String symbol = "\""; + private static final String postfix = "."; + private double changeThreshold; + + public AwsClient(final String accessKey, final String accessKeySecret, + final String zoneId, final String region, double changeThreshold) throws DnsException { + if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(accessKeySecret)) { + throw new DnsException(TypeEnum.DEPLOY_DOMAIN_FAILED, + "Need Route53 Access Key ID and secret to proceed"); + } + StaticCredentialsProvider staticCredentialsProvider = StaticCredentialsProvider.create( + new AwsCredentials() { + @Override + public String accessKeyId() { + return accessKey; + } + + @Override + public String secretAccessKey() { + return accessKeySecret; + } + }); + route53Client = Route53Client.builder() + .credentialsProvider(staticCredentialsProvider) + .region(Region.of(region)) + .build(); + this.zoneId = zoneId; + this.serverNodes = new HashSet<>(); + this.changeThreshold = changeThreshold; + } + + private void checkZone(String domain) { + if (StringUtils.isEmpty(this.zoneId)) { + this.zoneId = findZoneID(domain); + } + } + + private String findZoneID(String domain) { + log.info("Finding Route53 Zone ID for {}", domain); + ListHostedZonesByNameRequest.Builder request = ListHostedZonesByNameRequest.builder(); + while (true) { + ListHostedZonesByNameResponse response = route53Client.listHostedZonesByName(request.build()); + for (HostedZone hostedZone : response.hostedZones()) { + if (isSubdomain(domain, hostedZone.name())) { + // example: /hostedzone/Z0404776204LVYA8EZNVH + return hostedZone.id().split("/")[2]; + } + } + if (Boolean.FALSE.equals(response.isTruncated())) { + break; + } + request.dnsName(response.dnsName()); + request.hostedZoneId(response.nextHostedZoneId()); + } + return null; + } + + @Override + public void testConnect() throws Exception { + ListHostedZonesByNameRequest.Builder request = ListHostedZonesByNameRequest.builder(); + while (true) { + ListHostedZonesByNameResponse response = route53Client.listHostedZonesByName(request.build()); + if (Boolean.FALSE.equals(response.isTruncated())) { + break; + } + request.dnsName(response.dnsName()); + request.hostedZoneId(response.nextHostedZoneId()); + } + } + + // uploads the given tree to Route53. + @Override + public void deploy(String domain, Tree tree) throws Exception { + checkZone(domain); + + Map existing = collectRecords(domain); + log.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domain); + String represent = LinkEntry.buildRepresent(tree.getBase32PublicKey(), domain); + log.info("Trying to publish {}", represent); + + tree.setSeq(this.lastSeq + 1); + tree.sign(); //seq changed, wo need to sign again + Map records = tree.toTXT(domain); + + List changes = computeChanges(domain, records, existing); + + Set treeNodes = new HashSet<>(tree.getDnsNodes()); + treeNodes.removeAll(serverNodes); // tree - dns + int addNodeSize = treeNodes.size(); + + Set set1 = new HashSet<>(serverNodes); + treeNodes = new HashSet<>(tree.getDnsNodes()); + set1.removeAll(treeNodes); // dns - tree + int deleteNodeSize = set1.size(); + + if (serverNodes.isEmpty() + || (addNodeSize + deleteNodeSize) / (double) serverNodes.size() >= changeThreshold) { + String comment = String.format("Tree update of %s at seq %d", domain, tree.getSeq()); + log.info(comment); + submitChanges(changes, comment); + } else { + NumberFormat nf = NumberFormat.getNumberInstance(); + nf.setMaximumFractionDigits(4); + double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); + log.info("Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", + nf.format(changePercent), changeThreshold); + } + serverNodes.clear(); + } + + // removes all TXT records of the given domain. + @Override + public boolean deleteDomain(String rootDomain) throws Exception { + checkZone(rootDomain); + + Map existing = collectRecords(rootDomain); + log.info("Find {} TXT records for {}", existing.size(), rootDomain); + + List changes = makeDeletionChanges(new HashMap<>(), existing); + + String comment = String.format("delete entree of %s", rootDomain); + submitChanges(changes, comment); + return true; + } + + // collects all TXT records below the given name. it also update lastSeq + @Override + public Map collectRecords(String rootDomain) throws Exception { + Map existing = new HashMap<>(); + ListResourceRecordSetsRequest.Builder request = ListResourceRecordSetsRequest.builder(); + request.hostedZoneId(zoneId); + int page = 0; + + String rootContent = null; + Set collectServerNodes = new HashSet<>(); + while (true) { + log.info("Loading existing TXT records from name:{} zoneId:{} page:{}", rootDomain, zoneId, + page); + ListResourceRecordSetsResponse response = route53Client.listResourceRecordSets( + request.build()); + + List recordSetList = response.resourceRecordSets(); + for (ResourceRecordSet resourceRecordSet : recordSetList) { + if (!isSubdomain(resourceRecordSet.name(), rootDomain) + || resourceRecordSet.type() != RRType.TXT) { + continue; + } + List values = new ArrayList<>(); + for (ResourceRecord resourceRecord : resourceRecordSet.resourceRecords()) { + values.add(resourceRecord.value()); + } + RecordSet recordSet = new RecordSet(values.toArray(new String[0]), + resourceRecordSet.ttl()); + String name = StringUtils.stripEnd(resourceRecordSet.name(), postfix); + existing.put(name, recordSet); + + String content = StringUtils.join(values, ""); + content = StringUtils.strip(content, symbol); + if (rootDomain.equalsIgnoreCase(name)) { + rootContent = content; + } + if (content.startsWith(org.tron.p2p.dns.tree.Entry.nodesPrefix)) { + NodesEntry nodesEntry; + try { + nodesEntry = NodesEntry.parseEntry(content); + List dnsNodes = nodesEntry.getNodes(); + collectServerNodes.addAll(dnsNodes); + } catch (DnsException e) { + //ignore + log.error("Parse nodeEntry failed: {}", e.getMessage()); + } + } + log.info("Find name: {}", name); + } + + if (Boolean.FALSE.equals(response.isTruncated())) { + break; + } + // Set the cursor to the next batch. From the AWS docs: + // + // To display the next page of results, get the values of NextRecordName, + // NextRecordType, and NextRecordIdentifier (if any) from the response. Then submit + // another ListResourceRecordSets request, and specify those values for + // StartRecordName, StartRecordType, and StartRecordIdentifier. + request.startRecordIdentifier(response.nextRecordIdentifier()); + request.startRecordName(response.nextRecordName()); + request.startRecordType(response.nextRecordType()); + page += 1; + } + + if (rootContent != null) { + RootEntry rootEntry = RootEntry.parseEntry(rootContent); + this.lastSeq = rootEntry.getSeq(); + } + this.serverNodes = collectServerNodes; + return existing; + } + + // submits the given DNS changes to Route53. + public void submitChanges(List changes, String comment) { + if (changes.isEmpty()) { + log.info("No DNS changes needed"); + return; + } + + List> batchChanges = splitChanges(changes, route53ChangeSizeLimit, + route53ChangeCountLimit); + + ChangeResourceRecordSetsResponse[] responses = new ChangeResourceRecordSetsResponse[batchChanges.size()]; + for (int i = 0; i < batchChanges.size(); i++) { + log.info("Submit {}/{} changes to Route53", i + 1, batchChanges.size()); + + ChangeBatch.Builder builder = ChangeBatch.builder(); + builder.changes(batchChanges.get(i)); + builder.comment(comment + String.format(" (%d/%d)", i + 1, batchChanges.size())); + + ChangeResourceRecordSetsRequest.Builder request = ChangeResourceRecordSetsRequest.builder(); + request.changeBatch(builder.build()); + request.hostedZoneId(this.zoneId); + + responses[i] = route53Client.changeResourceRecordSets(request.build()); + } + + // Wait for all change batches to propagate. + for (ChangeResourceRecordSetsResponse response : responses) { + log.info("Waiting for change request {}", response.changeInfo().id()); + + GetChangeRequest.Builder request = GetChangeRequest.builder(); + request.id(response.changeInfo().id()); + + int count = 0; + while (true) { + GetChangeResponse changeResponse = route53Client.getChange(request.build()); + count += 1; + if (changeResponse.changeInfo().status() == ChangeStatus.INSYNC || count >= maxRetryLimit) { + break; + } + try { + Thread.sleep(15 * 1000); + } catch (InterruptedException e) { + } + } + } + log.info("Submit {} changes complete", changes.size()); + } + + // computeChanges creates DNS changes for the given set of DNS discovery records. + // records is the latest records to be put in Route53. + // The 'existing' arg is the set of records that already exist on Route53. + public List computeChanges(String domain, Map records, + Map existing) { + + List changes = new ArrayList<>(); + for (Entry entry : records.entrySet()) { + String path = entry.getKey(); + String value = entry.getValue(); + String newValue = splitTxt(value); + + // name's ttl in our domain will not changed, + // but this ttl on public dns server will decrease with time after request it first time + long ttl = path.equalsIgnoreCase(domain) ? rootTTL : treeNodeTTL; + + if (!existing.containsKey(path)) { + log.info("Create {} = {}", path, value); + Change change = newTXTChange(ChangeAction.CREATE, path, ttl, newValue); + changes.add(change); + } else { + RecordSet recordSet = existing.get(path); + String preValue = StringUtils.join(recordSet.values, ""); + + if (!preValue.equalsIgnoreCase(newValue) || recordSet.ttl != ttl) { + log.info("Updating {} from [{}] to [{}]", path, preValue, newValue); + if (path.equalsIgnoreCase(domain)) { + try { + RootEntry oldRoot = RootEntry.parseEntry(StringUtils.strip(preValue, symbol)); + RootEntry newRoot = RootEntry.parseEntry(StringUtils.strip(newValue, symbol)); + log.info("Updating root from [{}] to [{}]", oldRoot.getDnsRoot(), + newRoot.getDnsRoot()); + } catch (DnsException e) { + //ignore + } + } + Change change = newTXTChange(ChangeAction.UPSERT, path, ttl, newValue); + changes.add(change); + } + } + } + + List deleteChanges = makeDeletionChanges(records, existing); + changes.addAll(deleteChanges); + + sortChanges(changes); + return changes; + } + + // creates record changes which delete all records not contained in 'keep' + public List makeDeletionChanges(Map keeps, + Map existing) { + List changes = new ArrayList<>(); + for (Entry entry : existing.entrySet()) { + String path = entry.getKey(); + RecordSet recordSet = entry.getValue(); + if (!keeps.containsKey(path)) { + log.info("Delete {} = {}", path, StringUtils.join(existing.get(path).values, "")); + Change change = newTXTChange(ChangeAction.DELETE, path, recordSet.ttl, recordSet.values); + changes.add(change); + } + } + return changes; + } + + // ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order. + public static void sortChanges(List changes) { + changes.sort((o1, o2) -> { + if (getChangeOrder(o1) == getChangeOrder(o2)) { + return o1.resourceRecordSet().name().compareTo(o2.resourceRecordSet().name()); + } else { + return getChangeOrder(o1) - getChangeOrder(o2); + } + }); + } + + private static int getChangeOrder(Change change) { + switch (change.action()) { + case CREATE: + return 1; + case UPSERT: + return 2; + case DELETE: + return 3; + default: + return 4; + } + } + + // splits up DNS changes such that each change batch is smaller than the given RDATA limit. + private static List> splitChanges(List changes, int sizeLimit, + int countLimit) { + List> batchChanges = new ArrayList<>(); + + List subChanges = new ArrayList<>(); + int batchSize = 0; + int batchCount = 0; + for (Change change : changes) { + int changeCount = getChangeCount(change); + int changeSize = getChangeSize(change) * changeCount; + + if (batchCount + changeCount <= countLimit + && batchSize + changeSize <= sizeLimit) { + subChanges.add(change); + batchCount += changeCount; + batchSize += changeSize; + } else { + batchChanges.add(subChanges); + subChanges = new ArrayList<>(); + subChanges.add(change); + batchSize = changeSize; + batchCount = changeCount; + } + } + if (!subChanges.isEmpty()) { + batchChanges.add(subChanges); + } + return batchChanges; + } + + // returns the RDATA size of a DNS change. + private static int getChangeSize(Change change) { + int dataSize = 0; + for (ResourceRecord resourceRecord : change.resourceRecordSet().resourceRecords()) { + dataSize += resourceRecord.value().length(); + } + return dataSize; + } + + private static int getChangeCount(Change change) { + if (change.action() == ChangeAction.UPSERT) { + return 2; + } + return 1; + } + + public static boolean isSameChange(Change c1, Change c2) { + boolean isSame = c1.action().equals(c2.action()) + && c1.resourceRecordSet().ttl().longValue() == c2.resourceRecordSet().ttl().longValue() + && c1.resourceRecordSet().name().equals(c2.resourceRecordSet().name()) + && c1.resourceRecordSet().resourceRecords().size() == c2.resourceRecordSet() + .resourceRecords().size(); + if (!isSame) { + return false; + } + List list1 = c1.resourceRecordSet().resourceRecords(); + List list2 = c2.resourceRecordSet().resourceRecords(); + for (int i = 0; i < list1.size(); i++) { + if (!list1.get(i).equalsBySdkFields(list2.get(i))) { + return false; + } + } + return true; + } + + // creates a change to a TXT record. + public Change newTXTChange(ChangeAction action, String key, long ttl, String... values) { + ResourceRecordSet.Builder builder = ResourceRecordSet.builder() + .name(key) + .type(RRType.TXT) + .ttl(ttl); + List resourceRecords = new ArrayList<>(); + for (String value : values) { + ResourceRecord.Builder builder1 = ResourceRecord.builder(); + builder1.value(value); + resourceRecords.add(builder1.build()); + } + builder.resourceRecords(resourceRecords); + + Change.Builder builder2 = Change.builder(); + builder2.action(action); + builder2.resourceRecordSet(builder.build()); + return builder2.build(); + } + + // splits value into a list of quoted 255-character strings. + // only used in CREATE and UPSERT + private String splitTxt(String value) { + StringBuilder sb = new StringBuilder(); + while (value.length() > 253) { + sb.append(symbol).append(value, 0, 253).append(symbol); + value = value.substring(253); + } + if (value.length() > 0) { + sb.append(symbol).append(value).append(symbol); + } + return sb.toString(); + } + + public static boolean isSubdomain(String sub, String root) { + String subNoSuffix = postfix + StringUtils.strip(sub, postfix); + String rootNoSuffix = postfix + StringUtils.strip(root, postfix); + return subNoSuffix.endsWith(rootNoSuffix); + } + + public static class RecordSet { + + String[] values; + long ttl; + + public RecordSet(String[] values, long ttl) { + this.values = values; + this.ttl = ttl; + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java b/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java new file mode 100644 index 00000000000..ae935762d3d --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java @@ -0,0 +1,23 @@ +package org.tron.p2p.dns.update; + + +public enum DnsType { + AliYun(0, "aliyun dns server"), + AwsRoute53(1, "aws route53 server"); + + private final Integer value; + private final String desc; + + DnsType(Integer value, String desc) { + this.value = value; + this.desc = desc; + } + + public Integer getValue() { + return value; + } + + public String getDesc() { + return desc; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java b/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java new file mode 100644 index 00000000000..aa2733d716e --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java @@ -0,0 +1,19 @@ +package org.tron.p2p.dns.update; + + +import java.util.Map; +import org.tron.p2p.dns.tree.Tree; + +public interface Publish { + + int rootTTL = 10 * 60; + int treeNodeTTL = 7 * 24 * 60 * 60; + + void testConnect() throws Exception; + + void deploy(String domainName, Tree t) throws Exception; + + boolean deleteDomain(String domainName) throws Exception; + + Map collectRecords(String domainName) throws Exception; +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java new file mode 100644 index 00000000000..2da9f0acc82 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java @@ -0,0 +1,25 @@ +package org.tron.p2p.dns.update; + + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import lombok.Data; + +@Data +public class PublishConfig { + + private boolean dnsPublishEnable = false; + private String dnsPrivate = null; + private List knownTreeUrls = new ArrayList<>(); + private List staticNodes = new ArrayList<>(); + private String dnsDomain = null; + private double changeThreshold = 0.1; + private int maxMergeSize = 5; + private DnsType dnsType = null; + private String accessKeyId = null; + private String accessKeySecret = null; + private String aliDnsEndpoint = null; //for aliYun + private String awsHostZoneId = null; //for aws + private String awsRegion = null; //for aws +} diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java new file mode 100644 index 00000000000..5fd7cb4f497 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java @@ -0,0 +1,146 @@ +package org.tron.p2p.dns.update; + +import java.net.Inet4Address; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.NodeManager; +import org.tron.p2p.dns.DnsNode; +import org.tron.p2p.dns.tree.Tree; + +@Slf4j(topic = "net") +public class PublishService { + + private static final long publishDelay = 1 * 60 * 60; + + private ScheduledExecutorService publisher = Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("publishService").build()); + private Publish publish; + + public void init() { + boolean supportV4 = Parameter.p2pConfig.getIp() != null; + PublishConfig publishConfig = Parameter.p2pConfig.getPublishConfig(); + if (checkConfig(supportV4, publishConfig)) { + try { + publish = getPublish(publishConfig); + publish.testConnect(); + } catch (Exception e) { + log.error("Init PublishService failed", e); + return; + } + + if (publishConfig.getStaticNodes() != null && !publishConfig.getStaticNodes().isEmpty()) { + startPublish(); + } else { + publisher.scheduleWithFixedDelay(this::startPublish, 300, publishDelay, TimeUnit.SECONDS); + } + } + } + + private Publish getPublish(PublishConfig config) throws Exception { + Publish publish; + if (config.getDnsType() == DnsType.AliYun) { + publish = new AliClient(config.getAliDnsEndpoint(), + config.getAccessKeyId(), + config.getAccessKeySecret(), + config.getChangeThreshold()); + } else { + publish = new AwsClient(config.getAccessKeyId(), + config.getAccessKeySecret(), + config.getAwsHostZoneId(), + config.getAwsRegion(), + config.getChangeThreshold()); + } + return publish; + } + + private void startPublish() { + PublishConfig config = Parameter.p2pConfig.getPublishConfig(); + try { + Tree tree = new Tree(); + List nodes = getNodes(config); + tree.makeTree(1, nodes, config.getKnownTreeUrls(), config.getDnsPrivate()); + log.info("Try to publish node count:{}", tree.getDnsNodes().size()); + publish.deploy(config.getDnsDomain(), tree); + } catch (Exception e) { + log.error("Failed to publish dns", e); + } + } + + private List getNodes(PublishConfig config) throws UnknownHostException { + Set nodes = new HashSet<>(); + if (config.getStaticNodes() != null && !config.getStaticNodes().isEmpty()) { + for (InetSocketAddress staticAddress : config.getStaticNodes()) { + if (staticAddress.getAddress() instanceof Inet4Address) { + nodes.add(new Node(null, staticAddress.getAddress().getHostAddress(), null, + staticAddress.getPort())); + } else { + nodes.add(new Node(null, null, staticAddress.getAddress().getHostAddress(), + staticAddress.getPort())); + } + } + } else { + nodes.addAll(NodeManager.getConnectableNodes()); + nodes.add(NodeManager.getHomeNode()); + } + List dnsNodes = new ArrayList<>(); + for (Node node : nodes) { + DnsNode dnsNode = new DnsNode(node.getId(), node.getHostV4(), node.getHostV6(), + node.getPort()); + dnsNodes.add(dnsNode); + } + return Tree.merge(dnsNodes, config.getMaxMergeSize()); + } + + private boolean checkConfig(boolean supportV4, PublishConfig config) { + if (!config.isDnsPublishEnable()) { + log.info("Dns publish service is disable"); + return false; + } + if (!supportV4) { + log.error("Must have IP v4 connection to publish dns service"); + return false; + } + if (config.getDnsType() == null) { + log.error("The dns server type must be specified when enabling the dns publishing service"); + return false; + } + if (StringUtils.isEmpty(config.getDnsDomain())) { + log.error("The dns domain must be specified when enabling the dns publishing service"); + return false; + } + if (config.getDnsType() == DnsType.AliYun && + (StringUtils.isEmpty(config.getAccessKeyId()) || + StringUtils.isEmpty(config.getAccessKeySecret()) || + StringUtils.isEmpty(config.getAliDnsEndpoint()) + )) { + log.error("The configuration items related to the Aliyun dns server cannot be empty"); + return false; + } + if (config.getDnsType() == DnsType.AwsRoute53 && + (StringUtils.isEmpty(config.getAccessKeyId()) || + StringUtils.isEmpty(config.getAccessKeySecret()) || + config.getAwsRegion() == null)) { + log.error("The configuration items related to the AwsRoute53 dns server cannot be empty"); + return false; + } + return true; + } + + public void close() { + if (!publisher.isShutdown()) { + publisher.shutdown(); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/exception/DnsException.java b/p2p/src/main/java/org/tron/p2p/exception/DnsException.java new file mode 100644 index 00000000000..40e4ff78d31 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/exception/DnsException.java @@ -0,0 +1,73 @@ +package org.tron.p2p.exception; + + +public class DnsException extends Exception { + + private static final long serialVersionUID = 9096335228978001485L; + private final DnsException.TypeEnum type; + + public DnsException(DnsException.TypeEnum type, String errMsg) { + super(type.desc + ", " + errMsg); + this.type = type; + } + + public DnsException(DnsException.TypeEnum type, Throwable throwable) { + super(throwable); + this.type = type; + } + + public DnsException(DnsException.TypeEnum type, String errMsg, Throwable throwable) { + super(errMsg, throwable); + this.type = type; + } + + public DnsException.TypeEnum getType() { + return type; + } + + public enum TypeEnum { + LOOK_UP_ROOT_FAILED(0, "look up root failed"), + //Resolver/sync errors + NO_ROOT_FOUND(1, "no valid root found"), + NO_ENTRY_FOUND(2, "no valid tree entry found"), + HASH_MISS_MATCH(3, "hash miss match"), + NODES_IN_LINK_TREE(4, "nodes entry in link tree"), + LINK_IN_NODES_TREE(5, "link entry in nodes tree"), + + // Entry parse errors + UNKNOWN_ENTRY(6, "unknown entry type"), + NO_PUBLIC_KEY(7, "missing public key"), + BAD_PUBLIC_KEY(8, "invalid public key"), + INVALID_NODES(9, "invalid node list"), + INVALID_CHILD(10, "invalid child hash"), + INVALID_SIGNATURE(11, "invalid base64 signature"), + INVALID_ROOT(12, "invalid DnsRoot proto"), + INVALID_SCHEME_URL(13, "invalid scheme url"), + + // Publish error + DEPLOY_DOMAIN_FAILED(14, "failed to deploy domain"), + + OTHER_ERROR(15, "other error"); + + private final Integer value; + private final String desc; + + TypeEnum(Integer value, String desc) { + this.value = value; + this.desc = desc; + } + + public Integer getValue() { + return value; + } + + public String getDesc() { + return desc; + } + + @Override + public String toString() { + return value + "-" + desc; + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/exception/P2pException.java b/p2p/src/main/java/org/tron/p2p/exception/P2pException.java new file mode 100644 index 00000000000..32191fb3af7 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/exception/P2pException.java @@ -0,0 +1,59 @@ +package org.tron.p2p.exception; + +public class P2pException extends Exception { + + private static final long serialVersionUID = 1390312274369330710L; + private final TypeEnum type; + + public P2pException(TypeEnum type, String errMsg) { + super(errMsg); + this.type = type; + } + + public P2pException(TypeEnum type, Throwable throwable) { + super(throwable); + this.type = type; + } + + public P2pException(TypeEnum type, String errMsg, Throwable throwable) { + super(errMsg, throwable); + this.type = type; + } + + public TypeEnum getType() { + return type; + } + + public enum TypeEnum { + NO_SUCH_MESSAGE(1, "no such message"), + PARSE_MESSAGE_FAILED(2, "parse message failed"), + MESSAGE_WITH_WRONG_LENGTH(3, "message with wrong length"), + BAD_MESSAGE(4, "bad message"), + BAD_PROTOCOL(5, "bad protocol"), + TYPE_ALREADY_REGISTERED(6, "type already registered"), + EMPTY_MESSAGE(7, "empty message"), + BIG_MESSAGE(8, "big message"); + + private final Integer value; + private final String desc; + + TypeEnum(Integer value, String desc) { + this.value = value; + this.desc = desc; + } + + public Integer getValue() { + return value; + } + + public String getDesc() { + return desc; + } + + @Override + public String toString() { + return value + ", " + desc; + } + } + +} diff --git a/p2p/src/main/java/org/tron/p2p/stats/P2pStats.java b/p2p/src/main/java/org/tron/p2p/stats/P2pStats.java new file mode 100644 index 00000000000..946c1404841 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/stats/P2pStats.java @@ -0,0 +1,15 @@ +package org.tron.p2p.stats; + +import lombok.Data; + +@Data +public class P2pStats { + private long tcpOutSize; + private long tcpInSize; + private long tcpOutPackets; + private long tcpInPackets; + private long udpOutSize; + private long udpInSize; + private long udpOutPackets; + private long udpInPackets; +} diff --git a/p2p/src/main/java/org/tron/p2p/stats/StatsManager.java b/p2p/src/main/java/org/tron/p2p/stats/StatsManager.java new file mode 100644 index 00000000000..83ea3ef7440 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/stats/StatsManager.java @@ -0,0 +1,17 @@ +package org.tron.p2p.stats; + +public class StatsManager { + + public P2pStats getP2pStats() { + P2pStats stats = new P2pStats(); + stats.setTcpInPackets(TrafficStats.tcp.getInPackets().get()); + stats.setTcpOutPackets(TrafficStats.tcp.getOutPackets().get()); + stats.setTcpInSize(TrafficStats.tcp.getInSize().get()); + stats.setTcpOutSize(TrafficStats.tcp.getOutSize().get()); + stats.setUdpInPackets(TrafficStats.udp.getInPackets().get()); + stats.setUdpOutPackets(TrafficStats.udp.getOutPackets().get()); + stats.setUdpInSize(TrafficStats.udp.getInSize().get()); + stats.setUdpOutSize(TrafficStats.udp.getOutSize().get()); + return stats; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java b/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java new file mode 100644 index 00000000000..4671ba58f7a --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java @@ -0,0 +1,51 @@ +package org.tron.p2p.stats; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; +import io.netty.channel.socket.DatagramPacket; +import lombok.Getter; + +import java.util.concurrent.atomic.AtomicLong; + +public class TrafficStats { + public static final TrafficStatHandler tcp = new TrafficStatHandler(); + public static final TrafficStatHandler udp = new TrafficStatHandler(); + + @ChannelHandler.Sharable + static class TrafficStatHandler extends ChannelDuplexHandler { + @Getter + private AtomicLong outSize = new AtomicLong(); + @Getter + private AtomicLong inSize = new AtomicLong(); + @Getter + private AtomicLong outPackets = new AtomicLong(); + @Getter + private AtomicLong inPackets = new AtomicLong(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + inPackets.incrementAndGet(); + if (msg instanceof ByteBuf) { + inSize.addAndGet(((ByteBuf) msg).readableBytes()); + } else if (msg instanceof DatagramPacket) { + inSize.addAndGet(((DatagramPacket) msg).content().readableBytes()); + } + super.channelRead(ctx, msg); + } + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) + throws Exception { + outPackets.incrementAndGet(); + if (msg instanceof ByteBuf) { + outSize.addAndGet(((ByteBuf) msg).readableBytes()); + } else if (msg instanceof DatagramPacket) { + outSize.addAndGet(((DatagramPacket) msg).content().readableBytes()); + } + super.write(ctx, msg, promise); + } + } +} diff --git a/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java new file mode 100644 index 00000000000..b43f0dfc86a --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java @@ -0,0 +1,193 @@ +package org.tron.p2p.utils; + +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.bouncycastle.util.encoders.Hex; + + +/* + * Copyright (c) [2016] [ ] + * This file is part of the ethereumJ library. + * + * The ethereumJ library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The ethereumJ library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the ethereumJ library. If not, see . + */ +@Slf4j(topic = "net") +public class ByteArray { + + public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + public static final byte[] ZERO_BYTE_ARRAY = new byte[] {0}; + public static final int WORD_SIZE = 32; + + public static String toHexString(byte[] data) { + return data == null ? "" : Hex.toHexString(data); + } + + /** + * get bytes data from hex string data. + */ + public static byte[] fromHexString(String data) { + if (data == null) { + return EMPTY_BYTE_ARRAY; + } + if (data.startsWith("0x")) { + data = data.substring(2); + } + if (data.length() % 2 != 0) { + data = "0" + data; + } + return Hex.decode(data); + } + + /** + * get long data from bytes data. + */ + public static long toLong(byte[] b) { + return ArrayUtils.isEmpty(b) ? 0 : new BigInteger(1, b).longValue(); + } + + /** + * get int data from bytes data. + */ + public static int toInt(byte[] b) { + return ArrayUtils.isEmpty(b) ? 0 : new BigInteger(1, b).intValue(); + } + + /** + * get bytes data from string data. + */ + public static byte[] fromString(String s) { + return StringUtils.isBlank(s) ? null : s.getBytes(); + } + + /** + * get string data from bytes data. + */ + public static String toStr(byte[] b) { + return ArrayUtils.isEmpty(b) ? null : new String(b); + } + + public static byte[] fromLong(long val) { + return Longs.toByteArray(val); + } + + public static byte[] fromInt(int val) { + return Ints.toByteArray(val); + } + + /** + * get bytes data from object data. + */ + public static byte[] fromObject(Object obj) { + byte[] bytes = null; + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) { + objectOutputStream.writeObject(obj); + objectOutputStream.flush(); + bytes = byteArrayOutputStream.toByteArray(); + } catch (IOException e) { + log.error("Method objectToByteArray failed.", e); + } + return bytes; + } + + /** + * Stringify byte[] x + * null for null + * null for empty [] + */ + public static String toJsonHex(byte[] x) { + return x == null || x.length == 0 ? "0x" : "0x" + Hex.toHexString(x); + } + + + public static String toJsonHex(Long x) { + return x == null ? null : "0x" + Long.toHexString(x); + } + + public static String toJsonHex(int x) { + return toJsonHex((long) x); + } + + public static String toJsonHex(String x) { + return "0x" + x; + } + + public static BigInteger hexToBigInteger(String input) { + if (input.startsWith("0x")) { + return new BigInteger(input.substring(2), 16); + } else { + return new BigInteger(input, 10); + } + } + + + public static int jsonHexToInt(String x) throws Exception { + if (!x.startsWith("0x")) { + throw new Exception("Incorrect hex syntax"); + } + x = x.substring(2); + return Integer.parseInt(x, 16); + } + + /** + * Generate a subarray of a given byte array. + * + * @param input the input byte array + * @param start the start index + * @param end the end index + * @return a subarray of input, ranging from start (inclusively) to end + * (exclusively) + */ + public static byte[] subArray(byte[] input, int start, int end) { + byte[] result = new byte[end - start]; + System.arraycopy(input, start, result, 0, end - start); + return result; + } + + public static boolean isEmpty(byte[] input) { + return input == null || input.length == 0; + } + + public static boolean matrixContains(List source, byte[] obj) { + for (byte[] sobj : source) { + if (Arrays.equals(sobj, obj)) { + return true; + } + } + return false; + } + + public static String fromHex(String x) { + if (x.startsWith("0x")) { + x = x.substring(2); + } + if (x.length() % 2 != 0) { + x = "0" + x; + } + return x; + } + + public static int byte2int(byte b) { + return b & 0xFF; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java b/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java new file mode 100644 index 00000000000..e5b5511210c --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java @@ -0,0 +1,22 @@ +package org.tron.p2p.utils; + +import java.util.ArrayList; +import java.util.List; + + +public class CollectionUtils { + + public static List truncate(List items, int limit) { + if (limit > items.size()) { + return new ArrayList<>(items); + } + List truncated = new ArrayList<>(limit); + for (T item : items) { + truncated.add(item); + if (truncated.size() == limit) { + break; + } + } + return truncated; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java new file mode 100644 index 00000000000..d1682df80fb --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java @@ -0,0 +1,280 @@ +package org.tron.p2p.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.Socket; +import java.net.SocketException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.CompletionService; +import java.util.concurrent.ExecutorCompletionService; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.regex.Pattern; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.tron.p2p.base.Constant; +import org.tron.p2p.discover.Node; +import org.tron.p2p.protos.Discover; + +@Slf4j(topic = "net") +public class NetUtil { + + public static final Pattern PATTERN_IPv4 = + Pattern.compile("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"); + + //https://codeantenna.com/a/jvrULhCbdj + public static final Pattern PATTERN_IPv6 = Pattern.compile( + "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"); + + private static final String IPADDRESS_LOCALHOST = "127.0.0.1"; + + public static boolean validIpV4(String ip) { + if (StringUtils.isEmpty(ip)) { + return false; + } + return PATTERN_IPv4.matcher(ip).find(); + } + + public static boolean validIpV6(String ip) { + if (StringUtils.isEmpty(ip)) { + return false; + } + return PATTERN_IPv6.matcher(ip).find(); + } + + public static boolean validNode(Node node) { + if (node == null || node.getId() == null) { + return false; + } + if (node.getId().length != Constant.NODE_ID_LEN) { + return false; + } + if (StringUtils.isEmpty(node.getHostV4()) && StringUtils.isEmpty(node.getHostV6())) { + return false; + } + if (StringUtils.isNotEmpty(node.getHostV4()) && !validIpV4(node.getHostV4())) { + return false; + } + if (StringUtils.isNotEmpty(node.getHostV6()) && !validIpV6(node.getHostV6())) { + return false; + } + return true; + } + + public static Node getNode(Discover.Endpoint endpoint) { + return new Node(endpoint.getNodeId().toByteArray(), + ByteArray.toStr(endpoint.getAddress().toByteArray()), + ByteArray.toStr(endpoint.getAddressIpv6().toByteArray()), endpoint.getPort()); + } + + public static byte[] getNodeId() { + Random gen = new Random(); + byte[] id = new byte[Constant.NODE_ID_LEN]; + gen.nextBytes(id); + return id; + } + + private static String getExternalIp(String url, boolean isAskIpv4) { + BufferedReader in = null; + String ip = null; + try { + URLConnection urlConnection = new URL(url).openConnection(); + urlConnection.setConnectTimeout(10_000); //ms + urlConnection.setReadTimeout(10_000); //ms + in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); + ip = in.readLine(); + if (ip == null || ip.trim().isEmpty()) { + throw new IOException("Invalid address: " + ip); + } + InetAddress inetAddress = InetAddress.getByName(ip); + if (isAskIpv4 && !validIpV4(inetAddress.getHostAddress())) { + throw new IOException("Invalid address: " + ip); + } + if (!isAskIpv4 && !validIpV6(inetAddress.getHostAddress())) { + throw new IOException("Invalid address: " + ip); + } + return ip; + } catch (Exception e) { + log.warn("Fail to get {} by {}, cause:{}", + Constant.ipV4Urls.contains(url) ? "ipv4" : "ipv6", url, e.getMessage()); + return null; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + //ignore + } + } + } + } + + private static String getOuterIPv6Address() { + Enumeration networkInterfaces; + try { + networkInterfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + log.warn("GetOuterIPv6Address failed", e); + return null; + } + while (networkInterfaces.hasMoreElements()) { + Enumeration inetAds = networkInterfaces.nextElement().getInetAddresses(); + while (inetAds.hasMoreElements()) { + InetAddress inetAddress = inetAds.nextElement(); + if (inetAddress instanceof Inet6Address && !isReservedAddress(inetAddress)) { + String ipAddress = inetAddress.getHostAddress(); + int index = ipAddress.indexOf('%'); + if (index > 0) { + ipAddress = ipAddress.substring(0, index); + } + return ipAddress; + } + } + } + return null; + } + + public static Set getAllLocalAddress() { + Set localIpSet = new HashSet<>(); + Enumeration networkInterfaces; + try { + networkInterfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + log.warn("GetAllLocalAddress failed", e); + return localIpSet; + } + while (networkInterfaces.hasMoreElements()) { + Enumeration inetAds = networkInterfaces.nextElement().getInetAddresses(); + while (inetAds.hasMoreElements()) { + InetAddress inetAddress = inetAds.nextElement(); + String ipAddress = inetAddress.getHostAddress(); + int index = ipAddress.indexOf('%'); + if (index > 0) { + ipAddress = ipAddress.substring(0, index); + } + localIpSet.add(ipAddress); + } + } + return localIpSet; + } + + private static boolean isReservedAddress(InetAddress inetAddress) { + return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress() + || inetAddress.isLoopbackAddress() || inetAddress.isMulticastAddress(); + } + + public static String getExternalIpV4() { + long t1 = System.currentTimeMillis(); + String ipV4 = getIp(Constant.ipV4Urls, true); + log.debug("GetExternalIpV4 cost {} ms", System.currentTimeMillis() - t1); + return ipV4; + } + + public static String getExternalIpV6() { + long t1 = System.currentTimeMillis(); + String ipV6 = getIp(Constant.ipV6Urls, false); + if (null == ipV6) { + ipV6 = getOuterIPv6Address(); + } + log.debug("GetExternalIpV6 cost {} ms", System.currentTimeMillis() - t1); + return ipV6; + } + + public static InetSocketAddress parseInetSocketAddress(String para) { + int index = para.trim().lastIndexOf(":"); + if (index > 0) { + String host = para.substring(0, index); + if (host.startsWith("[") && host.endsWith("]")) { + host = host.substring(1, host.length() - 1); + } else { + if (host.contains(":")) { + throw new RuntimeException(String.format("Invalid inetSocketAddress: \"%s\", " + + "use ipv4:port or [ipv6]:port", para)); + } + } + int port = Integer.parseInt(para.substring(index + 1)); + return new InetSocketAddress(host, port); + } else { + throw new RuntimeException(String.format("Invalid inetSocketAddress: \"%s\", " + + "use ipv4:port or [ipv6]:port", para)); + } + } + + private static String getIp(List multiSrcUrls, boolean isAskIpv4) { + int threadSize = multiSrcUrls.size(); + ExecutorService executor = Executors.newFixedThreadPool(threadSize, + BasicThreadFactory.builder().namingPattern("getIp-%d").build()); + CompletionService completionService = new ExecutorCompletionService<>(executor); + + for (String url : multiSrcUrls) { + completionService.submit(() -> getExternalIp(url, isAskIpv4)); + } + + String ip = null; + for (int i = 0; i < threadSize; i++) { + try { + //block until any result return + Future f = completionService.take(); + String result = f.get(); + if (StringUtils.isNotEmpty(result)) { + ip = result; + break; + } + } catch (Exception ignored) { + //ignore + } + } + + executor.shutdownNow(); + return ip; + } + + public static String getLanIP() { + Enumeration networkInterfaces; + try { + networkInterfaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + log.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST, e); + return IPADDRESS_LOCALHOST; + } + while (networkInterfaces.hasMoreElements()) { + NetworkInterface ni = networkInterfaces.nextElement(); + try { + if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) { + continue; + } + } catch (SocketException e) { + continue; + } + Enumeration inetAds = ni.getInetAddresses(); + while (inetAds.hasMoreElements()) { + InetAddress inetAddress = inetAds.nextElement(); + if (inetAddress instanceof Inet4Address && !isReservedAddress(inetAddress)) { + String ipAddress = inetAddress.getHostAddress(); + if (PATTERN_IPv4.matcher(ipAddress).find()) { + return ipAddress; + } + } + } + } + log.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST); + return IPADDRESS_LOCALHOST; + } +} diff --git a/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java b/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java new file mode 100644 index 00000000000..afd0b7e33b3 --- /dev/null +++ b/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java @@ -0,0 +1,49 @@ +package org.tron.p2p.utils; + +import com.google.protobuf.ByteString; +import java.io.IOException; + +import org.tron.p2p.base.Parameter; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.protos.Connect; +import org.xerial.snappy.Snappy; + +public class ProtoUtil { + + public static Connect.CompressMessage compressMessage(byte[] data) throws IOException { + Connect.CompressMessage.CompressType type = Connect.CompressMessage.CompressType.uncompress; + byte[] bytes = data; + + byte[] compressData = Snappy.compress(data); + if (compressData.length < bytes.length) { + type = Connect.CompressMessage.CompressType.snappy; + bytes = compressData; + } + + return Connect.CompressMessage.newBuilder() + .setData(ByteString.copyFrom(bytes)) + .setType(type).build(); + } + + public static byte[] uncompressMessage(Connect.CompressMessage message) + throws IOException, P2pException { + byte[] data = message.getData().toByteArray(); + if (message.getType().equals(Connect.CompressMessage.CompressType.uncompress)) { + return data; + } + + int length = Snappy.uncompressedLength(data); + if (length >= Parameter.MAX_MESSAGE_LENGTH) { + throw new P2pException(P2pException.TypeEnum.BIG_MESSAGE, + "message is too big, len=" + length); + } + + byte[] d2 = Snappy.uncompress(data); + if (d2.length >= Parameter.MAX_MESSAGE_LENGTH) { + throw new P2pException(P2pException.TypeEnum.BIG_MESSAGE, + "uncompressed is too big, len=" + length); + } + return d2; + } + +} diff --git a/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java b/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java new file mode 100644 index 00000000000..c2886feb1a0 --- /dev/null +++ b/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.crypto; + +import java.math.BigInteger; + +/** An ECDSA Signature. */ +public class ECDSASignature { + public final BigInteger r; + public final BigInteger s; + + public ECDSASignature(BigInteger r, BigInteger s) { + this.r = r; + this.s = s; + } + + /** + * @return true if the S component is "low", that means it is below {@link + * Sign#HALF_CURVE_ORDER}. See + * BIP62. + */ + public boolean isCanonical() { + return s.compareTo(Sign.HALF_CURVE_ORDER) <= 0; + } + + /** + * Will automatically adjust the S component to be less than or equal to half the curve order, + * if necessary. This is required because for every signature (r,s) the signature (r, -s (mod + * N)) is a valid signature of the same message. However, we dislike the ability to modify the + * bits of a Bitcoin transaction after it's been signed, as that violates various assumed + * invariants. Thus in future only one of those forms will be considered legal and the other + * will be banned. + * + * @return the signature in a canonicalised form. + */ + public ECDSASignature toCanonicalised() { + if (!isCanonical()) { + // The order of the curve is the number of valid points that exist on that curve. + // If S is in the upper half of the number of valid points, then bring it back to + // the lower half. Otherwise, imagine that + // N = 10 + // s = 8, so (-8 % 10 == 2) thus both (r, 8) and (r, 2) are valid solutions. + // 10 - 8 == 2, giving us always the latter solution, which is canonical. + return new ECDSASignature(r, Sign.CURVE.getN().subtract(s)); + } else { + return this; + } + } +} diff --git a/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java b/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java new file mode 100644 index 00000000000..1efd0406bea --- /dev/null +++ b/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java @@ -0,0 +1,114 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.crypto; + +import java.math.BigInteger; +import java.security.KeyPair; +import java.util.Arrays; + +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.signers.ECDSASigner; +import org.bouncycastle.crypto.signers.HMacDSAKCalculator; +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; + +import org.web3j.utils.Numeric; + +/** Elliptic Curve SECP-256k1 generated key pair. */ +public class ECKeyPair { + private final BigInteger privateKey; + private final BigInteger publicKey; + + public ECKeyPair(BigInteger privateKey, BigInteger publicKey) { + this.privateKey = privateKey; + this.publicKey = publicKey; + } + + public BigInteger getPrivateKey() { + return privateKey; + } + + public BigInteger getPublicKey() { + return publicKey; + } + + /** + * Sign a hash with the private key of this key pair. + * + * @param transactionHash the hash to sign + * @return An {@link ECDSASignature} of the hash + */ + public ECDSASignature sign(byte[] transactionHash) { + ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); + + ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); + signer.init(true, privKey); + BigInteger[] components = signer.generateSignature(transactionHash); + + return new ECDSASignature(components[0], components[1]).toCanonicalised(); + } + + public static ECKeyPair create(KeyPair keyPair) { + BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); + BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); + + BigInteger privateKeyValue = privateKey.getD(); + + // Ethereum does not use encoded public keys like bitcoin - see + // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details + // Additionally, as the first bit is a constant prefix (0x04) we ignore this value + byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); + BigInteger publicKeyValue = + new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); + + return new ECKeyPair(privateKeyValue, publicKeyValue); + } + + public static ECKeyPair create(BigInteger privateKey) { + return new ECKeyPair(privateKey, Sign.publicKeyFromPrivate(privateKey)); + } + + public static ECKeyPair create(byte[] privateKey) { + return create(Numeric.toBigInt(privateKey)); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + ECKeyPair ecKeyPair = (ECKeyPair) o; + + if (privateKey != null + ? !privateKey.equals(ecKeyPair.privateKey) + : ecKeyPair.privateKey != null) { + return false; + } + + return publicKey != null + ? publicKey.equals(ecKeyPair.publicKey) + : ecKeyPair.publicKey == null; + } + + @Override + public int hashCode() { + int result = privateKey != null ? privateKey.hashCode() : 0; + result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0); + return result; + } +} diff --git a/p2p/src/main/java/org/web3j/crypto/Hash.java b/p2p/src/main/java/org/web3j/crypto/Hash.java new file mode 100644 index 00000000000..ed908894c5c --- /dev/null +++ b/p2p/src/main/java/org/web3j/crypto/Hash.java @@ -0,0 +1,138 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.crypto; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.bouncycastle.crypto.digests.RIPEMD160Digest; +import org.bouncycastle.crypto.digests.SHA512Digest; +import org.bouncycastle.crypto.macs.HMac; +import org.bouncycastle.crypto.params.KeyParameter; +import org.bouncycastle.jcajce.provider.digest.Blake2b; +import org.bouncycastle.jcajce.provider.digest.Keccak; + +import org.web3j.utils.Numeric; + +/** Cryptographic hash functions. */ +public class Hash { + private Hash() {} + + /** + * Generates a digest for the given {@code input}. + * + * @param input The input to digest + * @param algorithm The hash algorithm to use + * @return The hash value for the given input + * @throws RuntimeException If we couldn't find any provider for the given algorithm + */ + public static byte[] hash(byte[] input, String algorithm) { + try { + MessageDigest digest = MessageDigest.getInstance(algorithm.toUpperCase()); + return digest.digest(input); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Couldn't find a " + algorithm + " provider", e); + } + } + + /** + * Keccak-256 hash function. + * + * @param hexInput hex encoded input data with optional 0x prefix + * @return hash value as hex encoded string + */ + public static String sha3(String hexInput) { + byte[] bytes = Numeric.hexStringToByteArray(hexInput); + byte[] result = sha3(bytes); + return Numeric.toHexString(result); + } + + /** + * Keccak-256 hash function. + * + * @param input binary encoded input data + * @param offset of start of data + * @param length of data + * @return hash value + */ + public static byte[] sha3(byte[] input, int offset, int length) { + Keccak.DigestKeccak kecc = new Keccak.Digest256(); + kecc.update(input, offset, length); + return kecc.digest(); + } + + /** + * Keccak-256 hash function. + * + * @param input binary encoded input data + * @return hash value + */ + public static byte[] sha3(byte[] input) { + return sha3(input, 0, input.length); + } + + /** + * Keccak-256 hash function that operates on a UTF-8 encoded String. + * + * @param utf8String UTF-8 encoded string + * @return hash value as hex encoded string + */ + public static String sha3String(String utf8String) { + return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8))); + } + + /** + * Generates SHA-256 digest for the given {@code input}. + * + * @param input The input to digest + * @return The hash value for the given input + * @throws RuntimeException If we couldn't find any SHA-256 provider + */ + public static byte[] sha256(byte[] input) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + return digest.digest(input); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Couldn't find a SHA-256 provider", e); + } + } + + public static byte[] hmacSha512(byte[] key, byte[] input) { + HMac hMac = new HMac(new SHA512Digest()); + hMac.init(new KeyParameter(key)); + hMac.update(input, 0, input.length); + byte[] out = new byte[64]; + hMac.doFinal(out, 0); + return out; + } + + public static byte[] sha256hash160(byte[] input) { + byte[] sha256 = sha256(input); + RIPEMD160Digest digest = new RIPEMD160Digest(); + digest.update(sha256, 0, sha256.length); + byte[] out = new byte[20]; + digest.doFinal(out, 0); + return out; + } + + /** + * Blake2-256 hash function. + * + * @param input binary encoded input data + * @return hash value + */ + public static byte[] blake2b256(byte[] input) { + return new Blake2b.Blake2b256().digest(input); + } +} diff --git a/p2p/src/main/java/org/web3j/crypto/Sign.java b/p2p/src/main/java/org/web3j/crypto/Sign.java new file mode 100644 index 00000000000..e405156affc --- /dev/null +++ b/p2p/src/main/java/org/web3j/crypto/Sign.java @@ -0,0 +1,361 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.crypto; + +import java.math.BigInteger; +import java.security.SignatureException; +import java.util.Arrays; + +import org.bouncycastle.asn1.x9.X9ECParameters; +import org.bouncycastle.asn1.x9.X9IntegerConverter; +import org.bouncycastle.crypto.ec.CustomNamedCurves; +import org.bouncycastle.crypto.params.ECDomainParameters; +import org.bouncycastle.math.ec.ECAlgorithms; +import org.bouncycastle.math.ec.ECPoint; +import org.bouncycastle.math.ec.FixedPointCombMultiplier; +import org.bouncycastle.math.ec.custom.sec.SecP256K1Curve; + +import org.web3j.utils.Numeric; + +import static org.web3j.utils.Assertions.verifyPrecondition; + +/** + * Transaction signing logic. + * + *

Adapted from the + * BitcoinJ ECKey implementation. + */ +public class Sign { + + public static final X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1"); + static final ECDomainParameters CURVE = + new ECDomainParameters( + CURVE_PARAMS.getCurve(), + CURVE_PARAMS.getG(), + CURVE_PARAMS.getN(), + CURVE_PARAMS.getH()); + static final BigInteger HALF_CURVE_ORDER = CURVE_PARAMS.getN().shiftRight(1); + + static final String MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n"; + + static byte[] getEthereumMessagePrefix(int messageLength) { + return MESSAGE_PREFIX.concat(String.valueOf(messageLength)).getBytes(); + } + + static byte[] getEthereumMessageHash(byte[] message) { + byte[] prefix = getEthereumMessagePrefix(message.length); + + byte[] result = new byte[prefix.length + message.length]; + System.arraycopy(prefix, 0, result, 0, prefix.length); + System.arraycopy(message, 0, result, prefix.length, message.length); + + return Hash.sha3(result); + } + + public static SignatureData signPrefixedMessage(byte[] message, ECKeyPair keyPair) { + return signMessage(getEthereumMessageHash(message), keyPair, false); + } + + public static SignatureData signMessage(byte[] message, ECKeyPair keyPair) { + return signMessage(message, keyPair, true); + } + + public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) { + BigInteger publicKey = keyPair.getPublicKey(); + byte[] messageHash; + if (needToHash) { + messageHash = Hash.sha3(message); + } else { + messageHash = message; + } + + ECDSASignature sig = keyPair.sign(messageHash); + // Now we have to work backwards to figure out the recId needed to recover the signature. + int recId = -1; + for (int i = 0; i < 4; i++) { + BigInteger k = recoverFromSignature(i, sig, messageHash); + if (k != null && k.equals(publicKey)) { + recId = i; + break; + } + } + if (recId == -1) { + throw new RuntimeException( + "Could not construct a recoverable key. Are your credentials valid?"); + } + + int headerByte = recId + 27; + + // 1 header + 32 bytes for R + 32 bytes for S + byte[] v = new byte[] {(byte) headerByte}; + byte[] r = Numeric.toBytesPadded(sig.r, 32); + byte[] s = Numeric.toBytesPadded(sig.s, 32); + + return new SignatureData(v, r, s); + } + + /** + * Given the components of a signature and a selector value, recover and return the public key + * that generated the signature according to the algorithm in SEC1v2 section 4.1.6. + * + *

The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the + * correct one. Because the key recovery operation yields multiple potential keys, the correct + * key must either be stored alongside the signature, or you must be willing to try each recId + * in turn until you find one that outputs the key you are expecting. + * + *

If this method returns null it means recovery was not possible and recId should be + * iterated. + * + *

Given the above two points, a correct usage of this method is inside a for loop from 0 to + * 3, and if the output is null OR a key that is not the one you expect, you try again with the + * next recId. + * + * @param recId Which possible key to recover. + * @param sig the R and S components of the signature, wrapped. + * @param message Hash of the data that was signed. + * @return An ECKey containing only the public part, or null if recovery wasn't possible. + */ + public static BigInteger recoverFromSignature(int recId, ECDSASignature sig, byte[] message) { + verifyPrecondition(recId >= 0, "recId must be positive"); + verifyPrecondition(sig.r.signum() >= 0, "r must be positive"); + verifyPrecondition(sig.s.signum() >= 0, "s must be positive"); + verifyPrecondition(message != null, "message cannot be null"); + + // 1.0 For j from 0 to h (h == recId here and the loop is outside this function) + // 1.1 Let x = r + jn + BigInteger n = CURVE.getN(); // Curve order. + BigInteger i = BigInteger.valueOf((long) recId / 2); + BigInteger x = sig.r.add(i.multiply(n)); + // 1.2. Convert the integer x to an octet string X of length mlen using the conversion + // routine specified in Section 2.3.7, where mlen = ⌈(log2 p)/8⌉ or mlen = ⌈m/8⌉. + // 1.3. Convert the octet string (16 set binary digits)||X to an elliptic curve point R + // using the conversion routine specified in Section 2.3.4. If this conversion + // routine outputs "invalid", then do another iteration of Step 1. + // + // More concisely, what these points mean is to use X as a compressed public key. + BigInteger prime = SecP256K1Curve.q; + if (x.compareTo(prime) >= 0) { + // Cannot have point co-ordinates larger than this as everything takes place modulo Q. + return null; + } + // Compressed keys require you to know an extra bit of data about the y-coord as there are + // two possibilities. So it's encoded in the recId. + ECPoint R = decompressKey(x, (recId & 1) == 1); + // 1.4. If nR != point at infinity, then do another iteration of Step 1 (callers + // responsibility). + if (!R.multiply(n).isInfinity()) { + return null; + } + // 1.5. Compute e from M using Steps 2 and 3 of ECDSA signature verification. + BigInteger e = new BigInteger(1, message); + // 1.6. For k from 1 to 2 do the following. (loop is outside this function via + // iterating recId) + // 1.6.1. Compute a candidate public key as: + // Q = mi(r) * (sR - eG) + // + // Where mi(x) is the modular multiplicative inverse. We transform this into the following: + // Q = (mi(r) * s ** R) + (mi(r) * -e ** G) + // Where -e is the modular additive inverse of e, that is z such that z + e = 0 (mod n). + // In the above equation ** is point multiplication and + is point addition (the EC group + // operator). + // + // We can find the additive inverse by subtracting e from zero then taking the mod. For + // example the additive inverse of 3 modulo 11 is 8 because 3 + 8 mod 11 = 0, and + // -3 mod 11 = 8. + BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n); + BigInteger rInv = sig.r.modInverse(n); + BigInteger srInv = rInv.multiply(sig.s).mod(n); + BigInteger eInvrInv = rInv.multiply(eInv).mod(n); + ECPoint q = ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv); + + byte[] qBytes = q.getEncoded(false); + // We remove the prefix + return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); + } + + /** Decompress a compressed public key (x co-ord and low-bit of y-coord). */ + private static ECPoint decompressKey(BigInteger xBN, boolean yBit) { + X9IntegerConverter x9 = new X9IntegerConverter(); + byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve())); + compEnc[0] = (byte) (yBit ? 0x03 : 0x02); + return CURVE.getCurve().decodePoint(compEnc); + } + + /** + * Given an arbitrary piece of text and an Ethereum message signature encoded in bytes, returns + * the public key that was used to sign it. This can then be compared to the expected public key + * to determine if the signature was correct. + * + * @param message RLP encoded message. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a + * signature format error. + */ + public static BigInteger signedMessageToKey(byte[] message, SignatureData signatureData) + throws SignatureException { + return signedMessageHashToKey(Hash.sha3(message), signatureData); + } + + /** + * Given an arbitrary message and an Ethereum message signature encoded in bytes, returns the + * public key that was used to sign it. This can then be compared to the expected public key to + * determine if the signature was correct. + * + * @param message The message. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a + * signature format error. + */ + public static BigInteger signedPrefixedMessageToKey(byte[] message, SignatureData signatureData) + throws SignatureException { + return signedMessageHashToKey(getEthereumMessageHash(message), signatureData); + } + + /** + * Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns + * the public key that was used to sign it. This can then be compared to the expected public key + * to determine if the signature was correct. + * + * @param messageHash The message hash. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a + * signature format error. + */ + public static BigInteger signedMessageHashToKey(byte[] messageHash, SignatureData signatureData) + throws SignatureException { + + byte[] r = signatureData.getR(); + byte[] s = signatureData.getS(); + verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); + verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); + + int header = signatureData.getV()[0] & 0xFF; + // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, + // 0x1D = second key with even y, 0x1E = second key with odd y + if (header < 27 || header > 34) { + throw new SignatureException("Header byte out of range: " + header); + } + + ECDSASignature sig = + new ECDSASignature( + new BigInteger(1, signatureData.getR()), + new BigInteger(1, signatureData.getS())); + + int recId = header - 27; + BigInteger key = recoverFromSignature(recId, sig, messageHash); + if (key == null) { + throw new SignatureException("Could not recover public key from signature"); + } + return key; + } + + /** + * Returns public key from the given private key. + * + * @param privKey the private key to derive the public key from + * @return BigInteger encoded public key + */ + public static BigInteger publicKeyFromPrivate(BigInteger privKey) { + ECPoint point = publicPointFromPrivate(privKey); + + byte[] encoded = point.getEncoded(false); + return new BigInteger(1, Arrays.copyOfRange(encoded, 1, encoded.length)); // remove prefix + } + + /** + * Returns public key point from the given private key. + * + * @param privKey the private key to derive the public key from + * @return ECPoint public key + */ + public static ECPoint publicPointFromPrivate(BigInteger privKey) { + /* + * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group + * order, but that could change in future versions. + */ + if (privKey.bitLength() > CURVE.getN().bitLength()) { + privKey = privKey.mod(CURVE.getN()); + } + return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey); + } + + /** + * Returns public key point from the given curve. + * + * @param bits representing the point on the curve + * @return BigInteger encoded public key + */ + public static BigInteger publicFromPoint(byte[] bits) { + return new BigInteger(1, Arrays.copyOfRange(bits, 1, bits.length)); // remove prefix + } + + public static class SignatureData { + private final byte[] v; + private final byte[] r; + private final byte[] s; + + public SignatureData(byte v, byte[] r, byte[] s) { + this(new byte[] {v}, r, s); + } + + public SignatureData(byte[] v, byte[] r, byte[] s) { + this.v = v; + this.r = r; + this.s = s; + } + + public byte[] getV() { + return v; + } + + public byte[] getR() { + return r; + } + + public byte[] getS() { + return s; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + SignatureData that = (SignatureData) o; + + if (!Arrays.equals(v, that.v)) { + return false; + } + if (!Arrays.equals(r, that.r)) { + return false; + } + return Arrays.equals(s, that.s); + } + + @Override + public int hashCode() { + int result = Arrays.hashCode(v); + result = 31 * result + Arrays.hashCode(r); + result = 31 * result + Arrays.hashCode(s); + return result; + } + } +} diff --git a/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java b/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java new file mode 100644 index 00000000000..b3c0f5b9d3e --- /dev/null +++ b/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.exceptions; + +/** Encoding exception. */ +public class MessageDecodingException extends RuntimeException { + public MessageDecodingException(String message) { + super(message); + } + + public MessageDecodingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java b/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java new file mode 100644 index 00000000000..c0f6662d7b0 --- /dev/null +++ b/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.exceptions; + +/** Encoding exception. */ +public class MessageEncodingException extends RuntimeException { + public MessageEncodingException(String message) { + super(message); + } + + public MessageEncodingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/p2p/src/main/java/org/web3j/utils/Assertions.java b/p2p/src/main/java/org/web3j/utils/Assertions.java new file mode 100644 index 00000000000..e1fb221f491 --- /dev/null +++ b/p2p/src/main/java/org/web3j/utils/Assertions.java @@ -0,0 +1,29 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.utils; + +/** Assertion utility functions. */ +public class Assertions { + + /** + * Verify that the provided precondition holds true. + * + * @param assertionResult assertion value + * @param errorMessage error message if precondition failure + */ + public static void verifyPrecondition(boolean assertionResult, String errorMessage) { + if (!assertionResult) { + throw new RuntimeException(errorMessage); + } + } +} diff --git a/p2p/src/main/java/org/web3j/utils/Numeric.java b/p2p/src/main/java/org/web3j/utils/Numeric.java new file mode 100644 index 00000000000..377159da729 --- /dev/null +++ b/p2p/src/main/java/org/web3j/utils/Numeric.java @@ -0,0 +1,252 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.utils; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Arrays; + +import org.web3j.exceptions.MessageDecodingException; +import org.web3j.exceptions.MessageEncodingException; + +/** + * Message codec functions. + * + *

Implementation as per https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding + */ +public final class Numeric { + + private static final String HEX_PREFIX = "0x"; + + private Numeric() {} + + public static String encodeQuantity(BigInteger value) { + if (value.signum() != -1) { + return HEX_PREFIX + value.toString(16); + } else { + throw new MessageEncodingException("Negative values are not supported"); + } + } + + public static BigInteger decodeQuantity(String value) { + if (isLongValue(value)) { + return BigInteger.valueOf(Long.parseLong(value)); + } + + if (!isValidHexQuantity(value)) { + throw new MessageDecodingException("Value must be in format 0x[1-9]+[0-9]* or 0x0"); + } + try { + return new BigInteger(value.substring(2), 16); + } catch (NumberFormatException e) { + throw new MessageDecodingException("Negative ", e); + } + } + + private static boolean isLongValue(String value) { + try { + Long.parseLong(value); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + private static boolean isValidHexQuantity(String value) { + if (value == null) { + return false; + } + + if (value.length() < 3) { + return false; + } + + if (!value.startsWith(HEX_PREFIX)) { + return false; + } + + // If TestRpc resolves the following issue, we can reinstate this code + // https://github.com/ethereumjs/testrpc/issues/220 + // if (value.length() > 3 && value.charAt(2) == '0') { + // return false; + // } + + return true; + } + + public static String cleanHexPrefix(String input) { + if (containsHexPrefix(input)) { + return input.substring(2); + } else { + return input; + } + } + + public static String prependHexPrefix(String input) { + if (!containsHexPrefix(input)) { + return HEX_PREFIX + input; + } else { + return input; + } + } + + public static boolean containsHexPrefix(String input) { + return !Strings.isEmpty(input) + && input.length() > 1 + && input.charAt(0) == '0' + && input.charAt(1) == 'x'; + } + + public static BigInteger toBigInt(byte[] value, int offset, int length) { + return toBigInt((Arrays.copyOfRange(value, offset, offset + length))); + } + + public static BigInteger toBigInt(byte[] value) { + return new BigInteger(1, value); + } + + public static BigInteger toBigInt(String hexValue) { + String cleanValue = cleanHexPrefix(hexValue); + return toBigIntNoPrefix(cleanValue); + } + + public static BigInteger toBigIntNoPrefix(String hexValue) { + return new BigInteger(hexValue, 16); + } + + public static String toHexStringWithPrefix(BigInteger value) { + return HEX_PREFIX + value.toString(16); + } + + public static String toHexStringNoPrefix(BigInteger value) { + return value.toString(16); + } + + public static String toHexStringNoPrefix(byte[] input) { + return toHexString(input, 0, input.length, false); + } + + public static String toHexStringWithPrefixZeroPadded(BigInteger value, int size) { + return toHexStringZeroPadded(value, size, true); + } + + public static String toHexStringWithPrefixSafe(BigInteger value) { + String result = toHexStringNoPrefix(value); + if (result.length() < 2) { + result = Strings.zeros(1) + result; + } + return HEX_PREFIX + result; + } + + public static String toHexStringNoPrefixZeroPadded(BigInteger value, int size) { + return toHexStringZeroPadded(value, size, false); + } + + private static String toHexStringZeroPadded(BigInteger value, int size, boolean withPrefix) { + String result = toHexStringNoPrefix(value); + + int length = result.length(); + if (length > size) { + throw new UnsupportedOperationException( + "Value " + result + "is larger then length " + size); + } else if (value.signum() < 0) { + throw new UnsupportedOperationException("Value cannot be negative"); + } + + if (length < size) { + result = Strings.zeros(size - length) + result; + } + + if (withPrefix) { + return HEX_PREFIX + result; + } else { + return result; + } + } + + public static byte[] toBytesPadded(BigInteger value, int length) { + byte[] result = new byte[length]; + byte[] bytes = value.toByteArray(); + + int bytesLength; + int srcOffset; + if (bytes[0] == 0) { + bytesLength = bytes.length - 1; + srcOffset = 1; + } else { + bytesLength = bytes.length; + srcOffset = 0; + } + + if (bytesLength > length) { + throw new RuntimeException("Input is too large to put in byte array of size " + length); + } + + int destOffset = length - bytesLength; + System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); + return result; + } + + public static byte[] hexStringToByteArray(String input) { + String cleanInput = cleanHexPrefix(input); + + int len = cleanInput.length(); + + if (len == 0) { + return new byte[] {}; + } + + byte[] data; + int startIdx; + if (len % 2 != 0) { + data = new byte[(len / 2) + 1]; + data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); + startIdx = 1; + } else { + data = new byte[len / 2]; + startIdx = 0; + } + + for (int i = startIdx; i < len; i += 2) { + data[(i + 1) / 2] = + (byte) + ((Character.digit(cleanInput.charAt(i), 16) << 4) + + Character.digit(cleanInput.charAt(i + 1), 16)); + } + return data; + } + + public static String toHexString(byte[] input, int offset, int length, boolean withPrefix) { + StringBuilder stringBuilder = new StringBuilder(); + if (withPrefix) { + stringBuilder.append("0x"); + } + for (int i = offset; i < offset + length; i++) { + stringBuilder.append(String.format("%02x", input[i] & 0xFF)); + } + + return stringBuilder.toString(); + } + + public static String toHexString(byte[] input) { + return toHexString(input, 0, input.length, true); + } + + public static byte asByte(int m, int n) { + return (byte) ((m << 4) | n); + } + + public static boolean isIntegerValue(BigDecimal value) { + return value.signum() == 0 || value.scale() <= 0 || value.stripTrailingZeros().scale() <= 0; + } +} diff --git a/p2p/src/main/java/org/web3j/utils/Strings.java b/p2p/src/main/java/org/web3j/utils/Strings.java new file mode 100644 index 00000000000..e21628ab1d9 --- /dev/null +++ b/p2p/src/main/java/org/web3j/utils/Strings.java @@ -0,0 +1,58 @@ +/* + * Copyright 2019 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.utils; + +import java.util.List; + +/** String utility functions. */ +public class Strings { + + private Strings() {} + + public static String toCsv(List src) { + // return src == null ? null : String.join(", ", src.toArray(new String[0])); + return join(src, ", "); + } + + public static String join(List src, String delimiter) { + return src == null ? null : String.join(delimiter, src.toArray(new String[0])); + } + + public static String capitaliseFirstLetter(String string) { + if (string == null || string.length() == 0) { + return string; + } else { + return string.substring(0, 1).toUpperCase() + string.substring(1); + } + } + + public static String lowercaseFirstLetter(String string) { + if (string == null || string.length() == 0) { + return string; + } else { + return string.substring(0, 1).toLowerCase() + string.substring(1); + } + } + + public static String zeros(int n) { + return repeat('0', n); + } + + public static String repeat(char value, int n) { + return new String(new char[n]).replace("\0", String.valueOf(value)); + } + + public static boolean isEmpty(String s) { + return s == null || s.length() == 0; + } +} diff --git a/p2p/src/main/protos/Connect.proto b/p2p/src/main/protos/Connect.proto new file mode 100644 index 00000000000..d03d123a963 --- /dev/null +++ b/p2p/src/main/protos/Connect.proto @@ -0,0 +1,60 @@ +syntax = "proto3"; + +import "Discover.proto"; + +option java_package = "org.tron.p2p.protos"; +option java_outer_classname = "Connect"; + +message KeepAliveMessage { + int64 timestamp = 1; +} + +message HelloMessage { + Endpoint from = 1; + int32 network_id = 2; + int32 code = 3; + int64 timestamp = 4; + int32 version = 5; +} + +message StatusMessage { + Endpoint from = 1; + int32 version = 2; + int32 network_id = 3; + int32 maxConnections = 4; + int32 currentConnections = 5; + int64 timestamp = 6; +} + +message CompressMessage { + enum CompressType { + uncompress = 0; + snappy = 1; + } + + CompressType type = 1; + bytes data = 2; +} + +enum DisconnectReason { + PEER_QUITING = 0x00; + BAD_PROTOCOL = 0x01; + TOO_MANY_PEERS = 0x02; + DUPLICATE_PEER = 0x03; + DIFFERENT_VERSION = 0x04; + RANDOM_ELIMINATION = 0x05; + EMPTY_MESSAGE = 0X06; + PING_TIMEOUT = 0x07; + DISCOVER_MODE = 0x08; + //DETECT_COMPLETE = 0x09; + NO_SUCH_MESSAGE = 0x0A; + BAD_MESSAGE = 0x0B; + TOO_MANY_PEERS_WITH_SAME_IP = 0x0C; + RECENT_DISCONNECT = 0x0D; + DUP_HANDSHAKE = 0x0E; + UNKNOWN = 0xFF; +} + +message P2pDisconnectMessage { + DisconnectReason reason = 1; +} \ No newline at end of file diff --git a/p2p/src/main/protos/Discover.proto b/p2p/src/main/protos/Discover.proto new file mode 100644 index 00000000000..8a53761115c --- /dev/null +++ b/p2p/src/main/protos/Discover.proto @@ -0,0 +1,50 @@ +syntax = "proto3"; + +option java_package = "org.tron.p2p.protos"; +option java_outer_classname = "Discover"; + +message Endpoint { + bytes address = 1; + int32 port = 2; + bytes nodeId = 3; + bytes addressIpv6 = 4; +} + +message PingMessage { + Endpoint from = 1; + Endpoint to = 2; + int32 version = 3; + int64 timestamp = 4; +} + +message PongMessage { + Endpoint from = 1; + int32 echo = 2; + int64 timestamp = 3; +} + +message FindNeighbours { + Endpoint from = 1; + bytes targetId = 2; + int64 timestamp = 3; +} + +message Neighbours { + Endpoint from = 1; + repeated Endpoint neighbours = 2; + int64 timestamp = 3; +} + +message EndPoints { + repeated Endpoint nodes = 1; +} + +message DnsRoot { + message TreeRoot { + bytes eRoot = 1; + bytes lRoot = 2; + int32 seq = 3; + } + TreeRoot treeRoot = 1; + bytes signature = 2; +} diff --git a/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java b/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java new file mode 100644 index 00000000000..bbd7fc20ec4 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java @@ -0,0 +1,130 @@ +package org.tron.p2p.connection; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.handshake.DisconnectCode; + +import java.lang.reflect.Field; +import java.net.InetAddress; +import java.net.InetSocketAddress; + +@Slf4j(topic = "net") +public class ChannelManagerTest { + + @Test + public synchronized void testGetConnectionNum() throws Exception{ + 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()); + + Channel c2 = new Channel(); + InetSocketAddress a2 = new InetSocketAddress("100.1.1.2", 100); + field = c2.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c2, a2.getAddress()); + + Channel c3 = new Channel(); + InetSocketAddress a3 = new InetSocketAddress("100.1.1.2", 99); + field = c3.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c3, a3.getAddress()); + + int cnt = ChannelManager.getConnectionNum(a1.getAddress()); + Assert.assertTrue(cnt == 0); + + ChannelManager.getChannels().put(a1, c1); + cnt = ChannelManager.getConnectionNum(a1.getAddress()); + Assert.assertTrue(cnt == 1); + + ChannelManager.getChannels().put(a2, c2); + cnt = ChannelManager.getConnectionNum(a2.getAddress()); + Assert.assertTrue(cnt == 1); + + ChannelManager.getChannels().put(a3, c3); + cnt = ChannelManager.getConnectionNum(a3.getAddress()); + Assert.assertTrue(cnt == 2); + } + + @Test + public synchronized void testNotifyDisconnect() throws Exception { + Channel c1 = new Channel(); + InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100); + + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); + field.setAccessible(true); + field.set(c1, a1); + + InetAddress inetAddress = a1.getAddress(); + field = c1.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c1, inetAddress); + + ChannelManager.getChannels().put(a1, c1); + + Long time = ChannelManager.getBannedNodes().getIfPresent(a1.getAddress()); + Assert.assertTrue(ChannelManager.getChannels().size() == 1); + Assert.assertTrue(time == null); + + ChannelManager.notifyDisconnect(c1); + time = ChannelManager.getBannedNodes().getIfPresent(a1.getAddress()); + Assert.assertTrue(time != null); + Assert.assertTrue(ChannelManager.getChannels().size() == 0); + } + + @Test + public synchronized void testProcessPeer() throws Exception { + clearChannels(); + Parameter.p2pConfig = new P2pConfig(); + + Channel c1 = new Channel(); + InetSocketAddress a1 = new InetSocketAddress("100.1.1.2", 100); + + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); + field.setAccessible(true); + field.set(c1, a1); + field = c1.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c1, a1.getAddress()); + + DisconnectCode code = ChannelManager.processPeer(c1); + Assert.assertTrue(code.equals(DisconnectCode.NORMAL)); + + Thread.sleep(5); + + Parameter.p2pConfig.setMaxConnections(1); + + Channel c2 = new Channel(); + InetSocketAddress a2 = new InetSocketAddress("100.1.1.2", 99); + + field = c2.getClass().getDeclaredField("inetSocketAddress"); + field.setAccessible(true); + field.set(c2, a2); + field = c2.getClass().getDeclaredField("inetAddress"); + field.setAccessible(true); + field.set(c2, a2.getAddress()); + + code = ChannelManager.processPeer(c2); + Assert.assertTrue(code.equals(DisconnectCode.TOO_MANY_PEERS)); + + Parameter.p2pConfig.setMaxConnections(2); + Parameter.p2pConfig.setMaxConnectionsWithSameIp(1); + code = ChannelManager.processPeer(c2); + Assert.assertTrue(code.equals(DisconnectCode.MAX_CONNECTION_WITH_SAME_IP)); + + Parameter.p2pConfig.setMaxConnectionsWithSameIp(2); + c1.setNodeId("cc"); + c2.setNodeId("cc"); + code = ChannelManager.processPeer(c2); + Assert.assertTrue(code.equals(DisconnectCode.DUPLICATE_PEER)); + } + + private void clearChannels() { + ChannelManager.getChannels().clear(); + ChannelManager.getBannedNodes().invalidateAll(); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java b/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java new file mode 100644 index 00000000000..bd9849fbaea --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java @@ -0,0 +1,131 @@ +package org.tron.p2p.connection; + + +import java.lang.reflect.Field; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.pool.ConnPoolService; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.NodeManager; + +public class ConnPoolServiceTest { + + private static String localIp = "127.0.0.1"; + private static int port = 10000; + + @BeforeClass + public static void init() { + Parameter.p2pConfig = new P2pConfig(); + Parameter.p2pConfig.setDiscoverEnable(false); + Parameter.p2pConfig.setPort(port); + + NodeManager.init(); + ChannelManager.init(); + } + + private void clearChannels() { + ChannelManager.getChannels().clear(); + ChannelManager.getBannedNodes().invalidateAll(); + } + + @Test + public void getNodes_chooseHomeNode() { + InetSocketAddress localAddress = new InetSocketAddress(Parameter.p2pConfig.getIp(), + Parameter.p2pConfig.getPort()); + Set inetInUse = new HashSet<>(); + inetInUse.add(localAddress); + + List connectableNodes = new ArrayList<>(); + connectableNodes.add(NodeManager.getHomeNode()); + + ConnPoolService connPoolService = new ConnPoolService(); + List nodes = connPoolService.getNodes(new HashSet<>(), inetInUse, connectableNodes, + 1); + Assert.assertEquals(0, nodes.size()); + + nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, + 1); + Assert.assertEquals(1, nodes.size()); + } + + @Test + public void getNodes_orderByUpdateTimeDesc() throws Exception { + clearChannels(); + Node node1 = new Node(new InetSocketAddress(localIp, 90)); + Field field = node1.getClass().getDeclaredField("updateTime"); + field.setAccessible(true); + field.set(node1, System.currentTimeMillis()); + + Node node2 = new Node(new InetSocketAddress(localIp, 100)); + field = node2.getClass().getDeclaredField("updateTime"); + field.setAccessible(true); + field.set(node2, System.currentTimeMillis() + 10); + + Assert.assertTrue(node1.getUpdateTime() < node2.getUpdateTime()); + + List connectableNodes = new ArrayList<>(); + connectableNodes.add(node1); + connectableNodes.add(node2); + + ConnPoolService connPoolService = new ConnPoolService(); + List nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, + 2); + Assert.assertEquals(2, nodes.size()); + Assert.assertTrue(nodes.get(0).getUpdateTime() > nodes.get(1).getUpdateTime()); + + int limit = 1; + List nodes2 = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, + limit); + Assert.assertEquals(limit, nodes2.size()); + } + + @Test + public void getNodes_banNode() throws InterruptedException { + clearChannels(); + InetSocketAddress inetSocketAddress = new InetSocketAddress(localIp, 90); + long banTime = 500L; + ChannelManager.banNode(inetSocketAddress.getAddress(), banTime); + Node node = new Node(inetSocketAddress); + List connectableNodes = new ArrayList<>(); + connectableNodes.add(node); + + ConnPoolService connPoolService = new ConnPoolService(); + List nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, + 1); + Assert.assertEquals(0, nodes.size()); + Thread.sleep(2 * banTime); + + nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, 1); + Assert.assertEquals(1, nodes.size()); + } + + @Test + public void getNodes_nodeInUse() { + clearChannels(); + InetSocketAddress inetSocketAddress = new InetSocketAddress(localIp, 90); + Node node = new Node(inetSocketAddress); + List connectableNodes = new ArrayList<>(); + connectableNodes.add(node); + + Set nodesInUse = new HashSet<>(); + nodesInUse.add(node.getHexId()); + ConnPoolService connPoolService = new ConnPoolService(); + List nodes = connPoolService.getNodes(nodesInUse, new HashSet<>(), connectableNodes, 1); + Assert.assertEquals(0, nodes.size()); + } + + @AfterClass + public static void destroy() { + NodeManager.close(); + ChannelManager.close(); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java b/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java new file mode 100644 index 00000000000..17ca0094d8d --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java @@ -0,0 +1,88 @@ +package org.tron.p2p.connection; + + +import static org.tron.p2p.base.Parameter.NETWORK_TIME_DIFF; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.business.handshake.DisconnectCode; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.connection.message.MessageType; +import org.tron.p2p.connection.message.handshake.HelloMessage; +import org.tron.p2p.connection.message.keepalive.PingMessage; +import org.tron.p2p.connection.message.keepalive.PongMessage; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.exception.P2pException.TypeEnum; +import org.tron.p2p.protos.Connect; +import org.tron.p2p.protos.Connect.KeepAliveMessage; + +public class MessageTest { + + @Before + public void init() { + Parameter.p2pConfig = new P2pConfig(); + } + + @Test + public void testPing() { + PingMessage pingMessage = new PingMessage(); + byte[] messageData = pingMessage.getSendData(); + try { + Message message = Message.parse(messageData); + Assert.assertEquals(MessageType.KEEP_ALIVE_PING, message.getType()); + } catch (P2pException e) { + Assert.fail(); + } + } + + @Test + public void testPong() { + PongMessage pongMessage = new PongMessage(); + byte[] messageData = pongMessage.getSendData(); + try { + Message message = Message.parse(messageData); + Assert.assertEquals(MessageType.KEEP_ALIVE_PONG, message.getType()); + } catch (P2pException e) { + Assert.fail(); + } + } + + @Test + public void testHandShakeHello() { + HelloMessage helloMessage = new HelloMessage(DisconnectCode.NORMAL, 0); + byte[] messageData = helloMessage.getSendData(); + try { + Message message = Message.parse(messageData); + Assert.assertEquals(MessageType.HANDSHAKE_HELLO, message.getType()); + } catch (P2pException e) { + Assert.fail(); + } + } + + @Test + public void testUnKnownType() { + PingMessage pingMessage = new PingMessage(); + byte[] messageData = pingMessage.getSendData(); + messageData[0] = (byte) 0x00; + try { + Message.parse(messageData); + } catch (P2pException e) { + Assert.assertEquals(TypeEnum.NO_SUCH_MESSAGE, e.getType()); + } + } + + @Test + public void testInvalidTime() { + KeepAliveMessage keepAliveMessage = Connect.KeepAliveMessage.newBuilder() + .setTimestamp(System.currentTimeMillis() + NETWORK_TIME_DIFF * 2).build(); + try { + PingMessage message = new PingMessage(keepAliveMessage.toByteArray()); + Assert.assertFalse(message.valid()); + } catch (Exception e) { + Assert.fail(); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java b/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java new file mode 100644 index 00000000000..6dfb5232039 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java @@ -0,0 +1,77 @@ +package org.tron.p2p.connection; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.connection.message.Message; +import org.tron.p2p.discover.NodeManager; + +public class SocketTest { + + private static String localIp = "127.0.0.1"; + private static int port = 10001; + + @Before + public void init() { + Parameter.p2pConfig = new P2pConfig(); + Parameter.p2pConfig.setIp(localIp); + Parameter.p2pConfig.setPort(port); + Parameter.p2pConfig.setDiscoverEnable(false); + + NodeManager.init(); + ChannelManager.init(); + } + + private boolean sendMessage(io.netty.channel.Channel nettyChannel, Message message) { + AtomicBoolean sendSuccess = new AtomicBoolean(false); + nettyChannel.writeAndFlush(Unpooled.wrappedBuffer(message.getSendData())) + .addListener((ChannelFutureListener) future -> { + if (future.isSuccess()) { + sendSuccess.set(true); + } else { + sendSuccess.set(false); + } + }); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return sendSuccess.get(); + } + + //if we start handshake, we cannot connect with localhost, this test case will be invalid + @Test + public void testPeerServerAndPeerClient() throws InterruptedException { +// //wait some time until peer server thread starts at this port successfully +// Thread.sleep(500); +// Node serverNode = new Node(new InetSocketAddress(localIp, port)); +// +// //peer client try to connect peer server using random port +// io.netty.channel.Channel nettyChannel = ChannelManager.getPeerClient() +// .connectAsync(serverNode, false, false).channel(); +// +// while (true) { +// if (!nettyChannel.isActive()) { +// Thread.sleep(100); +// } else { +// System.out.println("send message test"); +// PingMessage pingMessage = new PingMessage(); +// boolean sendSuccess = sendMessage(nettyChannel, pingMessage); +// Assert.assertTrue(sendSuccess); +// break; +// } +// } + } + + @After + public void destroy() { + NodeManager.close(); + ChannelManager.close(); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java b/p2p/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java new file mode 100644 index 00000000000..41188f570ad --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/connection/message/handshake/HelloMessageTest.java @@ -0,0 +1,33 @@ +package org.tron.p2p.connection.message.handshake; + +import static org.tron.p2p.base.Parameter.p2pConfig; + +import java.util.Arrays; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.connection.business.handshake.DisconnectCode; +import org.tron.p2p.connection.message.MessageType; + +public class HelloMessageTest { + + @Test + public void testHelloMessage() throws Exception { + p2pConfig = new P2pConfig(); + HelloMessage m1 = new HelloMessage(DisconnectCode.NORMAL, 0); + Assert.assertEquals(0, m1.getCode()); + + Assert.assertTrue(Arrays.equals(p2pConfig.getNodeID(), m1.getFrom().getId())); + Assert.assertEquals(p2pConfig.getPort(), m1.getFrom().getPort()); + Assert.assertEquals(p2pConfig.getIp(), m1.getFrom().getHostV4()); + Assert.assertEquals(p2pConfig.getNetworkId(), m1.getNetworkId()); + Assert.assertEquals(MessageType.HANDSHAKE_HELLO, m1.getType()); + + HelloMessage m2 = new HelloMessage(m1.getData()); + Assert.assertTrue(Arrays.equals(p2pConfig.getNodeID(), m2.getFrom().getId())); + Assert.assertEquals(p2pConfig.getPort(), m2.getFrom().getPort()); + Assert.assertEquals(p2pConfig.getIp(), m2.getFrom().getHostV4()); + Assert.assertEquals(p2pConfig.getNetworkId(), m2.getNetworkId()); + Assert.assertEquals(MessageType.HANDSHAKE_HELLO, m2.getType()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/NodeManagerTest.java b/p2p/src/test/java/org/tron/p2p/discover/NodeManagerTest.java new file mode 100644 index 00000000000..3cb13ef509d --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/NodeManagerTest.java @@ -0,0 +1,25 @@ +package org.tron.p2p.discover; + +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; + +public class NodeManagerTest { + @Test + public void testNoSeeds() { + P2pConfig config = new P2pConfig(); + Parameter.p2pConfig = config; + try { + NodeManager.init(); + Thread.sleep(100); + Assert.assertEquals(0, NodeManager.getAllNodes().size()); + Assert.assertEquals(0, NodeManager.getTableNodes().size()); + Assert.assertEquals(0, NodeManager.getConnectableNodes().size()); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + NodeManager.close(); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/NodeTest.java b/p2p/src/test/java/org/tron/p2p/discover/NodeTest.java new file mode 100644 index 00000000000..9ceea0b7090 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/NodeTest.java @@ -0,0 +1,87 @@ +package org.tron.p2p.discover; + +import java.net.InetSocketAddress; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.utils.NetUtil; + +public class NodeTest { + + @Before + public void init() { + Parameter.p2pConfig = new P2pConfig(); + } + + @Test + public void nodeTest() throws InterruptedException { + Node node1 = new Node(new InetSocketAddress("127.0.0.1", 10001)); + Assert.assertEquals(64, node1.getId().length); + + Node node2 = new Node(NetUtil.getNodeId(), "127.0.0.1", null, 10002); + boolean isDif = node1.equals(node2); + Assert.assertFalse(isDif); + + long lastModifyTime = node1.getUpdateTime(); + Thread.sleep(1); + node1.touch(); + Assert.assertNotEquals(lastModifyTime, node1.getUpdateTime()); + + node1.setP2pVersion(11111); + Assert.assertTrue(node1.isConnectible(11111)); + Assert.assertFalse(node1.isConnectible(11112)); + Node node3 = new Node(NetUtil.getNodeId(), "127.0.0.1", null, 10003, 10004); + node3.setP2pVersion(11111); + Assert.assertFalse(node3.isConnectible(11111)); + } + + @Test + public void ipV4CompatibleTest() { + Parameter.p2pConfig.setIp("127.0.0.1"); + Parameter.p2pConfig.setIpv6(null); + + Node node1 = new Node(NetUtil.getNodeId(), "127.0.0.1", null, 10002); + Assert.assertNotNull(node1.getPreferInetSocketAddress()); + + Node node2 = new Node(NetUtil.getNodeId(), null, "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNull(node2.getPreferInetSocketAddress()); + + Node node3 = new Node(NetUtil.getNodeId(), "127.0.0.1", "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNotNull(node3.getPreferInetSocketAddress()); + } + + @Test + public void ipV6CompatibleTest() { + Parameter.p2pConfig.setIp(null); + Parameter.p2pConfig.setIpv6("fe80:0:0:0:204:61ff:fe9d:f157"); + + Node node1 = new Node(NetUtil.getNodeId(), "127.0.0.1", null, 10002); + Assert.assertNull(node1.getPreferInetSocketAddress()); + + Node node2 = new Node(NetUtil.getNodeId(), null, "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNotNull(node2.getPreferInetSocketAddress()); + + Node node3 = new Node(NetUtil.getNodeId(), "127.0.0.1", "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNotNull(node3.getPreferInetSocketAddress()); + } + + @Test + public void ipCompatibleTest() { + Parameter.p2pConfig.setIp("127.0.0.1"); + Parameter.p2pConfig.setIpv6("fe80:0:0:0:204:61ff:fe9d:f157"); + + Node node1 = new Node(NetUtil.getNodeId(), "127.0.0.1", null, 10002); + Assert.assertNotNull(node1.getPreferInetSocketAddress()); + + Node node2 = new Node(NetUtil.getNodeId(), null, "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNotNull(node2.getPreferInetSocketAddress()); + + Node node3 = new Node(NetUtil.getNodeId(), "127.0.0.1", "fe80:0:0:0:204:61ff:fe9d:f156", 10002); + Assert.assertNotNull(node3.getPreferInetSocketAddress()); + + Node node4 = new Node(NetUtil.getNodeId(), null, null, 10002); + Assert.assertNull(node4.getPreferInetSocketAddress()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java new file mode 100644 index 00000000000..b1b895e58bc --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java @@ -0,0 +1,54 @@ +package org.tron.p2p.discover.protocol.kad; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.kad.PingMessage; +import org.tron.p2p.discover.socket.UdpEvent; + +import java.net.InetSocketAddress; + +public class KadServiceTest { + + private static KadService kadService; + private static Node node1; + private static Node node2; + + @BeforeClass + public static void init() { + Parameter.p2pConfig = new P2pConfig(); + Parameter.p2pConfig.setDiscoverEnable(false); + kadService = new KadService(); + kadService.init(); + KadService.setPingTimeout(300); + node1 = new Node(new InetSocketAddress("127.0.0.1", 22222)); + node2 = new Node(new InetSocketAddress("127.0.0.2", 22222)); + } + + @Test + public void test() { + Assert.assertNotNull(kadService.getPongTimer()); + Assert.assertNotNull(kadService.getPublicHomeNode()); + Assert.assertEquals(0, kadService.getAllNodes().size()); + + NodeHandler nodeHandler = kadService.getNodeHandler(node1); + Assert.assertNotNull(nodeHandler); + Assert.assertEquals(1, kadService.getAllNodes().size()); + + UdpEvent event = new UdpEvent(new PingMessage(node2, kadService.getPublicHomeNode()), + new InetSocketAddress(node2.getHostV4(), node2.getPort())); + kadService.handleEvent(event); + Assert.assertEquals(2, kadService.getAllNodes().size()); + + } + + + @AfterClass + public static void destroy() { + kadService.close(); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java new file mode 100644 index 00000000000..215e4192e1f --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java @@ -0,0 +1,88 @@ +package org.tron.p2p.discover.protocol.kad; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.discover.Node; +import org.tron.p2p.discover.message.kad.PingMessage; +import org.tron.p2p.discover.message.kad.PongMessage; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.net.InetSocketAddress; + +public class NodeHandlerTest { + + private static KadService kadService; + private static Node currNode; + private static Node oldNode; + private static Node replaceNode; + private static NodeHandler currHandler; + private static NodeHandler oldHandler; + private static NodeHandler replaceHandler; + + @BeforeClass + public static void init() { + Parameter.p2pConfig = new P2pConfig(); + Parameter.p2pConfig.setDiscoverEnable(false); + kadService = new KadService(); + kadService.init(); + KadService.setPingTimeout(300); + currNode = new Node(new InetSocketAddress("127.0.0.1", 22222)); + oldNode = new Node(new InetSocketAddress("127.0.0.2", 22222)); + replaceNode = new Node(new InetSocketAddress("127.0.0.3", 22222)); + currHandler = new NodeHandler(currNode, kadService); + oldHandler = new NodeHandler(oldNode, kadService); + replaceHandler = new NodeHandler(replaceNode, kadService); + } + + @Test + public void test() throws InterruptedException { + Assert.assertEquals(NodeHandler.State.DISCOVERED, currHandler.getState()); + Assert.assertEquals(NodeHandler.State.DISCOVERED, oldHandler.getState()); + Assert.assertEquals(NodeHandler.State.DISCOVERED, replaceHandler.getState()); + Thread.sleep(2000); + Assert.assertEquals(NodeHandler.State.DEAD, currHandler.getState()); + Assert.assertEquals(NodeHandler.State.DEAD, oldHandler.getState()); + Assert.assertEquals(NodeHandler.State.DEAD, replaceHandler.getState()); + + PingMessage msg = new PingMessage(currNode, kadService.getPublicHomeNode()); + currHandler.handlePing(msg); + Assert.assertEquals(NodeHandler.State.DISCOVERED, currHandler.getState()); + PongMessage msg1 = new PongMessage(currNode); + currHandler.handlePong(msg1); + Assert.assertEquals(NodeHandler.State.ACTIVE, currHandler.getState()); + Assert.assertTrue(kadService.getTable().contains(currNode)); + kadService.getTable().dropNode(currNode); + } + + @Test + public void testChangeState() throws Exception { + currHandler.changeState(NodeHandler.State.ALIVE); + Assert.assertEquals(NodeHandler.State.ACTIVE, currHandler.getState()); + Assert.assertTrue(kadService.getTable().contains(currNode)); + + Class clazz = NodeHandler.class; + Constructor cn = clazz.getDeclaredConstructor(Node.class, KadService.class); + NodeHandler nh = cn.newInstance(oldNode, kadService); + Field declaredField = clazz.getDeclaredField("replaceCandidate"); + declaredField.setAccessible(true); + declaredField.set(nh, replaceHandler); + + kadService.getTable().addNode(oldNode); + nh.changeState(NodeHandler.State.EVICTCANDIDATE); + nh.changeState(NodeHandler.State.DEAD); + replaceHandler.changeState(NodeHandler.State.ALIVE); + + Assert.assertFalse(kadService.getTable().contains(oldNode)); + Assert.assertTrue(kadService.getTable().contains(replaceNode)); + } + + @AfterClass + public static void destroy() { + kadService.close(); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java new file mode 100644 index 00000000000..5f52f036dc4 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java @@ -0,0 +1,70 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.discover.Node; +import org.tron.p2p.utils.ByteArray; +import org.tron.p2p.utils.NetUtil; + +import java.net.InetSocketAddress; + +public class NodeEntryTest { + @Test + public void test() throws InterruptedException { + Node node1 = new Node(new InetSocketAddress("127.0.0.1", 10001)); + NodeEntry nodeEntry = new NodeEntry(NetUtil.getNodeId(), node1); + + long lastModified = nodeEntry.getModified(); + Thread.sleep(1); + nodeEntry.touch(); + long nowModified = nodeEntry.getModified(); + Assert.assertNotEquals(lastModified, nowModified); + + Node node2 = new Node(new InetSocketAddress("127.0.0.1", 10002)); + NodeEntry nodeEntry2 = new NodeEntry(NetUtil.getNodeId(), node2); + boolean isDif = nodeEntry.equals(nodeEntry2); + Assert.assertTrue(isDif); + } + + @Test + public void testDistance() { + byte[] randomId = NetUtil.getNodeId(); + String hexRandomIdStr = ByteArray.toHexString(randomId); + Assert.assertEquals(128, hexRandomIdStr.length()); + + byte[] nodeId1 = ByteArray.fromHexString( + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId2 = ByteArray.fromHexString( + "a000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertEquals(17, NodeEntry.distance(nodeId1, nodeId2)); + + byte[] nodeId3 = ByteArray.fromHexString( + "0000800000000000000000000000000000000000000000000000000000000001" + + "0000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertEquals(1, NodeEntry.distance(nodeId1, nodeId3)); + + byte[] nodeId4 = ByteArray.fromHexString( + "0000400000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertEquals(0, NodeEntry.distance(nodeId1, nodeId4)); // => 0 + + byte[] nodeId5 = ByteArray.fromHexString( + "0000200000000000000000000000000000000000000000000000000000000000" + + "4000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertEquals(-1, NodeEntry.distance(nodeId1, nodeId5)); // => 0 + + byte[] nodeId6 = ByteArray.fromHexString( + "0000100000000000000000000000000000000000000000000000000000000000" + + "2000000000000000000000000000000000000000000000000000000000000000"); + Assert.assertEquals(-2, NodeEntry.distance(nodeId1, nodeId6)); // => 0 + + byte[] nodeId7 = ByteArray.fromHexString( + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000001"); + Assert.assertEquals(-494, NodeEntry.distance(nodeId1, nodeId7)); // => 0 + + Assert.assertEquals(-495, NodeEntry.distance(nodeId1, nodeId1)); // => 0 + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java new file mode 100644 index 00000000000..fe0990f59f5 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java @@ -0,0 +1,202 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.p2p.discover.Node; +import org.tron.p2p.utils.NetUtil; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class NodeTableTest { + + private Node homeNode; + private NodeTable nodeTable; + private String[] ips; + private List ids; + + @Test + public void test() { + Node node1 = new Node(new InetSocketAddress("127.0.0.1", 10002)); + + NodeTable table = new NodeTable(node1); + Node nodeTemp = table.getNode(); + Assert.assertEquals(10002, nodeTemp.getPort()); + Assert.assertEquals(0, table.getNodesCount()); + Assert.assertEquals(0, table.getBucketsCount()); + + Node node2 = new Node(new InetSocketAddress("127.0.0.2", 10003)); + Node node3 = new Node(new InetSocketAddress("127.0.0.3", 10004)); + table.addNode(node2); + table.addNode(node3); + int bucketsCount = table.getBucketsCount(); + int nodeCount = table.getNodesCount(); + Assert.assertEquals(2, nodeCount); + Assert.assertTrue(bucketsCount > 0); + + boolean isExist = table.contains(node2); + table.touchNode(node2); + Assert.assertTrue(isExist); + + byte[] targetId = NetUtil.getNodeId(); + List nodeList = table.getClosestNodes(targetId); + Assert.assertFalse(nodeList.isEmpty()); + } + + /** + * init nodes for test. + */ + @Before + public void init() { + ids = new ArrayList<>(); + for (int i = 0; i < KademliaOptions.BUCKET_SIZE + 1; i++) { + byte[] id = new byte[64]; + id[0] = 17; + id[1] = 16; + if (i < 10) { + id[63] = (byte) i; + } else { + id[62] = 1; + id[63] = (byte) (i - 10); + } + ids.add(id); + } + + ips = new String[KademliaOptions.BUCKET_SIZE + 1]; + byte[] homeId = new byte[64]; + homeNode = new Node(homeId, "127.0.0.1", null, 18888, 18888); + nodeTable = new NodeTable(homeNode); + ips[0] = "127.0.0.2"; + ips[1] = "127.0.0.3"; + ips[2] = "127.0.0.4"; + ips[3] = "127.0.0.5"; + ips[4] = "127.0.0.6"; + ips[5] = "127.0.0.7"; + ips[6] = "127.0.0.8"; + ips[7] = "127.0.0.9"; + ips[8] = "127.0.0.10"; + ips[9] = "127.0.0.11"; + ips[10] = "127.0.0.12"; + ips[11] = "127.0.0.13"; + ips[12] = "127.0.0.14"; + ips[13] = "127.0.0.15"; + ips[14] = "127.0.0.16"; + ips[15] = "127.0.0.17"; + ips[16] = "127.0.0.18"; + } + + @Test + public void addNodeTest() { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + Assert.assertEquals(0, nodeTable.getNodesCount()); + nodeTable.addNode(node); + Assert.assertEquals(1, nodeTable.getNodesCount()); + Assert.assertTrue(nodeTable.contains(node)); + } + + @Test + public void addDupNodeTest() throws Exception { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + nodeTable.addNode(node); + long firstTouchTime = nodeTable.getAllNodes().get(0).getModified(); + TimeUnit.MILLISECONDS.sleep(20); + nodeTable.addNode(node); + long lastTouchTime = nodeTable.getAllNodes().get(0).getModified(); + Assert.assertTrue(lastTouchTime > firstTouchTime); + Assert.assertEquals(1, nodeTable.getNodesCount()); + } + + @Test + public void addNode_bucketFullTest() throws Exception { + for (int i = 0; i < KademliaOptions.BUCKET_SIZE; i++) { + TimeUnit.MILLISECONDS.sleep(10); + addNode(new Node(ids.get(i), ips[i], null, 18888, 18888)); + } + Node lastSeen = nodeTable.addNode(new Node(ids.get(16), ips[16], null, 18888, 18888)); + Assert.assertTrue(null != lastSeen); + Assert.assertEquals(ips[15], lastSeen.getHostV4()); + } + + public void addNode(Node n) { + nodeTable.addNode(n); + } + + @Test + public void dropNodeTest() { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + nodeTable.addNode(node); + Assert.assertTrue(nodeTable.contains(node)); + nodeTable.dropNode(node); + Assert.assertTrue(!nodeTable.contains(node)); + nodeTable.addNode(node); + nodeTable.dropNode(new Node(ids.get(1), ips[0], null, 10000, 10000)); + Assert.assertTrue(!nodeTable.contains(node)); + } + + @Test + public void getBucketsCountTest() { + Assert.assertEquals(0, nodeTable.getBucketsCount()); + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + nodeTable.addNode(node); + Assert.assertEquals(1, nodeTable.getBucketsCount()); + } + + @Test + public void touchNodeTest() throws Exception { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + nodeTable.addNode(node); + long firstTouchTime = nodeTable.getAllNodes().get(0).getModified(); + TimeUnit.MILLISECONDS.sleep(10); + nodeTable.touchNode(node); + long lastTouchTime = nodeTable.getAllNodes().get(0).getModified(); + Assert.assertTrue(firstTouchTime < lastTouchTime); + } + + @Test + public void containsTest() { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); + Assert.assertTrue(!nodeTable.contains(node)); + nodeTable.addNode(node); + Assert.assertTrue(nodeTable.contains(node)); + } + + @Test + public void getBuckIdTest() { + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); //id: 11100...000 + nodeTable.addNode(node); + NodeEntry nodeEntry = new NodeEntry(homeNode.getId(), node); + Assert.assertEquals(13, nodeTable.getBucketId(nodeEntry)); + } + + @Test + public void getClosestNodes_nodesMoreThanBucketCapacity() throws Exception { + byte[] bytes = new byte[64]; + bytes[0] = 15; + Node nearNode = new Node(bytes, "127.0.0.19", null, 18888, 18888); + bytes[0] = 70; + Node farNode = new Node(bytes, "127.0.0.20", null, 18888, 18888); + nodeTable.addNode(nearNode); + nodeTable.addNode(farNode); + for (int i = 0; i < KademliaOptions.BUCKET_SIZE - 1; i++) { + //To control totally 17 nodes, however closest's capacity is 16 + nodeTable.addNode(new Node(ids.get(i), ips[i], null, 18888, 18888)); + TimeUnit.MILLISECONDS.sleep(10); + } + Assert.assertTrue(nodeTable.getBucketsCount() > 1); + //3 buckets, nearnode's distance is 252, far's is 255, others' are 253 + List closest = nodeTable.getClosestNodes(homeNode.getId()); + Assert.assertTrue(closest.contains(nearNode)); + //the farest node should be excluded + } + + @Test + public void getClosestNodes_isDiscoverNode() { + Node node = new Node(ids.get(0), ips[0], null, 18888); + nodeTable.addNode(node); + List closest = nodeTable.getClosestNodes(homeNode.getId()); + Assert.assertFalse(closest.isEmpty()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java new file mode 100644 index 00000000000..b1e6325a3e6 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java @@ -0,0 +1,23 @@ +package org.tron.p2p.discover.protocol.kad.table; + +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.discover.Node; +import org.tron.p2p.utils.NetUtil; + +import java.net.InetSocketAddress; + +public class TimeComparatorTest { + @Test + public void test() throws InterruptedException { + Node node1 = new Node(new InetSocketAddress("127.0.0.1", 10001)); + NodeEntry ne1 = new NodeEntry(NetUtil.getNodeId(), node1); + Thread.sleep(1); + Node node2 = new Node(new InetSocketAddress("127.0.0.1", 10002)); + NodeEntry ne2 = new NodeEntry(NetUtil.getNodeId(), node2); + TimeComparator tc = new TimeComparator(); + int result = tc.compare(ne1, ne2); + Assert.assertEquals(1, result); + + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java b/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java new file mode 100644 index 00000000000..156844ea376 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java @@ -0,0 +1,106 @@ +package org.tron.p2p.dns; + + +import com.google.protobuf.ByteString; +import java.math.BigInteger; +import java.security.SignatureException; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.dns.tree.Algorithm; +import org.tron.p2p.protos.Discover.DnsRoot.TreeRoot; +import org.tron.p2p.utils.ByteArray; + +public class AlgorithmTest { + + public static String privateKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"; + + @Test + public void testPublicKeyCompressAndUnCompress() { + BigInteger publicKeyInt = Algorithm.generateKeyPair(privateKey).getPublicKey(); + + String publicKey = ByteArray.toHexString(publicKeyInt.toByteArray()); + String pubKeyCompressHex = Algorithm.compressPubKey(publicKeyInt); + String base32PubKey = Algorithm.encode32(ByteArray.fromHexString(pubKeyCompressHex)); + Assert.assertEquals("APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ", base32PubKey); + String unCompressPubKey = Algorithm.decompressPubKey(pubKeyCompressHex); + Assert.assertEquals(publicKey, unCompressPubKey); + } + + @Test + public void testSignatureAndVerify() { + BigInteger publicKeyInt = Algorithm.generateKeyPair(privateKey).getPublicKey(); + String publicKey = ByteArray.toHexString(publicKeyInt.toByteArray()); + + String msg = "Message for signing"; + byte[] sig = Algorithm.sigData(msg, privateKey); + try { + Assert.assertTrue(Algorithm.verifySignature(publicKey, msg, sig)); + } catch (SignatureException e) { + Assert.fail(); + } + } + + @Test + public void testEncode32() { + String content = "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"; + String base32 = Algorithm.encode32(content.getBytes()); + Assert.assertArrayEquals(content.getBytes(), Algorithm.decode32(base32)); + + Assert.assertEquals("USBZA4IGXFNVDBBQACEK3FGLWM", Algorithm.encode32AndTruncate(content)); + } + + @Test + public void testValidHash() { + Assert.assertTrue(Algorithm.isValidHash("C7HRFPF3BLGF3YR4DY5KX3SMBE")); + Assert.assertFalse(Algorithm.isValidHash("C7HRFPF3BLGF3YR4DY5KX3SMBE======")); + } + + @Test + public void testEncode64() { + String base64Sig = "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE"; + byte[] decoded; + try { + decoded = Algorithm.decode64(base64Sig); + Assert.assertEquals(base64Sig, Algorithm.encode64(decoded)); + } catch (Exception e) { + Assert.fail(); + } + + String base64Content = "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE="; + decoded = Algorithm.decode64(base64Content); + Assert.assertNotEquals(base64Content, Algorithm.encode64(decoded)); + } + + @Test + public void testRecoverPublicKey() { + TreeRoot.Builder builder = TreeRoot.newBuilder(); + builder.setERoot(ByteString.copyFrom("VXJIDGQECCIIYNY3GZEJSFSG6U".getBytes())); + builder.setLRoot(ByteString.copyFrom("FDXN3SN67NA5DKA4J2GOK7BVQI".getBytes())); + builder.setSeq(3447); + + //String eth_msg = "enrtree-root:v1 e=VXJIDGQECCIIYNY3GZEJSFSG6U l=FDXN3SN67NA5DKA4J2GOK7BVQI seq=3447"; + String msg = builder.toString(); + byte[] sig = Algorithm.sigData(builder.toString(), privateKey); + Assert.assertEquals(65, sig.length); + String base64Sig = Algorithm.encode64(sig); + Assert.assertEquals( + "_Zfgv2g7IUzjhqkMGCPZuPT_HAA01hTxiKAa3D1dyokk8_OKee-Jy2dSNo-nqEr6WOFkxv3A9ukYuiJRsf2v8hs", + base64Sig); + + byte[] sigData; + try { + sigData = Algorithm.decode64(base64Sig); + Assert.assertArrayEquals(sig, sigData); + } catch (Exception e) { + Assert.fail(); + } + + BigInteger publicKeyInt = Algorithm.generateKeyPair(privateKey).getPublicKey(); + try { + BigInteger recoverPublicKeyInt = Algorithm.recoverPublicKey(msg, sig); + Assert.assertEquals(publicKeyInt, recoverPublicKeyInt); + } catch (SignatureException e) { + Assert.fail(); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java b/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java new file mode 100644 index 00000000000..fff6e677181 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java @@ -0,0 +1,151 @@ +package org.tron.p2p.dns; + + +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.dns.update.AwsClient; +import org.tron.p2p.dns.update.AwsClient.RecordSet; +import org.tron.p2p.dns.update.PublishConfig; +import org.tron.p2p.exception.DnsException; +import software.amazon.awssdk.services.route53.model.Change; +import software.amazon.awssdk.services.route53.model.ChangeAction; + +public class AwsRoute53Test { + + @Test + public void testChangeSort() { + + Map existing = new HashMap<>(); + existing.put("n", new RecordSet(new String[] { + "tree-root-v1:CjoKGlVKQU9JQlMyUFlZMjJYUU1WRlNXT1RZSlhVEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgIEldBTE5aWHEyRkk5Ui1ubjdHQk9HdWJBRFVPakZ2MWp5TjZiUHJtSWNTNks0ZE0wc1dKMUwzT2paWFRGei1KcldDenZZVHJId2RMSTlUczRPZ2Q4TXlJUnM"}, + AwsClient.rootTTL)); + existing.put("2kfjogvxdqtxxugbh7gs7naaai.n", new RecordSet(new String[] { + "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", + "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"}, + 3333)); + existing.put("fdxn3sn67na5dka4j2gok7bvqi.n", + new RecordSet(new String[] {"tree-branch:"}, AwsClient.treeNodeTTL)); + + Map newRecords = new HashMap<>(); + newRecords.put("n", + "tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdCVlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVGc4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM"); + newRecords.put("c7hrfpf3blgf3yr4dy5kx3smbe.n", + "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"); + newRecords.put("jwxydbpxywg6fx3gmdibfa6cj4.n", + "tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24"); + newRecords.put("2xs2367yhaxjfglzhvawlqd4zy.n", + "nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA"); + newRecords.put("h4fht4b454p6uxfd7jcyq5pwdy.n", + "nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI"); + newRecords.put("mhtdo6tmubria2xwg5ludack24.n", + "nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o"); + + AwsClient publish; + try { + publish = new AwsClient("random1", "random2", "random3", + "us-east-1", new P2pConfig().getPublishConfig().getChangeThreshold()); + } catch (DnsException e) { + Assert.fail(); + return; + } + List changes = publish.computeChanges("n", newRecords, existing); + + Change[] wantChanges = new Change[] { + publish.newTXTChange(ChangeAction.CREATE, "2xs2367yhaxjfglzhvawlqd4zy.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA\""), + publish.newTXTChange(ChangeAction.CREATE, "c7hrfpf3blgf3yr4dy5kx3smbe.n", + AwsClient.treeNodeTTL, + "\"tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org\""), + publish.newTXTChange(ChangeAction.CREATE, "h4fht4b454p6uxfd7jcyq5pwdy.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI\""), + publish.newTXTChange(ChangeAction.CREATE, "jwxydbpxywg6fx3gmdibfa6cj4.n", + AwsClient.treeNodeTTL, + "\"tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24\""), + publish.newTXTChange(ChangeAction.CREATE, "mhtdo6tmubria2xwg5ludack24.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o\""), + + publish.newTXTChange(ChangeAction.UPSERT, "n", + AwsClient.rootTTL, + "\"tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdCVlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVGc4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM\""), + + publish.newTXTChange(ChangeAction.DELETE, "2kfjogvxdqtxxugbh7gs7naaai.n", + 3333, + "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", + "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"), + publish.newTXTChange(ChangeAction.DELETE, "fdxn3sn67na5dka4j2gok7bvqi.n", + AwsClient.treeNodeTTL, + "tree-branch:") + }; + + Assert.assertEquals(wantChanges.length, changes.size()); + for (int i = 0; i < changes.size(); i++) { + Assert.assertTrue(wantChanges[i].equalsBySdkFields(changes.get(i))); + Assert.assertTrue(AwsClient.isSameChange(wantChanges[i], changes.get(i))); + } + } + + @Test + public void testPublish() throws UnknownHostException { + + DnsNode[] nodes = TreeTest.sampleNode(); + List nodeList = Arrays.asList(nodes); + List enrList = Tree.merge(nodeList, new PublishConfig().getMaxMergeSize()); + + String[] links = new String[] { + "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example1.org", + "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example2.org"}; + List linkList = Arrays.asList(links); + + Tree tree = new Tree(); + try { + tree.makeTree(1, enrList, linkList, AlgorithmTest.privateKey); + } catch (DnsException e) { + Assert.fail(); + } + +// //warning: replace your key in the following section, or this test will fail +// AwsClient awsClient; +// try { +// awsClient = new AwsClient("replace your access key", +// "replace your access key secret", +// "replace your host zone id", +// Region.US_EAST_1); +// } catch (DnsException e) { +// Assert.fail(); +// return; +// } +// String domain = "replace with your domain"; +// try { +// awsClient.deploy(domain, tree); +// } catch (Exception e) { +// Assert.fail(); +// return; +// } +// +// BigInteger publicKeyInt = Algorithm.generateKeyPair(AlgorithmTest.privateKey).getPublicKey(); +// String puKeyCompress = Algorithm.compressPubKey(publicKeyInt); +// String base32Pubkey = Algorithm.encode32(ByteArray.fromHexString(puKeyCompress)); +// Client client = new Client(); +// +// Tree route53Tree = new Tree(); +// try { +// client.syncTree(Entry.linkPrefix + base32Pubkey + "@" + domain, null, +// route53Tree); +// } catch (Exception e) { +// Assert.fail(); +// return; +// } +// Assert.assertEquals(links.length, route53Tree.getLinksEntry().size()); +// Assert.assertEquals(nodes.length, route53Tree.getDnsNodes().size()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java b/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java new file mode 100644 index 00000000000..140d137787d --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java @@ -0,0 +1,53 @@ +package org.tron.p2p.dns; + + +import com.google.protobuf.InvalidProtocolBufferException; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class DnsNodeTest { + + @Test + public void testCompressDnsNode() throws UnknownHostException, InvalidProtocolBufferException { + DnsNode[] nodes = new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + }; + List nodeList = Arrays.asList(nodes); + String enrContent = DnsNode.compress(nodeList); + + List dnsNodes = DnsNode.decompress(enrContent); + Assert.assertEquals(1, dnsNodes.size()); + Assert.assertTrue(nodes[0].equals(dnsNodes.get(0))); + } + + @Test + public void testSortDnsNode() throws UnknownHostException { + DnsNode[] nodes = new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10001), + new DnsNode(null, "192.168.0.6", null, 10002), + new DnsNode(null, "192.168.0.6", null, 10003), + new DnsNode(null, "192.168.0.6", null, 10004), + new DnsNode(null, "192.168.0.6", null, 10005), + new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), + new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), + new DnsNode(null, null, "fe80::0001", 10000), + new DnsNode(null, null, "fe80::0002", 10000), + new DnsNode(null, null, "fe80::0002", 10001), + }; + List nodeList = Arrays.asList(nodes); + Collections.shuffle(nodeList); //random order + Collections.sort(nodeList); + for (int i = 0; i < nodeList.size(); i++) { + Assert.assertTrue(nodes[i].equals(nodeList.get(i))); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java b/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java new file mode 100644 index 00000000000..69cb448e439 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java @@ -0,0 +1,36 @@ +package org.tron.p2p.dns; + + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.dns.sync.LinkCache; + +public class LinkCacheTest { + + @Test + public void testLinkCache() { + LinkCache lc = new LinkCache(); + + lc.addLink("1", "2"); + Assert.assertTrue(lc.isChanged()); + + lc.setChanged(false); + lc.addLink("1", "2"); + Assert.assertFalse(lc.isChanged()); + + lc.addLink("2", "3"); + lc.addLink("3", "1"); + lc.addLink("2", "4"); + + for (String key : lc.getBackrefs().keySet()) { + System.out.println(key + ":" + StringUtils.join(lc.getBackrefs().get(key), ",")); + } + Assert.assertTrue(lc.isContainInOtherLink("3")); + Assert.assertFalse(lc.isContainInOtherLink("6")); + + lc.resetLinks("1", null); + Assert.assertTrue(lc.isChanged()); + Assert.assertEquals(0, lc.getBackrefs().size()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java b/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java new file mode 100644 index 00000000000..43f5f11a434 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java @@ -0,0 +1,37 @@ +package org.tron.p2p.dns; + + +import java.util.ArrayList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.dns.sync.Client; +import org.tron.p2p.dns.sync.RandomIterator; + +public class RandomTest { + + @Test + public void testRandomIterator() { + Parameter.p2pConfig = new P2pConfig(); + List treeUrls = new ArrayList<>(); + treeUrls.add( + "tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); + //treeUrls.add( + // "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@shasta.nftderby1.net"); + Parameter.p2pConfig.setTreeUrls(treeUrls); + + Client syncClient = new Client(); + + RandomIterator randomIterator = syncClient.newIterator(); + int count = 0; + while (count < 20) { + DnsNode dnsNode = randomIterator.next(); + Assert.assertNotNull(dnsNode); + Assert.assertNull(dnsNode.getId()); + count += 1; + System.out.println("get Node success:" + dnsNode.format()); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java b/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java new file mode 100644 index 00000000000..a11e83c32c3 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java @@ -0,0 +1,34 @@ +package org.tron.p2p.dns; + + +import java.util.ArrayList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.dns.sync.Client; +import org.tron.p2p.dns.sync.ClientTree; +import org.tron.p2p.dns.tree.Tree; + +public class SyncTest { + + @Test + public void testSync() { + Parameter.p2pConfig = new P2pConfig(); + List treeUrls = new ArrayList<>(); + treeUrls.add( + "tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); + Parameter.p2pConfig.setTreeUrls(treeUrls); + + Client syncClient = new Client(); + + ClientTree clientTree = new ClientTree(syncClient); + Tree tree = new Tree(); + try { + syncClient.syncTree(Parameter.p2pConfig.getTreeUrls().get(0), clientTree, tree); + } catch (Exception e) { + Assert.fail(); + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java b/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java new file mode 100644 index 00000000000..115f46c986e --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java @@ -0,0 +1,236 @@ +package org.tron.p2p.dns; + + +import com.google.protobuf.InvalidProtocolBufferException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.dns.tree.Algorithm; +import org.tron.p2p.dns.tree.Entry; +import org.tron.p2p.dns.tree.Tree; +import org.tron.p2p.dns.update.PublishConfig; +import org.tron.p2p.exception.DnsException; + +public class TreeTest { + + public static DnsNode[] sampleNode() throws UnknownHostException { + return new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10001), + new DnsNode(null, "192.168.0.6", null, 10002), + new DnsNode(null, "192.168.0.6", null, 10003), + new DnsNode(null, "192.168.0.6", null, 10004), + new DnsNode(null, "192.168.0.6", null, 10005), + new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), + new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), + new DnsNode(null, null, "fe80::0001", 10000), + new DnsNode(null, null, "fe80::0002", 10000), + new DnsNode(null, null, "fe80::0003", 10001), + new DnsNode(null, null, "fe80::0004", 10001), + }; + } + + @Test + public void testMerge() throws UnknownHostException { + DnsNode[] nodes = sampleNode(); + List nodeList = Arrays.asList(nodes); + + int maxMergeSize = new PublishConfig().getMaxMergeSize(); + List enrs = Tree.merge(nodeList, maxMergeSize); + int total = 0; + for (int i = 0; i < enrs.size(); i++) { + List subList = null; + try { + subList = DnsNode.decompress(enrs.get(i).substring(Entry.nodesPrefix.length())); + } catch (InvalidProtocolBufferException e) { + Assert.fail(); + } + Assert.assertTrue(subList.size() <= maxMergeSize); + total += subList.size(); + } + Assert.assertEquals(nodeList.size(), total); + } + + @Test + public void testTreeBuild() throws UnknownHostException { + int seq = 0; + + DnsNode[] dnsNodes = new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10000), + new DnsNode(null, "192.168.0.7", null, 10000), + new DnsNode(null, "192.168.0.8", null, 10000), + new DnsNode(null, "192.168.0.9", null, 10000), + new DnsNode(null, "192.168.0.10", null, 10000), + + new DnsNode(null, "192.168.0.11", null, 10000), + new DnsNode(null, "192.168.0.12", null, 10000), + new DnsNode(null, "192.168.0.13", null, 10000), + new DnsNode(null, "192.168.0.14", null, 10000), + new DnsNode(null, "192.168.0.15", null, 10000), + new DnsNode(null, "192.168.0.16", null, 10000), + new DnsNode(null, "192.168.0.17", null, 10000), + new DnsNode(null, "192.168.0.18", null, 10000), + new DnsNode(null, "192.168.0.19", null, 10000), + new DnsNode(null, "192.168.0.20", null, 10000), + + new DnsNode(null, "192.168.0.21", null, 10000), + new DnsNode(null, "192.168.0.22", null, 10000), + new DnsNode(null, "192.168.0.23", null, 10000), + new DnsNode(null, "192.168.0.24", null, 10000), + new DnsNode(null, "192.168.0.25", null, 10000), + new DnsNode(null, "192.168.0.26", null, 10000), + new DnsNode(null, "192.168.0.27", null, 10000), + new DnsNode(null, "192.168.0.28", null, 10000), + new DnsNode(null, "192.168.0.29", null, 10000), + new DnsNode(null, "192.168.0.30", null, 10000), + + new DnsNode(null, "192.168.0.31", null, 10000), + new DnsNode(null, "192.168.0.32", null, 10000), + new DnsNode(null, "192.168.0.33", null, 10000), + new DnsNode(null, "192.168.0.34", null, 10000), + new DnsNode(null, "192.168.0.35", null, 10000), + new DnsNode(null, "192.168.0.36", null, 10000), + new DnsNode(null, "192.168.0.37", null, 10000), + new DnsNode(null, "192.168.0.38", null, 10000), + new DnsNode(null, "192.168.0.39", null, 10000), + new DnsNode(null, "192.168.0.40", null, 10000), + }; + + String[] enrs = new String[dnsNodes.length]; + for (int i = 0; i < dnsNodes.length; i++) { + DnsNode dnsNode = dnsNodes[i]; + List nodeList = new ArrayList<>(); + nodeList.add(dnsNode); + enrs[i] = Entry.nodesPrefix + DnsNode.compress(nodeList); + } + + String[] links = new String[] {}; + + String linkBranch0 = "tree-branch:"; + String enrBranch1 = "tree-branch:OX22LN2ZUGOPGIPGBUQH35KZU4,XTGCXXQHPK3VUZPQHC6CGJDR3Q,BQLJLB6P5CRXHI37BRVWBWWACY,X4FURUK4SHXW3GVE6XBO3DFD5Y,SIUYMSVBYYXCE6HVW5TSGOFKVQ,2RKY3FUYIQBV4TFIDU7S42EIEU,KSEEGRTUGR4GCCBQ4TYHAWDKME,YGWDS6F6KLTFCC7T3AMAJHXI2A,K4HMVDEHRKOGOFQZXBJ2PSVIMM,NLLRMPWOTS6SP4D7YLCQA42IQQ,BBDLEDOZYAX5CWM6GNAALRVUXY,7NMT4ZISY5F4U6B6CQML2C526E,NVDRYMFHIERJEVGW5TE7QEAS2A"; + String enrBranch2 = "tree-branch:5ELKMY4HVAV5CBY6KDMXWOFSN4,7PHYT72EXSZJ6MT2IQ7VGUFQHI,AM6BJFCERRNKBG4A5X3MORBDZU,2WOYKPVTNYAY3KVDTDY4CEVOJM,PW5BHSJMPEHVJKRF5QTRXQB4LU,IS4YMOJGD4XPODBAMHZOUTIVMI,NSEE5WE57FWG2EERXI5TBBD32E,GOLZDJTTQ7V2MO2BG45O3Q22XI,4VL7USGBWKW576WM4TX7XIXS4A,GZQSPHDZYS7FXURGOQU3RIDUK4,T7L645CJJKCQVQMUADDO44EGOM,ATPMZZZB4RGYKC6K7QDFC22WIE,57KNNYA4WOKVZAODRCFYK64MBA"; + String enrBranch3 = "tree-branch:BJF5S37KVATG2SYHO6M7APDCNU,OUB3BDKUZQWXXFX5OSF5JCB6BA,6JZEHDWM6WWQYIEYVZN5QVMUXA,LXNNOBVTTZBPD3N5VTOCPVG7JE,LMWLKDCBT2U3CGSHKR2PYJNV5I,K2SSCP4ZIF7TQI4MRVLELFAQQE,MKR7II3GYETKN7MSCUQOF6MBQ4,FBJ5VFCV37SGUOEYA2SPGO3TLA,6SHSDL7PJCJAER3OS53NYPNDFI,KYU2OQJBU6AU3KJFCUSLOJWKVE,3N6XKDWY3WTBOSBS22YPUAHCFQ,IPEWOISXUGOL7ORZIOXBD24SPI,PCGDGGVEQQQFL4U2FYRXVHVMUM"; + String enrBranch4 = "tree-branch:WHCXLEQB3467BFATRY5SMIV62M,LAHEXJDXOPZSS2TDVXTJACCB6Q,QR4HMFZU3STBJEXOZIXPDRQTGM,JZUKVXBOLBPXCELWIE5G6E6UUU"; + + String[] branches = new String[] {linkBranch0, enrBranch1, enrBranch2, enrBranch3, enrBranch4}; + + List branchList = Arrays.asList(branches); + List enrList = Arrays.asList(enrs); + List linkList = Arrays.asList(links); + + Tree tree = new Tree(); + try { + tree.makeTree(seq, enrList, linkList, null); + } catch (DnsException e) { + Assert.fail(); + } + + /* + b r a n c h 4 + / / \ \ + / / \ \ + / / \ \ + branch1 branch2 branch3 \ + / \ / \ / \ \ + node:-01 ~ node:-13 node:-14 ~ node:-26 node:-27 ~ node:-39 node:-40 + */ + + Assert.assertEquals(branchList.size() + enrList.size() + linkList.size(), + tree.getEntries().size()); + Assert.assertEquals(branchList.size(), tree.getBranchesEntry().size()); + Assert.assertEquals(enrList.size(), tree.getNodesEntry().size()); + Assert.assertEquals(linkList.size(), tree.getLinksEntry().size()); + + for (String branch : tree.getBranchesEntry()) { + Assert.assertTrue(branchList.contains(branch)); + } + for (String nodeEntry : tree.getNodesEntry()) { + Assert.assertTrue(enrList.contains(nodeEntry)); + } + for (String link : tree.getLinksEntry()) { + Assert.assertTrue(linkList.contains(link)); + } + + Assert.assertEquals(Algorithm.encode32AndTruncate(enrBranch4), tree.getRootEntry().getERoot()); + Assert.assertEquals(Algorithm.encode32AndTruncate(linkBranch0), tree.getRootEntry().getLRoot()); + Assert.assertEquals(seq, tree.getSeq()); + } + + @Test + public void testGroupAndMerge() throws UnknownHostException { + Random random = new Random(); + //simulate some nodes + int ipCount = 2000; + int maxMergeSize = 5; + List dnsNodes = new ArrayList<>(); + Set ipSet = new HashSet<>(); + int i = 0; + while (i < ipCount) { + i += 1; + String ip = String.format("%d.%d.%d.%d", random.nextInt(256), random.nextInt(256), + random.nextInt(256), random.nextInt(256)); + if (ipSet.contains(ip)) { + continue; + } + ipSet.add(ip); + dnsNodes.add(new DnsNode(null, ip, null, 10000)); + } + Set enrSet1 = new HashSet<>(Tree.merge(dnsNodes, maxMergeSize)); + System.out.println("srcSize:" + enrSet1.size()); + + // delete some node + int deleteCount = 100; + i = 0; + while (i < deleteCount) { + i += 1; + int deleteIndex = random.nextInt(dnsNodes.size()); + dnsNodes.remove(deleteIndex); + } + + // add some node + int addCount = 100; + i = 0; + while (i < addCount) { + i += 1; + String ip = String.format("%d.%d.%d.%d", random.nextInt(256), random.nextInt(256), + random.nextInt(256), random.nextInt(256)); + if (ipSet.contains(ip)) { + continue; + } + ipSet.add(ip); + dnsNodes.add(new DnsNode(null, ip, null, 10000)); + } + Set enrSet2 = new HashSet<>(Tree.merge(dnsNodes, maxMergeSize)); + + // calculate changes + Set enrSet3 = new HashSet<>(enrSet2); + enrSet3.removeAll(enrSet1); // enrSet2 - enrSet1 + System.out.println("addSize:" + enrSet3.size()); + Assert.assertTrue(enrSet3.size() < enrSet1.size()); + + Set enrSet4 = new HashSet<>(enrSet1); + enrSet4.removeAll(enrSet2); //enrSet1 - enrSet2 + System.out.println("deleteSize:" + enrSet4.size()); + Assert.assertTrue(enrSet4.size() < enrSet1.size()); + + Set enrSet5 = new HashSet<>(enrSet1); + enrSet5.retainAll(enrSet2); // enrSet1 && enrSet2 + System.out.println("intersectionSize:" + enrSet5.size()); + Assert.assertTrue(enrSet5.size() < enrSet1.size()); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java b/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java new file mode 100644 index 00000000000..09b563101d1 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java @@ -0,0 +1,110 @@ +package org.tron.p2p.example; + + +import static java.lang.Thread.sleep; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pService; +import org.tron.p2p.discover.Node; +import org.tron.p2p.dns.update.DnsType; +import org.tron.p2p.dns.update.PublishConfig; +import org.tron.p2p.stats.P2pStats; + +public class DnsExample1 { + + private P2pService p2pService = new P2pService(); + + public void startP2pService() { + // config p2p parameters + P2pConfig config = new P2pConfig(); + initDnsPublishConfig(config); + + // start p2p service + p2pService.start(config); + + // after start about 300 seconds, you can find following log: + // Trying to publish tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org + // that is your tree url. you can publish your tree url on any somewhere such as github. + // for others, this url is a known tree url + while (true) { + try { + sleep(1000); + } catch (InterruptedException e) { + break; + } + } + } + + public void closeP2pService() { + p2pService.close(); + } + + public void connect(InetSocketAddress address) { + p2pService.connect(address); + } + + public P2pStats getP2pStats() { + return p2pService.getP2pStats(); + } + + public List getAllNodes() { + return p2pService.getAllNodes(); + } + + public List getTableNodes() { + return p2pService.getTableNodes(); + } + + public List getConnectableNodes() { + return p2pService.getConnectableNodes(); + } + + private void initDnsPublishConfig(P2pConfig config) { + // set p2p version + config.setNetworkId(11111); + + // set tcp and udp listen port + config.setPort(18888); + + // must turn node discovery on + config.setDiscoverEnable(true); + + // set discover seed nodes + List seedNodeList = new ArrayList<>(); + seedNodeList.add(new InetSocketAddress("13.124.62.58", 18888)); + seedNodeList.add(new InetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a", 18888)); + seedNodeList.add(new InetSocketAddress("127.0.0.4", 18888)); + config.setSeedNodes(seedNodeList); + + PublishConfig publishConfig = new PublishConfig(); + // config node private key, and then you should publish your public key + publishConfig.setDnsPrivate("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"); + + // config your domain + publishConfig.setDnsDomain("nodes.example.org"); + + // if you know other tree urls, you can attach it. it is optional + String[] urls = new String[] { + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example1.org", + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example2.org",}; + publishConfig.setKnownTreeUrls(Arrays.asList(urls)); + + //add your api key of aws or aliyun + publishConfig.setDnsType(DnsType.AwsRoute53); + publishConfig.setAccessKeyId("your access key"); + publishConfig.setAccessKeySecret("your access key secret"); + publishConfig.setAwsHostZoneId("your host zone id"); + publishConfig.setAwsRegion("us-east-1"); + + // enable dns publish + publishConfig.setDnsPublishEnable(true); + + // enable publish, so your nodes can be automatically published on domain periodically and others can download them + config.setPublishConfig(publishConfig); + } + +} diff --git a/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java b/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java new file mode 100644 index 00000000000..1eff4fd3991 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java @@ -0,0 +1,170 @@ +package org.tron.p2p.example; + + +import java.net.InetSocketAddress; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.commons.lang3.ArrayUtils; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pEventHandler; +import org.tron.p2p.P2pService; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.discover.Node; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.stats.P2pStats; +import org.tron.p2p.utils.ByteArray; + +public class DnsExample2 { + + private P2pService p2pService = new P2pService(); + private Map channels = new ConcurrentHashMap<>(); + + public void startP2pService() { + // config p2p parameters + P2pConfig config = new P2pConfig(); + + //if you use dns discovery, you can use following config + initDnsSyncConfig(config); + + // register p2p event handler + MyP2pEventHandler myP2pEventHandler = new MyP2pEventHandler(); + try { + p2pService.register(myP2pEventHandler); + } catch (P2pException e) { + // todo process exception + } + + // start p2p service + p2pService.start(config); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + // send message + TestMessage testMessage = new TestMessage(ByteArray.fromString("hello")); + for (Channel channel : channels.values()) { + channel.send(ByteArray.fromObject(testMessage)); + } + + // close channel + for (Channel channel : channels.values()) { + channel.close(); + } + } + + public void closeP2pService() { + p2pService.close(); + } + + public void connect(InetSocketAddress address) { + p2pService.connect(address); + } + + public P2pStats getP2pStats() { + return p2pService.getP2pStats(); + } + + public List getAllNodes() { + return p2pService.getAllNodes(); + } + + public List getTableNodes() { + return p2pService.getTableNodes(); + } + + public List getConnectableNodes() { + return p2pService.getConnectableNodes(); + } + + private void initDnsSyncConfig(P2pConfig config) { + // generally, discovery service is not needed if you only use dns nodes independently to establish tcp connections + config.setDiscoverEnable(false); + + // config your known tree urls + String[] urls = new String[] { + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org"}; + config.setTreeUrls(Arrays.asList(urls)); + } + + private class MyP2pEventHandler extends P2pEventHandler { + + public MyP2pEventHandler() { + this.messageTypes = new HashSet<>(); + this.messageTypes.add(MessageTypes.TEST.getType()); + } + + @Override + public void onConnect(Channel channel) { + channels.put(channel.getInetSocketAddress(), channel); + } + + @Override + public void onDisconnect(Channel channel) { + channels.remove(channel.getInetSocketAddress()); + } + + @Override + public void onMessage(Channel channel, byte[] data) { + byte type = data[0]; + byte[] messageData = ArrayUtils.subarray(data, 1, data.length); + switch (MessageTypes.fromByte(type)) { + case TEST: + TestMessage message = new TestMessage(messageData); + // process TestMessage + break; + default: + // todo + } + } + + } + + private enum MessageTypes { + + FIRST((byte) 0x00), + + TEST((byte) 0x01), + + LAST((byte) 0x8f); + + private final byte type; + + MessageTypes(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + + private static final Map map = new HashMap<>(); + + static { + for (MessageTypes value : values()) { + map.put(value.type, value); + } + } + + public static MessageTypes fromByte(byte type) { + return map.get(type); + } + } + + private static class TestMessage { + + protected MessageTypes type; + protected byte[] data; + + public TestMessage(byte[] data) { + this.type = MessageTypes.TEST; + this.data = data; + } + } +} diff --git a/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java b/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java new file mode 100644 index 00000000000..32d90d4340a --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java @@ -0,0 +1,201 @@ +package org.tron.p2p.example; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.commons.lang3.ArrayUtils; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pEventHandler; +import org.tron.p2p.P2pService; +import org.tron.p2p.connection.Channel; +import org.tron.p2p.discover.Node; +import org.tron.p2p.exception.P2pException; +import org.tron.p2p.stats.P2pStats; +import org.tron.p2p.utils.ByteArray; + +public class ImportUsing { + + private P2pService p2pService = new P2pService(); + private Map channels = new ConcurrentHashMap<>(); + + public void startP2pService() { + // config p2p parameters + P2pConfig config = new P2pConfig(); + initConfig(config); + + // register p2p event handler + MyP2pEventHandler myP2pEventHandler = new MyP2pEventHandler(); + try { + p2pService.register(myP2pEventHandler); + } catch (P2pException e) { + // todo process exception + } + + // start p2p service + p2pService.start(config); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + // send message + TestMessage testMessage = new TestMessage(ByteArray.fromString("hello")); + for (Channel channel : channels.values()) { + channel.send(ByteArray.fromObject(testMessage)); + } + + // close channel + for (Channel channel : channels.values()) { + channel.close(); + } + } + + public void closeP2pService() { + p2pService.close(); + } + + public void connect(InetSocketAddress address) { + p2pService.connect(address); + } + + public P2pStats getP2pStats() { + return p2pService.getP2pStats(); + } + + public List getAllNodes() { + return p2pService.getAllNodes(); + } + + public List getTableNodes() { + return p2pService.getTableNodes(); + } + + public List getConnectableNodes() { + return p2pService.getConnectableNodes(); + } + + private void initConfig(P2pConfig config) { + // set p2p version + config.setNetworkId(11111); + + // set tcp and udp listen port + config.setPort(18888); + + // turn node discovery on or off + config.setDiscoverEnable(true); + + // set discover seed nodes + List seedNodeList = new ArrayList<>(); + seedNodeList.add(new InetSocketAddress("13.124.62.58", 18888)); + seedNodeList.add(new InetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a", 18888)); + seedNodeList.add(new InetSocketAddress("127.0.0.4", 18888)); + config.setSeedNodes(seedNodeList); + + // set active nodes + List activeNodeList = new ArrayList<>(); + activeNodeList.add(new InetSocketAddress("127.0.0.2", 18888)); + activeNodeList.add(new InetSocketAddress("127.0.0.3", 18888)); + config.setActiveNodes(activeNodeList); + + // set trust nodes + List trustNodeList = new ArrayList<>(); + trustNodeList.add((new InetSocketAddress("127.0.0.2", 18888)).getAddress()); + config.setTrustNodes(trustNodeList); + + // set the minimum number of connections + config.setMinConnections(8); + + // set the minimum number of actively established connections + config.setMinActiveConnections(2); + + // set the maximum number of connections + config.setMaxConnections(30); + + // set the maximum number of connections with the same IP + config.setMaxConnectionsWithSameIp(2); + } + + private class MyP2pEventHandler extends P2pEventHandler { + + public MyP2pEventHandler() { + this.messageTypes = new HashSet<>(); + this.messageTypes.add(MessageTypes.TEST.getType()); + } + + @Override + public void onConnect(Channel channel) { + channels.put(channel.getInetSocketAddress(), channel); + } + + @Override + public void onDisconnect(Channel channel) { + channels.remove(channel.getInetSocketAddress()); + } + + @Override + public void onMessage(Channel channel, byte[] data) { + byte type = data[0]; + byte[] messageData = ArrayUtils.subarray(data, 1, data.length); + switch (MessageTypes.fromByte(type)) { + case TEST: + TestMessage message = new TestMessage(messageData); + // process TestMessage + break; + default: + // todo + } + } + + } + + private enum MessageTypes { + + FIRST((byte) 0x00), + + TEST((byte) 0x01), + + LAST((byte) 0x8f); + + private final byte type; + + MessageTypes(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + + private static final Map map = new HashMap<>(); + + static { + for (MessageTypes value : values()) { + map.put(value.type, value); + } + } + + public static MessageTypes fromByte(byte type) { + return map.get(type); + } + } + + private static class TestMessage { + + protected MessageTypes type; + protected byte[] data; + + public TestMessage(byte[] data) { + this.type = MessageTypes.TEST; + this.data = data; + } + + } + +} diff --git a/p2p/src/test/java/org/tron/p2p/example/README.md b/p2p/src/test/java/org/tron/p2p/example/README.md new file mode 100644 index 00000000000..49687352a41 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/example/README.md @@ -0,0 +1,421 @@ +libp2p can run independently or be used as a dependency. + +# 1. Run independently + +command of start a p2p node: + +```bash +$ java -jar libp2p.jar [options] +``` + +available cli options: + +```bash +usage: available p2p discovery cli options: + -a,--active-nodes active node(s), + ip:port[,ip:port[...]] + -d,--discover enable p2p discover, 0/1, default 1 + -h,--help print help message + -M,--max-connection max connection number, int, default + 50 + -m,--min-connection min connection number, int, default 8 + -ma,--min-active-connection min active connection number, int, + default 2 + -p,--port UDP & TCP port, int, default 18888 + -s,--seed-nodes seed node(s), required, + ip:port[,ip:port[...]] + -t,--trust-ips trust ip(s), ip[,ip[...]] + -v,--version p2p version, int, default 1 + +available dns read cli options: + -u,--url-schemes dns url(s) to get nodes, url format + tree://{pubkey}@{domain}, url[,url[...]] + +available dns publish cli options: + --access-key-id access key id of aws or aliyun api, + required, string + --access-key-secret access key secret of aws or aliyun api, + required, string + --aliyun-dns-endpoint if server-type is aliyun, it's endpoint + of aws dns server, required, string + --aws-region if server-type is aws, it's region of + aws api, such as "eu-south-1", required, + string + --change-threshold change threshold of add and delete to + publish, optional, should be > 0 and < + 1.0, default 0.1 + --dns-private dns private key used to publish, + required, hex string of length 64 + --domain dns domain to publish nodes, required, + string + --host-zone-id if server-type is aws, it's host zone id + of aws's domain, optional, string + --known-urls known dns urls to publish, url format + tree://{pubkey}@{domain}, optional, + url[,url[...]] + --max-merge-size max merge size to merge node to a leaf + node in dns tree, optional, should be + [1~5], default 5 + -publish,--publish enable dns publish + --server-type dns server to publish, required, only + aws or aliyun is support + --static-nodes static nodes to publish, if exist then + nodes from kad will be ignored, + optional, ip:port[,ip:port[...]] +``` + +For details please +check [StartApp](https://github.com/tronprotocol/libp2p/blob/main/src/main/java/org/tron/p2p/example/StartApp.java) +. + +## 1.1 Construct a p2p network using libp2p + +For example +Node A, starts with default configuration parameters. Let's say its IP is 127.0.0.1 + +```bash +$ java -jar libp2p.jar +``` + +Node B, start with seed nodes(127.0.0.1:18888). Let's say its IP is 127.0.0.2 + +```bash +$ java -jar libp2p.jar -s 127.0.0.1:18888 +``` + +Node C, start with with seed nodes(127.0.0.1:18888). Let's say its IP is 127.0.0.3 + +```bash +$ java -jar libp2p.jar -s 127.0.0.1:18888 +``` + +After the three nodes are successfully started, the usual situation is that node B can discover node +C (or node C can discover B), and the three of them can establish a TCP connection with each other. + +## 1.2 Publish our nodes on one domain + +Libp2p support publish nodes on dns domain. Before publishing, you must enable p2p +discover. Node lists can be deployed to any DNS provider such as CloudFlare DNS, dnsimple, Amazon +Route 53, Aliyun Cloud using their respective client libraries. But we only support Amazon Route 53 +and Aliyun Cloud. +You can see more detail on https://eips.ethereum.org/EIPS/eip-1459, we implement this eip, but have +some difference in data structure. + +### 1.2.1 Acquire your apikey from Amazon Route 53 or Aliyun Cloud + +* Amazon Route 53 include: AWS Access Key ID、AWS Access Key Secret、Route53 Zone ID、AWS Region, get more info +* Aliyun Cloud include: accessKeyId、accessKeySecret、endpoint, get more info + +### 1.2.2 Publish nodes + +Suppose you have a domain example.org hosted by Amazon Route 53, you can publish your nodes automatically +like this: + +```bash +java -jar libp2p.jar -p 18888 -v 201910292 -d 1 -s 127.0.0.1:18888 \ +-publish \ +--dns-private b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291 \ +--server-type aws \ +--access-key-id \ +--access-key-secret \ +--aws-region us-east-1 \ +--host-zone-id \ +--domain nodes.example.org +``` + +This program will do following periodically: + +* get nodes from p2p discover service and construct a tree using these nodes +* collect txt records from dns domain with API +* compare tree with the txt records +* submit changes to dns domain with API if necessary. + +We can get tree's url from log: + +``` +tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org +``` + +The compressed public Key APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ is responsed to +above dns-private key. + +### 1.2.3 Verify your dns txt records + +You can query dns record by following command and check if a TXT type record exists: + +```bash +dig nodes.example.org TXT +``` + +At last we can release the tree's url on anywhere later, such as github. So others can download this +tree to get nodes dynamically. + +# 2. Use as a dependency + +## 2.1 Core classes + +* [P2pService](https://github.com/tronprotocol/libp2p/blob/main/src/main/java/org/tron/p2p/P2pService.java) + is the entry class of p2p service and provides the startup interface of p2p service and the main + interfaces provided by p2p module. +* [P2pConfig](https://github.com/tronprotocol/libp2p/blob/main/src/main/java/org/tron/p2p/P2pConfig.java) + defines all the configurations of the p2p module, such as the listening port, the maximum number + of connections, etc. +* [P2pEventHandler](https://github.com/tronprotocol/libp2p/blob/main/src/main/java/org/tron/p2p/P2pEventHandler.java) + is the abstract class for p2p event handler. +* [Channel](https://github.com/tronprotocol/libp2p/blob/main/src/main/java/org/tron/p2p/connection/Channel.java) + is an implementation of the TCP connection channel in the p2p module. The new connection channel + is obtained through the `P2pEventHandler.onConnect` method. + +## 2.2 Interface + +* `P2pService.start` + - @param: p2pConfig P2pConfig + - @return: void + - desc: the startup interface of p2p service +* `P2pService.close` + - @param: + - @return: void + - desc: the close interface of p2p service +* `P2pService.register` + - @param: p2PEventHandler P2pEventHandler + - @return: void + - desc: register p2p event handler +* `P2pService.connect` + - @param: address InetSocketAddress + - @return: void + - desc: connect to a node with a socket address +* `P2pService.getAllNodes` + - @param: + - @return: List + - desc: get all the nodes +* `P2pService.getTableNodes` + - @param: + - @return: List + - desc: get all the nodes that in the hash table +* `P2pService.getConnectableNodes` + - @param: + - @return: List + - desc: get all the nodes that can be connected +* `P2pService.getP2pStats()` + - @param: + - @return: void + - desc: get statistics information of p2p service +* `Channel.send` + - @param: data byte[] + - @return: void + - desc: send messages to the peer node through the channel +* `Channel.close` + - @param: + - @return: void + - desc: the close interface of channel + +## 2.3 Steps for usage + +1. Config p2p discover parameters +2. (optional) Config dns parameters +3. Implement P2pEventHandler and register p2p event handler +4. Start p2p service +5. Use Channel's send and close interfaces as needed +6. Use P2pService's interfaces as needed + +### 2.3.1 Config discover parameters + +New p2p config instance + +```bash +P2pConfig config = new P2pConfig(); +``` + +Set p2p networkId (also called p2p version) + +```bash +config.setNetworkId(11111); +``` + +Set TCP and UDP listen port + +```bash +config.setPort(18888); +``` + +Turn node discovery on or off + +```bash +config.setDiscoverEnable(true); +``` + +Set discover seed nodes + +```bash +List seedNodeList = new ArrayList<>(); +seedNodeList.add(new InetSocketAddress("13.124.62.58", 18888)); +seedNodeList.add(new InetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a", 18888)); +seedNodeList.add(new InetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:1234]", 18888)); +seedNodeList.add(new InetSocketAddress("127.0.0.4", 18888)); +config.setSeedNodes(seedNodeList); +``` + +Set active nodes +```bash +List activeNodeList = new ArrayList<>(); +activeNodeList.add(new InetSocketAddress("127.0.0.2", 18888)); +activeNodeList.add(new InetSocketAddress("127.0.0.3", 18888)); +config.setActiveNodes(activeNodeList); +``` + +Set trust ips + +```bash +List trustNodeList = new ArrayList<>(); +trustNodeList.add((new InetSocketAddress("127.0.0.2", 18888)).getAddress()); +config.setTrustNodes(trustNodeList); +``` + +Set the minimum number of connections + +```bash +config.setMinConnections(8); +``` + +Set the minimum number of actively established connections + +```bash +config.setMinActiveConnections(2); +``` + +Set the maximum number of connections + +```bash +config.setMaxConnections(30); +``` + +Set the maximum number of connections with the same IP + +```bash +config.setMaxConnectionsWithSameIp(2); +``` + +### 2.3.2 (optional) Config dns parameters if needed +Suppose these scenes in libp2p: +* you don't want to config one or many fixed seed nodes in mobile app such as wallet, because nodes may be out of service but you cannot update the app timely +* you don't known any seed node but you still want to establish tcp connection + +You can config a dns tree regardless of whether discovery service is enabled or not. Assume you have a tree url of Tron's nile or shasta or mainnet nodes that publish on github like: +```azure +tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org +``` +You can config the parameters like that: +```bash +config.setDiscoverEnable(false); +String[] urls = new String[] {"tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org"}; +config.setTreeUrls(Arrays.asList(urls)); +``` +After that, libp2p will download the nodes from nile.nftderby1.net periodically. + +### 2.3.3 TCP Handler + +Implement definition message + +```bash +public class TestMessage { + protected MessageTypes type; + protected byte[] data; + public TestMessage(byte[] data) { + this.type = MessageTypes.TEST; + this.data = data; + } + +} + +public enum MessageTypes { + + FIRST((byte)0x00), + + TEST((byte)0x01), + + LAST((byte)0x8f); + + private final byte type; + + MessageTypes(byte type) { + this.type = type; + } + + public byte getType() { + return type; + } + + private static final Map map = new HashMap<>(); + + static { + for (MessageTypes value : values()) { + map.put(value.type, value); + } + } + + public static MessageTypes fromByte(byte type) { + return map.get(type); + } + } +``` + +Inheritance implements the P2pEventHandler class. + +* `onConnect` is called back after the TCP connection is established. +* `onDisconnect` is called back after the TCP connection is closed. +* `onMessage` is called back after receiving a message on the channel. Note that `data[0]` is the + message type. + +```bash +public class MyP2pEventHandler extends P2pEventHandler { + + public MyP2pEventHandler() { + this.typeSet = new HashSet<>(); + this.typeSet.add(MessageTypes.TEST.getType()); + } + + @Override + public void onConnect(Channel channel) { + channels.put(channel.getInetSocketAddress(), channel); + } + + @Override + public void onDisconnect(Channel channel) { + channels.remove(channel.getInetSocketAddress()); + } + + @Override + public void onMessage(Channel channel, byte[] data) { + byte type = data[0]; + byte[] messageData = ArrayUtils.subarray(data, 1, data.length); + switch (MessageTypes.fromByte(type)) { + case TEST: + TestMessage message = new TestMessage(messageData); + // process TestMessage + break; + default: + // todo + } + } +} +``` + +### 2.3.4 Start p2p service + +Start p2p service with P2pConfig and P2pEventHandler + +```bash +P2pService p2pService = new P2pService(); +MyP2pEventHandler myP2pEventHandler = new MyP2pEventHandler(); +try { + p2pService.register(myP2pEventHandler); +} catch (P2pException e) { + // todo process exception +} +p2pService.start(config); +``` + +For details please +check [ImportUsing](ImportUsing.java), [DnsExample1](DnsExample1.java), [DnsExample2](DnsExample2.java) + + diff --git a/p2p/src/test/java/org/tron/p2p/example/StartApp.java b/p2p/src/test/java/org/tron/p2p/example/StartApp.java new file mode 100644 index 00000000000..0ca54026268 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/example/StartApp.java @@ -0,0 +1,386 @@ +package org.tron.p2p.example; + + +import static java.lang.Thread.sleep; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.lang3.StringUtils; +import org.tron.p2p.P2pConfig; +import org.tron.p2p.P2pService; +import org.tron.p2p.base.Parameter; +import org.tron.p2p.dns.update.DnsType; +import org.tron.p2p.dns.update.PublishConfig; +import org.tron.p2p.utils.ByteArray; +import org.tron.p2p.utils.NetUtil; + +@Slf4j(topic = "net") +public class StartApp { + + public static void main(String[] args) { + StartApp app = new StartApp(); + Parameter.version = 1; + + P2pService p2pService = new P2pService(); + long t1 = System.currentTimeMillis(); + Parameter.p2pConfig = new P2pConfig(); + log.debug("P2pConfig cost {} ms", System.currentTimeMillis() - t1); + + CommandLine cli = null; + try { + cli = app.parseCli(args); + } catch (ParseException e) { + System.exit(0); + } + + if (cli.hasOption("s")) { + Parameter.p2pConfig.setSeedNodes(app.parseInetSocketAddressList(cli.getOptionValue("s"))); + log.info("Seed nodes {}", Parameter.p2pConfig.getSeedNodes()); + } + + if (cli.hasOption("a")) { + Parameter.p2pConfig.setActiveNodes(app.parseInetSocketAddressList(cli.getOptionValue("a"))); + log.info("Active nodes {}", Parameter.p2pConfig.getActiveNodes()); + } + + if (cli.hasOption("t")) { + InetSocketAddress address = new InetSocketAddress(cli.getOptionValue("t"), 0); + List trustNodes = new ArrayList<>(); + trustNodes.add(address.getAddress()); + Parameter.p2pConfig.setTrustNodes(trustNodes); + log.info("Trust nodes {}", Parameter.p2pConfig.getTrustNodes()); + } + + if (cli.hasOption("M")) { + Parameter.p2pConfig.setMaxConnections(Integer.parseInt(cli.getOptionValue("M"))); + } + + if (cli.hasOption("m")) { + Parameter.p2pConfig.setMinConnections(Integer.parseInt(cli.getOptionValue("m"))); + } + + if (cli.hasOption("ma")) { + Parameter.p2pConfig.setMinActiveConnections(Integer.parseInt(cli.getOptionValue("ma"))); + } + + if (Parameter.p2pConfig.getMinConnections() > Parameter.p2pConfig.getMaxConnections()) { + log.error("Check maxConnections({}) >= minConnections({}) failed", + Parameter.p2pConfig.getMaxConnections(), Parameter.p2pConfig.getMinConnections()); + System.exit(0); + } + + if (cli.hasOption("d")) { + int d = Integer.parseInt(cli.getOptionValue("d")); + if (d != 0 && d != 1) { + log.error("Check discover failed, must be 0/1"); + System.exit(0); + } + Parameter.p2pConfig.setDiscoverEnable(d == 1); + } + + if (cli.hasOption("p")) { + Parameter.p2pConfig.setPort(Integer.parseInt(cli.getOptionValue("p"))); + } + + if (cli.hasOption("v")) { + Parameter.p2pConfig.setNetworkId(Integer.parseInt(cli.getOptionValue("v"))); + } + if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) { + log.info("Local ipv6: {}", Parameter.p2pConfig.getIpv6()); + } + + app.checkDnsOption(cli); + + p2pService.start(Parameter.p2pConfig); + + while (true) { + try { + sleep(1000); + } catch (InterruptedException e) { + break; + } + } + } + + private CommandLine parseCli(String[] args) throws ParseException { + Options kadOptions = getKadOptions(); + Options dnsReadOptions = getDnsReadOption(); + Options dnsPublishOptions = getDnsPublishOption(); + + Options options = new Options(); + for (Option option : kadOptions.getOptions()) { + options.addOption(option); + } + for (Option option : dnsReadOptions.getOptions()) { + options.addOption(option); + } + for (Option option : dnsPublishOptions.getOptions()) { + options.addOption(option); + } + + CommandLine cli; + CommandLineParser cliParser = new DefaultParser(); + + try { + cli = cliParser.parse(options, args); + } catch (ParseException e) { + log.error("Parse cli failed", e); + printHelpMessage(kadOptions, dnsReadOptions, dnsPublishOptions); + throw e; + } + + if (cli.hasOption("help")) { + printHelpMessage(kadOptions, dnsReadOptions, dnsPublishOptions); + System.exit(0); + } + return cli; + } + + private static final String configPublish = "publish"; + private static final String configDnsPrivate = "dns-private"; + private static final String configKnownUrls = "known-urls"; + private static final String configStaticNodes = "static-nodes"; + private static final String configDomain = "domain"; + private static final String configChangeThreshold = "change-threshold"; + private static final String configMaxMergeSize = "max-merge-size"; + private static final String configServerType = "server-type"; + private static final String configAccessId = "access-key-id"; + private static final String configAccessSecret = "access-key-secret"; + private static final String configHostZoneId = "host-zone-id"; + private static final String configAwsRegion = "aws-region"; + private static final String configAliEndPoint = "aliyun-dns-endpoint"; + + private void checkDnsOption(CommandLine cli) { + if (cli.hasOption("u")) { + Parameter.p2pConfig.setTreeUrls(Arrays.asList(cli.getOptionValue("u").split(","))); + } + + PublishConfig publishConfig = new PublishConfig(); + if (cli.hasOption(configPublish)) { + publishConfig.setDnsPublishEnable(true); + } + + if (publishConfig.isDnsPublishEnable()) { + if (cli.hasOption(configDnsPrivate)) { + String privateKey = cli.getOptionValue(configDnsPrivate); + if (privateKey.length() != 64) { + log.error("Check {}, must be hex string of 64", configDnsPrivate); + System.exit(0); + } + try { + ByteArray.fromHexString(privateKey); + } catch (Exception ignore) { + log.error("Check {}, must be hex string of 64", configDnsPrivate); + System.exit(0); + } + publishConfig.setDnsPrivate(privateKey); + } else { + log.error("Check {}, must not be null", configDnsPrivate); + System.exit(0); + } + + if (cli.hasOption(configKnownUrls)) { + publishConfig.setKnownTreeUrls( + Arrays.asList(cli.getOptionValue(configKnownUrls).split(","))); + } + + if (cli.hasOption(configStaticNodes)) { + publishConfig.setStaticNodes( + parseInetSocketAddressList(cli.getOptionValue(configStaticNodes))); + } + + if (cli.hasOption(configDomain)) { + publishConfig.setDnsDomain(cli.getOptionValue(configDomain)); + } else { + log.error("Check {}, must not be null", configDomain); + System.exit(0); + } + + if (cli.hasOption(configChangeThreshold)) { + double changeThreshold = Double.parseDouble(cli.getOptionValue(configChangeThreshold)); + if (changeThreshold >= 1.0) { + log.error("Check {}, range between (0.0 ~ 1.0]", + configChangeThreshold); + } else { + publishConfig.setChangeThreshold(changeThreshold); + } + } + + if (cli.hasOption(configMaxMergeSize)) { + int maxMergeSize = Integer.parseInt(cli.getOptionValue(configMaxMergeSize)); + if (maxMergeSize > 5) { + log.error("Check {}, range between [1 ~ 5]", configMaxMergeSize); + } else { + publishConfig.setMaxMergeSize(maxMergeSize); + } + } + + if (cli.hasOption(configServerType)) { + String serverType = cli.getOptionValue(configServerType); + if (!"aws".equalsIgnoreCase(serverType) && !"aliyun".equalsIgnoreCase(serverType)) { + log.error("Check {}, must be aws or aliyun", configServerType); + System.exit(0); + } + if ("aws".equalsIgnoreCase(serverType)) { + publishConfig.setDnsType(DnsType.AwsRoute53); + } else { + publishConfig.setDnsType(DnsType.AliYun); + } + } else { + log.error("Check {}, must not be null", configServerType); + System.exit(0); + } + + if (!cli.hasOption(configAccessId)) { + log.error("Check {}, must not be null", configAccessId); + System.exit(0); + } else { + publishConfig.setAccessKeyId(cli.getOptionValue(configAccessId)); + } + + if (!cli.hasOption(configAccessSecret)) { + log.error("Check {}, must not be null", configAccessSecret); + System.exit(0); + } else { + publishConfig.setAccessKeySecret(cli.getOptionValue(configAccessSecret)); + } + + if (publishConfig.getDnsType() == DnsType.AwsRoute53) { + // host-zone-id can be null + if (cli.hasOption(configHostZoneId)) { + publishConfig.setAwsHostZoneId(cli.getOptionValue(configHostZoneId)); + } + + if (!cli.hasOption(configAwsRegion)) { + log.error("Check {}, must not be null", configAwsRegion); + System.exit(0); + } else { + String region = cli.getOptionValue(configAwsRegion); + publishConfig.setAwsRegion(region); + } + } else { + if (!cli.hasOption(configAliEndPoint)) { + log.error("Check {}, must not be null", configAliEndPoint); + System.exit(0); + } else { + publishConfig.setAliDnsEndpoint(cli.getOptionValue(configAliEndPoint)); + } + } + } + Parameter.p2pConfig.setPublishConfig(publishConfig); + } + + private Options getKadOptions() { + + Option opt1 = new Option("s", "seed-nodes", true, + "seed node(s), required, ip:port[,ip:port[...]]"); + Option opt2 = new Option("t", "trust-ips", true, "trust ip(s), ip[,ip[...]]"); + Option opt3 = new Option("a", "active-nodes", true, "active node(s), ip:port[,ip:port[...]]"); + Option opt4 = new Option("M", "max-connection", true, "max connection number, int, default 50"); + Option opt5 = new Option("m", "min-connection", true, "min connection number, int, default 8"); + Option opt6 = new Option("d", "discover", true, "enable p2p discover, 0/1, default 1"); + Option opt7 = new Option("p", "port", true, "UDP & TCP port, int, default 18888"); + Option opt8 = new Option("v", "version", true, "p2p version, int, default 1"); + Option opt9 = new Option("ma", "min-active-connection", true, + "min active connection number, int, default 2"); + Option opt10 = new Option("h", "help", false, "print help message"); + + Options group = new Options(); + group.addOption(opt1); + group.addOption(opt2); + group.addOption(opt3); + group.addOption(opt4); + group.addOption(opt5); + group.addOption(opt6); + group.addOption(opt7); + group.addOption(opt8); + group.addOption(opt9); + group.addOption(opt10); + return group; + } + + private Options getDnsReadOption() { + Option opt = new Option("u", "url-schemes", true, + "dns url(s) to get nodes, url format tree://{pubkey}@{domain}, url[,url[...]]"); + Options group = new Options(); + group.addOption(opt); + return group; + } + + private Options getDnsPublishOption() { + Option opt1 = new Option(configPublish, configPublish, false, "enable dns publish"); + Option opt2 = new Option(null, configDnsPrivate, true, + "dns private key used to publish, required, hex string of length 64"); + Option opt3 = new Option(null, configKnownUrls, true, + "known dns urls to publish, url format tree://{pubkey}@{domain}, optional, url[,url[...]]"); + Option opt4 = new Option(null, configStaticNodes, true, + "static nodes to publish, if exist then nodes from kad will be ignored, optional, ip:port[,ip:port[...]]"); + Option opt5 = new Option(null, configDomain, true, + "dns domain to publish nodes, required, string"); + Option opt6 = new Option(null, configChangeThreshold, true, + "change threshold of add and delete to publish, optional, should be > 0 and < 1.0, default 0.1"); + Option opt7 = new Option(null, configMaxMergeSize, true, + "max merge size to merge node to a leaf node in dns tree, optional, should be [1~5], default 5"); + Option opt8 = new Option(null, configServerType, true, + "dns server to publish, required, only aws or aliyun is support"); + Option opt9 = new Option(null, configAccessId, true, + "access key id of aws or aliyun api, required, string"); + Option opt10 = new Option(null, configAccessSecret, true, + "access key secret of aws or aliyun api, required, string"); + Option opt11 = new Option(null, configAwsRegion, true, + "if server-type is aws, it's region of aws api, such as \"eu-south-1\", required, string"); + Option opt12 = new Option(null, configHostZoneId, true, + "if server-type is aws, it's host zone id of aws's domain, optional, string"); + Option opt13 = new Option(null, configAliEndPoint, true, + "if server-type is aliyun, it's endpoint of aws dns server, required, string"); + + Options group = new Options(); + group.addOption(opt1); + group.addOption(opt2); + group.addOption(opt3); + group.addOption(opt4); + group.addOption(opt5); + group.addOption(opt6); + group.addOption(opt7); + group.addOption(opt8); + group.addOption(opt9); + group.addOption(opt10); + group.addOption(opt11); + group.addOption(opt12); + group.addOption(opt13); + return group; + } + + private void printHelpMessage(Options kadOptions, Options dnsReadOptions, + Options dnsPublishOptions) { + HelpFormatter helpFormatter = new HelpFormatter(); + helpFormatter.printHelp("available p2p discovery cli options:", kadOptions); + helpFormatter.setSyntaxPrefix("\n"); + helpFormatter.printHelp("available dns read cli options:", dnsReadOptions); + helpFormatter.setSyntaxPrefix("\n"); + helpFormatter.printHelp("available dns publish cli options:", dnsPublishOptions); + helpFormatter.setSyntaxPrefix("\n"); + } + + private List parseInetSocketAddressList(String paras) { + List nodes = new ArrayList<>(); + for (String para : paras.split(",")) { + InetSocketAddress inetSocketAddress = NetUtil.parseInetSocketAddress(para); + if (inetSocketAddress != null) { + nodes.add(inetSocketAddress); + } + } + return nodes; + } +} diff --git a/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java b/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java new file mode 100644 index 00000000000..626d0480b56 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java @@ -0,0 +1,26 @@ +package org.tron.p2p.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.dns.update.AwsClient; + + +public class ByteArrayTest { + + @Test + public void testHexToString() { + byte[] data = new byte[] {-128, -127, -1, 0, 1, 127}; + Assert.assertEquals("8081ff00017f", ByteArray.toHexString(data)); + } + + @Test + public void testSubdomain(){ + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com","abc.com")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.","abc.com")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com","abc.com.")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.","abc.com.")); + + Assert.assertFalse(AwsClient.isSubdomain("a-sub.abc.com","sub.abc.com")); + Assert.assertTrue(AwsClient.isSubdomain(".sub.abc.com","sub.abc.com")); + } +} diff --git a/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java new file mode 100644 index 00000000000..f78f8faf41e --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java @@ -0,0 +1,205 @@ +package org.tron.p2p.utils; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.InetSocketAddress; +import java.net.Socket; +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.base.Constant; +import org.tron.p2p.discover.Node; +import org.tron.p2p.protos.Discover; + +public class NetUtilTest { + + @Test + public void testValidIp() { + boolean flag = NetUtil.validIpV4(null); + Assert.assertFalse(flag); + flag = NetUtil.validIpV4("a.1.1.1"); + Assert.assertFalse(flag); + flag = NetUtil.validIpV4("1.1.1"); + Assert.assertFalse(flag); + flag = NetUtil.validIpV4("0.0.0.0"); + Assert.assertFalse(flag); + flag = NetUtil.validIpV4("256.1.2.3"); + Assert.assertFalse(flag); + flag = NetUtil.validIpV4("1.1.1.1"); + Assert.assertTrue(flag); + } + + @Test + public void testValidNode() { + boolean flag = NetUtil.validNode(null); + Assert.assertFalse(flag); + + InetSocketAddress address = new InetSocketAddress("1.1.1.1", 1000); + Node node = new Node(address); + flag = NetUtil.validNode(node); + Assert.assertTrue(flag); + + node.setId(new byte[10]); + flag = NetUtil.validNode(node); + Assert.assertFalse(flag); + + node = new Node(NetUtil.getNodeId(), "1.1.1", null, 1000); + flag = NetUtil.validNode(node); + Assert.assertFalse(flag); + } + + @Test + public void testGetNode() { + Discover.Endpoint endpoint = Discover.Endpoint.newBuilder() + .setPort(100).build(); + Node node = NetUtil.getNode(endpoint); + Assert.assertEquals(100, node.getPort()); + } + + @Test + public void testExternalIp() { + String ip = NetUtil.getExternalIpV4(); + Assert.assertFalse(ip.startsWith("10.")); + Assert.assertFalse(ip.startsWith("192.168.")); + Assert.assertFalse(ip.startsWith("172.16.")); + Assert.assertFalse(ip.startsWith("172.17.")); + Assert.assertFalse(ip.startsWith("172.18.")); + Assert.assertFalse(ip.startsWith("172.19.")); + Assert.assertFalse(ip.startsWith("172.20.")); + Assert.assertFalse(ip.startsWith("172.21.")); + Assert.assertFalse(ip.startsWith("172.22.")); + Assert.assertFalse(ip.startsWith("172.23.")); + Assert.assertFalse(ip.startsWith("172.24.")); + Assert.assertFalse(ip.startsWith("172.25.")); + Assert.assertFalse(ip.startsWith("172.26.")); + Assert.assertFalse(ip.startsWith("172.27.")); + Assert.assertFalse(ip.startsWith("172.28.")); + Assert.assertFalse(ip.startsWith("172.29.")); + Assert.assertFalse(ip.startsWith("172.30.")); + Assert.assertFalse(ip.startsWith("172.31.")); + } + + @Test + public void testGetIP() { + //notice: please check that you only have one externalIP + String ip1 = null, ip2 = null, ip3 = null; + try { + Method method = NetUtil.class.getDeclaredMethod("getExternalIp", String.class, boolean.class); + method.setAccessible(true); + ip1 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(0), true); + ip2 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(1), true); + ip3 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(2), true); + } catch (Exception e) { + Assert.fail(); + } + String ip4 = NetUtil.getExternalIpV4(); + Assert.assertEquals(ip1, ip4); + Assert.assertEquals(ip2, ip4); + Assert.assertEquals(ip3, ip4); + } + + private String getLanIP2() { + String lanIP; + try (Socket s = new Socket("www.baidu.com", 80)) { + lanIP = s.getLocalAddress().getHostAddress(); + } catch (IOException e) { + lanIP = "127.0.0.1"; + } + return lanIP; + } + + @Test + public void testGetLanIP() { + String lanIpv4 = NetUtil.getLanIP(); + Assert.assertNotNull(lanIpv4); + String lanIpv4Old = getLanIP2(); + Assert.assertEquals(lanIpv4, lanIpv4Old); + } + + @Test + public void testIPv6Format() { + String std = "fe80:0:0:0:204:61ff:fe9d:f156"; + int randomPort = 10001; + String ip1 = new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:fe9d:f156", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip1, std); + + String ip2 = new InetSocketAddress("fe80::204:61ff:fe9d:f156", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip2, std); + + String ip3 = new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:254.157.241.86", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip3, std); + + String ip4 = new InetSocketAddress("fe80:0:0:0:0204:61ff:254.157.241.86", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip4, std); + + String ip5 = new InetSocketAddress("fe80::204:61ff:254.157.241.86", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip5, std); + + String ip6 = new InetSocketAddress("FE80::204:61ff:254.157.241.86", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip6, std); + + String ip7 = new InetSocketAddress("[fe80:0:0:0:204:61ff:fe9d:f156]", + randomPort).getAddress().getHostAddress(); + Assert.assertEquals(ip7, std); + } + + @Test + public void testParseIpv6() { + InetSocketAddress address1 = NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + Assert.assertNotNull(address1); + Assert.assertEquals(18888, address1.getPort()); + Assert.assertEquals("2600:1f13:908:1b00:e1fd:5a84:251c:a32a", + address1.getAddress().getHostAddress()); + + try { + NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:abcd"); + Assert.fail(); + } catch (RuntimeException e) { + Assert.assertTrue(true); + } + + try { + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + Assert.fail(); + } catch (RuntimeException e) { + Assert.assertTrue(true); + } + + try { + NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + Assert.fail(); + } catch (RuntimeException e) { + Assert.assertTrue(true); + } + + try { + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + Assert.fail(); + } catch (RuntimeException e) { + Assert.assertTrue(true); + } + + try { + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a"); + Assert.fail(); + } catch (RuntimeException e) { + Assert.assertTrue(true); + } + + InetSocketAddress address5 = NetUtil.parseInetSocketAddress( + "192.168.0.1:18888"); + Assert.assertNotNull(address5); + } + +} diff --git a/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java b/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java new file mode 100644 index 00000000000..0892b6ee244 --- /dev/null +++ b/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java @@ -0,0 +1,30 @@ +package org.tron.p2p.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.tron.p2p.connection.message.keepalive.PingMessage; +import org.tron.p2p.protos.Connect; + +public class ProtoUtilTest { + + @Test + public void testCompressMessage() throws Exception { + PingMessage p1 = new PingMessage(); + + Connect.CompressMessage message = ProtoUtil.compressMessage(p1.getData()); + + byte[] d1 = ProtoUtil.uncompressMessage(message); + + PingMessage p2 = new PingMessage(d1); + + Assert.assertTrue(p1.getTimeStamp() == p2.getTimeStamp()); + + + Connect.CompressMessage m2 = ProtoUtil.compressMessage(new byte[1000]); + + byte[] d2 = ProtoUtil.uncompressMessage(m2); + + Assert.assertTrue(d2.length == 1000); + Assert.assertTrue(d2[0] == 0); + } +} diff --git a/settings.gradle b/settings.gradle index af32bfca702..c3c04636395 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,4 +9,5 @@ include 'example:actuator-example' include 'crypto' include 'plugins' include 'platform' +include 'p2p' From 115e9bdfbd943dd25a8c00324b5d8ca3a1c31056 Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 02:38:31 +0800 Subject: [PATCH 2/6] style(p2p): replace log field with logger for java-tron convention 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. --- p2p/build.gradle | 3 ++ p2p/lombok.config | 2 - .../main/java/org/tron/p2p/P2pService.java | 4 +- .../java/org/tron/p2p/connection/Channel.java | 18 ++++----- .../tron/p2p/connection/ChannelManager.java | 24 +++++------ .../business/detect/NodeDetectService.java | 4 +- .../business/handshake/HandshakeService.java | 6 +-- .../business/keepalive/KeepAliveService.java | 2 +- .../business/pool/ConnPoolService.java | 20 +++++----- .../p2p/connection/socket/MessageHandler.java | 4 +- .../socket/P2pChannelInitializer.java | 4 +- .../P2pProtobufVarint32FrameDecoder.java | 2 +- .../p2p/connection/socket/PeerClient.java | 4 +- .../p2p/connection/socket/PeerServer.java | 10 ++--- .../main/java/org/tron/p2p/discover/Node.java | 4 +- .../discover/protocol/kad/DiscoverTask.java | 8 ++-- .../p2p/discover/protocol/kad/KadService.java | 2 +- .../discover/protocol/kad/NodeHandler.java | 4 +- .../p2p/discover/socket/DiscoverServer.java | 16 ++++---- .../p2p/discover/socket/MessageHandler.java | 6 +-- .../p2p/discover/socket/P2pPacketDecoder.java | 10 ++--- .../java/org/tron/p2p/dns/DnsManager.java | 4 +- .../org/tron/p2p/dns/lookup/LookUpTxt.java | 8 ++-- .../java/org/tron/p2p/dns/sync/Client.java | 6 +-- .../org/tron/p2p/dns/sync/ClientTree.java | 12 +++--- .../org/tron/p2p/dns/sync/RandomIterator.java | 18 ++++----- .../org/tron/p2p/dns/tree/BranchEntry.java | 2 +- .../java/org/tron/p2p/dns/tree/RootEntry.java | 4 +- .../main/java/org/tron/p2p/dns/tree/Tree.java | 2 +- .../org/tron/p2p/dns/update/AliClient.java | 20 +++++----- .../org/tron/p2p/dns/update/AwsClient.java | 34 ++++++++-------- .../tron/p2p/dns/update/PublishService.java | 18 ++++----- .../java/org/tron/p2p/utils/ByteArray.java | 2 +- .../main/java/org/tron/p2p/utils/NetUtil.java | 14 +++---- .../java/org/tron/p2p/example/StartApp.java | 40 +++++++++---------- 35 files changed, 171 insertions(+), 170 deletions(-) delete mode 100644 p2p/lombok.config diff --git a/p2p/build.gradle b/p2p/build.gradle index a6ac4fa1f67..f0e6a4ff2ba 100644 --- a/p2p/build.gradle +++ b/p2p/build.gradle @@ -1,5 +1,8 @@ apply plugin: 'com.google.protobuf' +// TODO: enable checkstyle after code style alignment +// apply plugin: 'checkstyle' + def protobufVersion = '3.25.8' def grpcVersion = '1.75.0' diff --git a/p2p/lombok.config b/p2p/lombok.config deleted file mode 100644 index 012382f4b02..00000000000 --- a/p2p/lombok.config +++ /dev/null @@ -1,2 +0,0 @@ -config.stopBubbling = true -lombok.log.fieldName=log diff --git a/p2p/src/main/java/org/tron/p2p/P2pService.java b/p2p/src/main/java/org/tron/p2p/P2pService.java index 8173f40f4c6..5e0b05e56c8 100644 --- a/p2p/src/main/java/org/tron/p2p/P2pService.java +++ b/p2p/src/main/java/org/tron/p2p/P2pService.java @@ -29,7 +29,7 @@ public void start(P2pConfig p2pConfig) { NodeManager.init(); ChannelManager.init(); DnsManager.init(); - log.info("P2p service started"); + logger.info("P2p service started"); Runtime.getRuntime().addShutdownHook(new Thread(this::close)); } @@ -42,7 +42,7 @@ public void close() { DnsManager.close(); NodeManager.close(); ChannelManager.close(); - log.info("P2p service closed"); + logger.info("P2p service closed"); } public void register(P2pEventHandler p2PEventHandler) throws P2pException { diff --git a/p2p/src/main/java/org/tron/p2p/connection/Channel.java b/p2p/src/main/java/org/tron/p2p/connection/Channel.java index d57259809e8..811904b7d27 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/Channel.java +++ b/p2p/src/main/java/org/tron/p2p/connection/Channel.java @@ -94,18 +94,18 @@ public void processException(Throwable throwable) { baseThrowable = Throwables.getRootCause(baseThrowable); } catch (IllegalArgumentException e) { baseThrowable = e.getCause(); - log.warn("Loop in causal chain detected"); + logger.warn("Loop in causal chain detected"); } SocketAddress address = ctx.channel().remoteAddress(); if (throwable instanceof ReadTimeoutException || throwable instanceof IOException || throwable instanceof CorruptedFrameException) { - log.warn("Close peer {}, reason: {}", address, throwable.getMessage()); + logger.warn("Close peer {}, reason: {}", address, throwable.getMessage()); } else if (baseThrowable instanceof P2pException) { - log.warn("Close peer {}, type: ({}), info: {}", + logger.warn("Close peer {}, type: ({}), info: {}", address, ((P2pException) baseThrowable).getType(), baseThrowable.getMessage()); } else { - log.error("Close peer {}, exception caught", address, throwable); + logger.error("Close peer {}, exception caught", address, throwable); } close(); } @@ -137,9 +137,9 @@ public void close() { public void send(Message message) { if (message.needToLog()) { - log.info("Send message to channel {}, {}", inetSocketAddress, message); + logger.info("Send message to channel {}, {}", inetSocketAddress, message); } else { - log.debug("Send message to channel {}, {}", inetSocketAddress, message); + logger.debug("Send message to channel {}, {}", inetSocketAddress, message); } send(message.getSendData()); } @@ -148,7 +148,7 @@ public void send(byte[] data) { try { byte type = data[0]; if (isDisconnect) { - log.warn("Send to {} failed as channel has closed, message-type:{} ", + logger.warn("Send to {} failed as channel has closed, message-type:{} ", ctx.channel().remoteAddress(), type); return; } @@ -160,14 +160,14 @@ public void send(byte[] data) { ByteBuf byteBuf = Unpooled.wrappedBuffer(data); ctx.writeAndFlush(byteBuf).addListener((ChannelFutureListener) future -> { if (!future.isSuccess() && !isDisconnect) { - log.warn("Send to {} failed, message-type:{}, cause:{}", + logger.warn("Send to {} failed, message-type:{}, cause:{}", ctx.channel().remoteAddress(), ByteArray.byte2int(type), future.cause().getMessage()); } }); setLastSendTime(System.currentTimeMillis()); } catch (Exception e) { - log.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage()); + logger.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage()); ctx.channel().close(); } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java index f781d8ddff6..78db925c3dd 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java +++ b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java @@ -87,7 +87,7 @@ public static ChannelFuture connect(Node node, ChannelFutureListener future) { public static void notifyDisconnect(Channel channel) { if (channel.getInetSocketAddress() == null) { - log.warn("Notify Disconnect peer has no address."); + logger.warn("Notify Disconnect peer has no address."); return; } channels.remove(channel.getInetSocketAddress()); @@ -114,18 +114,18 @@ public static synchronized DisconnectCode processPeer(Channel channel) { InetAddress inetAddress = channel.getInetAddress(); if (bannedNodes.getIfPresent(inetAddress) != null && bannedNodes.getIfPresent(inetAddress) > System.currentTimeMillis()) { - log.info("Peer {} recently disconnected", channel); + logger.info("Peer {} recently disconnected", channel); return DisconnectCode.TIME_BANNED; } if (channels.size() >= Parameter.p2pConfig.getMaxConnections()) { - log.info("Too many peers, disconnected with {}", channel); + logger.info("Too many peers, disconnected with {}", channel); return DisconnectCode.TOO_MANY_PEERS; } int num = getConnectionNum(channel.getInetAddress()); if (num >= Parameter.p2pConfig.getMaxConnectionsWithSameIp()) { - log.info("Max connection with same ip {}", channel); + logger.info("Max connection with same ip {}", channel); return DisconnectCode.MAX_CONNECTION_WITH_SAME_IP; } } @@ -136,7 +136,7 @@ public static synchronized DisconnectCode processPeer(Channel channel) { if (c.getStartTime() > channel.getStartTime()) { c.close(); } else { - log.info("Duplicate peer {}, exist peer {}", channel, c); + logger.info("Duplicate peer {}, exist peer {}", channel, c); return DisconnectCode.DUPLICATE_PEER; } } @@ -145,7 +145,7 @@ public static synchronized DisconnectCode processPeer(Channel channel) { channels.put(channel.getInetSocketAddress(), channel); - log.info("Add peer {}, total channels: {}", channel.getInetSocketAddress(), channels.size()); + logger.info("Add peer {}, total channels: {}", channel.getInetSocketAddress(), channels.size()); return DisconnectCode.NORMAL; } @@ -175,7 +175,7 @@ public static DisconnectReason getDisconnectReason(DisconnectCode code) { } public static void logDisconnectReason(Channel channel, DisconnectReason reason) { - log.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name()); + logger.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name()); } public static void banNode(InetAddress inetAddress, Long banTime) { @@ -211,9 +211,9 @@ public static void processMessage(Channel channel, byte[] data) throws P2pExcept Message message = Message.parse(data); if (message.needToLog()) { - log.info("Receive message from channel: {}, {}", channel.getInetSocketAddress(), message); + logger.info("Receive message from channel: {}, {}", channel.getInetSocketAddress(), message); } else { - log.debug("Receive message from channel {}, {}", channel.getInetSocketAddress(), message); + logger.debug("Receive message from channel {}, {}", channel.getInetSocketAddress(), message); } switch (message.getType()) { @@ -264,7 +264,7 @@ private static void handMessage(Channel channel, byte[] data) throws P2pExceptio public static synchronized void updateNodeId(Channel channel, String nodeId) { channel.setNodeId(nodeId); if (nodeId.equals(Hex.toHexString(Parameter.p2pConfig.getNodeID()))) { - log.warn("Channel {} is myself", channel.getInetSocketAddress()); + logger.warn("Channel {} is myself", channel.getInetSocketAddress()); channel.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); channel.close(); return; @@ -282,11 +282,11 @@ public static synchronized void updateNodeId(Channel channel, String nodeId) { Channel c1 = list.get(0); Channel c2 = list.get(1); if (c1.getStartTime() > c2.getStartTime()) { - log.info("Close channel {}, other channel {} is earlier", c1, c2); + logger.info("Close channel {}, other channel {} is earlier", c1, c2); c1.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); c1.close(); } else { - log.info("Close channel {}, other channel {} is earlier", c2, c1); + logger.info("Close channel {}, other channel {} is earlier", c2, c1); c2.send(new P2pDisconnectMessage(DisconnectReason.DUPLICATE_PEER)); c2.close(); } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java index 3ef53b59a02..df2cec77f1a 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java @@ -64,7 +64,7 @@ public void init(PeerClient peerClient) { try { work(); } catch (Exception t) { - log.warn("Exception in node detect worker, {}", t.getMessage()); + logger.warn("Exception in node detect worker, {}", t.getMessage()); } }, 1, 5, TimeUnit.SECONDS); } @@ -137,7 +137,7 @@ private void detect(NodeStat stat) { setLastDetectTime(stat); peerClient.connectAsync(stat.getNode(), true); } catch (Exception e) { - log.warn("Detect node {} failed, {}", + logger.warn("Detect node {} failed, {}", stat.getNode().getPreferInetSocketAddress(), e.getMessage()); nodeStatMap.remove(stat.getSocketAddress()); } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java index 760849a3a83..38ba25c22ed 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java @@ -27,7 +27,7 @@ public void processMessage(Channel channel, Message message) { HelloMessage msg = (HelloMessage) message; if (channel.isFinishHandshake()) { - log.warn("Close channel {}, handshake is finished", channel.getInetSocketAddress()); + logger.warn("Close channel {}, handshake is finished", channel.getInetSocketAddress()); channel.send(new P2pDisconnectMessage(DisconnectReason.DUP_HANDSHAKE)); channel.close(); return; @@ -55,7 +55,7 @@ public void processMessage(Channel channel, Message message) { || (msg.getNetworkId() != networkId && msg.getVersion() != networkId)) { DisconnectCode disconnectCode = DisconnectCode.forNumber(msg.getCode()); //v0.1 have version, v0.2 both have version and networkId - log.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}", + logger.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}", channel.getInetSocketAddress(), msg.getCode(), disconnectCode.name(), @@ -68,7 +68,7 @@ public void processMessage(Channel channel, Message message) { } else { if (msg.getNetworkId() != networkId) { - log.info("Peer {} different network id, peer->{}, me->{}", + logger.info("Peer {} different network id, peer->{}, me->{}", channel.getInetSocketAddress(), msg.getNetworkId(), networkId); sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION, msg.getTimestamp()); logDisconnectReason(channel, DisconnectReason.DIFFERENT_VERSION); diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java index 19eea3b015a..2b7a3e5ef91 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java @@ -44,7 +44,7 @@ public void init() { } }); } catch (Exception t) { - log.error("Exception in keep alive task", t); + logger.error("Exception in keep alive task", t); } }, 2, 2, TimeUnit.SECONDS); } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java index 81523d6ed16..bc5a3423645 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java @@ -77,7 +77,7 @@ public void init(PeerClient peerClient) { try { connect(false); } catch (Exception t) { - log.error("Exception in poolLoopExecutor worker", t); + logger.error("Exception in poolLoopExecutor worker", t); } }, 200, 3600, TimeUnit.MILLISECONDS); @@ -86,7 +86,7 @@ public void init(PeerClient peerClient) { try { check(); } catch (Exception t) { - log.error("Exception in disconnectExecutor worker", t); + logger.error("Exception in disconnectExecutor worker", t); } }, 30, 30, TimeUnit.SECONDS); } @@ -189,12 +189,12 @@ private void connect(boolean isFilterActiveNodes) { connectNodes.addAll(newNodes); } - log.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}", + logger.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}", size, connectNodes.size(), isFilterActiveNodes); //establish tcp connection with chose nodes by peerClient { connectNodes.forEach(n -> { - log.info("Connect to peer {}", n.getPreferInetSocketAddress()); + logger.info("Connect to peer {}", n.getPreferInetSocketAddress()); peerClient.connectAsync(n, false); peerClientCache.put(n.getPreferInetSocketAddress().getAddress(), System.currentTimeMillis()); @@ -254,14 +254,14 @@ private void check() { if (!peers.isEmpty()) { List list = new ArrayList<>(peers); Channel peer = list.get(new Random().nextInt(peers.size())); - log.info("Disconnect with peer randomly: {}", peer); + logger.info("Disconnect with peer randomly: {}", peer); peer.send(new P2pDisconnectMessage(DisconnectReason.RANDOM_ELIMINATION)); peer.close(); } } private synchronized void logActivePeers() { - log.info("Peer stats: channels {}, activePeers {}, active {}, passive {}", + logger.info("Peer stats: channels {}, activePeers {}, active {}, passive {}", ChannelManager.getChannels().size(), activePeers.size(), activePeersCount.get(), passivePeersCount.get()); } @@ -272,7 +272,7 @@ public void triggerConnect(InetSocketAddress address) { } connectingPeersCount.decrementAndGet(); if (poolLoopExecutor.getQueue().size() >= Parameter.CONN_MAX_QUEUE_SIZE) { - log.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE); + logger.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE); return; } try { @@ -281,12 +281,12 @@ public void triggerConnect(InetSocketAddress address) { try { connect(true); } catch (Exception t) { - log.error("Exception in poolLoopExecutor worker", t); + logger.error("Exception in poolLoopExecutor worker", t); } }); } } catch (Exception e) { - log.warn("Submit task failed, message:{}", e.getMessage()); + logger.warn("Submit task failed, message:{}", e.getMessage()); } } @@ -333,7 +333,7 @@ public void close() { poolLoopExecutor.shutdownNow(); disconnectExecutor.shutdownNow(); } catch (Exception e) { - log.warn("Problems shutting down executor", e); + logger.warn("Problems shutting down executor", e); } } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java index 251b9a5590b..797197f0fad 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java @@ -29,7 +29,7 @@ public void handlerAdded(ChannelHandlerContext ctx) { @Override public void channelActive(ChannelHandlerContext ctx) { - log.debug("Channel active, {}", ctx.channel().remoteAddress()); + logger.debug("Channel active, {}", ctx.channel().remoteAddress()); channel.setChannelHandlerContext(ctx); if (channel.isActive()) { if (channel.isDiscoveryMode()) { @@ -76,7 +76,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List ou } channel.processException(e); } catch (Throwable t) { - log.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(), + logger.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(), ByteArray.toHexString(data)); throw t; } diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java index 1bac43f11b9..247ae4f0a62 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java @@ -42,7 +42,7 @@ public void initChannel(NioSocketChannel ch) { ChannelManager.getNodeDetectService().notifyDisconnect(channel); } else { try { - log.info("Close channel:{}", channel.getInetSocketAddress()); + logger.info("Close channel:{}", channel.getInetSocketAddress()); ChannelManager.notifyDisconnect(channel); } finally { if (channel.getInetSocketAddress() != null && channel.isActive() && trigger) { @@ -53,7 +53,7 @@ public void initChannel(NioSocketChannel ch) { }); } catch (Exception e) { - log.error("Unexpected initChannel error", e); + logger.error("Unexpected initChannel error", e); } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java index 6e04b1d2be7..4493551da96 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java @@ -75,7 +75,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int preIndex = in.readerIndex(); int length = readRawVarint32(in); if (length >= Parameter.MAX_MESSAGE_LENGTH) { - log.warn("Receive a big msg or not encoded msg, host : {}, msg length is : {}", + logger.warn("Receive a big msg or not encoded msg, host : {}, msg length is : {}", ctx.channel().remoteAddress(), length); in.clear(); channel.send(new P2pDisconnectMessage(DisconnectReason.BAD_MESSAGE)); diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java index 2f0bd943a41..6a49e89d200 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java @@ -38,7 +38,7 @@ public void connect(String host, int port, String remoteId) { f.sync().channel().closeFuture().sync(); } } catch (Exception e) { - log.warn("PeerClient can't connect to {}:{} ({})", host, port, e.getMessage()); + logger.warn("PeerClient can't connect to {}:{} ({})", host, port, e.getMessage()); } } @@ -69,7 +69,7 @@ public ChannelFuture connectAsync(Node node, boolean discoveryMode) { if (channelFuture != null) { channelFuture.addListener((ChannelFutureListener) future -> { if (!future.isSuccess()) { - log.warn("Connect to peer {} fail, cause:{}", node.getPreferInetSocketAddress(), + logger.warn("Connect to peer {} fail, cause:{}", node.getPreferInetSocketAddress(), future.cause().getMessage()); future.channel().close(); if (!discoveryMode) { diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java index 8a1b7d9adf2..1fdb231f96f 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java @@ -29,10 +29,10 @@ public void init() { public void close() { if (listening && channelFuture != null && channelFuture.channel().isOpen()) { try { - log.info("Closing TCP server..."); + logger.info("Closing TCP server..."); channelFuture.channel().close().sync(); } catch (Exception e) { - log.warn("Closing TCP server failed.", e); + logger.warn("Closing TCP server failed.", e); } } } @@ -57,7 +57,7 @@ public void start(int port) { b.childHandler(p2pChannelInitializer); // Start the client. - log.info("TCP listener started, bind port {}", port); + logger.info("TCP listener started, bind port {}", port); channelFuture = b.bind(port).sync(); @@ -66,10 +66,10 @@ public void start(int port) { // Wait until the connection is closed. channelFuture.channel().closeFuture().sync(); - log.info("TCP listener closed"); + logger.info("TCP listener closed"); } catch (Exception e) { - log.error("Start TCP server failed", e); + logger.error("Start TCP server failed", e); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); diff --git a/p2p/src/main/java/org/tron/p2p/discover/Node.java b/p2p/src/main/java/org/tron/p2p/discover/Node.java index 51156d2f462..af7f1eee772 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/Node.java +++ b/p2p/src/main/java/org/tron/p2p/discover/Node.java @@ -74,14 +74,14 @@ public Node(byte[] id, String hostV4, String hostV6, int port, int bindPort) { public void updateHostV4(String hostV4) { if (StringUtils.isEmpty(this.hostV4) && StringUtils.isNotEmpty(hostV4)) { - log.info("update hostV4:{} with hostV6:{}", hostV4, this.hostV6); + logger.info("update hostV4:{} with hostV6:{}", hostV4, this.hostV6); this.hostV4 = hostV4; } } public void updateHostV6(String hostV6) { if (StringUtils.isEmpty(this.hostV6) && StringUtils.isNotEmpty(hostV6)) { - log.info("update hostV6:{} with hostV4:{}", hostV6, this.hostV4); + logger.info("update hostV6:{} with hostV4:{}", hostV6, this.hostV4); this.hostV6 = hostV6; } } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java index 4ab23ec307c..823e4d4dd36 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java @@ -38,10 +38,10 @@ public void init() { } discover(nodeId, 0, new ArrayList<>()); } catch (Exception e) { - log.error("DiscoverTask fails to be executed", e); + logger.error("DiscoverTask fails to be executed", e); } }, 1, KademliaOptions.DISCOVER_CYCLE, TimeUnit.MILLISECONDS); - log.debug("DiscoverTask started"); + logger.debug("DiscoverTask started"); } private void discover(byte[] nodeId, int round, List prevTriedNodes) { @@ -54,7 +54,7 @@ private void discover(byte[] nodeId, int round, List prevTriedNodes) { kadService.getNodeHandler(n).sendFindNode(nodeId); tried.add(n); } catch (Exception e) { - log.error("Unexpected Exception occurred while sending FindNodeMessage", e); + logger.error("Unexpected Exception occurred while sending FindNodeMessage", e); } } @@ -66,7 +66,7 @@ private void discover(byte[] nodeId, int round, List prevTriedNodes) { try { Thread.sleep(KademliaOptions.WAIT_TIME); } catch (InterruptedException e) { - log.warn("Discover task interrupted"); + logger.warn("Discover task interrupted"); Thread.currentThread().interrupt(); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java index 1a137c4cbf8..cfdf30d1974 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java @@ -78,7 +78,7 @@ public void close() { discoverTask.close(); } } catch (Exception e) { - log.error("Close nodeManagerTasksTimer or pongTimer failed", e); + logger.error("Close nodeManagerTasksTimer or pongTimer failed", e); throw e; } } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java index de42c5f40d8..a6f569eae49 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java @@ -127,7 +127,7 @@ public void handlePong(PongMessage msg) { public void handleNeighbours(NeighborsMessage msg, InetSocketAddress sender) { if (!waitForNeighbors) { - log.warn("Receive neighbors from {} without send find nodes", sender); + logger.warn("Receive neighbors from {} without send find nodes", sender); return; } waitForNeighbors = false; @@ -171,7 +171,7 @@ public void sendPing() { handleTimedOut(); } } catch (Exception e) { - log.error("Unhandled exception in pong timer schedule", e); + logger.error("Unhandled exception in pong timer schedule", e); } }, KadService.getPingTimeout(), TimeUnit.MILLISECONDS); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java index 7b8ca97f2c9..4840d42a7e2 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java @@ -30,19 +30,19 @@ public void init(EventHandler eventHandler) { try { start(); } catch (Exception e) { - log.error("Discovery server start failed", e); + logger.error("Discovery server start failed", e); } }, "DiscoverServer").start(); } public void close() { - log.info("Closing discovery server..."); + logger.info("Closing discovery server..."); shutdown = true; if (channel != null) { try { channel.close().await(SERVER_CLOSE_WAIT, TimeUnit.SECONDS); } catch (Exception e) { - log.error("Closing discovery server failed", e); + logger.error("Closing discovery server failed", e); } } } @@ -71,21 +71,21 @@ public void initChannel(NioDatagramChannel ch) channel = b.bind(port).sync().channel(); - log.info("Discovery server started, bind port {}", port); + logger.info("Discovery server started, bind port {}", port); channel.closeFuture().sync(); if (shutdown) { - log.info("Shutdown discovery server"); + logger.info("Shutdown discovery server"); break; } - log.warn("Restart discovery server after 5 sec pause..."); + logger.warn("Restart discovery server after 5 sec pause..."); Thread.sleep(SERVER_RESTART_WAIT); } } catch (InterruptedException e) { - log.warn("Discover server interrupted"); + logger.warn("Discover server interrupted"); Thread.currentThread().interrupt(); } catch (Exception e) { - log.error("Start discovery server with port {} failed", port, e); + logger.error("Start discovery server with port {} failed", port, e); } finally { group.shutdownGracefully().sync(); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java index 502f1dbe30c..0f6fd0d3899 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java @@ -30,7 +30,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelRead0(ChannelHandlerContext ctx, UdpEvent udpEvent) { - log.debug("Rcv udp msg type {}, len {} from {} ", + logger.debug("Rcv udp msg type {}, len {} from {} ", udpEvent.getMessage().getType(), udpEvent.getMessage().getSendData().length, udpEvent.getAddress()); @@ -39,7 +39,7 @@ public void channelRead0(ChannelHandlerContext ctx, UdpEvent udpEvent) { @Override public void accept(UdpEvent udpEvent) { - log.debug("Send udp msg type {}, len {} to {} ", + logger.debug("Send udp msg type {}, len {} to {} ", udpEvent.getMessage().getType(), udpEvent.getMessage().getSendData().length, udpEvent.getAddress()); @@ -60,7 +60,7 @@ public void channelReadComplete(ChannelHandlerContext ctx) { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - log.warn("Exception caught in udp message handler, {} {}", + logger.warn("Exception caught in udp message handler, {} {}", ctx.channel().remoteAddress(), cause.getMessage()); ctx.close(); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java index 5cfcf110e68..8099fe83fe0 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java @@ -22,7 +22,7 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List= MAXSIZE) { - log.warn("UDP rcv bad packet, from {} length = {}", ctx.channel().remoteAddress(), length); + logger.warn("UDP rcv bad packet, from {} length = {}", ctx.channel().remoteAddress(), length); return; } byte[] encoded = new byte[length]; @@ -32,18 +32,18 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List getDnsNodes() { List dnsNodes = tree.getDnsNodes(); List ipv6Nodes = new ArrayList<>(); for (DnsNode dnsNode : dnsNodes) { - //log.debug("DnsNode:{}", dnsNode); + //logger.debug("DnsNode:{}", dnsNode); if (dnsNode.getInetSocketAddressV4() != null) { v4Size += 1; } @@ -66,7 +66,7 @@ public static List getDnsNodes() { .filter(node -> !localIpSet.contains( node.getPreferInetSocketAddress().getAddress().getHostAddress())) .collect(Collectors.toList()); - log.debug("Tree {} node size:{}, v4 node size:{}, v6 node size:{}, connectable size:{}", + logger.debug("Tree {} node size:{}, v4 node size:{}, v6 node size:{}, connectable size:{}", entry.getKey(), dnsNodes.size(), v4Size, v6Size, connectAbleNodes.size()); nodes.addAll(connectAbleNodes); } diff --git a/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java index 2c58b4f852b..bbb9dbecba4 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java +++ b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java @@ -59,7 +59,7 @@ public static TXTRecord lookUpTxt(String hash, String domain) // as dns server has dns cache, we may get the name's latest TXTRecord ttl later after it changes public static TXTRecord lookUpTxt(String name) throws TextParseException, UnknownHostException { TXTRecord txt = null; - log.info("LookUp name: {}", name); + logger.info("LookUp name: {}", name); Lookup lookup = new Lookup(name, Type.TXT); int times = 0; Record[] records = null; @@ -79,15 +79,15 @@ public static TXTRecord lookUpTxt(String name) throws TextParseException, Unknow long end = System.currentTimeMillis(); times += 1; if (records != null) { - log.debug("Succeed to use dns: {}, cur cost: {}ms, total cost: {}ms", publicDns, + logger.debug("Succeed to use dns: {}, cur cost: {}ms, total cost: {}ms", publicDns, end - thisTime, end - start); break; } else { - log.debug("Failed to use dns: {}, cur cost: {}ms", publicDns, end - thisTime); + logger.debug("Failed to use dns: {}, cur cost: {}ms", publicDns, end - thisTime); } } if (records == null) { - log.error("Failed to lookUp name:{}", name); + logger.error("Failed to lookUp name:{}", name); return null; } for (Record item : records) { diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java index 95781ec2c1a..2d258ad784c 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java @@ -66,7 +66,7 @@ public void startSync() { try { syncTree(urlScheme, clientTree, tree); } catch (Exception e) { - log.error("SyncTree failed, url:" + urlScheme, e); + logger.error("SyncTree failed, url:" + urlScheme, e); continue; } } @@ -97,7 +97,7 @@ public void syncTree(String urlScheme, ClientTree clientTree, Tree tree) throws } tree.setRootEntry(clientTree.getRoot()); - log.info("SyncTree {} complete, LinkEntry size:{}, NodesEntry size:{}, node size:{}", + logger.info("SyncTree {} complete, LinkEntry size:{}, NodesEntry size:{}, node size:{}", urlScheme, tree.getLinksEntry().size(), tree.getNodesEntry().size(), tree.getDnsNodes().size()); } @@ -174,7 +174,7 @@ public RandomIterator newIterator() { try { randomIterator.addTree(urlScheme); } catch (DnsException e) { - log.error("AddTree failed " + urlScheme, e); + logger.error("AddTree failed " + urlScheme, e); } } return randomIterator; diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java index 5c546ffd76f..5cb1e7a669b 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java @@ -132,21 +132,21 @@ private DnsNode syncNextRandomNode() int size = nodeList.size(); return nodeList.get(random.nextInt(size)); } - log.info("Get branch or link entry in syncNextRandomNode"); + logger.info("Get branch or link entry in syncNextRandomNode"); return null; } // updateRoot ensures that the given tree has an up-to-date root. private boolean[] updateRoot() throws TextParseException, DnsException, SignatureException, UnknownHostException { - log.info("UpdateRoot {}", linkEntry.getDomain()); + logger.info("UpdateRoot {}", linkEntry.getDomain()); lastValidateTime = System.currentTimeMillis(); RootEntry rootEntry = client.resolveRoot(linkEntry); if (rootEntry == null) { return new boolean[] {false, false}; } if (rootEntry.getSeq() <= lastSeq) { - log.info("The seq of url doesn't change, url:[{}], seq:{}", linkEntry.getRepresent(), + logger.info("The seq of url doesn't change, url:[{}], seq:{}", linkEntry.getRepresent(), lastSeq); return new boolean[] {false, false}; } @@ -162,7 +162,7 @@ private boolean[] updateRoot() updateLRoot = true; } else { // if lroot is not changed, wo do not to sync the link tree - log.info("The lroot of url doesn't change, url:[{}], lroot:[{}]", linkEntry.getRepresent(), + logger.info("The lroot of url doesn't change, url:[{}], lroot:[{}]", linkEntry.getRepresent(), linkSync.root); } @@ -171,7 +171,7 @@ private boolean[] updateRoot() updateERoot = true; } else { // if eroot is not changed, wo do not to sync the enr tree - log.info("The eroot of url doesn't change, url:[{}], eroot:[{}]", linkEntry.getRepresent(), + logger.info("The eroot of url doesn't change, url:[{}], eroot:[{}]", linkEntry.getRepresent(), enrSync.root); } return new boolean[] {updateLRoot, updateERoot}; @@ -180,7 +180,7 @@ private boolean[] updateRoot() private boolean rootUpdateDue() { boolean scheduledCheck = System.currentTimeMillis() > nextScheduledRootCheck(); if (scheduledCheck) { - log.info("Update root because of scheduledCheck, {}", linkEntry.getDomain()); + logger.info("Update root because of scheduledCheck, {}", linkEntry.getDomain()); } return root == null || scheduledCheck; } diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java index 35f4823415d..3cb9f0180ad 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java @@ -42,16 +42,16 @@ public DnsNode next() { i += 1; ClientTree clientTree = pickTree(); if (clientTree == null) { - log.error("clientTree is null"); + logger.error("clientTree is null"); return null; } - log.info("Choose clientTree:{} from {} ClientTree", clientTree.getLinkEntry().getRepresent(), + logger.info("Choose clientTree:{} from {} ClientTree", clientTree.getLinkEntry().getRepresent(), clientTrees.size()); DnsNode dnsNode; try { dnsNode = clientTree.syncRandom(); } catch (Exception e) { - log.warn("Error in DNS random node sync, tree:{}, cause:[{}]", + logger.warn("Error in DNS random node sync, tree:{}, cause:[{}]", clientTree.getLinkEntry().getDomain(), e.getMessage()); continue; } @@ -76,7 +76,7 @@ public void addTree(String url) throws DnsException { //the first random private ClientTree pickTree() { if (clientTrees == null) { - log.info("clientTrees is null"); + logger.info("clientTrees is null"); return null; } if (linkCache.isChanged()) { @@ -94,13 +94,13 @@ private ClientTree pickTree() { // if urlScheme is not contain in any other link, wo delete it from clientTrees // then create one ClientTree using this urlScheme, add it to clientTrees private void rebuildTrees() { - log.info("rebuildTrees..."); + logger.info("rebuildTrees..."); Iterator> it = clientTrees.entrySet().iterator(); while (it.hasNext()) { Entry entry = it.next(); String urlScheme = entry.getKey(); if (!linkCache.isContainInOtherLink(urlScheme)) { - log.info("remove tree from trees:{}", urlScheme); + logger.info("remove tree from trees:{}", urlScheme); it.remove(); } } @@ -111,13 +111,13 @@ private void rebuildTrees() { try { LinkEntry linkEntry = LinkEntry.parseEntry(urlScheme); clientTrees.put(urlScheme, new ClientTree(client, linkCache, linkEntry)); - log.info("add tree to clientTrees:{}", urlScheme); + logger.info("add tree to clientTrees:{}", urlScheme); } catch (DnsException e) { - log.error("Parse LinkEntry failed", e); + logger.error("Parse LinkEntry failed", e); } } } - log.info("Exist clientTrees: {}", StringUtils.join(clientTrees.keySet(), ",")); + logger.info("Exist clientTrees: {}", StringUtils.join(clientTrees.keySet(), ",")); } public void close() { diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java index 9359349f144..bc426328753 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java @@ -19,7 +19,7 @@ public BranchEntry(String[] children) { public static BranchEntry parseEntry(String e) { String content = e.substring(branchPrefix.length()); if (StringUtils.isEmpty(content)) { - log.info("children size is 0, e:[{}]", e); + logger.info("children size is 0, e:[{}]", e); return new BranchEntry(new String[0]); } else { return new BranchEntry(content.split(splitSymbol)); diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java index 65425c820e8..078a0e8838b 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java @@ -85,7 +85,7 @@ public static RootEntry parseEntry(String e) throws DnsException { public static RootEntry parseEntry(String e, String publicKey, String domain) throws SignatureException, DnsException { - log.info("Domain:{}, public key:{}", domain, publicKey); + logger.info("Domain:{}, public key:{}", domain, publicKey); RootEntry rootEntry = parseEntry(e); boolean verify = Algorithm.verifySignature(publicKey, rootEntry.toString(), rootEntry.getSignature()); @@ -99,7 +99,7 @@ public static RootEntry parseEntry(String e, String publicKey, String domain) throw new DnsException(TypeEnum.INVALID_CHILD, "eroot:" + rootEntry.getERoot() + " lroot:" + rootEntry.getLRoot()); } - log.info("Get dnsRoot:[{}]", rootEntry.dnsRoot.toString()); + logger.info("Get dnsRoot:[{}]", rootEntry.dnsRoot.toString()); return rootEntry; } diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java index c05204690e3..c53e24e625b 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java @@ -237,7 +237,7 @@ public List getDnsNodes() { try { subNodes = DnsNode.decompress(joinStr); } catch (InvalidProtocolBufferException | UnknownHostException e) { - log.error("", e); + logger.error("", e); continue; } nodes.addAll(subNodes); diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java index 795250763ed..312ae57beb7 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java @@ -54,10 +54,10 @@ public void deploy(String domainName, Tree t) throws DnsException { try { Map existing = collectRecords( domainName); - log.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), + logger.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domainName); String represent = LinkEntry.buildRepresent(t.getBase32PublicKey(), domainName); - log.info("Trying to publish {}", represent); + logger.info("Trying to publish {}", represent); t.setSeq(this.lastSeq + 1); t.sign(); //seq changed, wo need to sign again Map records = t.toTXT(null); @@ -74,13 +74,13 @@ public void deploy(String domainName, Tree t) throws DnsException { if (serverNodes.isEmpty() || (addNodeSize + deleteNodeSize) / (double) serverNodes.size() >= changeThreshold) { String comment = String.format("Tree update of %s at seq %d", domainName, t.getSeq()); - log.info(comment); + logger.info(comment); submitChanges(domainName, records, existing); } else { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(4); double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); - log.info( + logger.info( "Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", nf.format(changePercent), changeThreshold); } @@ -132,7 +132,7 @@ public Map collect collectServerNodes.addAll(dnsNodes); } catch (DnsException e) { //ignore - log.error("Parse nodeEntry failed: {}", e.getMessage()); + logger.error("Parse nodeEntry failed: {}", e.getMessage()); } } } @@ -145,7 +145,7 @@ public Map collect } } } catch (Exception e) { - log.warn("Failed to collect domain records, error msg: {}", e.getMessage()); + logger.warn("Failed to collect domain records, error msg: {}", e.getMessage()); throw e; } @@ -192,7 +192,7 @@ private void submitChanges(String domainName, deleteCount++; } } - log.info("Published successfully, add count:{}, update count:{}, delete count:{}", + logger.info("Published successfully, add count:{}, update count:{}, delete count:{}", addCount, updateCount, deleteCount); } @@ -276,7 +276,7 @@ public String getRecId(String domainName, String RR) { } } } catch (Exception e) { - log.warn("Failed to get record id, error msg: {}", e.getMessage()); + logger.warn("Failed to get record id, error msg: {}", e.getMessage()); } return recId; } @@ -306,7 +306,7 @@ public String update(String DomainName, String RR, String value, long ttl) { recId = response.getBody().getRecordId(); } } catch (Exception e) { - log.warn("Failed to update or add domain record, error mag: {}", e.getMessage()); + logger.warn("Failed to update or add domain record, error mag: {}", e.getMessage()); } return recId; @@ -324,7 +324,7 @@ public boolean deleteByRR(String domainName, String RR) { } } } catch (Exception e) { - log.warn("Failed to delete domain record, domain name: {}, RR: {}, error msg: {}", + logger.warn("Failed to delete domain record, domain name: {}, RR: {}, error msg: {}", domainName, RR, e.getMessage()); return false; } diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java index 133b294a795..12b00b6041b 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java @@ -90,7 +90,7 @@ private void checkZone(String domain) { } private String findZoneID(String domain) { - log.info("Finding Route53 Zone ID for {}", domain); + logger.info("Finding Route53 Zone ID for {}", domain); ListHostedZonesByNameRequest.Builder request = ListHostedZonesByNameRequest.builder(); while (true) { ListHostedZonesByNameResponse response = route53Client.listHostedZonesByName(request.build()); @@ -128,9 +128,9 @@ public void deploy(String domain, Tree tree) throws Exception { checkZone(domain); Map existing = collectRecords(domain); - log.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domain); + logger.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domain); String represent = LinkEntry.buildRepresent(tree.getBase32PublicKey(), domain); - log.info("Trying to publish {}", represent); + logger.info("Trying to publish {}", represent); tree.setSeq(this.lastSeq + 1); tree.sign(); //seq changed, wo need to sign again @@ -150,13 +150,13 @@ public void deploy(String domain, Tree tree) throws Exception { if (serverNodes.isEmpty() || (addNodeSize + deleteNodeSize) / (double) serverNodes.size() >= changeThreshold) { String comment = String.format("Tree update of %s at seq %d", domain, tree.getSeq()); - log.info(comment); + logger.info(comment); submitChanges(changes, comment); } else { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(4); double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); - log.info("Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", + logger.info("Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", nf.format(changePercent), changeThreshold); } serverNodes.clear(); @@ -168,7 +168,7 @@ public boolean deleteDomain(String rootDomain) throws Exception { checkZone(rootDomain); Map existing = collectRecords(rootDomain); - log.info("Find {} TXT records for {}", existing.size(), rootDomain); + logger.info("Find {} TXT records for {}", existing.size(), rootDomain); List changes = makeDeletionChanges(new HashMap<>(), existing); @@ -188,7 +188,7 @@ public Map collectRecords(String rootDomain) throws Exception String rootContent = null; Set collectServerNodes = new HashSet<>(); while (true) { - log.info("Loading existing TXT records from name:{} zoneId:{} page:{}", rootDomain, zoneId, + logger.info("Loading existing TXT records from name:{} zoneId:{} page:{}", rootDomain, zoneId, page); ListResourceRecordSetsResponse response = route53Client.listResourceRecordSets( request.build()); @@ -221,10 +221,10 @@ public Map collectRecords(String rootDomain) throws Exception collectServerNodes.addAll(dnsNodes); } catch (DnsException e) { //ignore - log.error("Parse nodeEntry failed: {}", e.getMessage()); + logger.error("Parse nodeEntry failed: {}", e.getMessage()); } } - log.info("Find name: {}", name); + logger.info("Find name: {}", name); } if (Boolean.FALSE.equals(response.isTruncated())) { @@ -253,7 +253,7 @@ public Map collectRecords(String rootDomain) throws Exception // submits the given DNS changes to Route53. public void submitChanges(List changes, String comment) { if (changes.isEmpty()) { - log.info("No DNS changes needed"); + logger.info("No DNS changes needed"); return; } @@ -262,7 +262,7 @@ public void submitChanges(List changes, String comment) { ChangeResourceRecordSetsResponse[] responses = new ChangeResourceRecordSetsResponse[batchChanges.size()]; for (int i = 0; i < batchChanges.size(); i++) { - log.info("Submit {}/{} changes to Route53", i + 1, batchChanges.size()); + logger.info("Submit {}/{} changes to Route53", i + 1, batchChanges.size()); ChangeBatch.Builder builder = ChangeBatch.builder(); builder.changes(batchChanges.get(i)); @@ -277,7 +277,7 @@ public void submitChanges(List changes, String comment) { // Wait for all change batches to propagate. for (ChangeResourceRecordSetsResponse response : responses) { - log.info("Waiting for change request {}", response.changeInfo().id()); + logger.info("Waiting for change request {}", response.changeInfo().id()); GetChangeRequest.Builder request = GetChangeRequest.builder(); request.id(response.changeInfo().id()); @@ -295,7 +295,7 @@ public void submitChanges(List changes, String comment) { } } } - log.info("Submit {} changes complete", changes.size()); + logger.info("Submit {} changes complete", changes.size()); } // computeChanges creates DNS changes for the given set of DNS discovery records. @@ -315,7 +315,7 @@ public List computeChanges(String domain, Map records, long ttl = path.equalsIgnoreCase(domain) ? rootTTL : treeNodeTTL; if (!existing.containsKey(path)) { - log.info("Create {} = {}", path, value); + logger.info("Create {} = {}", path, value); Change change = newTXTChange(ChangeAction.CREATE, path, ttl, newValue); changes.add(change); } else { @@ -323,12 +323,12 @@ public List computeChanges(String domain, Map records, String preValue = StringUtils.join(recordSet.values, ""); if (!preValue.equalsIgnoreCase(newValue) || recordSet.ttl != ttl) { - log.info("Updating {} from [{}] to [{}]", path, preValue, newValue); + logger.info("Updating {} from [{}] to [{}]", path, preValue, newValue); if (path.equalsIgnoreCase(domain)) { try { RootEntry oldRoot = RootEntry.parseEntry(StringUtils.strip(preValue, symbol)); RootEntry newRoot = RootEntry.parseEntry(StringUtils.strip(newValue, symbol)); - log.info("Updating root from [{}] to [{}]", oldRoot.getDnsRoot(), + logger.info("Updating root from [{}] to [{}]", oldRoot.getDnsRoot(), newRoot.getDnsRoot()); } catch (DnsException e) { //ignore @@ -355,7 +355,7 @@ public List makeDeletionChanges(Map keeps, String path = entry.getKey(); RecordSet recordSet = entry.getValue(); if (!keeps.containsKey(path)) { - log.info("Delete {} = {}", path, StringUtils.join(existing.get(path).values, "")); + logger.info("Delete {} = {}", path, StringUtils.join(existing.get(path).values, "")); Change change = newTXTChange(ChangeAction.DELETE, path, recordSet.ttl, recordSet.values); changes.add(change); } diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java index 5fd7cb4f497..75bb646d050 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java @@ -36,7 +36,7 @@ public void init() { publish = getPublish(publishConfig); publish.testConnect(); } catch (Exception e) { - log.error("Init PublishService failed", e); + logger.error("Init PublishService failed", e); return; } @@ -71,10 +71,10 @@ private void startPublish() { Tree tree = new Tree(); List nodes = getNodes(config); tree.makeTree(1, nodes, config.getKnownTreeUrls(), config.getDnsPrivate()); - log.info("Try to publish node count:{}", tree.getDnsNodes().size()); + logger.info("Try to publish node count:{}", tree.getDnsNodes().size()); publish.deploy(config.getDnsDomain(), tree); } catch (Exception e) { - log.error("Failed to publish dns", e); + logger.error("Failed to publish dns", e); } } @@ -105,19 +105,19 @@ private List getNodes(PublishConfig config) throws UnknownHostException private boolean checkConfig(boolean supportV4, PublishConfig config) { if (!config.isDnsPublishEnable()) { - log.info("Dns publish service is disable"); + logger.info("Dns publish service is disable"); return false; } if (!supportV4) { - log.error("Must have IP v4 connection to publish dns service"); + logger.error("Must have IP v4 connection to publish dns service"); return false; } if (config.getDnsType() == null) { - log.error("The dns server type must be specified when enabling the dns publishing service"); + logger.error("The dns server type must be specified when enabling the dns publishing service"); return false; } if (StringUtils.isEmpty(config.getDnsDomain())) { - log.error("The dns domain must be specified when enabling the dns publishing service"); + logger.error("The dns domain must be specified when enabling the dns publishing service"); return false; } if (config.getDnsType() == DnsType.AliYun && @@ -125,14 +125,14 @@ private boolean checkConfig(boolean supportV4, PublishConfig config) { StringUtils.isEmpty(config.getAccessKeySecret()) || StringUtils.isEmpty(config.getAliDnsEndpoint()) )) { - log.error("The configuration items related to the Aliyun dns server cannot be empty"); + logger.error("The configuration items related to the Aliyun dns server cannot be empty"); return false; } if (config.getDnsType() == DnsType.AwsRoute53 && (StringUtils.isEmpty(config.getAccessKeyId()) || StringUtils.isEmpty(config.getAccessKeySecret()) || config.getAwsRegion() == null)) { - log.error("The configuration items related to the AwsRoute53 dns server cannot be empty"); + logger.error("The configuration items related to the AwsRoute53 dns server cannot be empty"); return false; } return true; diff --git a/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java index b43f0dfc86a..5d0102ee417 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java +++ b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java @@ -105,7 +105,7 @@ public static byte[] fromObject(Object obj) { objectOutputStream.flush(); bytes = byteArrayOutputStream.toByteArray(); } catch (IOException e) { - log.error("Method objectToByteArray failed.", e); + logger.error("Method objectToByteArray failed.", e); } return bytes; } diff --git a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java index d1682df80fb..83d026ac12e 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java +++ b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java @@ -112,7 +112,7 @@ private static String getExternalIp(String url, boolean isAskIpv4) { } return ip; } catch (Exception e) { - log.warn("Fail to get {} by {}, cause:{}", + logger.warn("Fail to get {} by {}, cause:{}", Constant.ipV4Urls.contains(url) ? "ipv4" : "ipv6", url, e.getMessage()); return null; } finally { @@ -131,7 +131,7 @@ private static String getOuterIPv6Address() { try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { - log.warn("GetOuterIPv6Address failed", e); + logger.warn("GetOuterIPv6Address failed", e); return null; } while (networkInterfaces.hasMoreElements()) { @@ -157,7 +157,7 @@ public static Set getAllLocalAddress() { try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { - log.warn("GetAllLocalAddress failed", e); + logger.warn("GetAllLocalAddress failed", e); return localIpSet; } while (networkInterfaces.hasMoreElements()) { @@ -183,7 +183,7 @@ private static boolean isReservedAddress(InetAddress inetAddress) { public static String getExternalIpV4() { long t1 = System.currentTimeMillis(); String ipV4 = getIp(Constant.ipV4Urls, true); - log.debug("GetExternalIpV4 cost {} ms", System.currentTimeMillis() - t1); + logger.debug("GetExternalIpV4 cost {} ms", System.currentTimeMillis() - t1); return ipV4; } @@ -193,7 +193,7 @@ public static String getExternalIpV6() { if (null == ipV6) { ipV6 = getOuterIPv6Address(); } - log.debug("GetExternalIpV6 cost {} ms", System.currentTimeMillis() - t1); + logger.debug("GetExternalIpV6 cost {} ms", System.currentTimeMillis() - t1); return ipV6; } @@ -251,7 +251,7 @@ public static String getLanIP() { try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { - log.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST, e); + logger.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST, e); return IPADDRESS_LOCALHOST; } while (networkInterfaces.hasMoreElements()) { @@ -274,7 +274,7 @@ public static String getLanIP() { } } } - log.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST); + logger.warn("Can't get lan IP. Fall back to {}", IPADDRESS_LOCALHOST); return IPADDRESS_LOCALHOST; } } diff --git a/p2p/src/test/java/org/tron/p2p/example/StartApp.java b/p2p/src/test/java/org/tron/p2p/example/StartApp.java index 0ca54026268..8a8208d15eb 100644 --- a/p2p/src/test/java/org/tron/p2p/example/StartApp.java +++ b/p2p/src/test/java/org/tron/p2p/example/StartApp.java @@ -35,7 +35,7 @@ public static void main(String[] args) { P2pService p2pService = new P2pService(); long t1 = System.currentTimeMillis(); Parameter.p2pConfig = new P2pConfig(); - log.debug("P2pConfig cost {} ms", System.currentTimeMillis() - t1); + logger.debug("P2pConfig cost {} ms", System.currentTimeMillis() - t1); CommandLine cli = null; try { @@ -46,12 +46,12 @@ public static void main(String[] args) { if (cli.hasOption("s")) { Parameter.p2pConfig.setSeedNodes(app.parseInetSocketAddressList(cli.getOptionValue("s"))); - log.info("Seed nodes {}", Parameter.p2pConfig.getSeedNodes()); + logger.info("Seed nodes {}", Parameter.p2pConfig.getSeedNodes()); } if (cli.hasOption("a")) { Parameter.p2pConfig.setActiveNodes(app.parseInetSocketAddressList(cli.getOptionValue("a"))); - log.info("Active nodes {}", Parameter.p2pConfig.getActiveNodes()); + logger.info("Active nodes {}", Parameter.p2pConfig.getActiveNodes()); } if (cli.hasOption("t")) { @@ -59,7 +59,7 @@ public static void main(String[] args) { List trustNodes = new ArrayList<>(); trustNodes.add(address.getAddress()); Parameter.p2pConfig.setTrustNodes(trustNodes); - log.info("Trust nodes {}", Parameter.p2pConfig.getTrustNodes()); + logger.info("Trust nodes {}", Parameter.p2pConfig.getTrustNodes()); } if (cli.hasOption("M")) { @@ -75,7 +75,7 @@ public static void main(String[] args) { } if (Parameter.p2pConfig.getMinConnections() > Parameter.p2pConfig.getMaxConnections()) { - log.error("Check maxConnections({}) >= minConnections({}) failed", + logger.error("Check maxConnections({}) >= minConnections({}) failed", Parameter.p2pConfig.getMaxConnections(), Parameter.p2pConfig.getMinConnections()); System.exit(0); } @@ -83,7 +83,7 @@ public static void main(String[] args) { if (cli.hasOption("d")) { int d = Integer.parseInt(cli.getOptionValue("d")); if (d != 0 && d != 1) { - log.error("Check discover failed, must be 0/1"); + logger.error("Check discover failed, must be 0/1"); System.exit(0); } Parameter.p2pConfig.setDiscoverEnable(d == 1); @@ -97,7 +97,7 @@ public static void main(String[] args) { Parameter.p2pConfig.setNetworkId(Integer.parseInt(cli.getOptionValue("v"))); } if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) { - log.info("Local ipv6: {}", Parameter.p2pConfig.getIpv6()); + logger.info("Local ipv6: {}", Parameter.p2pConfig.getIpv6()); } app.checkDnsOption(cli); @@ -135,7 +135,7 @@ private CommandLine parseCli(String[] args) throws ParseException { try { cli = cliParser.parse(options, args); } catch (ParseException e) { - log.error("Parse cli failed", e); + logger.error("Parse cli failed", e); printHelpMessage(kadOptions, dnsReadOptions, dnsPublishOptions); throw e; } @@ -175,18 +175,18 @@ private void checkDnsOption(CommandLine cli) { if (cli.hasOption(configDnsPrivate)) { String privateKey = cli.getOptionValue(configDnsPrivate); if (privateKey.length() != 64) { - log.error("Check {}, must be hex string of 64", configDnsPrivate); + logger.error("Check {}, must be hex string of 64", configDnsPrivate); System.exit(0); } try { ByteArray.fromHexString(privateKey); } catch (Exception ignore) { - log.error("Check {}, must be hex string of 64", configDnsPrivate); + logger.error("Check {}, must be hex string of 64", configDnsPrivate); System.exit(0); } publishConfig.setDnsPrivate(privateKey); } else { - log.error("Check {}, must not be null", configDnsPrivate); + logger.error("Check {}, must not be null", configDnsPrivate); System.exit(0); } @@ -203,14 +203,14 @@ private void checkDnsOption(CommandLine cli) { if (cli.hasOption(configDomain)) { publishConfig.setDnsDomain(cli.getOptionValue(configDomain)); } else { - log.error("Check {}, must not be null", configDomain); + logger.error("Check {}, must not be null", configDomain); System.exit(0); } if (cli.hasOption(configChangeThreshold)) { double changeThreshold = Double.parseDouble(cli.getOptionValue(configChangeThreshold)); if (changeThreshold >= 1.0) { - log.error("Check {}, range between (0.0 ~ 1.0]", + logger.error("Check {}, range between (0.0 ~ 1.0]", configChangeThreshold); } else { publishConfig.setChangeThreshold(changeThreshold); @@ -220,7 +220,7 @@ private void checkDnsOption(CommandLine cli) { if (cli.hasOption(configMaxMergeSize)) { int maxMergeSize = Integer.parseInt(cli.getOptionValue(configMaxMergeSize)); if (maxMergeSize > 5) { - log.error("Check {}, range between [1 ~ 5]", configMaxMergeSize); + logger.error("Check {}, range between [1 ~ 5]", configMaxMergeSize); } else { publishConfig.setMaxMergeSize(maxMergeSize); } @@ -229,7 +229,7 @@ private void checkDnsOption(CommandLine cli) { if (cli.hasOption(configServerType)) { String serverType = cli.getOptionValue(configServerType); if (!"aws".equalsIgnoreCase(serverType) && !"aliyun".equalsIgnoreCase(serverType)) { - log.error("Check {}, must be aws or aliyun", configServerType); + logger.error("Check {}, must be aws or aliyun", configServerType); System.exit(0); } if ("aws".equalsIgnoreCase(serverType)) { @@ -238,19 +238,19 @@ private void checkDnsOption(CommandLine cli) { publishConfig.setDnsType(DnsType.AliYun); } } else { - log.error("Check {}, must not be null", configServerType); + logger.error("Check {}, must not be null", configServerType); System.exit(0); } if (!cli.hasOption(configAccessId)) { - log.error("Check {}, must not be null", configAccessId); + logger.error("Check {}, must not be null", configAccessId); System.exit(0); } else { publishConfig.setAccessKeyId(cli.getOptionValue(configAccessId)); } if (!cli.hasOption(configAccessSecret)) { - log.error("Check {}, must not be null", configAccessSecret); + logger.error("Check {}, must not be null", configAccessSecret); System.exit(0); } else { publishConfig.setAccessKeySecret(cli.getOptionValue(configAccessSecret)); @@ -263,7 +263,7 @@ private void checkDnsOption(CommandLine cli) { } if (!cli.hasOption(configAwsRegion)) { - log.error("Check {}, must not be null", configAwsRegion); + logger.error("Check {}, must not be null", configAwsRegion); System.exit(0); } else { String region = cli.getOptionValue(configAwsRegion); @@ -271,7 +271,7 @@ private void checkDnsOption(CommandLine cli) { } } else { if (!cli.hasOption(configAliEndPoint)) { - log.error("Check {}, must not be null", configAliEndPoint); + logger.error("Check {}, must not be null", configAliEndPoint); System.exit(0); } else { publishConfig.setAliDnsEndpoint(cli.getOptionValue(configAliEndPoint)); From 813ee973ca582dfa7a7a93d5057cf770833e440e Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 02:48:22 +0800 Subject: [PATCH 3/6] build(common): switch from external libp2p to local p2p module 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. --- common/build.gradle | 18 +----------------- gradle/verification-metadata.xml | 11 ----------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/common/build.gradle b/common/build.gradle index 98fc3257190..45aab494a83 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -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") } diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index f434248cddf..6da91eca3c1 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -870,17 +870,6 @@ - - - - - - - - - - - From 7e88b13da3c851fba5c7c98b8f6bba9d7d19bfbc Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 03:24:39 +0800 Subject: [PATCH 4/6] style(p2p): fix checkstyle violations and enable checkstyle 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. --- p2p/build.gradle | 13 +- p2p/src/main/java/org/tron/p2p/P2pConfig.java | 4 +- .../java/org/tron/p2p/P2pEventHandler.java | 12 +- .../main/java/org/tron/p2p/base/Constant.java | 17 +- .../java/org/tron/p2p/base/Parameter.java | 17 +- .../java/org/tron/p2p/connection/Channel.java | 90 ++- .../tron/p2p/connection/ChannelManager.java | 43 +- .../business/detect/NodeDetectService.java | 62 +- .../connection/business/detect/NodeStat.java | 3 +- .../business/handshake/HandshakeService.java | 13 +- .../business/keepalive/KeepAliveService.java | 56 +- .../business/pool/ConnPoolService.java | 223 ++++--- .../business/upgrade/UpgradeController.java | 5 +- .../p2p/connection/message/MessageType.java | 2 +- .../message/base/P2pDisconnectMessage.java | 12 +- .../message/detect/StatusMessage.java | 14 +- .../message/handshake/HelloMessage.java | 15 +- .../message/keepalive/PingMessage.java | 6 +- .../message/keepalive/PongMessage.java | 6 +- .../p2p/connection/socket/MessageHandler.java | 10 +- .../socket/P2pChannelInitializer.java | 40 +- .../P2pProtobufVarint32FrameDecoder.java | 6 +- .../p2p/connection/socket/PeerClient.java | 54 +- .../p2p/connection/socket/PeerServer.java | 14 +- .../tron/p2p/discover/DiscoverService.java | 4 +- .../main/java/org/tron/p2p/discover/Node.java | 54 +- .../org/tron/p2p/discover/NodeManager.java | 1 - .../tron/p2p/discover/message/Message.java | 1 - .../p2p/discover/message/MessageType.java | 3 +- .../discover/message/kad/FindNodeMessage.java | 14 +- .../p2p/discover/message/kad/KadMessage.java | 3 +- .../message/kad/NeighborsMessage.java | 12 +- .../p2p/discover/message/kad/PingMessage.java | 13 +- .../p2p/discover/message/kad/PongMessage.java | 11 +- .../discover/protocol/kad/DiscoverTask.java | 37 +- .../p2p/discover/protocol/kad/KadService.java | 48 +- .../discover/protocol/kad/NodeHandler.java | 37 +- .../protocol/kad/table/KademliaOptions.java | 4 +- .../protocol/kad/table/NodeTable.java | 2 +- .../p2p/discover/socket/DiscoverServer.java | 49 +- .../p2p/discover/socket/MessageHandler.java | 16 +- .../p2p/discover/socket/P2pPacketDecoder.java | 36 +- .../tron/p2p/discover/socket/UdpEvent.java | 4 +- .../java/org/tron/p2p/dns/DnsManager.java | 28 +- .../main/java/org/tron/p2p/dns/DnsNode.java | 20 +- .../org/tron/p2p/dns/lookup/LookUpTxt.java | 71 ++- .../java/org/tron/p2p/dns/sync/Client.java | 50 +- .../org/tron/p2p/dns/sync/ClientTree.java | 31 +- .../java/org/tron/p2p/dns/sync/LinkCache.java | 8 +- .../org/tron/p2p/dns/sync/RandomIterator.java | 19 +- .../org/tron/p2p/dns/sync/SubtreeSync.java | 1 - .../java/org/tron/p2p/dns/tree/Algorithm.java | 37 +- .../org/tron/p2p/dns/tree/BranchEntry.java | 4 +- .../java/org/tron/p2p/dns/tree/Entry.java | 1 - .../java/org/tron/p2p/dns/tree/LinkEntry.java | 13 +- .../org/tron/p2p/dns/tree/NodesEntry.java | 6 +- .../java/org/tron/p2p/dns/tree/RootEntry.java | 30 +- .../main/java/org/tron/p2p/dns/tree/Tree.java | 27 +- .../org/tron/p2p/dns/update/AliClient.java | 76 ++- .../org/tron/p2p/dns/update/AwsClient.java | 133 ++-- .../java/org/tron/p2p/dns/update/DnsType.java | 1 - .../java/org/tron/p2p/dns/update/Publish.java | 1 - .../tron/p2p/dns/update/PublishConfig.java | 7 +- .../tron/p2p/dns/update/PublishService.java | 67 +- .../org/tron/p2p/exception/DnsException.java | 3 +- .../org/tron/p2p/exception/P2pException.java | 1 - .../java/org/tron/p2p/stats/TrafficStats.java | 17 +- .../java/org/tron/p2p/utils/ByteArray.java | 35 +- .../org/tron/p2p/utils/CollectionUtils.java | 1 - .../main/java/org/tron/p2p/utils/NetUtil.java | 83 ++- .../java/org/tron/p2p/utils/ProtoUtil.java | 15 +- .../java/org/web3j/crypto/ECDSASignature.java | 87 +-- .../main/java/org/web3j/crypto/ECKeyPair.java | 168 ++--- p2p/src/main/java/org/web3j/crypto/Hash.java | 210 +++--- p2p/src/main/java/org/web3j/crypto/Sign.java | 599 +++++++++--------- .../exceptions/MessageDecodingException.java | 26 +- .../exceptions/MessageEncodingException.java | 26 +- .../main/java/org/web3j/utils/Assertions.java | 34 +- .../main/java/org/web3j/utils/Numeric.java | 358 +++++------ .../main/java/org/web3j/utils/Strings.java | 72 ++- .../p2p/connection/ChannelManagerTest.java | 27 +- .../p2p/connection/ConnPoolServiceTest.java | 23 +- .../org/tron/p2p/connection/MessageTest.java | 7 +- .../org/tron/p2p/connection/SocketTest.java | 43 +- .../discover/protocol/kad/KadServiceTest.java | 11 +- .../protocol/kad/NodeHandlerTest.java | 7 +- .../protocol/kad/table/NodeEntryTest.java | 52 +- .../protocol/kad/table/NodeTableTest.java | 21 +- .../kad/table/TimeComparatorTest.java | 4 +- .../java/org/tron/p2p/dns/AlgorithmTest.java | 16 +- .../java/org/tron/p2p/dns/AwsRoute53Test.java | 251 +++++--- .../java/org/tron/p2p/dns/DnsNodeTest.java | 45 +- .../java/org/tron/p2p/dns/LinkCacheTest.java | 1 - .../java/org/tron/p2p/dns/RandomTest.java | 6 +- .../test/java/org/tron/p2p/dns/SyncTest.java | 4 +- .../test/java/org/tron/p2p/dns/TreeTest.java | 192 +++--- .../org/tron/p2p/example/DnsExample1.java | 18 +- .../org/tron/p2p/example/DnsExample2.java | 14 +- .../org/tron/p2p/example/ImportUsing.java | 4 - .../java/org/tron/p2p/example/StartApp.java | 125 ++-- .../org/tron/p2p/utils/ByteArrayTest.java | 15 +- .../java/org/tron/p2p/utils/NetUtilTest.java | 76 ++- .../org/tron/p2p/utils/ProtoUtilTest.java | 1 - 103 files changed, 2382 insertions(+), 2033 deletions(-) diff --git a/p2p/build.gradle b/p2p/build.gradle index f0e6a4ff2ba..eb85287c672 100644 --- a/p2p/build.gradle +++ b/p2p/build.gradle @@ -1,7 +1,16 @@ apply plugin: 'com.google.protobuf' +apply plugin: 'checkstyle' -// TODO: enable checkstyle after code style alignment -// apply plugin: 'checkstyle' +checkstyle { + toolVersion = '8.7' + configFile = file("${rootDir}/config/checkstyle/checkStyleAll.xml") + maxWarnings = 0 +} + +checkstyleMain { + source = 'src/main/java' + exclude '**/protos/**' +} def protobufVersion = '3.25.8' def grpcVersion = '1.75.0' diff --git a/p2p/src/main/java/org/tron/p2p/P2pConfig.java b/p2p/src/main/java/org/tron/p2p/P2pConfig.java index 9a3aef6b9df..6724acddbc3 100644 --- a/p2p/src/main/java/org/tron/p2p/P2pConfig.java +++ b/p2p/src/main/java/org/tron/p2p/P2pConfig.java @@ -29,9 +29,9 @@ public class P2pConfig { private boolean disconnectionPolicyEnable = false; private boolean nodeDetectEnable = false; - //dns read config + // dns read config private List treeUrls = new ArrayList<>(); - //dns publish config + // dns publish config private PublishConfig publishConfig = new PublishConfig(); } diff --git a/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java b/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java index 7ca3f235049..ace751da243 100644 --- a/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java +++ b/p2p/src/main/java/org/tron/p2p/P2pEventHandler.java @@ -6,15 +6,11 @@ public abstract class P2pEventHandler { - @Getter - protected Set messageTypes; + @Getter protected Set messageTypes; - public void onConnect(Channel channel) { - } + public void onConnect(Channel channel) {} - public void onDisconnect(Channel channel) { - } + public void onDisconnect(Channel channel) {} - public void onMessage(Channel channel, byte[] data) { - } + public void onMessage(Channel channel, byte[] data) {} } diff --git a/p2p/src/main/java/org/tron/p2p/base/Constant.java b/p2p/src/main/java/org/tron/p2p/base/Constant.java index b87cfa287f5..fd648c87f3f 100644 --- a/p2p/src/main/java/org/tron/p2p/base/Constant.java +++ b/p2p/src/main/java/org/tron/p2p/base/Constant.java @@ -6,11 +6,14 @@ public class Constant { public static final int NODE_ID_LEN = 64; - public static final List ipV4Urls = Arrays.asList( - "http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/"); - public static final List ipV6Urls = Arrays.asList( - "https://v6.ident.me", "http://6.ipw.cn/", "https://api6.ipify.org", - "https://ipv6.icanhazip.com"); - public static final String ipV4Hex = "00000000"; //32 bit - public static final String ipV6Hex = "00000000000000000000000000000000"; //128 bit + public static final List ipV4Urls = + Arrays.asList("http://checkip.amazonaws.com", "https://ifconfig.me/ip", "https://4.ipw.cn/"); + public static final List ipV6Urls = + Arrays.asList( + "https://v6.ident.me", + "http://6.ipw.cn/", + "https://api6.ipify.org", + "https://ipv6.icanhazip.com"); + public static final String ipV4Hex = "00000000"; // 32 bit + public static final String ipV6Hex = "00000000000000000000000000000000"; // 128 bit } diff --git a/p2p/src/main/java/org/tron/p2p/base/Parameter.java b/p2p/src/main/java/org/tron/p2p/base/Parameter.java index 50055949dc8..bdeb4129d31 100644 --- a/p2p/src/main/java/org/tron/p2p/base/Parameter.java +++ b/p2p/src/main/java/org/tron/p2p/base/Parameter.java @@ -1,11 +1,10 @@ package org.tron.p2p.base; +import com.google.protobuf.ByteString; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - -import com.google.protobuf.ByteString; import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.tron.p2p.P2pConfig; @@ -59,16 +58,16 @@ public static void addP2pEventHandle(P2pEventHandler p2PEventHandler) throws P2p } public static Discover.Endpoint getHomeNode() { - Discover.Endpoint.Builder builder = Discover.Endpoint.newBuilder() - .setNodeId(ByteString.copyFrom(Parameter.p2pConfig.getNodeID())) - .setPort(Parameter.p2pConfig.getPort()); + Discover.Endpoint.Builder builder = + Discover.Endpoint.newBuilder() + .setNodeId(ByteString.copyFrom(Parameter.p2pConfig.getNodeID())) + .setPort(Parameter.p2pConfig.getPort()); if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) { - builder.setAddress(ByteString.copyFrom( - ByteArray.fromString(Parameter.p2pConfig.getIp()))); + builder.setAddress(ByteString.copyFrom(ByteArray.fromString(Parameter.p2pConfig.getIp()))); } if (StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) { - builder.setAddressIpv6(ByteString.copyFrom( - ByteArray.fromString(Parameter.p2pConfig.getIpv6()))); + builder.setAddressIpv6( + ByteString.copyFrom(ByteArray.fromString(Parameter.p2pConfig.getIpv6()))); } return builder.build(); } diff --git a/p2p/src/main/java/org/tron/p2p/connection/Channel.java b/p2p/src/main/java/org/tron/p2p/connection/Channel.java index 811904b7d27..a919c4953d3 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/Channel.java +++ b/p2p/src/main/java/org/tron/p2p/connection/Channel.java @@ -37,43 +37,22 @@ public class Channel { public volatile boolean waitForPong = false; public volatile long pingSent = System.currentTimeMillis(); - @Getter - private HelloMessage helloMessage; - @Getter - private Node node; - @Getter - private int version; - @Getter - private ChannelHandlerContext ctx; - @Getter - private InetSocketAddress inetSocketAddress; - @Getter - private InetAddress inetAddress; - @Getter - private volatile long disconnectTime; - @Getter - @Setter - private volatile boolean isDisconnect = false; - @Getter - @Setter - private long lastSendTime = System.currentTimeMillis(); - @Getter - private final long startTime = System.currentTimeMillis(); - @Getter - private boolean isActive = false; - @Getter - private boolean isTrustPeer; - @Getter - @Setter - private volatile boolean finishHandshake; - @Getter - @Setter - private String nodeId; - @Setter - @Getter - private boolean discoveryMode; - @Getter - private long avgLatency; + @Getter private HelloMessage helloMessage; + @Getter private Node node; + @Getter private int version; + @Getter private ChannelHandlerContext ctx; + @Getter private InetSocketAddress inetSocketAddress; + @Getter private InetAddress inetAddress; + @Getter private volatile long disconnectTime; + @Getter @Setter private volatile boolean isDisconnect = false; + @Getter @Setter private long lastSendTime = System.currentTimeMillis(); + @Getter private final long startTime = System.currentTimeMillis(); + @Getter private boolean isActive = false; + @Getter private boolean isTrustPeer; + @Getter @Setter private volatile boolean finishHandshake; + @Getter @Setter private String nodeId; + @Setter @Getter private boolean discoveryMode; + @Getter private long avgLatency; private long count; public void init(ChannelPipeline pipeline, String nodeId, boolean discoveryMode) { @@ -102,8 +81,11 @@ public void processException(Throwable throwable) { || throwable instanceof CorruptedFrameException) { logger.warn("Close peer {}, reason: {}", address, throwable.getMessage()); } else if (baseThrowable instanceof P2pException) { - logger.warn("Close peer {}, type: ({}), info: {}", - address, ((P2pException) baseThrowable).getType(), baseThrowable.getMessage()); + logger.warn( + "Close peer {}, type: ({}), info: {}", + address, + ((P2pException) baseThrowable).getType(), + baseThrowable.getMessage()); } else { logger.error("Close peer {}, exception caught", address, throwable); } @@ -113,7 +95,7 @@ public void processException(Throwable throwable) { public void setHelloMessage(HelloMessage helloMessage) { this.helloMessage = helloMessage; this.node = helloMessage.getFrom(); - this.nodeId = node.getHexId(); //update node id from handshake + this.nodeId = node.getHexId(); // update node id from handshake this.version = helloMessage.getVersion(); } @@ -148,8 +130,10 @@ public void send(byte[] data) { try { byte type = data[0]; if (isDisconnect) { - logger.warn("Send to {} failed as channel has closed, message-type:{} ", - ctx.channel().remoteAddress(), type); + logger.warn( + "Send to {} failed as channel has closed, message-type:{} ", + ctx.channel().remoteAddress(), + type); return; } @@ -158,13 +142,16 @@ public void send(byte[] data) { } ByteBuf byteBuf = Unpooled.wrappedBuffer(data); - ctx.writeAndFlush(byteBuf).addListener((ChannelFutureListener) future -> { - if (!future.isSuccess() && !isDisconnect) { - logger.warn("Send to {} failed, message-type:{}, cause:{}", - ctx.channel().remoteAddress(), ByteArray.byte2int(type), - future.cause().getMessage()); - } - }); + ctx.writeAndFlush(byteBuf) + .addListener((ChannelFutureListener) future -> { + if (!future.isSuccess() && !isDisconnect) { + logger.warn( + "Send to {} failed, message-type:{}, cause:{}", + ctx.channel().remoteAddress(), + ByteArray.byte2int(type), + future.cause().getMessage()); + } + }); setLastSendTime(System.currentTimeMillis()); } catch (Exception e) { logger.warn("Send message to {} failed, {}", inetSocketAddress, e.getMessage()); @@ -197,8 +184,7 @@ public int hashCode() { @Override public String toString() { - return String.format("%s | %s", inetSocketAddress, - StringUtils.isEmpty(nodeId) ? "" : nodeId); + return String.format( + "%s | %s", inetSocketAddress, StringUtils.isEmpty(nodeId) ? "" : nodeId); } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java index 78db925c3dd..54d94c37f56 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java +++ b/p2p/src/main/java/org/tron/p2p/connection/ChannelManager.java @@ -35,28 +35,23 @@ @Slf4j(topic = "net") public class ChannelManager { - @Getter - private static NodeDetectService nodeDetectService; + @Getter private static NodeDetectService nodeDetectService; private static PeerServer peerServer; - @Getter - private static PeerClient peerClient; + @Getter private static PeerClient peerClient; - @Getter - private static ConnPoolService connPoolService; + @Getter private static ConnPoolService connPoolService; private static KeepAliveService keepAliveService; - @Getter - private static HandshakeService handshakeService; + @Getter private static HandshakeService handshakeService; - @Getter - private static final Map channels = new ConcurrentHashMap<>(); + @Getter private static final Map channels = new ConcurrentHashMap<>(); @Getter - private static final Cache bannedNodes = CacheBuilder - .newBuilder().maximumSize(2000).build(); //ban timestamp + private static final Cache bannedNodes = + CacheBuilder.newBuilder().maximumSize(2000).build(); // ban timestamp private static boolean isInit = false; public static volatile boolean isShutdown = false; @@ -77,7 +72,9 @@ public static void init() { } public static void connect(InetSocketAddress address) { - peerClient.connect(address.getAddress().getHostAddress(), address.getPort(), + peerClient.connect( + address.getAddress().getHostAddress(), + address.getPort(), ByteArray.toHexString(NetUtil.getNodeId())); } @@ -167,15 +164,15 @@ public static DisconnectReason getDisconnectReason(DisconnectCode code) { case MAX_CONNECTION_WITH_SAME_IP: disconnectReason = DisconnectReason.TOO_MANY_PEERS_WITH_SAME_IP; break; - default: { + default: disconnectReason = DisconnectReason.UNKNOWN; - } } return disconnectReason; } public static void logDisconnectReason(Channel channel, DisconnectReason reason) { - logger.info("Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name()); + logger.info( + "Try to close channel: {}, reason: {}", channel.getInetSocketAddress(), reason.name()); } public static void banNode(InetAddress inetAddress, Long banTime) { @@ -198,7 +195,6 @@ public static void close() { nodeDetectService.close(); } - public static void processMessage(Channel channel, byte[] data) throws P2pException { if (data == null || data.length == 0) { throw new P2pException(TypeEnum.EMPTY_MESSAGE, ""); @@ -271,11 +267,14 @@ public static synchronized void updateNodeId(Channel channel, String nodeId) { } List list = new ArrayList<>(); - channels.values().forEach(c -> { - if (nodeId.equals(c.getNodeId())) { - list.add(c); - } - }); + channels + .values() + .forEach( + c -> { + if (nodeId.equals(c.getNodeId())) { + list.add(c); + } + }); if (list.size() <= 1) { return; } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java index df2cec77f1a..091b9a5f964 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java @@ -32,11 +32,12 @@ public class NodeDetectService implements MessageProcess { private Map nodeStatMap = new ConcurrentHashMap<>(); @Getter - private static final Cache badNodesCache = CacheBuilder - .newBuilder().maximumSize(5000).expireAfterWrite(1, TimeUnit.HOURS).build(); + private static final Cache badNodesCache = + CacheBuilder.newBuilder().maximumSize(5000).expireAfterWrite(1, TimeUnit.HOURS).build(); - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("nodeDetectService").build()); + private final ScheduledExecutorService executor = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("nodeDetectService").build()); private final long NODE_DETECT_THRESHOLD = 5 * 60 * 1000; @@ -54,19 +55,22 @@ public class NodeDetectService implements MessageProcess { private final int MIN_NODES = 200; - public void init(PeerClient peerClient) { if (!Parameter.p2pConfig.isNodeDetectEnable()) { return; } this.peerClient = peerClient; - executor.scheduleWithFixedDelay(() -> { - try { - work(); - } catch (Exception t) { - logger.warn("Exception in node detect worker, {}", t.getMessage()); - } - }, 1, 5, TimeUnit.SECONDS); + executor.scheduleWithFixedDelay( + () -> { + try { + work(); + } catch (Exception t) { + logger.warn("Exception in node detect worker, {}", t.getMessage()); + } + }, + 1, + 5, + TimeUnit.SECONDS); } public void close() { @@ -103,12 +107,13 @@ public void work() { public void trimNodeMap() { long now = System.currentTimeMillis(); - nodeStatMap.forEach((k, v) -> { - if (!v.finishDetect() && v.getLastDetectTime() < now - NODE_DETECT_TIMEOUT) { - nodeStatMap.remove(k); - badNodesCache.put(k.getAddress(), System.currentTimeMillis()); - } - }); + nodeStatMap.forEach( + (k, v) -> { + if (!v.finishDetect() && v.getLastDetectTime() < now - NODE_DETECT_TIMEOUT) { + nodeStatMap.remove(k); + badNodesCache.put(k.getAddress(), System.currentTimeMillis()); + } + }); } private void loadNodes() { @@ -137,8 +142,8 @@ private void detect(NodeStat stat) { setLastDetectTime(stat); peerClient.connectAsync(stat.getNode(), true); } catch (Exception e) { - logger.warn("Detect node {} failed, {}", - stat.getNode().getPreferInetSocketAddress(), e.getMessage()); + logger.warn( + "Detect node {} failed, {}", stat.getNode().getPreferInetSocketAddress(), e.getMessage()); nodeStatMap.remove(stat.getSocketAddress()); } } @@ -160,8 +165,7 @@ public synchronized void processMessage(Channel channel, Message message) { } long cost = System.currentTimeMillis() - nodeStat.getLastDetectTime(); - if (cost > NODE_DETECT_TIMEOUT - || statusMessage.getRemainConnections() == 0) { + if (cost > NODE_DETECT_TIMEOUT || statusMessage.getRemainConnections() == 0) { badNodesCache.put(socketAddress.getAddress(), cost); nodeStatMap.remove(socketAddress); } @@ -211,11 +215,14 @@ private synchronized void setStatusMessage(NodeStat nodeStat, StatusMessage mess public synchronized List getConnectableNodes() { List stats = new ArrayList<>(); List nodes = new ArrayList<>(); - nodeStatMap.values().forEach(stat -> { - if (stat.getStatusMessage() != null) { - stats.add(stat); - } - }); + nodeStatMap + .values() + .forEach( + stat -> { + if (stat.getStatusMessage() != null) { + stats.add(stat); + } + }); if (stats.isEmpty()) { return nodes; @@ -225,5 +232,4 @@ public synchronized List getConnectableNodes() { stats.forEach(stat -> nodes.add(stat.getNode())); return nodes; } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java index 2f2ef4a5ae7..395df70e314 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeStat.java @@ -1,11 +1,10 @@ package org.tron.p2p.connection.business.detect; +import java.net.InetSocketAddress; import lombok.Data; import org.tron.p2p.connection.message.detect.StatusMessage; import org.tron.p2p.discover.Node; -import java.net.InetSocketAddress; - @Data public class NodeStat { private int totalCount; diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java index 38ba25c22ed..250f6e78135 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/handshake/HandshakeService.java @@ -54,8 +54,9 @@ public void processMessage(Channel channel, Message message) { if (msg.getCode() != DisconnectCode.NORMAL.getValue() || (msg.getNetworkId() != networkId && msg.getVersion() != networkId)) { DisconnectCode disconnectCode = DisconnectCode.forNumber(msg.getCode()); - //v0.1 have version, v0.2 both have version and networkId - logger.info("Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}", + // v0.1 have version, v0.2 both have version and networkId + logger.info( + "Handshake failed {}, code: {}, reason: {}, networkId: {}, version: {}", channel.getInetSocketAddress(), msg.getCode(), disconnectCode.name(), @@ -68,8 +69,11 @@ public void processMessage(Channel channel, Message message) { } else { if (msg.getNetworkId() != networkId) { - logger.info("Peer {} different network id, peer->{}, me->{}", - channel.getInetSocketAddress(), msg.getNetworkId(), networkId); + logger.info( + "Peer {} different network id, peer->{}, me->{}", + channel.getInetSocketAddress(), + msg.getNetworkId(), + networkId); sendHelloMsg(channel, DisconnectCode.DIFFERENT_VERSION, msg.getTimestamp()); logDisconnectReason(channel, DisconnectReason.DIFFERENT_VERSION); channel.close(); @@ -86,5 +90,4 @@ private void sendHelloMsg(Channel channel, DisconnectCode code, long time) { HelloMessage helloMessage = new HelloMessage(code, time); channel.send(helloMessage); } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java index 2b7a3e5ef91..62fc3833b9e 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java @@ -20,33 +20,39 @@ @Slf4j(topic = "net") public class KeepAliveService implements MessageProcess { - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("keepAlive").build()); + private final ScheduledExecutorService executor = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("keepAlive").build()); public void init() { - executor.scheduleWithFixedDelay(() -> { - try { - long now = System.currentTimeMillis(); - ChannelManager.getChannels().values().stream() - .filter(p -> !p.isDisconnect()) - .forEach(p -> { - if (p.waitForPong) { - if (now - p.pingSent > KEEP_ALIVE_TIMEOUT) { - p.send(new P2pDisconnectMessage(DisconnectReason.PING_TIMEOUT)); - p.close(); - } - } else { - if (now - p.getLastSendTime() > PING_TIMEOUT && p.isFinishHandshake()) { - p.send(new PingMessage()); - p.waitForPong = true; - p.pingSent = now; - } - } - }); - } catch (Exception t) { - logger.error("Exception in keep alive task", t); - } - }, 2, 2, TimeUnit.SECONDS); + executor.scheduleWithFixedDelay( + () -> { + try { + long now = System.currentTimeMillis(); + ChannelManager.getChannels().values().stream() + .filter(p -> !p.isDisconnect()) + .forEach( + p -> { + if (p.waitForPong) { + if (now - p.pingSent > KEEP_ALIVE_TIMEOUT) { + p.send(new P2pDisconnectMessage(DisconnectReason.PING_TIMEOUT)); + p.close(); + } + } else { + if (now - p.getLastSendTime() > PING_TIMEOUT && p.isFinishHandshake()) { + p.send(new PingMessage()); + p.waitForPong = true; + p.pingSent = now; + } + } + }); + } catch (Exception t) { + logger.error("Exception in keep alive task", t); + } + }, + 2, + 2, + TimeUnit.SECONDS); } public void close() { diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java index bc5a3423645..126df7b83da 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java @@ -43,16 +43,18 @@ public class ConnPoolService extends P2pEventHandler { private final List activePeers = Collections.synchronizedList(new ArrayList<>()); - private final Cache peerClientCache = CacheBuilder.newBuilder() - .maximumSize(1000).expireAfterWrite(120, TimeUnit.SECONDS).recordStats().build(); - @Getter - private final AtomicInteger passivePeersCount = new AtomicInteger(0); - @Getter - private final AtomicInteger activePeersCount = new AtomicInteger(0); - @Getter - private final AtomicInteger connectingPeersCount = new AtomicInteger(0); - private final ScheduledThreadPoolExecutor poolLoopExecutor = new ScheduledThreadPoolExecutor(1, - BasicThreadFactory.builder().namingPattern("connPool").build()); + private final Cache peerClientCache = + CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterWrite(120, TimeUnit.SECONDS) + .recordStats() + .build(); + @Getter private final AtomicInteger passivePeersCount = new AtomicInteger(0); + @Getter private final AtomicInteger activePeersCount = new AtomicInteger(0); + @Getter private final AtomicInteger connectingPeersCount = new AtomicInteger(0); + private final ScheduledThreadPoolExecutor poolLoopExecutor = + new ScheduledThreadPoolExecutor( + 1, BasicThreadFactory.builder().namingPattern("connPool").build()); private final ScheduledExecutorService disconnectExecutor = Executors.newSingleThreadScheduledExecutor( BasicThreadFactory.builder().namingPattern("randomDisconnect").build()); @@ -62,33 +64,41 @@ public class ConnPoolService extends P2pEventHandler { private final List configActiveNodes = new ArrayList<>(); public ConnPoolService() { - this.messageTypes = new HashSet<>(); //no message type registers + this.messageTypes = new HashSet<>(); // no message type registers try { Parameter.addP2pEventHandle(this); configActiveNodes.addAll(p2pConfig.getActiveNodes()); } catch (P2pException e) { - //no exception will throw + // no exception will throw } } public void init(PeerClient peerClient) { this.peerClient = peerClient; - poolLoopExecutor.scheduleWithFixedDelay(() -> { - try { - connect(false); - } catch (Exception t) { - logger.error("Exception in poolLoopExecutor worker", t); - } - }, 200, 3600, TimeUnit.MILLISECONDS); + poolLoopExecutor.scheduleWithFixedDelay( + () -> { + try { + connect(false); + } catch (Exception t) { + logger.error("Exception in poolLoopExecutor worker", t); + } + }, + 200, + 3600, + TimeUnit.MILLISECONDS); if (p2pConfig.isDisconnectionPolicyEnable()) { - disconnectExecutor.scheduleWithFixedDelay(() -> { - try { - check(); - } catch (Exception t) { - logger.error("Exception in disconnectExecutor worker", t); - } - }, 30, 30, TimeUnit.SECONDS); + disconnectExecutor.scheduleWithFixedDelay( + () -> { + try { + check(); + } catch (Exception t) { + logger.error("Exception in disconnectExecutor worker", t); + } + }, + 30, + 30, + TimeUnit.SECONDS); } } @@ -106,40 +116,53 @@ private void addNode(Set inetSet, Node node) { private void connect(boolean isFilterActiveNodes) { List connectNodes = new ArrayList<>(); - //collect already used nodes in channelManager + // collect already used nodes in channelManager Set addressInUse = new HashSet<>(); Set inetInUse = new HashSet<>(); Set nodesInUse = new HashSet<>(); nodesInUse.add(Hex.toHexString(p2pConfig.getNodeID())); - ChannelManager.getChannels().values().forEach(channel -> { - if (StringUtils.isNotEmpty(channel.getNodeId())) { - nodesInUse.add(channel.getNodeId()); - } - addressInUse.add(channel.getInetAddress()); - inetInUse.add(channel.getInetSocketAddress()); - addNode(inetInUse, channel.getNode()); - }); + ChannelManager.getChannels() + .values() + .forEach( + channel -> { + if (StringUtils.isNotEmpty(channel.getNodeId())) { + nodesInUse.add(channel.getNodeId()); + } + addressInUse.add(channel.getInetAddress()); + inetInUse.add(channel.getInetSocketAddress()); + addNode(inetInUse, channel.getNode()); + }); - addNode(inetInUse, new Node(Parameter.p2pConfig.getNodeID(), Parameter.p2pConfig.getIp(), - Parameter.p2pConfig.getIpv6(), Parameter.p2pConfig.getPort())); + addNode( + inetInUse, + new Node( + Parameter.p2pConfig.getNodeID(), + Parameter.p2pConfig.getIp(), + Parameter.p2pConfig.getIpv6(), + Parameter.p2pConfig.getPort())); - p2pConfig.getActiveNodes().forEach(address -> { - if (!isFilterActiveNodes && !inetInUse.contains(address) && !addressInUse.contains( - address.getAddress())) { - addressInUse.add(address.getAddress()); - inetInUse.add(address); - Node node = new Node(address); //use a random NodeId for config activeNodes - if (node.getPreferInetSocketAddress() != null) { - connectNodes.add(node); - } - } - }); + p2pConfig + .getActiveNodes() + .forEach( + address -> { + if (!isFilterActiveNodes + && !inetInUse.contains(address) + && !addressInUse.contains(address.getAddress())) { + addressInUse.add(address.getAddress()); + inetInUse.add(address); + Node node = new Node(address); // use a random NodeId for config activeNodes + if (node.getPreferInetSocketAddress() != null) { + connectNodes.add(node); + } + } + }); - //calculate lackSize exclude config activeNodes + // calculate lackSize exclude config activeNodes int activeLackSize = p2pConfig.getMinActiveConnections() - connectingPeersCount.get(); - int size = Math.max( - p2pConfig.getMinConnections() - connectingPeersCount.get() - passivePeersCount.get(), - activeLackSize); + int size = + Math.max( + p2pConfig.getMinConnections() - connectingPeersCount.get() - passivePeersCount.get(), + activeLackSize); if (p2pConfig.getMinConnections() <= activePeers.size() && activeLackSize <= 0) { size = 0; } @@ -180,7 +203,7 @@ private void connect(boolean isFilterActiveNodes) { if (validNode(node, nodesInUse, inetInUse, null)) { DnsNode copyNode = (DnsNode) node.clone(); copyNode.setId(NetUtil.getNodeId()); - //for node1 {ipv4_1, ipv6}, node2 {ipv4_2, ipv6}, we will not connect it twice + // for node1 {ipv4_1, ipv6}, node2 {ipv4_2, ipv6}, we will not connect it twice addNode(inetInUse, node); filtered.add(copyNode); } @@ -189,24 +212,31 @@ private void connect(boolean isFilterActiveNodes) { connectNodes.addAll(newNodes); } - logger.debug("Lack size:{}, connectNodes size:{}, is disconnect trigger: {}", - size, connectNodes.size(), isFilterActiveNodes); - //establish tcp connection with chose nodes by peerClient + logger.debug( + "Lack size:{}, connectNodes size:{}, is disconnect trigger: {}", + size, + connectNodes.size(), + isFilterActiveNodes); + // establish tcp connection with chose nodes by peerClient { - connectNodes.forEach(n -> { - logger.info("Connect to peer {}", n.getPreferInetSocketAddress()); - peerClient.connectAsync(n, false); - peerClientCache.put(n.getPreferInetSocketAddress().getAddress(), - System.currentTimeMillis()); - if (!configActiveNodes.contains(n.getPreferInetSocketAddress())) { - connectingPeersCount.incrementAndGet(); - } - }); + connectNodes.forEach( + n -> { + logger.info("Connect to peer {}", n.getPreferInetSocketAddress()); + peerClient.connectAsync(n, false); + peerClientCache.put( + n.getPreferInetSocketAddress().getAddress(), System.currentTimeMillis()); + if (!configActiveNodes.contains(n.getPreferInetSocketAddress())) { + connectingPeersCount.incrementAndGet(); + } + }); } } - public List getNodes(Set nodesInUse, Set inetInUse, - List connectableNodes, int limit) { + public List getNodes( + Set nodesInUse, + Set inetInUse, + List connectableNodes, + int limit) { List filtered = new ArrayList<>(); Set dynamicInetInUse = new HashSet<>(inetInUse); for (Node node : connectableNodes) { @@ -220,15 +250,17 @@ public List getNodes(Set nodesInUse, Set inetIn return CollectionUtils.truncate(filtered, limit); } - private boolean validNode(Node node, Set nodesInUse, Set inetInUse, + private boolean validNode( + Node node, + Set nodesInUse, + Set inetInUse, Set dynamicInet) { long now = System.currentTimeMillis(); InetSocketAddress inetSocketAddress = node.getPreferInetSocketAddress(); InetAddress inetAddress = inetSocketAddress.getAddress(); Long forbiddenTime = ChannelManager.getBannedNodes().getIfPresent(inetAddress); if ((forbiddenTime != null && now <= forbiddenTime) - || (ChannelManager.getConnectionNum(inetAddress) - >= p2pConfig.getMaxConnectionsWithSameIp()) + || (ChannelManager.getConnectionNum(inetAddress) >= p2pConfig.getMaxConnectionsWithSameIp()) || (node.getId() != null && nodesInUse.contains(node.getHexId())) || (peerClientCache.getIfPresent(inetAddress) != null) || inetInUse.contains(inetSocketAddress) @@ -244,11 +276,12 @@ private void check() { } List channels = new ArrayList<>(activePeers); - Collection peers = channels.stream() - .filter(peer -> !peer.isDisconnect()) - .filter(peer -> !peer.isTrustPeer()) - .filter(peer -> !peer.isActive()) - .collect(Collectors.toList()); + Collection peers = + channels.stream() + .filter(peer -> !peer.isDisconnect()) + .filter(peer -> !peer.isTrustPeer()) + .filter(peer -> !peer.isActive()) + .collect(Collectors.toList()); // if len(peers) >= 0, disconnect randomly if (!peers.isEmpty()) { @@ -261,8 +294,11 @@ private void check() { } private synchronized void logActivePeers() { - logger.info("Peer stats: channels {}, activePeers {}, active {}, passive {}", - ChannelManager.getChannels().size(), activePeers.size(), activePeersCount.get(), + logger.info( + "Peer stats: channels {}, activePeers {}, active {}, passive {}", + ChannelManager.getChannels().size(), + activePeers.size(), + activePeersCount.get(), passivePeersCount.get()); } @@ -272,18 +308,20 @@ public void triggerConnect(InetSocketAddress address) { } connectingPeersCount.decrementAndGet(); if (poolLoopExecutor.getQueue().size() >= Parameter.CONN_MAX_QUEUE_SIZE) { - logger.warn("ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE); + logger.warn( + "ConnPool task' size is greater than or equal to {}", Parameter.CONN_MAX_QUEUE_SIZE); return; } try { if (!ChannelManager.isShutdown) { - poolLoopExecutor.submit(() -> { - try { - connect(true); - } catch (Exception t) { - logger.error("Exception in poolLoopExecutor worker", t); - } - }); + poolLoopExecutor.submit( + () -> { + try { + connect(true); + } catch (Exception t) { + logger.error("Exception in poolLoopExecutor worker", t); + } + }); } } catch (Exception e) { logger.warn("Submit task failed, message:{}", e.getMessage()); @@ -318,18 +356,19 @@ public synchronized void onDisconnect(Channel peer) { @Override public void onMessage(Channel channel, byte[] data) { - //do nothing + // do nothing } public void close() { List channels = new ArrayList<>(activePeers); try { - channels.forEach(p -> { - if (!p.isDisconnect()) { - p.send(new P2pDisconnectMessage(DisconnectReason.PEER_QUITING)); - p.close(); - } - }); + channels.forEach( + p -> { + if (!p.isDisconnect()) { + p.send(new P2pDisconnectMessage(DisconnectReason.PEER_QUITING)); + p.close(); + } + }); poolLoopExecutor.shutdownNow(); disconnectExecutor.shutdownNow(); } catch (Exception e) { diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java b/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java index c49247e59cf..bd14c2cca96 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/upgrade/UpgradeController.java @@ -6,7 +6,6 @@ import org.tron.p2p.exception.P2pException; import org.tron.p2p.exception.P2pException.TypeEnum; import org.tron.p2p.protos.Connect.CompressMessage; - import org.tron.p2p.utils.ProtoUtil; public class UpgradeController { @@ -18,7 +17,8 @@ public static byte[] codeSendData(int version, byte[] data) throws IOException { return ProtoUtil.compressMessage(data).toByteArray(); } - public static byte[] decodeReceiveData(int version, byte[] data) throws P2pException, IOException { + public static byte[] decodeReceiveData(int version, byte[] data) + throws P2pException, IOException { if (!supportCompress(version)) { return data; } @@ -34,5 +34,4 @@ public static byte[] decodeReceiveData(int version, byte[] data) throws P2pExcep private static boolean supportCompress(int version) { return Parameter.version >= 1 && version >= 1; } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java b/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java index 8109b18690a..a3667a8e67d 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/MessageType.java @@ -4,7 +4,6 @@ import java.util.Map; public enum MessageType { - KEEP_ALIVE_PING((byte) 0xff), KEEP_ALIVE_PONG((byte) 0xfe), @@ -34,6 +33,7 @@ public byte getType() { map.put(value.type, value); } } + public static MessageType fromByte(byte type) { MessageType typeEnum = map.get(type); return typeEnum == null ? UNKNOWN : typeEnum; diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java index 28460676dd4..384655b9fa1 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/base/P2pDisconnectMessage.java @@ -5,7 +5,6 @@ import org.tron.p2p.protos.Connect; import org.tron.p2p.protos.Connect.DisconnectReason; - public class P2pDisconnectMessage extends Message { private Connect.P2pDisconnectMessage p2pDisconnectMessage; @@ -17,8 +16,8 @@ public P2pDisconnectMessage(byte[] data) throws Exception { public P2pDisconnectMessage(DisconnectReason disconnectReason) { super(MessageType.DISCONNECT, null); - this.p2pDisconnectMessage = Connect.P2pDisconnectMessage.newBuilder() - .setReason(disconnectReason).build(); + this.p2pDisconnectMessage = + Connect.P2pDisconnectMessage.newBuilder().setReason(disconnectReason).build(); this.data = p2pDisconnectMessage.toByteArray(); } @@ -33,7 +32,10 @@ public boolean valid() { @Override public String toString() { - return new StringBuilder().append(super.toString()).append("reason: ") - .append(getReason()).toString(); + return new StringBuilder() + .append(super.toString()) + .append("reason: ") + .append(getReason()) + .toString(); } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java index 3cadb4620dc..84c1c4385a0 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/detect/StatusMessage.java @@ -20,12 +20,14 @@ public StatusMessage(byte[] data) throws Exception { public StatusMessage() { super(MessageType.STATUS, null); Discover.Endpoint endpoint = Parameter.getHomeNode(); - this.statusMessage = Connect.StatusMessage.newBuilder() - .setFrom(endpoint) - .setMaxConnections(Parameter.p2pConfig.getMaxConnections()) - .setCurrentConnections(ChannelManager.getChannels().size()) - .setNetworkId(Parameter.p2pConfig.getNetworkId()) - .setTimestamp(System.currentTimeMillis()).build(); + this.statusMessage = + Connect.StatusMessage.newBuilder() + .setFrom(endpoint) + .setMaxConnections(Parameter.p2pConfig.getMaxConnections()) + .setCurrentConnections(ChannelManager.getChannels().size()) + .setNetworkId(Parameter.p2pConfig.getNetworkId()) + .setTimestamp(System.currentTimeMillis()) + .build(); this.data = statusMessage.toByteArray(); } diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java index a3727d2d8c1..6221be79365 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/handshake/HelloMessage.java @@ -22,12 +22,14 @@ public HelloMessage(byte[] data) throws Exception { public HelloMessage(DisconnectCode code, long time) { super(MessageType.HANDSHAKE_HELLO, null); Discover.Endpoint endpoint = Parameter.getHomeNode(); - this.helloMessage = Connect.HelloMessage.newBuilder() - .setFrom(endpoint) - .setNetworkId(Parameter.p2pConfig.getNetworkId()) - .setCode(code.getValue()) - .setVersion(Parameter.version) - .setTimestamp(time).build(); + this.helloMessage = + Connect.HelloMessage.newBuilder() + .setFrom(endpoint) + .setNetworkId(Parameter.p2pConfig.getNetworkId()) + .setCode(code.getValue()) + .setVersion(Parameter.version) + .setTimestamp(time) + .build(); this.data = helloMessage.toByteArray(); } @@ -73,5 +75,4 @@ public String format() { } return sb.toString(); } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java index 8191b5e72f1..7e269e29e5f 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PingMessage.java @@ -16,8 +16,8 @@ public PingMessage(byte[] data) throws Exception { public PingMessage() { super(MessageType.KEEP_ALIVE_PING, null); - this.keepAliveMessage = Connect.KeepAliveMessage.newBuilder() - .setTimestamp(System.currentTimeMillis()).build(); + this.keepAliveMessage = + Connect.KeepAliveMessage.newBuilder().setTimestamp(System.currentTimeMillis()).build(); this.data = this.keepAliveMessage.toByteArray(); } @@ -28,6 +28,6 @@ public long getTimeStamp() { @Override public boolean valid() { return getTimeStamp() > 0 - && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; + && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java index b3689bea93b..7478f191546 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java +++ b/p2p/src/main/java/org/tron/p2p/connection/message/keepalive/PongMessage.java @@ -16,8 +16,8 @@ public PongMessage(byte[] data) throws Exception { public PongMessage() { super(MessageType.KEEP_ALIVE_PONG, null); - this.keepAliveMessage = Connect.KeepAliveMessage.newBuilder() - .setTimestamp(System.currentTimeMillis()).build(); + this.keepAliveMessage = + Connect.KeepAliveMessage.newBuilder().setTimestamp(System.currentTimeMillis()).build(); this.data = this.keepAliveMessage.toByteArray(); } @@ -28,6 +28,6 @@ public long getTimeStamp() { @Override public boolean valid() { return getTimeStamp() > 0 - && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; + && getTimeStamp() <= System.currentTimeMillis() + Parameter.NETWORK_TIME_DIFF; } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java index 797197f0fad..42c21f42d99 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/MessageHandler.java @@ -24,8 +24,7 @@ public MessageHandler(Channel channel) { } @Override - public void handlerAdded(ChannelHandlerContext ctx) { - } + public void handlerAdded(ChannelHandlerContext ctx) {} @Override public void channelActive(ChannelHandlerContext ctx) { @@ -76,7 +75,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List ou } channel.processException(e); } catch (Throwable t) { - logger.error("Decode message from {} failed, message:{}", channel.getInetSocketAddress(), + logger.error( + "Decode message from {} failed, message:{}", + channel.getInetSocketAddress(), ByteArray.toHexString(data)); throw t; } @@ -86,5 +87,4 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List ou public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { channel.processException(cause); } - -} \ No newline at end of file +} diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java index 247ae4f0a62..6cfef5f25e5 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pChannelInitializer.java @@ -5,7 +5,6 @@ import io.netty.channel.ChannelOption; import io.netty.channel.FixedRecvByteBufAllocator; import io.netty.channel.socket.nio.NioSocketChannel; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.tron.p2p.connection.Channel; import org.tron.p2p.connection.ChannelManager; @@ -15,9 +14,11 @@ public class P2pChannelInitializer extends ChannelInitializer private final String remoteId; - private boolean peerDiscoveryMode = false; //only be true when channel is activated by detect service + private boolean peerDiscoveryMode = + false; // only be true when channel is activated by detect service private boolean trigger = true; + public P2pChannelInitializer(String remoteId, boolean peerDiscoveryMode, boolean trigger) { this.remoteId = remoteId; this.peerDiscoveryMode = peerDiscoveryMode; @@ -36,25 +37,28 @@ public void initChannel(NioSocketChannel ch) { ch.config().setOption(ChannelOption.SO_BACKLOG, 1024); // be aware of channel closing - ch.closeFuture().addListener((ChannelFutureListener) future -> { - channel.setDisconnect(true); - if (channel.isDiscoveryMode()) { - ChannelManager.getNodeDetectService().notifyDisconnect(channel); - } else { - try { - logger.info("Close channel:{}", channel.getInetSocketAddress()); - ChannelManager.notifyDisconnect(channel); - } finally { - if (channel.getInetSocketAddress() != null && channel.isActive() && trigger) { - ChannelManager.triggerConnect(channel.getInetSocketAddress()); - } - } - } - }); + ch.closeFuture() + .addListener( + (ChannelFutureListener) future -> { + channel.setDisconnect(true); + if (channel.isDiscoveryMode()) { + ChannelManager.getNodeDetectService().notifyDisconnect(channel); + } else { + try { + logger.info("Close channel:{}", channel.getInetSocketAddress()); + ChannelManager.notifyDisconnect(channel); + } finally { + if (channel.getInetSocketAddress() != null + && channel.isActive() + && trigger) { + ChannelManager.triggerConnect(channel.getInetSocketAddress()); + } + } + } + }); } catch (Exception e) { logger.error("Unexpected initChannel error", e); } } - } diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java index 4493551da96..cf2872a7322 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/P2pProtobufVarint32FrameDecoder.java @@ -75,8 +75,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { int preIndex = in.readerIndex(); int length = readRawVarint32(in); if (length >= Parameter.MAX_MESSAGE_LENGTH) { - logger.warn("Receive a big msg or not encoded msg, host : {}, msg length is : {}", - ctx.channel().remoteAddress(), length); + logger.warn( + "Receive a big msg or not encoded msg, host : {}, msg length is : {}", + ctx.channel().remoteAddress(), + length); in.clear(); channel.send(new P2pDisconnectMessage(DisconnectReason.BAD_MESSAGE)); channel.close(); diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java index 6a49e89d200..55659b31746 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java @@ -22,8 +22,9 @@ public class PeerClient { private EventLoopGroup workerGroup; public void init() { - workerGroup = new NioEventLoopGroup(0, - BasicThreadFactory.builder().namingPattern("peerClient-%d").build()); + workerGroup = + new NioEventLoopGroup( + 0, BasicThreadFactory.builder().namingPattern("peerClient-%d").build()); } public void close() { @@ -43,11 +44,13 @@ public void connect(String host, int port, String remoteId) { } public ChannelFuture connect(Node node, ChannelFutureListener future) { - ChannelFuture channelFuture = connectAsync( - node.getPreferInetSocketAddress().getAddress().getHostAddress(), - node.getPort(), - node.getId() == null ? Hex.toHexString(NetUtil.getNodeId()) : node.getHexId(), false, - false); + ChannelFuture channelFuture = + connectAsync( + node.getPreferInetSocketAddress().getAddress().getHostAddress(), + node.getPort(), + node.getId() == null ? Hex.toHexString(NetUtil.getNodeId()) : node.getHexId(), + false, + false); if (ChannelManager.isShutdown) { return null; } @@ -59,33 +62,38 @@ public ChannelFuture connect(Node node, ChannelFutureListener future) { public ChannelFuture connectAsync(Node node, boolean discoveryMode) { ChannelFuture channelFuture = - connectAsync(node.getPreferInetSocketAddress().getAddress().getHostAddress(), + connectAsync( + node.getPreferInetSocketAddress().getAddress().getHostAddress(), node.getPort(), node.getId() == null ? Hex.toHexString(NetUtil.getNodeId()) : node.getHexId(), - discoveryMode, true); + discoveryMode, + true); if (ChannelManager.isShutdown) { return null; } if (channelFuture != null) { - channelFuture.addListener((ChannelFutureListener) future -> { - if (!future.isSuccess()) { - logger.warn("Connect to peer {} fail, cause:{}", node.getPreferInetSocketAddress(), - future.cause().getMessage()); - future.channel().close(); - if (!discoveryMode) { - ChannelManager.triggerConnect(node.getPreferInetSocketAddress()); - } - } - }); + channelFuture.addListener( + (ChannelFutureListener) future -> { + if (!future.isSuccess()) { + logger.warn( + "Connect to peer {} fail, cause:{}", + node.getPreferInetSocketAddress(), + future.cause().getMessage()); + future.channel().close(); + if (!discoveryMode) { + ChannelManager.triggerConnect(node.getPreferInetSocketAddress()); + } + } + }); } return channelFuture; } - private ChannelFuture connectAsync(String host, int port, String remoteId, - boolean discoveryMode, boolean trigger) { + private ChannelFuture connectAsync( + String host, int port, String remoteId, boolean discoveryMode, boolean trigger) { - P2pChannelInitializer p2pChannelInitializer = new P2pChannelInitializer(remoteId, - discoveryMode, trigger); + P2pChannelInitializer p2pChannelInitializer = + new P2pChannelInitializer(remoteId, discoveryMode, trigger); Bootstrap b = new Bootstrap(); b.group(workerGroup); diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java index 1fdb231f96f..6b58632e4e6 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java @@ -1,6 +1,5 @@ package org.tron.p2p.connection.socket; - import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; @@ -38,11 +37,13 @@ public void close() { } public void start(int port) { - EventLoopGroup bossGroup = new NioEventLoopGroup(1, - BasicThreadFactory.builder().namingPattern("peerBoss").build()); - //if threads = 0, it is number of core * 2 - EventLoopGroup workerGroup = new NioEventLoopGroup(Parameter.TCP_NETTY_WORK_THREAD_NUM, - BasicThreadFactory.builder().namingPattern("peerWorker-%d").build()); + EventLoopGroup bossGroup = + new NioEventLoopGroup(1, BasicThreadFactory.builder().namingPattern("peerBoss").build()); + // if threads = 0, it is number of core * 2 + EventLoopGroup workerGroup = + new NioEventLoopGroup( + Parameter.TCP_NETTY_WORK_THREAD_NUM, + BasicThreadFactory.builder().namingPattern("peerWorker-%d").build()); P2pChannelInitializer p2pChannelInitializer = new P2pChannelInitializer("", false, true); try { ServerBootstrap b = new ServerBootstrap(); @@ -76,5 +77,4 @@ public void start(int port) { listening = false; } } - } diff --git a/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java b/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java index fee40123c83..3b381da2ac5 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java +++ b/p2p/src/main/java/org/tron/p2p/discover/DiscoverService.java @@ -1,10 +1,9 @@ package org.tron.p2p.discover; +import java.util.List; import org.tron.p2p.discover.socket.EventHandler; import org.tron.p2p.discover.socket.UdpEvent; -import java.util.List; - public interface DiscoverService extends EventHandler { void init(); @@ -22,5 +21,4 @@ public interface DiscoverService extends EventHandler { void channelActivated(); void handleEvent(UdpEvent event); - } diff --git a/p2p/src/main/java/org/tron/p2p/discover/Node.java b/p2p/src/main/java/org/tron/p2p/discover/Node.java index af7f1eee772..4734801b7cb 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/Node.java +++ b/p2p/src/main/java/org/tron/p2p/discover/Node.java @@ -16,28 +16,19 @@ public class Node implements Serializable, Cloneable { private static final long serialVersionUID = -4267600517925770636L; - @Setter - @Getter - private byte[] id; + @Setter @Getter private byte[] id; - @Getter - protected String hostV4; + @Getter protected String hostV4; - @Getter - protected String hostV6; + @Getter protected String hostV6; - @Setter - @Getter - protected int port; + @Setter @Getter protected int port; - @Setter - private int bindPort; + @Setter private int bindPort; - @Setter - private int p2pVersion; + @Setter private int p2pVersion; - @Getter - private long updateTime; + @Getter private long updateTime; public Node(InetSocketAddress address) { this.id = NetUtil.getNodeId(); @@ -86,7 +77,7 @@ public void updateHostV6(String hostV6) { } } - //use standard ipv6 format + // use standard ipv6 format private void formatHostV6() { if (StringUtils.isNotEmpty(this.hostV6)) { this.hostV6 = new InetSocketAddress(hostV6, port).getAddress().getHostAddress(); @@ -100,8 +91,8 @@ public boolean isConnectible(int argsP2PVersion) { public InetSocketAddress getPreferInetSocketAddress() { if (StringUtils.isNotEmpty(hostV4) && StringUtils.isNotEmpty(Parameter.p2pConfig.getIp())) { return getInetSocketAddressV4(); - } else if (StringUtils.isNotEmpty(hostV6) && StringUtils.isNotEmpty( - Parameter.p2pConfig.getIpv6())) { + } else if (StringUtils.isNotEmpty(hostV6) + && StringUtils.isNotEmpty(Parameter.p2pConfig.getIpv6())) { return getInetSocketAddressV6(); } else { return null; @@ -133,12 +124,30 @@ public void touch() { @Override public String toString() { - return "Node{" + " hostV4='" + hostV4 + '\'' + ", hostV6='" + hostV6 + '\'' + ", port=" + port - + ", id=\'" + (id == null ? "null" : Hex.toHexString(id)) + "\'}"; + return "Node{" + + " hostV4='" + + hostV4 + + '\'' + + ", hostV6='" + + hostV6 + + '\'' + + ", port=" + + port + + ", id=\'" + + (id == null ? "null" : Hex.toHexString(id)) + + "\'}"; } public String format() { - return "Node{" + " hostV4='" + hostV4 + '\'' + ", hostV6='" + hostV6 + '\'' + ", port=" + port + return "Node{" + + " hostV4='" + + hostV4 + + '\'' + + ", hostV6='" + + hostV6 + + '\'' + + ", port=" + + port + '}'; } @@ -181,6 +190,7 @@ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ignored) { + // expected } return null; } diff --git a/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java b/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java index e995ff6bb8e..e61ece0041f 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java +++ b/p2p/src/main/java/org/tron/p2p/discover/NodeManager.java @@ -43,5 +43,4 @@ public static List getTableNodes() { public static List getAllNodes() { return discoverService.getAllNodes(); } - } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/Message.java b/p2p/src/main/java/org/tron/p2p/discover/message/Message.java index 592b7b133be..0a18168f734 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/Message.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/Message.java @@ -65,5 +65,4 @@ public String toString() { public boolean equals(Object obj) { return super.equals(obj); } - } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java b/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java index 29dd0ca9a0e..b2d91106530 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/MessageType.java @@ -4,7 +4,6 @@ import java.util.Map; public enum MessageType { - KAD_PING((byte) 0x01), KAD_PONG((byte) 0x02), @@ -32,9 +31,9 @@ public byte getType() { map.put(value.type, value); } } + public static MessageType fromByte(byte type) { MessageType typeEnum = map.get(type); return typeEnum == null ? UNKNOWN : typeEnum; } - } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java index d3f812ded0f..fe1a9f53d1b 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/FindNodeMessage.java @@ -20,11 +20,12 @@ public FindNodeMessage(byte[] data) throws Exception { public FindNodeMessage(Node from, byte[] targetId) { super(MessageType.KAD_FIND_NODE, null); Endpoint fromEndpoint = getEndpointFromNode(from); - this.findNeighbours = Discover.FindNeighbours.newBuilder() - .setFrom(fromEndpoint) - .setTargetId(ByteString.copyFrom(targetId)) - .setTimestamp(System.currentTimeMillis()) - .build(); + this.findNeighbours = + Discover.FindNeighbours.newBuilder() + .setFrom(fromEndpoint) + .setTargetId(ByteString.copyFrom(targetId)) + .setTimestamp(System.currentTimeMillis()) + .build(); this.data = this.findNeighbours.toByteArray(); } @@ -49,7 +50,6 @@ public String toString() { @Override public boolean valid() { - return NetUtil.validNode(getFrom()) - && getTargetId().length == Constant.NODE_ID_LEN; + return NetUtil.validNode(getFrom()) && getTargetId().length == Constant.NODE_ID_LEN; } } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java index d4506db5b78..3b09a5d98e4 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/KadMessage.java @@ -19,8 +19,7 @@ protected KadMessage(MessageType type, byte[] data) { public abstract long getTimestamp(); public static Endpoint getEndpointFromNode(Node node) { - Endpoint.Builder builder = Endpoint.newBuilder() - .setPort(node.getPort()); + Endpoint.Builder builder = Endpoint.newBuilder().setPort(node.getPort()); if (node.getId() != null) { builder.setNodeId(ByteString.copyFrom(node.getId())); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java index 37fd4d67271..50afdd9c54f 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/NeighborsMessage.java @@ -22,13 +22,13 @@ public NeighborsMessage(byte[] data) throws Exception { public NeighborsMessage(Node from, List neighbours, long sequence) { super(MessageType.KAD_NEIGHBORS, null); - Builder builder = Neighbours.newBuilder() - .setTimestamp(sequence); + Builder builder = Neighbours.newBuilder().setTimestamp(sequence); - neighbours.forEach(neighbour -> { - Endpoint endpoint = getEndpointFromNode(neighbour); - builder.addNeighbours(endpoint); - }); + neighbours.forEach( + neighbour -> { + Endpoint endpoint = getEndpointFromNode(neighbour); + builder.addNeighbours(endpoint); + }); Endpoint fromEndpoint = getEndpointFromNode(from); diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java index e1cca85aa79..9e843d73b3c 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PingMessage.java @@ -20,12 +20,13 @@ public PingMessage(Node from, Node to) { super(MessageType.KAD_PING, null); Endpoint fromEndpoint = getEndpointFromNode(from); Endpoint toEndpoint = getEndpointFromNode(to); - this.pingMessage = Discover.PingMessage.newBuilder() - .setVersion(Parameter.p2pConfig.getNetworkId()) - .setFrom(fromEndpoint) - .setTo(toEndpoint) - .setTimestamp(System.currentTimeMillis()) - .build(); + this.pingMessage = + Discover.PingMessage.newBuilder() + .setVersion(Parameter.p2pConfig.getNetworkId()) + .setFrom(fromEndpoint) + .setTo(toEndpoint) + .setTimestamp(System.currentTimeMillis()) + .build(); this.data = this.pingMessage.toByteArray(); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java index 8ae80fbb0f6..06ca4923f0f 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java +++ b/p2p/src/main/java/org/tron/p2p/discover/message/kad/PongMessage.java @@ -19,11 +19,12 @@ public PongMessage(byte[] data) throws Exception { public PongMessage(Node from) { super(MessageType.KAD_PONG, null); Endpoint toEndpoint = getEndpointFromNode(from); - this.pongMessage = Discover.PongMessage.newBuilder() - .setFrom(toEndpoint) - .setEcho(Parameter.p2pConfig.getNetworkId()) - .setTimestamp(System.currentTimeMillis()) - .build(); + this.pongMessage = + Discover.PongMessage.newBuilder() + .setFrom(toEndpoint) + .setEcho(Parameter.p2pConfig.getNetworkId()) + .setTimestamp(System.currentTimeMillis()) + .build(); this.data = this.pongMessage.toByteArray(); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java index 823e4d4dd36..021788d4110 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java @@ -14,8 +14,9 @@ @Slf4j(topic = "net") public class DiscoverTask { - private ScheduledExecutorService discoverer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("discoverTask").build()); + private ScheduledExecutorService discoverer = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("discoverTask").build()); private KadService kadService; @@ -27,20 +28,24 @@ public DiscoverTask(KadService kadService) { } public void init() { - discoverer.scheduleWithFixedDelay(() -> { - try { - loopNum++; - if (loopNum % KademliaOptions.MAX_LOOP_NUM == 0) { - loopNum = 0; - nodeId = kadService.getPublicHomeNode().getId(); - } else { - nodeId = NetUtil.getNodeId(); - } - discover(nodeId, 0, new ArrayList<>()); - } catch (Exception e) { - logger.error("DiscoverTask fails to be executed", e); - } - }, 1, KademliaOptions.DISCOVER_CYCLE, TimeUnit.MILLISECONDS); + discoverer.scheduleWithFixedDelay( + () -> { + try { + loopNum++; + if (loopNum % KademliaOptions.MAX_LOOP_NUM == 0) { + loopNum = 0; + nodeId = kadService.getPublicHomeNode().getId(); + } else { + nodeId = NetUtil.getNodeId(); + } + discover(nodeId, 0, new ArrayList<>()); + } catch (Exception e) { + logger.error("DiscoverTask fails to be executed", e); + } + }, + 1, + KademliaOptions.DISCOVER_CYCLE, + TimeUnit.MILLISECONDS); logger.debug("DiscoverTask started"); } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java index cfdf30d1974..161dd6d13ea 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java @@ -31,9 +31,7 @@ public class KadService implements DiscoverService { private static final int MAX_NODES = 2000; private static final int NODES_TRIM_THRESHOLD = 3000; - @Getter - @Setter - private static long pingTimeout = 15_000; + @Getter @Setter private static long pingTimeout = 15_000; private final List bootNodes = new ArrayList<>(); @@ -56,10 +54,15 @@ public void init() { for (InetSocketAddress address : Parameter.p2pConfig.getActiveNodes()) { bootNodes.add(new Node(address)); } - this.pongTimer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("pongTimer").build()); - this.homeNode = new Node(Parameter.p2pConfig.getNodeID(), Parameter.p2pConfig.getIp(), - Parameter.p2pConfig.getIpv6(), Parameter.p2pConfig.getPort()); + this.pongTimer = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("pongTimer").build()); + this.homeNode = + new Node( + Parameter.p2pConfig.getNodeID(), + Parameter.p2pConfig.getIp(), + Parameter.p2pConfig.getIpv6(), + Parameter.p2pConfig.getPort()); this.table = new NodeTable(homeNode); if (Parameter.p2pConfig.isDiscoverEnable()) { @@ -126,11 +129,21 @@ public void handleEvent(UdpEvent udpEvent) { Node n; if (sender.getAddress() instanceof Inet4Address) { - n = new Node(m.getFrom().getId(), sender.getHostString(), m.getFrom().getHostV6(), - sender.getPort(), m.getFrom().getPort()); + n = + new Node( + m.getFrom().getId(), + sender.getHostString(), + m.getFrom().getHostV6(), + sender.getPort(), + m.getFrom().getPort()); } else { - n = new Node(m.getFrom().getId(), m.getFrom().getHostV4(), sender.getHostString(), - sender.getPort(), m.getFrom().getPort()); + n = + new Node( + m.getFrom().getId(), + m.getFrom().getHostV4(), + sender.getHostString(), + sender.getPort(), + m.getFrom().getPort()); } NodeHandler nodeHandler = getNodeHandler(n); @@ -199,11 +212,14 @@ public ScheduledExecutorService getPongTimer() { private void trimTable() { if (nodeHandlerMap.size() > NODES_TRIM_THRESHOLD) { - nodeHandlerMap.values().forEach(handler -> { - if (!handler.getNode().isConnectible(Parameter.p2pConfig.getNetworkId())) { - nodeHandlerMap.values().remove(handler); - } - }); + nodeHandlerMap + .values() + .forEach( + handler -> { + if (!handler.getNode().isConnectible(Parameter.p2pConfig.getNetworkId())) { + nodeHandlerMap.values().remove(handler); + } + }); } if (nodeHandlerMap.size() > NODES_TRIM_THRESHOLD) { List sorted = new ArrayList<>(nodeHandlerMap.values()); diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java index a6f569eae49..6ec9866d20c 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/NodeHandler.java @@ -164,16 +164,21 @@ public void sendPing() { if (kadService.getPongTimer().isShutdown()) { return; } - kadService.getPongTimer().schedule(() -> { - try { - if (waitForPong) { - waitForPong = false; - handleTimedOut(); - } - } catch (Exception e) { - logger.error("Unhandled exception in pong timer schedule", e); - } - }, KadService.getPingTimeout(), TimeUnit.MILLISECONDS); + kadService + .getPongTimer() + .schedule( + () -> { + try { + if (waitForPong) { + waitForPong = false; + handleTimedOut(); + } + } catch (Exception e) { + logger.error("Unhandled exception in pong timer schedule", e); + } + }, + KadService.getPingTimeout(), + TimeUnit.MILLISECONDS); } public void sendPong() { @@ -198,8 +203,13 @@ private void sendMessage(Message msg) { @Override public String toString() { - return "NodeHandler[state: " + state + ", node: " + node.getHostKey() + ":" + node.getPort() - + "]"; + return "NodeHandler[state: " + + state + + ", node: " + + node.getHostKey() + + ":" + + node.getPort() + + "]"; } public enum State { @@ -211,8 +221,7 @@ public enum State { */ DISCOVERED, /** - * The node didn't send the Pong message back withing acceptable timeout This is the final - * state + * The node didn't send the Pong message back withing acceptable timeout This is the final state */ DEAD, /** diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java index b1362438af0..af0f246dea2 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/KademliaOptions.java @@ -7,6 +7,6 @@ public class KademliaOptions { public static final int MAX_STEPS = 8; public static final int MAX_LOOP_NUM = 5; - public static final long DISCOVER_CYCLE = 7200; //discovery cycle interval in millis - public static final long WAIT_TIME = 100; //wait time in millis + public static final long DISCOVER_CYCLE = 7200; // discovery cycle interval in millis + public static final long WAIT_TIME = 100; // wait time in millis } diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java index 274f5ca1800..28e66f84943 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java @@ -8,7 +8,7 @@ import org.tron.p2p.discover.Node; public class NodeTable { - private final Node node; // our node + private final Node node; // our node private transient NodeBucket[] buckets; private transient Map nodes; diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java index 4840d42a7e2..8dc5140af12 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/DiscoverServer.java @@ -26,13 +26,16 @@ public class DiscoverServer { public void init(EventHandler eventHandler) { this.eventHandler = eventHandler; - new Thread(() -> { - try { - start(); - } catch (Exception e) { - logger.error("Discovery server start failed", e); - } - }, "DiscoverServer").start(); + new Thread( + () -> { + try { + start(); + } catch (Exception e) { + logger.error("Discovery server start failed", e); + } + }, + "DiscoverServer") + .start(); } public void close() { @@ -48,26 +51,28 @@ public void close() { } private void start() throws Exception { - NioEventLoopGroup group = new NioEventLoopGroup(Parameter.UDP_NETTY_WORK_THREAD_NUM, - new BasicThreadFactory.Builder().namingPattern("discoverServer").build()); + NioEventLoopGroup group = + new NioEventLoopGroup( + Parameter.UDP_NETTY_WORK_THREAD_NUM, + new BasicThreadFactory.Builder().namingPattern("discoverServer").build()); try { while (!shutdown) { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioDatagramChannel.class) - .handler(new ChannelInitializer() { - @Override - public void initChannel(NioDatagramChannel ch) - throws Exception { - ch.pipeline().addLast(TrafficStats.udp); - ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); - ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); - ch.pipeline().addLast(new P2pPacketDecoder()); - MessageHandler messageHandler = new MessageHandler(ch, eventHandler); - eventHandler.setMessageSender(messageHandler); - ch.pipeline().addLast(messageHandler); - } - }); + .handler( + new ChannelInitializer() { + @Override + public void initChannel(NioDatagramChannel ch) throws Exception { + ch.pipeline().addLast(TrafficStats.udp); + ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); + ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); + ch.pipeline().addLast(new P2pPacketDecoder()); + MessageHandler messageHandler = new MessageHandler(ch, eventHandler); + eventHandler.setMessageSender(messageHandler); + ch.pipeline().addLast(messageHandler); + } + }); channel = b.bind(port).sync().channel(); diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java index 0f6fd0d3899..173e7339e74 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/MessageHandler.java @@ -1,13 +1,13 @@ package org.tron.p2p.discover.socket; -import java.net.InetSocketAddress; -import java.util.function.Consumer; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.nio.NioDatagramChannel; +import java.net.InetSocketAddress; +import java.util.function.Consumer; import lombok.extern.slf4j.Slf4j; @Slf4j(topic = "net") @@ -30,7 +30,8 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelRead0(ChannelHandlerContext ctx, UdpEvent udpEvent) { - logger.debug("Rcv udp msg type {}, len {} from {} ", + logger.debug( + "Rcv udp msg type {}, len {} from {} ", udpEvent.getMessage().getType(), udpEvent.getMessage().getSendData().length, udpEvent.getAddress()); @@ -39,7 +40,8 @@ public void channelRead0(ChannelHandlerContext ctx, UdpEvent udpEvent) { @Override public void accept(UdpEvent udpEvent) { - logger.debug("Send udp msg type {}, len {} to {} ", + logger.debug( + "Send udp msg type {}, len {} to {} ", udpEvent.getMessage().getType(), udpEvent.getMessage().getSendData().length, udpEvent.getAddress()); @@ -60,8 +62,10 @@ public void channelReadComplete(ChannelHandlerContext ctx) { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - logger.warn("Exception caught in udp message handler, {} {}", - ctx.channel().remoteAddress(), cause.getMessage()); + logger.warn( + "Exception caught in udp message handler, {} {}", + ctx.channel().remoteAddress(), + cause.getMessage()); ctx.close(); } } diff --git a/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java index 8099fe83fe0..720bc8b5e14 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java +++ b/p2p/src/main/java/org/tron/p2p/discover/socket/P2pPacketDecoder.java @@ -1,11 +1,11 @@ package org.tron.p2p.discover.socket; import com.google.protobuf.InvalidProtocolBufferException; -import java.util.List; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.socket.DatagramPacket; import io.netty.handler.codec.MessageToMessageDecoder; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.tron.p2p.discover.message.Message; import org.tron.p2p.exception.P2pException; @@ -32,20 +32,36 @@ public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List getDnsNodes() { Set nodes = new HashSet<>(); for (Map.Entry entry : syncClient.getTrees().entrySet()) { Tree tree = entry.getValue(); - int v4Size = 0, v6Size = 0; + int v4Size = 0; + int v6Size = 0; List dnsNodes = tree.getDnsNodes(); List ipv6Nodes = new ArrayList<>(); for (DnsNode dnsNode : dnsNodes) { - //logger.debug("DnsNode:{}", dnsNode); + // logger.debug("DnsNode:{}", dnsNode); if (dnsNode.getInetSocketAddressV4() != null) { v4Size += 1; } @@ -61,13 +61,21 @@ public static List getDnsNodes() { ipv6Nodes.add(dnsNode); } } - List connectAbleNodes = dnsNodes.stream() - .filter(node -> node.getPreferInetSocketAddress() != null) - .filter(node -> !localIpSet.contains( - node.getPreferInetSocketAddress().getAddress().getHostAddress())) - .collect(Collectors.toList()); - logger.debug("Tree {} node size:{}, v4 node size:{}, v6 node size:{}, connectable size:{}", - entry.getKey(), dnsNodes.size(), v4Size, v6Size, connectAbleNodes.size()); + List connectAbleNodes = + dnsNodes.stream() + .filter(node -> node.getPreferInetSocketAddress() != null) + .filter( + node -> + !localIpSet.contains( + node.getPreferInetSocketAddress().getAddress().getHostAddress())) + .collect(Collectors.toList()); + logger.debug( + "Tree {} node size:{}, v4 node size:{}, v6 node size:{}, connectable size:{}", + entry.getKey(), + dnsNodes.size(), + v4Size, + v6Size, + connectAbleNodes.size()); nodes.addAll(connectAbleNodes); } return new ArrayList<>(nodes); diff --git a/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java b/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java index 1c11128c50f..7d44402b039 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java +++ b/p2p/src/main/java/org/tron/p2p/dns/DnsNode.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import static org.tron.p2p.discover.message.kad.KadMessage.getEndpointFromNode; import com.google.protobuf.InvalidProtocolBufferException; @@ -38,10 +37,11 @@ public DnsNode(byte[] id, String hostV4, String hostV6, int port) throws Unknown public static String compress(List nodes) { Builder builder = Discover.EndPoints.newBuilder(); - nodes.forEach(node -> { - Endpoint endpoint = getEndpointFromNode(node); - builder.addNodes(endpoint); - }); + nodes.forEach( + node -> { + Endpoint endpoint = getEndpointFromNode(node); + builder.addNodes(endpoint); + }); return Algorithm.encode64(builder.build().toByteArray()); } @@ -52,10 +52,12 @@ public static List decompress(String base64Content) List dnsNodes = new ArrayList<>(); for (Endpoint endpoint : endPoints.getNodesList()) { - DnsNode dnsNode = new DnsNode(endpoint.getNodeId().toByteArray(), - new String(endpoint.getAddress().toByteArray()), - new String(endpoint.getAddressIpv6().toByteArray()), - endpoint.getPort()); + DnsNode dnsNode = + new DnsNode( + endpoint.getNodeId().toByteArray(), + new String(endpoint.getAddress().toByteArray()), + new String(endpoint.getAddressIpv6().toByteArray()), + endpoint.getPort()); dnsNodes.add(dnsNode); } return dnsNodes; diff --git a/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java index bbb9dbecba4..95a58dba5dd 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java +++ b/p2p/src/main/java/org/tron/p2p/dns/lookup/LookUpTxt.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.lookup; - import java.net.InetAddress; import java.net.UnknownHostException; import java.time.Duration; @@ -18,34 +17,43 @@ @Slf4j(topic = "net") public class LookUpTxt { - static String[] publicDnsV4 = new String[] { - "114.114.114.114", "114.114.115.115", //114 DNS - "223.5.5.5", "223.6.6.6", //AliDNS - //"180.76.76.76", //BaiduDNS slow - "119.29.29.29", //DNSPod DNS+ - // "182.254.116.116", //DNSPod DNS+ slow - //"1.2.4.8", "210.2.4.8", //CNNIC SDNS - "117.50.11.11", "117.50.22.22", //oneDNS - "101.226.4.6", "218.30.118.6", "123.125.81.6", "140.207.198.6", //DNS pai - "8.8.8.8", "8.8.4.4", //Google DNS - "9.9.9.9", //IBM Quad9 - //"208.67.222.222", "208.67.220.220", //OpenDNS slow - //"199.91.73.222", "178.79.131.110" //V2EX DNS - }; + static String[] publicDnsV4 = + new String[] { + "114.114.114.114", + "114.114.115.115", // 114 DNS + "223.5.5.5", + "223.6.6.6", // AliDNS + // "180.76.76.76", //BaiduDNS slow + "119.29.29.29", // DNSPod DNS+ + // "182.254.116.116", //DNSPod DNS+ slow + // "1.2.4.8", "210.2.4.8", //CNNIC SDNS + "117.50.11.11", + "117.50.22.22", // oneDNS + "101.226.4.6", + "218.30.118.6", + "123.125.81.6", + "140.207.198.6", // DNS pai + "8.8.8.8", + "8.8.4.4", // Google DNS + "9.9.9.9", // IBM Quad9 + // "208.67.222.222", "208.67.220.220", //OpenDNS slow + // "199.91.73.222", "178.79.131.110" //V2EX DNS + }; - static String[] publicDnsV6 = new String[] { - "2606:4700:4700::1111", "2606:4700:4700::1001", //Cloudflare - "2400:3200::1", "2400:3200:baba::1", //AliDNS - //"2400:da00::6666", //BaiduDNS - "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff", //AdGuard - "2620:74:1b::1:1", "2620:74:1c::2:2", //Verisign - //"2a05:dfc7:5::53", "2a05:dfc7:5::5353", //OpenNIC - "2a02:6b8::feed:0ff", "2a02:6b8:0:1::feed:0ff", //Yandex - "2001:4860:4860::8888", "2001:4860:4860::8844", //Google DNS - "2620:fe::fe", "2620:fe::9", //IBM Quad9 - //"2620:119:35::35", "2620:119:53::53", //OpenDNS - "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff" //AdGuard - }; + static String[] publicDnsV6 = + new String[] { + "2606:4700:4700::1111", "2606:4700:4700::1001", // Cloudflare + "2400:3200::1", "2400:3200:baba::1", // AliDNS + // "2400:da00::6666", //BaiduDNS + "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff", // AdGuard + "2620:74:1b::1:1", "2620:74:1c::2:2", // Verisign + // "2a05:dfc7:5::53", "2a05:dfc7:5::5353", //OpenNIC + "2a02:6b8::feed:0ff", "2a02:6b8:0:1::feed:0ff", // Yandex + "2001:4860:4860::8888", "2001:4860:4860::8844", // Google DNS + "2620:fe::fe", "2620:fe::9", // IBM Quad9 + // "2620:119:35::35", "2620:119:53::53", //OpenDNS + "2a00:5a60::ad1:0ff", "2a00:5a60::ad2:0ff" // AdGuard + }; static int maxRetryTimes = 5; static Random random = new Random(); @@ -79,8 +87,11 @@ public static TXTRecord lookUpTxt(String name) throws TextParseException, Unknow long end = System.currentTimeMillis(); times += 1; if (records != null) { - logger.debug("Succeed to use dns: {}, cur cost: {}ms, total cost: {}ms", publicDns, - end - thisTime, end - start); + logger.debug( + "Succeed to use dns: {}, cur cost: {}ms, total cost: {}ms", + publicDns, + end - thisTime, + end - start); break; } else { logger.debug("Failed to use dns: {}, cur cost: {}ms", publicDns, end - thisTime); diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java index 2d258ad784c..d96560c1976 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.sync; - import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.net.UnknownHostException; @@ -32,28 +31,24 @@ @Slf4j(topic = "net") public class Client { - public static final int recheckInterval = 60 * 60; //seconds, should be smaller than rootTTL + public static final int recheckInterval = 60 * 60; // seconds, should be smaller than rootTTL public static final int cacheLimit = 2000; public static final int randomRetryTimes = 10; private Cache cache; - @Getter - private final Map trees = new ConcurrentHashMap<>(); + @Getter private final Map trees = new ConcurrentHashMap<>(); private final Map clientTrees = new HashMap<>(); - private final ScheduledExecutorService syncer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("dnsSyncer").build()); + private final ScheduledExecutorService syncer = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("dnsSyncer").build()); public Client() { - this.cache = CacheBuilder.newBuilder() - .maximumSize(cacheLimit) - .recordStats() - .build(); + this.cache = CacheBuilder.newBuilder().maximumSize(cacheLimit).recordStats().build(); } public void init() { if (!Parameter.p2pConfig.getTreeUrls().isEmpty()) { - syncer.scheduleWithFixedDelay(this::startSync, 5, recheckInterval, - TimeUnit.SECONDS); + syncer.scheduleWithFixedDelay(this::startSync, 5, recheckInterval, TimeUnit.SECONDS); } } @@ -97,28 +92,32 @@ public void syncTree(String urlScheme, ClientTree clientTree, Tree tree) throws } tree.setRootEntry(clientTree.getRoot()); - logger.info("SyncTree {} complete, LinkEntry size:{}, NodesEntry size:{}, node size:{}", - urlScheme, tree.getLinksEntry().size(), tree.getNodesEntry().size(), + logger.info( + "SyncTree {} complete, LinkEntry size:{}, NodesEntry size:{}, node size:{}", + urlScheme, + tree.getLinksEntry().size(), + tree.getNodesEntry().size(), tree.getDnsNodes().size()); } - public RootEntry resolveRoot(LinkEntry linkEntry) throws TextParseException, DnsException, - SignatureException, UnknownHostException { - //do not put root in cache + public RootEntry resolveRoot(LinkEntry linkEntry) + throws TextParseException, DnsException, SignatureException, UnknownHostException { + // do not put root in cache TXTRecord txtRecord = LookUpTxt.lookUpTxt(linkEntry.getDomain()); if (txtRecord == null) { throw new DnsException(TypeEnum.LOOK_UP_ROOT_FAILED, "domain: " + linkEntry.getDomain()); } for (String txt : txtRecord.getStrings()) { if (txt.startsWith(Entry.rootPrefix)) { - return RootEntry.parseEntry(txt, linkEntry.getUnCompressHexPublicKey(), - linkEntry.getDomain()); + return RootEntry.parseEntry( + txt, linkEntry.getUnCompressHexPublicKey(), linkEntry.getDomain()); } } throw new DnsException(TypeEnum.NO_ROOT_FOUND, "domain: " + linkEntry.getDomain()); } - // resolveEntry retrieves an entry from the cache or fetches it from the network if it isn't cached. + // resolveEntry retrieves an entry from the cache or fetches it from the network if it isn't + // cached. public Entry resolveEntry(String domain, String hash) throws DnsException, TextParseException, UnknownHostException { Entry entry = cache.getIfPresent(hash); @@ -155,15 +154,16 @@ private Entry doResolveEntry(String domain, String hash) } if (entry == null) { - throw new DnsException(TypeEnum.NO_ENTRY_FOUND, - String.format("hash:%s, domain:%s, txt:%s", hash, domain, txt)); + throw new DnsException( + TypeEnum.NO_ENTRY_FOUND, String.format("hash:%s, domain:%s, txt:%s", hash, domain, txt)); } String wantHash = Algorithm.encode32AndTruncate(entry.toString()); if (!wantHash.equals(hash)) { - throw new DnsException(TypeEnum.HASH_MISS_MATCH, - String.format("hash mismatch, want: [%s], really: [%s], content: [%s]", wantHash, hash, - entry)); + throw new DnsException( + TypeEnum.HASH_MISS_MATCH, + String.format( + "hash mismatch, want: [%s], really: [%s], content: [%s]", wantHash, hash, entry)); } return entry; } diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java index 5cb1e7a669b..0655a8611ca 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/ClientTree.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.sync; - import java.net.UnknownHostException; import java.security.SignatureException; import java.util.HashSet; @@ -24,9 +23,7 @@ public class ClientTree { // used for construct private final Client client; - @Getter - @Setter - private LinkEntry linkEntry; + @Getter @Setter private LinkEntry linkEntry; private final LinkCache linkCache; // used for check @@ -34,13 +31,11 @@ public class ClientTree { private int lastSeq = -1; // used for sync - @Getter - @Setter - private RootEntry root; + @Getter @Setter private RootEntry root; private SubtreeSync enrSync; private SubtreeSync linkSync; - //all links in this tree + // all links in this tree private Set curLinks; private String linkGCRoot; @@ -61,8 +56,7 @@ public ClientTree(Client c, LinkCache lc, LinkEntry loc) { } public boolean[] syncAll(Map entries) - throws DnsException, UnknownHostException, - SignatureException, TextParseException { + throws DnsException, UnknownHostException, SignatureException, TextParseException { boolean[] isRootUpdate = updateRoot(); linkSync.resolveAll(entries); enrSync.resolveAll(entries); @@ -97,7 +91,8 @@ public boolean canSyncRandom() { return rootUpdateDue() || !linkSync.done() || !enrSync.done() || enrSync.leaves == 0; } - // gcLinks removes outdated links from the global link cache. GC runs once when the link sync finishes. + // gcLinks removes outdated links from the global link cache. GC runs once when the link sync + // finishes. public void gcLinks() { if (!linkSync.done() || root.getLRoot().equals(linkGCRoot)) { return; @@ -146,8 +141,8 @@ private boolean[] updateRoot() return new boolean[] {false, false}; } if (rootEntry.getSeq() <= lastSeq) { - logger.info("The seq of url doesn't change, url:[{}], seq:{}", linkEntry.getRepresent(), - lastSeq); + logger.info( + "The seq of url doesn't change, url:[{}], seq:{}", linkEntry.getRepresent(), lastSeq); return new boolean[] {false, false}; } @@ -158,11 +153,13 @@ private boolean[] updateRoot() boolean updateERoot = false; if (linkSync == null || !rootEntry.getLRoot().equals(linkSync.root)) { linkSync = new SubtreeSync(client, linkEntry, rootEntry.getLRoot(), true); - curLinks = new HashSet<>();//clear all links + curLinks = new HashSet<>(); // clear all links updateLRoot = true; } else { // if lroot is not changed, wo do not to sync the link tree - logger.info("The lroot of url doesn't change, url:[{}], lroot:[{}]", linkEntry.getRepresent(), + logger.info( + "The lroot of url doesn't change, url:[{}], lroot:[{}]", + linkEntry.getRepresent(), linkSync.root); } @@ -171,7 +168,9 @@ private boolean[] updateRoot() updateERoot = true; } else { // if eroot is not changed, wo do not to sync the enr tree - logger.info("The eroot of url doesn't change, url:[{}], eroot:[{}]", linkEntry.getRepresent(), + logger.info( + "The eroot of url doesn't change, url:[{}], eroot:[{}]", + linkEntry.getRepresent(), enrSync.root); } return new boolean[] {updateLRoot, updateERoot}; diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java b/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java index 964fb0bcc99..18582e47248 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/LinkCache.java @@ -12,15 +12,11 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; - @Slf4j(topic = "net") public class LinkCache { - @Getter - Map> backrefs; - @Getter - @Setter - private boolean changed; //if data in backrefs changes, we need to rebuild trees + @Getter Map> backrefs; + @Getter @Setter private boolean changed; // if data in backrefs changes, we need to rebuild trees public LinkCache() { backrefs = new HashMap<>(); diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java index 3cb9f0180ad..03c1f97d9e5 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/RandomIterator.java @@ -1,7 +1,6 @@ package org.tron.p2p.dns.sync; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -16,14 +15,12 @@ import org.tron.p2p.dns.tree.LinkEntry; import org.tron.p2p.exception.DnsException; - @Slf4j(topic = "net") public class RandomIterator implements Iterator { private final Client client; private Map clientTrees; - @Getter - private DnsNode cur; + @Getter private DnsNode cur; private final LinkCache linkCache; private final Random random; @@ -34,7 +31,7 @@ public RandomIterator(Client client) { random = new Random(); } - //syncs random tree entries until it finds a node. + // syncs random tree entries until it finds a node. @Override public DnsNode next() { int i = 0; @@ -45,14 +42,18 @@ public DnsNode next() { logger.error("clientTree is null"); return null; } - logger.info("Choose clientTree:{} from {} ClientTree", clientTree.getLinkEntry().getRepresent(), + logger.info( + "Choose clientTree:{} from {} ClientTree", + clientTree.getLinkEntry().getRepresent(), clientTrees.size()); DnsNode dnsNode; try { dnsNode = clientTree.syncRandom(); } catch (Exception e) { - logger.warn("Error in DNS random node sync, tree:{}, cause:[{}]", - clientTree.getLinkEntry().getDomain(), e.getMessage()); + logger.warn( + "Error in DNS random node sync, tree:{}, cause:[{}]", + clientTree.getLinkEntry().getDomain(), + e.getMessage()); continue; } if (dnsNode != null && dnsNode.getPreferInetSocketAddress() != null) { @@ -73,7 +74,7 @@ public void addTree(String url) throws DnsException { linkCache.addLink("", linkEntry.getRepresent()); } - //the first random + // the first random private ClientTree pickTree() { if (clientTrees == null) { logger.info("clientTrees is null"); diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java b/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java index eda6de84b31..3b48ad523c6 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/SubtreeSync.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.sync; - import java.net.UnknownHostException; import java.util.Arrays; import java.util.LinkedList; diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java index 0cc7ab6c1d6..3064782f6a8 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Algorithm.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.SignatureException; @@ -23,9 +22,7 @@ public class Algorithm { private static final int truncateLength = 26; public static final String padding = "="; - /** - * return compress public key with hex - */ + /** return compress public key with hex */ public static String compressPubKey(BigInteger pubKey) { String pubKeyYPrefix = pubKey.testBit(0) ? "03" : "02"; String pubKeyHex = pubKey.toString(16); @@ -38,10 +35,7 @@ public static String decompressPubKey(String hexPubKey) { X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1"); ECDomainParameters CURVE = new ECDomainParameters( - CURVE_PARAMS.getCurve(), - CURVE_PARAMS.getG(), - CURVE_PARAMS.getN(), - CURVE_PARAMS.getH()); + CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH()); byte[] pubKey = ByteArray.fromHexString(hexPubKey); ECPoint ecPoint = CURVE.getCurve().decodePoint(pubKey); byte[] encoded = ecPoint.getEncoded(false); @@ -55,9 +49,7 @@ public static ECKeyPair generateKeyPair(String privateKey) { return new ECKeyPair(privKey, pubKey); } - /** - * The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1. - */ + /** The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1. */ public static byte[] sigData(String msg, String privateKey) { ECKeyPair keyPair = generateKeyPair(privateKey); Sign.SignatureData signature = Sign.signMessage(msg.getBytes(), keyPair, true); @@ -73,8 +65,9 @@ public static BigInteger recoverPublicKey(String msg, byte[] sig) throws Signatu if (recId < 27) { recId += 27; } - Sign.SignatureData signature = new SignatureData((byte) recId, ByteArray.subArray(sig, 0, 32), - ByteArray.subArray(sig, 32, 64)); + Sign.SignatureData signature = + new SignatureData( + (byte) recId, ByteArray.subArray(sig, 0, 32), ByteArray.subArray(sig, 32, 64)); return Sign.signedMessageToKey(msg.getBytes(), signature); } @@ -89,9 +82,11 @@ public static boolean verifySignature(String publicKey, String msg, byte[] sig) return pubKey.equals(pubKeyRecovered); } - //we only use fix width hash + // we only use fix width hash public static boolean isValidHash(String base32Hash) { - if (base32Hash == null || base32Hash.length() != truncateLength || base32Hash.contains("\r") + if (base32Hash == null + || base32Hash.length() != truncateLength + || base32Hash.contains("\r") || base32Hash.contains("\n")) { return false; } @@ -108,8 +103,8 @@ public static boolean isValidHash(String base32Hash) { } public static String encode64(byte[] content) { - String base64Content = new String(Base64.getUrlEncoder().encode(content), - StandardCharsets.UTF_8); + String base64Content = + new String(Base64.getUrlEncoder().encode(content), StandardCharsets.UTF_8); return StringUtils.stripEnd(base64Content, padding); } @@ -127,17 +122,13 @@ public static String encode32(byte[] content) { return StringUtils.stripEnd(base32Content, padding); } - /** - * first get the hash of string, then get first 16 letter, last encode it with base32 - */ + /** first get the hash of string, then get first 16 letter, last encode it with base32 */ public static String encode32AndTruncate(String content) { return encode32(ByteArray.subArray(Hash.sha3(content.getBytes()), 0, 16)) .substring(0, truncateLength); } - /** - * if content's length is not multiple of 8, we padding it - */ + /** if content's length is not multiple of 8, we padding it */ public static byte[] decode32(String content) { int left = content.length() % 8; StringBuilder sb = new StringBuilder(content); diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java index bc426328753..062580ad034 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/BranchEntry.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -9,8 +8,7 @@ public class BranchEntry implements Entry { private static final String splitSymbol = ","; - @Getter - private String[] children; + @Getter private String[] children; public BranchEntry(String[] children) { this.children = children; diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java index e3ea47b137e..1a23f633a97 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Entry.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - public interface Entry { String rootPrefix = "tree-root-v1:"; diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java index b1f87490efa..91f8eee91c8 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/LinkEntry.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.tron.p2p.exception.DnsException; @@ -10,12 +9,9 @@ @Slf4j(topic = "net") public class LinkEntry implements Entry { - @Getter - private final String represent; - @Getter - private final String domain; - @Getter - private final String unCompressHexPublicKey; + @Getter private final String represent; + @Getter private final String domain; + @Getter private final String unCompressHexPublicKey; public LinkEntry(String represent, String domain, String unCompressHexPublicKey) { this.represent = represent; @@ -25,7 +21,8 @@ public LinkEntry(String represent, String domain, String unCompressHexPublicKey) public static LinkEntry parseEntry(String treeRepresent) throws DnsException { if (!treeRepresent.startsWith(linkPrefix)) { - throw new DnsException(TypeEnum.INVALID_SCHEME_URL, + throw new DnsException( + TypeEnum.INVALID_SCHEME_URL, "scheme url must starts with :[" + Entry.linkPrefix + "], but get " + treeRepresent); } String[] items = treeRepresent.substring(linkPrefix.length()).split("@"); diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java index 83db1ca930c..e7790081e53 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/NodesEntry.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import com.google.protobuf.InvalidProtocolBufferException; import java.net.UnknownHostException; import java.util.List; @@ -14,8 +13,7 @@ public class NodesEntry implements Entry { private final String represent; - @Getter - private final List nodes; + @Getter private final List nodes; public NodesEntry(String represent, List nodes) { this.represent = represent; @@ -26,7 +24,7 @@ public static NodesEntry parseEntry(String e) throws DnsException { String content = e.substring(nodesPrefix.length()); List nodeList; try { - nodeList = DnsNode.decompress(content.replace("\"","")); + nodeList = DnsNode.decompress(content.replace("\"", "")); } catch (InvalidProtocolBufferException | UnknownHostException ex) { throw new DnsException(TypeEnum.INVALID_NODES, ex); } diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java index 078a0e8838b..395cada8f09 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/RootEntry.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import java.security.SignatureException; @@ -14,8 +13,7 @@ @Slf4j(topic = "net") public class RootEntry implements Entry { - @Getter - private DnsRoot dnsRoot; + @Getter private DnsRoot dnsRoot; public RootEntry(DnsRoot dnsRoot) { this.dnsRoot = dnsRoot; @@ -75,9 +73,11 @@ public static RootEntry parseEntry(String e) throws DnsException { byte[] signature = Algorithm.decode64(new String(dnsRoot1.getSignature().toByteArray())); if (signature.length != 65) { - throw new DnsException(TypeEnum.INVALID_SIGNATURE, - String.format("signature's length(%d) != 65, signature: %s", signature.length, - ByteArray.toHexString(signature))); + throw new DnsException( + TypeEnum.INVALID_SIGNATURE, + String.format( + "signature's length(%d) != 65, signature: %s", + signature.length, ByteArray.toHexString(signature))); } return new RootEntry(dnsRoot1); @@ -87,16 +87,18 @@ public static RootEntry parseEntry(String e, String publicKey, String domain) throws SignatureException, DnsException { logger.info("Domain:{}, public key:{}", domain, publicKey); RootEntry rootEntry = parseEntry(e); - boolean verify = Algorithm.verifySignature(publicKey, rootEntry.toString(), - rootEntry.getSignature()); + boolean verify = + Algorithm.verifySignature(publicKey, rootEntry.toString(), rootEntry.getSignature()); if (!verify) { - throw new DnsException(TypeEnum.INVALID_SIGNATURE, - String.format("verify signature failed! data:[%s], publicKey:%s, domain:%s", e, publicKey, - domain)); + throw new DnsException( + TypeEnum.INVALID_SIGNATURE, + String.format( + "verify signature failed! data:[%s], publicKey:%s, domain:%s", e, publicKey, domain)); } - if (!Algorithm.isValidHash(rootEntry.getERoot()) || !Algorithm.isValidHash( - rootEntry.getLRoot())) { - throw new DnsException(TypeEnum.INVALID_CHILD, + if (!Algorithm.isValidHash(rootEntry.getERoot()) + || !Algorithm.isValidHash(rootEntry.getLRoot())) { + throw new DnsException( + TypeEnum.INVALID_CHILD, "eroot:" + rootEntry.getERoot() + " lroot:" + rootEntry.getLRoot()); } logger.info("Get dnsRoot:[{}]", rootEntry.dnsRoot.toString()); diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java index c53e24e625b..ce838796c40 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.tree; - import com.google.protobuf.InvalidProtocolBufferException; import java.math.BigInteger; import java.net.UnknownHostException; @@ -27,14 +26,10 @@ public class Tree { public static final int HashAbbrevSize = 1 + 16 * 13 / 8; // Size of an encoded hash (plus comma) public static final int MaxChildren = 370 / HashAbbrevSize; // 13 children - @Getter - @Setter - private RootEntry rootEntry; - @Getter - private Map entries; + @Getter @Setter private RootEntry rootEntry; + @Getter private Map entries; private String privateKey; - @Getter - private String base32PublicKey; + @Getter private String base32PublicKey; public Tree() { init(); @@ -58,7 +53,7 @@ private Entry build(List leafs) { return new BranchEntry(children); } - //every batch size of leaf entry construct a branch + // every batch size of leaf entry construct a branch List subtrees = new ArrayList<>(); while (!leafs.isEmpty()) { int total = leafs.size(); @@ -108,17 +103,19 @@ public void sign() throws DnsException { if (StringUtils.isEmpty(privateKey)) { return; } - byte[] sig = Algorithm.sigData(rootEntry.toString(), privateKey); //message don't include prefix + byte[] sig = + Algorithm.sigData(rootEntry.toString(), privateKey); // message don't include prefix rootEntry.setSignature(sig); BigInteger publicKeyInt = Algorithm.generateKeyPair(privateKey).getPublicKey(); String unCompressPublicKey = ByteArray.toHexString(publicKeyInt.toByteArray()); - //verify ourselves + // verify ourselves boolean verified; try { - verified = Algorithm.verifySignature(unCompressPublicKey, rootEntry.toString(), - rootEntry.getSignature()); + verified = + Algorithm.verifySignature( + unCompressPublicKey, rootEntry.toString(), rootEntry.getSignature()); } catch (SignatureException e) { throw new DnsException(TypeEnum.INVALID_SIGNATURE, e); } @@ -225,9 +222,7 @@ public void setEntries(Map entries) { this.entries = entries; } - /** - * get nodes from entries dynamically. when sync first time, entries change as time - */ + /** get nodes from entries dynamically. when sync first time, entries change as time */ public List getDnsNodes() { List nodesEntryList = getNodesEntry(); List nodes = new ArrayList<>(); diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java index 312ae57beb7..62155e32eed 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AliClient.java @@ -1,11 +1,24 @@ package org.tron.p2p.dns.update; import com.aliyun.alidns20150109.Client; -import com.aliyun.alidns20150109.models.*; +import com.aliyun.alidns20150109.models.AddDomainRecordRequest; +import com.aliyun.alidns20150109.models.AddDomainRecordResponse; +import com.aliyun.alidns20150109.models.DeleteDomainRecordRequest; +import com.aliyun.alidns20150109.models.DeleteDomainRecordResponse; +import com.aliyun.alidns20150109.models.DeleteSubDomainRecordsRequest; +import com.aliyun.alidns20150109.models.DeleteSubDomainRecordsResponse; +import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest; +import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse; +import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody; import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord; +import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest; +import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse; import com.aliyun.teaopenapi.models.Config; import java.text.NumberFormat; +import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -16,10 +29,6 @@ import org.tron.p2p.dns.tree.Tree; import org.tron.p2p.exception.DnsException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - @Slf4j(topic = "net") public class AliClient implements Publish { @@ -34,8 +43,9 @@ public class AliClient implements Publish { private double changeThreshold; public static final String aliyunRoot = "@"; - public AliClient(String endpoint, String accessKeyId, String accessKeySecret, - double changeThreshold) throws Exception { + public AliClient( + String endpoint, String accessKeyId, String accessKeySecret, double changeThreshold) + throws Exception { Config config = new Config(); config.accessKeyId = accessKeyId; config.accessKeySecret = accessKeySecret; @@ -46,20 +56,19 @@ public AliClient(String endpoint, String accessKeyId, String accessKeySecret, } @Override - public void testConnect() throws Exception { - } + public void testConnect() throws Exception {} @Override public void deploy(String domainName, Tree t) throws DnsException { try { - Map existing = collectRecords( - domainName); - logger.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), - domainName); + Map existing = + collectRecords(domainName); + logger.info( + "Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domainName); String represent = LinkEntry.buildRepresent(t.getBase32PublicKey(), domainName); logger.info("Trying to publish {}", represent); t.setSeq(this.lastSeq + 1); - t.sign(); //seq changed, wo need to sign again + t.sign(); // seq changed, wo need to sign again Map records = t.toTXT(null); Set treeNodes = new HashSet<>(t.getDnsNodes()); @@ -82,7 +91,8 @@ public void deploy(String domainName, Tree t) throws DnsException { double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); logger.info( "Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", - nf.format(changePercent), changeThreshold); + nf.format(changePercent), + changeThreshold); } serverNodes.clear(); } catch (Exception e) { @@ -116,22 +126,22 @@ public Map collect request.setPageNumber(currentPageNum); DescribeDomainRecordsResponse response = aliDnsClient.describeDomainRecords(request); if (response.statusCode == successCode) { - for (DescribeDomainRecordsResponseBodyDomainRecordsRecord r : response.getBody() - .getDomainRecords().getRecord()) { + for (DescribeDomainRecordsResponseBodyDomainRecordsRecord r : + response.getBody().getDomainRecords().getRecord()) { String name = StringUtils.stripEnd(r.getRR(), "."); records.put(name, r); if (aliyunRoot.equalsIgnoreCase(name)) { rootContent = r.value; } - if (StringUtils.isNotEmpty(r.value) && r.value.startsWith( - org.tron.p2p.dns.tree.Entry.nodesPrefix)) { + if (StringUtils.isNotEmpty(r.value) + && r.value.startsWith(org.tron.p2p.dns.tree.Entry.nodesPrefix)) { NodesEntry nodesEntry; try { nodesEntry = NodesEntry.parseEntry(r.value); List dnsNodes = nodesEntry.getNodes(); collectServerNodes.addAll(dnsNodes); } catch (DnsException e) { - //ignore + // ignore logger.error("Parse nodeEntry failed: {}", e.getMessage()); } } @@ -157,7 +167,8 @@ public Map collect return records; } - private void submitChanges(String domainName, + private void submitChanges( + String domainName, Map records, Map existing) throws Exception { @@ -174,10 +185,11 @@ private void submitChanges(String domainName, if (!existing.containsKey(entry.getKey())) { result = addRecord(domainName, entry.getKey(), entry.getValue(), ttl); addCount++; - } else if (!entry.getValue().equals(existing.get(entry.getKey()).getValue()) || - existing.get(entry.getKey()).getTTL() != ttl) { - result = updateRecord(existing.get(entry.getKey()).getRecordId(), entry.getKey(), - entry.getValue(), ttl); + } else if (!entry.getValue().equals(existing.get(entry.getKey()).getValue()) + || existing.get(entry.getKey()).getTTL() != ttl) { + result = + updateRecord( + existing.get(entry.getKey()).getRecordId(), entry.getKey(), entry.getValue(), ttl); updateCount++; } @@ -192,8 +204,11 @@ private void submitChanges(String domainName, deleteCount++; } } - logger.info("Published successfully, add count:{}, update count:{}, delete count:{}", - addCount, updateCount, deleteCount); + logger.info( + "Published successfully, add count:{}, update count:{}, delete count:{}", + addCount, + updateCount, + deleteCount); } public boolean addRecord(String domainName, String RR, String value, long ttl) throws Exception { @@ -324,8 +339,11 @@ public boolean deleteByRR(String domainName, String RR) { } } } catch (Exception e) { - logger.warn("Failed to delete domain record, domain name: {}, RR: {}, error msg: {}", - domainName, RR, e.getMessage()); + logger.warn( + "Failed to delete domain record, domain name: {}, RR: {}, error msg: {}", + domainName, + RR, + e.getMessage()); return false; } return true; diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java index 12b00b6041b..1df9abfed1f 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/AwsClient.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.update; - import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; @@ -56,28 +55,35 @@ public class AwsClient implements Publish { private static final String postfix = "."; private double changeThreshold; - public AwsClient(final String accessKey, final String accessKeySecret, - final String zoneId, final String region, double changeThreshold) throws DnsException { + public AwsClient( + final String accessKey, + final String accessKeySecret, + final String zoneId, + final String region, + double changeThreshold) + throws DnsException { if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(accessKeySecret)) { - throw new DnsException(TypeEnum.DEPLOY_DOMAIN_FAILED, - "Need Route53 Access Key ID and secret to proceed"); + throw new DnsException( + TypeEnum.DEPLOY_DOMAIN_FAILED, "Need Route53 Access Key ID and secret to proceed"); } - StaticCredentialsProvider staticCredentialsProvider = StaticCredentialsProvider.create( - new AwsCredentials() { - @Override - public String accessKeyId() { - return accessKey; - } - - @Override - public String secretAccessKey() { - return accessKeySecret; - } - }); - route53Client = Route53Client.builder() - .credentialsProvider(staticCredentialsProvider) - .region(Region.of(region)) - .build(); + StaticCredentialsProvider staticCredentialsProvider = + StaticCredentialsProvider.create( + new AwsCredentials() { + @Override + public String accessKeyId() { + return accessKey; + } + + @Override + public String secretAccessKey() { + return accessKeySecret; + } + }); + route53Client = + Route53Client.builder() + .credentialsProvider(staticCredentialsProvider) + .region(Region.of(region)) + .build(); this.zoneId = zoneId; this.serverNodes = new HashSet<>(); this.changeThreshold = changeThreshold; @@ -128,12 +134,13 @@ public void deploy(String domain, Tree tree) throws Exception { checkZone(domain); Map existing = collectRecords(domain); - logger.info("Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domain); + logger.info( + "Find {} TXT records, {} nodes for {}", existing.size(), serverNodes.size(), domain); String represent = LinkEntry.buildRepresent(tree.getBase32PublicKey(), domain); logger.info("Trying to publish {}", represent); tree.setSeq(this.lastSeq + 1); - tree.sign(); //seq changed, wo need to sign again + tree.sign(); // seq changed, wo need to sign again Map records = tree.toTXT(domain); List changes = computeChanges(domain, records, existing); @@ -156,8 +163,10 @@ public void deploy(String domain, Tree tree) throws Exception { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(4); double changePercent = (addNodeSize + deleteNodeSize) / (double) serverNodes.size(); - logger.info("Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", - nf.format(changePercent), changeThreshold); + logger.info( + "Sum of node add & delete percent {} is below changeThreshold {}, skip this changes", + nf.format(changePercent), + changeThreshold); } serverNodes.clear(); } @@ -188,10 +197,10 @@ public Map collectRecords(String rootDomain) throws Exception String rootContent = null; Set collectServerNodes = new HashSet<>(); while (true) { - logger.info("Loading existing TXT records from name:{} zoneId:{} page:{}", rootDomain, zoneId, - page); - ListResourceRecordSetsResponse response = route53Client.listResourceRecordSets( - request.build()); + logger.info( + "Loading existing TXT records from name:{} zoneId:{} page:{}", rootDomain, zoneId, page); + ListResourceRecordSetsResponse response = + route53Client.listResourceRecordSets(request.build()); List recordSetList = response.resourceRecordSets(); for (ResourceRecordSet resourceRecordSet : recordSetList) { @@ -203,8 +212,7 @@ public Map collectRecords(String rootDomain) throws Exception for (ResourceRecord resourceRecord : resourceRecordSet.resourceRecords()) { values.add(resourceRecord.value()); } - RecordSet recordSet = new RecordSet(values.toArray(new String[0]), - resourceRecordSet.ttl()); + RecordSet recordSet = new RecordSet(values.toArray(new String[0]), resourceRecordSet.ttl()); String name = StringUtils.stripEnd(resourceRecordSet.name(), postfix); existing.put(name, recordSet); @@ -220,7 +228,7 @@ public Map collectRecords(String rootDomain) throws Exception List dnsNodes = nodesEntry.getNodes(); collectServerNodes.addAll(dnsNodes); } catch (DnsException e) { - //ignore + // ignore logger.error("Parse nodeEntry failed: {}", e.getMessage()); } } @@ -257,10 +265,11 @@ public void submitChanges(List changes, String comment) { return; } - List> batchChanges = splitChanges(changes, route53ChangeSizeLimit, - route53ChangeCountLimit); + List> batchChanges = + splitChanges(changes, route53ChangeSizeLimit, route53ChangeCountLimit); - ChangeResourceRecordSetsResponse[] responses = new ChangeResourceRecordSetsResponse[batchChanges.size()]; + ChangeResourceRecordSetsResponse[] responses = + new ChangeResourceRecordSetsResponse[batchChanges.size()]; for (int i = 0; i < batchChanges.size(); i++) { logger.info("Submit {}/{} changes to Route53", i + 1, batchChanges.size()); @@ -292,6 +301,7 @@ public void submitChanges(List changes, String comment) { try { Thread.sleep(15 * 1000); } catch (InterruptedException e) { + // expected } } } @@ -301,8 +311,8 @@ public void submitChanges(List changes, String comment) { // computeChanges creates DNS changes for the given set of DNS discovery records. // records is the latest records to be put in Route53. // The 'existing' arg is the set of records that already exist on Route53. - public List computeChanges(String domain, Map records, - Map existing) { + public List computeChanges( + String domain, Map records, Map existing) { List changes = new ArrayList<>(); for (Entry entry : records.entrySet()) { @@ -328,10 +338,10 @@ public List computeChanges(String domain, Map records, try { RootEntry oldRoot = RootEntry.parseEntry(StringUtils.strip(preValue, symbol)); RootEntry newRoot = RootEntry.parseEntry(StringUtils.strip(newValue, symbol)); - logger.info("Updating root from [{}] to [{}]", oldRoot.getDnsRoot(), - newRoot.getDnsRoot()); + logger.info( + "Updating root from [{}] to [{}]", oldRoot.getDnsRoot(), newRoot.getDnsRoot()); } catch (DnsException e) { - //ignore + // ignore } } Change change = newTXTChange(ChangeAction.UPSERT, path, ttl, newValue); @@ -348,8 +358,8 @@ public List computeChanges(String domain, Map records, } // creates record changes which delete all records not contained in 'keep' - public List makeDeletionChanges(Map keeps, - Map existing) { + public List makeDeletionChanges( + Map keeps, Map existing) { List changes = new ArrayList<>(); for (Entry entry : existing.entrySet()) { String path = entry.getKey(); @@ -365,13 +375,14 @@ public List makeDeletionChanges(Map keeps, // ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order. public static void sortChanges(List changes) { - changes.sort((o1, o2) -> { - if (getChangeOrder(o1) == getChangeOrder(o2)) { - return o1.resourceRecordSet().name().compareTo(o2.resourceRecordSet().name()); - } else { - return getChangeOrder(o1) - getChangeOrder(o2); - } - }); + changes.sort( + (o1, o2) -> { + if (getChangeOrder(o1) == getChangeOrder(o2)) { + return o1.resourceRecordSet().name().compareTo(o2.resourceRecordSet().name()); + } else { + return getChangeOrder(o1) - getChangeOrder(o2); + } + }); } private static int getChangeOrder(Change change) { @@ -388,8 +399,8 @@ private static int getChangeOrder(Change change) { } // splits up DNS changes such that each change batch is smaller than the given RDATA limit. - private static List> splitChanges(List changes, int sizeLimit, - int countLimit) { + private static List> splitChanges( + List changes, int sizeLimit, int countLimit) { List> batchChanges = new ArrayList<>(); List subChanges = new ArrayList<>(); @@ -399,8 +410,7 @@ private static List> splitChanges(List changes, int sizeLim int changeCount = getChangeCount(change); int changeSize = getChangeSize(change) * changeCount; - if (batchCount + changeCount <= countLimit - && batchSize + changeSize <= sizeLimit) { + if (batchCount + changeCount <= countLimit && batchSize + changeSize <= sizeLimit) { subChanges.add(change); batchCount += changeCount; batchSize += changeSize; @@ -435,11 +445,12 @@ private static int getChangeCount(Change change) { } public static boolean isSameChange(Change c1, Change c2) { - boolean isSame = c1.action().equals(c2.action()) - && c1.resourceRecordSet().ttl().longValue() == c2.resourceRecordSet().ttl().longValue() - && c1.resourceRecordSet().name().equals(c2.resourceRecordSet().name()) - && c1.resourceRecordSet().resourceRecords().size() == c2.resourceRecordSet() - .resourceRecords().size(); + boolean isSame = + c1.action().equals(c2.action()) + && c1.resourceRecordSet().ttl().longValue() == c2.resourceRecordSet().ttl().longValue() + && c1.resourceRecordSet().name().equals(c2.resourceRecordSet().name()) + && c1.resourceRecordSet().resourceRecords().size() + == c2.resourceRecordSet().resourceRecords().size(); if (!isSame) { return false; } @@ -455,10 +466,8 @@ public static boolean isSameChange(Change c1, Change c2) { // creates a change to a TXT record. public Change newTXTChange(ChangeAction action, String key, long ttl, String... values) { - ResourceRecordSet.Builder builder = ResourceRecordSet.builder() - .name(key) - .type(RRType.TXT) - .ttl(ttl); + ResourceRecordSet.Builder builder = + ResourceRecordSet.builder().name(key).type(RRType.TXT).ttl(ttl); List resourceRecords = new ArrayList<>(); for (String value : values) { ResourceRecord.Builder builder1 = ResourceRecord.builder(); diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java b/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java index ae935762d3d..fc00b0b4a2c 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/DnsType.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.update; - public enum DnsType { AliYun(0, "aliyun dns server"), AwsRoute53(1, "aws route53 server"); diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java b/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java index aa2733d716e..c5ccfe214b0 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/Publish.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.update; - import java.util.Map; import org.tron.p2p.dns.tree.Tree; diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java index 2da9f0acc82..ddc94351511 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishConfig.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns.update; - import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; @@ -19,7 +18,7 @@ public class PublishConfig { private DnsType dnsType = null; private String accessKeyId = null; private String accessKeySecret = null; - private String aliDnsEndpoint = null; //for aliYun - private String awsHostZoneId = null; //for aws - private String awsRegion = null; //for aws + private String aliDnsEndpoint = null; // for aliYun + private String awsHostZoneId = null; // for aws + private String awsRegion = null; // for aws } diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java index 75bb646d050..26780a3c311 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java @@ -24,8 +24,9 @@ public class PublishService { private static final long publishDelay = 1 * 60 * 60; - private ScheduledExecutorService publisher = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("publishService").build()); + private ScheduledExecutorService publisher = + Executors.newSingleThreadScheduledExecutor( + BasicThreadFactory.builder().namingPattern("publishService").build()); private Publish publish; public void init() { @@ -51,16 +52,20 @@ public void init() { private Publish getPublish(PublishConfig config) throws Exception { Publish publish; if (config.getDnsType() == DnsType.AliYun) { - publish = new AliClient(config.getAliDnsEndpoint(), - config.getAccessKeyId(), - config.getAccessKeySecret(), - config.getChangeThreshold()); + publish = + new AliClient( + config.getAliDnsEndpoint(), + config.getAccessKeyId(), + config.getAccessKeySecret(), + config.getChangeThreshold()); } else { - publish = new AwsClient(config.getAccessKeyId(), - config.getAccessKeySecret(), - config.getAwsHostZoneId(), - config.getAwsRegion(), - config.getChangeThreshold()); + publish = + new AwsClient( + config.getAccessKeyId(), + config.getAccessKeySecret(), + config.getAwsHostZoneId(), + config.getAwsRegion(), + config.getChangeThreshold()); } return publish; } @@ -83,11 +88,19 @@ private List getNodes(PublishConfig config) throws UnknownHostException if (config.getStaticNodes() != null && !config.getStaticNodes().isEmpty()) { for (InetSocketAddress staticAddress : config.getStaticNodes()) { if (staticAddress.getAddress() instanceof Inet4Address) { - nodes.add(new Node(null, staticAddress.getAddress().getHostAddress(), null, - staticAddress.getPort())); + nodes.add( + new Node( + null, + staticAddress.getAddress().getHostAddress(), + null, + staticAddress.getPort())); } else { - nodes.add(new Node(null, null, staticAddress.getAddress().getHostAddress(), - staticAddress.getPort())); + nodes.add( + new Node( + null, + null, + staticAddress.getAddress().getHostAddress(), + staticAddress.getPort())); } } } else { @@ -96,8 +109,8 @@ private List getNodes(PublishConfig config) throws UnknownHostException } List dnsNodes = new ArrayList<>(); for (Node node : nodes) { - DnsNode dnsNode = new DnsNode(node.getId(), node.getHostV4(), node.getHostV6(), - node.getPort()); + DnsNode dnsNode = + new DnsNode(node.getId(), node.getHostV4(), node.getHostV6(), node.getPort()); dnsNodes.add(dnsNode); } return Tree.merge(dnsNodes, config.getMaxMergeSize()); @@ -113,25 +126,25 @@ private boolean checkConfig(boolean supportV4, PublishConfig config) { return false; } if (config.getDnsType() == null) { - logger.error("The dns server type must be specified when enabling the dns publishing service"); + logger.error( + "The dns server type must be specified when enabling the dns publishing service"); return false; } if (StringUtils.isEmpty(config.getDnsDomain())) { logger.error("The dns domain must be specified when enabling the dns publishing service"); return false; } - if (config.getDnsType() == DnsType.AliYun && - (StringUtils.isEmpty(config.getAccessKeyId()) || - StringUtils.isEmpty(config.getAccessKeySecret()) || - StringUtils.isEmpty(config.getAliDnsEndpoint()) - )) { + if (config.getDnsType() == DnsType.AliYun + && (StringUtils.isEmpty(config.getAccessKeyId()) + || StringUtils.isEmpty(config.getAccessKeySecret()) + || StringUtils.isEmpty(config.getAliDnsEndpoint()))) { logger.error("The configuration items related to the Aliyun dns server cannot be empty"); return false; } - if (config.getDnsType() == DnsType.AwsRoute53 && - (StringUtils.isEmpty(config.getAccessKeyId()) || - StringUtils.isEmpty(config.getAccessKeySecret()) || - config.getAwsRegion() == null)) { + if (config.getDnsType() == DnsType.AwsRoute53 + && (StringUtils.isEmpty(config.getAccessKeyId()) + || StringUtils.isEmpty(config.getAccessKeySecret()) + || config.getAwsRegion() == null)) { logger.error("The configuration items related to the AwsRoute53 dns server cannot be empty"); return false; } diff --git a/p2p/src/main/java/org/tron/p2p/exception/DnsException.java b/p2p/src/main/java/org/tron/p2p/exception/DnsException.java index 40e4ff78d31..28206f4c774 100644 --- a/p2p/src/main/java/org/tron/p2p/exception/DnsException.java +++ b/p2p/src/main/java/org/tron/p2p/exception/DnsException.java @@ -1,6 +1,5 @@ package org.tron.p2p.exception; - public class DnsException extends Exception { private static final long serialVersionUID = 9096335228978001485L; @@ -27,7 +26,7 @@ public DnsException.TypeEnum getType() { public enum TypeEnum { LOOK_UP_ROOT_FAILED(0, "look up root failed"), - //Resolver/sync errors + // Resolver/sync errors NO_ROOT_FOUND(1, "no valid root found"), NO_ENTRY_FOUND(2, "no valid tree entry found"), HASH_MISS_MATCH(3, "hash miss match"), diff --git a/p2p/src/main/java/org/tron/p2p/exception/P2pException.java b/p2p/src/main/java/org/tron/p2p/exception/P2pException.java index 32191fb3af7..c195b4f5303 100644 --- a/p2p/src/main/java/org/tron/p2p/exception/P2pException.java +++ b/p2p/src/main/java/org/tron/p2p/exception/P2pException.java @@ -55,5 +55,4 @@ public String toString() { return value + ", " + desc; } } - } diff --git a/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java b/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java index 4671ba58f7a..0b34c35a9da 100644 --- a/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java +++ b/p2p/src/main/java/org/tron/p2p/stats/TrafficStats.java @@ -6,9 +6,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.channel.socket.DatagramPacket; -import lombok.Getter; - import java.util.concurrent.atomic.AtomicLong; +import lombok.Getter; public class TrafficStats { public static final TrafficStatHandler tcp = new TrafficStatHandler(); @@ -16,14 +15,10 @@ public class TrafficStats { @ChannelHandler.Sharable static class TrafficStatHandler extends ChannelDuplexHandler { - @Getter - private AtomicLong outSize = new AtomicLong(); - @Getter - private AtomicLong inSize = new AtomicLong(); - @Getter - private AtomicLong outPackets = new AtomicLong(); - @Getter - private AtomicLong inPackets = new AtomicLong(); + @Getter private AtomicLong outSize = new AtomicLong(); + @Getter private AtomicLong inSize = new AtomicLong(); + @Getter private AtomicLong outPackets = new AtomicLong(); + @Getter private AtomicLong inPackets = new AtomicLong(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { @@ -38,7 +33,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) - throws Exception { + throws Exception { outPackets.incrementAndGet(); if (msg instanceof ByteBuf) { outSize.addAndGet(((ByteBuf) msg).readableBytes()); diff --git a/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java index 5d0102ee417..eeb37601a94 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java +++ b/p2p/src/main/java/org/tron/p2p/utils/ByteArray.java @@ -13,7 +13,6 @@ import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; - /* * Copyright (c) [2016] [ ] * This file is part of the ethereumJ library. @@ -42,9 +41,7 @@ public static String toHexString(byte[] data) { return data == null ? "" : Hex.toHexString(data); } - /** - * get bytes data from hex string data. - */ + /** get bytes data from hex string data. */ public static byte[] fromHexString(String data) { if (data == null) { return EMPTY_BYTE_ARRAY; @@ -58,30 +55,22 @@ public static byte[] fromHexString(String data) { return Hex.decode(data); } - /** - * get long data from bytes data. - */ + /** get long data from bytes data. */ public static long toLong(byte[] b) { return ArrayUtils.isEmpty(b) ? 0 : new BigInteger(1, b).longValue(); } - /** - * get int data from bytes data. - */ + /** get int data from bytes data. */ public static int toInt(byte[] b) { return ArrayUtils.isEmpty(b) ? 0 : new BigInteger(1, b).intValue(); } - /** - * get bytes data from string data. - */ + /** get bytes data from string data. */ public static byte[] fromString(String s) { return StringUtils.isBlank(s) ? null : s.getBytes(); } - /** - * get string data from bytes data. - */ + /** get string data from bytes data. */ public static String toStr(byte[] b) { return ArrayUtils.isEmpty(b) ? null : new String(b); } @@ -94,9 +83,7 @@ public static byte[] fromInt(int val) { return Ints.toByteArray(val); } - /** - * get bytes data from object data. - */ + /** get bytes data from object data. */ public static byte[] fromObject(Object obj) { byte[] bytes = null; try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); @@ -110,16 +97,11 @@ public static byte[] fromObject(Object obj) { return bytes; } - /** - * Stringify byte[] x - * null for null - * null for empty [] - */ + /** Stringify byte[] x null for null null for empty [] */ public static String toJsonHex(byte[] x) { return x == null || x.length == 0 ? "0x" : "0x" + Hex.toHexString(x); } - public static String toJsonHex(Long x) { return x == null ? null : "0x" + Long.toHexString(x); } @@ -140,7 +122,6 @@ public static BigInteger hexToBigInteger(String input) { } } - public static int jsonHexToInt(String x) throws Exception { if (!x.startsWith("0x")) { throw new Exception("Incorrect hex syntax"); @@ -156,7 +137,7 @@ public static int jsonHexToInt(String x) throws Exception { * @param start the start index * @param end the end index * @return a subarray of input, ranging from start (inclusively) to end - * (exclusively) + * (exclusively) */ public static byte[] subArray(byte[] input, int start, int end) { byte[] result = new byte[end - start]; diff --git a/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java b/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java index e5b5511210c..3ffad02e082 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java +++ b/p2p/src/main/java/org/tron/p2p/utils/CollectionUtils.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.List; - public class CollectionUtils { public static List truncate(List items, int limit) { diff --git a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java index 83d026ac12e..b63b8bbf114 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java +++ b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java @@ -8,7 +8,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; -import java.net.Socket; import java.net.SocketException; import java.net.URL; import java.net.URLConnection; @@ -34,14 +33,38 @@ public class NetUtil { public static final Pattern PATTERN_IPv4 = - Pattern.compile("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\" - + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" - + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" - + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"); + Pattern.compile( + "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\" + + ".(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"); - //https://codeantenna.com/a/jvrULhCbdj - public static final Pattern PATTERN_IPv6 = Pattern.compile( - "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"); + // https://codeantenna.com/a/jvrULhCbdj + public static final Pattern PATTERN_IPv6 = + Pattern.compile( + "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))" + + "|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}" + + "|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))" + + "|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})" + + "|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))" + + "|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})" + + "|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + + "|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})" + + "|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + + "|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})" + + "|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + + "|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})" + + "|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + + "|(:(((:[0-9A-Fa-f]{1,4}){1,7})" + + "|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + + "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))" + + "(%.+)?\\s*$"); private static final String IPADDRESS_LOCALHOST = "127.0.0.1"; @@ -79,9 +102,11 @@ public static boolean validNode(Node node) { } public static Node getNode(Discover.Endpoint endpoint) { - return new Node(endpoint.getNodeId().toByteArray(), + return new Node( + endpoint.getNodeId().toByteArray(), ByteArray.toStr(endpoint.getAddress().toByteArray()), - ByteArray.toStr(endpoint.getAddressIpv6().toByteArray()), endpoint.getPort()); + ByteArray.toStr(endpoint.getAddressIpv6().toByteArray()), + endpoint.getPort()); } public static byte[] getNodeId() { @@ -96,8 +121,8 @@ private static String getExternalIp(String url, boolean isAskIpv4) { String ip = null; try { URLConnection urlConnection = new URL(url).openConnection(); - urlConnection.setConnectTimeout(10_000); //ms - urlConnection.setReadTimeout(10_000); //ms + urlConnection.setConnectTimeout(10_000); // ms + urlConnection.setReadTimeout(10_000); // ms in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); ip = in.readLine(); if (ip == null || ip.trim().isEmpty()) { @@ -112,15 +137,18 @@ private static String getExternalIp(String url, boolean isAskIpv4) { } return ip; } catch (Exception e) { - logger.warn("Fail to get {} by {}, cause:{}", - Constant.ipV4Urls.contains(url) ? "ipv4" : "ipv6", url, e.getMessage()); + logger.warn( + "Fail to get {} by {}, cause:{}", + Constant.ipV4Urls.contains(url) ? "ipv4" : "ipv6", + url, + e.getMessage()); return null; } finally { if (in != null) { try { in.close(); } catch (IOException e) { - //ignore + // ignore } } } @@ -176,8 +204,10 @@ public static Set getAllLocalAddress() { } private static boolean isReservedAddress(InetAddress inetAddress) { - return inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress() - || inetAddress.isLoopbackAddress() || inetAddress.isMulticastAddress(); + return inetAddress.isAnyLocalAddress() + || inetAddress.isLinkLocalAddress() + || inetAddress.isLoopbackAddress() + || inetAddress.isMulticastAddress(); } public static String getExternalIpV4() { @@ -205,22 +235,25 @@ public static InetSocketAddress parseInetSocketAddress(String para) { host = host.substring(1, host.length() - 1); } else { if (host.contains(":")) { - throw new RuntimeException(String.format("Invalid inetSocketAddress: \"%s\", " - + "use ipv4:port or [ipv6]:port", para)); + throw new RuntimeException( + String.format( + "Invalid inetSocketAddress: \"%s\", " + "use ipv4:port or [ipv6]:port", para)); } } int port = Integer.parseInt(para.substring(index + 1)); return new InetSocketAddress(host, port); } else { - throw new RuntimeException(String.format("Invalid inetSocketAddress: \"%s\", " - + "use ipv4:port or [ipv6]:port", para)); + throw new RuntimeException( + String.format( + "Invalid inetSocketAddress: \"%s\", " + "use ipv4:port or [ipv6]:port", para)); } } private static String getIp(List multiSrcUrls, boolean isAskIpv4) { int threadSize = multiSrcUrls.size(); - ExecutorService executor = Executors.newFixedThreadPool(threadSize, - BasicThreadFactory.builder().namingPattern("getIp-%d").build()); + ExecutorService executor = + Executors.newFixedThreadPool( + threadSize, BasicThreadFactory.builder().namingPattern("getIp-%d").build()); CompletionService completionService = new ExecutorCompletionService<>(executor); for (String url : multiSrcUrls) { @@ -230,7 +263,7 @@ private static String getIp(List multiSrcUrls, boolean isAskIpv4) { String ip = null; for (int i = 0; i < threadSize; i++) { try { - //block until any result return + // block until any result return Future f = completionService.take(); String result = f.get(); if (StringUtils.isNotEmpty(result)) { @@ -238,7 +271,7 @@ private static String getIp(List multiSrcUrls, boolean isAskIpv4) { break; } } catch (Exception ignored) { - //ignore + // ignore } } diff --git a/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java b/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java index afd0b7e33b3..bb3db746ebc 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java +++ b/p2p/src/main/java/org/tron/p2p/utils/ProtoUtil.java @@ -2,7 +2,6 @@ import com.google.protobuf.ByteString; import java.io.IOException; - import org.tron.p2p.base.Parameter; import org.tron.p2p.exception.P2pException; import org.tron.p2p.protos.Connect; @@ -21,8 +20,9 @@ public static Connect.CompressMessage compressMessage(byte[] data) throws IOExce } return Connect.CompressMessage.newBuilder() - .setData(ByteString.copyFrom(bytes)) - .setType(type).build(); + .setData(ByteString.copyFrom(bytes)) + .setType(type) + .build(); } public static byte[] uncompressMessage(Connect.CompressMessage message) @@ -34,16 +34,15 @@ public static byte[] uncompressMessage(Connect.CompressMessage message) int length = Snappy.uncompressedLength(data); if (length >= Parameter.MAX_MESSAGE_LENGTH) { - throw new P2pException(P2pException.TypeEnum.BIG_MESSAGE, - "message is too big, len=" + length); + throw new P2pException( + P2pException.TypeEnum.BIG_MESSAGE, "message is too big, len=" + length); } byte[] d2 = Snappy.uncompress(data); if (d2.length >= Parameter.MAX_MESSAGE_LENGTH) { - throw new P2pException(P2pException.TypeEnum.BIG_MESSAGE, - "uncompressed is too big, len=" + length); + throw new P2pException( + P2pException.TypeEnum.BIG_MESSAGE, "uncompressed is too big, len=" + length); } return d2; } - } diff --git a/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java b/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java index c2886feb1a0..864651c85bb 100644 --- a/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java +++ b/p2p/src/main/java/org/web3j/crypto/ECDSASignature.java @@ -1,60 +1,63 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.crypto; import java.math.BigInteger; /** An ECDSA Signature. */ public class ECDSASignature { - public final BigInteger r; - public final BigInteger s; + public final BigInteger r; + public final BigInteger s; - public ECDSASignature(BigInteger r, BigInteger s) { - this.r = r; - this.s = s; - } + public ECDSASignature(BigInteger r, BigInteger s) { + this.r = r; + this.s = s; + } - /** - * @return true if the S component is "low", that means it is below {@link - * Sign#HALF_CURVE_ORDER}. See - * BIP62. - */ - public boolean isCanonical() { - return s.compareTo(Sign.HALF_CURVE_ORDER) <= 0; - } + /** + * @return true if the S component is "low", that means it is below {@link Sign#HALF_CURVE_ORDER}. + * See + * BIP62. + */ + public boolean isCanonical() { + return s.compareTo(Sign.HALF_CURVE_ORDER) <= 0; + } - /** - * Will automatically adjust the S component to be less than or equal to half the curve order, - * if necessary. This is required because for every signature (r,s) the signature (r, -s (mod - * N)) is a valid signature of the same message. However, we dislike the ability to modify the - * bits of a Bitcoin transaction after it's been signed, as that violates various assumed - * invariants. Thus in future only one of those forms will be considered legal and the other - * will be banned. - * - * @return the signature in a canonicalised form. - */ - public ECDSASignature toCanonicalised() { - if (!isCanonical()) { - // The order of the curve is the number of valid points that exist on that curve. - // If S is in the upper half of the number of valid points, then bring it back to - // the lower half. Otherwise, imagine that - // N = 10 - // s = 8, so (-8 % 10 == 2) thus both (r, 8) and (r, 2) are valid solutions. - // 10 - 8 == 2, giving us always the latter solution, which is canonical. - return new ECDSASignature(r, Sign.CURVE.getN().subtract(s)); - } else { - return this; - } + /** + * Will automatically adjust the S component to be less than or equal to half the curve order, if + * necessary. This is required because for every signature (r,s) the signature (r, -s (mod N)) is + * a valid signature of the same message. However, we dislike the ability to modify the bits of a + * Bitcoin transaction after it's been signed, as that violates various assumed invariants. Thus + * in future only one of those forms will be considered legal and the other will be banned. + * + * @return the signature in a canonicalised form. + */ + public ECDSASignature toCanonicalised() { + if (!isCanonical()) { + // The order of the curve is the number of valid points that exist on that curve. + // If S is in the upper half of the number of valid points, then bring it back to + // the lower half. Otherwise, imagine that + // N = 10 + // s = 8, so (-8 % 10 == 2) thus both (r, 8) and (r, 2) are valid solutions. + // 10 - 8 == 2, giving us always the latter solution, which is canonical. + return new ECDSASignature(r, Sign.CURVE.getN().subtract(s)); + } else { + return this; } + } } diff --git a/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java b/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java index 1efd0406bea..357cecca5e3 100644 --- a/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java +++ b/p2p/src/main/java/org/web3j/crypto/ECKeyPair.java @@ -1,114 +1,114 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.crypto; import java.math.BigInteger; import java.security.KeyPair; import java.util.Arrays; - import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.signers.ECDSASigner; import org.bouncycastle.crypto.signers.HMacDSAKCalculator; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; - import org.web3j.utils.Numeric; /** Elliptic Curve SECP-256k1 generated key pair. */ public class ECKeyPair { - private final BigInteger privateKey; - private final BigInteger publicKey; - - public ECKeyPair(BigInteger privateKey, BigInteger publicKey) { - this.privateKey = privateKey; - this.publicKey = publicKey; - } - - public BigInteger getPrivateKey() { - return privateKey; + private final BigInteger privateKey; + private final BigInteger publicKey; + + public ECKeyPair(BigInteger privateKey, BigInteger publicKey) { + this.privateKey = privateKey; + this.publicKey = publicKey; + } + + public BigInteger getPrivateKey() { + return privateKey; + } + + public BigInteger getPublicKey() { + return publicKey; + } + + /** + * Sign a hash with the private key of this key pair. + * + * @param transactionHash the hash to sign + * @return An {@link ECDSASignature} of the hash + */ + public ECDSASignature sign(byte[] transactionHash) { + ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); + + ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); + signer.init(true, privKey); + BigInteger[] components = signer.generateSignature(transactionHash); + + return new ECDSASignature(components[0], components[1]).toCanonicalised(); + } + + public static ECKeyPair create(KeyPair keyPair) { + BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); + BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); + + BigInteger privateKeyValue = privateKey.getD(); + + // Ethereum does not use encoded public keys like bitcoin - see + // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details + // Additionally, as the first bit is a constant prefix (0x04) we ignore this value + byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); + BigInteger publicKeyValue = + new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); + + return new ECKeyPair(privateKeyValue, publicKeyValue); + } + + public static ECKeyPair create(BigInteger privateKey) { + return new ECKeyPair(privateKey, Sign.publicKeyFromPrivate(privateKey)); + } + + public static ECKeyPair create(byte[] privateKey) { + return create(Numeric.toBigInt(privateKey)); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; } - - public BigInteger getPublicKey() { - return publicKey; + if (o == null || getClass() != o.getClass()) { + return false; } - /** - * Sign a hash with the private key of this key pair. - * - * @param transactionHash the hash to sign - * @return An {@link ECDSASignature} of the hash - */ - public ECDSASignature sign(byte[] transactionHash) { - ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); - - ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKey, Sign.CURVE); - signer.init(true, privKey); - BigInteger[] components = signer.generateSignature(transactionHash); + ECKeyPair ecKeyPair = (ECKeyPair) o; - return new ECDSASignature(components[0], components[1]).toCanonicalised(); + if (privateKey != null + ? !privateKey.equals(ecKeyPair.privateKey) + : ecKeyPair.privateKey != null) { + return false; } - public static ECKeyPair create(KeyPair keyPair) { - BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); - BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); - - BigInteger privateKeyValue = privateKey.getD(); - - // Ethereum does not use encoded public keys like bitcoin - see - // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details - // Additionally, as the first bit is a constant prefix (0x04) we ignore this value - byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); - BigInteger publicKeyValue = - new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); + return publicKey != null ? publicKey.equals(ecKeyPair.publicKey) : ecKeyPair.publicKey == null; + } - return new ECKeyPair(privateKeyValue, publicKeyValue); - } - - public static ECKeyPair create(BigInteger privateKey) { - return new ECKeyPair(privateKey, Sign.publicKeyFromPrivate(privateKey)); - } - - public static ECKeyPair create(byte[] privateKey) { - return create(Numeric.toBigInt(privateKey)); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - ECKeyPair ecKeyPair = (ECKeyPair) o; - - if (privateKey != null - ? !privateKey.equals(ecKeyPair.privateKey) - : ecKeyPair.privateKey != null) { - return false; - } - - return publicKey != null - ? publicKey.equals(ecKeyPair.publicKey) - : ecKeyPair.publicKey == null; - } - - @Override - public int hashCode() { - int result = privateKey != null ? privateKey.hashCode() : 0; - result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0); - return result; - } + @Override + public int hashCode() { + int result = privateKey != null ? privateKey.hashCode() : 0; + result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0); + return result; + } } diff --git a/p2p/src/main/java/org/web3j/crypto/Hash.java b/p2p/src/main/java/org/web3j/crypto/Hash.java index ed908894c5c..7c4e6d27d1b 100644 --- a/p2p/src/main/java/org/web3j/crypto/Hash.java +++ b/p2p/src/main/java/org/web3j/crypto/Hash.java @@ -1,138 +1,140 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.crypto; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; - import org.bouncycastle.crypto.digests.RIPEMD160Digest; import org.bouncycastle.crypto.digests.SHA512Digest; import org.bouncycastle.crypto.macs.HMac; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.jcajce.provider.digest.Blake2b; import org.bouncycastle.jcajce.provider.digest.Keccak; - import org.web3j.utils.Numeric; /** Cryptographic hash functions. */ public class Hash { - private Hash() {} + private Hash() {} - /** - * Generates a digest for the given {@code input}. - * - * @param input The input to digest - * @param algorithm The hash algorithm to use - * @return The hash value for the given input - * @throws RuntimeException If we couldn't find any provider for the given algorithm - */ - public static byte[] hash(byte[] input, String algorithm) { - try { - MessageDigest digest = MessageDigest.getInstance(algorithm.toUpperCase()); - return digest.digest(input); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("Couldn't find a " + algorithm + " provider", e); - } + /** + * Generates a digest for the given {@code input}. + * + * @param input The input to digest + * @param algorithm The hash algorithm to use + * @return The hash value for the given input + * @throws RuntimeException If we couldn't find any provider for the given algorithm + */ + public static byte[] hash(byte[] input, String algorithm) { + try { + MessageDigest digest = MessageDigest.getInstance(algorithm.toUpperCase()); + return digest.digest(input); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Couldn't find a " + algorithm + " provider", e); } + } - /** - * Keccak-256 hash function. - * - * @param hexInput hex encoded input data with optional 0x prefix - * @return hash value as hex encoded string - */ - public static String sha3(String hexInput) { - byte[] bytes = Numeric.hexStringToByteArray(hexInput); - byte[] result = sha3(bytes); - return Numeric.toHexString(result); - } + /** + * Keccak-256 hash function. + * + * @param hexInput hex encoded input data with optional 0x prefix + * @return hash value as hex encoded string + */ + public static String sha3(String hexInput) { + byte[] bytes = Numeric.hexStringToByteArray(hexInput); + byte[] result = sha3(bytes); + return Numeric.toHexString(result); + } - /** - * Keccak-256 hash function. - * - * @param input binary encoded input data - * @param offset of start of data - * @param length of data - * @return hash value - */ - public static byte[] sha3(byte[] input, int offset, int length) { - Keccak.DigestKeccak kecc = new Keccak.Digest256(); - kecc.update(input, offset, length); - return kecc.digest(); - } + /** + * Keccak-256 hash function. + * + * @param input binary encoded input data + * @param offset of start of data + * @param length of data + * @return hash value + */ + public static byte[] sha3(byte[] input, int offset, int length) { + Keccak.DigestKeccak kecc = new Keccak.Digest256(); + kecc.update(input, offset, length); + return kecc.digest(); + } - /** - * Keccak-256 hash function. - * - * @param input binary encoded input data - * @return hash value - */ - public static byte[] sha3(byte[] input) { - return sha3(input, 0, input.length); - } + /** + * Keccak-256 hash function. + * + * @param input binary encoded input data + * @return hash value + */ + public static byte[] sha3(byte[] input) { + return sha3(input, 0, input.length); + } - /** - * Keccak-256 hash function that operates on a UTF-8 encoded String. - * - * @param utf8String UTF-8 encoded string - * @return hash value as hex encoded string - */ - public static String sha3String(String utf8String) { - return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8))); - } + /** + * Keccak-256 hash function that operates on a UTF-8 encoded String. + * + * @param utf8String UTF-8 encoded string + * @return hash value as hex encoded string + */ + public static String sha3String(String utf8String) { + return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8))); + } - /** - * Generates SHA-256 digest for the given {@code input}. - * - * @param input The input to digest - * @return The hash value for the given input - * @throws RuntimeException If we couldn't find any SHA-256 provider - */ - public static byte[] sha256(byte[] input) { - try { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - return digest.digest(input); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("Couldn't find a SHA-256 provider", e); - } + /** + * Generates SHA-256 digest for the given {@code input}. + * + * @param input The input to digest + * @return The hash value for the given input + * @throws RuntimeException If we couldn't find any SHA-256 provider + */ + public static byte[] sha256(byte[] input) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + return digest.digest(input); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Couldn't find a SHA-256 provider", e); } + } - public static byte[] hmacSha512(byte[] key, byte[] input) { - HMac hMac = new HMac(new SHA512Digest()); - hMac.init(new KeyParameter(key)); - hMac.update(input, 0, input.length); - byte[] out = new byte[64]; - hMac.doFinal(out, 0); - return out; - } + public static byte[] hmacSha512(byte[] key, byte[] input) { + HMac hMac = new HMac(new SHA512Digest()); + hMac.init(new KeyParameter(key)); + hMac.update(input, 0, input.length); + byte[] out = new byte[64]; + hMac.doFinal(out, 0); + return out; + } - public static byte[] sha256hash160(byte[] input) { - byte[] sha256 = sha256(input); - RIPEMD160Digest digest = new RIPEMD160Digest(); - digest.update(sha256, 0, sha256.length); - byte[] out = new byte[20]; - digest.doFinal(out, 0); - return out; - } + public static byte[] sha256hash160(byte[] input) { + byte[] sha256 = sha256(input); + RIPEMD160Digest digest = new RIPEMD160Digest(); + digest.update(sha256, 0, sha256.length); + byte[] out = new byte[20]; + digest.doFinal(out, 0); + return out; + } - /** - * Blake2-256 hash function. - * - * @param input binary encoded input data - * @return hash value - */ - public static byte[] blake2b256(byte[] input) { - return new Blake2b.Blake2b256().digest(input); - } + /** + * Blake2-256 hash function. + * + * @param input binary encoded input data + * @return hash value + */ + public static byte[] blake2b256(byte[] input) { + return new Blake2b.Blake2b256().digest(input); + } } diff --git a/p2p/src/main/java/org/web3j/crypto/Sign.java b/p2p/src/main/java/org/web3j/crypto/Sign.java index e405156affc..f75dda8c7b4 100644 --- a/p2p/src/main/java/org/web3j/crypto/Sign.java +++ b/p2p/src/main/java/org/web3j/crypto/Sign.java @@ -1,21 +1,26 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.crypto; +import static org.web3j.utils.Assertions.verifyPrecondition; + import java.math.BigInteger; import java.security.SignatureException; import java.util.Arrays; - import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.asn1.x9.X9IntegerConverter; import org.bouncycastle.crypto.ec.CustomNamedCurves; @@ -24,11 +29,8 @@ import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.FixedPointCombMultiplier; import org.bouncycastle.math.ec.custom.sec.SecP256K1Curve; - import org.web3j.utils.Numeric; -import static org.web3j.utils.Assertions.verifyPrecondition; - /** * Transaction signing logic. * @@ -38,324 +40,319 @@ */ public class Sign { - public static final X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1"); - static final ECDomainParameters CURVE = - new ECDomainParameters( - CURVE_PARAMS.getCurve(), - CURVE_PARAMS.getG(), - CURVE_PARAMS.getN(), - CURVE_PARAMS.getH()); - static final BigInteger HALF_CURVE_ORDER = CURVE_PARAMS.getN().shiftRight(1); + public static final X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1"); + static final ECDomainParameters CURVE = + new ECDomainParameters( + CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH()); + static final BigInteger HALF_CURVE_ORDER = CURVE_PARAMS.getN().shiftRight(1); - static final String MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n"; + static final String MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n"; - static byte[] getEthereumMessagePrefix(int messageLength) { - return MESSAGE_PREFIX.concat(String.valueOf(messageLength)).getBytes(); - } + static byte[] getEthereumMessagePrefix(int messageLength) { + return MESSAGE_PREFIX.concat(String.valueOf(messageLength)).getBytes(); + } - static byte[] getEthereumMessageHash(byte[] message) { - byte[] prefix = getEthereumMessagePrefix(message.length); + static byte[] getEthereumMessageHash(byte[] message) { + byte[] prefix = getEthereumMessagePrefix(message.length); - byte[] result = new byte[prefix.length + message.length]; - System.arraycopy(prefix, 0, result, 0, prefix.length); - System.arraycopy(message, 0, result, prefix.length, message.length); + byte[] result = new byte[prefix.length + message.length]; + System.arraycopy(prefix, 0, result, 0, prefix.length); + System.arraycopy(message, 0, result, prefix.length, message.length); - return Hash.sha3(result); - } + return Hash.sha3(result); + } - public static SignatureData signPrefixedMessage(byte[] message, ECKeyPair keyPair) { - return signMessage(getEthereumMessageHash(message), keyPair, false); - } + public static SignatureData signPrefixedMessage(byte[] message, ECKeyPair keyPair) { + return signMessage(getEthereumMessageHash(message), keyPair, false); + } - public static SignatureData signMessage(byte[] message, ECKeyPair keyPair) { - return signMessage(message, keyPair, true); - } + public static SignatureData signMessage(byte[] message, ECKeyPair keyPair) { + return signMessage(message, keyPair, true); + } - public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) { - BigInteger publicKey = keyPair.getPublicKey(); - byte[] messageHash; - if (needToHash) { - messageHash = Hash.sha3(message); - } else { - messageHash = message; - } - - ECDSASignature sig = keyPair.sign(messageHash); - // Now we have to work backwards to figure out the recId needed to recover the signature. - int recId = -1; - for (int i = 0; i < 4; i++) { - BigInteger k = recoverFromSignature(i, sig, messageHash); - if (k != null && k.equals(publicKey)) { - recId = i; - break; - } - } - if (recId == -1) { - throw new RuntimeException( - "Could not construct a recoverable key. Are your credentials valid?"); - } - - int headerByte = recId + 27; - - // 1 header + 32 bytes for R + 32 bytes for S - byte[] v = new byte[] {(byte) headerByte}; - byte[] r = Numeric.toBytesPadded(sig.r, 32); - byte[] s = Numeric.toBytesPadded(sig.s, 32); - - return new SignatureData(v, r, s); + public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) { + BigInteger publicKey = keyPair.getPublicKey(); + byte[] messageHash; + if (needToHash) { + messageHash = Hash.sha3(message); + } else { + messageHash = message; } - /** - * Given the components of a signature and a selector value, recover and return the public key - * that generated the signature according to the algorithm in SEC1v2 section 4.1.6. - * - *

The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the - * correct one. Because the key recovery operation yields multiple potential keys, the correct - * key must either be stored alongside the signature, or you must be willing to try each recId - * in turn until you find one that outputs the key you are expecting. - * - *

If this method returns null it means recovery was not possible and recId should be - * iterated. - * - *

Given the above two points, a correct usage of this method is inside a for loop from 0 to - * 3, and if the output is null OR a key that is not the one you expect, you try again with the - * next recId. - * - * @param recId Which possible key to recover. - * @param sig the R and S components of the signature, wrapped. - * @param message Hash of the data that was signed. - * @return An ECKey containing only the public part, or null if recovery wasn't possible. - */ - public static BigInteger recoverFromSignature(int recId, ECDSASignature sig, byte[] message) { - verifyPrecondition(recId >= 0, "recId must be positive"); - verifyPrecondition(sig.r.signum() >= 0, "r must be positive"); - verifyPrecondition(sig.s.signum() >= 0, "s must be positive"); - verifyPrecondition(message != null, "message cannot be null"); - - // 1.0 For j from 0 to h (h == recId here and the loop is outside this function) - // 1.1 Let x = r + jn - BigInteger n = CURVE.getN(); // Curve order. - BigInteger i = BigInteger.valueOf((long) recId / 2); - BigInteger x = sig.r.add(i.multiply(n)); - // 1.2. Convert the integer x to an octet string X of length mlen using the conversion - // routine specified in Section 2.3.7, where mlen = ⌈(log2 p)/8⌉ or mlen = ⌈m/8⌉. - // 1.3. Convert the octet string (16 set binary digits)||X to an elliptic curve point R - // using the conversion routine specified in Section 2.3.4. If this conversion - // routine outputs "invalid", then do another iteration of Step 1. - // - // More concisely, what these points mean is to use X as a compressed public key. - BigInteger prime = SecP256K1Curve.q; - if (x.compareTo(prime) >= 0) { - // Cannot have point co-ordinates larger than this as everything takes place modulo Q. - return null; - } - // Compressed keys require you to know an extra bit of data about the y-coord as there are - // two possibilities. So it's encoded in the recId. - ECPoint R = decompressKey(x, (recId & 1) == 1); - // 1.4. If nR != point at infinity, then do another iteration of Step 1 (callers - // responsibility). - if (!R.multiply(n).isInfinity()) { - return null; - } - // 1.5. Compute e from M using Steps 2 and 3 of ECDSA signature verification. - BigInteger e = new BigInteger(1, message); - // 1.6. For k from 1 to 2 do the following. (loop is outside this function via - // iterating recId) - // 1.6.1. Compute a candidate public key as: - // Q = mi(r) * (sR - eG) - // - // Where mi(x) is the modular multiplicative inverse. We transform this into the following: - // Q = (mi(r) * s ** R) + (mi(r) * -e ** G) - // Where -e is the modular additive inverse of e, that is z such that z + e = 0 (mod n). - // In the above equation ** is point multiplication and + is point addition (the EC group - // operator). - // - // We can find the additive inverse by subtracting e from zero then taking the mod. For - // example the additive inverse of 3 modulo 11 is 8 because 3 + 8 mod 11 = 0, and - // -3 mod 11 = 8. - BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n); - BigInteger rInv = sig.r.modInverse(n); - BigInteger srInv = rInv.multiply(sig.s).mod(n); - BigInteger eInvrInv = rInv.multiply(eInv).mod(n); - ECPoint q = ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv); - - byte[] qBytes = q.getEncoded(false); - // We remove the prefix - return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); + ECDSASignature sig = keyPair.sign(messageHash); + // Now we have to work backwards to figure out the recId needed to recover the signature. + int recId = -1; + for (int i = 0; i < 4; i++) { + BigInteger k = recoverFromSignature(i, sig, messageHash); + if (k != null && k.equals(publicKey)) { + recId = i; + break; + } } - - /** Decompress a compressed public key (x co-ord and low-bit of y-coord). */ - private static ECPoint decompressKey(BigInteger xBN, boolean yBit) { - X9IntegerConverter x9 = new X9IntegerConverter(); - byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve())); - compEnc[0] = (byte) (yBit ? 0x03 : 0x02); - return CURVE.getCurve().decodePoint(compEnc); + if (recId == -1) { + throw new RuntimeException( + "Could not construct a recoverable key. Are your credentials valid?"); } - /** - * Given an arbitrary piece of text and an Ethereum message signature encoded in bytes, returns - * the public key that was used to sign it. This can then be compared to the expected public key - * to determine if the signature was correct. - * - * @param message RLP encoded message. - * @param signatureData The message signature components - * @return the public key used to sign the message - * @throws SignatureException If the public key could not be recovered or if there was a - * signature format error. - */ - public static BigInteger signedMessageToKey(byte[] message, SignatureData signatureData) - throws SignatureException { - return signedMessageHashToKey(Hash.sha3(message), signatureData); + int headerByte = recId + 27; + + // 1 header + 32 bytes for R + 32 bytes for S + byte[] v = new byte[] {(byte) headerByte}; + byte[] r = Numeric.toBytesPadded(sig.r, 32); + byte[] s = Numeric.toBytesPadded(sig.s, 32); + + return new SignatureData(v, r, s); + } + + /** + * Given the components of a signature and a selector value, recover and return the public key + * that generated the signature according to the algorithm in SEC1v2 section 4.1.6. + * + *

The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the + * correct one. Because the key recovery operation yields multiple potential keys, the correct key + * must either be stored alongside the signature, or you must be willing to try each recId in turn + * until you find one that outputs the key you are expecting. + * + *

If this method returns null it means recovery was not possible and recId should be iterated. + * + *

Given the above two points, a correct usage of this method is inside a for loop from 0 to 3, + * and if the output is null OR a key that is not the one you expect, you try again with the next + * recId. + * + * @param recId Which possible key to recover. + * @param sig the R and S components of the signature, wrapped. + * @param message Hash of the data that was signed. + * @return An ECKey containing only the public part, or null if recovery wasn't possible. + */ + public static BigInteger recoverFromSignature(int recId, ECDSASignature sig, byte[] message) { + verifyPrecondition(recId >= 0, "recId must be positive"); + verifyPrecondition(sig.r.signum() >= 0, "r must be positive"); + verifyPrecondition(sig.s.signum() >= 0, "s must be positive"); + verifyPrecondition(message != null, "message cannot be null"); + + // 1.0 For j from 0 to h (h == recId here and the loop is outside this function) + // 1.1 Let x = r + jn + BigInteger n = CURVE.getN(); // Curve order. + BigInteger i = BigInteger.valueOf((long) recId / 2); + BigInteger x = sig.r.add(i.multiply(n)); + // 1.2. Convert the integer x to an octet string X of length mlen using the conversion + // routine specified in Section 2.3.7, where mlen = ⌈(log2 p)/8⌉ or mlen = ⌈m/8⌉. + // 1.3. Convert the octet string (16 set binary digits)||X to an elliptic curve point R + // using the conversion routine specified in Section 2.3.4. If this conversion + // routine outputs "invalid", then do another iteration of Step 1. + // + // More concisely, what these points mean is to use X as a compressed public key. + BigInteger prime = SecP256K1Curve.q; + if (x.compareTo(prime) >= 0) { + // Cannot have point co-ordinates larger than this as everything takes place modulo Q. + return null; + } + // Compressed keys require you to know an extra bit of data about the y-coord as there are + // two possibilities. So it's encoded in the recId. + ECPoint R = decompressKey(x, (recId & 1) == 1); + // 1.4. If nR != point at infinity, then do another iteration of Step 1 (callers + // responsibility). + if (!R.multiply(n).isInfinity()) { + return null; } + // 1.5. Compute e from M using Steps 2 and 3 of ECDSA signature verification. + BigInteger e = new BigInteger(1, message); + // 1.6. For k from 1 to 2 do the following. (loop is outside this function via + // iterating recId) + // 1.6.1. Compute a candidate public key as: + // Q = mi(r) * (sR - eG) + // + // Where mi(x) is the modular multiplicative inverse. We transform this into the following: + // Q = (mi(r) * s ** R) + (mi(r) * -e ** G) + // Where -e is the modular additive inverse of e, that is z such that z + e = 0 (mod n). + // In the above equation ** is point multiplication and + is point addition (the EC group + // operator). + // + // We can find the additive inverse by subtracting e from zero then taking the mod. For + // example the additive inverse of 3 modulo 11 is 8 because 3 + 8 mod 11 = 0, and + // -3 mod 11 = 8. + BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n); + BigInteger rInv = sig.r.modInverse(n); + BigInteger srInv = rInv.multiply(sig.s).mod(n); + BigInteger eInvrInv = rInv.multiply(eInv).mod(n); + ECPoint q = ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv); + + byte[] qBytes = q.getEncoded(false); + // We remove the prefix + return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); + } + + /** Decompress a compressed public key (x co-ord and low-bit of y-coord). */ + private static ECPoint decompressKey(BigInteger xBN, boolean yBit) { + X9IntegerConverter x9 = new X9IntegerConverter(); + byte[] compEnc = x9.integerToBytes(xBN, 1 + x9.getByteLength(CURVE.getCurve())); + compEnc[0] = (byte) (yBit ? 0x03 : 0x02); + return CURVE.getCurve().decodePoint(compEnc); + } + + /** + * Given an arbitrary piece of text and an Ethereum message signature encoded in bytes, returns + * the public key that was used to sign it. This can then be compared to the expected public key + * to determine if the signature was correct. + * + * @param message RLP encoded message. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a signature + * format error. + */ + public static BigInteger signedMessageToKey(byte[] message, SignatureData signatureData) + throws SignatureException { + return signedMessageHashToKey(Hash.sha3(message), signatureData); + } + + /** + * Given an arbitrary message and an Ethereum message signature encoded in bytes, returns the + * public key that was used to sign it. This can then be compared to the expected public key to + * determine if the signature was correct. + * + * @param message The message. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a signature + * format error. + */ + public static BigInteger signedPrefixedMessageToKey(byte[] message, SignatureData signatureData) + throws SignatureException { + return signedMessageHashToKey(getEthereumMessageHash(message), signatureData); + } + + /** + * Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns the + * public key that was used to sign it. This can then be compared to the expected public key to + * determine if the signature was correct. + * + * @param messageHash The message hash. + * @param signatureData The message signature components + * @return the public key used to sign the message + * @throws SignatureException If the public key could not be recovered or if there was a signature + * format error. + */ + public static BigInteger signedMessageHashToKey(byte[] messageHash, SignatureData signatureData) + throws SignatureException { + + byte[] r = signatureData.getR(); + byte[] s = signatureData.getS(); + verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); + verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); + + int header = signatureData.getV()[0] & 0xFF; + // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, + // 0x1D = second key with even y, 0x1E = second key with odd y + if (header < 27 || header > 34) { + throw new SignatureException("Header byte out of range: " + header); + } + + ECDSASignature sig = + new ECDSASignature( + new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())); - /** - * Given an arbitrary message and an Ethereum message signature encoded in bytes, returns the - * public key that was used to sign it. This can then be compared to the expected public key to - * determine if the signature was correct. - * - * @param message The message. - * @param signatureData The message signature components - * @return the public key used to sign the message - * @throws SignatureException If the public key could not be recovered or if there was a - * signature format error. + int recId = header - 27; + BigInteger key = recoverFromSignature(recId, sig, messageHash); + if (key == null) { + throw new SignatureException("Could not recover public key from signature"); + } + return key; + } + + /** + * Returns public key from the given private key. + * + * @param privKey the private key to derive the public key from + * @return BigInteger encoded public key + */ + public static BigInteger publicKeyFromPrivate(BigInteger privKey) { + ECPoint point = publicPointFromPrivate(privKey); + + byte[] encoded = point.getEncoded(false); + return new BigInteger(1, Arrays.copyOfRange(encoded, 1, encoded.length)); // remove prefix + } + + /** + * Returns public key point from the given private key. + * + * @param privKey the private key to derive the public key from + * @return ECPoint public key + */ + public static ECPoint publicPointFromPrivate(BigInteger privKey) { + /* + * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group + * order, but that could change in future versions. */ - public static BigInteger signedPrefixedMessageToKey(byte[] message, SignatureData signatureData) - throws SignatureException { - return signedMessageHashToKey(getEthereumMessageHash(message), signatureData); + if (privKey.bitLength() > CURVE.getN().bitLength()) { + privKey = privKey.mod(CURVE.getN()); + } + return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey); + } + + /** + * Returns public key point from the given curve. + * + * @param bits representing the point on the curve + * @return BigInteger encoded public key + */ + public static BigInteger publicFromPoint(byte[] bits) { + return new BigInteger(1, Arrays.copyOfRange(bits, 1, bits.length)); // remove prefix + } + + public static class SignatureData { + private final byte[] v; + private final byte[] r; + private final byte[] s; + + public SignatureData(byte v, byte[] r, byte[] s) { + this(new byte[] {v}, r, s); } - /** - * Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns - * the public key that was used to sign it. This can then be compared to the expected public key - * to determine if the signature was correct. - * - * @param messageHash The message hash. - * @param signatureData The message signature components - * @return the public key used to sign the message - * @throws SignatureException If the public key could not be recovered or if there was a - * signature format error. - */ - public static BigInteger signedMessageHashToKey(byte[] messageHash, SignatureData signatureData) - throws SignatureException { - - byte[] r = signatureData.getR(); - byte[] s = signatureData.getS(); - verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); - verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); - - int header = signatureData.getV()[0] & 0xFF; - // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, - // 0x1D = second key with even y, 0x1E = second key with odd y - if (header < 27 || header > 34) { - throw new SignatureException("Header byte out of range: " + header); - } - - ECDSASignature sig = - new ECDSASignature( - new BigInteger(1, signatureData.getR()), - new BigInteger(1, signatureData.getS())); - - int recId = header - 27; - BigInteger key = recoverFromSignature(recId, sig, messageHash); - if (key == null) { - throw new SignatureException("Could not recover public key from signature"); - } - return key; + public SignatureData(byte[] v, byte[] r, byte[] s) { + this.v = v; + this.r = r; + this.s = s; } - /** - * Returns public key from the given private key. - * - * @param privKey the private key to derive the public key from - * @return BigInteger encoded public key - */ - public static BigInteger publicKeyFromPrivate(BigInteger privKey) { - ECPoint point = publicPointFromPrivate(privKey); + public byte[] getV() { + return v; + } - byte[] encoded = point.getEncoded(false); - return new BigInteger(1, Arrays.copyOfRange(encoded, 1, encoded.length)); // remove prefix + public byte[] getR() { + return r; } - /** - * Returns public key point from the given private key. - * - * @param privKey the private key to derive the public key from - * @return ECPoint public key - */ - public static ECPoint publicPointFromPrivate(BigInteger privKey) { - /* - * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group - * order, but that could change in future versions. - */ - if (privKey.bitLength() > CURVE.getN().bitLength()) { - privKey = privKey.mod(CURVE.getN()); - } - return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey); + public byte[] getS() { + return s; } - /** - * Returns public key point from the given curve. - * - * @param bits representing the point on the curve - * @return BigInteger encoded public key - */ - public static BigInteger publicFromPoint(byte[] bits) { - return new BigInteger(1, Arrays.copyOfRange(bits, 1, bits.length)); // remove prefix + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + SignatureData that = (SignatureData) o; + + if (!Arrays.equals(v, that.v)) { + return false; + } + if (!Arrays.equals(r, that.r)) { + return false; + } + return Arrays.equals(s, that.s); } - public static class SignatureData { - private final byte[] v; - private final byte[] r; - private final byte[] s; - - public SignatureData(byte v, byte[] r, byte[] s) { - this(new byte[] {v}, r, s); - } - - public SignatureData(byte[] v, byte[] r, byte[] s) { - this.v = v; - this.r = r; - this.s = s; - } - - public byte[] getV() { - return v; - } - - public byte[] getR() { - return r; - } - - public byte[] getS() { - return s; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - SignatureData that = (SignatureData) o; - - if (!Arrays.equals(v, that.v)) { - return false; - } - if (!Arrays.equals(r, that.r)) { - return false; - } - return Arrays.equals(s, that.s); - } - - @Override - public int hashCode() { - int result = Arrays.hashCode(v); - result = 31 * result + Arrays.hashCode(r); - result = 31 * result + Arrays.hashCode(s); - return result; - } + @Override + public int hashCode() { + int result = Arrays.hashCode(v); + result = 31 * result + Arrays.hashCode(r); + result = 31 * result + Arrays.hashCode(s); + return result; } + } } diff --git a/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java b/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java index b3c0f5b9d3e..e2232209d19 100644 --- a/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java +++ b/p2p/src/main/java/org/web3j/exceptions/MessageDecodingException.java @@ -1,24 +1,28 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.exceptions; /** Encoding exception. */ public class MessageDecodingException extends RuntimeException { - public MessageDecodingException(String message) { - super(message); - } + public MessageDecodingException(String message) { + super(message); + } - public MessageDecodingException(String message, Throwable cause) { - super(message, cause); - } + public MessageDecodingException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java b/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java index c0f6662d7b0..953a031e45d 100644 --- a/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java +++ b/p2p/src/main/java/org/web3j/exceptions/MessageEncodingException.java @@ -1,24 +1,28 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.exceptions; /** Encoding exception. */ public class MessageEncodingException extends RuntimeException { - public MessageEncodingException(String message) { - super(message); - } + public MessageEncodingException(String message) { + super(message); + } - public MessageEncodingException(String message, Throwable cause) { - super(message, cause); - } + public MessageEncodingException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/p2p/src/main/java/org/web3j/utils/Assertions.java b/p2p/src/main/java/org/web3j/utils/Assertions.java index e1fb221f491..77f0b7ad651 100644 --- a/p2p/src/main/java/org/web3j/utils/Assertions.java +++ b/p2p/src/main/java/org/web3j/utils/Assertions.java @@ -1,29 +1,33 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.utils; /** Assertion utility functions. */ public class Assertions { - /** - * Verify that the provided precondition holds true. - * - * @param assertionResult assertion value - * @param errorMessage error message if precondition failure - */ - public static void verifyPrecondition(boolean assertionResult, String errorMessage) { - if (!assertionResult) { - throw new RuntimeException(errorMessage); - } + /** + * Verify that the provided precondition holds true. + * + * @param assertionResult assertion value + * @param errorMessage error message if precondition failure + */ + public static void verifyPrecondition(boolean assertionResult, String errorMessage) { + if (!assertionResult) { + throw new RuntimeException(errorMessage); } + } } diff --git a/p2p/src/main/java/org/web3j/utils/Numeric.java b/p2p/src/main/java/org/web3j/utils/Numeric.java index 377159da729..31fef2a2513 100644 --- a/p2p/src/main/java/org/web3j/utils/Numeric.java +++ b/p2p/src/main/java/org/web3j/utils/Numeric.java @@ -1,21 +1,24 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.utils; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; - import org.web3j.exceptions.MessageDecodingException; import org.web3j.exceptions.MessageEncodingException; @@ -26,227 +29,226 @@ */ public final class Numeric { - private static final String HEX_PREFIX = "0x"; + private static final String HEX_PREFIX = "0x"; - private Numeric() {} + private Numeric() {} - public static String encodeQuantity(BigInteger value) { - if (value.signum() != -1) { - return HEX_PREFIX + value.toString(16); - } else { - throw new MessageEncodingException("Negative values are not supported"); - } + public static String encodeQuantity(BigInteger value) { + if (value.signum() != -1) { + return HEX_PREFIX + value.toString(16); + } else { + throw new MessageEncodingException("Negative values are not supported"); } + } - public static BigInteger decodeQuantity(String value) { - if (isLongValue(value)) { - return BigInteger.valueOf(Long.parseLong(value)); - } - - if (!isValidHexQuantity(value)) { - throw new MessageDecodingException("Value must be in format 0x[1-9]+[0-9]* or 0x0"); - } - try { - return new BigInteger(value.substring(2), 16); - } catch (NumberFormatException e) { - throw new MessageDecodingException("Negative ", e); - } + public static BigInteger decodeQuantity(String value) { + if (isLongValue(value)) { + return BigInteger.valueOf(Long.parseLong(value)); } - private static boolean isLongValue(String value) { - try { - Long.parseLong(value); - return true; - } catch (NumberFormatException e) { - return false; - } + if (!isValidHexQuantity(value)) { + throw new MessageDecodingException("Value must be in format 0x[1-9]+[0-9]* or 0x0"); } - - private static boolean isValidHexQuantity(String value) { - if (value == null) { - return false; - } - - if (value.length() < 3) { - return false; - } - - if (!value.startsWith(HEX_PREFIX)) { - return false; - } - - // If TestRpc resolves the following issue, we can reinstate this code - // https://github.com/ethereumjs/testrpc/issues/220 - // if (value.length() > 3 && value.charAt(2) == '0') { - // return false; - // } - - return true; + try { + return new BigInteger(value.substring(2), 16); + } catch (NumberFormatException e) { + throw new MessageDecodingException("Negative ", e); } + } - public static String cleanHexPrefix(String input) { - if (containsHexPrefix(input)) { - return input.substring(2); - } else { - return input; - } + private static boolean isLongValue(String value) { + try { + Long.parseLong(value); + return true; + } catch (NumberFormatException e) { + return false; } + } - public static String prependHexPrefix(String input) { - if (!containsHexPrefix(input)) { - return HEX_PREFIX + input; - } else { - return input; - } + private static boolean isValidHexQuantity(String value) { + if (value == null) { + return false; } - public static boolean containsHexPrefix(String input) { - return !Strings.isEmpty(input) - && input.length() > 1 - && input.charAt(0) == '0' - && input.charAt(1) == 'x'; + if (value.length() < 3) { + return false; } - public static BigInteger toBigInt(byte[] value, int offset, int length) { - return toBigInt((Arrays.copyOfRange(value, offset, offset + length))); + if (!value.startsWith(HEX_PREFIX)) { + return false; } - public static BigInteger toBigInt(byte[] value) { - return new BigInteger(1, value); - } + // If TestRpc resolves the following issue, we can reinstate this code + // https://github.com/ethereumjs/testrpc/issues/220 + // if (value.length() > 3 && value.charAt(2) == '0') { + // return false; + // } - public static BigInteger toBigInt(String hexValue) { - String cleanValue = cleanHexPrefix(hexValue); - return toBigIntNoPrefix(cleanValue); - } + return true; + } - public static BigInteger toBigIntNoPrefix(String hexValue) { - return new BigInteger(hexValue, 16); + public static String cleanHexPrefix(String input) { + if (containsHexPrefix(input)) { + return input.substring(2); + } else { + return input; } + } - public static String toHexStringWithPrefix(BigInteger value) { - return HEX_PREFIX + value.toString(16); + public static String prependHexPrefix(String input) { + if (!containsHexPrefix(input)) { + return HEX_PREFIX + input; + } else { + return input; } + } - public static String toHexStringNoPrefix(BigInteger value) { - return value.toString(16); - } + public static boolean containsHexPrefix(String input) { + return !Strings.isEmpty(input) + && input.length() > 1 + && input.charAt(0) == '0' + && input.charAt(1) == 'x'; + } - public static String toHexStringNoPrefix(byte[] input) { - return toHexString(input, 0, input.length, false); - } + public static BigInteger toBigInt(byte[] value, int offset, int length) { + return toBigInt((Arrays.copyOfRange(value, offset, offset + length))); + } - public static String toHexStringWithPrefixZeroPadded(BigInteger value, int size) { - return toHexStringZeroPadded(value, size, true); - } + public static BigInteger toBigInt(byte[] value) { + return new BigInteger(1, value); + } - public static String toHexStringWithPrefixSafe(BigInteger value) { - String result = toHexStringNoPrefix(value); - if (result.length() < 2) { - result = Strings.zeros(1) + result; - } - return HEX_PREFIX + result; - } + public static BigInteger toBigInt(String hexValue) { + String cleanValue = cleanHexPrefix(hexValue); + return toBigIntNoPrefix(cleanValue); + } - public static String toHexStringNoPrefixZeroPadded(BigInteger value, int size) { - return toHexStringZeroPadded(value, size, false); - } + public static BigInteger toBigIntNoPrefix(String hexValue) { + return new BigInteger(hexValue, 16); + } - private static String toHexStringZeroPadded(BigInteger value, int size, boolean withPrefix) { - String result = toHexStringNoPrefix(value); + public static String toHexStringWithPrefix(BigInteger value) { + return HEX_PREFIX + value.toString(16); + } - int length = result.length(); - if (length > size) { - throw new UnsupportedOperationException( - "Value " + result + "is larger then length " + size); - } else if (value.signum() < 0) { - throw new UnsupportedOperationException("Value cannot be negative"); - } + public static String toHexStringNoPrefix(BigInteger value) { + return value.toString(16); + } - if (length < size) { - result = Strings.zeros(size - length) + result; - } + public static String toHexStringNoPrefix(byte[] input) { + return toHexString(input, 0, input.length, false); + } - if (withPrefix) { - return HEX_PREFIX + result; - } else { - return result; - } - } + public static String toHexStringWithPrefixZeroPadded(BigInteger value, int size) { + return toHexStringZeroPadded(value, size, true); + } - public static byte[] toBytesPadded(BigInteger value, int length) { - byte[] result = new byte[length]; - byte[] bytes = value.toByteArray(); + public static String toHexStringWithPrefixSafe(BigInteger value) { + String result = toHexStringNoPrefix(value); + if (result.length() < 2) { + result = Strings.zeros(1) + result; + } + return HEX_PREFIX + result; + } - int bytesLength; - int srcOffset; - if (bytes[0] == 0) { - bytesLength = bytes.length - 1; - srcOffset = 1; - } else { - bytesLength = bytes.length; - srcOffset = 0; - } + public static String toHexStringNoPrefixZeroPadded(BigInteger value, int size) { + return toHexStringZeroPadded(value, size, false); + } - if (bytesLength > length) { - throw new RuntimeException("Input is too large to put in byte array of size " + length); - } + private static String toHexStringZeroPadded(BigInteger value, int size, boolean withPrefix) { + String result = toHexStringNoPrefix(value); - int destOffset = length - bytesLength; - System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); - return result; + int length = result.length(); + if (length > size) { + throw new UnsupportedOperationException("Value " + result + "is larger then length " + size); + } else if (value.signum() < 0) { + throw new UnsupportedOperationException("Value cannot be negative"); } - public static byte[] hexStringToByteArray(String input) { - String cleanInput = cleanHexPrefix(input); + if (length < size) { + result = Strings.zeros(size - length) + result; + } - int len = cleanInput.length(); + if (withPrefix) { + return HEX_PREFIX + result; + } else { + return result; + } + } - if (len == 0) { - return new byte[] {}; - } + public static byte[] toBytesPadded(BigInteger value, int length) { + byte[] result = new byte[length]; + byte[] bytes = value.toByteArray(); - byte[] data; - int startIdx; - if (len % 2 != 0) { - data = new byte[(len / 2) + 1]; - data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); - startIdx = 1; - } else { - data = new byte[len / 2]; - startIdx = 0; - } + int bytesLength; + int srcOffset; + if (bytes[0] == 0) { + bytesLength = bytes.length - 1; + srcOffset = 1; + } else { + bytesLength = bytes.length; + srcOffset = 0; + } - for (int i = startIdx; i < len; i += 2) { - data[(i + 1) / 2] = - (byte) - ((Character.digit(cleanInput.charAt(i), 16) << 4) - + Character.digit(cleanInput.charAt(i + 1), 16)); - } - return data; + if (bytesLength > length) { + throw new RuntimeException("Input is too large to put in byte array of size " + length); } - public static String toHexString(byte[] input, int offset, int length, boolean withPrefix) { - StringBuilder stringBuilder = new StringBuilder(); - if (withPrefix) { - stringBuilder.append("0x"); - } - for (int i = offset; i < offset + length; i++) { - stringBuilder.append(String.format("%02x", input[i] & 0xFF)); - } + int destOffset = length - bytesLength; + System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); + return result; + } + + public static byte[] hexStringToByteArray(String input) { + String cleanInput = cleanHexPrefix(input); - return stringBuilder.toString(); + int len = cleanInput.length(); + + if (len == 0) { + return new byte[] {}; } - public static String toHexString(byte[] input) { - return toHexString(input, 0, input.length, true); + byte[] data; + int startIdx; + if (len % 2 != 0) { + data = new byte[(len / 2) + 1]; + data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); + startIdx = 1; + } else { + data = new byte[len / 2]; + startIdx = 0; } - public static byte asByte(int m, int n) { - return (byte) ((m << 4) | n); + for (int i = startIdx; i < len; i += 2) { + data[(i + 1) / 2] = + (byte) + ((Character.digit(cleanInput.charAt(i), 16) << 4) + + Character.digit(cleanInput.charAt(i + 1), 16)); } + return data; + } - public static boolean isIntegerValue(BigDecimal value) { - return value.signum() == 0 || value.scale() <= 0 || value.stripTrailingZeros().scale() <= 0; + public static String toHexString(byte[] input, int offset, int length, boolean withPrefix) { + StringBuilder stringBuilder = new StringBuilder(); + if (withPrefix) { + stringBuilder.append("0x"); + } + for (int i = offset; i < offset + length; i++) { + stringBuilder.append(String.format("%02x", input[i] & 0xFF)); } + + return stringBuilder.toString(); + } + + public static String toHexString(byte[] input) { + return toHexString(input, 0, input.length, true); + } + + public static byte asByte(int m, int n) { + return (byte) ((m << 4) | n); + } + + public static boolean isIntegerValue(BigDecimal value) { + return value.signum() == 0 || value.scale() <= 0 || value.stripTrailingZeros().scale() <= 0; + } } diff --git a/p2p/src/main/java/org/web3j/utils/Strings.java b/p2p/src/main/java/org/web3j/utils/Strings.java index e21628ab1d9..34733642389 100644 --- a/p2p/src/main/java/org/web3j/utils/Strings.java +++ b/p2p/src/main/java/org/web3j/utils/Strings.java @@ -1,15 +1,19 @@ /* * Copyright 2019 Web3 Labs Ltd. * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. */ + package org.web3j.utils; import java.util.List; @@ -17,42 +21,42 @@ /** String utility functions. */ public class Strings { - private Strings() {} + private Strings() {} - public static String toCsv(List src) { - // return src == null ? null : String.join(", ", src.toArray(new String[0])); - return join(src, ", "); - } + public static String toCsv(List src) { + // return src == null ? null : String.join(", ", src.toArray(new String[0])); + return join(src, ", "); + } - public static String join(List src, String delimiter) { - return src == null ? null : String.join(delimiter, src.toArray(new String[0])); - } + public static String join(List src, String delimiter) { + return src == null ? null : String.join(delimiter, src.toArray(new String[0])); + } - public static String capitaliseFirstLetter(String string) { - if (string == null || string.length() == 0) { - return string; - } else { - return string.substring(0, 1).toUpperCase() + string.substring(1); - } + public static String capitaliseFirstLetter(String string) { + if (string == null || string.length() == 0) { + return string; + } else { + return string.substring(0, 1).toUpperCase() + string.substring(1); } + } - public static String lowercaseFirstLetter(String string) { - if (string == null || string.length() == 0) { - return string; - } else { - return string.substring(0, 1).toLowerCase() + string.substring(1); - } + public static String lowercaseFirstLetter(String string) { + if (string == null || string.length() == 0) { + return string; + } else { + return string.substring(0, 1).toLowerCase() + string.substring(1); } + } - public static String zeros(int n) { - return repeat('0', n); - } + public static String zeros(int n) { + return repeat('0', n); + } - public static String repeat(char value, int n) { - return new String(new char[n]).replace("\0", String.valueOf(value)); - } + public static String repeat(char value, int n) { + return new String(new char[n]).replace("\0", String.valueOf(value)); + } - public static boolean isEmpty(String s) { - return s == null || s.length() == 0; - } + public static boolean isEmpty(String s) { + return s == null || s.length() == 0; + } } diff --git a/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java b/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java index bbd7fc20ec4..253651e7a99 100644 --- a/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java +++ b/p2p/src/test/java/org/tron/p2p/connection/ChannelManagerTest.java @@ -1,5 +1,8 @@ package org.tron.p2p.connection; +import java.lang.reflect.Field; +import java.net.InetAddress; +import java.net.InetSocketAddress; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Test; @@ -7,30 +10,26 @@ import org.tron.p2p.base.Parameter; import org.tron.p2p.connection.business.handshake.DisconnectCode; -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.net.InetSocketAddress; - @Slf4j(topic = "net") public class ChannelManagerTest { @Test - public synchronized void testGetConnectionNum() throws Exception{ + public synchronized void testGetConnectionNum() throws Exception { Channel c1 = new Channel(); InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100); - Field field = c1.getClass().getDeclaredField("inetAddress"); + Field field = c1.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c1, a1.getAddress()); Channel c2 = new Channel(); InetSocketAddress a2 = new InetSocketAddress("100.1.1.2", 100); - field = c2.getClass().getDeclaredField("inetAddress"); + field = c2.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c2, a2.getAddress()); Channel c3 = new Channel(); InetSocketAddress a3 = new InetSocketAddress("100.1.1.2", 99); - field = c3.getClass().getDeclaredField("inetAddress"); + field = c3.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c3, a3.getAddress()); @@ -55,12 +54,12 @@ public synchronized void testNotifyDisconnect() throws Exception { Channel c1 = new Channel(); InetSocketAddress a1 = new InetSocketAddress("100.1.1.1", 100); - Field field = c1.getClass().getDeclaredField("inetSocketAddress"); + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); field.setAccessible(true); field.set(c1, a1); InetAddress inetAddress = a1.getAddress(); - field = c1.getClass().getDeclaredField("inetAddress"); + field = c1.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c1, inetAddress); @@ -84,10 +83,10 @@ public synchronized void testProcessPeer() throws Exception { Channel c1 = new Channel(); InetSocketAddress a1 = new InetSocketAddress("100.1.1.2", 100); - Field field = c1.getClass().getDeclaredField("inetSocketAddress"); + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); field.setAccessible(true); field.set(c1, a1); - field = c1.getClass().getDeclaredField("inetAddress"); + field = c1.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c1, a1.getAddress()); @@ -101,10 +100,10 @@ public synchronized void testProcessPeer() throws Exception { Channel c2 = new Channel(); InetSocketAddress a2 = new InetSocketAddress("100.1.1.2", 99); - field = c2.getClass().getDeclaredField("inetSocketAddress"); + field = c2.getClass().getDeclaredField("inetSocketAddress"); field.setAccessible(true); field.set(c2, a2); - field = c2.getClass().getDeclaredField("inetAddress"); + field = c2.getClass().getDeclaredField("inetAddress"); field.setAccessible(true); field.set(c2, a2.getAddress()); diff --git a/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java b/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java index bd9849fbaea..9be2411a0c8 100644 --- a/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java +++ b/p2p/src/test/java/org/tron/p2p/connection/ConnPoolServiceTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.connection; - import java.lang.reflect.Field; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -39,8 +38,8 @@ private void clearChannels() { @Test public void getNodes_chooseHomeNode() { - InetSocketAddress localAddress = new InetSocketAddress(Parameter.p2pConfig.getIp(), - Parameter.p2pConfig.getPort()); + InetSocketAddress localAddress = + new InetSocketAddress(Parameter.p2pConfig.getIp(), Parameter.p2pConfig.getPort()); Set inetInUse = new HashSet<>(); inetInUse.add(localAddress); @@ -48,12 +47,10 @@ public void getNodes_chooseHomeNode() { connectableNodes.add(NodeManager.getHomeNode()); ConnPoolService connPoolService = new ConnPoolService(); - List nodes = connPoolService.getNodes(new HashSet<>(), inetInUse, connectableNodes, - 1); + List nodes = connPoolService.getNodes(new HashSet<>(), inetInUse, connectableNodes, 1); Assert.assertEquals(0, nodes.size()); - nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, - 1); + nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, 1); Assert.assertEquals(1, nodes.size()); } @@ -77,14 +74,14 @@ public void getNodes_orderByUpdateTimeDesc() throws Exception { connectableNodes.add(node2); ConnPoolService connPoolService = new ConnPoolService(); - List nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, - 2); + List nodes = + connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, 2); Assert.assertEquals(2, nodes.size()); Assert.assertTrue(nodes.get(0).getUpdateTime() > nodes.get(1).getUpdateTime()); int limit = 1; - List nodes2 = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, - limit); + List nodes2 = + connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, limit); Assert.assertEquals(limit, nodes2.size()); } @@ -99,8 +96,8 @@ public void getNodes_banNode() throws InterruptedException { connectableNodes.add(node); ConnPoolService connPoolService = new ConnPoolService(); - List nodes = connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, - 1); + List nodes = + connPoolService.getNodes(new HashSet<>(), new HashSet<>(), connectableNodes, 1); Assert.assertEquals(0, nodes.size()); Thread.sleep(2 * banTime); diff --git a/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java b/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java index 17ca0094d8d..e0a04251f9b 100644 --- a/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java +++ b/p2p/src/test/java/org/tron/p2p/connection/MessageTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.connection; - import static org.tron.p2p.base.Parameter.NETWORK_TIME_DIFF; import org.junit.Assert; @@ -76,8 +75,10 @@ public void testUnKnownType() { @Test public void testInvalidTime() { - KeepAliveMessage keepAliveMessage = Connect.KeepAliveMessage.newBuilder() - .setTimestamp(System.currentTimeMillis() + NETWORK_TIME_DIFF * 2).build(); + KeepAliveMessage keepAliveMessage = + Connect.KeepAliveMessage.newBuilder() + .setTimestamp(System.currentTimeMillis() + NETWORK_TIME_DIFF * 2) + .build(); try { PingMessage message = new PingMessage(keepAliveMessage.toByteArray()); Assert.assertFalse(message.valid()); diff --git a/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java b/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java index 6dfb5232039..c6bab7cf4ee 100644 --- a/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java +++ b/p2p/src/test/java/org/tron/p2p/connection/SocketTest.java @@ -29,7 +29,8 @@ public void init() { private boolean sendMessage(io.netty.channel.Channel nettyChannel, Message message) { AtomicBoolean sendSuccess = new AtomicBoolean(false); - nettyChannel.writeAndFlush(Unpooled.wrappedBuffer(message.getSendData())) + nettyChannel + .writeAndFlush(Unpooled.wrappedBuffer(message.getSendData())) .addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { sendSuccess.set(true); @@ -45,28 +46,28 @@ private boolean sendMessage(io.netty.channel.Channel nettyChannel, Message messa return sendSuccess.get(); } - //if we start handshake, we cannot connect with localhost, this test case will be invalid + // if we start handshake, we cannot connect with localhost, this test case will be invalid @Test public void testPeerServerAndPeerClient() throws InterruptedException { -// //wait some time until peer server thread starts at this port successfully -// Thread.sleep(500); -// Node serverNode = new Node(new InetSocketAddress(localIp, port)); -// -// //peer client try to connect peer server using random port -// io.netty.channel.Channel nettyChannel = ChannelManager.getPeerClient() -// .connectAsync(serverNode, false, false).channel(); -// -// while (true) { -// if (!nettyChannel.isActive()) { -// Thread.sleep(100); -// } else { -// System.out.println("send message test"); -// PingMessage pingMessage = new PingMessage(); -// boolean sendSuccess = sendMessage(nettyChannel, pingMessage); -// Assert.assertTrue(sendSuccess); -// break; -// } -// } + // //wait some time until peer server thread starts at this port successfully + // Thread.sleep(500); + // Node serverNode = new Node(new InetSocketAddress(localIp, port)); + // + // //peer client try to connect peer server using random port + // io.netty.channel.Channel nettyChannel = ChannelManager.getPeerClient() + // .connectAsync(serverNode, false, false).channel(); + // + // while (true) { + // if (!nettyChannel.isActive()) { + // Thread.sleep(100); + // } else { + // System.out.println("send message test"); + // PingMessage pingMessage = new PingMessage(); + // boolean sendSuccess = sendMessage(nettyChannel, pingMessage); + // Assert.assertTrue(sendSuccess); + // break; + // } + // } } @After diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java index b1b895e58bc..72c63dda2a3 100644 --- a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/KadServiceTest.java @@ -1,5 +1,6 @@ package org.tron.p2p.discover.protocol.kad; +import java.net.InetSocketAddress; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; @@ -10,8 +11,6 @@ import org.tron.p2p.discover.message.kad.PingMessage; import org.tron.p2p.discover.socket.UdpEvent; -import java.net.InetSocketAddress; - public class KadServiceTest { private static KadService kadService; @@ -39,14 +38,14 @@ public void test() { Assert.assertNotNull(nodeHandler); Assert.assertEquals(1, kadService.getAllNodes().size()); - UdpEvent event = new UdpEvent(new PingMessage(node2, kadService.getPublicHomeNode()), - new InetSocketAddress(node2.getHostV4(), node2.getPort())); + UdpEvent event = + new UdpEvent( + new PingMessage(node2, kadService.getPublicHomeNode()), + new InetSocketAddress(node2.getHostV4(), node2.getPort())); kadService.handleEvent(event); Assert.assertEquals(2, kadService.getAllNodes().size()); - } - @AfterClass public static void destroy() { kadService.close(); diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java index 215e4192e1f..4961c3b5108 100644 --- a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/NodeHandlerTest.java @@ -1,5 +1,8 @@ package org.tron.p2p.discover.protocol.kad; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.net.InetSocketAddress; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; @@ -10,10 +13,6 @@ import org.tron.p2p.discover.message.kad.PingMessage; import org.tron.p2p.discover.message.kad.PongMessage; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.net.InetSocketAddress; - public class NodeHandlerTest { private static KadService kadService; diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java index 5f52f036dc4..e29ab783b14 100644 --- a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeEntryTest.java @@ -1,13 +1,12 @@ package org.tron.p2p.discover.protocol.kad.table; +import java.net.InetSocketAddress; import org.junit.Assert; import org.junit.Test; import org.tron.p2p.discover.Node; import org.tron.p2p.utils.ByteArray; import org.tron.p2p.utils.NetUtil; -import java.net.InetSocketAddress; - public class NodeEntryTest { @Test public void test() throws InterruptedException { @@ -32,37 +31,44 @@ public void testDistance() { String hexRandomIdStr = ByteArray.toHexString(randomId); Assert.assertEquals(128, hexRandomIdStr.length()); - byte[] nodeId1 = ByteArray.fromHexString( - "0000000000000000000000000000000000000000000000000000000000000000" - + "0000000000000000000000000000000000000000000000000000000000000000"); - byte[] nodeId2 = ByteArray.fromHexString( - "a000000000000000000000000000000000000000000000000000000000000000" - + "0000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId1 = + ByteArray.fromHexString( + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId2 = + ByteArray.fromHexString( + "a000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); Assert.assertEquals(17, NodeEntry.distance(nodeId1, nodeId2)); - byte[] nodeId3 = ByteArray.fromHexString( - "0000800000000000000000000000000000000000000000000000000000000001" - + "0000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId3 = + ByteArray.fromHexString( + "0000800000000000000000000000000000000000000000000000000000000001" + + "0000000000000000000000000000000000000000000000000000000000000000"); Assert.assertEquals(1, NodeEntry.distance(nodeId1, nodeId3)); - byte[] nodeId4 = ByteArray.fromHexString( - "0000400000000000000000000000000000000000000000000000000000000000" - + "0000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId4 = + ByteArray.fromHexString( + "0000400000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000000"); Assert.assertEquals(0, NodeEntry.distance(nodeId1, nodeId4)); // => 0 - byte[] nodeId5 = ByteArray.fromHexString( - "0000200000000000000000000000000000000000000000000000000000000000" - + "4000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId5 = + ByteArray.fromHexString( + "0000200000000000000000000000000000000000000000000000000000000000" + + "4000000000000000000000000000000000000000000000000000000000000000"); Assert.assertEquals(-1, NodeEntry.distance(nodeId1, nodeId5)); // => 0 - byte[] nodeId6 = ByteArray.fromHexString( - "0000100000000000000000000000000000000000000000000000000000000000" - + "2000000000000000000000000000000000000000000000000000000000000000"); + byte[] nodeId6 = + ByteArray.fromHexString( + "0000100000000000000000000000000000000000000000000000000000000000" + + "2000000000000000000000000000000000000000000000000000000000000000"); Assert.assertEquals(-2, NodeEntry.distance(nodeId1, nodeId6)); // => 0 - byte[] nodeId7 = ByteArray.fromHexString( - "0000000000000000000000000000000000000000000000000000000000000000" - + "0000000000000000000000000000000000000000000000000000000000000001"); + byte[] nodeId7 = + ByteArray.fromHexString( + "0000000000000000000000000000000000000000000000000000000000000000" + + "0000000000000000000000000000000000000000000000000000000000000001"); Assert.assertEquals(-494, NodeEntry.distance(nodeId1, nodeId7)); // => 0 Assert.assertEquals(-495, NodeEntry.distance(nodeId1, nodeId1)); // => 0 diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java index fe0990f59f5..b9bb1603065 100644 --- a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/NodeTableTest.java @@ -1,16 +1,15 @@ package org.tron.p2p.discover.protocol.kad.table; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.tron.p2p.discover.Node; import org.tron.p2p.utils.NetUtil; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - public class NodeTableTest { private Node homeNode; @@ -46,9 +45,7 @@ public void test() { Assert.assertFalse(nodeList.isEmpty()); } - /** - * init nodes for test. - */ + /** init nodes for test. */ @Before public void init() { ids = new ArrayList<>(); @@ -165,7 +162,7 @@ public void containsTest() { @Test public void getBuckIdTest() { - Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); //id: 11100...000 + Node node = new Node(ids.get(0), ips[0], null, 18888, 18888); // id: 11100...000 nodeTable.addNode(node); NodeEntry nodeEntry = new NodeEntry(homeNode.getId(), node); Assert.assertEquals(13, nodeTable.getBucketId(nodeEntry)); @@ -181,15 +178,15 @@ public void getClosestNodes_nodesMoreThanBucketCapacity() throws Exception { nodeTable.addNode(nearNode); nodeTable.addNode(farNode); for (int i = 0; i < KademliaOptions.BUCKET_SIZE - 1; i++) { - //To control totally 17 nodes, however closest's capacity is 16 + // To control totally 17 nodes, however closest's capacity is 16 nodeTable.addNode(new Node(ids.get(i), ips[i], null, 18888, 18888)); TimeUnit.MILLISECONDS.sleep(10); } Assert.assertTrue(nodeTable.getBucketsCount() > 1); - //3 buckets, nearnode's distance is 252, far's is 255, others' are 253 + // 3 buckets, nearnode's distance is 252, far's is 255, others' are 253 List closest = nodeTable.getClosestNodes(homeNode.getId()); Assert.assertTrue(closest.contains(nearNode)); - //the farest node should be excluded + // the farest node should be excluded } @Test diff --git a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java index b1e6325a3e6..71031c27080 100644 --- a/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java +++ b/p2p/src/test/java/org/tron/p2p/discover/protocol/kad/table/TimeComparatorTest.java @@ -1,12 +1,11 @@ package org.tron.p2p.discover.protocol.kad.table; +import java.net.InetSocketAddress; import org.junit.Assert; import org.junit.Test; import org.tron.p2p.discover.Node; import org.tron.p2p.utils.NetUtil; -import java.net.InetSocketAddress; - public class TimeComparatorTest { @Test public void test() throws InterruptedException { @@ -18,6 +17,5 @@ public void test() throws InterruptedException { TimeComparator tc = new TimeComparator(); int result = tc.compare(ne1, ne2); Assert.assertEquals(1, result); - } } diff --git a/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java b/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java index 156844ea376..2a4d6034718 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/AlgorithmTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import com.google.protobuf.ByteString; import java.math.BigInteger; import java.security.SignatureException; @@ -12,7 +11,8 @@ public class AlgorithmTest { - public static String privateKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"; + public static String privateKey = + "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"; @Test public void testPublicKeyCompressAndUnCompress() { @@ -42,7 +42,8 @@ public void testSignatureAndVerify() { @Test public void testEncode32() { - String content = "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"; + String content = + "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"; String base32 = Algorithm.encode32(content.getBytes()); Assert.assertArrayEquals(content.getBytes(), Algorithm.decode32(base32)); @@ -57,7 +58,8 @@ public void testValidHash() { @Test public void testEncode64() { - String base64Sig = "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE"; + String base64Sig = + "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE"; byte[] decoded; try { decoded = Algorithm.decode64(base64Sig); @@ -66,7 +68,8 @@ public void testEncode64() { Assert.fail(); } - String base64Content = "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE="; + String base64Content = + "1eFfi7ggzTbtAldC1pfXPn5A3mZQwEdk0-ZwCKGhZbQn2E6zWodG7v06kFu8gjiCe6FvJo04BYvgKHtPJ5pX5wE="; decoded = Algorithm.decode64(base64Content); Assert.assertNotEquals(base64Content, Algorithm.encode64(decoded)); } @@ -78,7 +81,8 @@ public void testRecoverPublicKey() { builder.setLRoot(ByteString.copyFrom("FDXN3SN67NA5DKA4J2GOK7BVQI".getBytes())); builder.setSeq(3447); - //String eth_msg = "enrtree-root:v1 e=VXJIDGQECCIIYNY3GZEJSFSG6U l=FDXN3SN67NA5DKA4J2GOK7BVQI seq=3447"; + // String eth_msg = "enrtree-root:v1 e=VXJIDGQECCIIYNY3GZEJSFSG6U l=FDXN3SN67NA5DKA4J2GOK7BVQI + // seq=3447"; String msg = builder.toString(); byte[] sig = Algorithm.sigData(builder.toString(), privateKey); Assert.assertEquals(65, sig.length); diff --git a/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java b/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java index fff6e677181..42b1d35cf16 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java +++ b/p2p/src/test/java/org/tron/p2p/dns/AwsRoute53Test.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import java.net.UnknownHostException; import java.util.Arrays; import java.util.HashMap; @@ -23,69 +22,140 @@ public class AwsRoute53Test { public void testChangeSort() { Map existing = new HashMap<>(); - existing.put("n", new RecordSet(new String[] { - "tree-root-v1:CjoKGlVKQU9JQlMyUFlZMjJYUU1WRlNXT1RZSlhVEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgIEldBTE5aWHEyRkk5Ui1ubjdHQk9HdWJBRFVPakZ2MWp5TjZiUHJtSWNTNks0ZE0wc1dKMUwzT2paWFRGei1KcldDenZZVHJId2RMSTlUczRPZ2Q4TXlJUnM"}, - AwsClient.rootTTL)); - existing.put("2kfjogvxdqtxxugbh7gs7naaai.n", new RecordSet(new String[] { - "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", - "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"}, - 3333)); - existing.put("fdxn3sn67na5dka4j2gok7bvqi.n", + existing.put( + "n", + new RecordSet( + new String[] { + "tree-root-v1:CjoKGlVKQU9JQlMyUFlZMjJYUU1WRlNXT1RZ" + + "SlhVEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZR" + + "SRgIEldBTE5aWHEyRkk5Ui1ubjdHQk9HdWJBRFVPa" + + "kZ2MWp5TjZiUHJtSWNTNks0ZE0wc1dKMUwzT2paWF" + + "RGei1KcldDenZZVHJId2RMSTlUczRPZ2Q4TXlJUnM" + }, + AwsClient.rootTTL)); + existing.put( + "2kfjogvxdqtxxugbh7gs7naaai.n", + new RecordSet( + new String[] { + "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", + "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4" + }, + 3333)); + existing.put( + "fdxn3sn67na5dka4j2gok7bvqi.n", new RecordSet(new String[] {"tree-branch:"}, AwsClient.treeNodeTTL)); Map newRecords = new HashMap<>(); - newRecords.put("n", - "tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdCVlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVGc4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM"); - newRecords.put("c7hrfpf3blgf3yr4dy5kx3smbe.n", - "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org"); - newRecords.put("jwxydbpxywg6fx3gmdibfa6cj4.n", - "tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24"); - newRecords.put("2xs2367yhaxjfglzhvawlqd4zy.n", - "nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA"); - newRecords.put("h4fht4b454p6uxfd7jcyq5pwdy.n", - "nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI"); - newRecords.put("mhtdo6tmubria2xwg5ludack24.n", - "nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o"); + newRecords.put( + "n", + "tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdC" + + "VlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZR" + + "SRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM" + + "2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVG" + + "c4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM"); + newRecords.put( + "c7hrfpf3blgf3yr4dy5kx3smbe.n", + "tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YY" + + "HIUV7BVDQ5FDPRT2@morenodes.example.org"); + newRecords.put( + "jwxydbpxywg6fx3gmdibfa6cj4.n", + "tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY," + + "H4FHT4B454P6UXFD7JCYQ5PWDY," + + "MHTDO6TMUBRIA2XWG5LUDACK24"); + newRecords.put( + "2xs2367yhaxjfglzhvawlqd4zy.n", + "nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDp" + + "qRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3" + + "snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaEC" + + "C2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA"); + newRecords.put( + "h4fht4b454p6uxfd7jcyq5pwdy.n", + "nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1ra" + + "YQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr" + + "7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaEC" + + "jrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI"); + newRecords.put( + "mhtdo6tmubria2xwg5ludack24.n", + "nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZl" + + "bYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPf" + + "hcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMa" + + "ECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o"); AwsClient publish; try { - publish = new AwsClient("random1", "random2", "random3", - "us-east-1", new P2pConfig().getPublishConfig().getChangeThreshold()); + publish = + new AwsClient( + "random1", + "random2", + "random3", + "us-east-1", + new P2pConfig().getPublishConfig().getChangeThreshold()); } catch (DnsException e) { Assert.fail(); return; } List changes = publish.computeChanges("n", newRecords, existing); - Change[] wantChanges = new Change[] { - publish.newTXTChange(ChangeAction.CREATE, "2xs2367yhaxjfglzhvawlqd4zy.n", - AwsClient.treeNodeTTL, - "\"nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA\""), - publish.newTXTChange(ChangeAction.CREATE, "c7hrfpf3blgf3yr4dy5kx3smbe.n", - AwsClient.treeNodeTTL, - "\"tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPRT2@morenodes.example.org\""), - publish.newTXTChange(ChangeAction.CREATE, "h4fht4b454p6uxfd7jcyq5pwdy.n", - AwsClient.treeNodeTTL, - "\"nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1raYQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaECjrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI\""), - publish.newTXTChange(ChangeAction.CREATE, "jwxydbpxywg6fx3gmdibfa6cj4.n", - AwsClient.treeNodeTTL, - "\"tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY,H4FHT4B454P6UXFD7JCYQ5PWDY,MHTDO6TMUBRIA2XWG5LUDACK24\""), - publish.newTXTChange(ChangeAction.CREATE, "mhtdo6tmubria2xwg5ludack24.n", - AwsClient.treeNodeTTL, - "\"nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZlbYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPfhcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMaECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o\""), - - publish.newTXTChange(ChangeAction.UPSERT, "n", - AwsClient.rootTTL, - "\"tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdCVlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZRSRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVGc4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM\""), - - publish.newTXTChange(ChangeAction.DELETE, "2kfjogvxdqtxxugbh7gs7naaai.n", - 3333, - "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", - "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"), - publish.newTXTChange(ChangeAction.DELETE, "fdxn3sn67na5dka4j2gok7bvqi.n", - AwsClient.treeNodeTTL, - "tree-branch:") - }; + Change[] wantChanges = + new Change[] { + publish.newTXTChange( + ChangeAction.CREATE, + "2xs2367yhaxjfglzhvawlqd4zy.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDp" + + "qRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3" + + "snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaEC" + + "C2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA\""), + publish.newTXTChange( + ChangeAction.CREATE, + "c7hrfpf3blgf3yr4dy5kx3smbe.n", + AwsClient.treeNodeTTL, + "\"tree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3" + + "YYHIUV7BVDQ5FDPRT2@morenodes.example.org\""), + publish.newTXTChange( + ChangeAction.CREATE, + "h4fht4b454p6uxfd7jcyq5pwdy.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QAggRauloj2SDLtIHN1XBkvhFZ1vtf1ra" + + "YQp9TBW2RD5EEawDzbtSmlXUfnaHcvwOizhVYLtr" + + "7e6vw7NAf6mTuoCgmlkgnY0iXNlY3AyNTZrMaEC" + + "jrXI8TLNXU0f8cthpAMxEshUyQlK-AM0PW2wfrnacNI\""), + publish.newTXTChange( + ChangeAction.CREATE, + "jwxydbpxywg6fx3gmdibfa6cj4.n", + AwsClient.treeNodeTTL, + "\"tree-branch:2XS2367YHAXJFGLZHVAWLQD4ZY," + + "H4FHT4B454P6UXFD7JCYQ5PWDY," + + "MHTDO6TMUBRIA2XWG5LUDACK24\""), + publish.newTXTChange( + ChangeAction.CREATE, + "mhtdo6tmubria2xwg5ludack24.n", + AwsClient.treeNodeTTL, + "\"nodes:-HW4QLAYqmrwllBEnzWWs7I5Ev2IAs7x_dZl" + + "bYdRdMUx5EyKHDXp7AV5CkuPGUPdvbv1_Ms1CPf" + + "hcGCvSElSosZmyoqAgmlkgnY0iXNlY3AyNTZrMa" + + "ECriawHKWdDRk2xeZkrOXBQ0dfMFLHY4eENZwdufn1S1o\""), + publish.newTXTChange( + ChangeAction.UPSERT, + "n", + AwsClient.rootTTL, + "\"tree-root-v1:CjoKGkZEWE4zU042N05BNURLQTRKMkdPSzdC" + + "VlFJEhpGRFhOM1NONjdOQTVES0E0SjJHT0s3QlZR" + + "SRgJElc5aDU4d1cyajUzdlBMeHNBSGN1cDMtV0ZEM" + + "2lvZUk4SkJrZkdYSk93dmI0R0lHR01pQVAxRkJVVG" + + "c4bHlORERleXJkck9uSDdSbUNUUnJRVGxqUm9UaHM\""), + publish.newTXTChange( + ChangeAction.DELETE, + "2kfjogvxdqtxxugbh7gs7naaai.n", + 3333, + "nodes:-HW4QO1ml1DdXLeZLsUxewnthhUy8eROqkDyoMTyavfks9JlYQIlMFEUoM78PovJDPQrAkrb3LRJ-", + "vtrymDguKCOIAWAgmlkgnY0iXNlY3AyNTZrMaEDffaGfJzgGhUif1JqFruZlYmA31HzathLSWxfbq_QoQ4"), + publish.newTXTChange( + ChangeAction.DELETE, + "fdxn3sn67na5dka4j2gok7bvqi.n", + AwsClient.treeNodeTTL, + "tree-branch:") + }; Assert.assertEquals(wantChanges.length, changes.size()); for (int i = 0; i < changes.size(); i++) { @@ -101,9 +171,11 @@ public void testPublish() throws UnknownHostException { List nodeList = Arrays.asList(nodes); List enrList = Tree.merge(nodeList, new PublishConfig().getMaxMergeSize()); - String[] links = new String[] { - "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example1.org", - "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example2.org"}; + String[] links = + new String[] { + "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example1.org", + "tree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@example2.org" + }; List linkList = Arrays.asList(links); Tree tree = new Tree(); @@ -113,39 +185,40 @@ public void testPublish() throws UnknownHostException { Assert.fail(); } -// //warning: replace your key in the following section, or this test will fail -// AwsClient awsClient; -// try { -// awsClient = new AwsClient("replace your access key", -// "replace your access key secret", -// "replace your host zone id", -// Region.US_EAST_1); -// } catch (DnsException e) { -// Assert.fail(); -// return; -// } -// String domain = "replace with your domain"; -// try { -// awsClient.deploy(domain, tree); -// } catch (Exception e) { -// Assert.fail(); -// return; -// } -// -// BigInteger publicKeyInt = Algorithm.generateKeyPair(AlgorithmTest.privateKey).getPublicKey(); -// String puKeyCompress = Algorithm.compressPubKey(publicKeyInt); -// String base32Pubkey = Algorithm.encode32(ByteArray.fromHexString(puKeyCompress)); -// Client client = new Client(); -// -// Tree route53Tree = new Tree(); -// try { -// client.syncTree(Entry.linkPrefix + base32Pubkey + "@" + domain, null, -// route53Tree); -// } catch (Exception e) { -// Assert.fail(); -// return; -// } -// Assert.assertEquals(links.length, route53Tree.getLinksEntry().size()); -// Assert.assertEquals(nodes.length, route53Tree.getDnsNodes().size()); + // //warning: replace your key in the following section, or this test will fail + // AwsClient awsClient; + // try { + // awsClient = new AwsClient("replace your access key", + // "replace your access key secret", + // "replace your host zone id", + // Region.US_EAST_1); + // } catch (DnsException e) { + // Assert.fail(); + // return; + // } + // String domain = "replace with your domain"; + // try { + // awsClient.deploy(domain, tree); + // } catch (Exception e) { + // Assert.fail(); + // return; + // } + // + // BigInteger publicKeyInt = + // Algorithm.generateKeyPair(AlgorithmTest.privateKey).getPublicKey(); + // String puKeyCompress = Algorithm.compressPubKey(publicKeyInt); + // String base32Pubkey = Algorithm.encode32(ByteArray.fromHexString(puKeyCompress)); + // Client client = new Client(); + // + // Tree route53Tree = new Tree(); + // try { + // client.syncTree(Entry.linkPrefix + base32Pubkey + "@" + domain, null, + // route53Tree); + // } catch (Exception e) { + // Assert.fail(); + // return; + // } + // Assert.assertEquals(links.length, route53Tree.getLinksEntry().size()); + // Assert.assertEquals(nodes.length, route53Tree.getDnsNodes().size()); } } diff --git a/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java b/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java index 140d137787d..41e2c1d41f6 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/DnsNodeTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import com.google.protobuf.InvalidProtocolBufferException; import java.net.UnknownHostException; import java.util.Arrays; @@ -13,9 +12,10 @@ public class DnsNodeTest { @Test public void testCompressDnsNode() throws UnknownHostException, InvalidProtocolBufferException { - DnsNode[] nodes = new DnsNode[] { - new DnsNode(null, "192.168.0.1", null, 10000), - }; + DnsNode[] nodes = + new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + }; List nodeList = Arrays.asList(nodes); String enrContent = DnsNode.compress(nodeList); @@ -26,25 +26,26 @@ public void testCompressDnsNode() throws UnknownHostException, InvalidProtocolBu @Test public void testSortDnsNode() throws UnknownHostException { - DnsNode[] nodes = new DnsNode[] { - new DnsNode(null, "192.168.0.1", null, 10000), - new DnsNode(null, "192.168.0.2", null, 10000), - new DnsNode(null, "192.168.0.3", null, 10000), - new DnsNode(null, "192.168.0.4", null, 10000), - new DnsNode(null, "192.168.0.5", null, 10000), - new DnsNode(null, "192.168.0.6", null, 10001), - new DnsNode(null, "192.168.0.6", null, 10002), - new DnsNode(null, "192.168.0.6", null, 10003), - new DnsNode(null, "192.168.0.6", null, 10004), - new DnsNode(null, "192.168.0.6", null, 10005), - new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), - new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), - new DnsNode(null, null, "fe80::0001", 10000), - new DnsNode(null, null, "fe80::0002", 10000), - new DnsNode(null, null, "fe80::0002", 10001), - }; + DnsNode[] nodes = + new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10001), + new DnsNode(null, "192.168.0.6", null, 10002), + new DnsNode(null, "192.168.0.6", null, 10003), + new DnsNode(null, "192.168.0.6", null, 10004), + new DnsNode(null, "192.168.0.6", null, 10005), + new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), + new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), + new DnsNode(null, null, "fe80::0001", 10000), + new DnsNode(null, null, "fe80::0002", 10000), + new DnsNode(null, null, "fe80::0002", 10001), + }; List nodeList = Arrays.asList(nodes); - Collections.shuffle(nodeList); //random order + Collections.shuffle(nodeList); // random order Collections.sort(nodeList); for (int i = 0; i < nodeList.size(); i++) { Assert.assertTrue(nodes[i].equals(nodeList.get(i))); diff --git a/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java b/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java index 69cb448e439..2893e1f8a9d 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/LinkCacheTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import org.apache.commons.lang3.StringUtils; import org.junit.Assert; import org.junit.Test; diff --git a/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java b/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java index 43f5f11a434..61bfdae9df2 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/RandomTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import java.util.ArrayList; import java.util.List; import org.junit.Assert; @@ -16,9 +15,8 @@ public class RandomTest { public void testRandomIterator() { Parameter.p2pConfig = new P2pConfig(); List treeUrls = new ArrayList<>(); - treeUrls.add( - "tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); - //treeUrls.add( + treeUrls.add("tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); + // treeUrls.add( // "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@shasta.nftderby1.net"); Parameter.p2pConfig.setTreeUrls(treeUrls); diff --git a/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java b/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java index a11e83c32c3..490b50734bc 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/SyncTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import java.util.ArrayList; import java.util.List; import org.junit.Assert; @@ -17,8 +16,7 @@ public class SyncTest { public void testSync() { Parameter.p2pConfig = new P2pConfig(); List treeUrls = new ArrayList<>(); - treeUrls.add( - "tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); + treeUrls.add("tree://AKMQMNAJJBL73LXWPXDI4I5ZWWIZ4AWO34DWQ636QOBBXNFXH3LQS@nile.trondisco.net"); Parameter.p2pConfig.setTreeUrls(treeUrls); Client syncClient = new Client(); diff --git a/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java b/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java index 115f46c986e..ca64bab7813 100644 --- a/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java +++ b/p2p/src/test/java/org/tron/p2p/dns/TreeTest.java @@ -1,6 +1,5 @@ package org.tron.p2p.dns; - import com.google.protobuf.InvalidProtocolBufferException; import java.net.UnknownHostException; import java.util.ArrayList; @@ -21,22 +20,22 @@ public class TreeTest { public static DnsNode[] sampleNode() throws UnknownHostException { return new DnsNode[] { - new DnsNode(null, "192.168.0.1", null, 10000), - new DnsNode(null, "192.168.0.2", null, 10000), - new DnsNode(null, "192.168.0.3", null, 10000), - new DnsNode(null, "192.168.0.4", null, 10000), - new DnsNode(null, "192.168.0.5", null, 10000), - new DnsNode(null, "192.168.0.6", null, 10001), - new DnsNode(null, "192.168.0.6", null, 10002), - new DnsNode(null, "192.168.0.6", null, 10003), - new DnsNode(null, "192.168.0.6", null, 10004), - new DnsNode(null, "192.168.0.6", null, 10005), - new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), - new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), - new DnsNode(null, null, "fe80::0001", 10000), - new DnsNode(null, null, "fe80::0002", 10000), - new DnsNode(null, null, "fe80::0003", 10001), - new DnsNode(null, null, "fe80::0004", 10001), + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10001), + new DnsNode(null, "192.168.0.6", null, 10002), + new DnsNode(null, "192.168.0.6", null, 10003), + new DnsNode(null, "192.168.0.6", null, 10004), + new DnsNode(null, "192.168.0.6", null, 10005), + new DnsNode(null, "192.168.0.10", "fe80::0001", 10005), + new DnsNode(null, "192.168.0.10", "fe80::0002", 10005), + new DnsNode(null, null, "fe80::0001", 10000), + new DnsNode(null, null, "fe80::0002", 10000), + new DnsNode(null, null, "fe80::0003", 10001), + new DnsNode(null, null, "fe80::0004", 10001), }; } @@ -65,51 +64,49 @@ public void testMerge() throws UnknownHostException { public void testTreeBuild() throws UnknownHostException { int seq = 0; - DnsNode[] dnsNodes = new DnsNode[] { - new DnsNode(null, "192.168.0.1", null, 10000), - new DnsNode(null, "192.168.0.2", null, 10000), - new DnsNode(null, "192.168.0.3", null, 10000), - new DnsNode(null, "192.168.0.4", null, 10000), - new DnsNode(null, "192.168.0.5", null, 10000), - new DnsNode(null, "192.168.0.6", null, 10000), - new DnsNode(null, "192.168.0.7", null, 10000), - new DnsNode(null, "192.168.0.8", null, 10000), - new DnsNode(null, "192.168.0.9", null, 10000), - new DnsNode(null, "192.168.0.10", null, 10000), - - new DnsNode(null, "192.168.0.11", null, 10000), - new DnsNode(null, "192.168.0.12", null, 10000), - new DnsNode(null, "192.168.0.13", null, 10000), - new DnsNode(null, "192.168.0.14", null, 10000), - new DnsNode(null, "192.168.0.15", null, 10000), - new DnsNode(null, "192.168.0.16", null, 10000), - new DnsNode(null, "192.168.0.17", null, 10000), - new DnsNode(null, "192.168.0.18", null, 10000), - new DnsNode(null, "192.168.0.19", null, 10000), - new DnsNode(null, "192.168.0.20", null, 10000), - - new DnsNode(null, "192.168.0.21", null, 10000), - new DnsNode(null, "192.168.0.22", null, 10000), - new DnsNode(null, "192.168.0.23", null, 10000), - new DnsNode(null, "192.168.0.24", null, 10000), - new DnsNode(null, "192.168.0.25", null, 10000), - new DnsNode(null, "192.168.0.26", null, 10000), - new DnsNode(null, "192.168.0.27", null, 10000), - new DnsNode(null, "192.168.0.28", null, 10000), - new DnsNode(null, "192.168.0.29", null, 10000), - new DnsNode(null, "192.168.0.30", null, 10000), - - new DnsNode(null, "192.168.0.31", null, 10000), - new DnsNode(null, "192.168.0.32", null, 10000), - new DnsNode(null, "192.168.0.33", null, 10000), - new DnsNode(null, "192.168.0.34", null, 10000), - new DnsNode(null, "192.168.0.35", null, 10000), - new DnsNode(null, "192.168.0.36", null, 10000), - new DnsNode(null, "192.168.0.37", null, 10000), - new DnsNode(null, "192.168.0.38", null, 10000), - new DnsNode(null, "192.168.0.39", null, 10000), - new DnsNode(null, "192.168.0.40", null, 10000), - }; + DnsNode[] dnsNodes = + new DnsNode[] { + new DnsNode(null, "192.168.0.1", null, 10000), + new DnsNode(null, "192.168.0.2", null, 10000), + new DnsNode(null, "192.168.0.3", null, 10000), + new DnsNode(null, "192.168.0.4", null, 10000), + new DnsNode(null, "192.168.0.5", null, 10000), + new DnsNode(null, "192.168.0.6", null, 10000), + new DnsNode(null, "192.168.0.7", null, 10000), + new DnsNode(null, "192.168.0.8", null, 10000), + new DnsNode(null, "192.168.0.9", null, 10000), + new DnsNode(null, "192.168.0.10", null, 10000), + new DnsNode(null, "192.168.0.11", null, 10000), + new DnsNode(null, "192.168.0.12", null, 10000), + new DnsNode(null, "192.168.0.13", null, 10000), + new DnsNode(null, "192.168.0.14", null, 10000), + new DnsNode(null, "192.168.0.15", null, 10000), + new DnsNode(null, "192.168.0.16", null, 10000), + new DnsNode(null, "192.168.0.17", null, 10000), + new DnsNode(null, "192.168.0.18", null, 10000), + new DnsNode(null, "192.168.0.19", null, 10000), + new DnsNode(null, "192.168.0.20", null, 10000), + new DnsNode(null, "192.168.0.21", null, 10000), + new DnsNode(null, "192.168.0.22", null, 10000), + new DnsNode(null, "192.168.0.23", null, 10000), + new DnsNode(null, "192.168.0.24", null, 10000), + new DnsNode(null, "192.168.0.25", null, 10000), + new DnsNode(null, "192.168.0.26", null, 10000), + new DnsNode(null, "192.168.0.27", null, 10000), + new DnsNode(null, "192.168.0.28", null, 10000), + new DnsNode(null, "192.168.0.29", null, 10000), + new DnsNode(null, "192.168.0.30", null, 10000), + new DnsNode(null, "192.168.0.31", null, 10000), + new DnsNode(null, "192.168.0.32", null, 10000), + new DnsNode(null, "192.168.0.33", null, 10000), + new DnsNode(null, "192.168.0.34", null, 10000), + new DnsNode(null, "192.168.0.35", null, 10000), + new DnsNode(null, "192.168.0.36", null, 10000), + new DnsNode(null, "192.168.0.37", null, 10000), + new DnsNode(null, "192.168.0.38", null, 10000), + new DnsNode(null, "192.168.0.39", null, 10000), + new DnsNode(null, "192.168.0.40", null, 10000), + }; String[] enrs = new String[dnsNodes.length]; for (int i = 0; i < dnsNodes.length; i++) { @@ -122,10 +119,53 @@ public void testTreeBuild() throws UnknownHostException { String[] links = new String[] {}; String linkBranch0 = "tree-branch:"; - String enrBranch1 = "tree-branch:OX22LN2ZUGOPGIPGBUQH35KZU4,XTGCXXQHPK3VUZPQHC6CGJDR3Q,BQLJLB6P5CRXHI37BRVWBWWACY,X4FURUK4SHXW3GVE6XBO3DFD5Y,SIUYMSVBYYXCE6HVW5TSGOFKVQ,2RKY3FUYIQBV4TFIDU7S42EIEU,KSEEGRTUGR4GCCBQ4TYHAWDKME,YGWDS6F6KLTFCC7T3AMAJHXI2A,K4HMVDEHRKOGOFQZXBJ2PSVIMM,NLLRMPWOTS6SP4D7YLCQA42IQQ,BBDLEDOZYAX5CWM6GNAALRVUXY,7NMT4ZISY5F4U6B6CQML2C526E,NVDRYMFHIERJEVGW5TE7QEAS2A"; - String enrBranch2 = "tree-branch:5ELKMY4HVAV5CBY6KDMXWOFSN4,7PHYT72EXSZJ6MT2IQ7VGUFQHI,AM6BJFCERRNKBG4A5X3MORBDZU,2WOYKPVTNYAY3KVDTDY4CEVOJM,PW5BHSJMPEHVJKRF5QTRXQB4LU,IS4YMOJGD4XPODBAMHZOUTIVMI,NSEE5WE57FWG2EERXI5TBBD32E,GOLZDJTTQ7V2MO2BG45O3Q22XI,4VL7USGBWKW576WM4TX7XIXS4A,GZQSPHDZYS7FXURGOQU3RIDUK4,T7L645CJJKCQVQMUADDO44EGOM,ATPMZZZB4RGYKC6K7QDFC22WIE,57KNNYA4WOKVZAODRCFYK64MBA"; - String enrBranch3 = "tree-branch:BJF5S37KVATG2SYHO6M7APDCNU,OUB3BDKUZQWXXFX5OSF5JCB6BA,6JZEHDWM6WWQYIEYVZN5QVMUXA,LXNNOBVTTZBPD3N5VTOCPVG7JE,LMWLKDCBT2U3CGSHKR2PYJNV5I,K2SSCP4ZIF7TQI4MRVLELFAQQE,MKR7II3GYETKN7MSCUQOF6MBQ4,FBJ5VFCV37SGUOEYA2SPGO3TLA,6SHSDL7PJCJAER3OS53NYPNDFI,KYU2OQJBU6AU3KJFCUSLOJWKVE,3N6XKDWY3WTBOSBS22YPUAHCFQ,IPEWOISXUGOL7ORZIOXBD24SPI,PCGDGGVEQQQFL4U2FYRXVHVMUM"; - String enrBranch4 = "tree-branch:WHCXLEQB3467BFATRY5SMIV62M,LAHEXJDXOPZSS2TDVXTJACCB6Q,QR4HMFZU3STBJEXOZIXPDRQTGM,JZUKVXBOLBPXCELWIE5G6E6UUU"; + String enrBranch1 = + "tree-branch:OX22LN2ZUGOPGIPGBUQH35KZU4," + + "XTGCXXQHPK3VUZPQHC6CGJDR3Q," + + "BQLJLB6P5CRXHI37BRVWBWWACY," + + "X4FURUK4SHXW3GVE6XBO3DFD5Y," + + "SIUYMSVBYYXCE6HVW5TSGOFKVQ," + + "2RKY3FUYIQBV4TFIDU7S42EIEU," + + "KSEEGRTUGR4GCCBQ4TYHAWDKME," + + "YGWDS6F6KLTFCC7T3AMAJHXI2A," + + "K4HMVDEHRKOGOFQZXBJ2PSVIMM," + + "NLLRMPWOTS6SP4D7YLCQA42IQQ," + + "BBDLEDOZYAX5CWM6GNAALRVUXY," + + "7NMT4ZISY5F4U6B6CQML2C526E," + + "NVDRYMFHIERJEVGW5TE7QEAS2A"; + String enrBranch2 = + "tree-branch:5ELKMY4HVAV5CBY6KDMXWOFSN4," + + "7PHYT72EXSZJ6MT2IQ7VGUFQHI," + + "AM6BJFCERRNKBG4A5X3MORBDZU," + + "2WOYKPVTNYAY3KVDTDY4CEVOJM," + + "PW5BHSJMPEHVJKRF5QTRXQB4LU," + + "IS4YMOJGD4XPODBAMHZOUTIVMI," + + "NSEE5WE57FWG2EERXI5TBBD32E," + + "GOLZDJTTQ7V2MO2BG45O3Q22XI," + + "4VL7USGBWKW576WM4TX7XIXS4A," + + "GZQSPHDZYS7FXURGOQU3RIDUK4," + + "T7L645CJJKCQVQMUADDO44EGOM," + + "ATPMZZZB4RGYKC6K7QDFC22WIE," + + "57KNNYA4WOKVZAODRCFYK64MBA"; + String enrBranch3 = + "tree-branch:BJF5S37KVATG2SYHO6M7APDCNU," + + "OUB3BDKUZQWXXFX5OSF5JCB6BA," + + "6JZEHDWM6WWQYIEYVZN5QVMUXA," + + "LXNNOBVTTZBPD3N5VTOCPVG7JE," + + "LMWLKDCBT2U3CGSHKR2PYJNV5I," + + "K2SSCP4ZIF7TQI4MRVLELFAQQE," + + "MKR7II3GYETKN7MSCUQOF6MBQ4," + + "FBJ5VFCV37SGUOEYA2SPGO3TLA," + + "6SHSDL7PJCJAER3OS53NYPNDFI," + + "KYU2OQJBU6AU3KJFCUSLOJWKVE," + + "3N6XKDWY3WTBOSBS22YPUAHCFQ," + + "IPEWOISXUGOL7ORZIOXBD24SPI," + + "PCGDGGVEQQQFL4U2FYRXVHVMUM"; + String enrBranch4 = + "tree-branch:WHCXLEQB3467BFATRY5SMIV62M," + + "LAHEXJDXOPZSS2TDVXTJACCB6Q," + + "QR4HMFZU3STBJEXOZIXPDRQTGM," + + "JZUKVXBOLBPXCELWIE5G6E6UUU"; String[] branches = new String[] {linkBranch0, enrBranch1, enrBranch2, enrBranch3, enrBranch4}; @@ -150,8 +190,8 @@ public void testTreeBuild() throws UnknownHostException { node:-01 ~ node:-13 node:-14 ~ node:-26 node:-27 ~ node:-39 node:-40 */ - Assert.assertEquals(branchList.size() + enrList.size() + linkList.size(), - tree.getEntries().size()); + Assert.assertEquals( + branchList.size() + enrList.size() + linkList.size(), tree.getEntries().size()); Assert.assertEquals(branchList.size(), tree.getBranchesEntry().size()); Assert.assertEquals(enrList.size(), tree.getNodesEntry().size()); Assert.assertEquals(linkList.size(), tree.getLinksEntry().size()); @@ -174,7 +214,7 @@ public void testTreeBuild() throws UnknownHostException { @Test public void testGroupAndMerge() throws UnknownHostException { Random random = new Random(); - //simulate some nodes + // simulate some nodes int ipCount = 2000; int maxMergeSize = 5; List dnsNodes = new ArrayList<>(); @@ -182,8 +222,10 @@ public void testGroupAndMerge() throws UnknownHostException { int i = 0; while (i < ipCount) { i += 1; - String ip = String.format("%d.%d.%d.%d", random.nextInt(256), random.nextInt(256), - random.nextInt(256), random.nextInt(256)); + String ip = + String.format( + "%d.%d.%d.%d", + random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(256)); if (ipSet.contains(ip)) { continue; } @@ -207,8 +249,10 @@ public void testGroupAndMerge() throws UnknownHostException { i = 0; while (i < addCount) { i += 1; - String ip = String.format("%d.%d.%d.%d", random.nextInt(256), random.nextInt(256), - random.nextInt(256), random.nextInt(256)); + String ip = + String.format( + "%d.%d.%d.%d", + random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(256)); if (ipSet.contains(ip)) { continue; } @@ -224,7 +268,7 @@ public void testGroupAndMerge() throws UnknownHostException { Assert.assertTrue(enrSet3.size() < enrSet1.size()); Set enrSet4 = new HashSet<>(enrSet1); - enrSet4.removeAll(enrSet2); //enrSet1 - enrSet2 + enrSet4.removeAll(enrSet2); // enrSet1 - enrSet2 System.out.println("deleteSize:" + enrSet4.size()); Assert.assertTrue(enrSet4.size() < enrSet1.size()); diff --git a/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java b/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java index 09b563101d1..039225d2274 100644 --- a/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java +++ b/p2p/src/test/java/org/tron/p2p/example/DnsExample1.java @@ -1,6 +1,5 @@ package org.tron.p2p.example; - import static java.lang.Thread.sleep; import java.net.InetSocketAddress; @@ -27,7 +26,8 @@ public void startP2pService() { p2pService.start(config); // after start about 300 seconds, you can find following log: - // Trying to publish tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org + // Trying to publish + // tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org // that is your tree url. you can publish your tree url on any somewhere such as github. // for others, this url is a known tree url while (true) { @@ -88,12 +88,14 @@ private void initDnsPublishConfig(P2pConfig config) { publishConfig.setDnsDomain("nodes.example.org"); // if you know other tree urls, you can attach it. it is optional - String[] urls = new String[] { - "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example1.org", - "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example2.org",}; + String[] urls = + new String[] { + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example1.org", + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example2.org", + }; publishConfig.setKnownTreeUrls(Arrays.asList(urls)); - //add your api key of aws or aliyun + // add your api key of aws or aliyun publishConfig.setDnsType(DnsType.AwsRoute53); publishConfig.setAccessKeyId("your access key"); publishConfig.setAccessKeySecret("your access key secret"); @@ -103,8 +105,8 @@ private void initDnsPublishConfig(P2pConfig config) { // enable dns publish publishConfig.setDnsPublishEnable(true); - // enable publish, so your nodes can be automatically published on domain periodically and others can download them + // enable publish, so your nodes can be automatically published on domain periodically and + // others can download them config.setPublishConfig(publishConfig); } - } diff --git a/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java b/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java index 1eff4fd3991..2c34f8184ad 100644 --- a/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java +++ b/p2p/src/test/java/org/tron/p2p/example/DnsExample2.java @@ -1,6 +1,5 @@ package org.tron.p2p.example; - import java.net.InetSocketAddress; import java.util.Arrays; import java.util.HashMap; @@ -27,7 +26,7 @@ public void startP2pService() { // config p2p parameters P2pConfig config = new P2pConfig(); - //if you use dns discovery, you can use following config + // if you use dns discovery, you can use following config initDnsSyncConfig(config); // register p2p event handler @@ -84,12 +83,15 @@ public List getConnectableNodes() { } private void initDnsSyncConfig(P2pConfig config) { - // generally, discovery service is not needed if you only use dns nodes independently to establish tcp connections + // generally, discovery service is not needed if you only use dns nodes independently to + // establish tcp connections config.setDiscoverEnable(false); // config your known tree urls - String[] urls = new String[] { - "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org"}; + String[] urls = + new String[] { + "tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes.example.org" + }; config.setTreeUrls(Arrays.asList(urls)); } @@ -123,11 +125,9 @@ public void onMessage(Channel channel, byte[] data) { // todo } } - } private enum MessageTypes { - FIRST((byte) 0x00), TEST((byte) 0x01), diff --git a/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java b/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java index 32d90d4340a..183c8dda3ae 100644 --- a/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java +++ b/p2p/src/test/java/org/tron/p2p/example/ImportUsing.java @@ -152,11 +152,9 @@ public void onMessage(Channel channel, byte[] data) { // todo } } - } private enum MessageTypes { - FIRST((byte) 0x00), TEST((byte) 0x01), @@ -195,7 +193,5 @@ public TestMessage(byte[] data) { this.type = MessageTypes.TEST; this.data = data; } - } - } diff --git a/p2p/src/test/java/org/tron/p2p/example/StartApp.java b/p2p/src/test/java/org/tron/p2p/example/StartApp.java index 8a8208d15eb..ef4ef56ffa4 100644 --- a/p2p/src/test/java/org/tron/p2p/example/StartApp.java +++ b/p2p/src/test/java/org/tron/p2p/example/StartApp.java @@ -1,6 +1,5 @@ package org.tron.p2p.example; - import static java.lang.Thread.sleep; import java.net.InetAddress; @@ -75,8 +74,10 @@ public static void main(String[] args) { } if (Parameter.p2pConfig.getMinConnections() > Parameter.p2pConfig.getMaxConnections()) { - logger.error("Check maxConnections({}) >= minConnections({}) failed", - Parameter.p2pConfig.getMaxConnections(), Parameter.p2pConfig.getMinConnections()); + logger.error( + "Check maxConnections({}) >= minConnections({}) failed", + Parameter.p2pConfig.getMaxConnections(), + Parameter.p2pConfig.getMinConnections()); System.exit(0); } @@ -210,8 +211,7 @@ private void checkDnsOption(CommandLine cli) { if (cli.hasOption(configChangeThreshold)) { double changeThreshold = Double.parseDouble(cli.getOptionValue(configChangeThreshold)); if (changeThreshold >= 1.0) { - logger.error("Check {}, range between (0.0 ~ 1.0]", - configChangeThreshold); + logger.error("Check {}, range between (0.0 ~ 1.0]", configChangeThreshold); } else { publishConfig.setChangeThreshold(changeThreshold); } @@ -283,8 +283,8 @@ private void checkDnsOption(CommandLine cli) { private Options getKadOptions() { - Option opt1 = new Option("s", "seed-nodes", true, - "seed node(s), required, ip:port[,ip:port[...]]"); + Option opt1 = + new Option("s", "seed-nodes", true, "seed node(s), required, ip:port[,ip:port[...]]"); Option opt2 = new Option("t", "trust-ips", true, "trust ip(s), ip[,ip[...]]"); Option opt3 = new Option("a", "active-nodes", true, "active node(s), ip:port[,ip:port[...]]"); Option opt4 = new Option("M", "max-connection", true, "max connection number, int, default 50"); @@ -292,8 +292,9 @@ private Options getKadOptions() { Option opt6 = new Option("d", "discover", true, "enable p2p discover, 0/1, default 1"); Option opt7 = new Option("p", "port", true, "UDP & TCP port, int, default 18888"); Option opt8 = new Option("v", "version", true, "p2p version, int, default 1"); - Option opt9 = new Option("ma", "min-active-connection", true, - "min active connection number, int, default 2"); + Option opt9 = + new Option( + "ma", "min-active-connection", true, "min active connection number, int, default 2"); Option opt10 = new Option("h", "help", false, "print help message"); Options group = new Options(); @@ -311,8 +312,12 @@ private Options getKadOptions() { } private Options getDnsReadOption() { - Option opt = new Option("u", "url-schemes", true, - "dns url(s) to get nodes, url format tree://{pubkey}@{domain}, url[,url[...]]"); + Option opt = + new Option( + "u", + "url-schemes", + true, + "dns url(s) to get nodes, url format tree://{pubkey}@{domain}, url[,url[...]]"); Options group = new Options(); group.addOption(opt); return group; @@ -320,30 +325,76 @@ private Options getDnsReadOption() { private Options getDnsPublishOption() { Option opt1 = new Option(configPublish, configPublish, false, "enable dns publish"); - Option opt2 = new Option(null, configDnsPrivate, true, - "dns private key used to publish, required, hex string of length 64"); - Option opt3 = new Option(null, configKnownUrls, true, - "known dns urls to publish, url format tree://{pubkey}@{domain}, optional, url[,url[...]]"); - Option opt4 = new Option(null, configStaticNodes, true, - "static nodes to publish, if exist then nodes from kad will be ignored, optional, ip:port[,ip:port[...]]"); - Option opt5 = new Option(null, configDomain, true, - "dns domain to publish nodes, required, string"); - Option opt6 = new Option(null, configChangeThreshold, true, - "change threshold of add and delete to publish, optional, should be > 0 and < 1.0, default 0.1"); - Option opt7 = new Option(null, configMaxMergeSize, true, - "max merge size to merge node to a leaf node in dns tree, optional, should be [1~5], default 5"); - Option opt8 = new Option(null, configServerType, true, - "dns server to publish, required, only aws or aliyun is support"); - Option opt9 = new Option(null, configAccessId, true, - "access key id of aws or aliyun api, required, string"); - Option opt10 = new Option(null, configAccessSecret, true, - "access key secret of aws or aliyun api, required, string"); - Option opt11 = new Option(null, configAwsRegion, true, - "if server-type is aws, it's region of aws api, such as \"eu-south-1\", required, string"); - Option opt12 = new Option(null, configHostZoneId, true, - "if server-type is aws, it's host zone id of aws's domain, optional, string"); - Option opt13 = new Option(null, configAliEndPoint, true, - "if server-type is aliyun, it's endpoint of aws dns server, required, string"); + Option opt2 = + new Option( + null, + configDnsPrivate, + true, + "dns private key used to publish, required, hex string of length 64"); + Option opt3 = + new Option( + null, + configKnownUrls, + true, + "known dns urls to publish, url format tree://{pubkey}@{domain}, optional," + + " url[,url[...]]"); + Option opt4 = + new Option( + null, + configStaticNodes, + true, + "static nodes to publish, if exist then nodes from kad will be ignored, optional," + + " ip:port[,ip:port[...]]"); + Option opt5 = + new Option(null, configDomain, true, "dns domain to publish nodes, required, string"); + Option opt6 = + new Option( + null, + configChangeThreshold, + true, + "change threshold of add and delete to publish, optional, should be > 0 and < 1.0," + + " default 0.1"); + Option opt7 = + new Option( + null, + configMaxMergeSize, + true, + "max merge size to merge node to a leaf node in dns tree, optional, should be [1~5]," + + " default 5"); + Option opt8 = + new Option( + null, + configServerType, + true, + "dns server to publish, required, only aws or aliyun is support"); + Option opt9 = + new Option( + null, configAccessId, true, "access key id of aws or aliyun api, required, string"); + Option opt10 = + new Option( + null, + configAccessSecret, + true, + "access key secret of aws or aliyun api, required, string"); + Option opt11 = + new Option( + null, + configAwsRegion, + true, + "if server-type is aws, it's region of aws api, such as \"eu-south-1\", required," + + " string"); + Option opt12 = + new Option( + null, + configHostZoneId, + true, + "if server-type is aws, it's host zone id of aws's domain, optional, string"); + Option opt13 = + new Option( + null, + configAliEndPoint, + true, + "if server-type is aliyun, it's endpoint of aws dns server, required, string"); Options group = new Options(); group.addOption(opt1); @@ -362,8 +413,8 @@ private Options getDnsPublishOption() { return group; } - private void printHelpMessage(Options kadOptions, Options dnsReadOptions, - Options dnsPublishOptions) { + private void printHelpMessage( + Options kadOptions, Options dnsReadOptions, Options dnsPublishOptions) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("available p2p discovery cli options:", kadOptions); helpFormatter.setSyntaxPrefix("\n"); diff --git a/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java b/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java index 626d0480b56..879e8dcf5f1 100644 --- a/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java +++ b/p2p/src/test/java/org/tron/p2p/utils/ByteArrayTest.java @@ -4,7 +4,6 @@ import org.junit.Test; import org.tron.p2p.dns.update.AwsClient; - public class ByteArrayTest { @Test @@ -14,13 +13,13 @@ public void testHexToString() { } @Test - public void testSubdomain(){ - Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com","abc.com")); - Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.","abc.com")); - Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com","abc.com.")); - Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.","abc.com.")); + public void testSubdomain() { + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com", "abc.com")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.", "abc.com")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com", "abc.com.")); + Assert.assertTrue(AwsClient.isSubdomain("cde.abc.com.", "abc.com.")); - Assert.assertFalse(AwsClient.isSubdomain("a-sub.abc.com","sub.abc.com")); - Assert.assertTrue(AwsClient.isSubdomain(".sub.abc.com","sub.abc.com")); + Assert.assertFalse(AwsClient.isSubdomain("a-sub.abc.com", "sub.abc.com")); + Assert.assertTrue(AwsClient.isSubdomain(".sub.abc.com", "sub.abc.com")); } } diff --git a/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java index f78f8faf41e..3fd5b12800b 100644 --- a/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java +++ b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java @@ -49,8 +49,7 @@ public void testValidNode() { @Test public void testGetNode() { - Discover.Endpoint endpoint = Discover.Endpoint.newBuilder() - .setPort(100).build(); + Discover.Endpoint endpoint = Discover.Endpoint.newBuilder().setPort(100).build(); Node node = NetUtil.getNode(endpoint); Assert.assertEquals(100, node.getPort()); } @@ -80,8 +79,10 @@ public void testExternalIp() { @Test public void testGetIP() { - //notice: please check that you only have one externalIP - String ip1 = null, ip2 = null, ip3 = null; + // notice: please check that you only have one externalIP + String ip1 = null; + String ip2 = null; + String ip3 = null; try { Method method = NetUtil.class.getDeclaredMethod("getExternalIp", String.class, boolean.class); method.setAccessible(true); @@ -119,87 +120,92 @@ public void testGetLanIP() { public void testIPv6Format() { String std = "fe80:0:0:0:204:61ff:fe9d:f156"; int randomPort = 10001; - String ip1 = new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:fe9d:f156", - randomPort).getAddress().getHostAddress(); + String ip1 = + new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:fe9d:f156", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip1, std); - String ip2 = new InetSocketAddress("fe80::204:61ff:fe9d:f156", - randomPort).getAddress().getHostAddress(); + String ip2 = + new InetSocketAddress("fe80::204:61ff:fe9d:f156", randomPort).getAddress().getHostAddress(); Assert.assertEquals(ip2, std); - String ip3 = new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:254.157.241.86", - randomPort).getAddress().getHostAddress(); + String ip3 = + new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:254.157.241.86", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip3, std); - String ip4 = new InetSocketAddress("fe80:0:0:0:0204:61ff:254.157.241.86", - randomPort).getAddress().getHostAddress(); + String ip4 = + new InetSocketAddress("fe80:0:0:0:0204:61ff:254.157.241.86", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip4, std); - String ip5 = new InetSocketAddress("fe80::204:61ff:254.157.241.86", - randomPort).getAddress().getHostAddress(); + String ip5 = + new InetSocketAddress("fe80::204:61ff:254.157.241.86", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip5, std); - String ip6 = new InetSocketAddress("FE80::204:61ff:254.157.241.86", - randomPort).getAddress().getHostAddress(); + String ip6 = + new InetSocketAddress("FE80::204:61ff:254.157.241.86", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip6, std); - String ip7 = new InetSocketAddress("[fe80:0:0:0:204:61ff:fe9d:f156]", - randomPort).getAddress().getHostAddress(); + String ip7 = + new InetSocketAddress("[fe80:0:0:0:204:61ff:fe9d:f156]", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip7, std); } @Test public void testParseIpv6() { - InetSocketAddress address1 = NetUtil.parseInetSocketAddress( - "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + InetSocketAddress address1 = + NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); Assert.assertNotNull(address1); Assert.assertEquals(18888, address1.getPort()); - Assert.assertEquals("2600:1f13:908:1b00:e1fd:5a84:251c:a32a", - address1.getAddress().getHostAddress()); + Assert.assertEquals( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a", address1.getAddress().getHostAddress()); try { - NetUtil.parseInetSocketAddress( - "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:abcd"); + NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:abcd"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress( - "2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress( - "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress( - "2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress( - "2600:1f13:908:1b00:e1fd:5a84:251c:a32a"); + NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } - InetSocketAddress address5 = NetUtil.parseInetSocketAddress( - "192.168.0.1:18888"); + InetSocketAddress address5 = NetUtil.parseInetSocketAddress("192.168.0.1:18888"); Assert.assertNotNull(address5); } - } diff --git a/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java b/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java index 0892b6ee244..572faae7df4 100644 --- a/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java +++ b/p2p/src/test/java/org/tron/p2p/utils/ProtoUtilTest.java @@ -19,7 +19,6 @@ public void testCompressMessage() throws Exception { Assert.assertTrue(p1.getTimeStamp() == p2.getTimeStamp()); - Connect.CompressMessage m2 = ProtoUtil.compressMessage(new byte[1000]); byte[] d2 = ProtoUtil.uncompressMessage(m2); From 6a794964815b0055cec3401320a507dd74d7172e Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 08:32:34 +0800 Subject: [PATCH 5/6] fix(p2p): address review findings for dependency and API compatibility - 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 --- gradle/verification-metadata.xml | 3 +++ p2p/.gitignore | 2 ++ p2p/build.gradle | 23 +++++-------------- .../business/detect/NodeDetectService.java | 2 +- .../business/keepalive/KeepAliveService.java | 2 +- .../business/pool/ConnPoolService.java | 4 ++-- .../p2p/connection/socket/PeerClient.java | 2 +- .../p2p/connection/socket/PeerServer.java | 5 ++-- .../discover/protocol/kad/DiscoverTask.java | 2 +- .../p2p/discover/protocol/kad/KadService.java | 2 +- .../java/org/tron/p2p/dns/sync/Client.java | 2 +- .../tron/p2p/dns/update/PublishService.java | 2 +- .../main/java/org/tron/p2p/utils/NetUtil.java | 2 +- 13 files changed, 24 insertions(+), 29 deletions(-) create mode 100644 p2p/.gitignore diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 6da91eca3c1..74d32c794e3 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -372,6 +372,9 @@ + + + diff --git a/p2p/.gitignore b/p2p/.gitignore new file mode 100644 index 00000000000..ebb224e762b --- /dev/null +++ b/p2p/.gitignore @@ -0,0 +1,2 @@ +# protobuf generated code (rebuilt by ./gradlew :p2p:generateProto) +src/main/java/org/tron/p2p/protos/ diff --git a/p2p/build.gradle b/p2p/build.gradle index eb85287c672..2b97d05c4f5 100644 --- a/p2p/build.gradle +++ b/p2p/build.gradle @@ -13,7 +13,6 @@ checkstyleMain { } def protobufVersion = '3.25.8' -def grpcVersion = '1.75.0' sourceSets { main { @@ -30,8 +29,9 @@ dependencies { // protobuf & grpc (implementation scope: not leaked to consumers) implementation "com.google.protobuf:protobuf-java:${protobufVersion}" implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}" - implementation "io.grpc:grpc-netty:${grpcVersion}" - implementation "io.grpc:grpc-core:${grpcVersion}" + // grpc-netty provides Netty transitively, which p2p uses for TCP/UDP transport. + // grpc itself is not used (p2p protos define only messages, no services). + implementation "io.grpc:grpc-netty:1.75.0" // p2p-specific dependencies implementation 'org.xerial.snappy:snappy-java:1.1.10.5' @@ -58,10 +58,9 @@ dependencies { exclude group: 'xpp3', module: 'xpp3' } - // commons-lang3: libp2p uses BasicThreadFactory.builder() which requires 3.12+. - // Root build.gradle provides 3.4 globally but as 'implementation' (not on compile classpath). - // Declare 3.18.0 (matching original libp2p) to ensure API compatibility. - implementation 'org.apache.commons:commons-lang3:3.18.0' + // commons-lang3: root provides 3.4 as 'implementation' (not on compile classpath). + // Re-declare here so p2p can compile. Uses 3.4-compatible API (new Builder() not builder()). + implementation 'org.apache.commons:commons-lang3:3.4' // provided by root build.gradle for all subprojects: // slf4j-api, logback, bcprov-jdk18on, lombok, junit, mockito @@ -72,22 +71,12 @@ protobuf { protoc { artifact = "com.google.protobuf:protoc:${protobufVersion}" } - plugins { - grpc { - artifact = "io.grpc:protoc-gen-grpc-java:${rootProject.archInfo.requires.ProtocGenVersion}" - } - } generateProtoTasks { all().each { task -> task.builtins { java { outputSubDir = "java" } } } - all()*.plugins { - grpc { - outputSubDir = "java" - } - } } } diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java index 091b9a5f964..d294b46a56c 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java @@ -37,7 +37,7 @@ public class NodeDetectService implements MessageProcess { private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("nodeDetectService").build()); + new BasicThreadFactory.Builder().namingPattern("nodeDetectService").build()); private final long NODE_DETECT_THRESHOLD = 5 * 60 * 1000; diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java index 62fc3833b9e..9952e8fdecc 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/keepalive/KeepAliveService.java @@ -22,7 +22,7 @@ public class KeepAliveService implements MessageProcess { private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("keepAlive").build()); + new BasicThreadFactory.Builder().namingPattern("keepAlive").build()); public void init() { executor.scheduleWithFixedDelay( diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java index 126df7b83da..1ce6cf41a08 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java @@ -54,10 +54,10 @@ public class ConnPoolService extends P2pEventHandler { @Getter private final AtomicInteger connectingPeersCount = new AtomicInteger(0); private final ScheduledThreadPoolExecutor poolLoopExecutor = new ScheduledThreadPoolExecutor( - 1, BasicThreadFactory.builder().namingPattern("connPool").build()); + 1, new BasicThreadFactory.Builder().namingPattern("connPool").build()); private final ScheduledExecutorService disconnectExecutor = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("randomDisconnect").build()); + new BasicThreadFactory.Builder().namingPattern("randomDisconnect").build()); public P2pConfig p2pConfig = Parameter.p2pConfig; private PeerClient peerClient; diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java index 55659b31746..cba3d69e0cc 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerClient.java @@ -24,7 +24,7 @@ public class PeerClient { public void init() { workerGroup = new NioEventLoopGroup( - 0, BasicThreadFactory.builder().namingPattern("peerClient-%d").build()); + 0, new BasicThreadFactory.Builder().namingPattern("peerClient-%d").build()); } public void close() { diff --git a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java index 6b58632e4e6..4edd1fbd556 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java +++ b/p2p/src/main/java/org/tron/p2p/connection/socket/PeerServer.java @@ -38,12 +38,13 @@ public void close() { public void start(int port) { EventLoopGroup bossGroup = - new NioEventLoopGroup(1, BasicThreadFactory.builder().namingPattern("peerBoss").build()); + new NioEventLoopGroup( + 1, new BasicThreadFactory.Builder().namingPattern("peerBoss").build()); // if threads = 0, it is number of core * 2 EventLoopGroup workerGroup = new NioEventLoopGroup( Parameter.TCP_NETTY_WORK_THREAD_NUM, - BasicThreadFactory.builder().namingPattern("peerWorker-%d").build()); + new BasicThreadFactory.Builder().namingPattern("peerWorker-%d").build()); P2pChannelInitializer p2pChannelInitializer = new P2pChannelInitializer("", false, true); try { ServerBootstrap b = new ServerBootstrap(); diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java index 021788d4110..f347102ffe7 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/DiscoverTask.java @@ -16,7 +16,7 @@ public class DiscoverTask { private ScheduledExecutorService discoverer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("discoverTask").build()); + new BasicThreadFactory.Builder().namingPattern("discoverTask").build()); private KadService kadService; diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java index 161dd6d13ea..a030f913078 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/KadService.java @@ -56,7 +56,7 @@ public void init() { } this.pongTimer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("pongTimer").build()); + new BasicThreadFactory.Builder().namingPattern("pongTimer").build()); this.homeNode = new Node( Parameter.p2pConfig.getNodeID(), diff --git a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java index d96560c1976..1f9e045bed3 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java +++ b/p2p/src/main/java/org/tron/p2p/dns/sync/Client.java @@ -40,7 +40,7 @@ public class Client { private final ScheduledExecutorService syncer = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("dnsSyncer").build()); + new BasicThreadFactory.Builder().namingPattern("dnsSyncer").build()); public Client() { this.cache = CacheBuilder.newBuilder().maximumSize(cacheLimit).recordStats().build(); diff --git a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java index 26780a3c311..3ada0f19031 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java +++ b/p2p/src/main/java/org/tron/p2p/dns/update/PublishService.java @@ -26,7 +26,7 @@ public class PublishService { private ScheduledExecutorService publisher = Executors.newSingleThreadScheduledExecutor( - BasicThreadFactory.builder().namingPattern("publishService").build()); + new BasicThreadFactory.Builder().namingPattern("publishService").build()); private Publish publish; public void init() { diff --git a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java index b63b8bbf114..b751680494c 100644 --- a/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java +++ b/p2p/src/main/java/org/tron/p2p/utils/NetUtil.java @@ -253,7 +253,7 @@ private static String getIp(List multiSrcUrls, boolean isAskIpv4) { int threadSize = multiSrcUrls.size(); ExecutorService executor = Executors.newFixedThreadPool( - threadSize, BasicThreadFactory.builder().namingPattern("getIp-%d").build()); + threadSize, new BasicThreadFactory.Builder().namingPattern("getIp-%d").build()); CompletionService completionService = new ExecutorCompletionService<>(executor); for (String url : multiSrcUrls) { From c54a7dc49729d4494466ef2ee5d08b7fe6d37663 Mon Sep 17 00:00:00 2001 From: Barbatos Date: Mon, 6 Apr 2026 21:31:01 +0800 Subject: [PATCH 6/6] fix(p2p): replace Math with StrictMath and fix flaky network tests - 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 --- .../business/detect/NodeDetectService.java | 2 +- .../business/pool/ConnPoolService.java | 2 +- .../protocol/kad/table/NodeEntry.java | 2 +- .../protocol/kad/table/NodeTable.java | 2 +- .../main/java/org/tron/p2p/dns/tree/Tree.java | 2 +- .../java/org/tron/p2p/utils/NetUtilTest.java | 183 ++++++++++++------ 6 files changed, 125 insertions(+), 68 deletions(-) diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java index d294b46a56c..fa68fbfc875 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/detect/NodeDetectService.java @@ -98,7 +98,7 @@ public void work() { n = MAX_NODE_SLOW_DETECT; } - n = Math.min(n, nodeStats.size()); + n = StrictMath.min(n, nodeStats.size()); for (int i = 0; i < n; i++) { detect(nodeStats.get(i)); diff --git a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java index 1ce6cf41a08..4a4e4f227e9 100644 --- a/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java +++ b/p2p/src/main/java/org/tron/p2p/connection/business/pool/ConnPoolService.java @@ -160,7 +160,7 @@ private void connect(boolean isFilterActiveNodes) { // calculate lackSize exclude config activeNodes int activeLackSize = p2pConfig.getMinActiveConnections() - connectingPeersCount.get(); int size = - Math.max( + StrictMath.max( p2pConfig.getMinConnections() - connectingPeersCount.get() - passivePeersCount.get(), activeLackSize); if (p2pConfig.getMinConnections() <= activePeers.size() && activeLackSize <= 0) { diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java index b31e9ee1f55..dc14a7fbd53 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeEntry.java @@ -19,7 +19,7 @@ public static int distance(byte[] ownerId, byte[] targetId) { byte[] h1 = targetId; byte[] h2 = ownerId; - byte[] hash = new byte[Math.min(h1.length, h2.length)]; + byte[] hash = new byte[StrictMath.min(h1.length, h2.length)]; for (int i = 0; i < hash.length; i++) { hash[i] = (byte) (h1[i] ^ h2[i]); diff --git a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java index 28e66f84943..20c08181eee 100644 --- a/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java +++ b/p2p/src/main/java/org/tron/p2p/discover/protocol/kad/table/NodeTable.java @@ -80,7 +80,7 @@ public int getBucketsCount() { public int getBucketId(NodeEntry e) { int id = e.getDistance() - 1; - return Math.max(id, 0); + return StrictMath.max(id, 0); } public synchronized int getNodesCount() { diff --git a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java index ce838796c40..ed20d3838bd 100644 --- a/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java +++ b/p2p/src/main/java/org/tron/p2p/dns/tree/Tree.java @@ -57,7 +57,7 @@ private Entry build(List leafs) { List subtrees = new ArrayList<>(); while (!leafs.isEmpty()) { int total = leafs.size(); - int n = Math.min(MaxChildren, total); + int n = StrictMath.min(MaxChildren, total); Entry branch = build(leafs.subList(0, n)); leafs = leafs.subList(n, total); diff --git a/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java index 3fd5b12800b..8bf5a6de884 100644 --- a/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java +++ b/p2p/src/test/java/org/tron/p2p/utils/NetUtilTest.java @@ -1,12 +1,19 @@ package org.tron.p2p.utils; +import static org.mockito.Mockito.mockStatic; + +import java.io.ByteArrayInputStream; import java.io.IOException; import java.lang.reflect.Method; import java.net.InetSocketAddress; -import java.net.Socket; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Test; -import org.tron.p2p.base.Constant; +import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.tron.p2p.discover.Node; import org.tron.p2p.protos.Discover; @@ -49,71 +56,105 @@ public void testValidNode() { @Test public void testGetNode() { - Discover.Endpoint endpoint = Discover.Endpoint.newBuilder().setPort(100).build(); + Discover.Endpoint endpoint = + Discover.Endpoint.newBuilder().setPort(100).build(); Node node = NetUtil.getNode(endpoint); Assert.assertEquals(100, node.getPort()); } @Test - public void testExternalIp() { - String ip = NetUtil.getExternalIpV4(); - Assert.assertFalse(ip.startsWith("10.")); - Assert.assertFalse(ip.startsWith("192.168.")); - Assert.assertFalse(ip.startsWith("172.16.")); - Assert.assertFalse(ip.startsWith("172.17.")); - Assert.assertFalse(ip.startsWith("172.18.")); - Assert.assertFalse(ip.startsWith("172.19.")); - Assert.assertFalse(ip.startsWith("172.20.")); - Assert.assertFalse(ip.startsWith("172.21.")); - Assert.assertFalse(ip.startsWith("172.22.")); - Assert.assertFalse(ip.startsWith("172.23.")); - Assert.assertFalse(ip.startsWith("172.24.")); - Assert.assertFalse(ip.startsWith("172.25.")); - Assert.assertFalse(ip.startsWith("172.26.")); - Assert.assertFalse(ip.startsWith("172.27.")); - Assert.assertFalse(ip.startsWith("172.28.")); - Assert.assertFalse(ip.startsWith("172.29.")); - Assert.assertFalse(ip.startsWith("172.30.")); - Assert.assertFalse(ip.startsWith("172.31.")); + public void testGetExternalIpWithMock() throws Exception { + String fakeIp = "203.0.113.42"; + URLConnection mockConn = Mockito.mock(URLConnection.class); + Mockito.when(mockConn.getInputStream()) + .thenReturn(new ByteArrayInputStream( + fakeIp.getBytes(StandardCharsets.UTF_8))); + + try (MockedConstruction urlMock = Mockito.mockConstruction(URL.class, + (mock, context) -> Mockito.when(mock.openConnection()) + .thenReturn(mockConn))) { + + Method method = NetUtil.class.getDeclaredMethod( + "getExternalIp", String.class, boolean.class); + method.setAccessible(true); + + String ip = (String) method.invoke(null, + "http://mock-service.test", true); + Assert.assertEquals(fakeIp, ip); + } } @Test - public void testGetIP() { - // notice: please check that you only have one externalIP - String ip1 = null; - String ip2 = null; - String ip3 = null; - try { - Method method = NetUtil.class.getDeclaredMethod("getExternalIp", String.class, boolean.class); + public void testGetExternalIpReturnsNullOnFailure() throws Exception { + URLConnection mockConn = Mockito.mock(URLConnection.class); + Mockito.when(mockConn.getInputStream()) + .thenThrow(new IOException("Connection refused")); + + try (MockedConstruction urlMock = Mockito.mockConstruction(URL.class, + (mock, context) -> Mockito.when(mock.openConnection()) + .thenReturn(mockConn))) { + + Method method = NetUtil.class.getDeclaredMethod( + "getExternalIp", String.class, boolean.class); method.setAccessible(true); - ip1 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(0), true); - ip2 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(1), true); - ip3 = (String) method.invoke(NetUtil.class, Constant.ipV4Urls.get(2), true); - } catch (Exception e) { - Assert.fail(); + + String ip = (String) method.invoke(null, + "http://unreachable.test", true); + Assert.assertNull(ip); + } + } + + @Test + public void testGetExternalIpRejectsInvalidIp() throws Exception { + String invalidIp = "not-an-ip"; + URLConnection mockConn = Mockito.mock(URLConnection.class); + Mockito.when(mockConn.getInputStream()) + .thenReturn(new ByteArrayInputStream( + invalidIp.getBytes(StandardCharsets.UTF_8))); + + try (MockedConstruction urlMock = Mockito.mockConstruction(URL.class, + (mock, context) -> Mockito.when(mock.openConnection()) + .thenReturn(mockConn))) { + + Method method = NetUtil.class.getDeclaredMethod( + "getExternalIp", String.class, boolean.class); + method.setAccessible(true); + + String ip = (String) method.invoke(null, + "http://bad-service.test", true); + Assert.assertNull(ip); } - String ip4 = NetUtil.getExternalIpV4(); - Assert.assertEquals(ip1, ip4); - Assert.assertEquals(ip2, ip4); - Assert.assertEquals(ip3, ip4); } - private String getLanIP2() { - String lanIP; - try (Socket s = new Socket("www.baidu.com", 80)) { - lanIP = s.getLocalAddress().getHostAddress(); - } catch (IOException e) { - lanIP = "127.0.0.1"; + @Test + public void testGetExternalIpRejectsEmptyResponse() throws Exception { + URLConnection mockConn = Mockito.mock(URLConnection.class); + Mockito.when(mockConn.getInputStream()) + .thenReturn(new ByteArrayInputStream( + "".getBytes(StandardCharsets.UTF_8))); + + try (MockedConstruction urlMock = Mockito.mockConstruction(URL.class, + (mock, context) -> Mockito.when(mock.openConnection()) + .thenReturn(mockConn))) { + + Method method = NetUtil.class.getDeclaredMethod( + "getExternalIp", String.class, boolean.class); + method.setAccessible(true); + + String ip = (String) method.invoke(null, + "http://empty-service.test", true); + Assert.assertNull(ip); } - return lanIP; } @Test public void testGetLanIP() { String lanIpv4 = NetUtil.getLanIP(); Assert.assertNotNull(lanIpv4); - String lanIpv4Old = getLanIP2(); - Assert.assertEquals(lanIpv4, lanIpv4Old); + // verify it's a valid IPv4 format (not relying on external network) + Assert.assertTrue( + "LAN IP should be valid IPv4 or loopback", + NetUtil.validIpV4(lanIpv4) || "127.0.0.1".equals(lanIpv4)); } @Test @@ -121,41 +162,49 @@ public void testIPv6Format() { String std = "fe80:0:0:0:204:61ff:fe9d:f156"; int randomPort = 10001; String ip1 = - new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:fe9d:f156", randomPort) + new InetSocketAddress( + "fe80:0000:0000:0000:0204:61ff:fe9d:f156", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip1, std); String ip2 = - new InetSocketAddress("fe80::204:61ff:fe9d:f156", randomPort).getAddress().getHostAddress(); + new InetSocketAddress("fe80::204:61ff:fe9d:f156", randomPort) + .getAddress() + .getHostAddress(); Assert.assertEquals(ip2, std); String ip3 = - new InetSocketAddress("fe80:0000:0000:0000:0204:61ff:254.157.241.86", randomPort) + new InetSocketAddress( + "fe80:0000:0000:0000:0204:61ff:254.157.241.86", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip3, std); String ip4 = - new InetSocketAddress("fe80:0:0:0:0204:61ff:254.157.241.86", randomPort) + new InetSocketAddress( + "fe80:0:0:0:0204:61ff:254.157.241.86", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip4, std); String ip5 = - new InetSocketAddress("fe80::204:61ff:254.157.241.86", randomPort) + new InetSocketAddress( + "fe80::204:61ff:254.157.241.86", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip5, std); String ip6 = - new InetSocketAddress("FE80::204:61ff:254.157.241.86", randomPort) + new InetSocketAddress( + "FE80::204:61ff:254.157.241.86", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip6, std); String ip7 = - new InetSocketAddress("[fe80:0:0:0:204:61ff:fe9d:f156]", randomPort) + new InetSocketAddress( + "[fe80:0:0:0:204:61ff:fe9d:f156]", randomPort) .getAddress() .getHostAddress(); Assert.assertEquals(ip7, std); @@ -164,48 +213,56 @@ public void testIPv6Format() { @Test public void testParseIpv6() { InetSocketAddress address1 = - NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); Assert.assertNotNull(address1); Assert.assertEquals(18888, address1.getPort()); Assert.assertEquals( - "2600:1f13:908:1b00:e1fd:5a84:251c:a32a", address1.getAddress().getHostAddress()); + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a", + address1.getAddress().getHostAddress()); try { - NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:abcd"); + NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:abcd"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress("[2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); + NetUtil.parseInetSocketAddress( + "[2600:1f13:908:1b00:e1fd:5a84:251c:a32a:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a]:18888"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } try { - NetUtil.parseInetSocketAddress("2600:1f13:908:1b00:e1fd:5a84:251c:a32a"); + NetUtil.parseInetSocketAddress( + "2600:1f13:908:1b00:e1fd:5a84:251c:a32a"); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(true); } - InetSocketAddress address5 = NetUtil.parseInetSocketAddress("192.168.0.1:18888"); + InetSocketAddress address5 = + NetUtil.parseInetSocketAddress("192.168.0.1:18888"); Assert.assertNotNull(address5); } }