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.

241 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. if len(block.LastCommit.Signatures) != state.LastValidators.Size() {
  81. return types.NewErrInvalidCommitSignatures(state.LastValidators.Size(), len(block.LastCommit.Signatures))
  82. }
  83. err := state.LastValidators.VerifyCommit(
  84. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
  85. if err != nil {
  86. return err
  87. }
  88. }
  89. // Validate block Time
  90. if block.Height > 1 {
  91. if !block.Time.After(state.LastBlockTime) {
  92. return fmt.Errorf("block time %v not greater than last block time %v",
  93. block.Time,
  94. state.LastBlockTime,
  95. )
  96. }
  97. medianTime := MedianTime(block.LastCommit, state.LastValidators)
  98. if !block.Time.Equal(medianTime) {
  99. return fmt.Errorf("invalid block time. Expected %v, got %v",
  100. medianTime,
  101. block.Time,
  102. )
  103. }
  104. } else if block.Height == 1 {
  105. genesisTime := state.LastBlockTime
  106. if !block.Time.Equal(genesisTime) {
  107. return fmt.Errorf("block time %v is not equal to genesis time %v",
  108. block.Time,
  109. genesisTime,
  110. )
  111. }
  112. }
  113. // Limit the amount of evidence
  114. maxNumEvidence, _ := types.MaxEvidencePerBlock(state.ConsensusParams.Block.MaxBytes)
  115. numEvidence := int64(len(block.Evidence.Evidence))
  116. if numEvidence > maxNumEvidence {
  117. return types.NewErrEvidenceOverflow(maxNumEvidence, numEvidence)
  118. }
  119. // Validate all evidence.
  120. for _, ev := range block.Evidence.Evidence {
  121. if evidencePool != nil {
  122. if evidencePool.IsCommitted(ev) {
  123. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
  124. }
  125. if evidencePool.IsPending(ev) {
  126. continue
  127. }
  128. }
  129. if err := VerifyEvidence(stateDB, state, ev, &block.Header); err != nil {
  130. return types.NewErrEvidenceInvalid(ev, err)
  131. }
  132. }
  133. // NOTE: We can't actually verify it's the right proposer because we dont
  134. // know what round the block was first proposed. So just check that it's
  135. // a legit address and a known validator.
  136. if len(block.ProposerAddress) != crypto.AddressSize ||
  137. !state.Validators.HasAddress(block.ProposerAddress) {
  138. return fmt.Errorf("block.Header.ProposerAddress, %X, is not a validator",
  139. block.ProposerAddress,
  140. )
  141. }
  142. return nil
  143. }
  144. // VerifyEvidence verifies the evidence fully by checking:
  145. // - it is sufficiently recent (MaxAge)
  146. // - it is from a key who was a validator at the given height
  147. // - it is internally consistent
  148. // - it was properly signed by the alleged equivocator
  149. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence, committedHeader *types.Header) error {
  150. var (
  151. height = state.LastBlockHeight
  152. evidenceParams = state.ConsensusParams.Evidence
  153. ageDuration = state.LastBlockTime.Sub(evidence.Time())
  154. ageNumBlocks = height - evidence.Height()
  155. )
  156. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  157. return fmt.Errorf(
  158. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  159. evidence.Height(),
  160. evidence.Time(),
  161. height-evidenceParams.MaxAgeNumBlocks,
  162. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  163. )
  164. }
  165. if ev, ok := evidence.(*types.LunaticValidatorEvidence); ok {
  166. if err := ev.VerifyHeader(committedHeader); err != nil {
  167. return err
  168. }
  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. addr := evidence.Address()
  177. var val *types.Validator
  178. // For PhantomValidatorEvidence, check evidence.Address was not part of the
  179. // validator set at height evidence.Height, but was a validator before OR
  180. // after.
  181. if phve, ok := evidence.(*types.PhantomValidatorEvidence); ok {
  182. _, val = valset.GetByAddress(addr)
  183. if val != nil {
  184. return fmt.Errorf("address %X was a validator at height %d", addr, evidence.Height())
  185. }
  186. // check if last height validator was in the validator set is within
  187. // MaxAgeNumBlocks.
  188. if ageNumBlocks > 0 && phve.LastHeightValidatorWasInSet <= ageNumBlocks {
  189. return fmt.Errorf("last time validator was in the set at height %d, min: %d",
  190. phve.LastHeightValidatorWasInSet, ageNumBlocks+1)
  191. }
  192. valset, err := LoadValidators(stateDB, phve.LastHeightValidatorWasInSet)
  193. if err != nil {
  194. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  195. // TODO: if its actually bad evidence, punish peer
  196. return err
  197. }
  198. _, val = valset.GetByAddress(addr)
  199. if val == nil {
  200. return fmt.Errorf("phantom validator %X not found", addr)
  201. }
  202. } else {
  203. // For all other types, expect evidence.Address to be a validator at height
  204. // evidence.Height.
  205. _, val = valset.GetByAddress(addr)
  206. if val == nil {
  207. return fmt.Errorf("address %X was not a validator at height %d", addr, evidence.Height())
  208. }
  209. }
  210. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  211. return err
  212. }
  213. return nil
  214. }