diff --git a/consensus/misc/eip1559/eip1559.go b/consensus/misc/eip1559/eip1559.go index f94b0a2ad2..a3f0823cc3 100644 --- a/consensus/misc/eip1559/eip1559.go +++ b/consensus/misc/eip1559/eip1559.go @@ -70,7 +70,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { var ( num = new(big.Int) denom = new(big.Int) - baseFeeChangeDenominatorUint64 = params.BaseFeeChangeDenominator(config.Bor, parent.Number) + baseFeeChangeDenominatorUint64 = baseFeeChangeDenominator(config, parent.Number) ) if parent.GasUsed > parentGasTarget { @@ -100,6 +100,25 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { } } +// baseFeeChangeDenominator returns the denominator used to bound the amount the base fee can change between blocks. +// The value varies based on the hard fork: +// - Pre-Delhi: 8 (default) +// - Post-Delhi: 16 +// - Post-Bhilai: 64 +// If borConfig is nil, returns the default value of 8. +func baseFeeChangeDenominator(config *params.ChainConfig, number *big.Int) uint64 { + if config.Bor == nil { + return params.DefaultBaseFeeChangeDenominator + } + if config.Bor.IsBhilai(number) { + return params.BaseFeeChangeDenominatorPostBhilai + } else if config.Bor.IsDelhi(number) { + return params.BaseFeeChangeDenominatorPostDelhi + } else { + return params.DefaultBaseFeeChangeDenominator + } +} + // calcParentGasTarget calculates the target gas based on parent block gas limit. Earlier // it was derived by `ElasticityMultiplier` as it had an integer multiplier value. Post // dandeli HF, a percentage value will be used to calculate the gas target. diff --git a/consensus/misc/eip1559/eip1559_test.go b/consensus/misc/eip1559/eip1559_test.go index c63a9c9292..eab54623bd 100644 --- a/consensus/misc/eip1559/eip1559_test.go +++ b/consensus/misc/eip1559/eip1559_test.go @@ -165,15 +165,14 @@ func TestCalcBaseFee(t *testing.T) { } } -// TestCalcBaseFeeDelhi assumes all blocks are 1559-blocks post Delhi Hard Fork +// TestCalcBaseFeeDelhi assumes all blocks are 1559-blocks and uses +// parameters post Delhi Hard Fork func TestCalcBaseFeeDelhi(t *testing.T) { t.Parallel() + // Delhi HF kicks in at block 8 testConfig := copyConfig(config()) - // Test Delhi Hard Fork - // Hard fork kicks in at block 8 - tests := []struct { parentBaseFee int64 parentGasLimit uint64 @@ -200,6 +199,41 @@ func TestCalcBaseFeeDelhi(t *testing.T) { } } +// TestCalcBaseFeeBhilai assumes all blocks are 1559-blocks and uses +// parameters post Bhilai Hard Fork +func TestCalcBaseFeeBhilai(t *testing.T) { + t.Parallel() + + // Bhilai HF kicks in at block 8 + testConfig := copyConfig(config()) + testConfig.Bor.BhilaiBlock = big.NewInt(8) + + tests := []struct { + parentBaseFee int64 + parentGasLimit uint64 + parentGasUsed uint64 + expectedBaseFee int64 + }{ + {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target + {params.InitialBaseFee, 20000000, 9000000, 998437500}, // usage below target + {params.InitialBaseFee, 20000000, 11000000, 1001562500}, // usage above target + {params.InitialBaseFee, 20000000, 20000000, 1015625000}, // usage full + {params.InitialBaseFee, 20000000, 0, 984375000}, // usage 0 + + } + for i, test := range tests { + parent := &types.Header{ + Number: big.NewInt(8), + GasLimit: test.parentGasLimit, + GasUsed: test.parentGasUsed, + BaseFee: big.NewInt(test.parentBaseFee), + } + if have, want := CalcBaseFee(testConfig, parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 { + t.Errorf("test %d: have %d want %d, ", i, have, want) + } + } +} + func TestCalcParentGasTarget(t *testing.T) { t.Parallel() diff --git a/params/protocol_params.go b/params/protocol_params.go index f2d4091ae1..f70a1215a3 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -130,15 +130,14 @@ const ( // Introduced in Tangerine Whistle (Eip 150) CreateBySelfdestructGas uint64 = 25000 - BaseFeeChangeDenominatorPreDelhi = 8 // Bounds the amount the base fee can change between blocks before Delhi Hard Fork. + DefaultBaseFeeChangeDenominator = 8 // Bounds the amount the base fee can change between blocks. BaseFeeChangeDenominatorPostDelhi = 16 // Bounds the amount the base fee can change between blocks after Delhi Hard Fork. BaseFeeChangeDenominatorPostBhilai = 64 // Bounds the amount the base fee can change between blocks after Bhilai Hard Fork. - ElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. - InitialBaseFee = 1000000000 // Initial base fee for EIP-1559 blocks. + DefaultElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. + ElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. - DefaultBaseFeeChangeDenominator = 8 // Bounds the amount the base fee can change between blocks. - DefaultElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. + InitialBaseFee = 1000000000 // Initial base fee for EIP-1559 blocks. DefaultTargetGasPercentage = 50 // Specifies target block gas as percentage of block gas limit for EIP-1559 TargetGasPercentagePostDandeli = 65 // Specifies target block gas as percentage of block gas limit for EIP-1559 after Dandeli hard fork @@ -235,23 +234,3 @@ var ( ConsolidationQueueAddress = common.HexToAddress("0x0000BBdDc7CE488642fb579F8B00f3a590007251") ConsolidationQueueCode = common.FromHex("3373fffffffffffffffffffffffffffffffffffffffe1460d35760115f54807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1461019a57600182026001905f5b5f82111560685781019083028483029004916001019190604d565b9093900492505050366060146088573661019a573461019a575f5260205ff35b341061019a57600154600101600155600354806004026004013381556001015f358155600101602035815560010160403590553360601b5f5260605f60143760745fa0600101600355005b6003546002548082038060021160e7575060025b5f5b8181146101295782810160040260040181607402815460601b815260140181600101548152602001816002015481526020019060030154905260010160e9565b910180921461013b5790600255610146565b90505f6002555f6003555b5f54807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff141561017357505f5b6001546001828201116101885750505f61018e565b01600190035b5f555f6001556074025ff35b5f5ffd") ) - -// BaseFeeChangeDenominator returns the denominator used to bound the amount the base fee can change between blocks. -// The value varies based on the hard fork: -// - Pre-Delhi: 8 (default) -// - Post-Delhi: 16 -// - Post-Bhilai: 64 -// If borConfig is nil, returns the default value of 8. -func BaseFeeChangeDenominator(borConfig *BorConfig, number *big.Int) uint64 { - // Handle cases where bor consensus isn't available to avoid panic - if borConfig == nil { - return DefaultBaseFeeChangeDenominator - } - if borConfig.IsBhilai(number) { - return BaseFeeChangeDenominatorPostBhilai - } else if borConfig.IsDelhi(number) { - return BaseFeeChangeDenominatorPostDelhi - } else { - return BaseFeeChangeDenominatorPreDelhi - } -}