tendermint/types/params_test.go

89 lines
2.2 KiB
Go
Raw Normal View History

package types
import (
2017-12-14 01:13:31 -08:00
"bytes"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func newConsensusParams(blockSize, partSize int) ConsensusParams {
return ConsensusParams{
2017-12-19 10:43:15 -08:00
BlockSize: BlockSize{MaxBytes: blockSize},
BlockGossip: BlockGossip{BlockPartSizeBytes: partSize},
}
}
func TestConsensusParamsValidation(t *testing.T) {
testCases := []struct {
params ConsensusParams
valid bool
}{
{newConsensusParams(1, 1), true},
{newConsensusParams(1, 0), false},
{newConsensusParams(0, 1), false},
{newConsensusParams(0, 0), false},
{newConsensusParams(0, 10), false},
{newConsensusParams(10, -1), false},
{newConsensusParams(47*1024*1024, 400), true},
{newConsensusParams(10, 400), true},
{newConsensusParams(100*1024*1024, 400), true},
{newConsensusParams(101*1024*1024, 400), false},
{newConsensusParams(1024*1024*1024, 400), false},
}
for _, testCase := range testCases {
if testCase.valid {
assert.NoError(t, testCase.params.Validate(), "expected no error for valid params")
} else {
assert.Error(t, testCase.params.Validate(), "expected error for non valid params")
}
}
}
2017-12-14 01:13:31 -08:00
func makeParams(blockBytes, blockTx, blockGas, txBytes,
txGas, partSize int) ConsensusParams {
return ConsensusParams{
2017-12-19 10:43:15 -08:00
BlockSize: BlockSize{
2017-12-14 01:13:31 -08:00
MaxBytes: blockBytes,
MaxTxs: blockTx,
2017-12-21 14:52:06 -08:00
MaxGas: int64(blockGas),
2017-12-14 01:13:31 -08:00
},
2017-12-19 10:43:15 -08:00
TxSize: TxSize{
2017-12-14 01:13:31 -08:00
MaxBytes: txBytes,
2017-12-21 14:52:06 -08:00
MaxGas: int64(txGas),
2017-12-14 01:13:31 -08:00
},
2017-12-19 10:43:15 -08:00
BlockGossip: BlockGossip{
2017-12-14 01:13:31 -08:00
BlockPartSizeBytes: partSize,
},
}
}
func TestConsensusParamsHash(t *testing.T) {
params := []ConsensusParams{
makeParams(1, 2, 3, 4, 5, 6),
makeParams(7, 2, 3, 4, 5, 6),
makeParams(1, 7, 3, 4, 5, 6),
makeParams(1, 2, 7, 4, 5, 6),
makeParams(1, 2, 3, 7, 5, 6),
makeParams(1, 2, 3, 4, 7, 6),
makeParams(1, 2, 3, 4, 5, 7),
makeParams(6, 5, 4, 3, 2, 1),
}
hashes := make([][]byte, len(params))
for i := range params {
hashes[i] = params[i].Hash()
}
// make sure there are no duplicates...
// sort, then check in order for matches
sort.Slice(hashes, func(i, j int) bool {
return bytes.Compare(hashes[i], hashes[j]) < 0
})
for i := 0; i < len(hashes)-1; i++ {
assert.NotEqual(t, hashes[i], hashes[i+1])
}
}