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.

187 lines
5.6 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 with the given txs and commit from the current state.
  83. func (state State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
  84. // build base block
  85. block := types.MakeBlock(height, txs, commit)
  86. // fill header with state data
  87. block.ChainID = state.ChainID
  88. block.TotalTxs = state.LastBlockTotalTx + block.NumTxs
  89. block.LastBlockID = state.LastBlockID
  90. block.ValidatorsHash = state.Validators.Hash()
  91. block.AppHash = state.AppHash
  92. block.ConsensusHash = state.ConsensusParams.Hash()
  93. block.LastResultsHash = state.LastResultsHash
  94. return block, block.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  95. }
  96. //------------------------------------------------------------------------
  97. // Genesis
  98. // MakeGenesisStateFromFile reads and unmarshals state from the given
  99. // file.
  100. //
  101. // Used during replay and in tests.
  102. func MakeGenesisStateFromFile(genDocFile string) (State, error) {
  103. genDoc, err := MakeGenesisDocFromFile(genDocFile)
  104. if err != nil {
  105. return State{}, err
  106. }
  107. return MakeGenesisState(genDoc)
  108. }
  109. // MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
  110. func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
  111. genDocJSON, err := ioutil.ReadFile(genDocFile)
  112. if err != nil {
  113. return nil, fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
  114. }
  115. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  116. if err != nil {
  117. return nil, fmt.Errorf("Error reading GenesisDoc: %v", err)
  118. }
  119. return genDoc, nil
  120. }
  121. // MakeGenesisState creates state from types.GenesisDoc.
  122. func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
  123. err := genDoc.ValidateAndComplete()
  124. if err != nil {
  125. return State{}, fmt.Errorf("Error in genesis file: %v", err)
  126. }
  127. // Make validators slice
  128. validators := make([]*types.Validator, len(genDoc.Validators))
  129. for i, val := range genDoc.Validators {
  130. pubKey := val.PubKey
  131. address := pubKey.Address()
  132. // Make validator
  133. validators[i] = &types.Validator{
  134. Address: address,
  135. PubKey: pubKey,
  136. VotingPower: val.Power,
  137. }
  138. }
  139. return State{
  140. ChainID: genDoc.ChainID,
  141. LastBlockHeight: 0,
  142. LastBlockID: types.BlockID{},
  143. LastBlockTime: genDoc.GenesisTime,
  144. Validators: types.NewValidatorSet(validators),
  145. LastValidators: types.NewValidatorSet(nil),
  146. LastHeightValidatorsChanged: 1,
  147. ConsensusParams: *genDoc.ConsensusParams,
  148. LastHeightConsensusParamsChanged: 1,
  149. AppHash: genDoc.AppHash,
  150. }, nil
  151. }