Repository navigation
fix(net): gate peer traffic on hello validation and enforce timeout #6993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kuny0707
merged 16 commits into
tronprotocol:release_v4.8.3
from
317787106:fix/hello_check
Oct 16, 2026
+167
−3
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
39b143f
fix the bug of check of hellomessage
317787106 8aef230
Merge branch 'release_v4.8.3' into fix/hello_check
317787106 196a65c
Merge branch 'release_v4.8.3' into fix/hello_check
317787106 183216c
valid HelloMessage before toString
317787106 31861e5
add validEndPoint for HelloMessage
317787106 efcb9b3
remove fork detect
317787106 639130f
don't print hash of hellomessage
317787106 0f7ccda
remove PeerHelloTimeoutTest
317787106 f8e310d
add validation of node id's length; move HandshakeService's valid bef…
317787106 e1cdeff
chore: merge release_v4.8.3 into fix/hello_check
317787106 623ec8e
Merge branch 'release_v4.8.3' into fix/hello_check
317787106 266846a
drop validate of HelloMessage
317787106 b31fe99
Merge remote-tracking branch 'upstream/release_v4.8.3' into fix/hello…
317787106 07370c6
fix testcase
317787106 d1dc60b
simply testcases
317787106 992d57f
fix(net): remove redundant empty-frame handling
317787106 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
framework/src/test/java/org/tron/core/net/P2pHelloGateTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package org.tron.core.net; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.tron.common.utils.ReflectUtils; | ||
| import org.tron.core.ChainBaseManager; | ||
| import org.tron.core.ChainBaseManager.NodeType; | ||
| import org.tron.core.capsule.BlockCapsule.BlockId; | ||
| import org.tron.core.net.message.MessageTypes; | ||
| import org.tron.core.net.message.PbftMessageFactory; | ||
| import org.tron.core.net.message.TronMessageFactory; | ||
| import org.tron.core.net.message.base.DisconnectMessage; | ||
| import org.tron.core.net.message.handshake.HelloMessage; | ||
| import org.tron.core.net.peer.PeerConnection; | ||
| import org.tron.core.net.peer.PeerManager; | ||
| import org.tron.core.net.service.handshake.HandshakeService; | ||
| import org.tron.core.net.service.statistics.NodeStatistics; | ||
| import org.tron.core.net.service.statistics.PeerStatistics; | ||
| import org.tron.p2p.connection.Channel; | ||
| import org.tron.p2p.discover.Node; | ||
| import org.tron.protos.Protocol.ReasonCode; | ||
|
|
||
| public class P2pHelloGateTest { | ||
|
|
||
| private final P2pEventHandlerImpl handler = new P2pEventHandlerImpl(); | ||
| private final Channel channel = mock(Channel.class); | ||
| private final PeerConnection peer = mock(PeerConnection.class); | ||
| private final HandshakeService handshakeService = mock(HandshakeService.class); | ||
| private MockedStatic<PeerManager> peerManager; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| ReflectUtils.setFieldValue(handler, "handshakeService", handshakeService); | ||
| peerManager = Mockito.mockStatic(PeerManager.class); | ||
| peerManager.when(() -> PeerManager.getPeerConnection(channel)).thenReturn(peer); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| peerManager.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRejectMessagesBeforeHelloWithoutParsing() { | ||
| try (MockedStatic<TronMessageFactory> tronFactory = | ||
| Mockito.mockStatic(TronMessageFactory.class); | ||
| MockedStatic<PbftMessageFactory> pbftFactory = | ||
| Mockito.mockStatic(PbftMessageFactory.class)) { | ||
| int rejected = 0; | ||
| for (MessageTypes type : MessageTypes.values()) { | ||
| if (type != MessageTypes.P2P_HELLO && type != MessageTypes.P2P_DISCONNECT) { | ||
| handler.onMessage(channel, new byte[] {type.asByte()}); | ||
| rejected++; | ||
| } | ||
| } | ||
| verify(peer, times(rejected)).disconnect(ReasonCode.BAD_PROTOCOL); | ||
| tronFactory.verifyNoInteractions(); | ||
| pbftFactory.verifyNoInteractions(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testHelloIsDispatchedBeforeHandshake() throws Exception { | ||
| ChainBaseManager chain = mock(ChainBaseManager.class); | ||
| when(chain.getGenesisBlockId()).thenReturn(new BlockId()); | ||
| when(chain.getSolidBlockId()).thenReturn(new BlockId()); | ||
| when(chain.getHeadBlockId()).thenReturn(new BlockId()); | ||
| when(chain.getNodeType()).thenReturn(NodeType.FULL); | ||
| HelloMessage hello = new HelloMessage( | ||
| new Node(new byte[64], "127.0.0.1", null, 18888), 0, chain); | ||
| when(peer.getPeerStatistics()).thenReturn(new PeerStatistics()); | ||
|
|
||
| handler.onMessage(channel, hello.getSendBytes()); | ||
|
|
||
| verify(handshakeService).processHelloMessage(eq(peer), any(HelloMessage.class)); | ||
| verify(peer, never()).disconnect(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDisconnectIsDispatchedBeforeHandshake() { | ||
| NodeStatistics stats = mock(NodeStatistics.class); | ||
| when(peer.getPeerStatistics()).thenReturn(new PeerStatistics()); | ||
| when(peer.getP2pRateLimiter()).thenReturn(new P2pRateLimiter()); | ||
| when(peer.getChannel()).thenReturn(channel); | ||
| when(peer.getNodeStatistics()).thenReturn(stats); | ||
|
|
||
| handler.onMessage(channel, new DisconnectMessage(ReasonCode.PEER_QUITING).getSendBytes()); | ||
|
|
||
| verify(channel).close(); | ||
| verify(stats).nodeDisconnectedRemote(ReasonCode.PEER_QUITING); | ||
| verify(peer, never()).disconnect(any()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For case when it already disconnected by other logic part.