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.

152 lines
4.4 KiB

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