refactor(consensus): fix config object not reference to same one and concurrency issues and refactor access pattern#2146
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
c4430e6 to
109c33b
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the XDPoS consensus engine's configuration access patterns to improve thread safety and reduce coupling. It prevents potential race conditions when reading V2Config from multiple goroutines by returning defensive copies, centralizes config access through a new Config() method on XDPoS_v2, and uses json.Decoder with UseNumber() in deepCloneJSON to preserve numeric precision.
Changes:
params/config.go: BothGetCurrentConfig()andConfig(round)now return a value copy of the config struct instead of the original pointer, preventing callers from mutating shared config.consensus/XDPoS/engines/engine_v2/engine.go: AddsConfig(r uint64)method as a centralized accessor, usesUseNumber()indeepCloneJSON, and removes an unnecessaryreturnstatement.eth/hooks/engine_v2_hooks.go: UpdatesHookPenaltyandHookRewardto retrieve config through the engine (viabc.Engine()) rather than directly from the chain config.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
params/config.go |
Returns defensive copies of V2Config in Config() and GetCurrentConfig() to prevent shared mutation |
consensus/XDPoS/engines/engine_v2/engine.go |
Adds Config() method, uses json.Decoder+UseNumber() in deepCloneJSON, removes bare return |
eth/hooks/engine_v2_hooks.go |
Hooks now resolve config via bc.Engine() cast to *XDPoS.XDPoS rather than the chain config |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -855,7 +855,10 @@ func (v2 *V2) Config(round uint64) *V2Config { | |||
| break | |||
| } | |||
| } | |||
| return v2.AllConfigs[index] | |||
|
|
|||
| // avoid config is changed by other goroutines | |||
| cfg := *v2.AllConfigs[index] | |||
| return &cfg | |||
There was a problem hiding this comment.
The V2.Config(round uint64) function accesses v2.configIndex and v2.AllConfigs without holding v2.lock, while the similar GetCurrentConfig() correctly acquires v2.lock.RLock() before accessing v2.CurrentConfig.
Since v2.configIndex is written by BuildConfigIndex() and v2.AllConfigs is a shared map (read at line 860), concurrent calls to Config(round) while another goroutine runs BuildConfigIndex() — or any code that modifies the underlying maps — would be a data race. The comment on line 859 ("avoid config is changed by other goroutines") acknowledges the intent to fix this, but the lock is not actually acquired.
The fix should add v2.lock.RLock() / v2.lock.RUnlock() at the start of Config(round uint64), consistent with how GetCurrentConfig() and UpdateConfig() protect accesses to shared fields.
There was a problem hiding this comment.
good suggustion
eth/hooks/engine_v2_hooks.go
Outdated
| XDPoSEngine, ok := bc.Engine().(*XDPoS.XDPoS) | ||
| if XDPoSEngine == nil || !ok { | ||
| return []common.Address{}, fmt.Errorf("[HookPenalty] XDPoS not found") | ||
| } | ||
| currentConfig := XDPoSEngine.EngineV2.Config(uint64(round)) |
There was a problem hiding this comment.
In both HookPenalty and HookReward, the new code performs a redundant bc.Engine().(*XDPoS.XDPoS) type assertion. The function's outer parameter adaptor is already the *XDPoS.XDPoS engine — this is verified in eth/backend.go:357 where c := eth.engine.(*XDPoS.XDPoS) is passed directly as adaptor. bc.Engine() returns the same eth.engine. The added type assertion is therefore unnecessary, and calling adaptor.EngineV2.Config(uint64(round)) directly would be simpler and eliminate the extra runtime check.
| XDPoSEngine, ok := bc.Engine().(*XDPoS.XDPoS) | |
| if XDPoSEngine == nil || !ok { | |
| return []common.Address{}, fmt.Errorf("[HookPenalty] XDPoS not found") | |
| } | |
| currentConfig := XDPoSEngine.EngineV2.Config(uint64(round)) | |
| currentConfig := adaptor.EngineV2.Config(uint64(round)) |
eth/hooks/engine_v2_hooks.go
Outdated
| XDPoSEngine, ok := bc.Engine().(*XDPoS.XDPoS) | ||
| if XDPoSEngine == nil || !ok { | ||
| return nil, fmt.Errorf("[HookReward] XDPoS not found") | ||
| } | ||
| currentConfig := XDPoSEngine.EngineV2.Config(uint64(round)) |
There was a problem hiding this comment.
Same redundant bc.Engine().(*XDPoS.XDPoS) assertion as in the HookPenalty closure (see comment on lines 102–106). The adaptor parameter already holds the *XDPoS.XDPoS engine, so adaptor.EngineV2.Config(uint64(round)) should be used directly.
| XDPoSEngine, ok := bc.Engine().(*XDPoS.XDPoS) | |
| if XDPoSEngine == nil || !ok { | |
| return nil, fmt.Errorf("[HookReward] XDPoS not found") | |
| } | |
| currentConfig := XDPoSEngine.EngineV2.Config(uint64(round)) | |
| currentConfig := adaptor.EngineV2.Config(uint64(round)) |
refactor(consensus): fix config concurrency issues and refactor access pattern
These changes prevent race conditions when accessing V2 configs from multiple
goroutines and establish a clearer separation of concerns by accessing
configuration through the consensus engine.
Changes:
Types of changes
What types of changes does your code introduce to XDC network?
Put an
✅in the boxes that applyImpacted Components
Which parts of the codebase does this PR touch?
Put an
✅in the boxes that applyChecklist
Put an
✅in the boxes once you have confirmed below actions (or provide reasons on not doing so) that