|
| 1 | +use reth::revm::{ |
| 2 | + Inspector, |
| 3 | + interpreter::{ |
| 4 | + interpreter::EthInterpreter, Gas, Interpreter, InterpreterAction, InstructionResult, |
| 5 | + }, |
| 6 | +}; |
| 7 | + |
| 8 | +/// Inspector that aborts a transaction once it executes more than `limit` `JUMPDEST` opcodes. |
| 9 | +/// |
| 10 | +/// Usage sketch (leave commented out until you want to enable it): |
| 11 | +/// - In `TaikoEvmFactory::create_evm_with_inspector`, swap the `NoOpInspector` for |
| 12 | +/// `JumpdestLimiter::new(100)` and set the wrapper's `inspect` flag to `true` so every tx is |
| 13 | +/// executed with this inspector. |
| 14 | +/// - In payload building (`crates/payload/src/builder.rs`) or block execution, match on the EVM |
| 15 | +/// error and `continue` to drop the offending tx instead of failing the whole payload: |
| 16 | +/// ``` |
| 17 | +/// // if let Err(BlockExecutionError::Evm { error, .. }) = builder.execute_transaction(tx.clone()) { |
| 18 | +/// // if error.is_fatal_external_error() { |
| 19 | +/// // // JUMPDEST limit hit; skip this tx |
| 20 | +/// // continue; |
| 21 | +/// // } |
| 22 | +/// // } |
| 23 | +/// ``` |
| 24 | +#[derive(Debug, Clone)] |
| 25 | +pub struct JumpdestLimiter { |
| 26 | + limit: u64, |
| 27 | + count: u64, |
| 28 | +} |
| 29 | + |
| 30 | +impl JumpdestLimiter { |
| 31 | + pub const fn new(limit: u64) -> Self { |
| 32 | + Self { limit, count: 0 } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<CTX> Inspector<CTX, EthInterpreter> for JumpdestLimiter { |
| 37 | + fn initialize_interp(&mut self, _interp: &mut Interpreter<EthInterpreter>, _ctx: &mut CTX) { |
| 38 | + // Reset per top-level transaction. |
| 39 | + self.count = 0; |
| 40 | + } |
| 41 | + |
| 42 | + fn step(&mut self, interp: &mut Interpreter<EthInterpreter>, _ctx: &mut CTX) { |
| 43 | + const JUMPDEST: u8 = 0x5b; |
| 44 | + if interp.bytecode.opcode() == JUMPDEST { |
| 45 | + self.count += 1; |
| 46 | + if self.count > self.limit { |
| 47 | + // Halt execution immediately; upstream sees a fatal external error for this tx. |
| 48 | + interp.bytecode.set_action(InterpreterAction::new_halt( |
| 49 | + InstructionResult::FatalExternalError, |
| 50 | + Gas::new(0), |
| 51 | + )); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments