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.

205 lines
6.0 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/tendermint/tendermint/crypto"
  7. dbm "github.com/tendermint/tendermint/libs/db"
  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. newTxs := int64(len(block.Data.Txs))
  44. if block.TotalTxs != state.LastBlockTotalTx+newTxs {
  45. return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v",
  46. state.LastBlockTotalTx+newTxs,
  47. block.TotalTxs,
  48. )
  49. }
  50. // Validate app info
  51. if !bytes.Equal(block.AppHash, state.AppHash) {
  52. return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v",
  53. state.AppHash,
  54. block.AppHash,
  55. )
  56. }
  57. if !bytes.Equal(block.ConsensusHash, state.ConsensusParams.Hash()) {
  58. return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v",
  59. state.ConsensusParams.Hash(),
  60. block.ConsensusHash,
  61. )
  62. }
  63. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  64. return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v",
  65. state.LastResultsHash,
  66. block.LastResultsHash,
  67. )
  68. }
  69. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  70. return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v",
  71. state.Validators.Hash(),
  72. block.ValidatorsHash,
  73. )
  74. }
  75. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  76. return fmt.Errorf("Wrong Block.Header.NextValidatorsHash. Expected %X, got %v",
  77. state.NextValidators.Hash(),
  78. block.NextValidatorsHash,
  79. )
  80. }
  81. // Validate block LastCommit.
  82. if block.Height == 1 {
  83. if len(block.LastCommit.Precommits) != 0 {
  84. return errors.New("Block at height 1 can't have LastCommit precommits")
  85. }
  86. } else {
  87. if len(block.LastCommit.Precommits) != state.LastValidators.Size() {
  88. return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
  89. state.LastValidators.Size(),
  90. len(block.LastCommit.Precommits),
  91. )
  92. }
  93. err := state.LastValidators.VerifyCommit(
  94. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. // Validate block Time
  100. if block.Height > 1 {
  101. if !block.Time.After(state.LastBlockTime) {
  102. return fmt.Errorf("Block time %v not greater than last block time %v",
  103. block.Time,
  104. state.LastBlockTime,
  105. )
  106. }
  107. medianTime := MedianTime(block.LastCommit, state.LastValidators)
  108. if !block.Time.Equal(medianTime) {
  109. return fmt.Errorf("Invalid block time. Expected %v, got %v",
  110. medianTime,
  111. block.Time,
  112. )
  113. }
  114. } else if block.Height == 1 {
  115. genesisTime := state.LastBlockTime
  116. if !block.Time.Equal(genesisTime) {
  117. return fmt.Errorf("Block time %v is not equal to genesis time %v",
  118. block.Time,
  119. genesisTime,
  120. )
  121. }
  122. }
  123. // Limit the amount of evidence
  124. maxNumEvidence, _ := types.MaxEvidencePerBlock(state.ConsensusParams.BlockSize.MaxBytes)
  125. numEvidence := int64(len(block.Evidence.Evidence))
  126. if numEvidence > maxNumEvidence {
  127. return types.NewErrEvidenceOverflow(maxNumEvidence, numEvidence)
  128. }
  129. // Validate all evidence.
  130. for _, ev := range block.Evidence.Evidence {
  131. if err := VerifyEvidence(stateDB, state, ev); err != nil {
  132. return types.NewErrEvidenceInvalid(ev, err)
  133. }
  134. if evidencePool != nil && evidencePool.IsCommitted(ev) {
  135. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
  136. }
  137. }
  138. // NOTE: We can't actually verify it's the right proposer because we dont
  139. // know what round the block was first proposed. So just check that it's
  140. // a legit address and a known validator.
  141. if len(block.ProposerAddress) != crypto.AddressSize ||
  142. !state.Validators.HasAddress(block.ProposerAddress) {
  143. return fmt.Errorf("Block.Header.ProposerAddress, %X, is not a validator",
  144. block.ProposerAddress,
  145. )
  146. }
  147. return nil
  148. }
  149. // VerifyEvidence verifies the evidence fully by checking:
  150. // - it is sufficiently recent (MaxAge)
  151. // - it is from a key who was a validator at the given height
  152. // - it is internally consistent
  153. // - it was properly signed by the alleged equivocator
  154. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
  155. height := state.LastBlockHeight
  156. evidenceAge := height - evidence.Height()
  157. maxAge := state.ConsensusParams.Evidence.MaxAge
  158. if evidenceAge > maxAge {
  159. return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
  160. evidence.Height(), height-maxAge)
  161. }
  162. valset, err := LoadValidators(stateDB, evidence.Height())
  163. if err != nil {
  164. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  165. // TODO: if its actually bad evidence, punish peer
  166. return err
  167. }
  168. // The address must have been an active validator at the height.
  169. // NOTE: we will ignore evidence from H if the key was not a validator
  170. // at H, even if it is a validator at some nearby H'
  171. // XXX: this makes lite-client bisection as is unsafe
  172. // See https://github.com/tendermint/tendermint/issues/3244
  173. ev := evidence
  174. height, addr := ev.Height(), ev.Address()
  175. _, val := valset.GetByAddress(addr)
  176. if val == nil {
  177. return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
  178. }
  179. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  180. return err
  181. }
  182. return nil
  183. }