update validate/complete logic for consensus params

This commit is contained in:
William Banfield
2022-01-28 17:41:02 -05:00
parent f50721c36a
commit 31e61d937c
6 changed files with 22 additions and 7 deletions

View File

@@ -455,7 +455,7 @@ func updateState(
if abciResponses.EndBlock.ConsensusParamUpdates != nil {
// NOTE: must not mutate s.ConsensusParams
nextParams = state.ConsensusParams.UpdateConsensusParams(abciResponses.EndBlock.ConsensusParamUpdates)
err := nextParams.ValidateConsensusParams()
err := nextParams.Validate()
if err != nil {
return state, fmt.Errorf("error updating consensus params: %w", err)
}

View File

@@ -239,7 +239,9 @@ func FromProto(pb *tmstate.State) (*State, error) {
}
state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
state.ConsensusParams = types.ConsensusParamsFromProto(pb.ConsensusParams)
c := types.ConsensusParamsFromProto(pb.ConsensusParams)
(&c).Complete()
state.ConsensusParams = c
state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
state.LastResultsHash = pb.LastResultsHash
state.AppHash = pb.AppHash

View File

@@ -269,7 +269,7 @@ func (c *Client) ConsensusParams(ctx context.Context, height *int64) (*coretypes
}
// Validate res.
if err := res.ConsensusParams.ValidateConsensusParams(); err != nil {
if err := res.ConsensusParams.Validate(); err != nil {
return nil, err
}
if res.BlockHeight <= 0 {

View File

@@ -113,7 +113,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
if genDoc.ConsensusParams == nil {
genDoc.ConsensusParams = DefaultConsensusParams()
} else if err := genDoc.ConsensusParams.ValidateConsensusParams(); err != nil {
} else if err := genDoc.ConsensusParams.Validate(); err != nil {
return err
}

View File

@@ -145,9 +145,22 @@ func (val *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
return false
}
func (params *ConsensusParams) Complete() {
if params.Synchrony.MessageDelay == 0 && params.Synchrony.Precision == 0 {
params.Synchrony = DefaultSynchronyParams()
}
}
func (params *ConsensusParams) ValidateAndComplete() error {
if params == nil {
params = DefaultConsensusParams()
}
params.Complete()
return params.Validate()
}
// Validate validates the ConsensusParams to ensure all values are within their
// allowed limits, and returns an error if they are not.
func (params ConsensusParams) ValidateConsensusParams() error {
func (params *ConsensusParams) Validate() error {
if params.Block.MaxBytes <= 0 {
return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
params.Block.MaxBytes)

View File

@@ -160,9 +160,9 @@ func TestConsensusParamsValidation(t *testing.T) {
}
for i, tc := range testCases {
if tc.valid {
assert.NoErrorf(t, tc.params.ValidateConsensusParams(), "expected no error for valid params (#%d)", i)
assert.NoErrorf(t, tc.params.Validate(), "expected no error for valid params (#%d)", i)
} else {
assert.Errorf(t, tc.params.ValidateConsensusParams(), "expected error for non valid params (#%d)", i)
assert.Errorf(t, tc.params.Validate(), "expected error for non valid params (#%d)", i)
}
}
}