eip1559_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/params"
  23. )
  24. // copyConfig does a _shallow_ copy of a given config. Safe to set new values, but
  25. // do not use e.g. SetInt() on the numbers. For testing only
  26. func copyConfig(original *params.ChainConfig) *params.ChainConfig {
  27. return &params.ChainConfig{
  28. ChainID: original.ChainID,
  29. HomesteadBlock: original.HomesteadBlock,
  30. DAOForkBlock: original.DAOForkBlock,
  31. DAOForkSupport: original.DAOForkSupport,
  32. EIP150Block: original.EIP150Block,
  33. EIP150Hash: original.EIP150Hash,
  34. EIP155Block: original.EIP155Block,
  35. EIP158Block: original.EIP158Block,
  36. ByzantiumBlock: original.ByzantiumBlock,
  37. ConstantinopleBlock: original.ConstantinopleBlock,
  38. PetersburgBlock: original.PetersburgBlock,
  39. IstanbulBlock: original.IstanbulBlock,
  40. MuirGlacierBlock: original.MuirGlacierBlock,
  41. BerlinBlock: original.BerlinBlock,
  42. LondonBlock: original.LondonBlock,
  43. TerminalTotalDifficulty: original.TerminalTotalDifficulty,
  44. Ethash: original.Ethash,
  45. Clique: original.Clique,
  46. }
  47. }
  48. func config() *params.ChainConfig {
  49. config := copyConfig(params.TestChainConfig)
  50. config.LondonBlock = big.NewInt(5)
  51. return config
  52. }
  53. // TestBlockGasLimits tests the gasLimit checks for blocks both across
  54. // the EIP-1559 boundary and post-1559 blocks
  55. func TestBlockGasLimits(t *testing.T) {
  56. initial := new(big.Int).SetUint64(params.InitialBaseFee)
  57. for i, tc := range []struct {
  58. pGasLimit uint64
  59. pNum int64
  60. gasLimit uint64
  61. ok bool
  62. }{
  63. // Transitions from non-london to london
  64. {10000000, 4, 20000000, true}, // No change
  65. {10000000, 4, 20019530, true}, // Upper limit
  66. {10000000, 4, 20019531, false}, // Upper +1
  67. {10000000, 4, 19980470, true}, // Lower limit
  68. {10000000, 4, 19980469, false}, // Lower limit -1
  69. // London to London
  70. {20000000, 5, 20000000, true},
  71. {20000000, 5, 20019530, true}, // Upper limit
  72. {20000000, 5, 20019531, false}, // Upper limit +1
  73. {20000000, 5, 19980470, true}, // Lower limit
  74. {20000000, 5, 19980469, false}, // Lower limit -1
  75. {40000000, 5, 40039061, true}, // Upper limit
  76. {40000000, 5, 40039062, false}, // Upper limit +1
  77. {40000000, 5, 39960939, true}, // lower limit
  78. {40000000, 5, 39960938, false}, // Lower limit -1
  79. } {
  80. parent := &types.Header{
  81. GasUsed: tc.pGasLimit / 2,
  82. GasLimit: tc.pGasLimit,
  83. BaseFee: initial,
  84. Number: big.NewInt(tc.pNum),
  85. }
  86. header := &types.Header{
  87. GasUsed: tc.gasLimit / 2,
  88. GasLimit: tc.gasLimit,
  89. BaseFee: initial,
  90. Number: big.NewInt(tc.pNum + 1),
  91. }
  92. err := VerifyEip1559Header(config(), parent, header)
  93. if tc.ok && err != nil {
  94. t.Errorf("test %d: Expected valid header: %s", i, err)
  95. }
  96. if !tc.ok && err == nil {
  97. t.Errorf("test %d: Expected invalid header", i)
  98. }
  99. }
  100. }
  101. // TestCalcBaseFee assumes all blocks are 1559-blocks
  102. func TestCalcBaseFee(t *testing.T) {
  103. tests := []struct {
  104. parentBaseFee int64
  105. parentGasLimit uint64
  106. parentGasUsed uint64
  107. expectedBaseFee int64
  108. }{
  109. {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target
  110. {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target
  111. {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target
  112. }
  113. for i, test := range tests {
  114. parent := &types.Header{
  115. Number: common.Big32,
  116. GasLimit: test.parentGasLimit,
  117. GasUsed: test.parentGasUsed,
  118. BaseFee: big.NewInt(test.parentBaseFee),
  119. }
  120. if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
  121. t.Errorf("test %d: have %d want %d, ", i, have, want)
  122. }
  123. }
  124. }