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.

186 lines
5.6 KiB

10 years ago
7 years ago
6 years ago
8 years ago
6 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. NextValidators *types.ValidatorSet
  35. Validators *types.ValidatorSet
  36. LastValidators *types.ValidatorSet
  37. LastHeightValidatorsChanged int64
  38. // Consensus parameters used for validating blocks.
  39. // Changes returned by EndBlock and updated after Commit.
  40. ConsensusParams types.ConsensusParams
  41. LastHeightConsensusParamsChanged int64
  42. // Merkle root of the results from executing prev block
  43. LastResultsHash []byte
  44. // the latest AppHash we've received from calling abci.Commit()
  45. AppHash []byte
  46. }
  47. // Copy makes a copy of the State for mutating.
  48. func (state State) Copy() State {
  49. return State{
  50. ChainID: state.ChainID,
  51. LastBlockHeight: state.LastBlockHeight,
  52. LastBlockTotalTx: state.LastBlockTotalTx,
  53. LastBlockID: state.LastBlockID,
  54. LastBlockTime: state.LastBlockTime,
  55. NextValidators: state.NextValidators.Copy(),
  56. Validators: state.Validators.Copy(),
  57. LastValidators: state.LastValidators.Copy(),
  58. LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
  59. ConsensusParams: state.ConsensusParams,
  60. LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
  61. AppHash: state.AppHash,
  62. LastResultsHash: state.LastResultsHash,
  63. }
  64. }
  65. // Equals returns true if the States are identical.
  66. func (state State) Equals(state2 State) bool {
  67. sbz, s2bz := state.Bytes(), state2.Bytes()
  68. return bytes.Equal(sbz, s2bz)
  69. }
  70. // Bytes serializes the State using go-amino.
  71. func (state State) Bytes() []byte {
  72. return cdc.MustMarshalBinaryBare(state)
  73. }
  74. // IsEmpty returns true if the State is equal to the empty State.
  75. func (state State) IsEmpty() bool {
  76. return state.Validators == nil // XXX can't compare to Empty
  77. }
  78. //------------------------------------------------------------------------
  79. // Create a block from the latest state
  80. // MakeBlock builds a block with the given txs and commit from the current state.
  81. func (state State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
  82. // Build base block.
  83. block := types.MakeBlock(height, txs, commit)
  84. // Fill header with state data.
  85. block.ChainID = state.ChainID
  86. block.TotalTxs = state.LastBlockTotalTx + block.NumTxs
  87. block.LastBlockID = state.LastBlockID
  88. block.ValidatorsHash = state.Validators.Hash()
  89. block.NextValidatorsHash = state.NextValidators.Hash()
  90. block.AppHash = state.AppHash
  91. block.ConsensusHash = state.ConsensusParams.Hash()
  92. block.LastResultsHash = state.LastResultsHash
  93. return block, block.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  94. }
  95. //------------------------------------------------------------------------
  96. // Genesis
  97. // MakeGenesisStateFromFile reads and unmarshals state from the given
  98. // file.
  99. //
  100. // Used during replay and in tests.
  101. func MakeGenesisStateFromFile(genDocFile string) (State, error) {
  102. genDoc, err := MakeGenesisDocFromFile(genDocFile)
  103. if err != nil {
  104. return State{}, err
  105. }
  106. return MakeGenesisState(genDoc)
  107. }
  108. // MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
  109. func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
  110. genDocJSON, err := ioutil.ReadFile(genDocFile)
  111. if err != nil {
  112. return nil, fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
  113. }
  114. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  115. if err != nil {
  116. return nil, fmt.Errorf("Error reading GenesisDoc: %v", err)
  117. }
  118. return genDoc, nil
  119. }
  120. // MakeGenesisState creates state from types.GenesisDoc.
  121. func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
  122. err := genDoc.ValidateAndComplete()
  123. if err != nil {
  124. return State{}, fmt.Errorf("Error in genesis file: %v", err)
  125. }
  126. // Make validators slice
  127. validators := make([]*types.Validator, len(genDoc.Validators))
  128. for i, val := range genDoc.Validators {
  129. pubKey := val.PubKey
  130. address := pubKey.Address()
  131. // Make validator
  132. validators[i] = &types.Validator{
  133. Address: address,
  134. PubKey: pubKey,
  135. VotingPower: val.Power,
  136. }
  137. }
  138. return State{
  139. ChainID: genDoc.ChainID,
  140. LastBlockHeight: 0,
  141. LastBlockID: types.BlockID{},
  142. LastBlockTime: genDoc.GenesisTime,
  143. NextValidators: types.NewValidatorSet(validators).CopyIncrementAccum(1),
  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. }