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.

125 lines
4.2 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/tendermint/tendermint/types"
  7. dbm "github.com/tendermint/tmlibs/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. // Validate block LastCommit.
  51. if block.Height == 1 {
  52. if len(block.LastCommit.Precommits) != 0 {
  53. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  54. }
  55. } else {
  56. if len(block.LastCommit.Precommits) != state.LastValidators.Size() {
  57. return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
  58. state.LastValidators.Size(), len(block.LastCommit.Precommits))
  59. }
  60. err := state.LastValidators.VerifyCommit(
  61. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. // TODO: Each check requires loading an old validator set.
  67. // We should cap the amount of evidence per block
  68. // to prevent potential proposer DoS.
  69. for _, ev := range block.Evidence.Evidence {
  70. if err := VerifyEvidence(stateDB, state, ev); err != nil {
  71. return types.NewEvidenceInvalidErr(ev, err)
  72. }
  73. }
  74. return nil
  75. }
  76. // VerifyEvidence verifies the evidence fully by checking:
  77. // - it is sufficiently recent (MaxAge)
  78. // - it is from a key who was a validator at the given height
  79. // - it is internally consistent
  80. // - it was properly signed by the alleged equivocator
  81. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
  82. height := state.LastBlockHeight
  83. evidenceAge := height - evidence.Height()
  84. maxAge := state.ConsensusParams.EvidenceParams.MaxAge
  85. if evidenceAge > maxAge {
  86. return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
  87. evidence.Height(), height-maxAge)
  88. }
  89. valset, err := LoadValidators(stateDB, evidence.Height())
  90. if err != nil {
  91. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  92. // TODO: if its actually bad evidence, punish peer
  93. return err
  94. }
  95. // The address must have been an active validator at the height.
  96. // NOTE: we will ignore evidence from H if the key was not a validator
  97. // at H, even if it is a validator at some nearby H'
  98. ev := evidence
  99. height, addr := ev.Height(), ev.Address()
  100. _, val := valset.GetByAddress(addr)
  101. if val == nil {
  102. return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
  103. }
  104. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  105. return err
  106. }
  107. return nil
  108. }