mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-06 16:17:11 +00:00
genesis json tests and mv ConsensusParams to types
This commit is contained in:
+6
-8
@@ -10,8 +10,6 @@ import (
|
||||
"github.com/tendermint/go-crypto"
|
||||
"github.com/tendermint/go-wire/data"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
|
||||
cfg "github.com/tendermint/tendermint/config"
|
||||
)
|
||||
|
||||
//------------------------------------------------------------
|
||||
@@ -26,11 +24,11 @@ type GenesisValidator struct {
|
||||
|
||||
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
|
||||
type GenesisDoc struct {
|
||||
GenesisTime time.Time `json:"genesis_time"`
|
||||
ChainID string `json:"chain_id"`
|
||||
ConsensusParams *cfg.ConsensusParams `json:"consensus_params"`
|
||||
Validators []GenesisValidator `json:"validators"`
|
||||
AppHash data.Bytes `json:"app_hash"`
|
||||
GenesisTime time.Time `json:"genesis_time"`
|
||||
ChainID string `json:"chain_id"`
|
||||
ConsensusParams *ConsensusParams `json:"consensus_params"`
|
||||
Validators []GenesisValidator `json:"validators"`
|
||||
AppHash data.Bytes `json:"app_hash"`
|
||||
}
|
||||
|
||||
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
|
||||
@@ -61,7 +59,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
|
||||
}
|
||||
|
||||
if genDoc.ConsensusParams == nil {
|
||||
genDoc.ConsensusParams = cfg.DefaultConsensusParams()
|
||||
genDoc.ConsensusParams = DefaultConsensusParams()
|
||||
} else {
|
||||
if err := genDoc.ConsensusParams.Validate(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
)
|
||||
|
||||
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(`{"validators":[{"pub_key":
|
||||
{"type":"ed25519","data":"961EAB8752E51A03618502F55C2B6E09C38C65635C64CCF3173ED452CF86C957"},
|
||||
"amount":10,"name":""}]}`), // missing chain_id
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
_, err := GenesisDocFromJSON(testCase)
|
||||
assert.NotNil(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")
|
||||
|
||||
// create a base gendoc from struct
|
||||
baseGenDoc := &GenesisDoc{
|
||||
ChainID: "abc",
|
||||
Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}},
|
||||
}
|
||||
genDocBytes, err = json.Marshal(baseGenDoc)
|
||||
assert.Nil(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.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")
|
||||
genDoc, err = GenesisDocFromJSON(genDocBytes)
|
||||
assert.Nil(t, err, "expected no error for valid genDoc json")
|
||||
|
||||
// test with invalid consensus params
|
||||
genDoc.ConsensusParams.MaxBlockSizeBytes = 0
|
||||
genDocBytes, err = json.Marshal(genDoc)
|
||||
assert.Nil(t, err, "error marshalling genDoc")
|
||||
genDoc, err = GenesisDocFromJSON(genDocBytes)
|
||||
assert.NotNil(t, err, "expected error for genDoc json with block size of 0")
|
||||
}
|
||||
|
||||
func TestConsensusParams(t *testing.T) {
|
||||
testCases := []struct {
|
||||
params *ConsensusParams
|
||||
valid bool
|
||||
}{
|
||||
{&ConsensusParams{1, 1}, true},
|
||||
{&ConsensusParams{1, 0}, false},
|
||||
{&ConsensusParams{0, 1}, false},
|
||||
{&ConsensusParams{0, 0}, false},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
if testCase.valid {
|
||||
assert.Nil(t, testCase.params.Validate(), "expected no error for valid params")
|
||||
} else {
|
||||
assert.NotNil(t, testCase.params.Validate(), "expected error for non valid params")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ConsensusParams contains consensus critical parameters
|
||||
// that determine the validity of blocks.
|
||||
type ConsensusParams struct {
|
||||
MaxBlockSizeBytes int `json:"max_block_size_bytes"`
|
||||
BlockPartSizeBytes int `json:"block_part_size_bytes"`
|
||||
}
|
||||
|
||||
// DefaultConsensusParams returns a default ConsensusParams.
|
||||
func DefaultConsensusParams() *ConsensusParams {
|
||||
return &ConsensusParams{
|
||||
MaxBlockSizeBytes: 22020096, // 21MB
|
||||
BlockPartSizeBytes: 65536, // 64kB,
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if params.MaxBlockSizeBytes <= 0 {
|
||||
return errors.Errorf("MaxBlockSizeBytes must be greater than 0. Got %d", params.MaxBlockSizeBytes)
|
||||
}
|
||||
if params.BlockPartSizeBytes <= 0 {
|
||||
return errors.Errorf("BlockPartSizeBytes must be greater than 0. Got %d", params.BlockPartSizeBytes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user