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.

170 lines
4.9 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/tendermint/tendermint/crypto"
  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.App != state.Version.Consensus.App ||
  19. block.Version.Block != state.Version.Consensus.Block {
  20. return fmt.Errorf("wrong Block.Header.Version. Expected %v, got %v",
  21. state.Version.Consensus,
  22. block.Version,
  23. )
  24. }
  25. if block.ChainID != state.ChainID {
  26. return fmt.Errorf("wrong Block.Header.ChainID. Expected %v, got %v",
  27. state.ChainID,
  28. block.ChainID,
  29. )
  30. }
  31. if state.LastBlockHeight == 0 && block.Height != state.InitialHeight {
  32. return fmt.Errorf("wrong Block.Header.Height. Expected %v for initial block, got %v",
  33. block.Height, state.InitialHeight)
  34. }
  35. if state.LastBlockHeight > 0 && block.Height != state.LastBlockHeight+1 {
  36. return fmt.Errorf("wrong Block.Header.Height. Expected %v, got %v",
  37. state.LastBlockHeight+1,
  38. block.Height,
  39. )
  40. }
  41. // Validate prev block info.
  42. if !block.LastBlockID.Equals(state.LastBlockID) {
  43. return fmt.Errorf("wrong Block.Header.LastBlockID. Expected %v, got %v",
  44. state.LastBlockID,
  45. block.LastBlockID,
  46. )
  47. }
  48. // Validate app info
  49. if !bytes.Equal(block.AppHash, state.AppHash) {
  50. return fmt.Errorf("wrong Block.Header.AppHash. Expected %X, got %v",
  51. state.AppHash,
  52. block.AppHash,
  53. )
  54. }
  55. hashCP := types.HashConsensusParams(state.ConsensusParams)
  56. if !bytes.Equal(block.ConsensusHash, hashCP) {
  57. return fmt.Errorf("wrong Block.Header.ConsensusHash. Expected %X, got %v",
  58. hashCP,
  59. block.ConsensusHash,
  60. )
  61. }
  62. if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
  63. return fmt.Errorf("wrong Block.Header.LastResultsHash. Expected %X, got %v",
  64. state.LastResultsHash,
  65. block.LastResultsHash,
  66. )
  67. }
  68. if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
  69. return fmt.Errorf("wrong Block.Header.ValidatorsHash. Expected %X, got %v",
  70. state.Validators.Hash(),
  71. block.ValidatorsHash,
  72. )
  73. }
  74. if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
  75. return fmt.Errorf("wrong Block.Header.NextValidatorsHash. Expected %X, got %v",
  76. state.NextValidators.Hash(),
  77. block.NextValidatorsHash,
  78. )
  79. }
  80. // Validate block LastCommit.
  81. if block.Height == state.InitialHeight {
  82. if len(block.LastCommit.Signatures) != 0 {
  83. return errors.New("initial block can't have LastCommit signatures")
  84. }
  85. } else {
  86. // LastCommit.Signatures length is checked in VerifyCommit.
  87. if err := state.LastValidators.VerifyCommit(
  88. state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit); err != nil {
  89. return err
  90. }
  91. }
  92. // NOTE: We can't actually verify it's the right proposer because we dont
  93. // know what round the block was first proposed. So just check that it's
  94. // a legit address and a known validator.
  95. if len(block.ProposerAddress) != crypto.AddressSize {
  96. return fmt.Errorf("expected ProposerAddress size %d, got %d",
  97. crypto.AddressSize,
  98. len(block.ProposerAddress),
  99. )
  100. }
  101. if !state.Validators.HasAddress(block.ProposerAddress) {
  102. return fmt.Errorf("block.Header.ProposerAddress %X is not a validator",
  103. block.ProposerAddress,
  104. )
  105. }
  106. // Validate block Time
  107. switch {
  108. case block.Height > state.InitialHeight:
  109. if !block.Time.After(state.LastBlockTime) {
  110. return fmt.Errorf("block time %v not greater than last block time %v",
  111. block.Time,
  112. state.LastBlockTime,
  113. )
  114. }
  115. medianTime := MedianTime(block.LastCommit, state.LastValidators)
  116. if !block.Time.Equal(medianTime) {
  117. return fmt.Errorf("invalid block time. Expected %v, got %v",
  118. medianTime,
  119. block.Time,
  120. )
  121. }
  122. case block.Height == state.InitialHeight:
  123. genesisTime := state.LastBlockTime
  124. if !block.Time.Equal(genesisTime) {
  125. return fmt.Errorf("block time %v is not equal to genesis time %v",
  126. block.Time,
  127. genesisTime,
  128. )
  129. }
  130. default:
  131. return fmt.Errorf("block height %v lower than initial height %v",
  132. block.Height, state.InitialHeight)
  133. }
  134. // Check evidence doesn't exceed the limit. MaxNumEvidence is capped at uint16, so conversion is always safe.
  135. if max, got := int(state.ConsensusParams.Evidence.MaxNum), len(block.Evidence.Evidence); got > max {
  136. return types.NewErrEvidenceOverflow(max, got)
  137. }
  138. // Validate all evidence.
  139. for idx, ev := range block.Evidence.Evidence {
  140. // Check that no evidence has been submitted more than once
  141. for i := idx + 1; i < len(block.Evidence.Evidence); i++ {
  142. if ev.Equal(block.Evidence.Evidence[i]) {
  143. return types.NewErrEvidenceInvalid(ev, errors.New("evidence was submitted twice"))
  144. }
  145. }
  146. // Verify evidence using the evidence pool
  147. err := evidencePool.Verify(ev)
  148. if err != nil {
  149. return types.NewErrEvidenceInvalid(ev, err)
  150. }
  151. }
  152. return nil
  153. }