Skip to content

Commit 0757426

Browse files
committed
negative guard; update unit tests
1 parent af1c710 commit 0757426

File tree

2 files changed

+56
-54
lines changed

2 files changed

+56
-54
lines changed

consensus/misc/basefee.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,38 @@ func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Head
5959
}
6060

6161
// CalcBaseFee returns the baseFee for the current block provided the parent header and config parameters
62-
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
62+
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) (baseFee *big.Int) {
6363
height := new(big.Int).Add(parent.Number, common.Big1)
6464
// If we are before EIP1559 activation, the baseFee is nil
6565
if !config.IsEIP1559(height) {
66-
return nil
66+
return
6767
}
68+
// never let baseFee drop below 0
69+
defer func() {
70+
if baseFee.Cmp(common.Big0) < 0 {
71+
baseFee.Set(common.Big0)
72+
}
73+
}()
6874
// If we are at the block of EIP1559 activation then the BaseFee is set to the initial value
6975
if config.EIP1559Block.Cmp(height) == 0 {
70-
return new(big.Int).SetUint64(config.EIP1559.InitialBaseFee)
76+
baseFee = new(big.Int).SetUint64(config.EIP1559.InitialBaseFee)
77+
return
7178
}
7279

7380
// Otherwise,
7481
// BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // PARENT_EIP1559_GAS_TARGET // BASEFEE_MAX_CHANGE_DENOMINATOR
7582
// Where delta = PARENT_GAS_USED - PARENT_EIP1559_GAS_TARGET (possibly negative)
7683
parentGasTarget := CalcEIP1559GasTarget(config, parent.Number, new(big.Int).SetUint64(parent.GasLimit))
7784
delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed), parentGasTarget)
78-
// If delta is 0, BASEFEE is unchanged
85+
// If PARENT_GAS_USAGE == TARGET_GAS_USAGE; BASEFEE = PARENT_BASEFEE - 1
7986
if delta.Cmp(common.Big0) == 0 {
80-
return new(big.Int).Sub(parent.BaseFee, common.Big1)
87+
baseFee = new(big.Int).Sub(parent.BaseFee, common.Big1)
88+
return
8189
}
8290
mul := new(big.Int).Mul(parent.BaseFee, delta)
8391
div := new(big.Int).Div(mul, parentGasTarget)
8492
div2 := new(big.Int).Div(div, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator))
85-
baseFee := new(big.Int).Add(parent.BaseFee, div2)
93+
baseFee = new(big.Int).Add(parent.BaseFee, div2)
8694

8795
// A valid BASEFEE is one such that
8896
// abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR)

consensus/misc/basefee_test.go

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ func TestCalcBaseFee(t *testing.T) {
297297
12000000,
298298
big.NewInt(1025000000),
299299
},
300+
301+
// Low parent baseFee tests
302+
303+
// parent gas usage >>>> parent gas limit
304+
// parent baseFee == 0
305+
// as expected, increment baseFee by 1
300306
{
301307
params.EIP1559ChainConfig,
302308
big.NewInt(1000),
@@ -307,87 +313,75 @@ func TestCalcBaseFee(t *testing.T) {
307313
1000000000000000,
308314
big.NewInt(1),
309315
},
310-
311-
// Low parent baseFee tests
316+
// parent gas usage <<<< parent gas limit
317+
// parent baseFee == 0
318+
// would normally expect decrement
319+
// but with parent baseFee == 0 the base fee calculation
320+
// reduces down to parent basefee + 0 = 0
321+
// this means the diff between parent and new basefee == 0 which is less than the minimum of |1|
322+
// so we default to incrementing by 1, it is a positive increment since the diff == 0 is non-negative
312323
{
313324
params.EIP1559ChainConfig,
314325
big.NewInt(1000),
315326
1000,
316327
big.NewInt(2000),
317-
big.NewInt(1),
328+
big.NewInt(0),
329+
1000000000000000,
318330
1,
319-
8,
320-
big.NewInt(2),
331+
big.NewInt(1),
321332
},
333+
// parent gas usage == parent gas limit
334+
// parent baseFee == 0
335+
// would normally expect decrement of 1
336+
// but that would go to negative numbers so it remains at 0
322337
{
323338
params.EIP1559ChainConfig,
324339
big.NewInt(1000),
325340
1000,
326341
big.NewInt(2000),
327-
big.NewInt(1),
342+
big.NewInt(0),
328343
1,
329-
9,
330-
big.NewInt(2),
344+
1,
345+
big.NewInt(0),
331346
},
347+
// parent gas usage >>>> parent gas limit
348+
// parent baseFee == 1
349+
// as expected, increment baseFee by 1
332350
{
333351
params.EIP1559ChainConfig,
334352
big.NewInt(1000),
335353
1000,
336354
big.NewInt(2000),
337355
big.NewInt(1),
338-
100000000,
339-
899999999,
356+
1,
357+
1000000000000000,
340358
big.NewInt(2),
341359
},
360+
// parent gas usage <<<< parent gas limit
361+
// parent baseFee == 1
362+
// as expected, decrement by 1
342363
{
343364
params.EIP1559ChainConfig,
344365
big.NewInt(1000),
345366
1000,
346367
big.NewInt(2000),
347368
big.NewInt(1),
348-
100000000,
349-
900000000,
350-
big.NewInt(2),
351-
},
352-
{
353-
params.EIP1559ChainConfig,
354-
big.NewInt(1000),
355-
1000,
356-
big.NewInt(2000),
357-
big.NewInt(2),
369+
1000000000000000,
358370
1,
359-
4,
360-
big.NewInt(3),
371+
big.NewInt(0),
361372
},
373+
// parent gas usage == parent gas limit
374+
// parent baseFee == 1
375+
// as expected, decrement by 1 when gas usage equals the gas target
362376
{
363377
params.EIP1559ChainConfig,
364378
big.NewInt(1000),
365379
1000,
366380
big.NewInt(2000),
367-
big.NewInt(2),
381+
big.NewInt(1),
368382
1,
369-
5,
370-
big.NewInt(3),
371-
},
372-
{
373-
params.EIP1559ChainConfig,
374-
big.NewInt(1000),
375-
1000,
376-
big.NewInt(2000),
377-
big.NewInt(2),
378-
100000000,
379-
499999999,
380-
big.NewInt(3),
381-
},
382-
{
383-
params.EIP1559ChainConfig,
384-
big.NewInt(1000),
385-
1000,
386-
big.NewInt(2000),
387-
big.NewInt(2),
388-
100000000,
389-
500000000,
390-
big.NewInt(3),
383+
1,
384+
big.NewInt(0),
391385
},
392386
}
393387
for i, test := range testConditions {
@@ -404,13 +398,13 @@ func TestCalcBaseFee(t *testing.T) {
404398
gasTarget := CalcBaseFee(&config, parent)
405399
if gasTarget != nil {
406400
if test.baseFee != nil && gasTarget.Cmp(test.baseFee) != 0 {
407-
t.Errorf("test %d expected BaseFee %d got %d", i+1, test.baseFee.Uint64(), gasTarget.Uint64())
401+
t.Errorf("test %d expected BaseFee %d got %d", i+1, test.baseFee.Int64(), gasTarget.Int64())
408402
}
409403
if test.baseFee == nil {
410-
t.Errorf("test %d expected nil BaseFee got %d", i+1, gasTarget.Uint64())
404+
t.Errorf("test %d expected nil BaseFee got %d", i+1, gasTarget.Int64())
411405
}
412406
} else if test.baseFee != nil {
413-
t.Errorf("test %d expected BaseFee %d got nil", i+1, test.baseFee.Uint64())
407+
t.Errorf("test %d expected BaseFee %d got nil", i+1, test.baseFee.Int64())
414408
}
415409
}
416410
}

0 commit comments

Comments
 (0)