Skip to content

add ContractExecutor dispatch enum (Aot + Emu)#1612

Open
avi-starkware wants to merge 2 commits into
avi/cairo_native/syscall-types-cratefrom
avi/cairo_native/contract-executor-2
Open

add ContractExecutor dispatch enum (Aot + Emu)#1612
avi-starkware wants to merge 2 commits into
avi/cairo_native/syscall-types-cratefrom
avi/cairo_native/contract-executor-2

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

Summary

Adds a public dispatch enum so a single call site can pick between cairo-native's AOT executor and the sierra-emu interpreter at runtime, without forcing every caller to maintain its own match.

Replaces #1598 / #1608. The new version drops SierraEmuSyscallBridge entirely — the prior PR in this stack (extracting cairo-native-syscalls) made cairo-native's and sierra-emu's StarknetSyscallHandler the same trait, so the handler flows through both paths unchanged.

Changes

  • src/executor/contract_executor.rs (new):
    • pub enum ContractExecutor { Aot(AotContractExecutor), Emu(EmuContractInfo) }, with Emu gated on the new sierra-emu cargo feature.
    • pub struct EmuContractInfo { program: Arc<Program>, entry_points: …, sierra_version: … }. The Arc<Program> is shared across invocations rather than cloned per call.
    • From impls for both variants.
    • ContractExecutor::run dispatches:
      • Aot arm → AotContractExecutor::run.
      • Emu arm → sierra_emu::VirtualMachine::new_starknet(…).run(&mut syscall_handler). No bridge wrapping the handler.
    • convert_builtin_costs translates cairo_native::utils::BuiltinCostssierra_emu::BuiltinCosts (these types are still separate; only the syscall handler shape converged).
  • src/executor.rs: register the new module and re-export ContractExecutor (+ EmuContractInfo under the sierra-emu feature).
  • Cargo.toml:
    • new sierra-emu = ["dep:sierra-emu"] feature.
    • with-trace-dump now depends on sierra-emu rather than the optional dep directly, so the two paths agree.

Diff against the original #1608

Aspect #1608 (closed) this PR
Handler in Emu arm wrapped in SierraEmuSyscallBridge passed directly
Bridge file required doesn't exist
Type conversions convert_u256 / convert_secp_*_point / convert_execution_info{,_v2} none (types are identical via re-export)

Stack

  1. align sierra-emu StarknetSyscallHandler trait/types with cairo-native #1610 — trait alignment
  2. extract shared cairo-native-syscalls crate #1611 — extract shared crate
  3. this PRContractExecutor (supersedes add ContractExecutor dispatch enum (Aot + Emu) #1598 / add ContractExecutor dispatch enum (Aot + Emu) #1608)
  4. (next) run_with_libfunc_profile + AotWithProgram (supersedes add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1599 / add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1609)

Test plan

  • cargo check (default features) clean
  • cargo check --features sierra-emu clean
  • cargo check --features with-trace-dump clean (transitively activates sierra-emu)
  • cargo check --workspace --all-features clean
  • CI green

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 14, 2026

✅ Code is now correctly formatted.

@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from b3a574c to 319a6be Compare May 14, 2026 12:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 98ed085 to 64c12b3 Compare May 14, 2026 12:26
avi-starkware and others added 2 commits May 14, 2026 15:31
Adds a public dispatch enum so a single call site can pick between the
AOT executor and the sierra-emu interpreter at runtime, without forcing
every caller to maintain its own match.

- ContractExecutor { Aot(AotContractExecutor), Emu(EmuContractInfo) },
  with Emu gated on the new sierra-emu cargo feature.
- EmuContractInfo carries Arc<Program> so the program is shared across
  invocations rather than cloned per call.
- ContractExecutor::run dispatches: Aot delegates to
  AotContractExecutor::run; Emu constructs a sierra_emu::VirtualMachine
  and runs it with the caller's syscall handler directly. No adapter is
  needed: the trait is shared via the cairo-native-syscalls crate.
- Cargo: new `sierra-emu` feature (= ["dep:sierra-emu"]). The existing
  `with-trace-dump` feature now activates `sierra-emu` instead of the
  optional dep directly.

Companion to the bridge-free design: with cairo-native and sierra-emu
sharing one trait, the SierraEmuSyscallBridge that PR #1597 / #1607
introduced is no longer necessary.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The Emu arm previously called .expect() on VirtualMachine::run, which
aborts the host on any internal VM failure. The Aot arm propagates
errors via Result; align the Emu arm with it.

VirtualMachine::run returns Option<ContractExecutionResult> (None means
"VM never produced a final state"). Convert None to
Error::UnexpectedValue with a descriptive message so callers can decide
how to handle.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 319a6be to 92ecf03 Compare May 14, 2026 12:33
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 64c12b3 to 7e5fcd7 Compare May 14, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant