Skip to content

add ContractExecutor dispatch enum (Aot + Emu)#1612

Open
avi-starkware wants to merge 3 commits into
mainfrom
avi/cairo_native/contract-executor-2
Open

add ContractExecutor dispatch enum (Aot + Emu)#1612
avi-starkware wants to merge 3 commits into
mainfrom
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 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
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 92ecf03 to 3b3e5af Compare May 17, 2026 11:57
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 7e5fcd7 to 4176352 Compare May 17, 2026 11:57
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 3b3e5af to 24f61ba Compare May 17, 2026 12:08
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 4176352 to 7af0508 Compare May 17, 2026 12:08
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 24f61ba to 0ae0d06 Compare May 17, 2026 13:24
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 7af0508 to 38c5f2f Compare May 17, 2026 13:24
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 0ae0d06 to 0791b36 Compare May 17, 2026 13:27
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 38c5f2f to 81093b3 Compare May 17, 2026 13:27
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 0791b36 to 4e80562 Compare May 17, 2026 13:58
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch 2 times, most recently from 4f53f27 to 6f77130 Compare May 20, 2026 09:43
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 4e80562 to 34f9e15 Compare May 20, 2026 09:43
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 34f9e15 to eec22ef Compare May 28, 2026 14:56
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch 2 times, most recently from aa4bf47 to e6a4ad9 Compare May 31, 2026 12:03
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from eec22ef to 487deb9 Compare May 31, 2026 12:03
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from e6a4ad9 to 538f655 Compare May 31, 2026 13:33
Both cairo-native and sierra-emu now re-export the StarknetSyscallHandler
trait and supporting types (U256, Block/Tx/Execution Info v{1,2,3},
ResourceBounds, Secp256{k1,r1}Point) from the new
`cairo-starknet-syscalls` crate. The crate is workspace-local at the
top level (sibling to `test_utils/`).

The two crates' types are now nominally identical, so a single handler
impl can drive both runtimes without an adapter / bridge.

Notes:
- `cairo-starknet-syscalls` rather than `cairo-native-syscalls`: the
  crate's content doesn't depend on cairo-native semantically; the
  cairo-native prefix would imply ownership it doesn't have.
- StubSyscallHandler's `unimplemented!()` stubs now carry a one-line
  rationale each (deploy needs constructor execution, call_contract
  needs address-to-class resolution, send_message_to_l1 needs an L1
  queue, etc.), plus a scope doc-comment on the type itself.
- sierra-emu's previous inherent into_value/from_value methods (which
  depend on sierra-emu's `Value` enum and can't follow the moved types)
  become free functions in the new sub-module
  `sierra_emu::starknet::value_conv`, each with a doc comment.
- `cheatcode` trait method's default impl returns an error felt instead
  of panicking with unimplemented!(). On the sierra-emu path, a contract
  invoking the cheatcode libfunc against a handler that didn't override
  it would otherwise unwind through the VM and abort the host;
  production handlers (e.g. blockifier's NativeSyscallHandler) don't
  implement cheatcode, so the default is what runs.
- The per-type files under debug_utils/sierra-emu/src/starknet/* are
  deleted now that the struct definitions live in the shared crate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
avi-starkware and others added 2 commits June 1, 2026 14:25
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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-starknet-syscalls crate.
  VirtualMachine::run's `Option` return is propagated as
  Error::UnexpectedValue rather than `.expect()`-aborted, matching the
  Aot arm's error-handling style.
- 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>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 538f655 to c8ed1f7 Compare June 1, 2026 11:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 701a87c to 61059c1 Compare June 1, 2026 11:26
Base automatically changed from avi/cairo_native/syscall-types-crate to main June 1, 2026 13:59
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