Skip to content

Commit 9cf633d

Browse files
authored
Merge pull request #4995 from wubin01/sync_opt
fix(net): solve the problem of block repeating obtaining
2 parents b169228 + d585cf3 commit 9cf633d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ 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()
54+
.maximumSize(10_000)
5455
.expireAfterWrite(blockCacheTimeout, TimeUnit.MINUTES).initialCapacity(10_000)
5556
.recordStats().build();
5657

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

139140
public void onDisconnect(PeerConnection peer) {
140141
if (!peer.getSyncBlockRequested().isEmpty()) {
141-
peer.getSyncBlockRequested().keySet().forEach(blockId -> invalid(blockId));
142+
peer.getSyncBlockRequested().keySet().forEach(blockId -> invalid(blockId, peer));
142143
}
143144
}
144145

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

150154
private LinkedList<BlockId> getBlockChainSummary(PeerConnection peer) throws P2pException {
@@ -209,7 +213,7 @@ private void startFetchSyncBlock() {
209213
}
210214
for (BlockId blockId : peer.getSyncBlockToFetch()) {
211215
if (requestBlockIds.getIfPresent(blockId) == null) {
212-
requestBlockIds.put(blockId, System.currentTimeMillis());
216+
requestBlockIds.put(blockId, peer);
213217
peer.getSyncBlockRequested().put(blockId, System.currentTimeMillis());
214218
send.get(peer).add(blockId);
215219
if (send.get(peer).size() >= MAX_BLOCK_FETCH_PER_PEER) {
@@ -243,7 +247,7 @@ private synchronized void handleSyncBlock() {
243247
synchronized (tronNetDelegate.getBlockLock()) {
244248
if (peerConnection.isDisconnect()) {
245249
blockWaitToProcess.remove(msg);
246-
invalid(msg.getBlockId());
250+
invalid(msg.getBlockId(), peerConnection);
247251
return;
248252
}
249253
final boolean[] isFound = {false};

0 commit comments

Comments
 (0)