fix(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.
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