44import static org .tron .core .config .Parameter .NodeConstant .MAX_BLOCKS_IN_PROCESS ;
55import static org .tron .core .config .Parameter .NodeConstant .MAX_BLOCKS_SYNC_FROM_ONE_PEER ;
66
7+ import com .google .common .collect .Iterables ;
78import io .netty .util .internal .ConcurrentSet ;
89import java .util .ArrayList ;
910import java .util .Collection ;
2526import java .util .concurrent .TimeUnit ;
2627import javafx .util .Pair ;
2728import lombok .extern .slf4j .Slf4j ;
29+ import org .apache .commons .collections4 .CollectionUtils ;
2830import org .springframework .beans .factory .annotation .Autowired ;
2931import org .springframework .context .annotation .Lazy ;
3032import org .springframework .stereotype .Component ;
@@ -320,7 +322,6 @@ private void activeTronPump() {
320322 peer .getAdvObjWeSpread ().put (idToSpread .getKey (), Time .getCurrentMillis ());
321323 sendPackage .add (idToSpread , peer );
322324 });
323- // peer.cleanInvGarbage();
324325 });
325326
326327 sendPackage .sendInv ();
@@ -343,17 +344,16 @@ private void activeTronPump() {
343344 InvToSend sendPackage = new InvToSend ();
344345 advObjToFetch .entrySet ()
345346 .forEach (idToFetch ->
346- getActivePeer ().stream ().filter (peer -> !peer .isBusy ()
347- && peer .getAdvObjSpreadToUs ().containsKey (idToFetch .getKey ()))
348- .findFirst ()
349- .ifPresent (peer -> {
350- //TODO: don't fetch too much obj from only one peer
351- sendPackage .add (idToFetch , peer );
352- advObjToFetch .remove (idToFetch .getKey ());
353- peer .getAdvObjWeRequested ()
354- .put (idToFetch .getKey (), Time .getCurrentMillis ());
355- })
356- );
347+ getActivePeer ().stream ().filter (peer -> !peer .isBusy ()
348+ && peer .getAdvObjSpreadToUs ().containsKey (idToFetch .getKey ()))
349+ .findFirst ()
350+ .ifPresent (peer -> {
351+ //TODO: don't fetch too much obj from only one peer
352+ sendPackage .add (idToFetch , peer );
353+ advObjToFetch .remove (idToFetch .getKey ());
354+ peer .getAdvObjWeRequested ()
355+ .put (idToFetch .getKey (), Time .getCurrentMillis ());
356+ }));
357357 sendPackage .sendFetch ();
358358 }
359359 }
@@ -532,9 +532,6 @@ private synchronized void disconnectInactive() {
532532
533533
534534 private void onHandleInventoryMessage (PeerConnection peer , InventoryMessage msg ) {
535- //logger.info("on handle advertise inventory message");
536- // peer.cleanInvGarbage();
537-
538535 msg .getHashList ().forEach (id -> {
539536 final boolean [] spreaded = {false };
540537 final boolean [] requested = {false };
@@ -631,7 +628,6 @@ private void processAdvBlock(PeerConnection peer, BlockCapsule block) {
631628 p .setHeadBlockTimeWeBothHave (block .getTimeStamp ());
632629 });
633630
634- // getActivePeer().forEach(p -> p.cleanInvGarbage());
635631 //rebroadcast
636632 broadcast (new BlockMessage (block ));
637633
@@ -738,7 +734,15 @@ private void onHandleSyncBlockChainMessage(PeerConnection peer, SyncBlockChainMe
738734 }
739735
740736 if (blockIds .isEmpty ()) {
741- peer .setNeedSyncFromUs (false );
737+ if (CollectionUtils .isNotEmpty (summaryChainIds )
738+ && !del .canChainRevoke (summaryChainIds .get (0 ).getNum ())) {
739+ logger .info (
740+ "Node sync block fail, disconnect peer:{}, sync message:{}" ,
741+ peer , syncMsg );
742+ peer .disconnect (ReasonCode .SYNC_FAIL );
743+ } else {
744+ peer .setNeedSyncFromUs (false );
745+ }
742746 } else if (blockIds .size () == 1
743747 && !summaryChainIds .isEmpty ()
744748 && (summaryChainIds .contains (blockIds .peekFirst ())
@@ -750,11 +754,12 @@ private void onHandleSyncBlockChainMessage(PeerConnection peer, SyncBlockChainMe
750754 }
751755
752756 //TODO: need a block older than revokingDB size exception. otherwise will be a dead loop here
753- // if (!peer.isNeedSyncFromPeer()
754- // && !summaryChainIds.isEmpty()
755- // && !del.contain(Iterables.getLast(summaryChainIds), MessageTypes.BLOCK)) {
756- // startSyncWithPeer(peer);
757- // }
757+ if (!peer .isNeedSyncFromPeer ()
758+ && CollectionUtils .isNotEmpty (summaryChainIds )
759+ && !del .contain (Iterables .getLast (summaryChainIds ), MessageTypes .BLOCK )
760+ && del .canChainRevoke (summaryChainIds .get (0 ).getNum ())) {
761+ startSyncWithPeer (peer );
762+ }
758763
759764 peer .sendMessage (new ChainInventoryMessage (blockIds , remainNum ));
760765 }
@@ -989,7 +994,6 @@ private void updateBlockWeBothHave(PeerConnection peer, BlockId blockId) {
989994
990995 private void onHandleBlockInventoryMessage (PeerConnection peer , BlockInventoryMessage msg ) {
991996 logger .info ("on handle advertise blocks inventory message" );
992- // peer.cleanInvGarbage();
993997
994998 //todo: check this peer's advertise history and the history of our request to this peer.
995999 //simple implement here first
@@ -1004,9 +1008,9 @@ private void onHandleBlockInventoryMessage(PeerConnection peer, BlockInventoryMe
10041008 loopFetchBlocks .push (fetchMsg );
10051009 }
10061010
1007- // private void startSync() {
1008- // mapPeer.values ().forEach(this::startSyncWithPeer);
1009- // }
1011+ private void startSync () {
1012+ getActivePeer ().forEach (this ::startSyncWithPeer );
1013+ }
10101014
10111015 private Collection <PeerConnection > getActivePeer () {
10121016 return pool .getActivePeers ();
@@ -1023,7 +1027,7 @@ private void startSyncWithPeer(PeerConnection peer) {
10231027 }
10241028
10251029 private void syncNextBatchChainIds (PeerConnection peer ) {
1026- if (peer .getSyncChainRequested () != null ){
1030+ if (peer .getSyncChainRequested () != null ) {
10271031 logger .info ("peer {}:{} is in sync." , peer .getNode ().getHost (), peer .getNode ().getPort ());
10281032 return ;
10291033 }
0 commit comments