4141@ Slf4j (topic = "net" )
4242@ Component
4343public class AdvService {
44-
4544 private final int MAX_INV_TO_FETCH_CACHE_SIZE = 100_000 ;
4645 private final int MAX_TRX_CACHE_SIZE = 50_000 ;
4746 private final int MAX_BLOCK_CACHE_SIZE = 10 ;
4847 private final int MAX_SPREAD_SIZE = 1_000 ;
48+ private final long TIMEOUT = MSG_CACHE_DURATION_IN_BLOCKS * BLOCK_PRODUCED_INTERVAL ;
4949
5050 @ Autowired
5151 private TronNetDelegate tronNetDelegate ;
@@ -264,13 +264,12 @@ private void consumerInvToFetch() {
264264 Collection <PeerConnection > peers = tronNetDelegate .getActivePeer ().stream ()
265265 .filter (peer -> peer .isIdle ())
266266 .collect (Collectors .toList ());
267-
268267 InvSender invSender = new InvSender ();
269- long now = System .currentTimeMillis ();
270268 synchronized (this ) {
271269 if (invToFetch .isEmpty () || peers .isEmpty ()) {
272270 return ;
273271 }
272+ long now = System .currentTimeMillis ();
274273 invToFetch .forEach ((item , time ) -> {
275274 if (time < now - MSG_CACHE_DURATION_IN_BLOCKS * BLOCK_PRODUCED_INTERVAL ) {
276275 logger .info ("This obj is too late to fetch, type: {} hash: {}" , item .getType (),
@@ -279,15 +278,16 @@ private void consumerInvToFetch() {
279278 invToFetchCache .invalidate (item );
280279 return ;
281280 }
282- peers .stream ().filter (peer -> peer .getAdvInvReceive ().getIfPresent (item ) != null
283- && invSender .getSize (peer ) < MAX_TRX_FETCH_PER_PEER )
284- .sorted (Comparator .comparingInt (peer -> invSender .getSize (peer )))
285- .findFirst ().ifPresent (peer -> {
286- if (peer .checkAndPutAdvInvRequest (item , now )) {
287- invSender .add (item , peer );
288- }
289- invToFetch .remove (item );
290- });
281+ peers .stream ().filter (peer -> {
282+ Long t = peer .getAdvInvReceive ().getIfPresent (item );
283+ return t != null && now - t < TIMEOUT && invSender .getSize (peer ) < MAX_TRX_FETCH_PER_PEER ;
284+ }).sorted (Comparator .comparingInt (peer -> invSender .getSize (peer )))
285+ .findFirst ().ifPresent (peer -> {
286+ if (peer .checkAndPutAdvInvRequest (item , now )) {
287+ invSender .add (item , peer );
288+ }
289+ invToFetch .remove (item );
290+ });
291291 });
292292 }
293293
0 commit comments