From 5feeb65cf0df559a3467a6a9d2f7271a94328178 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 21 Sep 2017 14:34:36 -0400 Subject: [PATCH] dont use pointers for ConsensusParams --- consensus/replay_test.go | 4 ++-- state/state.go | 4 +++- types/genesis.go | 5 +++-- types/genesis_test.go | 10 +++++----- types/params.go | 26 +++++++++++++------------- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 3fa885176..03ca6c8d7 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -570,13 +570,13 @@ func stateAndStore(config *cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBl type mockBlockStore struct { config *cfg.Config - params *types.ConsensusParams + params types.ConsensusParams chain []*types.Block commits []*types.Commit } // TODO: NewBlockStore(db.NewMemDB) ... -func NewMockBlockStore(config *cfg.Config, params *types.ConsensusParams) *mockBlockStore { +func NewMockBlockStore(config *cfg.Config, params types.ConsensusParams) *mockBlockStore { return &mockBlockStore{config, params, nil, nil} } diff --git a/state/state.go b/state/state.go index 0ea15acaf..1374fcf46 100644 --- a/state/state.go +++ b/state/state.go @@ -263,7 +263,9 @@ func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) { // Params returns the consensus parameters used for // validating blocks -func (s *State) Params() *types.ConsensusParams { +func (s *State) Params() types.ConsensusParams { + // TODO: this should move into the State proper + // when we allow the app to change it return s.GenesisDoc.ConsensusParams } diff --git a/types/genesis.go b/types/genesis.go index 822a4e166..06b3d124c 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -26,7 +26,7 @@ type GenesisValidator struct { type GenesisDoc struct { GenesisTime time.Time `json:"genesis_time"` ChainID string `json:"chain_id"` - ConsensusParams *ConsensusParams `json:"consensus_params"` + ConsensusParams ConsensusParams `json:"consensus_params"` Validators []GenesisValidator `json:"validators"` AppHash data.Bytes `json:"app_hash"` } @@ -58,7 +58,8 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error { return errors.Errorf("Genesis doc must include non-empty chain_id") } - if genDoc.ConsensusParams == nil { + var emptyParams ConsensusParams + if genDoc.ConsensusParams == emptyParams { genDoc.ConsensusParams = DefaultConsensusParams() } else { if err := genDoc.ConsensusParams.Validate(); err != nil { diff --git a/types/genesis_test.go b/types/genesis_test.go index 75184e5a6..d06a70dab 100644 --- a/types/genesis_test.go +++ b/types/genesis_test.go @@ -60,10 +60,10 @@ func TestGenesis(t *testing.T) { assert.Error(t, err, "expected error for genDoc json with block size of 0") } -func newConsensusParams(blockSize, partSize int) *ConsensusParams { - return &ConsensusParams{ - BlockSizeParams: &BlockSizeParams{MaxBytes: blockSize}, - BlockGossipParams: &BlockGossipParams{BlockPartSizeBytes: partSize}, +func newConsensusParams(blockSize, partSize int) ConsensusParams { + return ConsensusParams{ + BlockSizeParams: BlockSizeParams{MaxBytes: blockSize}, + BlockGossipParams: BlockGossipParams{BlockPartSizeBytes: partSize}, } } @@ -71,7 +71,7 @@ func newConsensusParams(blockSize, partSize int) *ConsensusParams { func TestConsensusParams(t *testing.T) { testCases := []struct { - params *ConsensusParams + params ConsensusParams valid bool }{ {newConsensusParams(1, 1), true}, diff --git a/types/params.go b/types/params.go index b55ceb8c3..cd3242192 100644 --- a/types/params.go +++ b/types/params.go @@ -11,9 +11,9 @@ const ( // ConsensusParams contains consensus critical parameters // that determine the validity of blocks. type ConsensusParams struct { - *BlockSizeParams `json:"block_size_params"` - *TxSizeParams `json:"tx_size_params"` - *BlockGossipParams `json:"block_gossip_params"` + BlockSizeParams `json:"block_size_params"` + TxSizeParams `json:"tx_size_params"` + BlockGossipParams `json:"block_gossip_params"` } // BlockSizeParams contain limits on the block size. @@ -35,8 +35,8 @@ type BlockGossipParams struct { } // DefaultConsensusParams returns a default ConsensusParams. -func DefaultConsensusParams() *ConsensusParams { - return &ConsensusParams{ +func DefaultConsensusParams() ConsensusParams { + return ConsensusParams{ DefaultBlockSizeParams(), DefaultTxSizeParams(), DefaultBlockGossipParams(), @@ -44,8 +44,8 @@ func DefaultConsensusParams() *ConsensusParams { } // DefaultBlockSizeParams returns a default BlockSizeParams. -func DefaultBlockSizeParams() *BlockSizeParams { - return &BlockSizeParams{ +func DefaultBlockSizeParams() BlockSizeParams { + return BlockSizeParams{ MaxBytes: 22020096, // 21MB MaxTxs: 100000, MaxGas: -1, @@ -53,16 +53,16 @@ func DefaultBlockSizeParams() *BlockSizeParams { } // DefaultTxSizeParams returns a default TxSizeParams. -func DefaultTxSizeParams() *TxSizeParams { - return &TxSizeParams{ +func DefaultTxSizeParams() TxSizeParams { + return TxSizeParams{ MaxBytes: 10240, // 10kB MaxGas: -1, } } // DefaultBlockGossipParams returns a default BlockGossipParams. -func DefaultBlockGossipParams() *BlockGossipParams { - return &BlockGossipParams{ +func DefaultBlockGossipParams() BlockGossipParams { + return BlockGossipParams{ BlockPartSizeBytes: 65536, // 64kB, } } @@ -79,9 +79,9 @@ func (params *ConsensusParams) Validate() error { } // ensure blocks aren't too big - if cp.BlockSizeParams.MaxBytes > maxBlockSizeBytes { + if params.BlockSizeParams.MaxBytes > maxBlockSizeBytes { return errors.Errorf("BlockSizeParams.MaxBytes is too big. %d > %d", - cp.BlockSizeParams.MaxBytes, maxBlockSizeBytes) + params.BlockSizeParams.MaxBytes, maxBlockSizeBytes) } return nil }