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.

365 lines
11 KiB

8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "sync"
  7. "time"
  8. abci "github.com/tendermint/abci/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. "github.com/tendermint/tmlibs/log"
  12. wire "github.com/tendermint/go-wire"
  13. "github.com/tendermint/tendermint/state/txindex"
  14. "github.com/tendermint/tendermint/state/txindex/null"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. var (
  18. stateKey = []byte("stateKey")
  19. abciResponsesKey = []byte("abciResponsesKey")
  20. )
  21. func calcValidatorsKey(height int) []byte {
  22. return []byte(cmn.Fmt("validatorsKey:%v", height))
  23. }
  24. //-----------------------------------------------------------------------------
  25. // State represents the latest committed state of the Tendermint consensus,
  26. // including the last committed block and validator set.
  27. // Newly committed blocks are validated and executed against the State.
  28. // NOTE: not goroutine-safe.
  29. type State struct {
  30. // mtx for writing to db
  31. mtx sync.Mutex
  32. db dbm.DB
  33. // should not change
  34. GenesisDoc *types.GenesisDoc
  35. ChainID string
  36. // These fields are updated by SetBlockAndValidators.
  37. // LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
  38. // LastValidators is used to validate block.LastCommit.
  39. LastBlockHeight int
  40. LastBlockID types.BlockID
  41. LastBlockTime time.Time
  42. Validators *types.ValidatorSet
  43. LastValidators *types.ValidatorSet
  44. // AppHash is updated after Commit
  45. AppHash []byte
  46. TxIndexer txindex.TxIndexer `json:"-"` // Transaction indexer
  47. // When a block returns a validator set change via EndBlock,
  48. // the change only applies to the next block.
  49. // So, if s.LastBlockHeight causes a valset change,
  50. // we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
  51. LastHeightValidatorsChanged int
  52. logger log.Logger
  53. }
  54. // GetState loads the most recent state from the database,
  55. // or creates a new one from the given genesisFile and persists the result
  56. // to the database.
  57. func GetState(stateDB dbm.DB, genesisFile string) *State {
  58. state := LoadState(stateDB)
  59. if state == nil {
  60. state = MakeGenesisStateFromFile(stateDB, genesisFile)
  61. state.Save()
  62. }
  63. return state
  64. }
  65. // LoadState loads the State from the database.
  66. func LoadState(db dbm.DB) *State {
  67. return loadState(db, stateKey)
  68. }
  69. func loadState(db dbm.DB, key []byte) *State {
  70. s := &State{db: db, TxIndexer: &null.TxIndex{}}
  71. buf := db.Get(key)
  72. if len(buf) == 0 {
  73. return nil
  74. } else {
  75. r, n, err := bytes.NewReader(buf), new(int), new(error)
  76. wire.ReadBinaryPtr(&s, r, 0, n, err)
  77. if *err != nil {
  78. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  79. cmn.Exit(cmn.Fmt("LoadState: Data has been corrupted or its spec has changed: %v\n", *err))
  80. }
  81. // TODO: ensure that buf is completely read.
  82. }
  83. return s
  84. }
  85. // SetLogger sets the logger on the State.
  86. func (s *State) SetLogger(l log.Logger) {
  87. s.logger = l
  88. }
  89. // Copy makes a copy of the State for mutating.
  90. func (s *State) Copy() *State {
  91. return &State{
  92. db: s.db,
  93. GenesisDoc: s.GenesisDoc,
  94. ChainID: s.ChainID,
  95. LastBlockHeight: s.LastBlockHeight,
  96. LastBlockID: s.LastBlockID,
  97. LastBlockTime: s.LastBlockTime,
  98. Validators: s.Validators.Copy(),
  99. LastValidators: s.LastValidators.Copy(),
  100. AppHash: s.AppHash,
  101. TxIndexer: s.TxIndexer, // pointer here, not value
  102. LastHeightValidatorsChanged: s.LastHeightValidatorsChanged,
  103. logger: s.logger,
  104. }
  105. }
  106. // Save persists the State to the database.
  107. func (s *State) Save() {
  108. s.mtx.Lock()
  109. defer s.mtx.Unlock()
  110. s.saveValidatorsInfo()
  111. s.db.SetSync(stateKey, s.Bytes())
  112. }
  113. // SaveABCIResponses persists the ABCIResponses to the database.
  114. // This is useful in case we crash after app.Commit and before s.Save().
  115. func (s *State) SaveABCIResponses(abciResponses *ABCIResponses) {
  116. s.db.SetSync(abciResponsesKey, abciResponses.Bytes())
  117. }
  118. // LoadABCIResponses loads the ABCIResponses from the database.
  119. func (s *State) LoadABCIResponses() *ABCIResponses {
  120. abciResponses := new(ABCIResponses)
  121. buf := s.db.Get(abciResponsesKey)
  122. if len(buf) != 0 {
  123. r, n, err := bytes.NewReader(buf), new(int), new(error)
  124. wire.ReadBinaryPtr(abciResponses, r, 0, n, err)
  125. if *err != nil {
  126. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  127. cmn.Exit(cmn.Fmt("LoadABCIResponses: Data has been corrupted or its spec has changed: %v\n", *err))
  128. }
  129. // TODO: ensure that buf is completely read.
  130. }
  131. return abciResponses
  132. }
  133. // LoadValidators loads the ValidatorSet for a given height.
  134. func (s *State) LoadValidators(height int) (*types.ValidatorSet, error) {
  135. v := s.loadValidators(height)
  136. if v == nil {
  137. return nil, ErrNoValSetForHeight{height}
  138. }
  139. if v.ValidatorSet == nil {
  140. v = s.loadValidators(v.LastHeightChanged)
  141. if v == nil {
  142. cmn.PanicSanity(fmt.Sprintf(`Couldn't find validators at
  143. height %d as last changed from height %d`, v.LastHeightChanged, height))
  144. }
  145. }
  146. return v.ValidatorSet, nil
  147. }
  148. func (s *State) loadValidators(height int) *ValidatorsInfo {
  149. buf := s.db.Get(calcValidatorsKey(height))
  150. if len(buf) == 0 {
  151. return nil
  152. }
  153. v := new(ValidatorsInfo)
  154. r, n, err := bytes.NewReader(buf), new(int), new(error)
  155. wire.ReadBinaryPtr(v, r, 0, n, err)
  156. if *err != nil {
  157. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  158. cmn.Exit(cmn.Fmt("LoadValidators: Data has been corrupted or its spec has changed: %v\n", *err))
  159. }
  160. // TODO: ensure that buf is completely read.
  161. return v
  162. }
  163. // saveValidatorsInfo persists the validator set for the next block to disk.
  164. // It should be called from s.Save(), right before the state itself is persisted.
  165. // If the validator set did not change after processing the latest block,
  166. // only the last height for which the validators changed is persisted.
  167. func (s *State) saveValidatorsInfo() {
  168. changeHeight := s.LastHeightValidatorsChanged
  169. nextHeight := s.LastBlockHeight + 1
  170. vi := &ValidatorsInfo{
  171. LastHeightChanged: changeHeight,
  172. }
  173. if changeHeight == nextHeight {
  174. vi.ValidatorSet = s.Validators
  175. }
  176. s.db.SetSync(calcValidatorsKey(nextHeight), vi.Bytes())
  177. }
  178. // Equals returns true if the States are identical.
  179. func (s *State) Equals(s2 *State) bool {
  180. return bytes.Equal(s.Bytes(), s2.Bytes())
  181. }
  182. // Bytes serializes the State using go-wire.
  183. func (s *State) Bytes() []byte {
  184. return wire.BinaryBytes(s)
  185. }
  186. // SetBlockAndValidators mutates State variables to update block and validators after running EndBlock.
  187. func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, abciResponses *ABCIResponses) {
  188. // copy the valset so we can apply changes from EndBlock
  189. // and update s.LastValidators and s.Validators
  190. prevValSet := s.Validators.Copy()
  191. nextValSet := prevValSet.Copy()
  192. // update the validator set with the latest abciResponses
  193. if len(abciResponses.EndBlock.Diffs) > 0 {
  194. err := updateValidators(nextValSet, abciResponses.EndBlock.Diffs)
  195. if err != nil {
  196. s.logger.Error("Error changing validator set", "err", err)
  197. // TODO: err or carry on?
  198. }
  199. // change results from this height but only applies to the next height
  200. s.LastHeightValidatorsChanged = header.Height + 1
  201. }
  202. // Update validator accums and set state variables
  203. nextValSet.IncrementAccum(1)
  204. s.setBlockAndValidators(header.Height,
  205. types.BlockID{header.Hash(), blockPartsHeader},
  206. header.Time,
  207. prevValSet, nextValSet)
  208. }
  209. func (s *State) setBlockAndValidators(
  210. height int, blockID types.BlockID, blockTime time.Time,
  211. prevValSet, nextValSet *types.ValidatorSet) {
  212. s.LastBlockHeight = height
  213. s.LastBlockID = blockID
  214. s.LastBlockTime = blockTime
  215. s.Validators = nextValSet
  216. s.LastValidators = prevValSet
  217. }
  218. // GetValidators returns the last and current validator sets.
  219. func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
  220. return s.LastValidators, s.Validators
  221. }
  222. // Params returns the consensus parameters used for
  223. // validating blocks
  224. func (s *State) Params() *types.ConsensusParams {
  225. return s.GenesisDoc.ConsensusParams
  226. }
  227. //------------------------------------------------------------------------
  228. // ABCIResponses retains the responses of the various ABCI calls during block processing.
  229. // It is persisted to disk before calling Commit.
  230. type ABCIResponses struct {
  231. Height int
  232. DeliverTx []*abci.ResponseDeliverTx
  233. EndBlock abci.ResponseEndBlock
  234. txs types.Txs // reference for indexing results by hash
  235. }
  236. // NewABCIResponses returns a new ABCIResponses
  237. func NewABCIResponses(block *types.Block) *ABCIResponses {
  238. return &ABCIResponses{
  239. Height: block.Height,
  240. DeliverTx: make([]*abci.ResponseDeliverTx, block.NumTxs),
  241. txs: block.Data.Txs,
  242. }
  243. }
  244. // Bytes serializes the ABCIResponse using go-wire
  245. func (a *ABCIResponses) Bytes() []byte {
  246. return wire.BinaryBytes(*a)
  247. }
  248. //-----------------------------------------------------------------------------
  249. // ValidatorsInfo represents the latest validator set, or the last time it changed
  250. type ValidatorsInfo struct {
  251. ValidatorSet *types.ValidatorSet
  252. LastHeightChanged int
  253. }
  254. // Bytes serializes the ValidatorsInfo using go-wire
  255. func (vi *ValidatorsInfo) Bytes() []byte {
  256. return wire.BinaryBytes(*vi)
  257. }
  258. //------------------------------------------------------------------------
  259. // Genesis
  260. // MakeGenesisStateFromFile reads and unmarshals state from the given
  261. // file.
  262. //
  263. // Used during replay and in tests.
  264. func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
  265. genDocJSON, err := ioutil.ReadFile(genDocFile)
  266. if err != nil {
  267. cmn.Exit(cmn.Fmt("Couldn't read GenesisDoc file: %v", err))
  268. }
  269. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  270. if err != nil {
  271. cmn.Exit(cmn.Fmt("Error reading GenesisDoc: %v", err))
  272. }
  273. return MakeGenesisState(db, genDoc)
  274. }
  275. // MakeGenesisState creates state from types.GenesisDoc.
  276. //
  277. // Used in tests.
  278. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
  279. err := genDoc.ValidateAndComplete()
  280. if err != nil {
  281. cmn.Exit(cmn.Fmt("Error in genesis file: %v", err))
  282. }
  283. // Make validators slice
  284. validators := make([]*types.Validator, len(genDoc.Validators))
  285. for i, val := range genDoc.Validators {
  286. pubKey := val.PubKey
  287. address := pubKey.Address()
  288. // Make validator
  289. validators[i] = &types.Validator{
  290. Address: address,
  291. PubKey: pubKey,
  292. VotingPower: val.Amount,
  293. }
  294. }
  295. return &State{
  296. db: db,
  297. GenesisDoc: genDoc,
  298. ChainID: genDoc.ChainID,
  299. LastBlockHeight: 0,
  300. LastBlockID: types.BlockID{},
  301. LastBlockTime: genDoc.GenesisTime,
  302. Validators: types.NewValidatorSet(validators),
  303. LastValidators: types.NewValidatorSet(nil),
  304. AppHash: genDoc.AppHash,
  305. TxIndexer: &null.TxIndex{}, // we do not need indexer during replay and in tests
  306. LastHeightValidatorsChanged: 1,
  307. }
  308. }