Skip to content

Commit a5f9c6f

Browse files
committed
fix(net): solve the problem of obtaining the same block during block synchronization
1 parent b169228 commit a5f9c6f

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SyncService {
5050
private Map<BlockMessage, PeerConnection> blockJustReceived = new ConcurrentHashMap<>();
5151

5252
private long blockCacheTimeout = Args.getInstance().getBlockCacheTimeout();
53-
private Cache<BlockId, Long> requestBlockIds = CacheBuilder.newBuilder().maximumSize(10_000)
53+
private Cache<BlockId, PeerConnection> requestBlockIds = CacheBuilder.newBuilder().maximumSize(10_000)
5454
.expireAfterWrite(blockCacheTimeout, TimeUnit.MINUTES).initialCapacity(10_000)
5555
.recordStats().build();
5656

@@ -138,13 +138,16 @@ public void processBlock(PeerConnection peer, BlockMessage blockMessage) {
138138

139139
public void onDisconnect(PeerConnection peer) {
140140
if (!peer.getSyncBlockRequested().isEmpty()) {
141-
peer.getSyncBlockRequested().keySet().forEach(blockId -> invalid(blockId));
141+
peer.getSyncBlockRequested().keySet().forEach(blockId -> invalid(blockId, peer));
142142
}
143143
}
144144

145-
private void invalid(BlockId blockId) {
146-
requestBlockIds.invalidate(blockId);
147-
fetchFlag = true;
145+
private void invalid(BlockId blockId, PeerConnection peerConnection) {
146+
PeerConnection p = requestBlockIds.getIfPresent(blockId);
147+
if (peerConnection.equals(p)) {
148+
requestBlockIds.invalidate(blockId);
149+
fetchFlag = true;
150+
}
148151
}
149152

150153
private LinkedList<BlockId> getBlockChainSummary(PeerConnection peer) throws P2pException {
@@ -209,7 +212,7 @@ private void startFetchSyncBlock() {
209212
}
210213
for (BlockId blockId : peer.getSyncBlockToFetch()) {
211214
if (requestBlockIds.getIfPresent(blockId) == null) {
212-
requestBlockIds.put(blockId, System.currentTimeMillis());
215+
requestBlockIds.put(blockId, peer);
213216
peer.getSyncBlockRequested().put(blockId, System.currentTimeMillis());
214217
send.get(peer).add(blockId);
215218
if (send.get(peer).size() >= MAX_BLOCK_FETCH_PER_PEER) {
@@ -243,7 +246,7 @@ private synchronized void handleSyncBlock() {
243246
synchronized (tronNetDelegate.getBlockLock()) {
244247
if (peerConnection.isDisconnect()) {
245248
blockWaitToProcess.remove(msg);
246-
invalid(msg.getBlockId());
249+
invalid(msg.getBlockId(), peerConnection);
247250
return;
248251
}
249252
final boolean[] isFound = {false};

0 commit comments

Comments
 (0)