Skip to content

Commit e50e11f

Browse files
committed
feat(net):test for P2P message rate limit
1 parent aa682c9 commit e50e11f

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class FetchInvDataMsgHandler implements TronMsgHandler {
4141
.maximumSize(1000).expireAfterWrite(1, TimeUnit.HOURS).build();
4242

4343
private static final int MAX_SIZE = 1_000_000;
44+
private static final int MAX_FETCH_SIZE = 100;
4445
@Autowired
4546
private TronNetDelegate tronNetDelegate;
4647
@Autowired
@@ -163,7 +164,7 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) thr
163164
if (!peer.isNeedSyncFromUs()) {
164165
throw new P2pException(TypeEnum.BAD_MESSAGE, "no need sync");
165166
}
166-
if (fetchInvDataMsg.getHashList().size() > 100) {
167+
if (fetchInvDataMsg.getHashList().size() > MAX_FETCH_SIZE) {
167168
throw new P2pException(TypeEnum.BAD_MESSAGE, "fetch too more blocks, size:"
168169
+ fetchInvDataMsg.getHashList().size());
169170
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.tron.core.net;
2+
3+
import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA;
4+
import static org.tron.core.net.message.MessageTypes.SYNC_BLOCK_CHAIN;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class P2pRateLimiterTest {
10+
@Test
11+
public void test() {
12+
P2pRateLimiter limiter = new P2pRateLimiter();
13+
limiter.register(SYNC_BLOCK_CHAIN.asByte(), 2);
14+
limiter.acquire(SYNC_BLOCK_CHAIN.asByte());
15+
limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
16+
boolean ret = limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
17+
Assert.assertFalse(ret);
18+
ret = limiter.tryAcquire(FETCH_INV_DATA.asByte());
19+
Assert.assertTrue(ret);
20+
}
21+
}

framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.tron.common.utils.Sha256Hash;
1414
import org.tron.core.capsule.BlockCapsule;
1515
import org.tron.core.config.Parameter;
16+
import org.tron.core.net.P2pRateLimiter;
1617
import org.tron.core.net.TronNetDelegate;
1718
import org.tron.core.net.message.adv.BlockMessage;
1819
import org.tron.core.net.message.adv.FetchInvDataMessage;
@@ -21,6 +22,8 @@
2122
import org.tron.core.net.service.adv.AdvService;
2223
import org.tron.protos.Protocol;
2324

25+
import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA;
26+
2427
public class FetchInvDataMsgHandlerTest {
2528

2629
@Test
@@ -93,4 +96,35 @@ public void testSyncFetchCheck() {
9396
Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000");
9497
}
9598
}
99+
100+
@Test
101+
public void testRateLimiter() {
102+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L);
103+
List<Sha256Hash> blockIds = new LinkedList<>();
104+
for (int i = 0; i <= 100; i++) {
105+
blockIds.add(blockId);
106+
}
107+
FetchInvDataMessage msg =
108+
new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK);
109+
PeerConnection peer = Mockito.mock(PeerConnection.class);
110+
Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true);
111+
Cache<Item, Long> advInvSpread = CacheBuilder.newBuilder().maximumSize(100)
112+
.expireAfterWrite(1, TimeUnit.HOURS).recordStats().build();
113+
Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread);
114+
P2pRateLimiter p2pRateLimiter = new P2pRateLimiter();
115+
p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 1);
116+
Mockito.when(peer.getP2pRateLimiter()).thenReturn(p2pRateLimiter);
117+
FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler();
118+
119+
try {
120+
fetchInvDataMsgHandler.processMessage(peer, msg);
121+
} catch (Exception e) {
122+
Assert.assertEquals("fetch too more blocks, size:101", e.getMessage());
123+
}
124+
try {
125+
fetchInvDataMsgHandler.processMessage(peer, msg);
126+
} catch (Exception e) {
127+
Assert.assertTrue(e.getMessage().endsWith("rate limit"));
128+
}
129+
}
96130
}

0 commit comments

Comments
 (0)