feat(net) : optimize the isIdle method by zeusoo001 · Pull Request #5921 · tronprotocol/java-tron

What does this PR do?
Optimize the isIdle method. refer to issue #5913

Why are these changes required?
During the block synchronization process, if the broadcast list has not been received, the synchronization may fail. The detailed process is as follows:

  1. After processing the chain inventory message, set fetchFlag to true.
  2. The scheduler will execute the startFetchSyncBlock method to fetch the block, and at this time, fetchFlag will be set to false.
etchExecutor.scheduleWithFixedDelay(() -> {
 try {
   if (fetchFlag) {
     fetchFlag = false;
     startFetchSyncBlock();
   }
 } catch (Exception e) {
   logger.error("Fetch sync block error", e);
 }
}, 10, 1, TimeUnit.SECONDS);
  1. Since advInvRequest is not empty at this time, peer.isIdle() returns false, so after this scheduling, the block is not obtained, but fetchFlag is set to false and cannot be set back, so the block cannot be obtained later.
private void startFetchSyncBlock() {
 HashMap<PeerConnection, List<BlockId>> send = new HashMap<>();
 tronNetDelegate.getActivePeer().stream()
     .filter(peer -> peer.isNeedSyncFromPeer() && peer.isIdle())
     .filter(peer -> peer.isFetchAble())
  1. Since the block is not obtained, the peer status cannot be updated, the status check will fail, and the connection will be disconnected.

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details