Skip to content

Commit 1fbb6ef

Browse files
committed
unit tests for new basefee and gas target methods
1 parent 765f39a commit 1fbb6ef

File tree

14 files changed

+372
-51
lines changed

14 files changed

+372
-51
lines changed

cmd/geth/retesteth.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,8 @@ func (api *RetestethAPI) mineBlock() error {
502502
}
503503

504504
var gp1559 *core.GasPool
505-
var gasPool *core.GasPool
506-
// See core/gaspool.go for detials on how these gas limit values are calculated
507-
gasPool = core.NewLegacyGasPool(api.chainConfig, header.Number, new(big.Int).SetUint64(header.GasLimit))
505+
// See core/gaspool.go for details on how these gas limit values are calculated
506+
gasPool := core.NewLegacyGasPool(api.chainConfig, header.Number, new(big.Int).SetUint64(header.GasLimit))
508507
if api.chainConfig.IsEIP1559(header.Number) {
509508
gp1559 = core.NewEIP1559GasPool(api.chainConfig, header.Number, new(big.Int).SetUint64(header.GasLimit))
510509
}

consensus/misc/basefee.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
7272
}
7373

7474
// Otherwise,
75-
// BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // EIP1559_GAS_TARGET // BASEFEE_MAX_CHANGE_DENOMINATOR
76-
// Where delta = block.gas_used - EIP1559_GAS_TARGET
75+
// BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // PARENT_EIP1559_GAS_TARGET // BASEFEE_MAX_CHANGE_DENOMINATOR
76+
// Where delta = parent.GasUsed - PARENT_EIP1559_GAS_TARGET
7777
parentGasTarget := CalcEIP1559GasTarget(config, parent.Number, new(big.Int).SetUint64(parent.GasLimit))
7878
delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed), parentGasTarget)
7979
mul := new(big.Int).Mul(parent.BaseFee, delta)

consensus/misc/basefee_test.go

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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+
}

core/chain_makers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (b *BlockGen) SetCoinbase(addr common.Address) {
5959
panic("coinbase can only be set once")
6060
}
6161
b.header.Coinbase = addr
62-
// See core/gaspool.go for detials on how these gas limit values are calculated
62+
// See core/gaspool.go for details on how these gas limit values are calculated
6363
b.gasPool = NewLegacyGasPool(b.config, b.header.Number, new(big.Int).SetUint64(b.header.GasLimit))
6464
if b.config.IsEIP1559(b.header.Number) {
6565
b.gasPool1559 = NewEIP1559GasPool(b.config, b.header.Number, new(big.Int).SetUint64(b.header.GasLimit))

core/chain_makers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ func generateChainDuringTransition(t *testing.T) {
245245
if state.GetBalance(addr1).Uint64() != 989000 {
246246
t.Fatalf("expected balance of addr1 to equal %d got %d", 989000, state.GetBalance(addr1).Uint64())
247247
}
248-
if state.GetBalance(addr2).Uint64() != 4917051584000 {
249-
t.Fatalf("expected balance of addr2 to equal %d got %d", 10000, state.GetBalance(addr2).Uint64())
248+
if state.GetBalance(addr2).Uint64() != 4901403728000 {
249+
t.Fatalf("expected balance of addr2 to equal %d got %d", 4901403728000, state.GetBalance(addr2).Uint64())
250250
}
251251
// This value is different because the test config we use has Constantinople active (uses ConstantinopleBlockReward)
252252
bal, _ := new(big.Int).SetString("7875000000000001000", 10)
@@ -389,11 +389,11 @@ func generateChainAfterFinalization2(t *testing.T) {
389389
if blockchain.CurrentBlock().Number().Uint64() != 5 {
390390
t.Fatalf("expected last block to equal %d got %d", 5, blockchain.CurrentBlock().Number().Uint64())
391391
}
392-
if state.GetBalance(addr1).Uint64() != 7542051573000 {
393-
t.Fatalf("expected balance of addr1 to equal %d got %d", 7542051573000, state.GetBalance(addr1).Uint64())
392+
if state.GetBalance(addr1).Uint64() != 7536639348000 {
393+
t.Fatalf("expected balance of addr1 to equal %d got %d", 7536639348000, state.GetBalance(addr1).Uint64())
394394
}
395-
if state.GetBalance(addr2).Uint64() != 4917051584000 {
396-
t.Fatalf("expected balance of addr2 to equal %d got %d", 4917051584000, state.GetBalance(addr2).Uint64())
395+
if state.GetBalance(addr2).Uint64() != 4911639359000 {
396+
t.Fatalf("expected balance of addr2 to equal %d got %d", 4911639359000, state.GetBalance(addr2).Uint64())
397397
}
398398
// This value is different than in TestGenerateChain because the test config we use has Constantinople active (uses ConstantinopleBlockReward)
399399
bal, _ := new(big.Int).SetString("7875000000000001000", 10)

core/state_prefetcher.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine conse
5151
// only goal is to pre-cache transaction signatures and state trie nodes.
5252
func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *uint32) {
5353
var (
54-
header = block.Header()
55-
gp *GasPool
56-
gp1559 *GasPool
54+
header = block.Header()
55+
gp *GasPool
56+
gp1559 *GasPool
5757
)
58-
// See core/gaspool.go for detials on how these gas limit values are calculated
58+
// See core/gaspool.go for details on how these gas limit values are calculated
5959
gp = NewLegacyGasPool(p.config, block.Number(), new(big.Int).SetUint64(block.GasLimit()))
6060
if p.config.IsEIP1559(block.Number()) {
6161
gp1559 = NewEIP1559GasPool(p.config, block.Number(), new(big.Int).SetUint64(block.GasLimit()))

0 commit comments

Comments
 (0)