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.

129 lines
4.4 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/tendermint/tendermint/types"
  7. dbm "github.com/tendermint/tendermint/libs/db"
  8. )
  9. //-----------------------------------------------------
  10. // Validate block
  11. func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
  12. // Validate internal consistency.
  13. if err := block.ValidateBasic(); err != nil {
  14. return err
  15. }
  16. // Validate basic info.
  17. if block.ChainID != state.ChainID {
  18. return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", state.ChainID, block.ChainID)
  19. }
  20. if block.Height != state.LastBlockHeight+1 {
  21. return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", state.LastBlockHeight+1, block.Height)
  22. }
  23. /* TODO: Determine bounds for Time
  24. See blockchain/reactor "stopSyncingDurationMinutes"
  25. if !block.Time.After(lastBlockTime) {
  26. return errors.New("Invalid Block.Header.Time")
  27. }
  28. */
  29. // Validate prev block info.
  30. if !block.LastBlockID.Equals(state.LastBlockID) {
  31. return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", state.LastBlockID, block.LastBlockID)
  32. }
  33. newTxs := int64(len(block.Data.Txs))
  34. if block.TotalTxs != state.LastBlockTotalTx+newTxs {
  35. return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", state.LastBlockTotalTx+newTxs, block.TotalTxs)
  36. }
  37. // Validate app info
  38. if !bytes.Equal(block.AppHash, state.AppHash) {
  39. return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", state.AppHash, block.AppHash)
  40. }
  41. if !bytes.Equal(block.ConsensusHash, state.ConsensusParams.Hash()) {
  42. return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", state.ConsensusParams.Hash(), block.ConsensusHash)
  43. }
  44. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  45. return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", state.LastResultsHash, block.LastResultsHash)
  46. }
  47. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  48. return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", state.Validators.Hash(), block.ValidatorsHash)
  49. }
  50. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  51. return fmt.Errorf("Wrong Block.Header.NextValidatorsHash. Expected %X, got %v", state.NextValidators.Hash(), block.NextValidatorsHash)
  52. }
  53. // Validate block LastCommit.
  54. if block.Height == 1 {
  55. if len(block.LastCommit.Precommits) != 0 {
  56. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  57. }
  58. } else {
  59. if len(block.LastCommit.Precommits) != state.LastValidators.Size() {
  60. return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
  61. state.LastValidators.Size(), len(block.LastCommit.Precommits))
  62. }
  63. err := state.LastValidators.VerifyCommit(
  64. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. // Validate all evidence.
  70. // TODO: Each check requires loading an old validator set.
  71. // We should cap the amount of evidence per block
  72. // to prevent potential proposer DoS.
  73. for _, ev := range block.Evidence.Evidence {
  74. if err := VerifyEvidence(stateDB, state, ev); err != nil {
  75. return types.NewEvidenceInvalidErr(ev, err)
  76. }
  77. }
  78. return nil
  79. }
  80. // VerifyEvidence verifies the evidence fully by checking:
  81. // - it is sufficiently recent (MaxAge)
  82. // - it is from a key who was a validator at the given height
  83. // - it is internally consistent
  84. // - it was properly signed by the alleged equivocator
  85. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
  86. height := state.LastBlockHeight
  87. evidenceAge := height - evidence.Height()
  88. maxAge := state.ConsensusParams.EvidenceParams.MaxAge
  89. if evidenceAge > maxAge {
  90. return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
  91. evidence.Height(), height-maxAge)
  92. }
  93. valset, err := LoadValidators(stateDB, evidence.Height())
  94. if err != nil {
  95. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  96. // TODO: if its actually bad evidence, punish peer
  97. return err
  98. }
  99. // The address must have been an active validator at the height.
  100. // NOTE: we will ignore evidence from H if the key was not a validator
  101. // at H, even if it is a validator at some nearby H'
  102. ev := evidence
  103. height, addr := ev.Height(), ev.Address()
  104. _, val := valset.GetByAddress(addr)
  105. if val == nil {
  106. return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
  107. }
  108. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  109. return err
  110. }
  111. return nil
  112. }