eip1559.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2021 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 misc
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. // VerifyEip1559Header verifies some header attributes which were changed in EIP-1559,
  26. // - gas limit check
  27. // - basefee check
  28. func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Header) error {
  29. // Verify that the gas limit remains within allowed bounds
  30. parentGasLimit := parent.GasLimit
  31. if !config.IsLondon(parent.Number) {
  32. parentGasLimit = parent.GasLimit * params.ElasticityMultiplier
  33. }
  34. if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
  35. return err
  36. }
  37. // Verify the header is not malformed
  38. if header.BaseFee == nil {
  39. return fmt.Errorf("header is missing baseFee")
  40. }
  41. // Verify the baseFee is correct based on the parent header.
  42. expectedBaseFee := CalcBaseFee(config, parent)
  43. if header.BaseFee.Cmp(expectedBaseFee) != 0 {
  44. return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
  45. header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
  46. }
  47. return nil
  48. }
  49. // CalcBaseFee calculates the basefee of the header.
  50. func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
  51. // If the current block is the first EIP-1559 block, return the InitialBaseFee.
  52. if !config.IsLondon(parent.Number) {
  53. return new(big.Int).SetUint64(params.InitialBaseFee)
  54. }
  55. parentGasTarget := parent.GasLimit / params.ElasticityMultiplier
  56. // If the parent gasUsed is the same as the target, the baseFee remains unchanged.
  57. if parent.GasUsed == parentGasTarget {
  58. return new(big.Int).Set(parent.BaseFee)
  59. }
  60. var (
  61. num = new(big.Int)
  62. denom = new(big.Int)
  63. )
  64. if parent.GasUsed > parentGasTarget {
  65. // If the parent block used more gas than its target, the baseFee should increase.
  66. // max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
  67. num.SetUint64(parent.GasUsed - parentGasTarget)
  68. num.Mul(num, parent.BaseFee)
  69. num.Div(num, denom.SetUint64(parentGasTarget))
  70. num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator))
  71. baseFeeDelta := math.BigMax(num, common.Big1)
  72. return num.Add(parent.BaseFee, baseFeeDelta)
  73. } else {
  74. // Otherwise if the parent block used less gas than its target, the baseFee should decrease.
  75. // max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
  76. num.SetUint64(parentGasTarget - parent.GasUsed)
  77. num.Mul(num, parent.BaseFee)
  78. num.Div(num, denom.SetUint64(parentGasTarget))
  79. num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator))
  80. baseFee := num.Sub(parent.BaseFee, num)
  81. return math.BigMax(baseFee, common.Big0)
  82. }
  83. }