Skip to content

feat(net): add rate limiting logic for P2P messages #6393

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

Open
wants to merge 7 commits into
base: release_v4.8.1
Choose a base branch
from

Conversation

fyyhtx
Copy link
Contributor

@fyyhtx fyyhtx commented Jul 9, 2025

What does this PR do?
Add rate limiting logic for P2P messages. Closes #6297
Why are these changes required?

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

@kuny0707 kuny0707 moved this to In Review in java-tron Jul 9, 2025
@fyyhtx fyyhtx changed the base branch from develop to release_v4.8.1 July 9, 2025 04:00
@fyyhtx fyyhtx changed the title feat(net): add P2P message rate limit logic feat(net): add rate limiting logic for P2P messages Jul 9, 2025
peer.getChannel().close();
peer.getNodeStatistics()
.nodeDisconnectedRemote(((DisconnectMessage)msg).getReason());
if (peer.getP2pRateLimiter().tryAcquire(type.asByte())) {
Copy link

@Sunny6889 Sunny6889 Jul 10, 2025

Choose a reason for hiding this comment

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

why disconnect need a rate limiter? As in your issue mentioned "After receiving the message, the connection will be disconnected and no response will be given."

Choose a reason for hiding this comment

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

Another question: is this rate limit set per Peer or shared by all Peers? Will this rate limiter effect the normal disconnect logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another question: is this rate limit set per Peer or shared by all Peers? Will this rate limiter effect the normal disconnect logic?

This rate limit is set per peer. This rate limiter does not affect the normal disconnection logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why disconnect need a rate limiter? As in your issue mentioned "After receiving the message, the connection will be disconnected and no response will be given."

To prevent the peer from sending a large number of repeated disconnect messages at once, it will not respond to the message, but will execute the disconnection logic.


public class P2pRateLimiter {
private final Cache<Byte, RateLimiter> rateLimiters = CacheBuilder.newBuilder()
.maximumSize(32).build();

Choose a reason for hiding this comment

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

here is a hidden size 32 limit, can make it a large one so it normally won't never hit the limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The message types defined by the current protocol are very limited, and the limit of 32 is sufficient for the foreseeable future.

@@ -164,6 +171,9 @@ public void setChannel(Channel channel) {
}
this.nodeStatistics = TronStatsManager.getNodeStatistics(channel.getInetAddress());
lastInteractiveTime = System.currentTimeMillis();
p2pRateLimiter.register(SYNC_BLOCK_CHAIN.asByte(), 2);
p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 2);
p2pRateLimiter.register(P2P_DISCONNECT.asByte(), 1);

Choose a reason for hiding this comment

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

make this hardcode 2,2,1 number to use a static variable

Copy link
Contributor

Choose a reason for hiding this comment

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

Will relay nodes trigger speed limits? What about busy sync nodes on the network?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will relay nodes trigger speed limits? What about busy sync nodes on the network?

For synchronization logic, relay nodes are no different from ordinary full nodes.

@@ -156,6 +156,14 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) thr
if (!peer.isNeedSyncFromUs()) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "no need sync");
}
if (!peer.getP2pRateLimiter().tryAcquire(fetchInvDataMsg.getType().asByte())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

consumerInvToFetch task is scheduled every 30ms, but the limit FETCH_INV_DATA_RATE is 3 times/s, there will be conflict.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This rate limit is based on the premise of block synchronization.

@@ -318,6 +318,9 @@ public class Constant {

public static final String RATE_LIMITER_HTTP = "rate.limiter.http";
public static final String RATE_LIMITER_RPC = "rate.limiter.rpc";
public static final String RATE_LIMITER_P2P_SYNC_BLOCK_CHAIN = "rate.limiter.p2p.syncBlockChain";
Copy link

@Sunny6889 Sunny6889 Jul 22, 2025

Choose a reason for hiding this comment

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

I wonder should config file to add these new configurations with default values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's necessary, they are not critical configuration items. The rate limit parameter configuration is a precautionary solution, these configuration items may never be used in practice.

Choose a reason for hiding this comment

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

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

P2P message rate limit
6 participants