1+ // Copyright 2024 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 eip1559
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+ func TestBluebirdEIP1559Parameters (t * testing.T ) {
28+ // Create a config with Bluebird enabled at timestamp 1000
29+ bluebirdTime := uint64 (1000 )
30+ config := & params.ChainConfig {
31+ ChainID : big .NewInt (1 ),
32+ HomesteadBlock : big .NewInt (0 ),
33+ EIP150Block : big .NewInt (0 ),
34+ EIP155Block : big .NewInt (0 ),
35+ EIP158Block : big .NewInt (0 ),
36+ ByzantiumBlock : big .NewInt (0 ),
37+ ConstantinopleBlock : big .NewInt (0 ),
38+ PetersburgBlock : big .NewInt (0 ),
39+ IstanbulBlock : big .NewInt (0 ),
40+ MuirGlacierBlock : big .NewInt (0 ),
41+ BerlinBlock : big .NewInt (0 ),
42+ LondonBlock : big .NewInt (0 ),
43+ BluebirdTime : & bluebirdTime ,
44+ }
45+
46+ // Test ElasticityMultiplier
47+ t .Run ("ElasticityMultiplier" , func (t * testing.T ) {
48+ // Before Bluebird
49+ beforeTime := uint64 (999 )
50+ elasticity := config .ElasticityMultiplier (beforeTime )
51+ if elasticity != params .DefaultElasticityMultiplier {
52+ t .Errorf ("Before Bluebird: expected elasticity %d, got %d" , params .DefaultElasticityMultiplier , elasticity )
53+ }
54+
55+ // At Bluebird activation
56+ atTime := uint64 (1000 )
57+ elasticity = config .ElasticityMultiplier (atTime )
58+ if elasticity != params .BluebirdElasticityMultiplier {
59+ t .Errorf ("At Bluebird: expected elasticity %d, got %d" , params .BluebirdElasticityMultiplier , elasticity )
60+ }
61+
62+ // After Bluebird
63+ afterTime := uint64 (1001 )
64+ elasticity = config .ElasticityMultiplier (afterTime )
65+ if elasticity != params .BluebirdElasticityMultiplier {
66+ t .Errorf ("After Bluebird: expected elasticity %d, got %d" , params .BluebirdElasticityMultiplier , elasticity )
67+ }
68+ })
69+
70+ // Test BaseFeeChangeDenominator
71+ t .Run ("BaseFeeChangeDenominator" , func (t * testing.T ) {
72+ // Before Bluebird
73+ beforeTime := uint64 (999 )
74+ denominator := config .BaseFeeChangeDenominator (beforeTime )
75+ if denominator != params .DefaultBaseFeeChangeDenominator {
76+ t .Errorf ("Before Bluebird: expected denominator %d, got %d" , params .DefaultBaseFeeChangeDenominator , denominator )
77+ }
78+
79+ // At Bluebird activation
80+ atTime := uint64 (1000 )
81+ denominator = config .BaseFeeChangeDenominator (atTime )
82+ if denominator != params .BluebirdBaseFeeChangeDenominator {
83+ t .Errorf ("At Bluebird: expected denominator %d, got %d" , params .BluebirdBaseFeeChangeDenominator , denominator )
84+ }
85+
86+ // After Bluebird
87+ afterTime := uint64 (1001 )
88+ denominator = config .BaseFeeChangeDenominator (afterTime )
89+ if denominator != params .BluebirdBaseFeeChangeDenominator {
90+ t .Errorf ("After Bluebird: expected denominator %d, got %d" , params .BluebirdBaseFeeChangeDenominator , denominator )
91+ }
92+ })
93+
94+ // Test minimum base fee enforcement
95+ t .Run ("MinimumBaseFee" , func (t * testing.T ) {
96+ // Create a parent header with very low base fee
97+ parent := & types.Header {
98+ Number : big .NewInt (1 ),
99+ GasLimit : 30_000_000 ,
100+ GasUsed : 0 , // No gas used, should decrease base fee
101+ BaseFee : big .NewInt (1000 ), // Very low base fee
102+ }
103+
104+ // Before Bluebird - base fee can go to near zero
105+ beforeTime := uint64 (999 )
106+ baseFee := CalcBaseFee (config , parent , beforeTime )
107+ if baseFee .Cmp (big .NewInt (0 )) < 0 {
108+ t .Errorf ("Base fee should never be negative, got %s" , baseFee )
109+ }
110+
111+ // After Bluebird - base fee should respect minimum
112+ afterTime := uint64 (1001 )
113+ baseFee = CalcBaseFee (config , parent , afterTime )
114+ minBaseFee := new (big.Int ).SetUint64 (params .BluebirdMinBaseFee )
115+ if baseFee .Cmp (minBaseFee ) < 0 {
116+ t .Errorf ("After Bluebird: base fee %s is below minimum %s" , baseFee , minBaseFee )
117+ }
118+ })
119+
120+ // Test base fee calculation with new parameters
121+ t .Run ("BaseFeeCalculationWithNewParams" , func (t * testing.T ) {
122+ // Test with block at exactly the target gas usage
123+ parent := & types.Header {
124+ Number : big .NewInt (1 ),
125+ GasLimit : 30_000_000 ,
126+ GasUsed : 15_000_000 , // At target for default elasticity of 2
127+ BaseFee : big .NewInt (1_000_000_000 ), // 1 gwei
128+ }
129+
130+ // Before Bluebird: gas used = target, so base fee unchanged
131+ beforeTime := uint64 (999 )
132+ baseFeeBeforeBluebird := CalcBaseFee (config , parent , beforeTime )
133+ if baseFeeBeforeBluebird .Cmp (parent .BaseFee ) != 0 {
134+ t .Errorf ("At target gas usage, base fee should remain unchanged" )
135+ }
136+
137+ // After Bluebird: gas used > new target (7.5M), so base fee increases
138+ afterTime := uint64 (1001 )
139+ baseFeeAfterBluebird := CalcBaseFee (config , parent , afterTime )
140+
141+ t .Logf ("Base fee before Bluebird: %s" , baseFeeBeforeBluebird )
142+ t .Logf ("Base fee after Bluebird: %s" , baseFeeAfterBluebird )
143+ t .Logf ("Gas target before Bluebird: %d" , parent .GasLimit / params .DefaultElasticityMultiplier )
144+ t .Logf ("Gas target after Bluebird: %d" , parent .GasLimit / params .BluebirdElasticityMultiplier )
145+
146+ // After Bluebird, with higher elasticity, the same gas usage is now above target
147+ if baseFeeAfterBluebird .Cmp (parent .BaseFee ) <= 0 {
148+ t .Errorf ("After Bluebird, base fee should increase as gas usage is now above new target" )
149+ }
150+ })
151+
152+ // Test that higher denominator makes changes more gradual
153+ t .Run ("DenominatorMakesChangesGradual" , func (t * testing.T ) {
154+ parent := & types.Header {
155+ Number : big .NewInt (1 ),
156+ GasLimit : 30_000_000 ,
157+ GasUsed : 30_000_000 , // Full block
158+ BaseFee : big .NewInt (1_000_000_000 ), // 1 gwei
159+ }
160+
161+ // Calculate base fee increase percentage before and after Bluebird
162+ beforeTime := uint64 (999 )
163+ baseFeeBeforeBluebird := CalcBaseFee (config , parent , beforeTime )
164+ percentIncreaseBeforeBluebird := new (big.Int ).Sub (baseFeeBeforeBluebird , parent .BaseFee )
165+ percentIncreaseBeforeBluebird .Mul (percentIncreaseBeforeBluebird , big .NewInt (100 ))
166+ percentIncreaseBeforeBluebird .Div (percentIncreaseBeforeBluebird , parent .BaseFee )
167+
168+ afterTime := uint64 (1001 )
169+ baseFeeAfterBluebird := CalcBaseFee (config , parent , afterTime )
170+ percentIncreaseAfterBluebird := new (big.Int ).Sub (baseFeeAfterBluebird , parent .BaseFee )
171+ percentIncreaseAfterBluebird .Mul (percentIncreaseAfterBluebird , big .NewInt (100 ))
172+ percentIncreaseAfterBluebird .Div (percentIncreaseAfterBluebird , parent .BaseFee )
173+
174+ t .Logf ("Percent increase before Bluebird: %s%%" , percentIncreaseBeforeBluebird )
175+ t .Logf ("Percent increase after Bluebird: %s%%" , percentIncreaseAfterBluebird )
176+
177+ // Both elasticity and denominator changes affect the calculation
178+ // The test should verify the parameters are applied correctly
179+ if config .ElasticityMultiplier (afterTime ) != params .BluebirdElasticityMultiplier {
180+ t .Errorf ("Elasticity multiplier not correctly applied" )
181+ }
182+ if config .BaseFeeChangeDenominator (afterTime ) != params .BluebirdBaseFeeChangeDenominator {
183+ t .Errorf ("Base fee denominator not correctly applied" )
184+ }
185+ })
186+ }
0 commit comments