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.

267 lines
8.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.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 state.LastBlockHeight == 0 && block.Height != state.InitialHeight {
  32. return fmt.Errorf("wrong Block.Header.Height. Expected %v for initial block, got %v",
  33. block.Height, state.InitialHeight)
  34. }
  35. if state.LastBlockHeight > 0 && block.Height != state.LastBlockHeight+1 {
  36. return fmt.Errorf("wrong Block.Header.Height. Expected %v, got %v",
  37. state.LastBlockHeight+1,
  38. block.Height,
  39. )
  40. }
  41. // Validate prev block info.
  42. if !block.LastBlockID.Equals(state.LastBlockID) {
  43. return fmt.Errorf("wrong Block.Header.LastBlockID. Expected %v, got %v",
  44. state.LastBlockID,
  45. block.LastBlockID,
  46. )
  47. }
  48. // Validate app info
  49. if !bytes.Equal(block.AppHash, state.AppHash) {
  50. return fmt.Errorf("wrong Block.Header.AppHash. Expected %X, got %v",
  51. state.AppHash,
  52. block.AppHash,
  53. )
  54. }
  55. hashCP := types.HashConsensusParams(state.ConsensusParams)
  56. if !bytes.Equal(block.ConsensusHash, hashCP) {
  57. return fmt.Errorf("wrong Block.Header.ConsensusHash. Expected %X, got %v",
  58. hashCP,
  59. block.ConsensusHash,
  60. )
  61. }
  62. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  63. return fmt.Errorf("wrong Block.Header.LastResultsHash. Expected %X, got %v",
  64. state.LastResultsHash,
  65. block.LastResultsHash,
  66. )
  67. }
  68. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  69. return fmt.Errorf("wrong Block.Header.ValidatorsHash. Expected %X, got %v",
  70. state.Validators.Hash(),
  71. block.ValidatorsHash,
  72. )
  73. }
  74. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  75. return fmt.Errorf("wrong Block.Header.NextValidatorsHash. Expected %X, got %v",
  76. state.NextValidators.Hash(),
  77. block.NextValidatorsHash,
  78. )
  79. }
  80. // Validate block LastCommit.
  81. if block.Height == state.InitialHeight {
  82. if len(block.LastCommit.Signatures) != 0 {
  83. return errors.New("initial block can't have LastCommit signatures")
  84. }
  85. } else {
  86. // LastCommit.Signatures length is checked in VerifyCommit.
  87. if err := state.LastValidators.VerifyCommit(
  88. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit); err != nil {
  89. return err
  90. }
  91. }
  92. // NOTE: We can't actually verify it's the right proposer because we dont
  93. // know what round the block was first proposed. So just check that it's
  94. // a legit address and a known validator.
  95. if len(block.ProposerAddress) != crypto.AddressSize {
  96. return fmt.Errorf("expected ProposerAddress size %d, got %d",
  97. crypto.AddressSize,
  98. len(block.ProposerAddress),
  99. )
  100. }
  101. if !state.Validators.HasAddress(block.ProposerAddress) {
  102. return fmt.Errorf("block.Header.ProposerAddress %X is not a validator",
  103. block.ProposerAddress,
  104. )
  105. }
  106. // Validate block Time
  107. switch {
  108. case block.Height > state.InitialHeight:
  109. if !block.Time.After(state.LastBlockTime) {
  110. return fmt.Errorf("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("invalid block time. Expected %v, got %v",
  118. medianTime,
  119. block.Time,
  120. )
  121. }
  122. case block.Height == state.InitialHeight:
  123. genesisTime := state.LastBlockTime
  124. if !block.Time.Equal(genesisTime) {
  125. return fmt.Errorf("block time %v is not equal to genesis time %v",
  126. block.Time,
  127. genesisTime,
  128. )
  129. }
  130. default:
  131. return fmt.Errorf("block height %v lower than initial height %v",
  132. block.Height, state.InitialHeight)
  133. }
  134. // Limit the amount of evidence
  135. numEvidence := len(block.Evidence.Evidence)
  136. // MaxNumEvidence is capped at uint16, so conversion is always safe.
  137. if maxEvidence := int(state.ConsensusParams.Evidence.MaxNum); numEvidence > maxEvidence {
  138. return types.NewErrEvidenceOverflow(maxEvidence, numEvidence)
  139. }
  140. // Validate all evidence.
  141. for idx, ev := range block.Evidence.Evidence {
  142. // check that no evidence has been submitted more than once
  143. for i := idx + 1; i < len(block.Evidence.Evidence); i++ {
  144. if ev.Equal(block.Evidence.Evidence[i]) {
  145. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was submitted twice"))
  146. }
  147. }
  148. if evidencePool != nil {
  149. if evidencePool.IsCommitted(ev) {
  150. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
  151. }
  152. if evidencePool.IsPending(ev) {
  153. continue
  154. }
  155. }
  156. // if we don't already have amnesia evidence we need to add it to start our own trial period unless
  157. // a) a valid polc has already been attached
  158. // b) the accused node voted back on an earlier round
  159. if ae, ok := ev.(*types.AmnesiaEvidence); ok && ae.Polc.IsAbsent() && ae.PotentialAmnesiaEvidence.VoteA.Round <
  160. ae.PotentialAmnesiaEvidence.VoteB.Round {
  161. if err := evidencePool.AddEvidence(ae.PotentialAmnesiaEvidence); err != nil {
  162. return types.NewErrEvidenceInvalid(ev,
  163. fmt.Errorf("unknown amnesia evidence, trying to add to evidence pool, err: %w", err))
  164. }
  165. return types.NewErrEvidenceInvalid(ev, errors.New("amnesia evidence is new and hasn't undergone trial period yet"))
  166. }
  167. // A header needs to be fetched. For lunatic evidence this is so we can verify
  168. // that some of the fields are different to the ones we have. For all evidence it
  169. // it so we can verify that the time of the evidence is correct
  170. header := evidencePool.Header(ev.Height())
  171. if header == nil {
  172. return fmt.Errorf("don't have block meta at height #%d", ev.Height())
  173. }
  174. if err := VerifyEvidence(stateDB, state, ev, header); err != nil {
  175. return types.NewErrEvidenceInvalid(ev, err)
  176. }
  177. }
  178. return nil
  179. }
  180. // VerifyEvidence verifies the evidence fully by checking:
  181. // - it is sufficiently recent (MaxAge)
  182. // - it is from a key who was a validator at the given height
  183. // - it is internally consistent
  184. // - it was properly signed by the alleged equivocator
  185. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence, committedHeader *types.Header) error {
  186. var (
  187. height = state.LastBlockHeight
  188. evidenceParams = state.ConsensusParams.Evidence
  189. ageDuration = state.LastBlockTime.Sub(evidence.Time())
  190. ageNumBlocks = height - evidence.Height()
  191. )
  192. if committedHeader.Time != evidence.Time() {
  193. return fmt.Errorf("evidence time (%v) is different to the time of the header we have for the same height (%v)",
  194. evidence.Time(),
  195. committedHeader.Time,
  196. )
  197. }
  198. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  199. return fmt.Errorf(
  200. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  201. evidence.Height(),
  202. evidence.Time(),
  203. height-evidenceParams.MaxAgeNumBlocks,
  204. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  205. )
  206. }
  207. if ev, ok := evidence.(*types.LunaticValidatorEvidence); ok {
  208. if err := ev.VerifyHeader(committedHeader); err != nil {
  209. return err
  210. }
  211. }
  212. valset, err := LoadValidators(stateDB, evidence.Height())
  213. if err != nil {
  214. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  215. // TODO: if its actually bad evidence, punish peer
  216. return err
  217. }
  218. addr := evidence.Address()
  219. var val *types.Validator
  220. if ae, ok := evidence.(*types.AmnesiaEvidence); ok {
  221. // check the validator set against the polc to make sure that a majority of valid votes was reached
  222. if !ae.Polc.IsAbsent() {
  223. err = ae.Polc.ValidateVotes(valset, state.ChainID)
  224. if err != nil {
  225. return fmt.Errorf("amnesia evidence contains invalid polc, err: %w", err)
  226. }
  227. }
  228. }
  229. // For all other types, expect evidence.Address to be a validator at height
  230. // evidence.Height.
  231. _, val = valset.GetByAddress(addr)
  232. if val == nil {
  233. return fmt.Errorf("address %X was not a validator at height %d", addr, evidence.Height())
  234. }
  235. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  236. return err
  237. }
  238. return nil
  239. }