moonproof_runtime/executable.rs
1//! Data types describing the result of running a contract's
2//! constructor or a provable circuit off-chain.
3//!
4//! There is one off-chain executor for every Compact contract:
5//! [`moonproof_runtime_ir_driver::IrExecutable<W>`], which interprets a
6//! `contract.mpir` (emitted by the patched compactc fork) at runtime.
7//! That executor's inherent `initialize` and `execute_circuit` methods
8//! return the data types defined here, which the deploy / call builders
9//! then assemble into ledger-typed transactions.
10//!
11//! Private state and constructor / circuit arguments are
12//! `serde_json::Value`. The IR is dynamic — its argument descriptors
13//! tell the executor how to encode each `Value` into the corresponding
14//! `AlignedValue` for the on-chain VM. There is no per-contract typed
15//! `PrivateState` parameter and no per-contract typed `ConstructorArgs`
16//! parameter; the IR's own descriptors are the typing.
17
18use moonproof_ledger::base_crypto::fab::AlignedValue;
19use moonproof_ledger::base_crypto::time::Timestamp;
20use moonproof_ledger::coin_structure::contract::ContractAddress;
21use moonproof_ledger::ledger::structure::LedgerParameters;
22use moonproof_ledger::onchain_runtime::ops::Op;
23use moonproof_ledger::onchain_runtime::result_mode::ResultModeGather;
24use moonproof_ledger::onchain_runtime::state::ContractState;
25use moonproof_ledger::signatures::SigningKey;
26use moonproof_ledger::storage::db::InMemoryDB;
27use moonproof_runtime_ir::RuntimeValue as Value;
28
29use crate::types::CoinPublicKey;
30use crate::zswap_local::CompactZswapLocalState;
31
32/// The result of running a circuit off-chain — everything needed to
33/// assemble a `ContractDeploy` / `ContractCallPrototype` downstream.
34#[derive(Debug, Clone)]
35pub struct ExecuteCircuitResult {
36 /// What the call claimed and moved: nullifiers, coin receives and
37 /// spends, contract calls, mints and unshielded transfers, as the
38 /// ledger checks them against the transaction.
39 pub effects: moonproof_ledger::onchain_runtime::context::Effects<InMemoryDB>,
40 /// The contract state after circuit execution.
41 pub next_contract_state: ContractState<InMemoryDB>,
42 /// The private state after circuit execution. Walked by the
43 /// witness callbacks; the host serializes it for persistence.
44 pub next_private_state: Value,
45 /// The compact-runtime zswap local state after circuit execution
46 /// — every coin the circuit emitted as an `output` (and every
47 /// input it consumed). Translated into a ledger-typed
48 /// `Offer<ProofPreimage>` by
49 /// [`crate::utils::zswap_utils::zswap_state_to_offer`] when the
50 /// call-tx pipeline wraps the prototype in a `Transaction`.
51 pub next_zswap_local_state: CompactZswapLocalState,
52 /// Contract events the circuit emitted (MIP-0002), in emission
53 /// order across the whole execution — a cross-contract callee's
54 /// events appear inline with the caller's, matching midnight-js's
55 /// execution-wide `CallResultPublic.events`.
56 pub emitted_events: Vec<crate::ir::interp::EmittedEvent>,
57 /// Field-element representation of the circuit arguments.
58 pub input: AlignedValue,
59 /// Field-element representation of the circuit output.
60 pub output: AlignedValue,
61 /// The typed circuit return value as JSON — the IR's typed
62 /// return descriptor decoded back into a `Value`.
63 pub result: Value,
64 /// The full public transcript emitted by the circuit, in
65 /// `ResultModeGather` mode (inline results alongside reads).
66 pub public_transcript: Vec<Op<ResultModeGather, InMemoryDB>>,
67 /// Private-witness outputs the circuit emitted, in evaluation
68 /// order.
69 pub private_transcript_outputs: Vec<AlignedValue>,
70 /// Read results gathered while executing the circuit's
71 /// `Op::Popeq` ops, in the order they fired.
72 ///
73 /// This is the gather-mode `QueryResults::events` equivalent —
74 /// every `Op::Popeq` in `public_transcript` consumes one entry
75 /// from this `Vec` when the gather→verify translation runs.
76 pub popeq_read_results: Vec<AlignedValue>,
77 /// Structured execution trace recorded when tracing was enabled on the
78 /// executor; `None` when tracing is [`crate::TraceLevel::Off`].
79 pub trace: Option<crate::trace::ExecutionTrace>,
80}
81
82/// The result of running a contract's constructor.
83#[derive(Debug, Clone)]
84pub struct InitializeResult {
85 /// The initial contract state the deploy will install.
86 pub initial_contract_state: ContractState<InMemoryDB>,
87 /// The initial private state. The witness callbacks may rotate
88 /// this during constructor execution; the result here is the
89 /// final value the host should persist alongside the deploy.
90 pub initial_private_state: Value,
91 /// The initial zswap local state — any coins the constructor
92 /// produced.
93 pub initial_zswap_local_state: CompactZswapLocalState,
94}
95
96/// Call-site context provided to the executor's `execute_circuit`.
97#[derive(Debug, Clone)]
98pub struct CircuitCallContext {
99 /// The address of the contract whose circuit is being executed.
100 pub contract_address: ContractAddress,
101 /// The current (pre-execution) contract state.
102 pub contract_state: ContractState<InMemoryDB>,
103 /// The current private state. The circuit may read and rotate it
104 /// via witness callbacks; the rotated value is returned in
105 /// [`ExecuteCircuitResult::next_private_state`].
106 pub private_state: Value,
107 /// Ledger parameters in effect at call time. The VM reads block
108 /// time etc. from these.
109 pub ledger_parameters: LedgerParameters,
110 /// The caller's coin public key. Used when the circuit emits
111 /// outputs addressed back to the caller.
112 pub coin_public_key: CoinPublicKey,
113 /// Block time to stamp on the call context. Defaults to
114 /// `Timestamp::from_secs(0)` if the caller has no better value.
115 pub tblock: Timestamp,
116}
117
118/// Constructor context — the subset of [`CircuitCallContext`] that's
119/// meaningful before the contract has been deployed (no address yet,
120/// no on-chain ledger parameters to read).
121#[derive(Debug, Clone)]
122pub struct ConstructorContext {
123 /// The initial private state to run the constructor against.
124 pub initial_private_state: Value,
125 /// The constructor's initial zswap local state — typically empty.
126 pub initial_zswap_local_state: CompactZswapLocalState,
127 /// The caller's coin public key.
128 pub coin_public_key: CoinPublicKey,
129 /// The signing key being registered as the contract's maintenance
130 /// authority. The constructor embeds this into the initial
131 /// contract state.
132 pub signing_key: SigningKey,
133 /// The block time the constructor sees; `None` is the wall clock, as
134 /// compact-runtime defaults to. A simulator passes its own clock.
135 pub time: Option<moonproof_ledger::base_crypto::time::Timestamp>,
136}