You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.7 KiB

10 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "time"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. // database keys
  10. var (
  11. stateKey = []byte("stateKey")
  12. )
  13. //-----------------------------------------------------------------------------
  14. // State is a short description of the latest committed block of the Tendermint consensus.
  15. // It keeps all information necessary to validate new blocks,
  16. // including the last validator set and the consensus params.
  17. // All fields are exposed so the struct can be easily serialized,
  18. // but none of them should be mutated directly.
  19. // Instead, use state.Copy() or state.NextState(...).
  20. // NOTE: not goroutine-safe.
  21. type State struct {
  22. // Immutable
  23. ChainID string
  24. // LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
  25. LastBlockHeight int64
  26. LastBlockTotalTx int64
  27. LastBlockID types.BlockID
  28. LastBlockTime time.Time
  29. // LastValidators is used to validate block.LastCommit.
  30. // Validators are persisted to the database separately every time they change,
  31. // so we can query for historical validator sets.
  32. // Note that if s.LastBlockHeight causes a valset change,
  33. // we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
  34. Validators *types.ValidatorSet
  35. LastValidators *types.ValidatorSet
  36. LastHeightValidatorsChanged int64
  37. // Consensus parameters used for validating blocks.
  38. // Changes returned by EndBlock and updated after Commit.
  39. ConsensusParams types.ConsensusParams
  40. LastHeightConsensusParamsChanged int64
  41. // Merkle root of the results from executing prev block
  42. LastResultsHash []byte
  43. // The latest AppHash we've received from calling abci.Commit()
  44. AppHash []byte
  45. }
  46. // Copy makes a copy of the State for mutating.
  47. func (state State) Copy() State {
  48. return State{
  49. ChainID: state.ChainID,
  50. LastBlockHeight: state.LastBlockHeight,
  51. LastBlockTotalTx: state.LastBlockTotalTx,
  52. LastBlockID: state.LastBlockID,
  53. LastBlockTime: state.LastBlockTime,
  54. Validators: state.Validators.Copy(),
  55. LastValidators: state.LastValidators.Copy(),
  56. LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
  57. ConsensusParams: state.ConsensusParams,
  58. LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
  59. AppHash: state.AppHash,
  60. LastResultsHash: state.LastResultsHash,
  61. }
  62. }
  63. // Equals returns true if the States are identical.
  64. func (state State) Equals(state2 State) bool {
  65. sbz, s2bz := state.Bytes(), state2.Bytes()
  66. return bytes.Equal(sbz, s2bz)
  67. }
  68. // Bytes serializes the State using go-amino.
  69. func (state State) Bytes() []byte {
  70. return cdc.MustMarshalBinaryBare(state)
  71. }
  72. // IsEmpty returns true if the State is equal to the empty State.
  73. func (state State) IsEmpty() bool {
  74. return state.Validators == nil // XXX can't compare to Empty
  75. }
  76. // GetValidators returns the last and current validator sets.
  77. func (state State) GetValidators() (last *types.ValidatorSet, current *types.ValidatorSet) {
  78. return state.LastValidators, state.Validators
  79. }
  80. //------------------------------------------------------------------------
  81. // Create a block from the latest state
  82. // MakeBlock builds a block from the current state with the given txs, commit, and evidence.
  83. func (state State) MakeBlock(
  84. height int64,
  85. txs []types.Tx,
  86. commit *types.Commit,
  87. evidence []types.Evidence,
  88. ) (*types.Block, *types.PartSet) {
  89. // Build base block with block data.
  90. block := types.MakeBlock(height, txs, commit, evidence)
  91. // Fill rest of header with state data.
  92. block.ChainID = state.ChainID
  93. block.LastBlockID = state.LastBlockID
  94. block.TotalTxs = state.LastBlockTotalTx + block.NumTxs
  95. block.ValidatorsHash = state.Validators.Hash()
  96. block.ConsensusHash = state.ConsensusParams.Hash()
  97. block.AppHash = state.AppHash
  98. block.LastResultsHash = state.LastResultsHash
  99. return block, block.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  100. }
  101. //------------------------------------------------------------------------
  102. // Genesis
  103. // MakeGenesisStateFromFile reads and unmarshals state from the given
  104. // file.
  105. //
  106. // Used during replay and in tests.
  107. func MakeGenesisStateFromFile(genDocFile string) (State, error) {
  108. genDoc, err := MakeGenesisDocFromFile(genDocFile)
  109. if err != nil {
  110. return State{}, err
  111. }
  112. return MakeGenesisState(genDoc)
  113. }
  114. // MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
  115. func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
  116. genDocJSON, err := ioutil.ReadFile(genDocFile)
  117. if err != nil {
  118. return nil, fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
  119. }
  120. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  121. if err != nil {
  122. return nil, fmt.Errorf("Error reading GenesisDoc: %v", err)
  123. }
  124. return genDoc, nil
  125. }
  126. // MakeGenesisState creates state from types.GenesisDoc.
  127. func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
  128. err := genDoc.ValidateAndComplete()
  129. if err != nil {
  130. return State{}, fmt.Errorf("Error in genesis file: %v", err)
  131. }
  132. // Make validators slice
  133. validators := make([]*types.Validator, len(genDoc.Validators))
  134. for i, val := range genDoc.Validators {
  135. pubKey := val.PubKey
  136. address := pubKey.Address()
  137. // Make validator
  138. validators[i] = &types.Validator{
  139. Address: address,
  140. PubKey: pubKey,
  141. VotingPower: val.Power,
  142. }
  143. }
  144. return State{
  145. ChainID: genDoc.ChainID,
  146. LastBlockHeight: 0,
  147. LastBlockID: types.BlockID{},
  148. LastBlockTime: genDoc.GenesisTime,
  149. Validators: types.NewValidatorSet(validators),
  150. LastValidators: types.NewValidatorSet(nil),
  151. LastHeightValidatorsChanged: 1,
  152. ConsensusParams: *genDoc.ConsensusParams,
  153. LastHeightConsensusParamsChanged: 1,
  154. AppHash: genDoc.AppHash,
  155. }, nil
  156. }