save historical consensus params
This commit is contained in:
parent
70a744558c
commit
35521b553a
|
@ -37,6 +37,10 @@ type (
|
|||
ErrNoValSetForHeight struct {
|
||||
Height int64
|
||||
}
|
||||
|
||||
ErrNoConsensusParamsForHeight struct {
|
||||
Height int64
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrUnknownBlock) Error() string {
|
||||
|
@ -61,3 +65,7 @@ func (e ErrStateMismatch) Error() string {
|
|||
func (e ErrNoValSetForHeight) Error() string {
|
||||
return cmn.Fmt("Could not find validator set for height #%d", e.Height)
|
||||
}
|
||||
|
||||
func (e ErrNoConsensusParamsForHeight) Error() string {
|
||||
return cmn.Fmt("Could not find consensus params for height #%d", e.Height)
|
||||
}
|
||||
|
|
|
@ -279,7 +279,10 @@ func (s *State) ApplyBlock(txEventPublisher types.TxEventPublisher, proxyAppConn
|
|||
fail.Fail() // XXX
|
||||
|
||||
// now update the block and validators
|
||||
s.SetBlockAndValidators(block.Header, partsHeader, abciResponses)
|
||||
err = s.SetBlockAndValidators(block.Header, partsHeader, abciResponses)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Commit failed for application: %v", err)
|
||||
}
|
||||
|
||||
// lock mempool, commit state, update mempoool
|
||||
err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool)
|
||||
|
|
104
state/state.go
104
state/state.go
|
@ -28,9 +28,9 @@ func calcValidatorsKey(height int64) []byte {
|
|||
return []byte(cmn.Fmt("validatorsKey:%v", height))
|
||||
}
|
||||
|
||||
/*func calcConsensusParamsKey(height int64) []byte {
|
||||
func calcConsensusParamsKey(height int64) []byte {
|
||||
return []byte(cmn.Fmt("consensusParamsKey:%v", height))
|
||||
}*/
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
@ -155,6 +155,7 @@ func (s *State) Save() {
|
|||
defer s.mtx.Unlock()
|
||||
|
||||
s.saveValidatorsInfo()
|
||||
s.saveConsensusParamsInfo()
|
||||
s.db.SetSync(stateKey, s.Bytes())
|
||||
}
|
||||
|
||||
|
@ -188,13 +189,13 @@ func (s *State) LoadABCIResponses() *ABCIResponses {
|
|||
|
||||
// LoadValidators loads the ValidatorSet for a given height.
|
||||
func (s *State) LoadValidators(height int64) (*types.ValidatorSet, error) {
|
||||
valInfo := s.loadValidators(height)
|
||||
valInfo := s.loadValidatorsInfo(height)
|
||||
if valInfo == nil {
|
||||
return nil, ErrNoValSetForHeight{height}
|
||||
}
|
||||
|
||||
if valInfo.ValidatorSet == nil {
|
||||
valInfo = s.loadValidators(valInfo.LastHeightChanged)
|
||||
valInfo = s.loadValidatorsInfo(valInfo.LastHeightChanged)
|
||||
if valInfo == nil {
|
||||
cmn.PanicSanity(fmt.Sprintf(`Couldn't find validators at height %d as
|
||||
last changed from height %d`, valInfo.LastHeightChanged, height))
|
||||
|
@ -204,7 +205,7 @@ func (s *State) LoadValidators(height int64) (*types.ValidatorSet, error) {
|
|||
return valInfo.ValidatorSet, nil
|
||||
}
|
||||
|
||||
func (s *State) loadValidators(height int64) *ValidatorsInfo {
|
||||
func (s *State) loadValidatorsInfo(height int64) *ValidatorsInfo {
|
||||
buf := s.db.Get(calcValidatorsKey(height))
|
||||
if len(buf) == 0 {
|
||||
return nil
|
||||
|
@ -239,6 +240,61 @@ func (s *State) saveValidatorsInfo() {
|
|||
s.db.SetSync(calcValidatorsKey(nextHeight), valInfo.Bytes())
|
||||
}
|
||||
|
||||
// LoadConsensusParams loads the ConsensusParams for a given height.
|
||||
func (s *State) LoadConsensusParams(height int64) (types.ConsensusParams, error) {
|
||||
empty := types.ConsensusParams{}
|
||||
|
||||
paramsInfo := s.loadConsensusParamsInfo(height)
|
||||
if paramsInfo == nil {
|
||||
return empty, ErrNoConsensusParamsForHeight{height}
|
||||
}
|
||||
|
||||
if paramsInfo.ConsensusParams == empty {
|
||||
paramsInfo = s.loadConsensusParamsInfo(paramsInfo.LastHeightChanged)
|
||||
if paramsInfo == nil {
|
||||
cmn.PanicSanity(fmt.Sprintf(`Couldn't find consensus params at height %d as
|
||||
last changed from height %d`, paramsInfo.LastHeightChanged, height))
|
||||
}
|
||||
}
|
||||
|
||||
return paramsInfo.ConsensusParams, nil
|
||||
}
|
||||
|
||||
func (s *State) loadConsensusParamsInfo(height int64) *ConsensusParamsInfo {
|
||||
buf := s.db.Get(calcConsensusParamsKey(height))
|
||||
if len(buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
paramsInfo := new(ConsensusParamsInfo)
|
||||
r, n, err := bytes.NewReader(buf), new(int), new(error)
|
||||
wire.ReadBinaryPtr(paramsInfo, r, 0, n, err)
|
||||
if *err != nil {
|
||||
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
||||
cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed:
|
||||
%v\n`, *err))
|
||||
}
|
||||
// TODO: ensure that buf is completely read.
|
||||
|
||||
return paramsInfo
|
||||
}
|
||||
|
||||
// saveConsensusParamsInfo persists the consensus params for the next block to disk.
|
||||
// It should be called from s.Save(), right before the state itself is persisted.
|
||||
// If the consensus params did not change after processing the latest block,
|
||||
// only the last height for which they changed is persisted.
|
||||
func (s *State) saveConsensusParamsInfo() {
|
||||
changeHeight := s.LastHeightConsensusParamsChanged
|
||||
nextHeight := s.LastBlockHeight + 1
|
||||
paramsInfo := &ConsensusParamsInfo{
|
||||
LastHeightChanged: changeHeight,
|
||||
}
|
||||
if changeHeight == nextHeight {
|
||||
paramsInfo.ConsensusParams = s.ConsensusParams
|
||||
}
|
||||
s.db.SetSync(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
|
||||
}
|
||||
|
||||
// Equals returns true if the States are identical.
|
||||
func (s *State) Equals(s2 *State) bool {
|
||||
return bytes.Equal(s.Bytes(), s2.Bytes())
|
||||
|
@ -252,7 +308,7 @@ func (s *State) Bytes() []byte {
|
|||
// SetBlockAndValidators mutates State variables
|
||||
// to update block and validators after running EndBlock.
|
||||
func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader,
|
||||
abciResponses *ABCIResponses) {
|
||||
abciResponses *ABCIResponses) error {
|
||||
|
||||
// copy the valset so we can apply changes from EndBlock
|
||||
// and update s.LastValidators and s.Validators
|
||||
|
@ -263,8 +319,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
|
|||
if len(abciResponses.EndBlock.ValidatorUpdates) > 0 {
|
||||
err := updateValidators(nextValSet, abciResponses.EndBlock.ValidatorUpdates)
|
||||
if err != nil {
|
||||
s.logger.Error("Error changing validator set", "err", err)
|
||||
// TODO: err or carry on?
|
||||
return fmt.Errorf("Error changing validator set: %v", err)
|
||||
}
|
||||
// change results from this height but only applies to the next height
|
||||
s.LastHeightValidatorsChanged = header.Height + 1
|
||||
|
@ -273,13 +328,17 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
|
|||
// Update validator accums and set state variables
|
||||
nextValSet.IncrementAccum(1)
|
||||
|
||||
// NOTE: must not mutate s.ConsensusParams
|
||||
nextParams := s.ConsensusParams.Update(abciResponses.EndBlock.ConsensusParamUpdates)
|
||||
err := nextParams.Validate()
|
||||
if err != nil {
|
||||
s.logger.Error("Error updating consensus params", "err", err)
|
||||
// TODO: err or carry on?
|
||||
nextParams = s.ConsensusParams
|
||||
// update the params with the latest abciResponses
|
||||
nextParams := s.ConsensusParams
|
||||
if abciResponses.EndBlock.ConsensusParamUpdates != nil {
|
||||
// NOTE: must not mutate s.ConsensusParams
|
||||
nextParams = s.ConsensusParams.Update(abciResponses.EndBlock.ConsensusParamUpdates)
|
||||
err := nextParams.Validate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error updating consensus params: %v", err)
|
||||
}
|
||||
// change results from this height but only applies to the next height
|
||||
s.LastHeightConsensusParamsChanged = header.Height + 1
|
||||
}
|
||||
|
||||
s.setBlockAndValidators(header.Height,
|
||||
|
@ -288,7 +347,7 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ
|
|||
header.Time,
|
||||
nextValSet,
|
||||
nextParams)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *State) setBlockAndValidators(height int64,
|
||||
|
@ -353,6 +412,19 @@ func (valInfo *ValidatorsInfo) Bytes() []byte {
|
|||
return wire.BinaryBytes(*valInfo)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// ConsensusParamsInfo represents the latest consensus params, or the last height it changed
|
||||
type ConsensusParamsInfo struct {
|
||||
ConsensusParams types.ConsensusParams
|
||||
LastHeightChanged int64
|
||||
}
|
||||
|
||||
// Bytes serializes the ConsensusParamsInfo using go-wire
|
||||
func (params ConsensusParamsInfo) Bytes() []byte {
|
||||
return wire.BinaryBytes(params)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Genesis
|
||||
|
||||
|
|
|
@ -192,6 +192,65 @@ func TestValidatorChangesSaveLoad(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestConsensusParamsChangesSaveLoad tests saving and loading consensus params with changes.
|
||||
func TestConsensusParamsChangesSaveLoad(t *testing.T) {
|
||||
tearDown, _, state := setupTestCase(t)
|
||||
defer tearDown(t)
|
||||
// nolint: vetshadow
|
||||
assert := assert.New(t)
|
||||
|
||||
// change vals at these heights
|
||||
changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
|
||||
N := len(changeHeights)
|
||||
|
||||
// each valset is just one validator.
|
||||
// create list of them
|
||||
params := make([]types.ConsensusParams, N+1)
|
||||
params[0] = state.ConsensusParams
|
||||
for i := 1; i < N+1; i++ {
|
||||
params[i] = *types.DefaultConsensusParams()
|
||||
params[i].BlockSize.MaxBytes += i
|
||||
}
|
||||
|
||||
// build the params history by running SetBlockAndValidators
|
||||
// with the right params set for each height
|
||||
highestHeight := changeHeights[N-1] + 5
|
||||
changeIndex := 0
|
||||
cp := params[changeIndex]
|
||||
for i := int64(1); i < highestHeight; i++ {
|
||||
// when we get to a change height,
|
||||
// use the next params
|
||||
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
|
||||
changeIndex++
|
||||
cp = params[changeIndex]
|
||||
}
|
||||
header, parts, responses := makeHeaderPartsResponsesParams(state, i, cp)
|
||||
state.SetBlockAndValidators(header, parts, responses)
|
||||
state.saveConsensusParamsInfo()
|
||||
}
|
||||
|
||||
// make all the test cases by using the same params until after the change
|
||||
testCases := make([]paramsChangeTestCase, highestHeight)
|
||||
changeIndex = 0
|
||||
cp = params[changeIndex]
|
||||
for i := int64(1); i < highestHeight+1; i++ {
|
||||
// we we get to the height after a change height
|
||||
// use the next pubkey (note our counter starts at 0 this time)
|
||||
if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
|
||||
changeIndex++
|
||||
cp = params[changeIndex]
|
||||
}
|
||||
testCases[i-1] = paramsChangeTestCase{i, cp}
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
p, err := state.LoadConsensusParams(testCase.height)
|
||||
assert.Nil(err, fmt.Sprintf("expected no err at height %d", testCase.height))
|
||||
assert.Equal(testCase.params, p, fmt.Sprintf(`unexpected consensus params at
|
||||
height %d`, testCase.height))
|
||||
}
|
||||
}
|
||||
|
||||
func makeParams(blockBytes, blockTx, blockGas, txBytes,
|
||||
txGas, partSize int) types.ConsensusParams {
|
||||
|
||||
|
@ -199,11 +258,11 @@ func makeParams(blockBytes, blockTx, blockGas, txBytes,
|
|||
BlockSize: types.BlockSize{
|
||||
MaxBytes: blockBytes,
|
||||
MaxTxs: blockTx,
|
||||
MaxGas: blockGas,
|
||||
MaxGas: int64(blockGas),
|
||||
},
|
||||
TxSize: types.TxSize{
|
||||
MaxBytes: txBytes,
|
||||
MaxGas: txGas,
|
||||
MaxGas: int64(txGas),
|
||||
},
|
||||
BlockGossip: types.BlockGossip{
|
||||
BlockPartSizeBytes: partSize,
|
||||
|
@ -252,7 +311,7 @@ func TestApplyUpdates(t *testing.T) {
|
|||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
res := applyUpdates(tc.init, tc.updates)
|
||||
res := tc.init.Update(tc.updates)
|
||||
assert.Equal(t, tc.expected, res, "case %d", i)
|
||||
}
|
||||
}
|
||||
|
@ -284,3 +343,19 @@ type valChangeTestCase struct {
|
|||
height int64
|
||||
vals crypto.PubKey
|
||||
}
|
||||
|
||||
func makeHeaderPartsResponsesParams(state *State, height int64,
|
||||
params types.ConsensusParams) (*types.Header, types.PartSetHeader, *ABCIResponses) {
|
||||
|
||||
block := makeBlock(state, height)
|
||||
abciResponses := &ABCIResponses{
|
||||
Height: height,
|
||||
EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(¶ms)},
|
||||
}
|
||||
return block.Header, types.PartSetHeader{}, abciResponses
|
||||
}
|
||||
|
||||
type paramsChangeTestCase struct {
|
||||
height int64
|
||||
params types.ConsensusParams
|
||||
}
|
||||
|
|
|
@ -51,3 +51,21 @@ func (tm2pb) Validators(vals *ValidatorSet) []*types.Validator {
|
|||
}
|
||||
return validators
|
||||
}
|
||||
|
||||
func (tm2pb) ConsensusParams(params *ConsensusParams) *types.ConsensusParams {
|
||||
return &types.ConsensusParams{
|
||||
BlockSize: &types.BlockSize{
|
||||
|
||||
MaxBytes: int32(params.BlockSize.MaxBytes),
|
||||
MaxTxs: int32(params.BlockSize.MaxTxs),
|
||||
MaxGas: params.BlockSize.MaxGas,
|
||||
},
|
||||
TxSize: &types.TxSize{
|
||||
MaxBytes: int32(params.TxSize.MaxBytes),
|
||||
MaxGas: params.TxSize.MaxGas,
|
||||
},
|
||||
BlockGossip: &types.BlockGossip{
|
||||
BlockPartSizeBytes: int32(params.BlockGossip.BlockPartSizeBytes),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue