Skip to content

Commit d549b86

Browse files
authored
Merge pull request #5160 from wubin01/optimize_block_summary
feat(net): optimize getBlockChainSummary logic
2 parents 43fba01 + 747757b commit d549b86

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ public class Manager {
193193
@Getter
194194
@Setter
195195
private boolean isSyncMode;
196-
196+
@Getter
197+
private Object forkLock = new Object();
197198
// map<Long, IncrementalMerkleTree>
198199
@Getter
199200
@Setter
@@ -1269,8 +1270,9 @@ public void pushBlock(final BlockCapsule block)
12691270
chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp(),
12701271
khaosDb.getHead(), khaosDb.getMiniStore().size(),
12711272
khaosDb.getMiniUnlinkedStore().size());
1272-
1273-
switchFork(newBlock);
1273+
synchronized (forkLock) {
1274+
switchFork(newBlock);
1275+
}
12741276
logger.info(SAVE_BLOCK, newBlock);
12751277

12761278
logger.warn(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,8 @@ public boolean allowPBFT() {
349349
return chainBaseManager.getDynamicPropertiesStore().allowPBFT();
350350
}
351351

352+
public Object getForkLock() {
353+
return dbManager.getForkLock();
354+
}
355+
352356
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.tron.core.capsule.BlockCapsule.BlockId;
2424
import org.tron.core.config.Parameter.NetConstants;
2525
import org.tron.core.config.args.Args;
26-
import org.tron.core.exception.BadBlockException;
2726
import org.tron.core.exception.P2pException;
2827
import org.tron.core.exception.P2pException.TypeEnum;
2928
import org.tron.core.net.TronNetDelegate;
@@ -114,7 +113,10 @@ public void syncNext(PeerConnection peer) {
114113
logger.warn("Peer {} is in sync", peer.getInetSocketAddress());
115114
return;
116115
}
117-
LinkedList<BlockId> chainSummary = getBlockChainSummary(peer);
116+
LinkedList<BlockId> chainSummary;
117+
synchronized (tronNetDelegate.getForkLock()) {
118+
chainSummary = getBlockChainSummary(peer);
119+
}
118120
peer.setSyncChainRequested(new Pair<>(chainSummary, System.currentTimeMillis()));
119121
peer.sendMessage(new SyncBlockChainMessage(chainSummary));
120122
} catch (Exception e) {

0 commit comments

Comments
 (0)