|
| 1 | +package proofs_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + batcherFlags "github.com/ethereum-optimism/optimism/op-batcher/flags" |
| 8 | + "github.com/ethereum-optimism/optimism/op-chain-ops/genesis" |
| 9 | + actionsHelpers "github.com/ethereum-optimism/optimism/op-e2e/actions/helpers" |
| 10 | + "github.com/ethereum-optimism/optimism/op-e2e/actions/proofs/helpers" |
| 11 | + legacybindings "github.com/ethereum-optimism/optimism/op-e2e/bindings" |
| 12 | + "github.com/ethereum-optimism/optimism/op-service/eth" |
| 13 | + "github.com/ethereum-optimism/optimism/op-service/predeploys" |
| 14 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 15 | + "github.com/ethereum/go-ethereum/common" |
| 16 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 17 | + "github.com/ethereum/go-ethereum/consensus/misc/eip4844" |
| 18 | + "github.com/ethereum/go-ethereum/params" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +// Test_ProgramAction_BlobParameterForks tests the blob base fee calculation for different forks. |
| 23 | +func Test_ProgramAction_BlobParameterForks(gt *testing.T) { |
| 24 | + runBlobParameterForksTest := func(gt *testing.T, testCfg *helpers.TestCfg[any]) { |
| 25 | + t := actionsHelpers.NewDefaultTesting(gt) |
| 26 | + |
| 27 | + // Create test environment with Fusaka activation |
| 28 | + env := helpers.NewL2FaultProofEnv(t, testCfg, helpers.NewTestParams(), |
| 29 | + helpers.NewBatcherCfg( |
| 30 | + func(c *actionsHelpers.BatcherCfg) { |
| 31 | + c.DataAvailabilityType = batcherFlags.BlobsType |
| 32 | + }, |
| 33 | + ), |
| 34 | + func(dp *genesis.DeployConfig) { |
| 35 | + dp.L1CancunTimeOffset = ptr(hexutil.Uint64(0)) |
| 36 | + dp.L1PragueTimeOffset = ptr(hexutil.Uint64(12)) |
| 37 | + dp.L1OsakaTimeOffset = ptr(hexutil.Uint64(24)) |
| 38 | + dp.L1BPO1TimeOffset = ptr(hexutil.Uint64(36)) |
| 39 | + dp.L1BPO2TimeOffset = ptr(hexutil.Uint64(48)) |
| 40 | + dp.L1BPO3TimeOffset = ptr(hexutil.Uint64(60)) |
| 41 | + dp.L1BPO4TimeOffset = ptr(hexutil.Uint64(72)) |
| 42 | + dp.L1BlobScheduleConfig = ¶ms.BlobScheduleConfig{ |
| 43 | + Cancun: params.DefaultCancunBlobConfig, |
| 44 | + Osaka: params.DefaultOsakaBlobConfig, |
| 45 | + Prague: params.DefaultPragueBlobConfig, |
| 46 | + BPO1: params.DefaultBPO1BlobConfig, |
| 47 | + BPO2: params.DefaultBPO2BlobConfig, |
| 48 | + BPO3: params.DefaultBPO3BlobConfig, |
| 49 | + BPO4: params.DefaultBPO4BlobConfig, |
| 50 | + } |
| 51 | + dp.L1GenesisBlockExcessBlobGas = ptr(hexutil.Uint64(1e8)) // Jack up the blob market so we can test the blob fee calculation |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + miner, sequencer := env.Miner, env.Sequencer |
| 56 | + |
| 57 | + // Bind to L1Block contract on L2 |
| 58 | + l1BlockContract, err := legacybindings.NewL1Block(predeploys.L1BlockAddr, env.Engine.EthClient()) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + atBlockWithHash := func(hash common.Hash) *bind.CallOpts { |
| 62 | + return &bind.CallOpts{ |
| 63 | + BlockHash: hash, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // requireConsistentBlobBaseFeeForFork requires the blob base fee to be consistent between |
| 68 | + // the L1 Origin block (computed using the excess blob gas and l1 chain config) |
| 69 | + // and the L1 Block contract on L2 (accessed with a contract method call), for a given fork predicate. |
| 70 | + requireConsistentBlobBaseFeeForFork := func(t actionsHelpers.StatefulTesting, l2Block eth.L2BlockRef, expectActive bool, label string, isActive func(num *big.Int, time uint64) bool) { |
| 71 | + bbfL2, err := l1BlockContract.BlobBaseFee(atBlockWithHash(l2Block.Hash)) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + l1Origin := miner.L1Chain().GetHeaderByHash(l2Block.L1Origin.Hash) |
| 75 | + if expectActive { |
| 76 | + require.True(t, isActive(l1Origin.Number, l1Origin.Time), "%s not active at l1 origin %d, time %d", label, l1Origin.Number, l1Origin.Time) |
| 77 | + } else { |
| 78 | + require.False(t, isActive(l1Origin.Number, l1Origin.Time), "%s should not be active at l1 origin %d, time %d", label, l1Origin.Number, l1Origin.Time) |
| 79 | + } |
| 80 | + bbfL1 := eip4844.CalcBlobFee(env.Sd.L1Cfg.Config, l1Origin) |
| 81 | + |
| 82 | + require.True(t, bbfL2.Cmp(bbfL1) == 0, |
| 83 | + "%s: blob base fee does not match, bbfL2=%d, bbfL1=%d, l1BlockNum=%d, l2BlockNum=%d", label, bbfL2, bbfL1, l1Origin.Number, l2Block.Number) |
| 84 | + |
| 85 | + require.True(t, bbfL2.Cmp(big.NewInt(1)) > 0, |
| 86 | + "%s: blob base fee is unrealistically low and doesn't exercise the blob fee calculation", label) |
| 87 | + } |
| 88 | + |
| 89 | + // buildL1ToTime advances L1 with empty blocks until the given fork time. |
| 90 | + buildL1ToTime := func(t actionsHelpers.StatefulTesting, forkTime *uint64) { |
| 91 | + require.NotNil(t, forkTime, "fork time must be configured") |
| 92 | + h := miner.L1Chain().CurrentHeader() |
| 93 | + for h.Time < *forkTime { |
| 94 | + h = miner.ActEmptyBlock(t).Header() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Iterate through all forks and assert pre/post activation blob fees match expectations |
| 99 | + cfg := env.Sd.L1Cfg.Config |
| 100 | + forks := []struct { |
| 101 | + label string |
| 102 | + forkTime *uint64 |
| 103 | + isActive func(num *big.Int, time uint64) bool |
| 104 | + }{ |
| 105 | + {"Prague", cfg.PragueTime, func(num *big.Int, time uint64) bool { return cfg.IsPrague(num, time) }}, |
| 106 | + {"Osaka", cfg.OsakaTime, func(num *big.Int, time uint64) bool { return cfg.IsOsaka(num, time) }}, |
| 107 | + {"BPO1", cfg.BPO1Time, func(num *big.Int, time uint64) bool { return cfg.IsBPO1(num, time) }}, |
| 108 | + {"BPO2", cfg.BPO2Time, func(num *big.Int, time uint64) bool { return cfg.IsBPO2(num, time) }}, |
| 109 | + {"BPO3", cfg.BPO3Time, func(num *big.Int, time uint64) bool { return cfg.IsBPO3(num, time) }}, |
| 110 | + {"BPO4", cfg.BPO4Time, func(num *big.Int, time uint64) bool { return cfg.IsBPO4(num, time) }}, |
| 111 | + } |
| 112 | + for _, f := range forks { |
| 113 | + // Advance L1 to fork activation |
| 114 | + buildL1ToTime(t, f.forkTime) |
| 115 | + |
| 116 | + // Build an empty L2 block which still has a pre-fork L1 origin, and check blob fee |
| 117 | + sequencer.ActL2EmptyBlock(t) |
| 118 | + l2Block := sequencer.SyncStatus().UnsafeL2 |
| 119 | + requireConsistentBlobBaseFeeForFork(t, l2Block, false, f.label, f.isActive) |
| 120 | + |
| 121 | + // Advance L2 chain until L1 origin is at/after the fork activation |
| 122 | + sequencer.ActL1HeadSignal(t) |
| 123 | + sequencer.ActBuildToL1HeadUnsafe(t) |
| 124 | + |
| 125 | + l2Block = sequencer.L2Unsafe() |
| 126 | + require.Greater(t, l2Block.Number, uint64(1)) |
| 127 | + requireConsistentBlobBaseFeeForFork(t, l2Block, true, f.label, f.isActive) |
| 128 | + } |
| 129 | + |
| 130 | + // Final sync |
| 131 | + env.BatchMineAndSync(t) |
| 132 | + |
| 133 | + // Run fault proof program |
| 134 | + safeL2Head := sequencer.L2Safe() |
| 135 | + env.RunFaultProofProgramFromGenesis(t, safeL2Head.Number, testCfg.CheckResult, testCfg.InputParams...) |
| 136 | + } |
| 137 | + |
| 138 | + matrix := helpers.NewMatrix[any]() |
| 139 | + matrix.AddDefaultTestCases(nil, helpers.NewForkMatrix(helpers.LatestFork), runBlobParameterForksTest) |
| 140 | + matrix.Run(gt) |
| 141 | +} |
0 commit comments