Skip to content

Commit 7fdb3c5

Browse files
warku123claude
andcommitted
fix(metrics): use HashSet and add fork rollback guard for SR set change
- Replace ConcurrentHashMap.newKeySet() with HashSet since applyBlock() is only called from a single-threaded path (synchronized on blockLock) - Add fork rollback detection: when nextMaintenanceTime < lastNextMaintenanceTime, reinitialize in-memory state instead of diffing, preventing phantom SR changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7f351bf commit 7fdb3c5

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

framework/src/main/java/org/tron/core/metrics/blockchain/BlockChainMetricManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public class BlockChainMetricManager {
4545
private long failProcessBlockNum = 0;
4646
@Setter
4747
private String failProcessBlockReason = "";
48-
private final Set<String> lastActiveWitnesses = ConcurrentHashMap.newKeySet();
48+
// Only accessed from block processing thread (synchronized on blockLock)
49+
private final Set<String> lastActiveWitnesses = new HashSet<>();
4950
// To control SR set change metric update logic, -1 means not initialized
5051
private long lastNextMaintenanceTime = -1;
5152

@@ -192,7 +193,13 @@ public void applyBlock(BlockCapsule block) {
192193
.stream()
193194
.map(w -> StringUtil.encode58Check(w.toByteArray()))
194195
.collect(Collectors.toSet());
195-
recordSrSetChange(currentWitnesses);
196+
if (nextMaintenanceTime < lastNextMaintenanceTime) {
197+
// Fork rollback detected — reinitialize instead of diffing
198+
lastActiveWitnesses.clear();
199+
lastActiveWitnesses.addAll(currentWitnesses);
200+
} else {
201+
recordSrSetChange(currentWitnesses);
202+
}
196203
lastNextMaintenanceTime = nextMaintenanceTime;
197204
}
198205
}

0 commit comments

Comments
 (0)