|
13 | 13 | import org.tron.common.utils.Sha256Hash; |
14 | 14 | import org.tron.core.capsule.BlockCapsule; |
15 | 15 | import org.tron.core.config.Parameter; |
| 16 | +import org.tron.core.net.P2pRateLimiter; |
16 | 17 | import org.tron.core.net.TronNetDelegate; |
17 | 18 | import org.tron.core.net.message.adv.BlockMessage; |
18 | 19 | import org.tron.core.net.message.adv.FetchInvDataMessage; |
|
21 | 22 | import org.tron.core.net.service.adv.AdvService; |
22 | 23 | import org.tron.protos.Protocol; |
23 | 24 |
|
| 25 | +import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA; |
| 26 | + |
24 | 27 | public class FetchInvDataMsgHandlerTest { |
25 | 28 |
|
26 | 29 | @Test |
@@ -93,4 +96,35 @@ public void testSyncFetchCheck() { |
93 | 96 | Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000"); |
94 | 97 | } |
95 | 98 | } |
| 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 | + } |
96 | 130 | } |
0 commit comments