block_validator.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/consensus"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/ethereum/go-ethereum/trie"
  24. )
  25. // BlockValidator is responsible for validating block headers, uncles and
  26. // processed state.
  27. //
  28. // BlockValidator implements Validator.
  29. type BlockValidator struct {
  30. config *params.ChainConfig // Chain configuration options
  31. bc *BlockChain // Canonical block chain
  32. engine consensus.Engine // Consensus engine used for validating
  33. }
  34. // NewBlockValidator returns a new block validator which is safe for re-use
  35. func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
  36. validator := &BlockValidator{
  37. config: config,
  38. engine: engine,
  39. bc: blockchain,
  40. }
  41. return validator
  42. }
  43. // ValidateBody validates the given block's uncles and verifies the block
  44. // header's transaction and uncle roots. The headers are assumed to be already
  45. // validated at this point.
  46. func (v *BlockValidator) ValidateBody(block *types.Block) error {
  47. // Check whether the block's known, and if not, that it's linkable
  48. if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
  49. return ErrKnownBlock
  50. }
  51. // Header validity is known at this point, check the uncles and transactions
  52. header := block.Header()
  53. if err := v.engine.VerifyUncles(v.bc, block); err != nil {
  54. return err
  55. }
  56. if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
  57. return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash)
  58. }
  59. if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash {
  60. return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash)
  61. }
  62. if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
  63. if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
  64. return consensus.ErrUnknownAncestor
  65. }
  66. return consensus.ErrPrunedAncestor
  67. }
  68. return nil
  69. }
  70. // ValidateState validates the various changes that happen after a state
  71. // transition, such as amount of used gas, the receipt roots and the state root
  72. // itself. ValidateState returns a database batch if the validation was a success
  73. // otherwise nil and an error is returned.
  74. func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
  75. header := block.Header()
  76. if block.GasUsed() != usedGas {
  77. return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
  78. }
  79. // Validate the received block's bloom with the one derived from the generated receipts.
  80. // For valid blocks this should always validate to true.
  81. rbloom := types.CreateBloom(receipts)
  82. if rbloom != header.Bloom {
  83. return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
  84. }
  85. // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
  86. receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
  87. if receiptSha != header.ReceiptHash {
  88. return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
  89. }
  90. // Validate the state root against the received state root and throw
  91. // an error if they don't match.
  92. if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
  93. return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
  94. }
  95. return nil
  96. }
  97. // CalcGasLimit computes the gas limit of the next block after parent. It aims
  98. // to keep the baseline gas close to the provided target, and increase it towards
  99. // the target if the baseline gas is lower.
  100. func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
  101. delta := parentGasLimit/params.GasLimitBoundDivisor - 1
  102. limit := parentGasLimit
  103. if desiredLimit < params.MinGasLimit {
  104. desiredLimit = params.MinGasLimit
  105. }
  106. // If we're outside our allowed gas range, we try to hone towards them
  107. if limit < desiredLimit {
  108. limit = parentGasLimit + delta
  109. if limit > desiredLimit {
  110. limit = desiredLimit
  111. }
  112. return limit
  113. }
  114. if limit > desiredLimit {
  115. limit = parentGasLimit - delta
  116. if limit < desiredLimit {
  117. limit = desiredLimit
  118. }
  119. }
  120. return limit
  121. }