From bff069f83cc02c2432266572a4ebfd5a1a6044e4 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sun, 15 Oct 2017 15:29:31 -0600 Subject: [PATCH] types: ConsensusParams test + document the ranges/limits Fixes https://github.com/tendermint/tendermint/issues/747 Updates https://github.com/tendermint/tendermint/issues/693 * Document the unmentioned limits for ConsensusParams.Validate() * Make the limit for ConsensusParams.BlockSizeParams.MaxBytes clear at 100MiB --- types/params.go | 10 +++++-- types/params_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 types/params_test.go diff --git a/types/params.go b/types/params.go index 495d1fd48..dc811df25 100644 --- a/types/params.go +++ b/types/params.go @@ -5,7 +5,7 @@ import ( ) const ( - maxBlockSizeBytes = 104857600 // 100MB + _100MiB = 104857600 ) // ConsensusParams contains consensus critical parameters @@ -69,6 +69,10 @@ func DefaultBlockGossipParams() BlockGossipParams { // Validate validates the ConsensusParams to ensure all values // are within their allowed limits, and returns an error if they are not. +// Expecting: +// * BlockSizeParams.MaxBytes > 0 +// * BlockGossipParams.BlockPartSizeBytes > 0 +// * BlockSizeParams.MaxBytes <= 100MiB func (params *ConsensusParams) Validate() error { // ensure some values are greater than 0 if params.BlockSizeParams.MaxBytes <= 0 { @@ -79,9 +83,9 @@ func (params *ConsensusParams) Validate() error { } // ensure blocks aren't too big - if params.BlockSizeParams.MaxBytes > maxBlockSizeBytes { + if params.BlockSizeParams.MaxBytes > _100MiB { return errors.Errorf("BlockSizeParams.MaxBytes is too big. %d > %d", - params.BlockSizeParams.MaxBytes, maxBlockSizeBytes) + params.BlockSizeParams.MaxBytes, _100MiB) } return nil } diff --git a/types/params_test.go b/types/params_test.go new file mode 100644 index 000000000..17a5cc0b0 --- /dev/null +++ b/types/params_test.go @@ -0,0 +1,66 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/tendermint/tendermint/types" +) + +func TestConsensusParamsValidation(t *testing.T) { + tests := [...]struct { + params *types.ConsensusParams + wantErr string + }{ + {&types.ConsensusParams{}, "BlockSizeParams.MaxBytes must be greater than 0"}, + { + &types.ConsensusParams{BlockSizeParams: types.BlockSizeParams{MaxBytes: 10}}, + "BlockGossipParams.BlockPartSizeBytes must be greater than 0", + }, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 10}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: -1}, + }, "BlockGossipParams.BlockPartSizeBytes must be greater than 0", + }, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 10}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: 400}, + }, ""}, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 1024 * 1024 * 47}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: 400}, + }, "", + }, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 1024 * 1024 * 100}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: 400}, + }, "", + }, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 1024 * 1024 * 101}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: 400}, + }, "BlockSizeParams.MaxBytes is too big", + }, + { + &types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{MaxBytes: 1024 * 1024 * 1024}, + BlockGossipParams: types.BlockGossipParams{BlockPartSizeBytes: 400}, + }, "BlockSizeParams.MaxBytes is too big", + }, + } + + for i, tt := range tests { + err := tt.params.Validate() + if tt.wantErr != "" { + assert.Contains(t, err.Error(), tt.wantErr, "#%d: params: %#v", i, tt.params) + } else { + assert.Nil(t, err, "#%d: want nil error; Params: %#v", i, tt.params) + } + } +}