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.

259 lines
7.7 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. //-----------------------------------------------------
  11. // Validate block
  12. func validateBlock(evidencePool EvidencePool, 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.App != state.Version.Consensus.App ||
  19. block.Version.Block != state.Version.Consensus.Block {
  20. return fmt.Errorf("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("wrong Block.Header.ChainID. Expected %v, got %v",
  27. state.ChainID,
  28. block.ChainID,
  29. )
  30. }
  31. if block.Height != state.LastBlockHeight+1 {
  32. return fmt.Errorf("wrong Block.Header.Height. Expected %v, got %v",
  33. state.LastBlockHeight+1,
  34. block.Height,
  35. )
  36. }
  37. // Validate prev block info.
  38. if !block.LastBlockID.Equals(state.LastBlockID) {
  39. return fmt.Errorf("wrong Block.Header.LastBlockID. Expected %v, got %v",
  40. state.LastBlockID,
  41. block.LastBlockID,
  42. )
  43. }
  44. // Validate app info
  45. if !bytes.Equal(block.AppHash, state.AppHash) {
  46. return fmt.Errorf("wrong Block.Header.AppHash. Expected %X, got %v",
  47. state.AppHash,
  48. block.AppHash,
  49. )
  50. }
  51. hashCP := types.HashConsensusParams(state.ConsensusParams)
  52. if !bytes.Equal(block.ConsensusHash, hashCP) {
  53. return fmt.Errorf("wrong Block.Header.ConsensusHash. Expected %X, got %v",
  54. hashCP,
  55. block.ConsensusHash,
  56. )
  57. }
  58. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  59. return fmt.Errorf("wrong Block.Header.LastResultsHash. Expected %X, got %v",
  60. state.LastResultsHash,
  61. block.LastResultsHash,
  62. )
  63. }
  64. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  65. return fmt.Errorf("wrong Block.Header.ValidatorsHash. Expected %X, got %v",
  66. state.Validators.Hash(),
  67. block.ValidatorsHash,
  68. )
  69. }
  70. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  71. return fmt.Errorf("wrong Block.Header.NextValidatorsHash. Expected %X, got %v",
  72. state.NextValidators.Hash(),
  73. block.NextValidatorsHash,
  74. )
  75. }
  76. // Validate block LastCommit.
  77. if block.Height == 1 {
  78. if len(block.LastCommit.Signatures) != 0 {
  79. return errors.New("block at height 1 can't have LastCommit signatures")
  80. }
  81. } else {
  82. // LastCommit.Signatures length is checked in VerifyCommit.
  83. if err := state.LastValidators.VerifyCommit(
  84. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit); err != nil {
  85. return err
  86. }
  87. }
  88. // Validate block Time
  89. if block.Height > 1 {
  90. if !block.Time.After(state.LastBlockTime) {
  91. return fmt.Errorf("block time %v not greater than last block time %v",
  92. block.Time,
  93. state.LastBlockTime,
  94. )
  95. }
  96. medianTime := MedianTime(block.LastCommit, state.LastValidators)
  97. if !block.Time.Equal(medianTime) {
  98. return fmt.Errorf("invalid block time. Expected %v, got %v",
  99. medianTime,
  100. block.Time,
  101. )
  102. }
  103. } else if block.Height == 1 {
  104. genesisTime := state.LastBlockTime
  105. if !block.Time.Equal(genesisTime) {
  106. return fmt.Errorf("block time %v is not equal to genesis time %v",
  107. block.Time,
  108. genesisTime,
  109. )
  110. }
  111. }
  112. // Limit the amount of evidence
  113. numEvidence := len(block.Evidence.Evidence)
  114. // MaxNumEvidence is capped at uint16, so conversion is always safe.
  115. if maxEvidence := int(state.ConsensusParams.Evidence.MaxNum); numEvidence > maxEvidence {
  116. return types.NewErrEvidenceOverflow(maxEvidence, numEvidence)
  117. }
  118. // Validate all evidence.
  119. for idx, ev := range block.Evidence.Evidence {
  120. // check that no evidence has been submitted more than once
  121. for i := idx + 1; i < len(block.Evidence.Evidence); i++ {
  122. if ev.Equal(block.Evidence.Evidence[i]) {
  123. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was submitted twice"))
  124. }
  125. }
  126. if evidencePool != nil {
  127. if evidencePool.IsCommitted(ev) {
  128. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
  129. }
  130. if evidencePool.IsPending(ev) {
  131. continue
  132. }
  133. }
  134. var header *types.Header
  135. if _, ok := ev.(*types.LunaticValidatorEvidence); ok {
  136. header = evidencePool.Header(ev.Height())
  137. if header == nil {
  138. return fmt.Errorf("don't have block meta at height #%d", ev.Height())
  139. }
  140. }
  141. if err := VerifyEvidence(stateDB, state, ev, header); 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) != crypto.AddressSize {
  149. return fmt.Errorf("expected ProposerAddress size %d, got %d",
  150. crypto.AddressSize,
  151. len(block.ProposerAddress),
  152. )
  153. }
  154. if !state.Validators.HasAddress(block.ProposerAddress) {
  155. return fmt.Errorf("block.Header.ProposerAddress %X is not a validator",
  156. block.ProposerAddress,
  157. )
  158. }
  159. return nil
  160. }
  161. // VerifyEvidence verifies the evidence fully by checking:
  162. // - it is sufficiently recent (MaxAge)
  163. // - it is from a key who was a validator at the given height
  164. // - it is internally consistent
  165. // - it was properly signed by the alleged equivocator
  166. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence, committedHeader *types.Header) error {
  167. var (
  168. height = state.LastBlockHeight
  169. evidenceParams = state.ConsensusParams.Evidence
  170. ageDuration = state.LastBlockTime.Sub(evidence.Time())
  171. ageNumBlocks = height - evidence.Height()
  172. )
  173. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  174. return fmt.Errorf(
  175. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  176. evidence.Height(),
  177. evidence.Time(),
  178. height-evidenceParams.MaxAgeNumBlocks,
  179. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  180. )
  181. }
  182. if ev, ok := evidence.(*types.LunaticValidatorEvidence); ok {
  183. if err := ev.VerifyHeader(committedHeader); err != nil {
  184. return err
  185. }
  186. }
  187. valset, err := LoadValidators(stateDB, evidence.Height())
  188. if err != nil {
  189. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  190. // TODO: if its actually bad evidence, punish peer
  191. return err
  192. }
  193. addr := evidence.Address()
  194. var val *types.Validator
  195. // For PhantomValidatorEvidence, check evidence.Address was not part of the
  196. // validator set at height evidence.Height, but was a validator before OR
  197. // after.
  198. if phve, ok := evidence.(*types.PhantomValidatorEvidence); ok {
  199. _, val = valset.GetByAddress(addr)
  200. if val != nil {
  201. return fmt.Errorf("address %X was a validator at height %d", addr, evidence.Height())
  202. }
  203. // check if last height validator was in the validator set is within
  204. // MaxAgeNumBlocks.
  205. if ageNumBlocks > 0 && phve.LastHeightValidatorWasInSet <= ageNumBlocks {
  206. return fmt.Errorf("last time validator was in the set at height %d, min: %d",
  207. phve.LastHeightValidatorWasInSet, ageNumBlocks+1)
  208. }
  209. valset, err := LoadValidators(stateDB, phve.LastHeightValidatorWasInSet)
  210. if err != nil {
  211. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  212. // TODO: if its actually bad evidence, punish peer
  213. return err
  214. }
  215. _, val = valset.GetByAddress(addr)
  216. if val == nil {
  217. return fmt.Errorf("phantom validator %X not found", addr)
  218. }
  219. } else {
  220. // For all other types, expect evidence.Address to be a validator at height
  221. // evidence.Height.
  222. _, val = valset.GetByAddress(addr)
  223. if val == nil {
  224. return fmt.Errorf("address %X was not a validator at height %d", addr, evidence.Height())
  225. }
  226. }
  227. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  228. return err
  229. }
  230. return nil
  231. }