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.

242 lines
7.1 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 _, ev := range block.Evidence.Evidence {
  118. if evidencePool != nil {
  119. if evidencePool.IsCommitted(ev) {
  120. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
  121. }
  122. if evidencePool.IsPending(ev) {
  123. continue
  124. }
  125. }
  126. if err := VerifyEvidence(stateDB, state, ev, &block.Header); err != nil {
  127. return types.NewErrEvidenceInvalid(ev, err)
  128. }
  129. }
  130. // NOTE: We can't actually verify it's the right proposer because we dont
  131. // know what round the block was first proposed. So just check that it's
  132. // a legit address and a known validator.
  133. if len(block.ProposerAddress) != crypto.AddressSize {
  134. return fmt.Errorf("expected ProposerAddress size %d, got %d",
  135. crypto.AddressSize,
  136. len(block.ProposerAddress),
  137. )
  138. }
  139. if !state.Validators.HasAddress(block.ProposerAddress) {
  140. return fmt.Errorf("block.Header.ProposerAddress %X is not a validator",
  141. block.ProposerAddress,
  142. )
  143. }
  144. return nil
  145. }
  146. // VerifyEvidence verifies the evidence fully by checking:
  147. // - it is sufficiently recent (MaxAge)
  148. // - it is from a key who was a validator at the given height
  149. // - it is internally consistent
  150. // - it was properly signed by the alleged equivocator
  151. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence, committedHeader *types.Header) error {
  152. var (
  153. height = state.LastBlockHeight
  154. evidenceParams = state.ConsensusParams.Evidence
  155. ageDuration = state.LastBlockTime.Sub(evidence.Time())
  156. ageNumBlocks = height - evidence.Height()
  157. )
  158. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  159. return fmt.Errorf(
  160. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  161. evidence.Height(),
  162. evidence.Time(),
  163. height-evidenceParams.MaxAgeNumBlocks,
  164. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  165. )
  166. }
  167. if ev, ok := evidence.(*types.LunaticValidatorEvidence); ok {
  168. if err := ev.VerifyHeader(committedHeader); err != nil {
  169. return err
  170. }
  171. }
  172. valset, err := LoadValidators(stateDB, evidence.Height())
  173. if err != nil {
  174. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  175. // TODO: if its actually bad evidence, punish peer
  176. return err
  177. }
  178. addr := evidence.Address()
  179. var val *types.Validator
  180. // For PhantomValidatorEvidence, check evidence.Address was not part of the
  181. // validator set at height evidence.Height, but was a validator before OR
  182. // after.
  183. if phve, ok := evidence.(*types.PhantomValidatorEvidence); ok {
  184. _, val = valset.GetByAddress(addr)
  185. if val != nil {
  186. return fmt.Errorf("address %X was a validator at height %d", addr, evidence.Height())
  187. }
  188. // check if last height validator was in the validator set is within
  189. // MaxAgeNumBlocks.
  190. if ageNumBlocks > 0 && phve.LastHeightValidatorWasInSet <= ageNumBlocks {
  191. return fmt.Errorf("last time validator was in the set at height %d, min: %d",
  192. phve.LastHeightValidatorWasInSet, ageNumBlocks+1)
  193. }
  194. valset, err := LoadValidators(stateDB, phve.LastHeightValidatorWasInSet)
  195. if err != nil {
  196. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  197. // TODO: if its actually bad evidence, punish peer
  198. return err
  199. }
  200. _, val = valset.GetByAddress(addr)
  201. if val == nil {
  202. return fmt.Errorf("phantom validator %X not found", addr)
  203. }
  204. } else {
  205. // For all other types, expect evidence.Address to be a validator at height
  206. // evidence.Height.
  207. _, val = valset.GetByAddress(addr)
  208. if val == nil {
  209. return fmt.Errorf("address %X was not a validator at height %d", addr, evidence.Height())
  210. }
  211. }
  212. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  213. return err
  214. }
  215. return nil
  216. }