Skip to content

Commit af1c710

Browse files
committed
baseFee increment magnitude minimum of 1; -1 when parent gas usage == parent gas target
1 parent e91b52b commit af1c710

File tree

2 files changed

+19
-129
lines changed

2 files changed

+19
-129
lines changed

consensus/misc/basefee.go

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

7373
// Otherwise,
7474
// BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // PARENT_EIP1559_GAS_TARGET // BASEFEE_MAX_CHANGE_DENOMINATOR
75-
// Where delta = parent.GasUsed - PARENT_EIP1559_GAS_TARGET
75+
// Where delta = PARENT_GAS_USED - PARENT_EIP1559_GAS_TARGET (possibly negative)
7676
parentGasTarget := CalcEIP1559GasTarget(config, parent.Number, new(big.Int).SetUint64(parent.GasLimit))
7777
delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed), parentGasTarget)
78+
// If delta is 0, BASEFEE is unchanged
79+
if delta.Cmp(common.Big0) == 0 {
80+
return new(big.Int).Sub(parent.BaseFee, common.Big1)
81+
}
7882
mul := new(big.Int).Mul(parent.BaseFee, delta)
7983
div := new(big.Int).Div(mul, parentGasTarget)
8084
div2 := new(big.Int).Div(div, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator))
8185
baseFee := new(big.Int).Add(parent.BaseFee, div2)
8286

83-
// A valid BASEFEE is one such that abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR)
87+
// A valid BASEFEE is one such that
88+
// abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR)
89+
// abs(BASEFEE - PARENT_BASEFEE) >= 1
8490
diff := new(big.Int).Sub(baseFee, parent.BaseFee)
8591
neg := false
8692
if diff.Sign() < 0 {
8793
neg = true
8894
diff.Neg(diff)
8995
}
96+
min := common.Big1
9097
max := new(big.Int).Div(parent.BaseFee, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator))
9198
if max.Cmp(common.Big1) < 0 {
9299
max = common.Big1
93100
}
94-
// If derived BaseFee is not valid, restrict it within the bounds
101+
// If BASEFEE is not valid, restrict it within the bounds
95102
if diff.Cmp(max) > 0 {
96103
if neg {
97104
max.Neg(max)
98105
}
99106
baseFee.Set(new(big.Int).Add(parent.BaseFee, max))
100-
}
101-
// Prevent BaseFee from dropping to zero
102-
if baseFee.Cmp(common.Big0) == 0 {
103-
baseFee.Set(common.Big1)
107+
} else if diff.Cmp(min) < 0 {
108+
if neg {
109+
min.Neg(min)
110+
}
111+
baseFee.Set(new(big.Int).Add(parent.BaseFee, min))
104112
}
105113
return baseFee
106114
}

consensus/misc/basefee_test.go

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func TestCalcBaseFee(t *testing.T) {
225225
big.NewInt(1000000000),
226226
10000000,
227227
10000000,
228-
big.NewInt(1000000000),
228+
big.NewInt(999999999), // baseFee diff is -1 when usage == target
229229
},
230230
{
231231
params.EIP1559ChainConfig,
@@ -308,7 +308,7 @@ func TestCalcBaseFee(t *testing.T) {
308308
big.NewInt(1),
309309
},
310310

311-
/* fails
311+
// Low parent baseFee tests
312312
{
313313
params.EIP1559ChainConfig,
314314
big.NewInt(1000),
@@ -319,7 +319,6 @@ func TestCalcBaseFee(t *testing.T) {
319319
8,
320320
big.NewInt(2),
321321
},
322-
*/
323322
{
324323
params.EIP1559ChainConfig,
325324
big.NewInt(1000),
@@ -330,29 +329,6 @@ func TestCalcBaseFee(t *testing.T) {
330329
9,
331330
big.NewInt(2),
332331
},
333-
/* fails
334-
{
335-
params.EIP1559ChainConfig,
336-
big.NewInt(1000),
337-
1000,
338-
big.NewInt(2000),
339-
big.NewInt(1),
340-
100000,
341-
899999,
342-
big.NewInt(2),
343-
},
344-
*/
345-
{
346-
params.EIP1559ChainConfig,
347-
big.NewInt(1000),
348-
1000,
349-
big.NewInt(2000),
350-
big.NewInt(1),
351-
100000,
352-
900000,
353-
big.NewInt(2),
354-
},
355-
/* fails
356332
{
357333
params.EIP1559ChainConfig,
358334
big.NewInt(1000),
@@ -363,7 +339,6 @@ func TestCalcBaseFee(t *testing.T) {
363339
899999999,
364340
big.NewInt(2),
365341
},
366-
*/
367342
{
368343
params.EIP1559ChainConfig,
369344
big.NewInt(1000),
@@ -374,7 +349,6 @@ func TestCalcBaseFee(t *testing.T) {
374349
900000000,
375350
big.NewInt(2),
376351
},
377-
/* fails
378352
{
379353
params.EIP1559ChainConfig,
380354
big.NewInt(1000),
@@ -385,7 +359,6 @@ func TestCalcBaseFee(t *testing.T) {
385359
4,
386360
big.NewInt(3),
387361
},
388-
*/
389362
{
390363
params.EIP1559ChainConfig,
391364
big.NewInt(1000),
@@ -396,40 +369,16 @@ func TestCalcBaseFee(t *testing.T) {
396369
5,
397370
big.NewInt(3),
398371
},
399-
/* fails
400-
{
401-
params.EIP1559ChainConfig,
402-
big.NewInt(1000),
403-
1000,
404-
big.NewInt(2000),
405-
big.NewInt(2),
406-
100000,
407-
499999,
408-
big.NewInt(3),
409-
},
410-
*/
411372
{
412373
params.EIP1559ChainConfig,
413374
big.NewInt(1000),
414375
1000,
415376
big.NewInt(2000),
416377
big.NewInt(2),
417-
100000,
418-
500000,
378+
100000000,
379+
499999999,
419380
big.NewInt(3),
420381
},
421-
/*
422-
{
423-
params.EIP1559ChainConfig,
424-
big.NewInt(1000),
425-
1000,
426-
big.NewInt(2000),
427-
big.NewInt(2),
428-
100000000,
429-
499999999,
430-
big.NewInt(3),
431-
},
432-
*/
433382
{
434383
params.EIP1559ChainConfig,
435384
big.NewInt(1000),
@@ -440,73 +389,6 @@ func TestCalcBaseFee(t *testing.T) {
440389
500000000,
441390
big.NewInt(3),
442391
},
443-
444-
/* fails
445-
{
446-
params.EIP1559ChainConfig,
447-
big.NewInt(1000),
448-
1000,
449-
big.NewInt(2000),
450-
big.NewInt(3),
451-
1,
452-
3,
453-
big.NewInt(4),
454-
},
455-
*/
456-
{
457-
params.EIP1559ChainConfig,
458-
big.NewInt(1000),
459-
1000,
460-
big.NewInt(2000),
461-
big.NewInt(3),
462-
1,
463-
4,
464-
big.NewInt(4),
465-
},
466-
/* fails
467-
{
468-
params.EIP1559ChainConfig,
469-
big.NewInt(1000),
470-
1000,
471-
big.NewInt(2000),
472-
big.NewInt(3),
473-
100000,
474-
366666,
475-
big.NewInt(4),
476-
},
477-
*/
478-
{
479-
params.EIP1559ChainConfig,
480-
big.NewInt(1000),
481-
1000,
482-
big.NewInt(2000),
483-
big.NewInt(3),
484-
100000,
485-
366667,
486-
big.NewInt(4),
487-
},
488-
/* fails
489-
{
490-
params.EIP1559ChainConfig,
491-
big.NewInt(1000),
492-
1000,
493-
big.NewInt(2000),
494-
big.NewInt(3),
495-
100000000,
496-
366666666,
497-
big.NewInt(4),
498-
},
499-
*/
500-
{
501-
params.EIP1559ChainConfig,
502-
big.NewInt(1000),
503-
1000,
504-
big.NewInt(2000),
505-
big.NewInt(3),
506-
100000000,
507-
366666667,
508-
big.NewInt(4),
509-
},
510392
}
511393
for i, test := range testConditions {
512394
config := *test.config

0 commit comments

Comments
 (0)