Skip to content

Commit 8879879

Browse files
committed
feat(net):optimize sync service
1 parent afb14ec commit 8879879

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ public class CommonParameter {
199199
@Getter
200200
@Setter
201201
public PublishConfig dnsPublishConfig;
202+
@Getter
203+
@Setter
204+
public long syncFetchBatchNum;
202205

203206
//If you are running a solidity node for java tron, this flag is set to true
204207
@Getter

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class Constant {
9090
public static final String NODE_MAX_CONNECTIONS = "node.maxConnections";
9191
public static final String NODE_MIN_CONNECTIONS = "node.minConnections";
9292
public static final String NODE_MIN_ACTIVE_CONNECTIONS = "node.minActiveConnections";
93+
public static final String NODE_SYNC_FETCH_BATCH_NUM = "node.syncFetchBatchNum";
9394

9495
public static final String NODE_MAX_ACTIVE_NODES = "node.maxActiveNodes";
9596
public static final String NODE_MAX_ACTIVE_NODES_WITH_SAME_IP = "node.maxActiveNodesWithSameIp";

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public static void clearParam() {
146146
PARAMETER.nodeEnableIpv6 = false;
147147
PARAMETER.dnsTreeUrls = new ArrayList<>();
148148
PARAMETER.dnsPublishConfig = null;
149+
PARAMETER.syncFetchBatchNum = 1000;
149150
PARAMETER.rpcPort = 0;
150151
PARAMETER.rpcOnSolidityPort = 0;
151152
PARAMETER.rpcOnPBFTPort = 0;
@@ -657,6 +658,15 @@ public static void setParam(final String[] args, final String confFileName) {
657658

658659
PARAMETER.dnsPublishConfig = loadDnsPublishConfig(config);
659660

661+
PARAMETER.syncFetchBatchNum = config.hasPath(Constant.NODE_SYNC_FETCH_BATCH_NUM) ? config
662+
.getInt(Constant.NODE_SYNC_FETCH_BATCH_NUM) : 1000;
663+
if (PARAMETER.syncFetchBatchNum > 2000) {
664+
PARAMETER.syncFetchBatchNum = 2000;
665+
}
666+
if (PARAMETER.syncFetchBatchNum < 100) {
667+
PARAMETER.syncFetchBatchNum = 100;
668+
}
669+
660670
PARAMETER.rpcPort =
661671
config.hasPath(Constant.NODE_RPC_PORT)
662672
? config.getInt(Constant.NODE_RPC_PORT) : 50051;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.tron.core.capsule.BlockCapsule.BlockId;
1414
import org.tron.core.config.Parameter.ChainConstant;
1515
import org.tron.core.config.Parameter.NetConstants;
16+
import org.tron.core.config.args.Args;
1617
import org.tron.core.exception.P2pException;
1718
import org.tron.core.exception.P2pException.TypeEnum;
1819
import org.tron.core.net.TronNetDelegate;
@@ -32,6 +33,8 @@ public class ChainInventoryMsgHandler implements TronMsgHandler {
3233
@Autowired
3334
private SyncService syncService;
3435

36+
private final long syncFetchBatchNum = Args.getInstance().getSyncFetchBatchNum();
37+
3538
@Override
3639
public void processMessage(PeerConnection peer, TronMessage msg) throws P2pException {
3740

@@ -88,7 +91,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
8891
peer.setFetchAble(true);
8992
if ((chainInventoryMessage.getRemainNum() == 0 && !peer.getSyncBlockToFetch().isEmpty())
9093
|| (chainInventoryMessage.getRemainNum() != 0
91-
&& peer.getSyncBlockToFetch().size() > NetConstants.SYNC_FETCH_BATCH_NUM)) {
94+
&& peer.getSyncBlockToFetch().size() > syncFetchBatchNum)) {
9295
syncService.setFetchFlag(true);
9396
} else {
9497
syncService.syncNext(peer);

framework/src/main/java/org/tron/core/net/service/sync/SyncService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.tron.common.utils.Pair;
2222
import org.tron.core.capsule.BlockCapsule;
2323
import org.tron.core.capsule.BlockCapsule.BlockId;
24-
import org.tron.core.config.Parameter.NetConstants;
2524
import org.tron.core.config.args.Args;
2625
import org.tron.core.exception.P2pException;
2726
import org.tron.core.exception.P2pException.TypeEnum;
@@ -65,6 +64,8 @@ public class SyncService {
6564
@Setter
6665
private volatile boolean fetchFlag = false;
6766

67+
private final long syncFetchBatchNum = Args.getInstance().getSyncFetchBatchNum();
68+
6869
public void init() {
6970
fetchExecutor.scheduleWithFixedDelay(() -> {
7071
try {
@@ -132,7 +133,7 @@ public void processBlock(PeerConnection peer, BlockMessage blockMessage) {
132133
handleFlag = true;
133134
if (peer.isIdle()) {
134135
if (peer.getRemainNum() > 0
135-
&& peer.getSyncBlockToFetch().size() <= NetConstants.SYNC_FETCH_BATCH_NUM) {
136+
&& peer.getSyncBlockToFetch().size() <= syncFetchBatchNum) {
136137
syncNext(peer);
137138
} else {
138139
fetchFlag = true;
@@ -250,10 +251,12 @@ private synchronized void handleSyncBlock() {
250251
}
251252

252253
final boolean[] isProcessed = {true};
254+
final long[] solidNum = {0};
253255

254256
while (isProcessed[0]) {
255257

256258
isProcessed[0] = false;
259+
solidNum[0] = tronNetDelegate.getSolidBlockId().getNum();
257260

258261
blockWaitToProcess.forEach((msg, peerConnection) -> {
259262
synchronized (tronNetDelegate.getBlockLock()) {
@@ -262,6 +265,10 @@ private synchronized void handleSyncBlock() {
262265
invalid(msg.getBlockId(), peerConnection);
263266
return;
264267
}
268+
if (msg.getBlockId().getNum() <= solidNum[0]) {
269+
blockWaitToProcess.remove(msg);
270+
return;
271+
}
265272
final boolean[] isFound = {false};
266273
tronNetDelegate.getActivePeer().stream()
267274
.filter(peer -> msg.getBlockId().equals(peer.getSyncBlockToFetch().peek()))

0 commit comments

Comments
 (0)