Skip to content

Commit 5b2bb67

Browse files
authored
Merge pull request #6360 from zeusoo001/volatile-modifier
feat(net): solve the problem of concurrent access to fields of PeerConnection
2 parents 5a487dc + bae51b0 commit 5b2bb67

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class PeerConnection {
8585

8686
@Getter
8787
@Setter
88-
private TronState tronState = TronState.INIT;
88+
private volatile TronState tronState = TronState.INIT;
8989

9090
@Autowired
9191
private TronNetDelegate tronNetDelegate;
@@ -123,15 +123,15 @@ public class PeerConnection {
123123
private Map<Item, Long> advInvRequest = new ConcurrentHashMap<>();
124124

125125
@Setter
126-
private BlockId fastForwardBlock;
126+
private volatile BlockId fastForwardBlock;
127127

128128
@Getter
129-
private BlockId blockBothHave = new BlockId();
129+
private volatile BlockId blockBothHave = new BlockId();
130130
@Getter
131131
private volatile long blockBothHaveUpdateTime = System.currentTimeMillis();
132132
@Setter
133133
@Getter
134-
private BlockId lastSyncBlockId;
134+
private volatile BlockId lastSyncBlockId;
135135
@Setter
136136
@Getter
137137
private volatile long remainNum;
@@ -146,7 +146,7 @@ public class PeerConnection {
146146
private Map<BlockId, Long> syncBlockRequested = new ConcurrentHashMap<>();
147147
@Setter
148148
@Getter
149-
private Pair<Deque<BlockId>, Long> syncChainRequested = null;
149+
private volatile Pair<Deque<BlockId>, Long> syncChainRequested = null;
150150
@Setter
151151
@Getter
152152
private Set<BlockId> syncBlockInProcess = new HashSet<>();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ private void invalid(BlockId blockId, PeerConnection peerConnection) {
163163
}
164164

165165
private LinkedList<BlockId> getBlockChainSummary(PeerConnection peer) throws P2pException {
166-
167-
BlockId beginBlockId = peer.getBlockBothHave();
168166
List<BlockId> blockIds = new ArrayList<>(peer.getSyncBlockToFetch());
167+
BlockId beginBlockId = peer.getBlockBothHave();
169168
List<BlockId> forkList = new LinkedList<>();
170169
LinkedList<BlockId> summary = new LinkedList<>();
171170
long syncBeginNumber = tronNetDelegate.getSyncBeginNumber();
@@ -327,9 +326,9 @@ private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection)
327326
for (PeerConnection peer : tronNetDelegate.getActivePeer()) {
328327
BlockId bid = peer.getSyncBlockToFetch().peek();
329328
if (blockId.equals(bid)) {
329+
peer.setBlockBothHave(blockId);
330330
peer.getSyncBlockToFetch().remove(bid);
331331
if (flag) {
332-
peer.setBlockBothHave(blockId);
333332
if (peer.getSyncBlockToFetch().isEmpty() && peer.isFetchAble()) {
334333
syncNext(peer);
335334
}

0 commit comments

Comments
 (0)