mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-21 07:24:36 +00:00
state: proto migration (#4972)
## Description the second part of state proto migration Closes: #XXX
This commit is contained in:
+111
-13
@@ -2,10 +2,14 @@ package state
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
tmstate "github.com/tendermint/tendermint/proto/state"
|
||||
tmproto "github.com/tendermint/tendermint/proto/types"
|
||||
tmversion "github.com/tendermint/tendermint/proto/version"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
@@ -20,20 +24,11 @@ var (
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Version is for versioning the State.
|
||||
// It holds the Block and App version needed for making blocks,
|
||||
// and the software version to support upgrades to the format of
|
||||
// the State as stored on disk.
|
||||
type Version struct {
|
||||
Consensus tmversion.Consensus
|
||||
Software string
|
||||
}
|
||||
|
||||
// InitStateVersion sets the Consensus.Block and Software versions,
|
||||
// but leaves the Consensus.App version blank.
|
||||
// The Consensus.App version will be set during the Handshake, once
|
||||
// we hear from the app what protocol version it is running.
|
||||
var InitStateVersion = Version{
|
||||
var InitStateVersion = tmstate.Version{
|
||||
Consensus: tmversion.Consensus{
|
||||
Block: version.BlockProtocol,
|
||||
App: 0,
|
||||
@@ -51,7 +46,7 @@ var InitStateVersion = Version{
|
||||
// Instead, use state.Copy() or state.NextState(...).
|
||||
// NOTE: not goroutine-safe.
|
||||
type State struct {
|
||||
Version Version
|
||||
Version tmstate.Version
|
||||
|
||||
// immutable
|
||||
ChainID string
|
||||
@@ -86,6 +81,7 @@ type State struct {
|
||||
|
||||
// Copy makes a copy of the State for mutating.
|
||||
func (state State) Copy() State {
|
||||
|
||||
return State{
|
||||
Version: state.Version,
|
||||
ChainID: state.ChainID,
|
||||
@@ -114,9 +110,18 @@ func (state State) Equals(state2 State) bool {
|
||||
return bytes.Equal(sbz, s2bz)
|
||||
}
|
||||
|
||||
// Bytes serializes the State using go-amino.
|
||||
// Bytes serializes the State using protobuf.
|
||||
// It panics if either casting to protobuf or serialization fails.
|
||||
func (state State) Bytes() []byte {
|
||||
return cdc.MustMarshalBinaryBare(state)
|
||||
sm, err := state.ToProto()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bz, err := proto.Marshal(sm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the State is equal to the empty State.
|
||||
@@ -124,6 +129,99 @@ func (state State) IsEmpty() bool {
|
||||
return state.Validators == nil // XXX can't compare to Empty
|
||||
}
|
||||
|
||||
//ToProto takes the local state type and returns the equivalent proto type
|
||||
func (state *State) ToProto() (*tmstate.State, error) {
|
||||
if state == nil {
|
||||
return nil, errors.New("state is nil")
|
||||
}
|
||||
|
||||
sm := new(tmstate.State)
|
||||
|
||||
sm.Version = state.Version
|
||||
sm.ChainID = state.ChainID
|
||||
sm.LastBlockHeight = state.LastBlockHeight
|
||||
|
||||
sm.LastBlockID = state.LastBlockID.ToProto()
|
||||
sm.LastBlockTime = state.LastBlockTime
|
||||
vals, err := state.Validators.ToProto()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sm.Validators = vals
|
||||
|
||||
nVals, err := state.NextValidators.ToProto()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sm.NextValidators = nVals
|
||||
|
||||
if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
|
||||
lVals, err := state.LastValidators.ToProto()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sm.LastValidators = lVals
|
||||
}
|
||||
|
||||
sm.LastHeightValidatorsChanged = state.LastHeightValidatorsChanged
|
||||
sm.ConsensusParams = state.ConsensusParams
|
||||
sm.LastHeightConsensusParamsChanged = state.LastHeightConsensusParamsChanged
|
||||
sm.LastResultsHash = state.LastResultsHash
|
||||
sm.AppHash = state.AppHash
|
||||
|
||||
return sm, nil
|
||||
}
|
||||
|
||||
// StateFromProto takes a state proto message & returns the local state type
|
||||
func StateFromProto(pb *tmstate.State) (*State, error) { //nolint:golint
|
||||
if pb == nil {
|
||||
return nil, errors.New("nil State")
|
||||
}
|
||||
|
||||
state := new(State)
|
||||
|
||||
state.Version = pb.Version
|
||||
state.ChainID = pb.ChainID
|
||||
|
||||
bi, err := types.BlockIDFromProto(&pb.LastBlockID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.LastBlockID = *bi
|
||||
state.LastBlockHeight = pb.LastBlockHeight
|
||||
state.LastBlockTime = pb.LastBlockTime
|
||||
|
||||
vals, err := types.ValidatorSetFromProto(pb.Validators)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.Validators = vals
|
||||
|
||||
nVals, err := types.ValidatorSetFromProto(pb.NextValidators)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.NextValidators = nVals
|
||||
|
||||
if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
|
||||
lVals, err := types.ValidatorSetFromProto(pb.LastValidators)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.LastValidators = lVals
|
||||
} else {
|
||||
state.LastValidators = types.NewValidatorSet(nil)
|
||||
}
|
||||
|
||||
state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
|
||||
state.ConsensusParams = pb.ConsensusParams
|
||||
state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
|
||||
state.LastResultsHash = pb.LastResultsHash
|
||||
state.AppHash = pb.AppHash
|
||||
|
||||
return state, nil
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Create a block from the latest state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user