Browse Source

fixes from review

pull/650/head
Ethan Buchman 7 years ago
parent
commit
715e74186c
2 changed files with 27 additions and 15 deletions
  1. +16
    -15
      types/genesis_test.go
  2. +11
    -0
      types/params.go

+ 16
- 15
types/genesis_test.go View File

@ -12,11 +12,12 @@ func TestGenesis(t *testing.T) {
// test some bad ones from raw json
testCases := [][]byte{
[]byte{}, // empty
[]byte{1, 1, 1, 1, 1}, // junk
[]byte(`{}`), // empty
[]byte(`{"chain_id": "mychain"}`), // missing validators
[]byte(`{"validators": [{"data":abcd}`), // missing validators
[]byte{}, // empty
[]byte{1, 1, 1, 1, 1}, // junk
[]byte(`{}`), // empty
[]byte(`{"chain_id": "mychain"}`), // missing validators
[]byte(`{"chain_id": "mychain", "validators": []`), // missing validators
[]byte(`{"chain_id": "mychain", "validators": [{}]`), // missing validators
[]byte(`{"validators":[{"pub_key":
{"type":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},
"amount":10,"name":""}]}`), // missing chain_id
@ -24,13 +25,13 @@ func TestGenesis(t *testing.T) {
for _, testCase := range testCases {
_, err := GenesisDocFromJSON(testCase)
assert.NotNil(t, err, "expected error for empty genDoc json")
assert.Error(t, err, "expected error for empty genDoc json")
}
// test a good one by raw json
genDocBytes := []byte(`{"genesis_time":"0001-01-01T00:00:00Z","chain_id":"test-chain-QDKdJr","consensus_params":null,"validators":[{"pub_key":{"type":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},"amount":10,"name":""}],"app_hash":""}`)
_, err := GenesisDocFromJSON(genDocBytes)
assert.Nil(t, err, "expected no error for good genDoc json")
assert.NoError(t, err, "expected no error for good genDoc json")
// create a base gendoc from struct
baseGenDoc := &GenesisDoc{
@ -38,25 +39,25 @@ func TestGenesis(t *testing.T) {
Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
}
genDocBytes, err = json.Marshal(baseGenDoc)
assert.Nil(t, err, "error marshalling genDoc")
assert.NoError(t, err, "error marshalling genDoc")
// test base gendoc and check consensus params were filled
genDoc, err := GenesisDocFromJSON(genDocBytes)
assert.Nil(t, err, "expected no error for valid genDoc json")
assert.NoError(t, err, "expected no error for valid genDoc json")
assert.NotNil(t, genDoc.ConsensusParams, "expected consensus params to be filled in")
// create json with consensus params filled
genDocBytes, err = json.Marshal(genDoc)
assert.Nil(t, err, "error marshalling genDoc")
assert.NoError(t, err, "error marshalling genDoc")
genDoc, err = GenesisDocFromJSON(genDocBytes)
assert.Nil(t, err, "expected no error for valid genDoc json")
assert.NoError(t, err, "expected no error for valid genDoc json")
// test with invalid consensus params
genDoc.ConsensusParams.BlockSizeParams.MaxBytes = 0
genDocBytes, err = json.Marshal(genDoc)
assert.Nil(t, err, "error marshalling genDoc")
assert.NoError(t, err, "error marshalling genDoc")
genDoc, err = GenesisDocFromJSON(genDocBytes)
assert.NotNil(t, err, "expected error for genDoc json with block size of 0")
assert.Error(t, err, "expected error for genDoc json with block size of 0")
}
func newConsensusParams(blockSize, partSize int) *ConsensusParams {
@ -80,9 +81,9 @@ func TestConsensusParams(t *testing.T) {
}
for _, testCase := range testCases {
if testCase.valid {
assert.Nil(t, testCase.params.Validate(), "expected no error for valid params")
assert.NoError(t, testCase.params.Validate(), "expected no error for valid params")
} else {
assert.NotNil(t, testCase.params.Validate(), "expected error for non valid params")
assert.Error(t, testCase.params.Validate(), "expected error for non valid params")
}
}
}

+ 11
- 0
types/params.go View File

@ -4,6 +4,10 @@ import (
"github.com/pkg/errors"
)
const (
maxBlockSizeBytes = 104857600 // 100MB
)
// ConsensusParams contains consensus critical parameters
// that determine the validity of blocks.
type ConsensusParams struct {
@ -66,11 +70,18 @@ func DefaultBlockGossipParams() *BlockGossipParams {
// Validate validates the ConsensusParams to ensure all values
// are within their allowed limits, and returns an error if they are not.
func (params *ConsensusParams) Validate() error {
// ensure some values are greater than 0
if params.BlockSizeParams.MaxBytes <= 0 {
return errors.Errorf("BlockSizeParams.MaxBytes must be greater than 0. Got %d", params.BlockSizeParams.MaxBytes)
}
if params.BlockGossipParams.BlockPartSizeBytes <= 0 {
return errors.Errorf("BlockGossipParams.BlockPartSizeBytes must be greater than 0. Got %d", params.BlockGossipParams.BlockPartSizeBytes)
}
// ensure blocks aren't too big
if cp.BlockSizeParams.MaxBytes > maxBlockSizeBytes {
return errors.Errorf("BlockSizeParams.MaxBytes is too big. %d > %d",
cp.BlockSizeParams.MaxBytes, maxBlockSizeBytes)
}
return nil
}

Loading…
Cancel
Save