-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protection check for stakingV4 configs #4983
Merged
mariusmihaic
merged 6 commits into
feat/staking-v4
from
MX-13669-stakingv4-epochs-protection
Feb 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba6d253
FEAT: Add first version of checking stakingV4 config
mariusmihaic 070ac93
Merge branch 'feat/staking-v4' into MX-13669-stakingv4-epochs-protection
mariusmihaic e631a64
FEAT: Move config checker in separate file
mariusmihaic 187972c
FEAT: Add unit tests for configChecker.go
mariusmihaic 32181f0
FIX: Unit test
mariusmihaic e67fd44
FIX: After review
mariusmihaic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,55 +2,41 @@ package config | |
|
||
import ( | ||
"fmt" | ||
|
||
logger "github.com/multiversx/mx-chain-logger-go" | ||
) | ||
|
||
var log = logger.GetOrCreate("configChecker") | ||
|
||
// SanityCheckEnableEpochsStakingV4 checks if the enable epoch configs for stakingV4 are set correctly | ||
func SanityCheckEnableEpochsStakingV4(cfg *Configs) error { | ||
enableEpochsCfg := cfg.EpochConfig.EnableEpochs | ||
err := checkStakingV4EpochsOrder(enableEpochsCfg) | ||
if err != nil { | ||
return err | ||
if !areStakingV4StepsInOrder(enableEpochsCfg) { | ||
return errStakingV4StepsNotInOrder | ||
} | ||
|
||
numOfShards := cfg.GeneralConfig.GeneralSettings.GenesisMaxNumberOfShards | ||
return checkStakingV4MaxNodesChangeCfg(enableEpochsCfg, numOfShards) | ||
} | ||
|
||
func checkStakingV4EpochsOrder(enableEpochsCfg EnableEpochs) error { | ||
stakingV4StepsInOrder := (enableEpochsCfg.StakingV4Step1EnableEpoch < enableEpochsCfg.StakingV4Step2EnableEpoch) && | ||
(enableEpochsCfg.StakingV4Step2EnableEpoch < enableEpochsCfg.StakingV4Step3EnableEpoch) | ||
|
||
if !stakingV4StepsInOrder { | ||
return errStakingV4StepsNotInOrder | ||
} | ||
|
||
stakingV4StepsInExpectedOrder := (enableEpochsCfg.StakingV4Step1EnableEpoch == enableEpochsCfg.StakingV4Step2EnableEpoch-1) && | ||
func areStakingV4StepsInOrder(enableEpochsCfg EnableEpochs) bool { | ||
return (enableEpochsCfg.StakingV4Step1EnableEpoch == enableEpochsCfg.StakingV4Step2EnableEpoch-1) && | ||
(enableEpochsCfg.StakingV4Step2EnableEpoch == enableEpochsCfg.StakingV4Step3EnableEpoch-1) | ||
if !stakingV4StepsInExpectedOrder { | ||
log.Warn("staking v4 enable epoch steps should be in cardinal order " + | ||
"(e.g.: StakingV4Step1EnableEpoch = 2, StakingV4Step2EnableEpoch = 3, StakingV4Step3EnableEpoch = 4)" + | ||
"; can leave them as they are for playground purposes" + | ||
", but DO NOT use them in production, since system's behavior is undefined") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkStakingV4MaxNodesChangeCfg(enableEpochsCfg EnableEpochs, numOfShards uint32) error { | ||
maxNodesChangeCfg := enableEpochsCfg.MaxNodesChangeEnableEpoch | ||
if len(maxNodesChangeCfg) <= 1 { | ||
return errNotEnoughMaxNodesChanges | ||
} | ||
|
||
maxNodesConfigAdaptedForStakingV4 := false | ||
|
||
for idx, currMaxNodesChangeCfg := range enableEpochsCfg.MaxNodesChangeEnableEpoch { | ||
for idx, currMaxNodesChangeCfg := range maxNodesChangeCfg { | ||
if currMaxNodesChangeCfg.EpochEnable == enableEpochsCfg.StakingV4Step3EnableEpoch { | ||
|
||
maxNodesConfigAdaptedForStakingV4 = true | ||
|
||
if idx == 0 { | ||
log.Warn(fmt.Sprintf("found config change in MaxNodesChangeEnableEpoch for StakingV4Step3EnableEpoch = %d, ", enableEpochsCfg.StakingV4Step3EnableEpoch) + | ||
"but no previous config change entry in MaxNodesChangeEnableEpoch, DO NOT use this config in production") | ||
return fmt.Errorf("found config change in MaxNodesChangeEnableEpoch for StakingV4Step3EnableEpoch = %d, but %w ", | ||
enableEpochsCfg.StakingV4Step3EnableEpoch, errNoMaxNodesConfigBeforeStakingV4) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could have dropped the |
||
prevMaxNodesChange := enableEpochsCfg.MaxNodesChangeEnableEpoch[idx-1] | ||
prevMaxNodesChange := maxNodesChangeCfg[idx-1] | ||
err := checkMaxNodesChangedCorrectly(prevMaxNodesChange, currMaxNodesChangeCfg, numOfShards) | ||
if err != nil { | ||
return err | ||
|
@@ -70,9 +56,7 @@ func checkStakingV4MaxNodesChangeCfg(enableEpochsCfg EnableEpochs, numOfShards u | |
|
||
func checkMaxNodesChangedCorrectly(prevMaxNodesChange MaxNodesChangeConfig, currMaxNodesChange MaxNodesChangeConfig, numOfShards uint32) error { | ||
if prevMaxNodesChange.NodesToShufflePerShard != currMaxNodesChange.NodesToShufflePerShard { | ||
log.Warn("previous MaxNodesChangeEnableEpoch.NodesToShufflePerShard != MaxNodesChangeEnableEpoch.NodesToShufflePerShard" + | ||
" with EnableEpoch = StakingV4Step3EnableEpoch; can leave them as they are for playground purposes," + | ||
" but DO NOT use them in production, since this will influence rewards") | ||
return errMismatchNodesToShuffle | ||
} | ||
|
||
totalShuffled := (numOfShards + 1) * prevMaxNodesChange.NodesToShufflePerShard | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,12 @@ package config | |
|
||
import "errors" | ||
|
||
var errStakingV4StepsNotInOrder = errors.New("staking v4 enable epochs are not in ascending order; expected StakingV4Step1EnableEpoch < StakingV4Step2EnableEpoch < StakingV4Step3EnableEpoch") | ||
var errStakingV4StepsNotInOrder = errors.New("staking v4 enable epoch steps should be in cardinal order(e.g.: StakingV4Step1EnableEpoch = 2, StakingV4Step2EnableEpoch = 3, StakingV4Step3EnableEpoch = 4)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 good and comprehensive errors. |
||
|
||
var errNotEnoughMaxNodesChanges = errors.New("not enough entries in MaxNodesChangeEnableEpoch config; expected one entry before stakingV4 and another one starting StakingV4Step3EnableEpoch") | ||
|
||
var errNoMaxNodesConfigBeforeStakingV4 = errors.New("no previous config change entry in MaxNodesChangeEnableEpoch before entry with EpochEnable = StakingV4Step3EnableEpoch") | ||
|
||
var errMismatchNodesToShuffle = errors.New("previous MaxNodesChangeEnableEpoch.NodesToShufflePerShard != MaxNodesChangeEnableEpoch.NodesToShufflePerShard with EnableEpoch = StakingV4Step3EnableEpoch") | ||
|
||
var errNoMaxNodesConfigChangeForStakingV4 = errors.New("no MaxNodesChangeEnableEpoch config found for EpochEnable = StakingV4Step3EnableEpoch") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment to public function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, added it