33import static org .tron .core .config .Parameter .ChainConstant .BLOCK_PRODUCED_INTERVAL ;
44import static org .tron .core .config .Parameter .NetConstants .MAX_TRX_PER_PEER ;
55import static org .tron .core .config .Parameter .NetConstants .MSG_CACHE_DURATION_IN_BLOCKS ;
6+ import static org .tron .core .config .Parameter .NetConstants .NET_MAX_TRX_PER_SECOND ;
67import static org .tron .core .config .Parameter .NodeConstant .MAX_BLOCKS_ALREADY_FETCHED ;
78import static org .tron .core .config .Parameter .NodeConstant .MAX_BLOCKS_IN_PROCESS ;
89import static org .tron .core .config .Parameter .NodeConstant .MAX_BLOCKS_SYNC_FROM_ONE_PEER ;
4344import org .tron .common .overlay .server .SyncPool ;
4445import org .tron .common .utils .ExecutorLoop ;
4546import org .tron .common .utils .Sha256Hash ;
47+ import org .tron .common .utils .SlidingWindowCounter ;
4648import org .tron .common .utils .Time ;
4749import org .tron .core .capsule .BlockCapsule ;
4850import org .tron .core .capsule .BlockCapsule .BlockId ;
@@ -88,11 +90,13 @@ public class NodeImpl extends PeerConnectionDelegate implements Node {
8890 .maximumSize (10 ).expireAfterWrite (60 , TimeUnit .SECONDS )
8991 .recordStats ().build ();
9092
93+ private SlidingWindowCounter fetchWaterLine =
94+ new SlidingWindowCounter (BLOCK_PRODUCED_INTERVAL * MSG_CACHE_DURATION_IN_BLOCKS / 100 );
95+
9196 private int maxTrxsSize = 1_000_000 ;
9297
9398 private int maxTrxsCnt = 100 ;
9499
95-
96100 @ Getter
97101 class PriorItem implements java .lang .Comparable <PriorItem > {
98102
@@ -281,6 +285,9 @@ public Thread newThread(Runnable r) {
281285 private ScheduledExecutorService handleSyncBlockExecutor = Executors
282286 .newSingleThreadScheduledExecutor ();
283287
288+ private ScheduledExecutorService fetchWaterLineExecutor = Executors
289+ .newSingleThreadScheduledExecutor ();
290+
284291 private volatile boolean isHandleSyncBlockActive = false ;
285292
286293 private AtomicLong fetchSequenceCounter = new AtomicLong (0L );
@@ -467,6 +474,15 @@ private void activeTronPump() {
467474 logger .error ("Unhandled exception" , t );
468475 }
469476 }, 10 , 1 , TimeUnit .SECONDS );
477+
478+ //fetchWaterLine:
479+ fetchWaterLineExecutor .scheduleWithFixedDelay (() -> {
480+ try {
481+ fetchWaterLine .advance ();
482+ } catch (Throwable t ) {
483+ logger .error ("Unhandled exception" , t );
484+ }
485+ }, 1000 , 100 , TimeUnit .MILLISECONDS );
470486 }
471487
472488 private void consumerAdvObjToFetch () {
@@ -687,15 +703,18 @@ private void onHandleInventoryMessage(PeerConnection peer, InventoryMessage msg)
687703
688704 //avoid TRX flood attack here.
689705 if (msg .getInventoryType ().equals (InventoryType .TRX )
690- && peer .isAdvInvFull ()) {
691- logger .info ("A peer is flooding us, stop handle inv, the peer is:" + peer );
706+ && (peer .isAdvInvFull ()
707+ || isFlooded ())) {
708+ logger .warn ("A peer is flooding us, stop handle inv, the peer is: " + peer );
692709 return ;
693710 }
694711
695712 peer .getAdvObjSpreadToUs ().put (id , System .currentTimeMillis ());
696713 if (!requested [0 ]) {
697714 if (!badAdvObj .containsKey (id )) {
698715 if (!advObjToFetch .contains (id )) {
716+ fetchWaterLine .increase ();
717+ logger .info ("water line:" + fetchWaterLine .totalCount ());
699718 this .advObjToFetch .put (id , new PriorItem (new Item (id , msg .getInventoryType ()),
700719 fetchSequenceCounter .incrementAndGet ()));
701720 } else {
@@ -708,6 +727,11 @@ private void onHandleInventoryMessage(PeerConnection peer, InventoryMessage msg)
708727 }
709728 }
710729
730+ private boolean isFlooded () {
731+ return fetchWaterLine .totalCount ()
732+ > BLOCK_PRODUCED_INTERVAL * NET_MAX_TRX_PER_SECOND * MSG_CACHE_DURATION_IN_BLOCKS / 1000 ;
733+ }
734+
711735 @ Override
712736 public void syncFrom (Sha256Hash myHeadBlockHash ) {
713737 try {
@@ -721,7 +745,6 @@ public void syncFrom(Sha256Hash myHeadBlockHash) {
721745 logger .info ("wait end" );
722746 }
723747
724-
725748 private void onHandleBlockMessage (PeerConnection peer , BlockMessage blkMsg ) {
726749 Map <Item , Long > advObjWeRequested = peer .getAdvObjWeRequested ();
727750 Map <BlockId , Long > syncBlockRequested = peer .getSyncBlockRequested ();
0 commit comments