99import org .apache .commons .collections4 .CollectionUtils ;
1010import org .springframework .beans .factory .annotation .Autowired ;
1111import org .springframework .stereotype .Component ;
12+ import org .tron .common .utils .Pair ;
1213import org .tron .core .capsule .BlockCapsule .BlockId ;
1314import org .tron .core .config .Parameter .ChainConstant ;
1415import org .tron .core .config .Parameter .NetConstants ;
@@ -50,6 +51,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
5051 Deque <BlockId > blockIdWeGet = new LinkedList <>(chainInventoryMessage .getBlockIds ());
5152
5253 if (blockIdWeGet .size () == 1 && tronNetDelegate .containBlock (blockIdWeGet .peek ())) {
54+ peer .setRemainNum (0 );
5355 peer .setTronState (TronState .SYNC_COMPLETED );
5456 peer .setNeedSyncFromPeer (false );
5557 return ;
@@ -93,7 +95,8 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
9395 }
9496
9597 private void check (PeerConnection peer , ChainInventoryMessage msg ) throws P2pException {
96- if (peer .getSyncChainRequested () == null ) {
98+ Pair <Deque <BlockId >, Long > requested = peer .getSyncChainRequested ();
99+ if (requested == null ) {
97100 throw new P2pException (TypeEnum .BAD_MESSAGE , "not send syncBlockChainMsg" );
98101 }
99102
@@ -123,9 +126,9 @@ private void check(PeerConnection peer, ChainInventoryMessage msg) throws P2pExc
123126 }
124127 }
125128
126- if (!peer . getSyncChainRequested () .getKey ().contains (blockIds .get (0 ))) {
129+ if (!requested .getKey ().contains (blockIds .get (0 ))) {
127130 throw new P2pException (TypeEnum .BAD_MESSAGE , "unlinked block, my head: "
128- + peer . getSyncChainRequested () .getKey ().getLast ().getString ()
131+ + requested .getKey ().getLast ().getString ()
129132 + ", peer: " + blockIds .get (0 ).getString ());
130133 }
131134
@@ -137,7 +140,7 @@ private void check(PeerConnection peer, ChainInventoryMessage msg) throws P2pExc
137140 maxRemainTime / BLOCK_PRODUCED_INTERVAL + tronNetDelegate .getSolidBlockId ().getNum ();
138141 long lastNum = blockIds .get (blockIds .size () - 1 ).getNum ();
139142 long declaredHighestNum = lastNum + msg .getRemainNum ();
140- if (declaredHighestNum < 0 || declaredHighestNum > maxFutureNum ) {
143+ if (declaredHighestNum < 0 || lastNum > maxFutureNum || declaredHighestNum > maxFutureNum ) {
141144 throw new P2pException (TypeEnum .BAD_MESSAGE ,
142145 "Invalid declared highest block number: " + declaredHighestNum
143146 + ", lastNum: " + lastNum + ", remainNum: " + msg .getRemainNum ()
0 commit comments