|
| 1 | +// Copyright 2017 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 | +// TestCalcEIP1559GasTarget tests that CalEIP1559GasTarget()returns the correct value |
| 28 | +func TestCalcEIP1559GasTarget(t *testing.T) { |
| 29 | + testConditions := []struct { |
| 30 | + // Test inputs |
| 31 | + config *params.ChainConfig |
| 32 | + eip1559activation *big.Int |
| 33 | + transitionDuration uint64 |
| 34 | + height *big.Int |
| 35 | + gasLimit *big.Int |
| 36 | + // Expected result |
| 37 | + eip1559GasTarget *big.Int |
| 38 | + }{ |
| 39 | + { |
| 40 | + params.EIP1559ChainConfig, |
| 41 | + big.NewInt(1000), |
| 42 | + 1000, |
| 43 | + big.NewInt(1), |
| 44 | + big.NewInt(100000), |
| 45 | + big.NewInt(0), |
| 46 | + }, |
| 47 | + { |
| 48 | + params.EIP1559ChainConfig, |
| 49 | + big.NewInt(1000), |
| 50 | + 1000, |
| 51 | + big.NewInt(999), |
| 52 | + big.NewInt(100000), |
| 53 | + big.NewInt(0), |
| 54 | + }, |
| 55 | + { |
| 56 | + params.EIP1559ChainConfig, |
| 57 | + big.NewInt(1000), |
| 58 | + 1000, |
| 59 | + big.NewInt(1000), |
| 60 | + big.NewInt(100000), |
| 61 | + big.NewInt(50000), |
| 62 | + }, |
| 63 | + { |
| 64 | + params.EIP1559ChainConfig, |
| 65 | + big.NewInt(1000), |
| 66 | + 1000, |
| 67 | + big.NewInt(1001), |
| 68 | + big.NewInt(100000), |
| 69 | + big.NewInt(50050), |
| 70 | + }, |
| 71 | + { |
| 72 | + params.EIP1559ChainConfig, |
| 73 | + big.NewInt(1000), |
| 74 | + 1000, |
| 75 | + big.NewInt(1500), |
| 76 | + big.NewInt(100000), |
| 77 | + big.NewInt(75000), |
| 78 | + }, |
| 79 | + { |
| 80 | + params.EIP1559ChainConfig, |
| 81 | + big.NewInt(1000), |
| 82 | + 1000, |
| 83 | + big.NewInt(1999), |
| 84 | + big.NewInt(100000), |
| 85 | + big.NewInt(99950), |
| 86 | + }, |
| 87 | + { |
| 88 | + params.EIP1559ChainConfig, |
| 89 | + big.NewInt(1000), |
| 90 | + 1000, |
| 91 | + big.NewInt(2000), |
| 92 | + big.NewInt(100000), |
| 93 | + big.NewInt(100000), |
| 94 | + }, |
| 95 | + { |
| 96 | + params.EIP1559ChainConfig, |
| 97 | + big.NewInt(1000), |
| 98 | + 1000, |
| 99 | + big.NewInt(2001), |
| 100 | + big.NewInt(100000), |
| 101 | + big.NewInt(100000), |
| 102 | + }, |
| 103 | + } |
| 104 | + for i, test := range testConditions { |
| 105 | + config := *test.config |
| 106 | + config.EIP1559Block = test.eip1559activation |
| 107 | + config.EIP1559.MigrationBlockDuration = test.transitionDuration |
| 108 | + config.EIP1559FinalizedBlock = new(big.Int).Add(config.EIP1559Block, new(big.Int).SetUint64(config.EIP1559.MigrationBlockDuration)) |
| 109 | + gasTarget := CalcEIP1559GasTarget(&config, test.height, test.gasLimit) |
| 110 | + if gasTarget.Cmp(test.eip1559GasTarget) != 0 { |
| 111 | + t.Errorf("test %d expected EIP1559GasTarget %d got %d", i+1, test.eip1559GasTarget.Uint64(), gasTarget.Uint64()) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestCalcBaseFee tests that CalcBaseFee()returns the correct value |
| 117 | +func TestCalcBaseFee(t *testing.T) { |
| 118 | + testConditions := []struct { |
| 119 | + // Test inputs |
| 120 | + config *params.ChainConfig |
| 121 | + eip1559activation *big.Int |
| 122 | + transitionDuration uint64 |
| 123 | + parentHeight *big.Int |
| 124 | + parentBaseFee *big.Int |
| 125 | + parentGasLimit uint64 |
| 126 | + parentGasUsed uint64 |
| 127 | + // Expected result |
| 128 | + baseFee *big.Int |
| 129 | + }{ |
| 130 | + { |
| 131 | + params.EIP1559ChainConfig, |
| 132 | + big.NewInt(1000), |
| 133 | + 1000, |
| 134 | + big.NewInt(1), |
| 135 | + big.NewInt(1000000000), |
| 136 | + 1000000, |
| 137 | + 10000000, |
| 138 | + nil, |
| 139 | + }, |
| 140 | + { |
| 141 | + params.EIP1559ChainConfig, |
| 142 | + big.NewInt(1000), |
| 143 | + 1000, |
| 144 | + big.NewInt(999), |
| 145 | + big.NewInt(1000000000), |
| 146 | + 1000000, |
| 147 | + 10000000, |
| 148 | + new(big.Int).SetUint64(params.EIP1559ChainConfig.EIP1559.InitialBaseFee), |
| 149 | + }, |
| 150 | + { |
| 151 | + params.EIP1559ChainConfig, |
| 152 | + big.NewInt(1000), |
| 153 | + 1000, |
| 154 | + big.NewInt(1000), |
| 155 | + big.NewInt(1000000000), |
| 156 | + 1000000, |
| 157 | + 10000000, |
| 158 | + big.NewInt(1125000000), |
| 159 | + }, |
| 160 | + { |
| 161 | + params.EIP1559ChainConfig, |
| 162 | + big.NewInt(1000), |
| 163 | + 1000, |
| 164 | + big.NewInt(2000), // past finalization parentGasLimit is the EIP1559GasTarget |
| 165 | + big.NewInt(1000000000), |
| 166 | + 500000, |
| 167 | + 10000000, |
| 168 | + big.NewInt(1125000000), |
| 169 | + }, |
| 170 | + { |
| 171 | + params.EIP1559ChainConfig, |
| 172 | + big.NewInt(1000), |
| 173 | + 1000, |
| 174 | + big.NewInt(2000), |
| 175 | + big.NewInt(1000000000), |
| 176 | + 1000000, |
| 177 | + 10000000, |
| 178 | + big.NewInt(1125000000), |
| 179 | + }, |
| 180 | + { |
| 181 | + params.EIP1559ChainConfig, |
| 182 | + big.NewInt(1000), |
| 183 | + 1000, |
| 184 | + big.NewInt(2000), |
| 185 | + big.NewInt(1000000000), |
| 186 | + 6000000, |
| 187 | + 10000000, |
| 188 | + big.NewInt(1083333333), |
| 189 | + }, |
| 190 | + { |
| 191 | + params.EIP1559ChainConfig, |
| 192 | + big.NewInt(1000), |
| 193 | + 1000, |
| 194 | + big.NewInt(2000), |
| 195 | + big.NewInt(1000000000), |
| 196 | + 7000000, |
| 197 | + 10000000, |
| 198 | + big.NewInt(1053571428), |
| 199 | + }, |
| 200 | + { |
| 201 | + params.EIP1559ChainConfig, |
| 202 | + big.NewInt(1000), |
| 203 | + 1000, |
| 204 | + big.NewInt(2000), |
| 205 | + big.NewInt(1000000000), |
| 206 | + 8000000, |
| 207 | + 10000000, |
| 208 | + big.NewInt(1031250000), |
| 209 | + }, |
| 210 | + { |
| 211 | + params.EIP1559ChainConfig, |
| 212 | + big.NewInt(1000), |
| 213 | + 1000, |
| 214 | + big.NewInt(2000), |
| 215 | + big.NewInt(1000000000), |
| 216 | + 9000000, |
| 217 | + 10000000, |
| 218 | + big.NewInt(1013888888), |
| 219 | + }, |
| 220 | + { |
| 221 | + params.EIP1559ChainConfig, |
| 222 | + big.NewInt(1000), |
| 223 | + 1000, |
| 224 | + big.NewInt(2000), |
| 225 | + big.NewInt(1000000000), |
| 226 | + 10000000, |
| 227 | + 10000000, |
| 228 | + big.NewInt(1000000000), |
| 229 | + }, |
| 230 | + { |
| 231 | + params.EIP1559ChainConfig, |
| 232 | + big.NewInt(1000), |
| 233 | + 1000, |
| 234 | + big.NewInt(2000), |
| 235 | + big.NewInt(1000000000), |
| 236 | + 11000000, |
| 237 | + 10000000, |
| 238 | + big.NewInt(988636363), |
| 239 | + }, |
| 240 | + { |
| 241 | + params.EIP1559ChainConfig, |
| 242 | + big.NewInt(1000), |
| 243 | + 1000, |
| 244 | + big.NewInt(2000), |
| 245 | + big.NewInt(900000000), |
| 246 | + 1000000, |
| 247 | + 10000000, |
| 248 | + big.NewInt(1012500000), |
| 249 | + }, |
| 250 | + { |
| 251 | + params.EIP1559ChainConfig, |
| 252 | + big.NewInt(1000), |
| 253 | + 1000, |
| 254 | + big.NewInt(2000), |
| 255 | + big.NewInt(1100000000), |
| 256 | + 1000000, |
| 257 | + 10000000, |
| 258 | + big.NewInt(1237500000), |
| 259 | + }, |
| 260 | + { |
| 261 | + params.EIP1559ChainConfig, |
| 262 | + big.NewInt(1000), |
| 263 | + 1000, |
| 264 | + big.NewInt(2000), |
| 265 | + big.NewInt(1200000000), |
| 266 | + 1000000, |
| 267 | + 10000000, |
| 268 | + big.NewInt(1350000000), |
| 269 | + }, |
| 270 | + { |
| 271 | + params.EIP1559ChainConfig, |
| 272 | + big.NewInt(1000), |
| 273 | + 1000, |
| 274 | + big.NewInt(2000), |
| 275 | + big.NewInt(1000000000), |
| 276 | + 10000000, |
| 277 | + 9000000, |
| 278 | + big.NewInt(987500000), |
| 279 | + }, |
| 280 | + { |
| 281 | + params.EIP1559ChainConfig, |
| 282 | + big.NewInt(1000), |
| 283 | + 1000, |
| 284 | + big.NewInt(2000), |
| 285 | + big.NewInt(1000000000), |
| 286 | + 10000000, |
| 287 | + 11000000, |
| 288 | + big.NewInt(1012500000), |
| 289 | + }, |
| 290 | + { |
| 291 | + params.EIP1559ChainConfig, |
| 292 | + big.NewInt(1000), |
| 293 | + 1000, |
| 294 | + big.NewInt(2000), |
| 295 | + big.NewInt(1000000000), |
| 296 | + 10000000, |
| 297 | + 12000000, |
| 298 | + big.NewInt(1025000000), |
| 299 | + }, |
| 300 | + } |
| 301 | + for i, test := range testConditions { |
| 302 | + config := *test.config |
| 303 | + config.EIP1559Block = test.eip1559activation |
| 304 | + config.EIP1559.MigrationBlockDuration = test.transitionDuration |
| 305 | + config.EIP1559FinalizedBlock = new(big.Int).Add(config.EIP1559Block, new(big.Int).SetUint64(config.EIP1559.MigrationBlockDuration)) |
| 306 | + parent := &types.Header{ |
| 307 | + GasLimit: test.parentGasLimit, |
| 308 | + GasUsed: test.parentGasUsed, |
| 309 | + Number: test.parentHeight, |
| 310 | + BaseFee: test.parentBaseFee, |
| 311 | + } |
| 312 | + gasTarget := CalcBaseFee(&config, parent) |
| 313 | + if gasTarget != nil { |
| 314 | + if test.baseFee != nil && gasTarget.Cmp(test.baseFee) != 0 { |
| 315 | + t.Errorf("test %d expected BaseFee %d got %d", i+1, test.baseFee.Uint64(), gasTarget.Uint64()) |
| 316 | + } |
| 317 | + if test.baseFee == nil { |
| 318 | + t.Errorf("test %d expected nil BaseFee got %d", i+1, gasTarget.Uint64()) |
| 319 | + } |
| 320 | + } else if test.baseFee != nil { |
| 321 | + t.Errorf("test %d expected BaseFee %d got nil", i+1, test.baseFee.Uint64()) |
| 322 | + } |
| 323 | + } |
| 324 | +} |
0 commit comments