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.

122 lines
4.1 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. for _, ev := range block.Evidence.Evidence {
  67. if err := VerifyEvidence(stateDB, state, ev); err != nil {
  68. return types.NewEvidenceInvalidErr(ev, err)
  69. }
  70. }
  71. return nil
  72. }
  73. // XXX: What's cheaper (ie. what should be checked first):
  74. // evidence internal validity (ie. sig checks) or validator existed (fetch historical val set from db)
  75. // VerifyEvidence verifies the evidence fully by checking it is internally
  76. // consistent and sufficiently recent.
  77. func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
  78. height := state.LastBlockHeight
  79. evidenceAge := height - evidence.Height()
  80. maxAge := state.ConsensusParams.EvidenceParams.MaxAge
  81. if evidenceAge > maxAge {
  82. return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
  83. evidence.Height(), height-maxAge)
  84. }
  85. if err := evidence.Verify(state.ChainID); err != nil {
  86. return err
  87. }
  88. valset, err := LoadValidators(stateDB, evidence.Height())
  89. if err != nil {
  90. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  91. // TODO: if its actually bad evidence, punish peer
  92. return err
  93. }
  94. // The address must have been an active validator at the height
  95. ev := evidence
  96. height, addr, idx := ev.Height(), ev.Address(), ev.Index()
  97. valIdx, val := valset.GetByAddress(addr)
  98. if val == nil {
  99. return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
  100. } else if idx != valIdx {
  101. return fmt.Errorf("Address %X was validator %d at height %d, not %d", addr, valIdx, height, idx)
  102. }
  103. return nil
  104. }