Browse Source

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
pull/748/head
Emmanuel Odeke 7 years ago
parent
commit
bff069f83c
No known key found for this signature in database GPG Key ID: 1CA47A292F89DD40
2 changed files with 73 additions and 3 deletions
  1. +7
    -3
      types/params.go
  2. +66
    -0
      types/params_test.go

+ 7
- 3
types/params.go View File

@ -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
}

+ 66
- 0
types/params_test.go View File

@ -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)
}
}
}

Loading…
Cancel
Save