Skip to content

Commit 5c6d276

Browse files
geokneefakedev9999
andauthored
core/types: implement operator fee fix (Jovian) (#696)
* fix!: multiply operatorFeeScalar by 100 instead of dividing by 1e6 * apply comments * apply comments * remove dup IsOperatorFeeFix --------- Co-authored-by: fakedev9999 <[email protected]>
1 parent ded9c88 commit 5c6d276

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

core/types/rollup_cost.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var (
8383
// attributes
8484
OperatorFeeParamsSlot = common.BigToHash(big.NewInt(8))
8585

86+
oneHundred = big.NewInt(100)
8687
oneMillion = big.NewInt(1_000_000)
8788
ecotoneDivisor = big.NewInt(1_000_000 * 16)
8889
fjordDivisor = big.NewInt(1_000_000_000_000)
@@ -232,7 +233,11 @@ func NewOperatorCostFunc(config *params.ChainConfig, statedb StateGetter) Operat
232233
}
233234
operatorFeeScalar, operatorFeeConstant := ExtractOperatorFeeParams(operatorFeeParams)
234235

235-
return newOperatorCostFunc(operatorFeeScalar, operatorFeeConstant)
236+
// Return the Operator Fee fix version if the feature is active
237+
if config.IsOperatorFeeFix(blockTime) {
238+
return newOperatorCostFuncOperatorFeeFix(operatorFeeScalar, operatorFeeConstant)
239+
}
240+
return newOperatorCostFuncIsthmus(operatorFeeScalar, operatorFeeConstant)
236241
}
237242

238243
return func(gas uint64, blockTime uint64) *uint256.Int {
@@ -245,7 +250,8 @@ func NewOperatorCostFunc(config *params.ChainConfig, statedb StateGetter) Operat
245250
}
246251
}
247252

248-
func newOperatorCostFunc(operatorFeeScalar *big.Int, operatorFeeConstant *big.Int) operatorCostFunc {
253+
// newOperatorCostFuncIsthmus returns the operator cost function introduced with Isthmus.
254+
func newOperatorCostFuncIsthmus(operatorFeeScalar *big.Int, operatorFeeConstant *big.Int) operatorCostFunc {
249255
return func(gas uint64) *uint256.Int {
250256
fee := new(big.Int).SetUint64(gas)
251257
fee = fee.Mul(fee, operatorFeeScalar)
@@ -254,7 +260,25 @@ func newOperatorCostFunc(operatorFeeScalar *big.Int, operatorFeeConstant *big.In
254260

255261
feeU256, overflow := uint256.FromBig(fee)
256262
if overflow {
257-
// This should never happen, as (u64.max * u32.max / 1e6) + u64.max is an int of bit length 77
263+
// This should never happen, as ((u64.max * u32.max) / 1e6) + u64.max fits in 77 bits
264+
panic("overflow in operator cost calculation")
265+
}
266+
267+
return feeU256
268+
}
269+
}
270+
271+
// newOperatorCostFuncOperatorFeeFix returns the operator cost function for the operator fee fix feature.
272+
func newOperatorCostFuncOperatorFeeFix(operatorFeeScalar *big.Int, operatorFeeConstant *big.Int) operatorCostFunc {
273+
return func(gas uint64) *uint256.Int {
274+
fee := new(big.Int).SetUint64(gas)
275+
fee = fee.Mul(fee, operatorFeeScalar)
276+
fee = fee.Mul(fee, oneHundred)
277+
fee = fee.Add(fee, operatorFeeConstant)
278+
279+
feeU256, overflow := uint256.FromBig(fee)
280+
if overflow {
281+
// This should never happen, as (u64.max * u32.max * 100) + u64.max fits in 103 bits
258282
panic("overflow in operator cost calculation")
259283
}
260284

core/types/rollup_cost_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
// the emptyTx is out of bounds for the linear regression so it uses the minimum size
3434
fjordFee = big.NewInt(3203000) // 100_000_000 * (2 * 1000 * 1e6 * 16 + 3 * 10 * 1e6) / 1e12
3535
ithmusOperatorFee = uint256.NewInt(1256417826611659930) // 1618 * 1439103868 / 1e6 + 1256417826609331460
36+
jovianOperatorFee = uint256.NewInt(1256650673615173860) // 1618 * 1439103868 * 100 + 1256417826609331460
3637

3738
bedrockGas = big.NewInt(1618)
3839
regolithGas = big.NewInt(530) // 530 = 1618 - (16*68)
@@ -461,6 +462,13 @@ func TestNewOperatorCostFunc(t *testing.T) {
461462
fee = costFunc(bedrockGas.Uint64(), time)
462463
require.NotNil(t, fee)
463464
require.Equal(t, ithmusOperatorFee, fee)
465+
466+
// emptyTx fee w/ jovian config should be not 0
467+
config.JovianTime = &time
468+
costFunc = NewOperatorCostFunc(config, statedb)
469+
fee = costFunc(bedrockGas.Uint64(), time)
470+
require.NotNil(t, fee)
471+
require.Equal(t, jovianOperatorFee, fee)
464472
}
465473

466474
func TestFlzCompressLen(t *testing.T) {
@@ -516,14 +524,16 @@ var emptyTxWithGas = NewTransaction(
516524
// combines the L1 cost and operator cost.
517525
func TestTotalRollupCostFunc(t *testing.T) {
518526
zero := uint64(0)
519-
later := uint64(10)
527+
isthmusTime := uint64(10)
528+
jovianTime := uint64(20)
520529
config := &params.ChainConfig{
521530
Optimism: params.OptimismTestConfig.Optimism,
522531
RegolithTime: &zero,
523532
EcotoneTime: &zero,
524533
FjordTime: &zero,
525534
HoloceneTime: &zero,
526-
IsthmusTime: &later,
535+
IsthmusTime: &isthmusTime,
536+
JovianTime: &jovianTime,
527537
}
528538
statedb := &testStateGetter{
529539
baseFee: baseFee,
@@ -537,13 +547,24 @@ func TestTotalRollupCostFunc(t *testing.T) {
537547
}
538548

539549
costFunc := NewTotalRollupCostFunc(config, statedb)
540-
cost := costFunc(emptyTxWithGas, later-1)
550+
551+
// Pre-Isthmus: only L1 cost
552+
cost := costFunc(emptyTxWithGas, isthmusTime-1)
541553
require.NotNil(t, cost)
542554
expCost := uint256.MustFromBig(fjordFee)
543555
require.Equal(t, expCost, cost, "pre-Isthmus total rollup cost should only contain L1 cost")
544556

545-
cost = costFunc(emptyTxWithGas, later+1)
557+
// Isthmus: L1 cost + Isthmus operator cost
558+
cost = costFunc(emptyTxWithGas, isthmusTime+1)
546559
require.NotNil(t, cost)
560+
expCost = uint256.MustFromBig(fjordFee)
547561
expCost.Add(expCost, ithmusOperatorFee)
548-
require.Equal(t, expCost, cost, "Isthmus total rollup cost should contain L1 cost and operator cost")
562+
require.Equal(t, expCost, cost, "Isthmus total rollup cost should contain L1 cost and Isthmus operator cost")
563+
564+
// Jovian: L1 cost + fixed operator cost
565+
cost = costFunc(emptyTxWithGas, jovianTime+1)
566+
require.NotNil(t, cost)
567+
expCost = uint256.MustFromBig(fjordFee)
568+
expCost.Add(expCost, jovianOperatorFee)
569+
require.Equal(t, expCost, cost, "Jovian total rollup cost should contain L1 cost and Jovian operator cost")
549570
}

0 commit comments

Comments
 (0)