Skip to content

Commit 632e647

Browse files
authored
fix(net): correct sync completion and chain summary request timeouts (#7000)
1 parent 42742d7 commit 632e647

6 files changed

Lines changed: 864 additions & 71 deletions

File tree

‎common/src/main/java/org/tron/core/config/Parameter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public class NetConstants {
102102

103103
public static final long SYNC_FETCH_BATCH_NUM = 2000;
104104
public static final long HELLO_TIME_OUT = 10000L;
105-
public static final long ADV_TIME_OUT = 20000L;
106-
public static final long SYNC_TIME_OUT = 5000L;
105+
public static final long ADV_TIME_OUT = 15000L;
106+
public static final long SYNC_TIME_OUT = 10000L;
107107
public static final long NET_MAX_TRX_PER_SECOND = 700L;
108108
public static final int MSG_CACHE_DURATION_IN_BLOCKS = 5;
109109
public static final int MAX_BLOCK_FETCH_PER_PEER = 100;

‎framework/src/main/java/org/tron/core/net/messagehandler/ChainInventoryMsgHandler.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.commons.collections4.CollectionUtils;
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.stereotype.Component;
12+
import org.tron.common.utils.Pair;
1213
import org.tron.core.capsule.BlockCapsule.BlockId;
1314
import org.tron.core.config.Parameter.ChainConstant;
1415
import 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()

‎framework/src/main/java/org/tron/core/net/peer/PeerStatusCheck.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.tron.core.net.peer;
22

3+
import java.util.Deque;
34
import java.util.concurrent.ScheduledExecutorService;
45
import java.util.concurrent.TimeUnit;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Component;
89
import org.tron.common.es.ExecutorServiceManager;
10+
import org.tron.common.utils.Pair;
11+
import org.tron.core.capsule.BlockCapsule.BlockId;
912
import org.tron.core.config.Parameter.NetConstants;
1013
import org.tron.core.net.TronNetDelegate;
1114
import org.tron.protos.Protocol.ReasonCode;
@@ -72,6 +75,15 @@ public void statusCheck() {
7275
}
7376
}
7477

78+
if (!isDisconnected) {
79+
Pair<Deque<BlockId>, Long> requested = peer.getSyncChainRequested();
80+
isDisconnected = requested != null
81+
&& requested.getValue() < now - NetConstants.SYNC_TIME_OUT;
82+
if (isDisconnected) {
83+
logger.warn("Peer {} get chain inventory timeout", peer.getInetAddress());
84+
}
85+
}
86+
7587
if (!isDisconnected) {
7688
isDisconnected = peer.getSyncBlockRequested().values().stream()
7789
.anyMatch(time -> time < now - NetConstants.SYNC_TIME_OUT);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.tron.core.net;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.doNothing;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.spy;
7+
import static org.mockito.Mockito.when;
8+
9+
import java.net.InetSocketAddress;
10+
import org.tron.common.utils.ReflectUtils;
11+
import org.tron.common.utils.Sha256Hash;
12+
import org.tron.core.capsule.BlockCapsule.BlockId;
13+
import org.tron.core.net.message.handshake.HelloMessage;
14+
import org.tron.core.net.peer.PeerConnection;
15+
import org.tron.p2p.connection.Channel;
16+
import org.tron.protos.Protocol;
17+
18+
public final class PeerSyncTestSupport {
19+
20+
private PeerSyncTestSupport() {
21+
}
22+
23+
public static BlockId blockId(long number) {
24+
return new BlockId(Sha256Hash.ZERO_HASH, number);
25+
}
26+
27+
public static HelloMessage helloMessage(long headNum) throws Exception {
28+
return new HelloMessage(Protocol.HelloMessage.newBuilder()
29+
.setGenesisBlockId(Protocol.HelloMessage.BlockId.newBuilder()
30+
.setHash(blockId(0).getByteString()).setNumber(0))
31+
.setSolidBlockId(Protocol.HelloMessage.BlockId.newBuilder()
32+
.setHash(blockId(0).getByteString()).setNumber(0))
33+
.setHeadBlockId(Protocol.HelloMessage.BlockId.newBuilder()
34+
.setHash(blockId(headNum).getByteString()).setNumber(headNum))
35+
.build().toByteArray());
36+
}
37+
38+
public static PeerConnection peer(int port) {
39+
PeerConnection peer = spy(new PeerConnection());
40+
Channel channel = mock(Channel.class);
41+
InetSocketAddress address = new InetSocketAddress("127.0.0.1", port);
42+
when(channel.getInetSocketAddress()).thenReturn(address);
43+
when(channel.getInetAddress()).thenReturn(address.getAddress());
44+
ReflectUtils.setFieldValue(peer, "channel", channel);
45+
doNothing().when(peer).sendMessage(any());
46+
doNothing().when(peer).disconnect(any());
47+
return peer;
48+
}
49+
}

0 commit comments

Comments
 (0)