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.

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