@@ -34,6 +34,7 @@ public static void init() throws Exception {
3434 public void testProcessInventoryMessage () throws Exception {
3535 CommonParameter parameter = CommonParameter .getInstance ();
3636 parameter .setMaxTps (10 );
37+ parameter .setMaxBlockInvPerSecond (10 );
3738
3839 PeerStatistics peerStatistics = new PeerStatistics ();
3940
@@ -75,7 +76,7 @@ public void testProcessInventoryMessage() throws Exception {
7576
7677 count = peer .getPeerStatistics ().messageStatistics .tronInTrxInventoryElement .getCount (10 );
7778
78- Assert .assertEquals (110 , count );
79+ Assert .assertEquals (10 , count ); // 100 hashes dropped: 10+100=110 > maxCountIn10s(100)
7980
8081 list .clear ();
8182 for (int i = 0 ; i < 100 ; i ++) {
@@ -88,7 +89,7 @@ public void testProcessInventoryMessage() throws Exception {
8889
8990 count = peer .getPeerStatistics ().messageStatistics .tronInTrxInventoryElement .getCount (10 );
9091
91- Assert .assertEquals (110 , count );
92+ Assert .assertEquals (10 , count ); // still dropped: window=10, 10+100=110 > 100
9293
9394 list .clear ();
9495 for (int i = 0 ; i < 200 ; i ++) {
@@ -101,7 +102,7 @@ public void testProcessInventoryMessage() throws Exception {
101102
102103 count = peer .getPeerStatistics ().messageStatistics .tronInBlockInventoryElement .getCount (10 );
103104
104- Assert .assertEquals (200 , count );
105+ Assert .assertEquals (0 , count ); // 200 hashes dropped: 0+200=200 > maxBlockInvIn10s(100)
105106
106107 list .clear ();
107108 for (int i = 0 ; i < 100 ; i ++) {
@@ -114,10 +115,100 @@ public void testProcessInventoryMessage() throws Exception {
114115
115116 count = peer .getPeerStatistics ().messageStatistics .tronInBlockInventoryElement .getCount (10 );
116117
117- Assert .assertEquals (300 , count );
118+ Assert .assertEquals (100 , count ); // passes: window=0, 0+100=100, not > 100
118119
119120 }
120121
122+ @ Test
123+ public void testCheckInvRateLimitTrxBoundary () throws Exception {
124+ // maxTps=10 → maxCountIn10s=100
125+ CommonParameter parameter = CommonParameter .getInstance ();
126+ parameter .setMaxTps (10 );
127+ parameter .setMaxBlockInvPerSecond (10 );
128+
129+ PeerStatistics peerStatistics = new PeerStatistics ();
130+ PeerConnection peer = mock (PeerConnection .class );
131+ Mockito .when (peer .getPeerStatistics ()).thenReturn (peerStatistics );
132+
133+ P2pEventHandlerImpl handler = new P2pEventHandlerImpl ();
134+ Method method = handler .getClass ()
135+ .getDeclaredMethod ("processMessage" , PeerConnection .class , byte [].class );
136+ method .setAccessible (true );
137+
138+ // Fill window to 91: send 91 TRX hashes → passes (0+91=91 ≤ 100)
139+ List <Sha256Hash > list91 = new ArrayList <>();
140+ for (int i = 0 ; i < 91 ; i ++) {
141+ list91 .add (new Sha256Hash (i , new byte [32 ]));
142+ }
143+ InventoryMessage msg91 = new InventoryMessage (list91 , InventoryType .TRX );
144+ method .invoke (handler , peer , msg91 .getSendBytes ());
145+ Assert .assertEquals (91 ,
146+ peer .getPeerStatistics ().messageStatistics .tronInTrxInventoryElement .getCount (10 ));
147+
148+ // Send 9 more TRX hashes → passes (91+9=100, not > 100)
149+ List <Sha256Hash > list9 = new ArrayList <>();
150+ for (int i = 0 ; i < 9 ; i ++) {
151+ list9 .add (new Sha256Hash (i , new byte [32 ]));
152+ }
153+ InventoryMessage msg9 = new InventoryMessage (list9 , InventoryType .TRX );
154+ method .invoke (handler , peer , msg9 .getSendBytes ());
155+ Assert .assertEquals (100 ,
156+ peer .getPeerStatistics ().messageStatistics .tronInTrxInventoryElement .getCount (10 ));
157+
158+ // Send 1 more TRX hash → DROPPED (100+1=101 > 100)
159+ List <Sha256Hash > list1 = new ArrayList <>();
160+ list1 .add (new Sha256Hash (0 , new byte [32 ]));
161+ InventoryMessage msg1 = new InventoryMessage (list1 , InventoryType .TRX );
162+ method .invoke (handler , peer , msg1 .getSendBytes ());
163+ Assert .assertEquals (100 , // count unchanged: message was dropped
164+ peer .getPeerStatistics ().messageStatistics .tronInTrxInventoryElement .getCount (10 ));
165+ }
166+
167+ @ Test
168+ public void testCheckInvRateLimitBlockBoundary () throws Exception {
169+ // maxBlockInvPerSecond=10 → maxBlockInvIn10s=100
170+ CommonParameter parameter = CommonParameter .getInstance ();
171+ parameter .setMaxTps (1000 );
172+ parameter .setMaxBlockInvPerSecond (10 );
173+
174+ PeerStatistics peerStatistics = new PeerStatistics ();
175+ PeerConnection peer = mock (PeerConnection .class );
176+ Mockito .when (peer .getPeerStatistics ()).thenReturn (peerStatistics );
177+
178+ P2pEventHandlerImpl handler = new P2pEventHandlerImpl ();
179+ Method method = handler .getClass ()
180+ .getDeclaredMethod ("processMessage" , PeerConnection .class , byte [].class );
181+ method .setAccessible (true );
182+
183+ // Send 101 BLOCK hashes → DROPPED (0+101=101 > 100)
184+ List <Sha256Hash > list101 = new ArrayList <>();
185+ for (int i = 0 ; i < 101 ; i ++) {
186+ list101 .add (new Sha256Hash (i , new byte [32 ]));
187+ }
188+ InventoryMessage msgBlock101 = new InventoryMessage (list101 , InventoryType .BLOCK );
189+ method .invoke (handler , peer , msgBlock101 .getSendBytes ());
190+ Assert .assertEquals (0 ,
191+ peer .getPeerStatistics ().messageStatistics .tronInBlockInventoryElement .getCount (10 ));
192+
193+ // Send 100 BLOCK hashes → passes (0+100=100, not > 100)
194+ List <Sha256Hash > list100 = new ArrayList <>();
195+ for (int i = 0 ; i < 100 ; i ++) {
196+ list100 .add (new Sha256Hash (i , new byte [32 ]));
197+ }
198+ InventoryMessage msgBlock100 = new InventoryMessage (list100 , InventoryType .BLOCK );
199+ method .invoke (handler , peer , msgBlock100 .getSendBytes ());
200+ Assert .assertEquals (100 ,
201+ peer .getPeerStatistics ().messageStatistics .tronInBlockInventoryElement .getCount (10 ));
202+
203+ // Send 1 more BLOCK hash → DROPPED (100+1=101 > 100)
204+ List <Sha256Hash > list1 = new ArrayList <>();
205+ list1 .add (new Sha256Hash (0 , new byte [32 ]));
206+ InventoryMessage msgBlock1 = new InventoryMessage (list1 , InventoryType .BLOCK );
207+ method .invoke (handler , peer , msgBlock1 .getSendBytes ());
208+ Assert .assertEquals (100 , // count unchanged: message was dropped
209+ peer .getPeerStatistics ().messageStatistics .tronInBlockInventoryElement .getCount (10 ));
210+ }
211+
121212 @ Test
122213 public void testUpdateLastInteractiveTime () throws Exception {
123214 PeerConnection peer = new PeerConnection ();
0 commit comments