Browse Source

Added tests for applying consensus param changes

pull/972/head
Ethan Frey 7 years ago
committed by Ethan Buchman
parent
commit
030fd00232
3 changed files with 104 additions and 39 deletions
  1. +39
    -1
      state/state.go
  2. +65
    -0
      state/state_test.go
  3. +0
    -38
      types/params.go

+ 39
- 1
state/state.go View File

@ -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,


+ 65
- 0
state/state_test.go View File

@ -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) {


+ 0
- 38
types/params.go View File

@ -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


Loading…
Cancel
Save