From 31e61d937ce329a2f1940169ee2ad247aa60bc87 Mon Sep 17 00:00:00 2001 From: William Banfield Date: Fri, 28 Jan 2022 17:41:02 -0500 Subject: [PATCH] update validate/complete logic for consensus params --- internal/state/execution.go | 2 +- internal/state/state.go | 4 +++- light/rpc/client.go | 2 +- types/genesis.go | 2 +- types/params.go | 15 ++++++++++++++- types/params_test.go | 4 ++-- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/internal/state/execution.go b/internal/state/execution.go index 96a680a5b..9052d6311 100644 --- a/internal/state/execution.go +++ b/internal/state/execution.go @@ -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) } diff --git a/internal/state/state.go b/internal/state/state.go index 20a36a1c4..b6a80b4c3 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -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 diff --git a/light/rpc/client.go b/light/rpc/client.go index 51dd6e3c0..0b4ad4ceb 100644 --- a/light/rpc/client.go +++ b/light/rpc/client.go @@ -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 { diff --git a/types/genesis.go b/types/genesis.go index f711a7090..49692961e 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -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 } diff --git a/types/params.go b/types/params.go index b4905c6f0..ebb9d532f 100644 --- a/types/params.go +++ b/types/params.go @@ -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) diff --git a/types/params_test.go b/types/params_test.go index 0aaf1e2b9..79c6b68aa 100644 --- a/types/params_test.go +++ b/types/params_test.go @@ -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) } } }