mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-21 06:36:19 +00:00
proto: deduplicate consensus params (#9287)
This commit is contained in:
+4
-5
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
"github.com/tendermint/tendermint/mempool"
|
||||
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
"github.com/tendermint/tendermint/proxy"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
@@ -452,7 +451,7 @@ func extendedCommitInfo(c abci.CommitInfo, votes []*types.Vote) abci.ExtendedCom
|
||||
}
|
||||
|
||||
func validateValidatorUpdates(abciUpdates []abci.ValidatorUpdate,
|
||||
params tmproto.ValidatorParams) error {
|
||||
params types.ValidatorParams) error {
|
||||
for _, valUpdate := range abciUpdates {
|
||||
if valUpdate.GetPower() < 0 {
|
||||
return fmt.Errorf("voting power can't be negative %v", valUpdate)
|
||||
@@ -508,13 +507,13 @@ func updateState(
|
||||
lastHeightParamsChanged := state.LastHeightConsensusParamsChanged
|
||||
if abciResponses.EndBlock.ConsensusParamUpdates != nil {
|
||||
// NOTE: must not mutate s.ConsensusParams
|
||||
nextParams = types.UpdateConsensusParams(state.ConsensusParams, abciResponses.EndBlock.ConsensusParamUpdates)
|
||||
err := types.ValidateConsensusParams(nextParams)
|
||||
nextParams = state.ConsensusParams.Update(abciResponses.EndBlock.ConsensusParamUpdates)
|
||||
err := nextParams.ValidateBasic()
|
||||
if err != nil {
|
||||
return state, fmt.Errorf("error updating consensus params: %v", err)
|
||||
}
|
||||
|
||||
state.Version.Consensus.App = nextParams.Version.AppVersion
|
||||
state.Version.Consensus.App = nextParams.Version.App
|
||||
|
||||
// Change results from this height but only applies to the next height.
|
||||
lastHeightParamsChanged = header.Height + 1
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
mpmocks "github.com/tendermint/tendermint/mempool/mocks"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
||||
"github.com/tendermint/tendermint/proxy"
|
||||
pmocks "github.com/tendermint/tendermint/proxy/mocks"
|
||||
@@ -332,13 +331,13 @@ func TestValidateValidatorUpdates(t *testing.T) {
|
||||
pk2, err := cryptoenc.PubKeyToProto(pubkey2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defaultValidatorParams := tmproto.ValidatorParams{PubKeyTypes: []string{types.ABCIPubKeyTypeEd25519}}
|
||||
defaultValidatorParams := types.ValidatorParams{PubKeyTypes: []string{types.ABCIPubKeyTypeEd25519}}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
||||
abciUpdates []abci.ValidatorUpdate
|
||||
validatorParams tmproto.ValidatorParams
|
||||
validatorParams types.ValidatorParams
|
||||
|
||||
shouldErr bool
|
||||
}{
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
@@ -36,7 +35,7 @@ func UpdateState(
|
||||
|
||||
// ValidateValidatorUpdates is an alias for validateValidatorUpdates exported
|
||||
// from execution.go, exclusively and explicitly for testing.
|
||||
func ValidateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, params tmproto.ValidatorParams) error {
|
||||
func ValidateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, params types.ValidatorParams) error {
|
||||
return validateValidatorUpdates(abciUpdates, params)
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import (
|
||||
|
||||
type paramsChangeTestCase struct {
|
||||
height int64
|
||||
params tmproto.ConsensusParams
|
||||
params types.ConsensusParams
|
||||
}
|
||||
|
||||
func newTestApp() proxy.AppConns {
|
||||
@@ -197,7 +197,7 @@ func makeHeaderPartsResponsesParams(
|
||||
block := sf.MakeBlock(state, state.LastBlockHeight+1, new(types.Commit))
|
||||
abciResponses := &tmstate.ABCIResponses{
|
||||
BeginBlock: &abci.ResponseBeginBlock{},
|
||||
EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(¶ms)},
|
||||
EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: ¶ms},
|
||||
}
|
||||
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, abciResponses
|
||||
}
|
||||
@@ -244,9 +244,9 @@ func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlo
|
||||
func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
|
||||
return abci.ResponseEndBlock{
|
||||
ValidatorUpdates: app.ValidatorUpdates,
|
||||
ConsensusParamUpdates: &abci.ConsensusParams{
|
||||
ConsensusParamUpdates: &tmproto.ConsensusParams{
|
||||
Version: &tmproto.VersionParams{
|
||||
AppVersion: 1}}}
|
||||
App: 1}}}
|
||||
}
|
||||
|
||||
func (app *testApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
|
||||
|
||||
+8
-10
@@ -8,9 +8,7 @@ import (
|
||||
|
||||
tendermintstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
|
||||
tenderminttypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
types "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
types "github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// Store is an autogenerated mock type for the Store type
|
||||
@@ -112,18 +110,18 @@ func (_m *Store) LoadConsensusParams(_a0 int64) (types.ConsensusParams, error) {
|
||||
}
|
||||
|
||||
// LoadFromDBOrGenesisDoc provides a mock function with given fields: _a0
|
||||
func (_m *Store) LoadFromDBOrGenesisDoc(_a0 *tenderminttypes.GenesisDoc) (state.State, error) {
|
||||
func (_m *Store) LoadFromDBOrGenesisDoc(_a0 *types.GenesisDoc) (state.State, error) {
|
||||
ret := _m.Called(_a0)
|
||||
|
||||
var r0 state.State
|
||||
if rf, ok := ret.Get(0).(func(*tenderminttypes.GenesisDoc) state.State); ok {
|
||||
if rf, ok := ret.Get(0).(func(*types.GenesisDoc) state.State); ok {
|
||||
r0 = rf(_a0)
|
||||
} else {
|
||||
r0 = ret.Get(0).(state.State)
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*tenderminttypes.GenesisDoc) error); ok {
|
||||
if rf, ok := ret.Get(1).(func(*types.GenesisDoc) error); ok {
|
||||
r1 = rf(_a0)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
@@ -154,15 +152,15 @@ func (_m *Store) LoadFromDBOrGenesisFile(_a0 string) (state.State, error) {
|
||||
}
|
||||
|
||||
// LoadValidators provides a mock function with given fields: _a0
|
||||
func (_m *Store) LoadValidators(_a0 int64) (*tenderminttypes.ValidatorSet, error) {
|
||||
func (_m *Store) LoadValidators(_a0 int64) (*types.ValidatorSet, error) {
|
||||
ret := _m.Called(_a0)
|
||||
|
||||
var r0 *tenderminttypes.ValidatorSet
|
||||
if rf, ok := ret.Get(0).(func(int64) *tenderminttypes.ValidatorSet); ok {
|
||||
var r0 *types.ValidatorSet
|
||||
if rf, ok := ret.Get(0).(func(int64) *types.ValidatorSet); ok {
|
||||
r0 = rf(_a0)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*tenderminttypes.ValidatorSet)
|
||||
r0 = ret.Get(0).(*types.ValidatorSet)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ func Rollback(bs BlockStore, ss Store) (int64, []byte, error) {
|
||||
Version: tmstate.Version{
|
||||
Consensus: tmversion.Consensus{
|
||||
Block: version.BlockProtocol,
|
||||
App: previousParams.Version.AppVersion,
|
||||
App: previousParams.Version.App,
|
||||
},
|
||||
Software: version.TMCoreSemVer,
|
||||
},
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestRollback(t *testing.T) {
|
||||
|
||||
// perform the rollback over a version bump
|
||||
newParams := types.DefaultConsensusParams()
|
||||
newParams.Version.AppVersion = 11
|
||||
newParams.Version.App = 11
|
||||
newParams.Block.MaxBytes = 1000
|
||||
nextState := initialState.Copy()
|
||||
nextState.LastBlockHeight = nextHeight
|
||||
@@ -119,7 +119,7 @@ func setupStateStore(t *testing.T, height int64) state.Store {
|
||||
valSet, _ := types.RandValidatorSet(5, 10)
|
||||
|
||||
params := types.DefaultConsensusParams()
|
||||
params.Version.AppVersion = 10
|
||||
params.Version.App = 10
|
||||
|
||||
initialState := state.State{
|
||||
Version: tmstate.Version{
|
||||
|
||||
+4
-5
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
tmtime "github.com/tendermint/tendermint/types/time"
|
||||
@@ -70,7 +69,7 @@ type State struct {
|
||||
|
||||
// Consensus parameters used for validating blocks.
|
||||
// Changes returned by EndBlock and updated after Commit.
|
||||
ConsensusParams tmproto.ConsensusParams
|
||||
ConsensusParams types.ConsensusParams
|
||||
LastHeightConsensusParamsChanged int64
|
||||
|
||||
// Merkle root of the results from executing prev block
|
||||
@@ -167,7 +166,7 @@ func (state *State) ToProto() (*tmstate.State, error) {
|
||||
}
|
||||
|
||||
sm.LastHeightValidatorsChanged = state.LastHeightValidatorsChanged
|
||||
sm.ConsensusParams = state.ConsensusParams
|
||||
sm.ConsensusParams = state.ConsensusParams.ToProto()
|
||||
sm.LastHeightConsensusParamsChanged = state.LastHeightConsensusParamsChanged
|
||||
sm.LastResultsHash = state.LastResultsHash
|
||||
sm.AppHash = state.AppHash
|
||||
@@ -218,7 +217,7 @@ func FromProto(pb *tmstate.State) (*State, error) { //nolint:golint
|
||||
}
|
||||
|
||||
state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
|
||||
state.ConsensusParams = pb.ConsensusParams
|
||||
state.ConsensusParams = types.ConsensusParamsFromProto(pb.ConsensusParams)
|
||||
state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
|
||||
state.LastResultsHash = pb.LastResultsHash
|
||||
state.AppHash = pb.AppHash
|
||||
@@ -256,7 +255,7 @@ func (state State) MakeBlock(
|
||||
state.Version.Consensus, state.ChainID,
|
||||
timestamp, state.LastBlockID,
|
||||
state.Validators.Hash(), state.NextValidators.Hash(),
|
||||
types.HashConsensusParams(state.ConsensusParams), state.AppHash, state.LastResultsHash,
|
||||
state.ConsensusParams.Hash(), state.AppHash, state.LastResultsHash,
|
||||
proposerAddress,
|
||||
)
|
||||
|
||||
|
||||
+3
-4
@@ -19,7 +19,6 @@ import (
|
||||
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
|
||||
tmrand "github.com/tendermint/tendermint/libs/rand"
|
||||
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
sf "github.com/tendermint/tendermint/state/test/factory"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
@@ -1030,12 +1029,12 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
|
||||
|
||||
// Each valset is just one validator.
|
||||
// create list of them.
|
||||
params := make([]tmproto.ConsensusParams, N+1)
|
||||
params := make([]types.ConsensusParams, N+1)
|
||||
params[0] = state.ConsensusParams
|
||||
for i := 1; i < N+1; i++ {
|
||||
params[i] = *types.DefaultConsensusParams()
|
||||
params[i].Block.MaxBytes += int64(i)
|
||||
params[i].Block.TimeIotaMs = 10
|
||||
|
||||
}
|
||||
|
||||
// Build the params history by running updateState
|
||||
@@ -1051,7 +1050,7 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
|
||||
changeIndex++
|
||||
cp = params[changeIndex]
|
||||
}
|
||||
header, blockID, responses := makeHeaderPartsResponsesParams(t, state, cp)
|
||||
header, blockID, responses := makeHeaderPartsResponsesParams(t, state, cp.ToProto())
|
||||
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
|
||||
require.NoError(t, err)
|
||||
state, err = sm.UpdateState(state, blockID, &header, responses, validatorUpdates)
|
||||
|
||||
+12
-9
@@ -59,7 +59,7 @@ type Store interface {
|
||||
// LoadABCIResponses loads the abciResponse for a given height
|
||||
LoadABCIResponses(int64) (*tmstate.ABCIResponses, error)
|
||||
// LoadConsensusParams loads the consensus params for a given height
|
||||
LoadConsensusParams(int64) (tmproto.ConsensusParams, error)
|
||||
LoadConsensusParams(int64) (types.ConsensusParams, error)
|
||||
// Save overwrites the previous state with the updated one
|
||||
Save(State) error
|
||||
// SaveABCIResponses saves ABCIResponses for a given height
|
||||
@@ -298,10 +298,11 @@ func (store dbStore) PruneStates(from int64, to int64) error {
|
||||
}
|
||||
|
||||
if p.ConsensusParams.Equal(&tmproto.ConsensusParams{}) {
|
||||
p.ConsensusParams, err = store.LoadConsensusParams(h)
|
||||
params, err := store.LoadConsensusParams(h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.ConsensusParams = params.ToProto()
|
||||
|
||||
p.LastHeightChanged = h
|
||||
bz, err := p.Marshal()
|
||||
@@ -526,15 +527,17 @@ func (store dbStore) saveValidatorsInfo(height, lastHeightChanged int64, valSet
|
||||
// ConsensusParamsInfo represents the latest consensus params, or the last height it changed
|
||||
|
||||
// LoadConsensusParams loads the ConsensusParams for a given height.
|
||||
func (store dbStore) LoadConsensusParams(height int64) (tmproto.ConsensusParams, error) {
|
||||
empty := tmproto.ConsensusParams{}
|
||||
|
||||
func (store dbStore) LoadConsensusParams(height int64) (types.ConsensusParams, error) {
|
||||
var (
|
||||
empty = types.ConsensusParams{}
|
||||
emptypb = tmproto.ConsensusParams{}
|
||||
)
|
||||
paramsInfo, err := store.loadConsensusParamsInfo(height)
|
||||
if err != nil {
|
||||
return empty, fmt.Errorf("could not find consensus params for height #%d: %w", height, err)
|
||||
}
|
||||
|
||||
if paramsInfo.ConsensusParams.Equal(&empty) {
|
||||
if paramsInfo.ConsensusParams.Equal(&emptypb) {
|
||||
paramsInfo2, err := store.loadConsensusParamsInfo(paramsInfo.LastHeightChanged)
|
||||
if err != nil {
|
||||
return empty, fmt.Errorf(
|
||||
@@ -548,7 +551,7 @@ func (store dbStore) LoadConsensusParams(height int64) (tmproto.ConsensusParams,
|
||||
paramsInfo = paramsInfo2
|
||||
}
|
||||
|
||||
return paramsInfo.ConsensusParams, nil
|
||||
return types.ConsensusParamsFromProto(paramsInfo.ConsensusParams), nil
|
||||
}
|
||||
|
||||
func (store dbStore) loadConsensusParamsInfo(height int64) (*tmstate.ConsensusParamsInfo, error) {
|
||||
@@ -575,13 +578,13 @@ func (store dbStore) loadConsensusParamsInfo(height int64) (*tmstate.ConsensusPa
|
||||
// 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 (store dbStore) saveConsensusParamsInfo(nextHeight, changeHeight int64, params tmproto.ConsensusParams) error {
|
||||
func (store dbStore) saveConsensusParamsInfo(nextHeight, changeHeight int64, params types.ConsensusParams) error {
|
||||
paramsInfo := &tmstate.ConsensusParamsInfo{
|
||||
LastHeightChanged: changeHeight,
|
||||
}
|
||||
|
||||
if changeHeight == nextHeight {
|
||||
paramsInfo.ConsensusParams = params
|
||||
paramsInfo.ConsensusParams = params.ToProto()
|
||||
}
|
||||
bz, err := paramsInfo.Marshal()
|
||||
if err != nil {
|
||||
|
||||
+4
-4
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
tmrand "github.com/tendermint/tendermint/libs/rand"
|
||||
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
@@ -133,8 +132,8 @@ func TestPruneStates(t *testing.T) {
|
||||
LastBlockHeight: h - 1,
|
||||
Validators: validatorSet,
|
||||
NextValidators: validatorSet,
|
||||
ConsensusParams: tmproto.ConsensusParams{
|
||||
Block: tmproto.BlockParams{MaxBytes: 10e6},
|
||||
ConsensusParams: types.ConsensusParams{
|
||||
Block: types.BlockParams{MaxBytes: 10e6},
|
||||
},
|
||||
LastHeightValidatorsChanged: valsChanged,
|
||||
LastHeightConsensusParamsChanged: paramsChanged,
|
||||
@@ -182,9 +181,10 @@ func TestPruneStates(t *testing.T) {
|
||||
params, err := stateStore.LoadConsensusParams(h)
|
||||
if expectParams[h] {
|
||||
require.NoError(t, err, "params height %v", h)
|
||||
require.False(t, params.Equal(&tmproto.ConsensusParams{}))
|
||||
require.NotEmpty(t, params)
|
||||
} else {
|
||||
require.Error(t, err, "params height %v", h)
|
||||
require.Empty(t, params)
|
||||
}
|
||||
|
||||
abci, err := stateStore.LoadABCIResponses(h)
|
||||
|
||||
+2
-3
@@ -57,10 +57,9 @@ func validateBlock(state State, block *types.Block) error {
|
||||
block.AppHash,
|
||||
)
|
||||
}
|
||||
hashCP := types.HashConsensusParams(state.ConsensusParams)
|
||||
if !bytes.Equal(block.ConsensusHash, hashCP) {
|
||||
if !bytes.Equal(block.ConsensusHash, state.ConsensusParams.Hash()) {
|
||||
return fmt.Errorf("wrong Block.Header.ConsensusHash. Expected %X, got %v",
|
||||
hashCP,
|
||||
state.ConsensusParams.Hash(),
|
||||
block.ConsensusHash,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user