diff --git a/docs/public-docs/node-operators/kona-node/rpc/rollup.mdx b/docs/public-docs/node-operators/kona-node/rpc/rollup.mdx index 59becec189c..9c7a3b3e654 100644 --- a/docs/public-docs/node-operators/kona-node/rpc/rollup.mdx +++ b/docs/public-docs/node-operators/kona-node/rpc/rollup.mdx @@ -120,6 +120,136 @@ Returns the output root at a specific block number, including the L2 block refer } ``` +## `optimism_rollupConfig` + +Returns the rollup configuration parameters that define the rollup chain's behavior and properties. + +| Client | Method invocation | +| ------ | ---------------------------------------------- | +| RPC | `{"method": "optimism_rollupConfig", "params": []}` | + +### Returns + +`RollupConfig` - An object containing the complete rollup configuration: +- `genesis` (`ChainGenesis`): The genesis state of the rollup +- `blockTime` (`number`): The block time of the L2 in seconds +- `maxSequencerDrift` (`number`): Maximum sequencer drift in seconds +- `seqWindowSize` (`number`): The sequencer window size +- `channelTimeout` (`number`): Number of L1 blocks between channel open/close +- `graniteChannelTimeout` (`number`): Channel timeout after Granite hardfork +- `l1ChainId` (`number`): The L1 chain ID +- `l2ChainId` (`number`): The L2 chain ID +- `batchInboxAddress` (`string`): L1 address where batches are sent +- `depositContractAddress` (`string`): L1 address for deposits +- `l1SystemConfigAddress` (`string`): L1 address for system config +- `protocolVersionsAddress` (`string`): L1 address for protocol versions +- Additional configuration fields for hardforks, fees, and interoperability + +### Example + +```js +// > {"jsonrpc":"2.0","id":1,"method":"optimism_rollupConfig","params":[]} +{ + "jsonrpc": "2.0", + "id": 1, + "result": { + "genesis": { + "l1": { + "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", + "number": 18000000 + }, + "l2": { + "hash": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", + "number": 0, + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "timestamp": 1699000000, + "l1Origin": { + "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", + "number": 18000000 + }, + "sequenceNumber": 0 + }, + "l2Time": 1699000000, + "systemConfig": { + "batcherAddr": "0x1234567890123456789012345678901234567890", + "overhead": "0x00000000000000000000000000000000000000000000000000000000000000bc", + "scalar": "0x00000000000000000000000000000000000000000000000000000000000f4240", + "gasLimit": 30000000 + } + }, + "blockTime": 2, + "maxSequencerDrift": 600, + "seqWindowSize": 3600, + "channelTimeout": 300, + "graniteChannelTimeout": 50, + "l1ChainId": 1, + "l2ChainId": 10, + "batchInboxAddress": "0xff00000000000000000000000000000000000010", + "depositContractAddress": "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed", + "l1SystemConfigAddress": "0x229047fed2591dbec1eF1118d64F7aF3dB9EB290", + "protocolVersionsAddress": "0x8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935", + "superchainConfigAddress": "0x95703e0982140D16f8ebA6d158FccEde42f04a4C", + "blobs_data": 1710374400, + "interopMessageExpiryWindow": 3600, + "chainOpConfig": { + "canyon_denominator": 250, + "canyon_elasticity": 6 + } + } +} +``` + +## `optimism_safeHeadAtL1Block` + +Returns the most recent recorded L2 safe head whose derivation source L1 block is at or before the requested L1 block. Enable persistence with `--safedb.path` or `KONA_NODE_SAFEDB_PATH`. + +| Client | Method invocation | +| ------ | ------------------------------------------------------------------ | +| RPC | `{"method": "optimism_safeHeadAtL1Block", "params": [l1BlockNumber]}` | + +### Parameters + +- `l1BlockNumber` (`string`): An L1 block number encoded as a hexadecimal quantity, such as `"0x64"`. Block tags are not supported. + +### Returns + +An object containing: + +- `l1Block`: The recorded derivation source L1 block, with `hash` and `number` fields. +- `safeHead`: The corresponding L2 safe head, with `hash` and `number` fields. + +Both block numbers in the response are JSON integers. The recorded L1 block can precede the requested block because records are sparse. A request above the recorded tip returns the latest record; it does not establish that derivation has processed the requested L1 block. + +### Errors + +| Condition | Code | Message | +| --- | --- | --- | +| No record exists at or before the requested L1 block | `-32000` | `not found` | +| SafeDB is disabled | `-32000` | Contains `safe head database not enabled` | +| Invalid parameter encoding | `-32602` | Invalid parameters | + +A disabled database is distinct from an enabled database with no matching record. Database read failures also return an RPC error; callers must not interpret a failed lookup as proof of historical safety. + +### Example + +```js +// > {"jsonrpc":"2.0","id":1,"method":"optimism_safeHeadAtL1Block","params":["0x64"]} +{ + "jsonrpc": "2.0", + "id": 1, + "result": { + "l1Block": { + "hash": "0x1111111111111111111111111111111111111111111111111111111111111111", + "number": 98 + }, + "safeHead": { + "hash": "0x2222222222222222222222222222222222222222222222222222222222222222", + "number": 42 + } + } +} +``` + ## `optimism_syncStatus` Returns the current synchronization status of the rollup node, including information about L1 and L2 block states. @@ -217,104 +347,92 @@ Returns the current synchronization status of the rollup node, including informa } ``` -## `optimism_rollupConfig` +## `optimism_version` -Returns the rollup configuration parameters that define the rollup chain's behavior and properties. +Returns the software version of the Kona rollup node. -| Client | Method invocation | -| ------ | ---------------------------------------------- | -| RPC | `{"method": "optimism_rollupConfig", "params": []}` | +| Client | Method invocation | +| ------ | ---------------------------------------- | +| RPC | `{"method": "optimism_version", "params": []}` | ### Returns -`RollupConfig` - An object containing the complete rollup configuration: -- `genesis` (`ChainGenesis`): The genesis state of the rollup -- `blockTime` (`number`): The block time of the L2 in seconds -- `maxSequencerDrift` (`number`): Maximum sequencer drift in seconds -- `seqWindowSize` (`number`): The sequencer window size -- `channelTimeout` (`number`): Number of L1 blocks between channel open/close -- `graniteChannelTimeout` (`number`): Channel timeout after Granite hardfork -- `l1ChainId` (`number`): The L1 chain ID -- `l2ChainId` (`number`): The L2 chain ID -- `batchInboxAddress` (`string`): L1 address where batches are sent -- `depositContractAddress` (`string`): L1 address for deposits -- `l1SystemConfigAddress` (`string`): L1 address for system config -- `protocolVersionsAddress` (`string`): L1 address for protocol versions -- Additional configuration fields for hardforks, fees, and interoperability +`string` - The version string of the Kona software (e.g., "0.1.0") ### Example ```js -// > {"jsonrpc":"2.0","id":1,"method":"optimism_rollupConfig","params":[]} +// > {"jsonrpc":"2.0","id":1,"method":"optimism_version","params":[]} { "jsonrpc": "2.0", "id": 1, - "result": { - "genesis": { - "l1": { - "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", - "number": 18000000 - }, - "l2": { - "hash": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", - "number": 0, - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "timestamp": 1699000000, - "l1Origin": { - "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", - "number": 18000000 - }, - "sequenceNumber": 0 - }, - "l2Time": 1699000000, - "systemConfig": { - "batcherAddr": "0x1234567890123456789012345678901234567890", - "overhead": "0x00000000000000000000000000000000000000000000000000000000000000bc", - "scalar": "0x00000000000000000000000000000000000000000000000000000000000f4240", - "gasLimit": 30000000 - } - }, - "blockTime": 2, - "maxSequencerDrift": 600, - "seqWindowSize": 3600, - "channelTimeout": 300, - "graniteChannelTimeout": 50, - "l1ChainId": 1, - "l2ChainId": 10, - "batchInboxAddress": "0xff00000000000000000000000000000000000010", - "depositContractAddress": "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed", - "l1SystemConfigAddress": "0x229047fed2591dbec1eF1118d64F7aF3dB9EB290", - "protocolVersionsAddress": "0x8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935", - "superchainConfigAddress": "0x95703e0982140D16f8ebA6d158FccEde42f04a4C", - "blobs_data": 1710374400, - "interopMessageExpiryWindow": 3600, - "chainOpConfig": { - "canyon_denominator": 250, - "canyon_elasticity": 6 - } - } + "result": "0.1.0" } ``` -## `optimism_version` +## `superroot_atTimestamp` -Returns the software version of the Kona rollup node. +Returns a pre-interop, single-chain super root and its L1 derivation requirements. This method uses the `superroot` namespace rather than the `optimism` namespace. SafeDB must be enabled and contain the relevant history for a non-genesis block that is locally safe. -| Client | Method invocation | -| ------ | ---------------------------------------- | -| RPC | `{"method": "optimism_version", "params": []}` | +| Client | Method invocation | +| ------ | ---------------------------------------------------------- | +| RPC | `{"method": "superroot_atTimestamp", "params": [timestamp]}` | + +### Parameters + +- `timestamp` (`string`): An L2 timestamp encoded as a hexadecimal quantity. The rollup configuration maps the timestamp to an L2 block number. ### Returns -`string` - The version string of the Kona software (e.g., "0.1.0") +The response reports derivation progress even when the requested block is not yet locally safe: + +- `chain_ids`: The single chain ID, encoded as a decimal string. +- `current_l1`: The derivation cursor, with `hash` and `number` fields. Every L1 block strictly below this cursor has been fully processed. Consumers checking an L1 scope `X` must wait for `current_l1.number > X`. +- `finalized_timestamp`: The finalized L2 head timestamp, as a JSON integer. +- `local_safe_timestamp`: The locally safe L2 head timestamp, as a JSON integer. +- `optimistic_at_timestamp`: A map keyed by decimal chain ID. Once the requested block is locally safe, its entry contains `output`, `output_root`, and the SafeDB-backed `required_l1` block. +- `safe_timestamp`: The safe L2 head timestamp, as a JSON integer. + +When the block is locally safe, `data` contains: + +- `super`: The version 1 preimage, containing `chains` and a hexadecimal `timestamp`. Each chain entry contains `ChainID` as a decimal string and `Output` as an output-root hash. +- `super_root`: The super-root hash. +- `verified_required_l1`: The SafeDB-backed L1 block required to derive the output. For the single-chain response, it equals that chain's `required_l1`. + +The `output` preimage contains `blockHash`, `messagePasserStorageRoot`, and `stateRoot`. L1 block references contain a hash and a JSON integer block number. Genesis has a zero L1 requirement. + +When the requested block is above local-safe, `optimistic_at_timestamp` is empty and `data` is omitted. Consumers must wait for a response containing the requested output rather than treating a progress-only response as a verified super root. + +### Errors + +- A timestamp before L2 genesis returns `-32602`. +- History not yet recorded returns `-32000` with a message containing `l1 at safe head not found`. +- History before the available range returns `-32000` with a message containing `l1 at safe head history unavailable`. Waiting for more derivation does not restore that missing historical range. +- A disabled database returns `-32000` with a message containing `safe head database not enabled` when a non-genesis historical lookup is required. +- If history changes repeatedly while the output is read, the request returns `-32000` with `safe head history changed during query; retry`. +- Database and engine read failures return RPC errors rather than an unverified root. + +SafeDB lookup messages for this method include the operation prefix `l1AtSafeHead@`. They differ from the bare `not found` message returned by `optimism_safeHeadAtL1Block`. ### Example +A progress-only response, returned while the requested block is not yet locally safe: + ```js -// > {"jsonrpc":"2.0","id":1,"method":"optimism_version","params":[]} +// > {"jsonrpc":"2.0","id":1,"method":"superroot_atTimestamp","params":["0x6553f100"]} { "jsonrpc": "2.0", "id": 1, - "result": "0.1.0" + "result": { + "chain_ids": ["901"], + "current_l1": { + "hash": "0x1111111111111111111111111111111111111111111111111111111111111111", + "number": 100 + }, + "finalized_timestamp": 1699999900, + "local_safe_timestamp": 1699999998, + "optimistic_at_timestamp": {}, + "safe_timestamp": 1699999998 + } } ``` diff --git a/op-acceptance-tests/tests/proofs/cannon_kona/setup_test.go b/op-acceptance-tests/tests/proofs/cannon_kona/setup_test.go index b84663e040f..d049d5aecce 100644 --- a/op-acceptance-tests/tests/proofs/cannon_kona/setup_test.go +++ b/op-acceptance-tests/tests/proofs/cannon_kona/setup_test.go @@ -11,9 +11,7 @@ func cannonOpts() []presets.Option { return []presets.Option{ presets.WithGameTypeAdded(gameTypes.CannonKonaGameType), presets.WithDeployerOptions(sysgo.WithJovianAtGenesis), - presets.WithGlobalL2CLOption(sysgo.L2CLOptionFn(func(p devtest.T, _ sysgo.ComponentTarget, cfg *sysgo.L2CLConfig) { - cfg.SafeDBPath = p.TempDir() - })), + presets.WithTimeTravelEnabled(), } } diff --git a/op-acceptance-tests/tests/proofs/cannon_kona/step_test.go b/op-acceptance-tests/tests/proofs/cannon_kona/step_test.go index bf49e843ac3..f3a6ed5bdc3 100644 --- a/op-acceptance-tests/tests/proofs/cannon_kona/step_test.go +++ b/op-acceptance-tests/tests/proofs/cannon_kona/step_test.go @@ -2,10 +2,11 @@ package cannon_kona import ( "testing" + "time" + gameTypes "github.com/ethereum-optimism/optimism/op-challenger/game/types" "github.com/ethereum-optimism/optimism/op-devstack/devtest" "github.com/ethereum-optimism/optimism/op-devstack/dsl/proofs" - "github.com/ethereum-optimism/optimism/op-devstack/sysgo" "github.com/ethereum-optimism/optimism/op-service/eth" safety "github.com/ethereum-optimism/optimism/op-service/eth/safety" @@ -13,19 +14,6 @@ import ( func TestExecuteStep_CannonKona(gt *testing.T) { t := devtest.ParallelT(gt) - // Example error with kona-node: - // - // assertions.go:387: ERROR[03-31|10:33:38.442] - // assertions.go:387: Error Trace: /optimism/op-devstack/dsl/proofs/game_helper.go:263 - // assertions.go:387: /optimism/op-devstack/dsl/proofs/game_helper.go:196 - // assertions.go:387: /optimism/op-devstack/dsl/proofs/fault_dispute_game.go:124 - // assertions.go:387: /optimism/op-acceptance-tests/tests/proofs/cannon/step_test.go:40 - // assertions.go:387: /.local/share/mise/installs/go/1.24.13/src/runtime/asm_arm64.s:1223 - // assertions.go:387: Error: Received unexpected error: - // assertions.go:387: failed to get safe head at L1 block 0xe926b953777546729b2607011590354a6a42dc35e36b7ea68c9dd4b470261541:6: not found - // assertions.go:387: Test: TestExecuteStep_CannonKona - // assertions.go:387: Messages: Failed to get honest root claim - sysgo.SkipOnKonaNode(t, "not supported") sys := newSystem(t) l1User := sys.FunderL1.NewFundedEOA(eth.ThousandEther) @@ -39,4 +27,6 @@ func TestExecuteStep_CannonKona(gt *testing.T) { claim = game.DisputeToStep(l1User, claim, 1000) // Skip down to max depth game.LogGameData() claim.WaitForCountered() + sys.AdvanceTime(game.MaxClockDuration() + time.Second) + game.WaitForGameStatus(gameTypes.GameStatusDefenderWon) } diff --git a/op-acceptance-tests/tests/safedb_proof_consumers/consumers_test.go b/op-acceptance-tests/tests/safedb_proof_consumers/consumers_test.go new file mode 100644 index 00000000000..ad2e1c8840e --- /dev/null +++ b/op-acceptance-tests/tests/safedb_proof_consumers/consumers_test.go @@ -0,0 +1,113 @@ +package safedb_proof_consumers + +import ( + "testing" + + gameTypes "github.com/ethereum-optimism/optimism/op-challenger/game/types" + "github.com/ethereum-optimism/optimism/op-core/devfeatures" + "github.com/ethereum-optimism/optimism/op-devstack/devtest" + "github.com/ethereum-optimism/optimism/op-devstack/dsl" + "github.com/ethereum-optimism/optimism/op-devstack/dsl/disputemon" + "github.com/ethereum-optimism/optimism/op-devstack/dsl/proofs" + "github.com/ethereum-optimism/optimism/op-devstack/presets" + "github.com/ethereum-optimism/optimism/op-devstack/sysgo" + "github.com/ethereum-optimism/optimism/op-service/eth" + + safety "github.com/ethereum-optimism/optimism/op-service/eth/safety" +) + +func TestProposerPublishesSuperRoot(gt *testing.T) { + t := devtest.ParallelT(gt) + require := t.Require() + system := presets.NewMinimal(t, + presets.WithDeployerOptions( + sysgo.WithJovianAtGenesis, + sysgo.WithUseInterop(), + sysgo.WithDevFeatureEnabled(devfeatures.SuperRootGamesMigrationFlag), + ), + ) + + // The genesis block's required L1 is fixed at 0 without a SafeDB read, so wait for a game past it. + game := system.DisputeGameFactory().WaitForSuperGameFrom(system.L2Chain.TimestampForBlockNum(1)) + game.VerifyGameType(gameTypes.SuperPermissionedGameType) + response := dsl.NewOpNodeSuperRoots(system.L2CL).AwaitSuperRootMatching(game.L2SequenceNumber(), game.RootClaimValue()) + + chainOutput, ok := response.OptimisticAtTimestamp[system.L2Chain.ChainID()] + require.True(ok, "super root response must include the L2 chain") + block := system.L2EL.BlockRefByHash(chainOutput.Output.BlockHash) + require.Equal(system.L2CL.OutputAtBlock(block.Number).OutputRoot, chainOutput.OutputRoot) + requireFirstL1WithSafeHead(t, system.L2CL, block.Number, response.Data.VerifiedRequiredL1) +} + +func TestDisputeMonitorAgreesWithSafeOutputRoot(gt *testing.T) { + t := devtest.ParallelT(gt) + require := t.Require() + system := newOutputRootSystem(t) + proposer := system.FunderL1.NewFundedEOA(eth.OneEther) + game := system.DisputeGameFactory().StartCannonKonaGame(proposer, proofs.WithL2SequenceNumber(3)) + + monitor := system.StartDisputeMon() + monitor.VerifyState( + disputemon.GameCount(gameTypes.CannonKonaGameType, 1), + disputemon.FailedGames(0), + disputemon.AgreedRoots(1), + disputemon.DisagreedRoots(0), + ) + + // dispute-mon also agrees when it cannot read the SafeDB, so check the record it relies on. + l1Head := system.L1EL.BlockRefByHash(game.L1Head()) + safeHead := system.L2CL.SafeHeadRecordAtL1Block(l1Head.Number) + require.NotNil(safeHead, "SafeDB must have a record at the game's L1 head %d", l1Head.Number) + require.GreaterOrEqual(safeHead.SafeHead.Number, game.L2SequenceNumber()) +} + +func TestDisputeMonitorRejectsOutputRootNotSafeAtGameL1Head(gt *testing.T) { + t := devtest.ParallelT(gt) + system := newOutputRootSystem(t) + proposer := system.FunderL1.NewFundedEOA(eth.OneEther) + system.L2CL.Reached(safety.LocalSafe, 3, 30) + system.L2Batcher.Stop() + system.L2CL.Advanced(safety.LocalUnsafe, 3, 30) + blockNumber := system.L2CL.HeadBlockRef(safety.LocalUnsafe).Number + + game := system.DisputeGameFactory().StartCannonKonaGame(proposer, + proofs.WithL2SequenceNumber(blockNumber), + proofs.WithFutureProposal(), + ) + system.L2Batcher.Start() + system.L2CL.Reached(safety.LocalSafe, blockNumber, 30) + + monitor := system.StartDisputeMon() + monitor.VerifyState( + disputemon.GameCount(gameTypes.CannonKonaGameType, 1), + disputemon.FailedGames(0), + disputemon.AgreedRoots(0), + disputemon.DisagreedRoots(1), + disputemon.InvalidProposalObserved(game), + ) +} + +func newOutputRootSystem(t devtest.T) *presets.Minimal { + return presets.NewMinimalNoFaultProofs(t, + presets.WithDeployerOptions(sysgo.WithJovianAtGenesis), + presets.WithGameTypeAdded(gameTypes.CannonKonaGameType), + ) +} + +// requireFirstL1WithSafeHead asserts that requiredL1 is the first L1 block at which the +// SafeDB records l2Block as safe. +func requireFirstL1WithSafeHead(t devtest.T, cl *dsl.L2CLNode, l2Block uint64, requiredL1 eth.BlockID) { + require := t.Require() + require.NotZero(requiredL1.Number, "required L1 for L2 block %d", l2Block) + + atRequired := cl.SafeHeadRecordAtL1Block(requiredL1.Number) + require.NotNil(atRequired, "SafeDB must have a record at the required L1 block %d", requiredL1.Number) + require.Equal(requiredL1, atRequired.L1Block, "required L1 must be a SafeDB record") + require.GreaterOrEqual(atRequired.SafeHead.Number, l2Block) + + beforeRequired := cl.SafeHeadRecordAtL1Block(requiredL1.Number - 1) + if beforeRequired == nil { + return + } + require.Less(beforeRequired.SafeHead.Number, l2Block, "L2 block %d was already safe before the required L1 block", l2Block) +} diff --git a/op-acceptance-tests/tests/safeheaddb_clsync/safeheaddb_test.go b/op-acceptance-tests/tests/safeheaddb_clsync/safeheaddb_test.go index f5d3e3472bc..3f579e1d7aa 100644 --- a/op-acceptance-tests/tests/safeheaddb_clsync/safeheaddb_test.go +++ b/op-acceptance-tests/tests/safeheaddb_clsync/safeheaddb_test.go @@ -1,4 +1,4 @@ -package safeheaddb_elsync +package safeheaddb_clsync import ( "testing" diff --git a/op-acceptance-tests/tests/safeheaddb_elsync/safeheaddb_test.go b/op-acceptance-tests/tests/safeheaddb_elsync/safeheaddb_test.go index 017e6da6fb9..bba108eca40 100644 --- a/op-acceptance-tests/tests/safeheaddb_elsync/safeheaddb_test.go +++ b/op-acceptance-tests/tests/safeheaddb_elsync/safeheaddb_test.go @@ -76,7 +76,7 @@ func TestNotTruncateDatabaseOnRestartWithExistingDatabase(gt *testing.T) { preRestartSafeBlock := sys.L2CLB.SafeHeadDBFloor() sys.L2CLB.VerifySafeHeadDatabaseMatches(sys.L2CL, dsl.WithMinRequiredL2Block(preRestartSafeBlock)) - // Restart the verifier op-node, but not the EL so the existing chain data is not deleted. + // Keep the EL's chain data while restarting the verifier CL. sys.L2CLB.Stop() sys.L2CL.Advanced(safety.LocalSafe, 3, 30) diff --git a/op-devstack/dsl/l1_el.go b/op-devstack/dsl/l1_el.go index 20b81b6b539..32b37a34b3b 100644 --- a/op-devstack/dsl/l1_el.go +++ b/op-devstack/dsl/l1_el.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum-optimism/optimism/op-service/clock" "github.com/ethereum-optimism/optimism/op-service/eth" "github.com/ethereum-optimism/optimism/op-service/retry" + "github.com/ethereum/go-ethereum/common" ) // L1ELNode wraps a stack.L1ELNode interface for DSL operations @@ -107,6 +108,14 @@ func (el *L1ELNode) BlockRefByNumber(number uint64) eth.L1BlockRef { return block } +func (el *L1ELNode) BlockRefByHash(hash common.Hash) eth.L1BlockRef { + ctx, cancel := context.WithTimeout(el.ctx, DefaultTimeout) + defer cancel() + block, err := el.inner.EthClient().BlockRefByHash(ctx, hash) + el.require.NoError(err, "block not found using block hash %s", hash) + return block +} + // ReorgTriggeredFn returns a lambda that checks that a L1 reorg occurred on the expected block // Composable with other lambdas to wait in parallel func (el *L1ELNode) ReorgTriggeredFn(target eth.L1BlockRef, attempts int) CheckFunc { diff --git a/op-devstack/dsl/l2_cl.go b/op-devstack/dsl/l2_cl.go index 602858b10bb..992d45d7b48 100644 --- a/op-devstack/dsl/l2_cl.go +++ b/op-devstack/dsl/l2_cl.go @@ -563,6 +563,12 @@ func (cl *L2CLNode) SafeHeadAtL1Block(l1BlockNum uint64) (*eth.SafeHeadResponse, return cl.inner.RollupAPI().SafeHeadAtL1Block(ctx, l1BlockNum) } +// SafeHeadRecordAtL1Block returns the safe head recorded at or before the given L1 block, or nil +// when the SafeDB has no record there. Any other error fails the test. +func (cl *L2CLNode) SafeHeadRecordAtL1Block(l1BlockNum uint64) *eth.SafeHeadResponse { + return cl.safeHeadAtL1Block(l1BlockNum) +} + func (cl *L2CLNode) safeHeadAtL1Block(l1BlockNum uint64) *eth.SafeHeadResponse { resp, err := cl.SafeHeadAtL1Block(l1BlockNum) if errors.Is(err, safedb.ErrNotFound) { diff --git a/op-devstack/dsl/proofs/dispute_game_factory.go b/op-devstack/dsl/proofs/dispute_game_factory.go index d44ef2aa0c1..810383f87e5 100644 --- a/op-devstack/dsl/proofs/dispute_game_factory.go +++ b/op-devstack/dsl/proofs/dispute_game_factory.go @@ -3,6 +3,7 @@ package proofs import ( "context" "encoding/binary" + "errors" "math" "math/big" "net/url" @@ -229,23 +230,66 @@ func (f *DisputeGameFactory) GameArgs(gameType gameTypes.GameType) []byte { } func (f *DisputeGameFactory) WaitForGame() *FaultDisputeGame { - initialCount := f.GameCount() + idx, err := f.waitForNewGame(func(context.Context, int64) (bool, error) { return true, nil }) + f.require.NoError(err, "dispute game factory did not create a new game") + return f.GameAtIndex(idx) +} + +// WaitForSuperGameFrom waits for a new super game whose L2 timestamp is at least minTimestamp, +// skipping new games before it. +func (f *DisputeGameFactory) WaitForSuperGameFrom(minTimestamp uint64) *SuperFaultDisputeGame { + idx, err := f.waitForNewGame(func(ctx context.Context, idx int64) (bool, error) { + gameInfo, err := contractio.Read(f.dgf.GameAtIndex(big.NewInt(idx)), ctx) + if err != nil { + return false, err + } + game := bindings.NewFaultDisputeGame(bindings.WithClient(f.ethClient), bindings.WithTo(gameInfo.Proxy), bindings.WithTest(f.t)) + timestamp, err := contractio.Read(game.L2SequenceNumber(), ctx) + if err != nil { + return false, err + } + if bigs.Uint64Strict(timestamp) < minTimestamp { + f.log.Info("Skipping game before timestamp", "index", idx, "gameTimestamp", timestamp, "minTimestamp", minTimestamp) + return false, nil + } + return true, nil + }) + f.require.NoErrorf(err, "dispute game factory did not create a new game at or after timestamp %d", minTimestamp) + return f.SuperGameAtIndex(idx) +} + +// waitForNewGame checks games created after the call, in index order, until isWanted accepts +// one, and returns its index. Read errors are retried until the timeout. +func (f *DisputeGameFactory) waitForNewGame(isWanted func(ctx context.Context, idx int64) (bool, error)) (int64, error) { + nextIndex := f.GameCount() timedCtx, cancel := context.WithTimeout(f.t.Ctx(), 10*time.Minute) defer cancel() var lastReadErr error err := wait.For(timedCtx, time.Second, func() (bool, error) { count, readErr := contractio.Read(f.dgf.GameCount(), timedCtx) - lastReadErr = readErr if readErr != nil { - f.log.Debug("Game count unavailable while waiting for new game", "current", initialCount, "err", readErr) + lastReadErr = readErr + f.log.Debug("Game count unavailable while waiting for new game", "next", nextIndex, "err", readErr) return false, nil } - gameCount := count.Int64() - f.t.Logf("waiting for new game. current=%d new=%d", initialCount, gameCount) - return gameCount > initialCount, nil + f.log.Info("Waiting for new game", "next", nextIndex, "count", count) + for ; nextIndex < count.Int64(); nextIndex++ { + wanted, readErr := isWanted(timedCtx, nextIndex) + if readErr != nil { + lastReadErr = readErr + f.log.Debug("Game unavailable while waiting for new game", "index", nextIndex, "err", readErr) + return false, nil + } + if wanted { + return true, nil + } + } + return false, nil }) - f.require.NoErrorf(err, "dispute game factory did not create a new game beyond count %d; last read error: %v", initialCount, lastReadErr) - return f.GameAtIndex(initialCount) + if err != nil { + return 0, errors.Join(err, lastReadErr) + } + return nextIndex, nil } // WaitForZKGameAtIndex waits until the factory holds a ZK dispute game at the diff --git a/op-devstack/dsl/super_root_source.go b/op-devstack/dsl/super_root_source.go index a00936848a0..a382ab64d6e 100644 --- a/op-devstack/dsl/super_root_source.go +++ b/op-devstack/dsl/super_root_source.go @@ -77,7 +77,18 @@ func (s *SuperRootQuerier) SuperRootAtTimestamp(timestamp uint64) eth.SuperRootA // AssertSuperRootAtTimestamp asserts that the super-root at the given timestamp matches the expected root claim. func (s *SuperRootQuerier) AssertSuperRootAtTimestamp(l2SequenceNumber uint64, rootClaim common.Hash) { - resp := s.SuperRootAtTimestamp(l2SequenceNumber) + s.requireSuperRootMatches(s.SuperRootAtTimestamp(l2SequenceNumber), l2SequenceNumber, rootClaim) +} + +// AwaitSuperRootMatching waits for the super-root at the given timestamp, asserts that it matches +// the expected root claim, and returns the response. +func (s *SuperRootQuerier) AwaitSuperRootMatching(l2SequenceNumber uint64, rootClaim common.Hash) eth.SuperRootAtTimestampResponse { + resp := s.AwaitSuperRootAtTimestamp(l2SequenceNumber) + s.requireSuperRootMatches(resp, l2SequenceNumber, rootClaim) + return resp +} + +func (s *SuperRootQuerier) requireSuperRootMatches(resp eth.SuperRootAtTimestampResponse, l2SequenceNumber uint64, rootClaim common.Hash) { s.require.NotNilf(resp.Data, "super root does not exist at time %d", l2SequenceNumber) superRoot := eth.SuperRoot(resp.Data.Super) s.require.Equal(superRoot[:], rootClaim[:]) @@ -85,16 +96,27 @@ func (s *SuperRootQuerier) AssertSuperRootAtTimestamp(l2SequenceNumber uint64, r // AwaitValidatedTimestamp waits for the super-root at the given timestamp to be available. func (s *SuperRootQuerier) AwaitValidatedTimestamp(timestamp uint64) { + s.AwaitSuperRootAtTimestamp(timestamp) +} + +// AwaitSuperRootAtTimestamp waits for the super-root at the given timestamp to be available and returns it. +func (s *SuperRootQuerier) AwaitSuperRootAtTimestamp(timestamp uint64) eth.SuperRootAtTimestampResponse { ctx, cancel := context.WithTimeout(s.ctx, 5*DefaultTimeout) defer cancel() + var resp eth.SuperRootAtTimestampResponse + var lastErr error err := wait.For(ctx, 1*time.Second, func() (bool, error) { - resp, err := s.api.SuperRootAtTimestamp(ctx, timestamp) + var err error + resp, err = s.api.SuperRootAtTimestamp(ctx, timestamp) if err != nil { - return false, nil // Ignore transient errors. + lastErr = err + s.log.Debug("Super-root query failed, retrying", "timestamp", timestamp, "err", err) + return false, nil // Retry: some errors are transient. } return resp.Data != nil, nil }) - s.require.NoError(err, "super-root at timestamp %d was not validated in time", timestamp) + s.require.NoError(err, "super-root at timestamp %d was not validated in time; last error: %v", timestamp, lastErr) + return resp } // AwaitOptimisticBlockAtTimestamp waits for one chain's optimistic output to become available.