Skip to content

Commit 54b0395

Browse files
authored
Merge pull request #5924 from 317787106/feature/test_isolated3
feat(net): disconnect from inactive nodes if necessary
2 parents d6bbbe4 + 14fda8f commit 54b0395

18 files changed

Lines changed: 448 additions & 2 deletions

File tree

chainbase/src/main/java/org/tron/core/ChainBaseManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ public class ChainBaseManager {
244244
@Setter
245245
private long lowestBlockNum = -1; // except num = 0.
246246

247+
@Getter
248+
@Setter
249+
private long latestSaveBlockTime;
250+
247251
// for test only
248252
public List<ByteString> getWitnesses() {
249253
return witnessScheduleStore.getActiveWitnesses();
@@ -381,6 +385,7 @@ private void init() {
381385
this.lowestBlockNum = this.blockIndexStore.getLimitNumber(1, 1).stream()
382386
.map(BlockId::getNum).findFirst().orElse(0L);
383387
this.nodeType = getLowestBlockNum() > 1 ? NodeType.LITE : NodeType.FULL;
388+
this.latestSaveBlockTime = System.currentTimeMillis();
384389
}
385390

386391
public void shutdown() {

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ public class CommonParameter {
333333
public boolean isOpenFullTcpDisconnect;
334334
@Getter
335335
@Setter
336+
public int inactiveThreshold;
337+
@Getter
338+
@Setter
336339
public boolean nodeDetectEnable;
337340
@Getter
338341
@Setter

common/src/main/java/org/tron/core/Constant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public class Constant {
198198

199199
public static final String NODE_IS_OPEN_FULL_TCP_DISCONNECT = "node.isOpenFullTcpDisconnect";
200200

201+
public static final String NODE_INACTIVE_THRESHOLD = "node.inactiveThreshold";
202+
201203
public static final String NODE_DETECT_ENABLE = "node.nodeDetectEnable";
202204

203205
public static final String NODE_MAX_TRANSACTION_PENDING_SIZE = "node.maxTransactionPendingSize";

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public static void clearParam() {
173173
PARAMETER.receiveTcpMinDataLength = 2048;
174174
PARAMETER.isOpenFullTcpDisconnect = false;
175175
PARAMETER.nodeDetectEnable = false;
176+
PARAMETER.inactiveThreshold = 600;
176177
PARAMETER.supportConstant = false;
177178
PARAMETER.debug = false;
178179
PARAMETER.minTimeRatio = 0.0;
@@ -845,6 +846,9 @@ public static void setParam(final String[] args, final String confFileName) {
845846
PARAMETER.nodeDetectEnable = config.hasPath(Constant.NODE_DETECT_ENABLE)
846847
&& config.getBoolean(Constant.NODE_DETECT_ENABLE);
847848

849+
PARAMETER.inactiveThreshold = config.hasPath(Constant.NODE_INACTIVE_THRESHOLD)
850+
? config.getInt(Constant.NODE_INACTIVE_THRESHOLD) : 600;
851+
848852
PARAMETER.maxTransactionPendingSize = config.hasPath(Constant.NODE_MAX_TRANSACTION_PENDING_SIZE)
849853
? config.getInt(Constant.NODE_MAX_TRANSACTION_PENDING_SIZE) : 2000;
850854

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ public void updateDynamicProperties(BlockCapsule block) {
13841384
(chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber()
13851385
- chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum()
13861386
+ 1));
1387+
chainBaseManager.setLatestSaveBlockTime(System.currentTimeMillis());
13871388
Metrics.gaugeSet(MetricKeys.Gauge.HEADER_HEIGHT, block.getNum());
13881389
Metrics.gaugeSet(MetricKeys.Gauge.HEADER_TIME, block.getTimeStamp());
13891390
}

framework/src/main/java/org/tron/core/net/P2pEventHandlerImpl.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.tron.core.net.message.PbftMessageFactory;
2020
import org.tron.core.net.message.TronMessage;
2121
import org.tron.core.net.message.TronMessageFactory;
22+
import org.tron.core.net.message.adv.FetchInvDataMessage;
2223
import org.tron.core.net.message.adv.InventoryMessage;
2324
import org.tron.core.net.message.base.DisconnectMessage;
2425
import org.tron.core.net.message.handshake.HelloMessage;
@@ -38,6 +39,7 @@
3839
import org.tron.p2p.P2pEventHandler;
3940
import org.tron.p2p.connection.Channel;
4041
import org.tron.protos.Protocol;
42+
import org.tron.protos.Protocol.Inventory.InventoryType;
4143
import org.tron.protos.Protocol.ReasonCode;
4244

4345
@Slf4j(topic = "net")
@@ -205,6 +207,7 @@ private void processMessage(PeerConnection peer, byte[] data) {
205207
default:
206208
throw new P2pException(P2pException.TypeEnum.NO_SUCH_MESSAGE, msg.getType().toString());
207209
}
210+
updateLastActiveTime(peer, msg);
208211
} catch (Exception e) {
209212
processException(peer, msg, e);
210213
} finally {
@@ -220,6 +223,27 @@ private void processMessage(PeerConnection peer, byte[] data) {
220223
}
221224
}
222225

226+
private void updateLastActiveTime(PeerConnection peer, TronMessage msg) {
227+
MessageTypes type = msg.getType();
228+
229+
boolean flag = false;
230+
switch (type) {
231+
case SYNC_BLOCK_CHAIN:
232+
case BLOCK_CHAIN_INVENTORY:
233+
case BLOCK:
234+
flag = true;
235+
break;
236+
case FETCH_INV_DATA:
237+
flag = ((FetchInvDataMessage) msg).getInventoryType().equals(InventoryType.BLOCK);
238+
break;
239+
default:
240+
break;
241+
}
242+
if (flag) {
243+
peer.setLastActiveTime(System.currentTimeMillis());
244+
}
245+
}
246+
223247
private void processException(PeerConnection peer, TronMessage msg, Exception ex) {
224248
Protocol.ReasonCode code;
225249

framework/src/main/java/org/tron/core/net/TronNetService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.tron.core.net.peer.PeerStatusCheck;
2323
import org.tron.core.net.service.adv.AdvService;
2424
import org.tron.core.net.service.effective.EffectiveCheckService;
25+
import org.tron.core.net.service.effective.ResilienceService;
2526
import org.tron.core.net.service.fetchblock.FetchBlockService;
2627
import org.tron.core.net.service.nodepersist.NodePersistService;
2728
import org.tron.core.net.service.relay.RelayService;
@@ -50,6 +51,9 @@ public class TronNetService {
5051
@Autowired
5152
private PeerStatusCheck peerStatusCheck;
5253

54+
@Autowired
55+
private ResilienceService resilienceService;
56+
5357
@Autowired
5458
private TransactionsMsgHandler transactionsMsgHandler;
5559

@@ -88,6 +92,7 @@ public void start() {
8892
advService.init();
8993
syncService.init();
9094
peerStatusCheck.init();
95+
resilienceService.init();
9196
transactionsMsgHandler.init();
9297
fetchBlockService.init();
9398
nodePersistService.init();
@@ -110,6 +115,7 @@ public void close() {
110115
nodePersistService.close();
111116
advService.close();
112117
syncService.close();
118+
resilienceService.close();
113119
peerStatusCheck.close();
114120
transactionsMsgHandler.close();
115121
fetchBlockService.close();
@@ -177,7 +183,7 @@ private P2pConfig updateConfig(P2pConfig config) {
177183
config.setMaxConnectionsWithSameIp(parameter.getMaxConnectionsWithSameIp());
178184
config.setPort(parameter.getNodeListenPort());
179185
config.setNetworkId(parameter.getNodeP2pVersion());
180-
config.setDisconnectionPolicyEnable(parameter.isOpenFullTcpDisconnect());
186+
config.setDisconnectionPolicyEnable(false);
181187
config.setNodeDetectEnable(parameter.isNodeDetectEnable());
182188
config.setDiscoverEnable(parameter.isNodeDiscoveryEnable());
183189
if (StringUtils.isEmpty(config.getIp()) && hasIpv4Stack(NetUtil.getAllLocalAddress())) {

framework/src/main/java/org/tron/core/net/messagehandler/InventoryMsgHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public void processMessage(PeerConnection peer, TronMessage msg) {
3939
Item item = new Item(id, type);
4040
peer.getAdvInvReceive().put(item, System.currentTimeMillis());
4141
advService.addInv(item);
42+
if (type.equals(InventoryType.BLOCK) && peer.getAdvInvSpread().getIfPresent(item) == null) {
43+
peer.setLastActiveTime(System.currentTimeMillis());
44+
}
4245
}
4346
}
4447

framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
3333
peer.disconnect(Protocol.ReasonCode.BAD_PROTOCOL);
3434
return;
3535
}
36-
3736
long remainNum = 0;
3837

3938
List<BlockId> summaryChainIds = syncBlockChainMessage.getBlockIds();

framework/src/main/java/org/tron/core/net/peer/PeerConnection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class PeerConnection {
7979
@Setter
8080
private ByteString address;
8181

82+
@Getter
83+
@Setter
84+
private volatile long lastActiveTime;
85+
8286
@Getter
8387
@Setter
8488
private TronState tronState = TronState.INIT;
@@ -159,6 +163,7 @@ public void setChannel(Channel channel) {
159163
this.isRelayPeer = true;
160164
}
161165
this.nodeStatistics = TronStatsManager.getNodeStatistics(channel.getInetAddress());
166+
lastActiveTime = System.currentTimeMillis();
162167
}
163168

164169
public void setBlockBothHave(BlockId blockId) {

0 commit comments

Comments
 (0)