From 4bb9525f838bbefda15d0988a56885aec2864150 Mon Sep 17 00:00:00 2001 From: Sebastian Stammler Date: Fri, 18 Sep 2026 19:57:14 +0200 Subject: [PATCH] op-alloy,kona!: fix Bedrock deposit decoding and pin gas parity Establish deposit gas parity across Bedrock, Regolith and Karst before the reth update, using native Kona proofs against op-geth-derived blocks. Decode canonical Bedrock system-deposit RPC responses so those proofs can load their parent blocks. BREAKING CHANGE: isSystemTx uses a JSON boolean rather than a hex quantity. Callers exchanging historical Bedrock system deposits must use the canonical boolean form, updating older op-reth RPC endpoints when pairing them with the new decoder. --- op-core/types/deposit_tx_test.go | 22 +++ rust/kona/tests/proofs/deposit_gas_test.go | 165 ++++++++++++++++++ rust/kona/tests/proofs/helpers/env.go | 11 +- rust/kona/tests/proofs/helpers/matrix.go | 5 +- .../consensus/src/transaction/deposit.rs | 7 +- .../crates/rpc-types/src/transaction.rs | 23 +++ 6 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 rust/kona/tests/proofs/deposit_gas_test.go diff --git a/op-core/types/deposit_tx_test.go b/op-core/types/deposit_tx_test.go index 69a8aa01c09..723bdbbec93 100644 --- a/op-core/types/deposit_tx_test.go +++ b/op-core/types/deposit_tx_test.go @@ -151,6 +151,28 @@ func TestDepositHashParity(t *testing.T) { } } +// TestBedrockSystemDepositHashGoldenVector pins the cross-client hash of a pre-Regolith +// L1-info deposit captured from op-geth. The system flag is part of the canonical encoding, +// so both the flag and this hash must stay aligned with op-alloy after op-geth is removed. +func TestBedrockSystemDepositHashGoldenVector(t *testing.T) { + to := common.HexToAddress("0x4200000000000000000000000000000000000015") + tx := &optypes.DepositTx{ + SourceHash: common.HexToHash("0xfd3be1af69bee3e315a5dc5b773c64432230d76db68edae7a64193522118a377"), + From: common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001"), + To: &to, + Mint: big.NewInt(0), + Value: big.NewInt(0), + Gas: 150_000_000, + IsSystemTransaction: true, + Data: common.FromHex( + "0x015d8eb90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006aad7005000000000000000000000000000000000000000000000000000000003b9aca001b7ca7f22fd5e435bbc1f175d4e7721cee397530e7ef753563b0b8772ec58d86000000000000000000000000000000000000000000000000000000000000000b0000000000000000000000003c44cdddb6a900fa2b585dd299e03d12fa4293bc000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240", + ), + } + const opAlloyTxHash = "0x67c97e03f0759855b20459dd9b5f9ddebdc094f3669bf039011252bae68f5f24" + + require.Equal(t, opAlloyTxHash, tx.Hash().Hex()) +} + func TestUnmarshalDepositTxRoundTrip(t *testing.T) { for _, tc := range depositTxTestCases() { t.Run(tc.name, func(t *testing.T) { diff --git a/rust/kona/tests/proofs/deposit_gas_test.go b/rust/kona/tests/proofs/deposit_gas_test.go new file mode 100644 index 00000000000..9874b606c37 --- /dev/null +++ b/rust/kona/tests/proofs/deposit_gas_test.go @@ -0,0 +1,165 @@ +package proofs + +import ( + "math/big" + "testing" + + batcherFlags "github.com/ethereum-optimism/optimism/op-batcher/flags" + actionsHelpers "github.com/ethereum-optimism/optimism/op-e2e/actions/helpers" + "github.com/ethereum-optimism/optimism/rust/kona/tests/proofs/helpers" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/stretchr/testify/require" +) + +const ( + userDepositGasLimit = uint64(100_000) + revertDepositGas = uint64(params.TxGas + 6) // PUSH1 0; PUSH1 0; REVERT +) + +var ( + revertDepositTarget = common.HexToAddress("0xfffffffffffffffffffffffffffffffffffffffe") + revertDepositCode = common.FromHex("0x60006000fd") +) + +type depositGasVariant uint8 + +const ( + l1InfoDeposit depositGasVariant = iota + ordinaryDepositSuccess + ordinaryDepositRevert +) + +func TestDepositGasParity(gt *testing.T) { + runDepositGasTest := func(gt *testing.T, testCfg *helpers.TestCfg[depositGasVariant]) { + t := actionsHelpers.NewDefaultTesting(gt) + + if testCfg.Custom == ordinaryDepositRevert { + // Each matrix entry owns its allocation map: honest/junk claim cases and forks may run + // concurrently. The runtime uses only pre-Shanghai opcodes so it behaves identically at + // every fork under test. + allocs := *actionsHelpers.DefaultAlloc + allocs.L2Alloc = map[common.Address]types.Account{ + revertDepositTarget: { + Code: revertDepositCode, + Nonce: 1, + Balance: new(big.Int), + }, + } + testCfg.Allocs = &allocs + } + + batcherCfg := helpers.NewBatcherCfg(func(c *actionsHelpers.BatcherCfg) { + // Calldata is the only batch transport available at every fork in this matrix. + c.DataAvailabilityType = batcherFlags.CalldataType + }) + env := helpers.NewL2FaultProofEnv(t, testCfg, helpers.NewTestParams(), batcherCfg) + preRegolith := testCfg.Hardfork.Precedence < helpers.Regolith.Precedence + + if testCfg.Custom == l1InfoDeposit { + // Every naturally built L2 block starts with an L1-info deposit. Do not force the + // system flag: derivation sets it only before Regolith. + env.Sequencer.ActL2EmptyBlock(t) + } else { + target := env.Dp.Addresses.Bob + if testCfg.Custom == ordinaryDepositRevert { + target = revertDepositTarget + } + + // Submit the ordinary deposit through the real portal, mine its L1 receipt, and let + // the sequencer derive it when adopting the new L1 origin. + env.Alice.L1.ActResetTxOpts(t) + env.Alice.L2.ActResetTxOpts(t) + env.Alice.L2.ActSetTxToAddr(&target)(t) + env.Alice.L2.ActSetTxGasLimit(userDepositGasLimit)(t) + env.Alice.ActDeposit(t) + env.Miner.ActL1StartBlock(helpers.L1BlockTime)(t) + env.Miner.ActL1IncludeTx(env.Alice.Address())(t) + env.Miner.ActL1EndBlock(t) + env.Sequencer.ActL1HeadSignal(t) + env.Sequencer.ActBuildToL1HeadUnsafe(t) + env.Alice.ActCheckDepositStatus(true, testCfg.Custom == ordinaryDepositSuccess)(t) + } + + // Inspect the exact op-geth block that contains the deposit under test. + testedBlockRef := env.Sequencer.L2Unsafe() + testedBlock := env.Engine.L2Chain().GetBlockByHash(testedBlockRef.Hash) + require.NotNil(t, testedBlock) + txs := testedBlock.Transactions() + receipts := env.Engine.L2Chain().GetReceiptsByHash(testedBlockRef.Hash) + require.Len(t, receipts, len(txs), "every transaction must have a receipt") + + if testCfg.Custom == l1InfoDeposit { + require.Len(t, txs, 1, "empty block must contain only its L1-info deposit") + } else { + require.Len(t, txs, 2, "deposit block must contain L1-info and the portal deposit") + } + + l1InfoTx := txs[0] + l1InfoReceipt := receipts[0] + require.True(t, l1InfoTx.IsDepositTx(), "L1-info transaction must be a deposit") + require.Equal(t, preRegolith, l1InfoTx.IsSystemTx(), "L1-info system flag must transition at Regolith") + require.Equal(t, types.ReceiptStatusSuccessful, l1InfoReceipt.Status) + if preRegolith { + require.Zero(t, l1InfoReceipt.GasUsed, "Bedrock system deposit must report zero gas") + } else { + require.NotZero(t, l1InfoReceipt.GasUsed, "post-Regolith L1-info deposit must report actual gas") + require.Less(t, l1InfoReceipt.GasUsed, l1InfoTx.Gas(), "L1-info gas limit must exceed actual gas") + } + + if testCfg.Custom != l1InfoDeposit { + depositTx := txs[1] + depositReceipt := receipts[1] + require.True(t, depositTx.IsDepositTx(), "portal transaction must derive to a deposit") + require.False(t, depositTx.IsSystemTx(), "ordinary portal deposit must not be a system transaction") + require.Equal(t, userDepositGasLimit, depositTx.Gas()) + + expectedStatus := uint64(types.ReceiptStatusSuccessful) + expectedActualGas := uint64(params.TxGas) + if testCfg.Custom == ordinaryDepositRevert { + expectedStatus = types.ReceiptStatusFailed + expectedActualGas = revertDepositGas + require.Equal(t, revertDepositTarget, *depositTx.To()) + } else { + require.Equal(t, env.Dp.Addresses.Bob, *depositTx.To()) + } + require.Greater(t, userDepositGasLimit, expectedActualGas, "fixture must leave unused deposit gas") + require.Equal(t, expectedStatus, depositReceipt.Status) + + expectedReportedGas := expectedActualGas + if preRegolith { + expectedReportedGas = userDepositGasLimit + } + require.Equal(t, expectedReportedGas, depositReceipt.GasUsed, + "deposit receipt gas must follow the active fork's accounting rules") + } + + var cumulativeGas uint64 + for i, receipt := range receipts { + require.Equal(t, txs[i].Hash(), receipt.TxHash) + cumulativeGas += receipt.GasUsed + require.Equal(t, cumulativeGas, receipt.CumulativeGasUsed, + "receipt cumulative gas must equal the sum of reported transaction gas") + } + require.Equal(t, cumulativeGas, testedBlock.GasUsed(), + "header gas used must equal the final receipt cumulative gas") + + // Publish the unsafe chain, derive it back as safe, and prove only the transition into + // the block containing the tested deposit (rather than every preceding empty block). + l2SafeHead := env.BatchMineAndSync(t) + require.Equal(t, testedBlockRef.Number, l2SafeHead.Number, + "safe head must be the tested deposit block") + require.Equal(t, testedBlockRef.Hash, l2SafeHead.Hash, + "derivation must reproduce the op-geth block used as the proof claim") + + env.RunFaultProofProgram(t, testedBlockRef.Number, testCfg.CheckResult, testCfg.InputParams...) + } + + forks := helpers.NewForkMatrix(helpers.Bedrock, helpers.Regolith, helpers.Karst) + matrix := helpers.NewMatrix[depositGasVariant]() + matrix.AddDefaultTestCasesWithName("L1InfoDeposit", l1InfoDeposit, forks, runDepositGasTest) + matrix.AddDefaultTestCasesWithName("OrdinaryDepositSuccess", ordinaryDepositSuccess, forks, runDepositGasTest) + matrix.AddDefaultTestCasesWithName("OrdinaryDepositRevert", ordinaryDepositRevert, forks, runDepositGasTest) + matrix.Run(gt) +} diff --git a/rust/kona/tests/proofs/helpers/env.go b/rust/kona/tests/proofs/helpers/env.go index 2cc24a0faea..85653ca9182 100644 --- a/rust/kona/tests/proofs/helpers/env.go +++ b/rust/kona/tests/proofs/helpers/env.go @@ -49,7 +49,16 @@ func NewL2FaultProofEnv[c any](t helpers.Testing, testCfg *TestCfg[c], tp *e2eut if testCfg.Hardfork == nil { t.Fatalf("HF not set") } - dp.DeployConfig.ActivateForkAtGenesis(forks.Name(testCfg.Hardfork.Name)) + hardfork := forks.Name(testCfg.Hardfork.Name) + if hardfork == forks.Bedrock { + // Bedrock is implicit and has no activation offset. Disable only the + // schedulable mainline L2 forks, preserving independent fork settings. + for _, fork := range forks.From(forks.Regolith) { + dp.DeployConfig.SetForkTimeOffset(fork, nil) + } + } else { + dp.DeployConfig.ActivateForkAtGenesis(hardfork) + } for _, override := range deployConfigOverrides { override(dp.DeployConfig) diff --git a/rust/kona/tests/proofs/helpers/matrix.go b/rust/kona/tests/proofs/helpers/matrix.go index 9da40799cf8..eacb85d0b90 100644 --- a/rust/kona/tests/proofs/helpers/matrix.go +++ b/rust/kona/tests/proofs/helpers/matrix.go @@ -116,6 +116,7 @@ type ForkMatrix = []*Hardfork // Hardfork definitions var ( + Bedrock = &Hardfork{Name: string(forks.Bedrock), Precedence: 0} Regolith = &Hardfork{Name: string(forks.Regolith), Precedence: 1} Canyon = &Hardfork{Name: string(forks.Canyon), Precedence: 2} Delta = &Hardfork{Name: string(forks.Delta), Precedence: 3} @@ -129,7 +130,7 @@ var ( ) var ( - Hardforks = ForkMatrix{Regolith, Canyon, Delta, Ecotone, Fjord, Granite, Holocene, Isthmus, Jovian, Karst} + Hardforks = ForkMatrix{Bedrock, Regolith, Canyon, Delta, Ecotone, Fjord, Granite, Holocene, Isthmus, Jovian, Karst} LatestFork = Hardforks[len(Hardforks)-1] LatestForkOnly = ForkMatrix{LatestFork} ) @@ -141,7 +142,7 @@ func NewForkMatrix(forks ...*Hardfork) ForkMatrix { func FaultProofForks() ForkMatrix { var forks ForkMatrix for _, hf := range Hardforks { - if hf == Regolith || hf == Canyon || hf == Delta { + if hf == Bedrock || hf == Regolith || hf == Canyon || hf == Delta { continue } forks = append(forks, hf) diff --git a/rust/op-alloy/crates/consensus/src/transaction/deposit.rs b/rust/op-alloy/crates/consensus/src/transaction/deposit.rs index 68c36c1876b..13e3407dac5 100644 --- a/rust/op-alloy/crates/consensus/src/transaction/deposit.rs +++ b/rust/op-alloy/crates/consensus/src/transaction/deposit.rs @@ -36,12 +36,7 @@ pub struct TxDeposit { /// Field indicating if this transaction is exempt from the L2 gas limit. #[cfg_attr( feature = "serde", - serde( - default, - with = "alloy_serde::quantity", - rename = "isSystemTx", - skip_serializing_if = "core::ops::Not::not" - ) + serde(default, rename = "isSystemTx", skip_serializing_if = "core::ops::Not::not") )] pub is_system_transaction: bool, /// Input has two uses depending if transaction is Create or Call (if `to` field is None or diff --git a/rust/op-alloy/crates/rpc-types/src/transaction.rs b/rust/op-alloy/crates/rpc-types/src/transaction.rs index 8d1d8d9a891..774509e8ad3 100644 --- a/rust/op-alloy/crates/rpc-types/src/transaction.rs +++ b/rust/op-alloy/crates/rpc-types/src/transaction.rs @@ -396,6 +396,29 @@ mod tests { similar_asserts::assert_eq!(deserialized, expected); } + #[test] + fn can_roundtrip_bedrock_system_deposit() { + // op-geth eth_getBlockByHash response for a pre-Regolith L1-info deposit. + let rpc_tx = r#"{"blockHash":"0x2a3d4ceb37456815f793e8900fe9c863a15737bfbba646df4c51a5914149b4dd","blockNumber":"0xb","blockTimestamp":"0x6aad7010","from":"0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001","gas":"0x8f0d180","gasPrice":"0x0","hash":"0x67c97e03f0759855b20459dd9b5f9ddebdc094f3669bf039011252bae68f5f24","input":"0x015d8eb90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006aad7005000000000000000000000000000000000000000000000000000000003b9aca001b7ca7f22fd5e435bbc1f175d4e7721cee397530e7ef753563b0b8772ec58d86000000000000000000000000000000000000000000000000000000000000000b0000000000000000000000003c44cdddb6a900fa2b585dd299e03d12fa4293bc000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240","nonce":"0x0","to":"0x4200000000000000000000000000000000000015","transactionIndex":"0x0","value":"0x0","type":"0x7e","v":"0x0","r":"0x0","s":"0x0","sourceHash":"0xfd3be1af69bee3e315a5dc5b773c64432230d76db68edae7a64193522118a377","mint":"0x0","isSystemTx":true}"#; + let expected_hash = "0x67c97e03f0759855b20459dd9b5f9ddebdc094f3669bf039011252bae68f5f24" + .parse::() + .unwrap(); + + let tx = serde_json::from_str::(rpc_tx).unwrap(); + let deposit = tx.as_ref().as_deposit().expect("expected deposit transaction"); + assert!(deposit.is_system_transaction); + assert_eq!(deposit.hash(), expected_hash); + assert_eq!(deposit.inner().tx_hash(), expected_hash); + + let serialized = serde_json::to_value(&tx).unwrap(); + assert_eq!(serialized["isSystemTx"], serde_json::json!(true)); + let round_trip = serde_json::from_value::(serialized).unwrap(); + let deposit = + round_trip.as_ref().as_deposit().expect("expected deposit transaction after roundtrip"); + assert!(deposit.is_system_transaction); + assert_eq!(deposit.hash(), expected_hash); + } + #[test] fn can_serialize_post_exec_rpc_transaction() { let post_exec = build_post_exec_tx(42, vec![SDMGasEntry { index: 3, gas_refund: 7 }]);