|
| 1 | +package osaka |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/rand" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/ethereum-optimism/optimism/op-acceptance-tests/tests/interop/loadtest" |
| 17 | + "github.com/ethereum-optimism/optimism/op-batcher/batcher" |
| 18 | + "github.com/ethereum-optimism/optimism/op-batcher/flags" |
| 19 | + "github.com/ethereum-optimism/optimism/op-chain-ops/devkeys" |
| 20 | + "github.com/ethereum-optimism/optimism/op-devstack/devtest" |
| 21 | + "github.com/ethereum-optimism/optimism/op-devstack/presets" |
| 22 | + "github.com/ethereum-optimism/optimism/op-devstack/stack" |
| 23 | + "github.com/ethereum-optimism/optimism/op-devstack/sysgo" |
| 24 | + "github.com/ethereum-optimism/optimism/op-e2e/e2eutils/intentbuilder" |
| 25 | + "github.com/ethereum-optimism/optimism/op-node/rollup/derive" |
| 26 | + "github.com/ethereum-optimism/optimism/op-service/eth" |
| 27 | + "github.com/ethereum-optimism/optimism/op-service/txinclude" |
| 28 | + "github.com/ethereum-optimism/optimism/op-service/txplan" |
| 29 | + "github.com/ethereum/go-ethereum/common" |
| 30 | + "github.com/ethereum/go-ethereum/consensus/misc/eip4844" |
| 31 | + "github.com/ethereum/go-ethereum/core/types" |
| 32 | + "github.com/ethereum/go-ethereum/params" |
| 33 | +) |
| 34 | + |
| 35 | +// configureDevstackEnvVars sets the appropriate env vars to use a mise-installed geth binary for |
| 36 | +// the L1 EL. This is useful in Osaka acceptance tests since op-geth does not include full Osaka |
| 37 | +// support. This is meant to run before presets.DoMain in a TestMain function. It will log to |
| 38 | +// stdout. ResetDevstackEnvVars should be used to reset the environment variables when TestMain |
| 39 | +// exits. |
| 40 | +// |
| 41 | +// Note that this is a no-op if either [sysgo.DevstackL1ELKindVar] or [sysgo.GethExecPathEnvVar] |
| 42 | +// are set. |
| 43 | +// |
| 44 | +// The returned callback resets any modified environment variables. |
| 45 | +func configureDevstackEnvVars() func() { |
| 46 | + if _, ok := os.LookupEnv(sysgo.DevstackL1ELKindEnvVar); ok { |
| 47 | + return func() {} |
| 48 | + } |
| 49 | + if _, ok := os.LookupEnv(sysgo.GethExecPathEnvVar); ok { |
| 50 | + return func() {} |
| 51 | + } |
| 52 | + |
| 53 | + cmd := exec.Command("mise", "which", "geth") |
| 54 | + buf := bytes.NewBuffer([]byte{}) |
| 55 | + cmd.Stdout = buf |
| 56 | + if err := cmd.Run(); err != nil { |
| 57 | + fmt.Printf("Failed to find mise-installed geth: %v\n", err) |
| 58 | + return func() {} |
| 59 | + } |
| 60 | + execPath := strings.TrimSpace(buf.String()) |
| 61 | + fmt.Println("Found mise-installed geth:", execPath) |
| 62 | + _ = os.Setenv(sysgo.GethExecPathEnvVar, execPath) |
| 63 | + _ = os.Setenv(sysgo.DevstackL1ELKindEnvVar, "geth") |
| 64 | + return func() { |
| 65 | + _ = os.Unsetenv(sysgo.GethExecPathEnvVar) |
| 66 | + _ = os.Unsetenv(sysgo.DevstackL1ELKindEnvVar) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestMain(m *testing.M) { |
| 71 | + resetEnvVars := configureDevstackEnvVars() |
| 72 | + defer resetEnvVars() |
| 73 | + |
| 74 | + presets.DoMain(m, stack.MakeCommon(stack.Combine[*sysgo.Orchestrator]( |
| 75 | + sysgo.DefaultMinimalSystem(&sysgo.DefaultMinimalSystemIDs{}), |
| 76 | + sysgo.WithDeployerOptions(func(_ devtest.P, _ devkeys.Keys, builder intentbuilder.Builder) { |
| 77 | + _, l1Config := builder.WithL1(sysgo.DefaultL1ID) |
| 78 | + l1Config.WithOsakaOffset(0) |
| 79 | + l1Config.WithBPO1Offset(0) |
| 80 | + l1Config.WithL1BlobSchedule(¶ms.BlobScheduleConfig{ |
| 81 | + Cancun: params.DefaultCancunBlobConfig, |
| 82 | + Osaka: params.DefaultOsakaBlobConfig, |
| 83 | + Prague: params.DefaultPragueBlobConfig, |
| 84 | + BPO1: params.DefaultBPO1BlobConfig, |
| 85 | + }) |
| 86 | + }), |
| 87 | + sysgo.WithBatcherOption(func(_ stack.L2BatcherID, cfg *batcher.CLIConfig) { |
| 88 | + cfg.DataAvailabilityType = flags.BlobsType |
| 89 | + }), |
| 90 | + ))) |
| 91 | +} |
| 92 | + |
| 93 | +func TestBatcherUsesNewSidecarFormatAfterOsaka(gt *testing.T) { |
| 94 | + t := devtest.SerialT(gt) |
| 95 | + sys := presets.NewMinimal(t) |
| 96 | + t.Log("Waiting for Osaka to activate") |
| 97 | + t.Require().NotNil(sys.L1Network.Escape().ChainConfig().OsakaTime) |
| 98 | + sys.L1EL.WaitForTime(*sys.L1Network.Escape().ChainConfig().OsakaTime) |
| 99 | + t.Log("Osaka activated") |
| 100 | + |
| 101 | + // 1. Wait for the sequencer to build a block after Osaka is activated. This avoids a race |
| 102 | + // condition where the unsafe head has been posted as part of a blob, but has not been |
| 103 | + // marked as "safe" yet. |
| 104 | + sys.L2EL.WaitForBlock() |
| 105 | + |
| 106 | + // 2. Wait for the batcher to include target in a batch and post it to L1. Because the batch is |
| 107 | + // posted after Osaka has activated, it means the batcher must have successfully used the |
| 108 | + // new format. |
| 109 | + target := sys.L2EL.BlockRefByLabel(eth.Unsafe) |
| 110 | + blockTime := time.Duration(sys.L2Chain.Escape().RollupConfig().BlockTime) * time.Second |
| 111 | + for range time.Tick(blockTime) { |
| 112 | + if sys.L2EL.BlockRefByLabel(eth.Safe).Number >= target.Number { |
| 113 | + // If the safe head is ahead of the target height and the target block is part of the |
| 114 | + // canonical chain, then the target block is safe. |
| 115 | + _, err := sys.L2EL.Escape().EthClient().BlockRefByHash(t.Ctx(), target.Hash) |
| 116 | + t.Require().NoError(err) |
| 117 | + return |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestBlobBaseFeeIsCorrectAfterBPOFork(gt *testing.T) { |
| 123 | + t := devtest.SerialT(gt) |
| 124 | + sys := presets.NewMinimal(t) |
| 125 | + t.Log("Waiting for BPO1 to activate") |
| 126 | + t.Require().NotNil(sys.L1Network.Escape().ChainConfig().BPO1Time) |
| 127 | + sys.L1EL.WaitForTime(*sys.L1Network.Escape().ChainConfig().BPO1Time) |
| 128 | + t.Log("BPO1 activated") |
| 129 | + |
| 130 | + sys.L1EL.WaitForBlock() |
| 131 | + l1BlockTime := sys.L1EL.EstimateBlockTime() |
| 132 | + l1ChainConfig := sys.L1Network.Escape().ChainConfig() |
| 133 | + |
| 134 | + spamBlobs(t, sys) // Raise the blob base fee to make blob parameter changes visible. |
| 135 | + |
| 136 | + // Wait for the blob base fee to rise above 1 so the blob parameter changes will be visible. |
| 137 | + for range time.Tick(l1BlockTime) { |
| 138 | + info, _, err := sys.L1EL.EthClient().InfoAndTxsByLabel(t.Ctx(), eth.Unsafe) |
| 139 | + t.Require().NoError(err) |
| 140 | + if calcBlobBaseFee(l1ChainConfig, info).Cmp(big.NewInt(1)) > 0 { |
| 141 | + break |
| 142 | + } |
| 143 | + t.Logf("Waiting for blob base fee to rise above 1") |
| 144 | + } |
| 145 | + |
| 146 | + l2UnsafeRef := sys.L2CL.SyncStatus().UnsafeL2 |
| 147 | + |
| 148 | + // Get the L1 blob base fee. |
| 149 | + l1OriginInfo, err := sys.L1EL.EthClient().InfoByHash(t.Ctx(), l2UnsafeRef.L1Origin.Hash) |
| 150 | + t.Require().NoError(err) |
| 151 | + l1BlobBaseFee := calcBlobBaseFee(l1ChainConfig, l1OriginInfo) |
| 152 | + |
| 153 | + // Get the L2 blob base fee from the system deposit tx. |
| 154 | + info, txs, err := sys.L2EL.Escape().EthClient().InfoAndTxsByHash(t.Ctx(), l2UnsafeRef.Hash) |
| 155 | + t.Require().NoError(err) |
| 156 | + blockInfo, err := derive.L1BlockInfoFromBytes(sys.L2Chain.Escape().RollupConfig(), info.Time(), txs[0].Data()) |
| 157 | + t.Require().NoError(err) |
| 158 | + l2BlobBaseFee := blockInfo.BlobBaseFee |
| 159 | + |
| 160 | + t.Require().Equal(l1BlobBaseFee, l2BlobBaseFee) |
| 161 | +} |
| 162 | + |
| 163 | +func spamBlobs(t devtest.T, sys *presets.Minimal) { |
| 164 | + l1BlockTime := sys.L1EL.EstimateBlockTime() |
| 165 | + l1ChainConfig := sys.L1Network.Escape().ChainConfig() |
| 166 | + |
| 167 | + eoa := sys.FunderL1.NewFundedEOA(eth.OneEther.Mul(5)) |
| 168 | + signer := txinclude.NewPkSigner(eoa.Key().Priv(), sys.L1Network.ChainID().ToBig()) |
| 169 | + l1ETHClient := sys.L1EL.EthClient() |
| 170 | + syncEOA := loadtest.NewSyncEOA(txinclude.NewPersistent(signer, struct { |
| 171 | + *txinclude.Monitor |
| 172 | + *txinclude.Resubmitter |
| 173 | + }{ |
| 174 | + txinclude.NewMonitor(l1ETHClient, l1BlockTime), |
| 175 | + txinclude.NewResubmitter(l1ETHClient, l1BlockTime), |
| 176 | + }), eoa.Plan()) |
| 177 | + |
| 178 | + var blob eth.Blob |
| 179 | + _, err := rand.Read(blob[:]) |
| 180 | + t.Require().NoError(err) |
| 181 | + // get the field-elements into a valid range |
| 182 | + for i := range 4096 { |
| 183 | + blob[32*i] &= 0b0011_1111 |
| 184 | + } |
| 185 | + |
| 186 | + const maxBlobTxsPerAccountInMempool = 16 // Private policy param in geth. |
| 187 | + spammer := loadtest.SpammerFunc(func(t devtest.T) error { |
| 188 | + _, err := syncEOA.Include(t, txplan.WithBlobs([]*eth.Blob{&blob}, l1ChainConfig), txplan.WithTo(&common.Address{})) |
| 189 | + return err |
| 190 | + }) |
| 191 | + txsPerSlot := min(l1ChainConfig.BlobScheduleConfig.BPO1.Max*3/4, maxBlobTxsPerAccountInMempool) |
| 192 | + schedule := loadtest.NewConstant(l1BlockTime, loadtest.WithBaseRPS(uint64(txsPerSlot))) |
| 193 | + |
| 194 | + ctx, cancel := context.WithCancel(t.Ctx()) |
| 195 | + var wg sync.WaitGroup |
| 196 | + t.Cleanup(func() { |
| 197 | + cancel() |
| 198 | + wg.Wait() |
| 199 | + }) |
| 200 | + wg.Add(1) |
| 201 | + go func() { |
| 202 | + defer wg.Done() |
| 203 | + schedule.Run(t.WithCtx(ctx), spammer) |
| 204 | + }() |
| 205 | +} |
| 206 | + |
| 207 | +func calcBlobBaseFee(cfg *params.ChainConfig, info eth.BlockInfo) *big.Int { |
| 208 | + return eip4844.CalcBlobFee(cfg, &types.Header{ |
| 209 | + // It's unfortunate that we can't build a proper header from a BlockInfo. |
| 210 | + // We do our best to work around deficiencies in the BlockInfo implementation here. |
| 211 | + Time: info.Time(), |
| 212 | + ExcessBlobGas: info.ExcessBlobGas(), |
| 213 | + }) |
| 214 | +} |
0 commit comments