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.

167 lines
4.7 KiB

9 years ago
  1. package state
  2. import (
  3. "errors"
  4. "fmt"
  5. . "github.com/tendermint/go-common"
  6. "github.com/tendermint/go-events"
  7. "github.com/tendermint/tendermint/proxy"
  8. "github.com/tendermint/tendermint/types"
  9. tmsp "github.com/tendermint/tmsp/types"
  10. )
  11. // Validate block
  12. func (s *State) ValidateBlock(block *types.Block) error {
  13. return s.validateBlock(block)
  14. }
  15. // Execute the block to mutate State.
  16. // Validates block and then executes Data.Txs in the block.
  17. func (s *State) ExecBlock(evsw *events.EventSwitch, proxyAppConn proxy.AppConn, block *types.Block, blockPartsHeader types.PartSetHeader) error {
  18. // Validate the block.
  19. err := s.validateBlock(block)
  20. if err != nil {
  21. return err
  22. }
  23. // Update the validator set
  24. valSet := s.Validators.Copy()
  25. // Update valSet with signatures from block.
  26. updateValidatorsWithBlock(s.LastValidators, valSet, block)
  27. // TODO: Update the validator set (e.g. block.Data.ValidatorUpdates?)
  28. nextValSet := valSet.Copy()
  29. // Execute the block txs
  30. err = s.execBlockOnProxyApp(evsw, proxyAppConn, block)
  31. if err != nil {
  32. // There was some error in proxyApp
  33. // TODO Report error and wait for proxyApp to be available.
  34. return err
  35. }
  36. // All good!
  37. nextValSet.IncrementAccum(1)
  38. s.LastBlockHeight = block.Height
  39. s.LastBlockHash = block.Hash()
  40. s.LastBlockParts = blockPartsHeader
  41. s.LastBlockTime = block.Time
  42. s.Validators = nextValSet
  43. s.LastValidators = valSet
  44. return nil
  45. }
  46. // Executes block's transactions on proxyAppConn.
  47. // TODO: Generate a bitmap or otherwise store tx validity in state.
  48. func (s *State) execBlockOnProxyApp(evsw *events.EventSwitch, proxyAppConn proxy.AppConn, block *types.Block) error {
  49. var validTxs, invalidTxs = 0, 0
  50. // Execute transactions and get hash
  51. proxyCb := func(req tmsp.Request, res tmsp.Response) {
  52. switch res := res.(type) {
  53. case tmsp.ResponseAppendTx:
  54. // TODO: make use of res.Log
  55. // TODO: make use of this info
  56. // Blocks may include invalid txs.
  57. // reqAppendTx := req.(tmsp.RequestAppendTx)
  58. if res.Code == tmsp.RetCodeOK {
  59. validTxs += 1
  60. } else {
  61. log.Debug("Invalid tx", "code", res.Code, "log", res.Log)
  62. invalidTxs += 1
  63. }
  64. }
  65. }
  66. proxyAppConn.SetResponseCallback(proxyCb)
  67. // Run next txs in the block and get new AppHash
  68. for _, tx := range block.Txs {
  69. proxyAppConn.AppendTxAsync(tx)
  70. if err := proxyAppConn.Error(); err != nil {
  71. return err
  72. }
  73. }
  74. hash, logStr, err := proxyAppConn.GetHashSync()
  75. if err != nil {
  76. log.Warn("Error computing proxyAppConn hash", "error", err)
  77. return err
  78. }
  79. if logStr != "" {
  80. log.Debug("GetHash.Log: " + logStr)
  81. }
  82. log.Info(Fmt("ExecBlock got %v valid txs and %v invalid txs", validTxs, invalidTxs))
  83. // Set the state's new AppHash
  84. s.AppHash = hash
  85. return nil
  86. }
  87. func (s *State) validateBlock(block *types.Block) error {
  88. // Basic block validation.
  89. err := block.ValidateBasic(s.ChainID, s.LastBlockHeight, s.LastBlockHash, s.LastBlockParts, s.LastBlockTime, s.AppHash)
  90. if err != nil {
  91. return err
  92. }
  93. // Validate block LastValidation.
  94. if block.Height == 1 {
  95. if len(block.LastValidation.Precommits) != 0 {
  96. return errors.New("Block at height 1 (first block) should have no LastValidation precommits")
  97. }
  98. } else {
  99. if len(block.LastValidation.Precommits) != s.LastValidators.Size() {
  100. return fmt.Errorf("Invalid block validation size. Expected %v, got %v",
  101. s.LastValidators.Size(), len(block.LastValidation.Precommits))
  102. }
  103. err := s.LastValidators.VerifyValidation(
  104. s.ChainID, s.LastBlockHash, s.LastBlockParts, block.Height-1, block.LastValidation)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }
  111. // Updates the LastCommitHeight of the validators in valSet, in place.
  112. // Assumes that lastValSet matches the valset of block.LastValidation
  113. // CONTRACT: lastValSet is not mutated.
  114. func updateValidatorsWithBlock(lastValSet *types.ValidatorSet, valSet *types.ValidatorSet, block *types.Block) {
  115. for i, precommit := range block.LastValidation.Precommits {
  116. if precommit == nil {
  117. continue
  118. }
  119. _, val := lastValSet.GetByIndex(i)
  120. if val == nil {
  121. PanicCrisis(Fmt("Failed to fetch validator at index %v", i))
  122. }
  123. if _, val_ := valSet.GetByAddress(val.Address); val_ != nil {
  124. val_.LastCommitHeight = block.Height - 1
  125. updated := valSet.Update(val_)
  126. if !updated {
  127. PanicCrisis("Failed to update validator LastCommitHeight")
  128. }
  129. } else {
  130. // XXX This is not an error if validator was removed.
  131. // But, we don't mutate validators yet so go ahead and panic.
  132. PanicCrisis("Could not find validator")
  133. }
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. type InvalidTxError struct {
  138. Tx types.Tx
  139. Code tmsp.RetCode
  140. }
  141. func (txErr InvalidTxError) Error() string {
  142. return Fmt("Invalid tx: [%v] code: [%v]", txErr.Tx, txErr.Code)
  143. }