github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/consensus/misc/eip1559_test.go (about) 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 17 package misc 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/core/types" 24 "github.com/ethereum/go-ethereum/params" 25 ) 26 27 // copyConfig does a _shallow_ copy of a given config. Safe to set new values, but 28 // do not use e.g. SetInt() on the numbers. For testing only 29 func copyConfig(original *params.ChainConfig) *params.ChainConfig { 30 return ¶ms.ChainConfig{ 31 ChainID: original.ChainID, 32 HomesteadBlock: original.HomesteadBlock, 33 DAOForkBlock: original.DAOForkBlock, 34 DAOForkSupport: original.DAOForkSupport, 35 EIP150Block: original.EIP150Block, 36 EIP150Hash: original.EIP150Hash, 37 EIP155Block: original.EIP155Block, 38 EIP158Block: original.EIP158Block, 39 ByzantiumBlock: original.ByzantiumBlock, 40 ConstantinopleBlock: original.ConstantinopleBlock, 41 PetersburgBlock: original.PetersburgBlock, 42 IstanbulBlock: original.IstanbulBlock, 43 MuirGlacierBlock: original.MuirGlacierBlock, 44 BerlinBlock: original.BerlinBlock, 45 LondonBlock: original.LondonBlock, 46 TerminalTotalDifficulty: original.TerminalTotalDifficulty, 47 Ethash: original.Ethash, 48 Clique: original.Clique, 49 Bor: original.Bor, 50 } 51 } 52 53 func config() *params.ChainConfig { 54 config := copyConfig(params.TestChainConfig) 55 config.LondonBlock = big.NewInt(5) 56 config.Bor.DelhiBlock = big.NewInt(8) 57 return config 58 } 59 60 // TestBlockGasLimits tests the gasLimit checks for blocks both across 61 // the EIP-1559 boundary and post-1559 blocks 62 func TestBlockGasLimits(t *testing.T) { 63 initial := new(big.Int).SetUint64(params.InitialBaseFee) 64 65 for i, tc := range []struct { 66 pGasLimit uint64 67 pNum int64 68 gasLimit uint64 69 ok bool 70 }{ 71 // Transitions from non-london to london 72 {10000000, 4, 20000000, true}, // No change 73 {10000000, 4, 20019530, true}, // Upper limit 74 {10000000, 4, 20019531, false}, // Upper +1 75 {10000000, 4, 19980470, true}, // Lower limit 76 {10000000, 4, 19980469, false}, // Lower limit -1 77 // London to London 78 {20000000, 5, 20000000, true}, 79 {20000000, 5, 20019530, true}, // Upper limit 80 {20000000, 5, 20019531, false}, // Upper limit +1 81 {20000000, 5, 19980470, true}, // Lower limit 82 {20000000, 5, 19980469, false}, // Lower limit -1 83 {40000000, 5, 40039061, true}, // Upper limit 84 {40000000, 5, 40039062, false}, // Upper limit +1 85 {40000000, 5, 39960939, true}, // lower limit 86 {40000000, 5, 39960938, false}, // Lower limit -1 87 } { 88 parent := &types.Header{ 89 GasUsed: tc.pGasLimit / 2, 90 GasLimit: tc.pGasLimit, 91 BaseFee: initial, 92 Number: big.NewInt(tc.pNum), 93 } 94 header := &types.Header{ 95 GasUsed: tc.gasLimit / 2, 96 GasLimit: tc.gasLimit, 97 BaseFee: initial, 98 Number: big.NewInt(tc.pNum + 1), 99 } 100 err := VerifyEip1559Header(config(), parent, header) 101 if tc.ok && err != nil { 102 t.Errorf("test %d: Expected valid header: %s", i, err) 103 } 104 if !tc.ok && err == nil { 105 t.Errorf("test %d: Expected invalid header", i) 106 } 107 } 108 } 109 110 // TestCalcBaseFee assumes all blocks are 1559-blocks 111 func TestCalcBaseFee(t *testing.T) { 112 t.Parallel() 113 114 tests := []struct { 115 parentBaseFee int64 116 parentGasLimit uint64 117 parentGasUsed uint64 118 expectedBaseFee int64 119 }{ 120 {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target 121 {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target 122 {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target 123 {params.InitialBaseFee, 20000000, 20000000, 1125000000}, // usage full 124 {params.InitialBaseFee, 20000000, 0, 875000000}, // usage 0 125 } 126 for i, test := range tests { 127 parent := &types.Header{ 128 Number: big.NewInt(6), 129 GasLimit: test.parentGasLimit, 130 GasUsed: test.parentGasUsed, 131 BaseFee: big.NewInt(test.parentBaseFee), 132 } 133 if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 { 134 t.Errorf("test %d: have %d want %d, ", i, have, want) 135 } 136 } 137 } 138 139 // TestCalcBaseFee assumes all blocks are 1559-blocks post Delhi Hard Fork 140 func TestCalcBaseFeeDelhi(t *testing.T) { 141 t.Parallel() 142 143 testConfig := copyConfig(config()) 144 145 // Test Delhi Hard Fork 146 // Hard fork kicks in at block 8 147 148 tests := []struct { 149 parentBaseFee int64 150 parentGasLimit uint64 151 parentGasUsed uint64 152 expectedBaseFee int64 153 }{ 154 {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target 155 {params.InitialBaseFee, 20000000, 9000000, 993750000}, // usage below target 156 {params.InitialBaseFee, 20000000, 11000000, 1006250000}, // usage above target 157 {params.InitialBaseFee, 20000000, 20000000, 1062500000}, // usage full 158 {params.InitialBaseFee, 20000000, 0, 937500000}, // usage 0 159 160 } 161 for i, test := range tests { 162 parent := &types.Header{ 163 Number: big.NewInt(8), 164 GasLimit: test.parentGasLimit, 165 GasUsed: test.parentGasUsed, 166 BaseFee: big.NewInt(test.parentBaseFee), 167 } 168 if have, want := CalcBaseFee(testConfig, parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 { 169 t.Errorf("test %d: have %d want %d, ", i, have, want) 170 } 171 } 172 }