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
3.9 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, s State, b *types.Block) error {
  12. // validate internal consistency
  13. if err := b.ValidateBasic(); err != nil {
  14. return err
  15. }
  16. // validate basic info
  17. if b.ChainID != s.ChainID {
  18. return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID)
  19. }
  20. if b.Height != s.LastBlockHeight+1 {
  21. return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", s.LastBlockHeight+1, b.Height)
  22. }
  23. /* TODO: Determine bounds for Time
  24. See blockchain/reactor "stopSyncingDurationMinutes"
  25. if !b.Time.After(lastBlockTime) {
  26. return errors.New("Invalid Block.Header.Time")
  27. }
  28. */
  29. // validate prev block info
  30. if !b.LastBlockID.Equals(s.LastBlockID) {
  31. return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
  32. }
  33. newTxs := int64(len(b.Data.Txs))
  34. if b.TotalTxs != s.LastBlockTotalTx+newTxs {
  35. return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs)
  36. }
  37. // validate app info
  38. if !bytes.Equal(b.AppHash, s.AppHash) {
  39. return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash)
  40. }
  41. if !bytes.Equal(b.ConsensusHash, s.ConsensusParams.Hash()) {
  42. return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", s.ConsensusParams.Hash(), b.ConsensusHash)
  43. }
  44. if !bytes.Equal(b.LastResultsHash, s.LastResultsHash) {
  45. return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", s.LastResultsHash, b.LastResultsHash)
  46. }
  47. if !bytes.Equal(b.ValidatorsHash, s.Validators.Hash()) {
  48. return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", s.Validators.Hash(), b.ValidatorsHash)
  49. }
  50. // Validate block LastCommit.
  51. if b.Height == 1 {
  52. if len(b.LastCommit.Precommits) != 0 {
  53. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  54. }
  55. } else {
  56. if len(b.LastCommit.Precommits) != s.LastValidators.Size() {
  57. return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
  58. s.LastValidators.Size(), len(b.LastCommit.Precommits))
  59. }
  60. err := s.LastValidators.VerifyCommit(
  61. s.ChainID, s.LastBlockID, b.Height-1, b.LastCommit)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. for _, ev := range b.Evidence.Evidence {
  67. if err := VerifyEvidence(stateDB, s, 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, s State, evidence types.Evidence) error {
  78. height := s.LastBlockHeight
  79. evidenceAge := height - evidence.Height()
  80. maxAge := s.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(s.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. }