diff --git a/consensus/misc/eip7783/eip7783.go b/consensus/misc/eip7783/eip7783.go new file mode 100644 index 0000000000..47ecd8aa98 --- /dev/null +++ b/consensus/misc/eip7783/eip7783.go @@ -0,0 +1,21 @@ +package eip7783 + +import "math/big" + +/* +Implementation of EIP-7783: + +def compute_gas_limit(blockNum: int, blockNumStart: int, initialGasLimit: int, r: int, gasLimitCap: int) -> int: + + if blockNum < blockNumStart: + return initialGasLimit + else: + return min(gasLimitCap, initialGasLimit + r * (blockNum - blockNumStart)) +*/ +func CalcGasLimitEIP7783(blockNum, startBlockNum *big.Int, initialGasLimit, gasIncreaseRate, gasLimitCap uint64) uint64 { + if blockNum.Cmp(startBlockNum) < 0 { + return initialGasLimit + } else { + return min(gasLimitCap, initialGasLimit+gasIncreaseRate*(blockNum.Uint64()-startBlockNum.Uint64())) + } +} diff --git a/consensus/misc/eip7783/eip7783_test.go b/consensus/misc/eip7783/eip7783_test.go new file mode 100644 index 0000000000..0fb0a596b4 --- /dev/null +++ b/consensus/misc/eip7783/eip7783_test.go @@ -0,0 +1,24 @@ +package eip7783 + +import ( + "math/big" + "testing" +) + +func TestCalcGasLimitEIP7783Test(t *testing.T) { + // Do multiple tests here + tests := []struct { + blockNum, startBlockNum *big.Int + initialGasLimit, gasIncreaseRate, gasLimitCap, expectedGasLimit uint64 + }{ + {big.NewInt(100), big.NewInt(50), 100000, 10, 200000, 100500}, + {big.NewInt(100), big.NewInt(100), 100000, 10, 200000, 100000}, + {big.NewInt(99), big.NewInt(100), 100000, 10, 200000, 100000}, + } + + for i, test := range tests { + if have, want := CalcGasLimitEIP7783(test.blockNum, test.startBlockNum, test.initialGasLimit, test.gasIncreaseRate, test.gasLimitCap), test.expectedGasLimit; have != want { + t.Errorf("test %d: have %d want %d, ", i, have, want) + } + } +}