From 030fd0023284b50abfcce90534ed21f9d9713df4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 13 Dec 2017 19:26:44 +0100 Subject: [PATCH] Added tests for applying consensus param changes --- state/state.go | 40 +++++++++++++++++++++++++++- state/state_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ types/params.go | 38 -------------------------- 3 files changed, 104 insertions(+), 39 deletions(-) diff --git a/state/state.go b/state/state.go index 3b04cd69c..34383cb15 100644 --- a/state/state.go +++ b/state/state.go @@ -251,7 +251,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ // Update validator accums and set state variables nextValSet.IncrementAccum(1) - nextParams := s.Params.ApplyChanges( + nextParams := applyChanges(s.Params, abciResponses.EndBlock.ConsensusParamChanges) s.setBlockAndValidators(header.Height, @@ -263,6 +263,44 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ } +// applyChanges returns a new param set, overriding any +// parameter that is non-zero in argument +func applyChanges(p types.ConsensusParams, + c *abci.ConsensusParams) types.ConsensusParams { + + if c == nil { + return p + } + res := p + // we must defensively consider any structs may be nil + if c.BlockSizeParams != nil { + + if c.BlockSizeParams.MaxBytes != 0 { + res.BlockSizeParams.MaxBytes = int(c.BlockSizeParams.MaxBytes) + } + if c.BlockSizeParams.MaxTxs != 0 { + res.BlockSizeParams.MaxTxs = int(c.BlockSizeParams.MaxTxs) + } + if c.BlockSizeParams.MaxGas != 0 { + res.BlockSizeParams.MaxGas = int(c.BlockSizeParams.MaxGas) + } + } + if c.TxSizeParams != nil { + if c.TxSizeParams.MaxBytes != 0 { + res.TxSizeParams.MaxBytes = int(c.TxSizeParams.MaxBytes) + } + if c.TxSizeParams.MaxGas != 0 { + res.TxSizeParams.MaxGas = int(c.TxSizeParams.MaxGas) + } + } + if c.BlockGossipParams != nil { + if c.BlockGossipParams.BlockPartSizeBytes != 0 { + res.BlockGossipParams.BlockPartSizeBytes = int(c.BlockGossipParams.BlockPartSizeBytes) + } + } + return res +} + func (s *State) setBlockAndValidators(height int64, newTxs int64, blockID types.BlockID, blockTime time.Time, prevValSet, nextValSet *types.ValidatorSet, diff --git a/state/state_test.go b/state/state_test.go index 2c403a425..d560f9532 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -192,6 +192,71 @@ func TestValidatorChangesSaveLoad(t *testing.T) { } } +func makeParams(blockBytes, blockTx, blockGas, txBytes, + txGas, partSize int) types.ConsensusParams { + + return types.ConsensusParams{ + BlockSizeParams: types.BlockSizeParams{ + MaxBytes: blockBytes, + MaxTxs: blockTx, + MaxGas: blockGas, + }, + TxSizeParams: types.TxSizeParams{ + MaxBytes: txBytes, + MaxGas: txGas, + }, + BlockGossipParams: types.BlockGossipParams{ + BlockPartSizeBytes: partSize, + }, + } +} + +func TestApplyChanges(t *testing.T) { + initParams := makeParams(1, 2, 3, 4, 5, 6) + + cases := [...]struct { + init types.ConsensusParams + changes *abci.ConsensusParams + expected types.ConsensusParams + }{ + 0: {initParams, nil, initParams}, + 1: {initParams, &abci.ConsensusParams{}, initParams}, + 2: {initParams, + &abci.ConsensusParams{ + TxSizeParams: &abci.TxSizeParams{ + MaxBytes: 123, + }, + }, + makeParams(1, 2, 3, 123, 5, 6)}, + 3: {initParams, + &abci.ConsensusParams{ + BlockSizeParams: &abci.BlockSizeParams{ + MaxTxs: 44, + MaxGas: 55, + }, + }, + makeParams(1, 44, 55, 4, 5, 6)}, + 4: {initParams, + &abci.ConsensusParams{ + BlockSizeParams: &abci.BlockSizeParams{ + MaxTxs: 789, + }, + TxSizeParams: &abci.TxSizeParams{ + MaxGas: 888, + }, + BlockGossipParams: &abci.BlockGossipParams{ + BlockPartSizeBytes: 2002, + }, + }, + makeParams(1, 789, 3, 4, 888, 2002)}, + } + + for i, tc := range cases { + res := applyChanges(tc.init, tc.changes) + assert.Equal(t, tc.expected, res, "case %d", i) + } +} + func makeHeaderPartsResponses(state *State, height int64, pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) { diff --git a/types/params.go b/types/params.go index 4047913c3..322cba612 100644 --- a/types/params.go +++ b/types/params.go @@ -2,8 +2,6 @@ package types import ( "github.com/pkg/errors" - - abci "github.com/tendermint/abci/types" ) const ( @@ -18,42 +16,6 @@ type ConsensusParams struct { BlockGossipParams `json:"block_gossip_params"` } -// ApplyChanges returns a new param set, overriding any -// parameter that is non-zero in argument -func (p ConsensusParams) ApplyChanges(c *abci.ConsensusParams) ConsensusParams { - if c == nil { - return p - } - res := p - // we must defensively consider any structs may be nil - if c.BlockSizeParams != nil { - - if c.BlockSizeParams.MaxBytes != 0 { - res.BlockSizeParams.MaxBytes = int(c.BlockSizeParams.MaxBytes) - } - if c.BlockSizeParams.MaxTxs != 0 { - res.BlockSizeParams.MaxTxs = int(c.BlockSizeParams.MaxTxs) - } - if c.BlockSizeParams.MaxGas != 0 { - res.BlockSizeParams.MaxGas = int(c.BlockSizeParams.MaxGas) - } - } - if c.TxSizeParams != nil { - if c.TxSizeParams.MaxBytes != 0 { - res.TxSizeParams.MaxBytes = int(c.TxSizeParams.MaxBytes) - } - if c.TxSizeParams.MaxGas != 0 { - res.TxSizeParams.MaxGas = int(c.TxSizeParams.MaxGas) - } - } - if c.BlockGossipParams != nil { - if c.BlockGossipParams.BlockPartSizeBytes != 0 { - res.BlockGossipParams.BlockPartSizeBytes = int(c.BlockGossipParams.BlockPartSizeBytes) - } - } - return res -} - // BlockSizeParams contain limits on the block size. type BlockSizeParams struct { MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB