|
| 1 | +package flashblocks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ethereum-optimism/optimism/op-chain-ops/pkg/sdm" |
| 11 | + "github.com/ethereum-optimism/optimism/op-devstack/devtest" |
| 12 | + "github.com/ethereum-optimism/optimism/op-devstack/presets" |
| 13 | + "github.com/ethereum-optimism/optimism/op-devstack/sysgo" |
| 14 | + "github.com/ethereum-optimism/optimism/op-service/bigs" |
| 15 | + "github.com/ethereum-optimism/optimism/op-service/eth" |
| 16 | + "github.com/ethereum-optimism/optimism/op-service/txplan" |
| 17 | +) |
| 18 | + |
| 19 | +// phantomMaxGasPerTxn caps op-rbuilder's per-tx gas used. A candidate over the cap is executed |
| 20 | +// (warming the slots it touches) then declined — the "executed but not committed" path that leaks |
| 21 | +// SDM warming when it isn't rolled back. Sized between the victim's gas and the poisoner's. |
| 22 | +const phantomMaxGasPerTxn = 200_000 |
| 23 | + |
| 24 | +// The declined "poisoner" call writes a superset of the committed "victim" call's slots, so the |
| 25 | +// victim re-touches slots that — in the canonical block — were only warmed by a tx that never |
| 26 | +// entered the block. |
| 27 | +const ( |
| 28 | + poisonerSlots = 30 // run(30) ≈ 0.68M gas > cap => executed then declined |
| 29 | + victimSlots = 4 // run(4) ≈ 0.11M gas < cap => committed |
| 30 | +) |
| 31 | + |
| 32 | +// TestFlashblocksSDMPhantomWarmingDivergence makes the op-rbuilder phantom-warming bug observable |
| 33 | +// at the acceptance level (ethereum-optimism/optimism#21354). |
| 34 | +// |
| 35 | +// SDM block-warming refunds are recorded during EVM execution (before the commit decision) and |
| 36 | +// aren't journaled. When op-rbuilder executes a candidate then declines it (here: over |
| 37 | +// `--builder.max_gas_per_txn`), the declined candidate's warming is (pre-fix) left behind, and a |
| 38 | +// later committed tx touching the same slots claims a "phantom" refund for a tx that never entered |
| 39 | +// the block. Commit-only paths (block import, `debug_replaySDMBlock` derivation) never see that |
| 40 | +// warmth. This test drives a persistently-declined poisoner alongside committed victims and, per |
| 41 | +// victim block, compares the producer-baked SDM payload against the commit-only derivation; they |
| 42 | +// must be identical. Pre-fix they diverge and the failure prints the phantom entries. |
| 43 | +func TestFlashblocksSDMPhantomWarmingDivergence(gt *testing.T) { |
| 44 | + t := devtest.SerialT(gt) |
| 45 | + sysgo.SkipOnKonaNode(t, "flashblocks acceptance preset requires user RPC") |
| 46 | + sysgo.SkipOnOpGeth(t, "SDM flashblocks require op-reth post-exec support") |
| 47 | + |
| 48 | + // Cap per-tx gas so the poisoner is declined post-execution (the warming-leak path). SDM rides |
| 49 | + // Interop; activating it at genesis turns SDM on across op-reth, op-rbuilder, and op-node. |
| 50 | + sys := presets.NewSingleChainWithFlashblocks(t, |
| 51 | + presets.WithInteropAtGenesis(), |
| 52 | + presets.WithOPRBuilderOption(sysgo.OPRBuilderNodeWithExtraArgs( |
| 53 | + fmt.Sprintf("--builder.max_gas_per_txn=%d", phantomMaxGasPerTxn), |
| 54 | + )), |
| 55 | + ) |
| 56 | + |
| 57 | + driveViaTestSequencer(t, sys, 2) |
| 58 | + |
| 59 | + // Opt SDM PostExec production in on both the sequencer EL (op-reth fallback) and op-rbuilder; |
| 60 | + // the flag is process-local and starts disabled on boot. See flashblocks_sdm_test.go. |
| 61 | + setFlashblocksSDMEnabled(t, sys.L2EL.Escape().L2EthClient().RPC(), true) |
| 62 | + setFlashblocksSDMEnabled(t, sys.L2OPRBuilder.Escape().L2EthClient().RPC(), true) |
| 63 | + |
| 64 | + rpc := sys.L2EL.Escape().L2EthClient().RPC() |
| 65 | + |
| 66 | + // Deploy the StateBloat contract; run(n) writes slots [0, n). |
| 67 | + deployer := sys.FunderL2.NewFundedEOA(eth.OneEther) |
| 68 | + stateBloat := flashblocksDeployContract(t, deployer, sdm.StateBloatBin) |
| 69 | + |
| 70 | + // The poisoner: one persistent, top-priority call that always exceeds the gas cap, so op-rbuilder |
| 71 | + // re-declines it every block — warming the poisoner's slots before any victim commits. A declined |
| 72 | + // candidate isn't marked invalid, so it lingers in the mempool and keeps poisoning blocks. It's |
| 73 | + // never included, so we don't wait for it. |
| 74 | + poisoner := sys.FunderL2.NewFundedEOA(eth.OneEther) |
| 75 | + flashblocksSubmitTxWithoutWait(t, poisoner, poisoner.PendingNonce(), |
| 76 | + txplan.WithTo(&stateBloat), |
| 77 | + txplan.WithData(sdm.EncodeRun(poisonerSlots)), |
| 78 | + txplan.WithGasLimit(1_500_000), |
| 79 | + // Highest priority so op-rbuilder processes (and declines) it first in each block. |
| 80 | + txplan.WithGasTipCap(big.NewInt(5_000_000_000)), |
| 81 | + txplan.WithGasFeeCap(big.NewInt(500_000_000_000)), |
| 82 | + ) |
| 83 | + |
| 84 | + // The victims: small committed calls that re-touch the poisoner's slots, sent one at a time |
| 85 | + // (waiting for each receipt) so each lands in its own block behind the declined poisoner. |
| 86 | + const victimCount = 6 |
| 87 | + victim := sys.FunderL2.NewFundedEOA(eth.OneEther) |
| 88 | + victimBlocks := make([]uint64, 0, victimCount) |
| 89 | + seen := make(map[uint64]bool) |
| 90 | + for i := range victimCount { |
| 91 | + ptx := txplan.NewPlannedTx( |
| 92 | + victim.Plan(), |
| 93 | + txplan.WithNonce(victim.PendingNonce()), |
| 94 | + txplan.WithTo(&stateBloat), |
| 95 | + txplan.WithData(sdm.EncodeRun(victimSlots)), |
| 96 | + txplan.WithGasLimit(400_000), |
| 97 | + ) |
| 98 | + receipt, err := ptx.Included.Eval(t.Ctx()) |
| 99 | + t.Require().NoError(err, "victim %d must be included", i) |
| 100 | + bn := bigs.Uint64Strict(receipt.BlockNumber) |
| 101 | + if !seen[bn] { |
| 102 | + seen[bn] = true |
| 103 | + victimBlocks = append(victimBlocks, bn) |
| 104 | + } |
| 105 | + } |
| 106 | + slices.Sort(victimBlocks) |
| 107 | + t.Require().NotEmpty(victimBlocks, "expected at least one victim block") |
| 108 | + |
| 109 | + // Compare producer-baked vs commit-only-derived SDM payload for each victim block. |
| 110 | + var divergentBlocks []uint64 |
| 111 | + var viz strings.Builder |
| 112 | + for _, bn := range victimBlocks { |
| 113 | + replay, err := sdm.ReplayBlockWithSDM(t.Ctx(), rpc, bn, true) |
| 114 | + t.Require().NoError(err, "debug_replaySDMBlock(%d)", bn) |
| 115 | + if !replay.PostExecTxPresent { |
| 116 | + // No SDM post-exec tx in this block (no committed warm re-touch landed here); skip. |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + producer := entryMap(replay.EmbeddedPayload) |
| 121 | + derived := entryMap(&replay.SynthesizedPayload) |
| 122 | + if payloadsEqual(producer, derived) { |
| 123 | + continue |
| 124 | + } |
| 125 | + divergentBlocks = append(divergentBlocks, bn) |
| 126 | + writeBlockViz(&viz, bn, replay, producer, derived) |
| 127 | + } |
| 128 | + |
| 129 | + t.Logger().Info("SDM phantom-warming scan complete", |
| 130 | + "victim_blocks", len(victimBlocks), |
| 131 | + "divergent_blocks", len(divergentBlocks)) |
| 132 | + |
| 133 | + t.Require().Empty(divergentBlocks, |
| 134 | + "producer-baked SDM payload diverges from commit-only derivation in %d block(s) — "+ |
| 135 | + "op-rbuilder credited phantom warming refunds from a declined (uncommitted) candidate "+ |
| 136 | + "(ethereum-optimism/optimism#21354):\n%s", len(divergentBlocks), viz.String()) |
| 137 | +} |
| 138 | + |
| 139 | +// entryMap indexes an SDM payload's refund entries by tx index. A nil payload yields an empty map. |
| 140 | +func entryMap(p *sdm.PostExecPayload) map[uint64]uint64 { |
| 141 | + out := make(map[uint64]uint64) |
| 142 | + if p == nil { |
| 143 | + return out |
| 144 | + } |
| 145 | + for _, e := range p.GasRefundEntries { |
| 146 | + out[e.Index] = e.GasRefund |
| 147 | + } |
| 148 | + return out |
| 149 | +} |
| 150 | + |
| 151 | +func payloadsEqual(a, b map[uint64]uint64) bool { |
| 152 | + if len(a) != len(b) { |
| 153 | + return false |
| 154 | + } |
| 155 | + for idx, v := range a { |
| 156 | + if b[idx] != v { |
| 157 | + return false |
| 158 | + } |
| 159 | + } |
| 160 | + return true |
| 161 | +} |
| 162 | + |
| 163 | +// writeBlockViz renders a per-tx table contrasting the producer-baked refund against the |
| 164 | +// commit-only-derived refund, flagging phantom entries (present in the producer payload, absent or |
| 165 | +// smaller in derivation). |
| 166 | +func writeBlockViz(w *strings.Builder, bn uint64, replay *sdm.ReplaySDMBlock, producer, derived map[uint64]uint64) { |
| 167 | + fmt.Fprintf(w, "block %d (hash %s):\n", bn, replay.BlockHash) |
| 168 | + fmt.Fprintf(w, " embedded(producer) entries=%d synthesized(derivation) entries=%d mismatches=%d\n", |
| 169 | + len(producer), len(derived), len(replay.Mismatches)) |
| 170 | + fmt.Fprintf(w, " %-9s %-18s %-18s %s\n", "tx_index", "producer_refund", "derived_refund", "") |
| 171 | + idxs := unionKeys(producer, derived) |
| 172 | + for _, idx := range idxs { |
| 173 | + p, pOK := producer[idx] |
| 174 | + d, dOK := derived[idx] |
| 175 | + flag := "" |
| 176 | + if pOK && (!dOK || d < p) { |
| 177 | + flag = "<- phantom" |
| 178 | + } |
| 179 | + fmt.Fprintf(w, " %-9d %-18d %-18d %s\n", idx, p, d, flag) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func unionKeys(a, b map[uint64]uint64) []uint64 { |
| 184 | + set := make(map[uint64]struct{}) |
| 185 | + for k := range a { |
| 186 | + set[k] = struct{}{} |
| 187 | + } |
| 188 | + for k := range b { |
| 189 | + set[k] = struct{}{} |
| 190 | + } |
| 191 | + out := make([]uint64, 0, len(set)) |
| 192 | + for k := range set { |
| 193 | + out = append(out, k) |
| 194 | + } |
| 195 | + slices.Sort(out) |
| 196 | + return out |
| 197 | +} |
0 commit comments