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.

209 lines
5.8 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/tendermint/tendermint/crypto/tmhash"
  7. dbm "github.com/tendermint/tendermint/libs/db"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. //-----------------------------------------------------
  11. // Validate block
  12. func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
  13. // Validate internal consistency.
  14. if err := block.ValidateBasic(); err != nil {
  15. return err
  16. }
  17. // Validate basic info.
  18. if block.Version != state.Version.Consensus {
  19. return fmt.Errorf(
  20. "Wrong Block.Header.Version. Expected %v, got %v",
  21. state.Version.Consensus,
  22. block.Version,
  23. )
  24. }
  25. if block.ChainID != state.ChainID {
  26. return fmt.Errorf(
  27. "Wrong Block.Header.ChainID. Expected %v, got %v",
  28. state.ChainID,
  29. block.ChainID,
  30. )
  31. }
  32. if block.Height != state.LastBlockHeight+1 {
  33. return fmt.Errorf(
  34. "Wrong Block.Header.Height. Expected %v, got %v",
  35. state.LastBlockHeight+1,
  36. block.Height,
  37. )
  38. }
  39. // Validate prev block info.
  40. if !block.LastBlockID.Equals(state.LastBlockID) {
  41. return fmt.Errorf(
  42. "Wrong Block.Header.LastBlockID. Expected %v, got %v",
  43. state.LastBlockID,
  44. block.LastBlockID,
  45. )
  46. }
  47. newTxs := int64(len(block.Data.Txs))
  48. if block.TotalTxs != state.LastBlockTotalTx+newTxs {
  49. return fmt.Errorf(
  50. "Wrong Block.Header.TotalTxs. Expected %v, got %v",
  51. state.LastBlockTotalTx+newTxs,
  52. block.TotalTxs,
  53. )
  54. }
  55. // Validate app info
  56. if !bytes.Equal(block.AppHash, state.AppHash) {
  57. return fmt.Errorf(
  58. "Wrong Block.Header.AppHash. Expected %X, got %v",
  59. state.AppHash,
  60. block.AppHash,
  61. )
  62. }
  63. if !bytes.Equal(block.ConsensusHash, state.ConsensusParams.Hash()) {
  64. return fmt.Errorf(
  65. "Wrong Block.Header.ConsensusHash. Expected %X, got %v",
  66. state.ConsensusParams.Hash(),
  67. block.ConsensusHash,
  68. )
  69. }
  70. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  71. return fmt.Errorf(
  72. "Wrong Block.Header.LastResultsHash. Expected %X, got %v",
  73. state.LastResultsHash,
  74. block.LastResultsHash,
  75. )
  76. }
  77. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  78. return fmt.Errorf(
  79. "Wrong Block.Header.ValidatorsHash. Expected %X, got %v",
  80. state.Validators.Hash(),
  81. block.ValidatorsHash,
  82. )
  83. }
  84. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  85. return fmt.Errorf("Wrong Block.Header.NextValidatorsHash. Expected %X, got %v", state.NextValidators.Hash(), block.NextValidatorsHash)
  86. }
  87. // Validate block LastCommit.
  88. if block.Height == 1 {
  89. if len(block.LastCommit.Precommits) != 0 {
  90. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  91. }
  92. } else {
  93. if len(block.LastCommit.Precommits) != state.LastValidators.Size() {
  94. return fmt.Errorf(
  95. "Invalid block commit size. Expected %v, got %v",
  96. state.LastValidators.Size(),
  97. len(block.LastCommit.Precommits),
  98. )
  99. }
  100. err := state.LastValidators.VerifyCommit(
  101. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. // Validate block Time
  107. if block.Height > 1 {
  108. if !block.Time.After(state.LastBlockTime) {
  109. return fmt.Errorf(
  110. "Block time %v not greater than last block time %v",
  111. block.Time,
  112. state.LastBlockTime,
  113. )
  114. }
  115. medianTime := MedianTime(block.LastCommit, state.LastValidators)
  116. if !block.Time.Equal(medianTime) {
  117. return fmt.Errorf(
  118. "Invalid block time. Expected %v, got %v",
  119. medianTime,
  120. block.Time,
  121. )
  122. }
  123. } else if block.Height == 1 {
  124. genesisTime := state.LastBlockTime
  125. if !block.Time.Equal(genesisTime) {
  126. return fmt.Errorf(
  127. "Block time %v is not equal to genesis time %v",
  128. block.Time,
  129. genesisTime,
  130. )
  131. }
  132. }
  133. // Limit the amount of evidence
  134. maxEvidenceBytes := types.MaxEvidenceBytesPerBlock(state.ConsensusParams.BlockSize.MaxBytes)
  135. evidenceBytes := int64(len(block.Evidence.Evidence)) * types.MaxEvidenceBytes
  136. if evidenceBytes > maxEvidenceBytes {
  137. return types.NewErrEvidenceOverflow(maxEvidenceBytes, evidenceBytes)
  138. }
  139. // Validate all evidence.
  140. for _, ev := range block.Evidence.Evidence {
  141. if err := VerifyEvidence(stateDB, state, ev); err != nil {
  142. return types.NewErrEvidenceInvalid(ev, err)
  143. }
  144. }
  145. // NOTE: We can't actually verify it's the right proposer because we dont
  146. // know what round the block was first proposed. So just check that it's
  147. // a legit address and a known validator.
  148. if len(block.ProposerAddress) != tmhash.Size ||
  149. !state.Validators.HasAddress(block.ProposerAddress) {
  150. return fmt.Errorf(
  151. "Block.Header.ProposerAddress, %X, is not a validator",
  152. block.ProposerAddress,
  153. )
  154. }
  155. return nil
  156. }
  157. // VerifyEvidence verifies the evidence fully by checking:
  158. // - it is sufficiently recent (MaxAge)
  159. // - it is from a key who was a validator at the given height
  160. // - it is internally consistent
  161. // - it was properly signed by the alleged equivocator
  162. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
  163. height := state.LastBlockHeight
  164. evidenceAge := height - evidence.Height()
  165. maxAge := state.ConsensusParams.EvidenceParams.MaxAge
  166. if evidenceAge > maxAge {
  167. return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
  168. evidence.Height(), height-maxAge)
  169. }
  170. valset, err := LoadValidators(stateDB, evidence.Height())
  171. if err != nil {
  172. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  173. // TODO: if its actually bad evidence, punish peer
  174. return err
  175. }
  176. // The address must have been an active validator at the height.
  177. // NOTE: we will ignore evidence from H if the key was not a validator
  178. // at H, even if it is a validator at some nearby H'
  179. ev := evidence
  180. height, addr := ev.Height(), ev.Address()
  181. _, val := valset.GetByAddress(addr)
  182. if val == nil {
  183. return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
  184. }
  185. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  186. return err
  187. }
  188. return nil
  189. }