moonproof-runtime 0.2.1

Wasm-friendly Compact contract runtime core — IR walker, IR-driven circuit executor, contract state, runtime values
Documentation
//! Data types describing the result of running a contract's
//! constructor or a provable circuit off-chain.
//!
//! There is one off-chain executor for every Compact contract:
//! [`moonproof_runtime_ir_driver::IrExecutable<W>`], which interprets a
//! `contract.mpir` (emitted by the patched compactc fork) at runtime.
//! That executor's inherent `initialize` and `execute_circuit` methods
//! return the data types defined here, which the deploy / call builders
//! then assemble into ledger-typed transactions.
//!
//! Private state and constructor / circuit arguments are
//! `serde_json::Value`. The IR is dynamic — its argument descriptors
//! tell the executor how to encode each `Value` into the corresponding
//! `AlignedValue` for the on-chain VM. There is no per-contract typed
//! `PrivateState` parameter and no per-contract typed `ConstructorArgs`
//! parameter; the IR's own descriptors are the typing.

use moonproof_ledger::base_crypto::fab::AlignedValue;
use moonproof_ledger::base_crypto::time::Timestamp;
use moonproof_ledger::coin_structure::contract::ContractAddress;
use moonproof_ledger::ledger::structure::LedgerParameters;
use moonproof_ledger::onchain_runtime::ops::Op;
use moonproof_ledger::onchain_runtime::result_mode::ResultModeGather;
use moonproof_ledger::onchain_runtime::state::ContractState;
use moonproof_ledger::signatures::SigningKey;
use moonproof_ledger::storage::db::InMemoryDB;
use moonproof_runtime_ir::RuntimeValue as Value;

use crate::types::CoinPublicKey;
use crate::zswap_local::CompactZswapLocalState;

/// The result of running a circuit off-chain — everything needed to
/// assemble a `ContractDeploy` / `ContractCallPrototype` downstream.
#[derive(Debug, Clone)]
pub struct ExecuteCircuitResult {
    /// What the call claimed and moved: nullifiers, coin receives and
    /// spends, contract calls, mints and unshielded transfers, as the
    /// ledger checks them against the transaction.
    pub effects: moonproof_ledger::onchain_runtime::context::Effects<InMemoryDB>,
    /// The contract state after circuit execution.
    pub next_contract_state: ContractState<InMemoryDB>,
    /// The private state after circuit execution. Walked by the
    /// witness callbacks; the host serializes it for persistence.
    pub next_private_state: Value,
    /// The compact-runtime zswap local state after circuit execution
    /// — every coin the circuit emitted as an `output` (and every
    /// input it consumed). Translated into a ledger-typed
    /// `Offer<ProofPreimage>` by
    /// [`crate::utils::zswap_utils::zswap_state_to_offer`] when the
    /// call-tx pipeline wraps the prototype in a `Transaction`.
    pub next_zswap_local_state: CompactZswapLocalState,
    /// Contract events the circuit emitted (MIP-0002), in emission
    /// order across the whole execution — a cross-contract callee's
    /// events appear inline with the caller's, matching midnight-js's
    /// execution-wide `CallResultPublic.events`.
    pub emitted_events: Vec<crate::ir::interp::EmittedEvent>,
    /// Field-element representation of the circuit arguments.
    pub input: AlignedValue,
    /// Field-element representation of the circuit output.
    pub output: AlignedValue,
    /// The typed circuit return value as JSON — the IR's typed
    /// return descriptor decoded back into a `Value`.
    pub result: Value,
    /// The full public transcript emitted by the circuit, in
    /// `ResultModeGather` mode (inline results alongside reads).
    pub public_transcript: Vec<Op<ResultModeGather, InMemoryDB>>,
    /// Private-witness outputs the circuit emitted, in evaluation
    /// order.
    pub private_transcript_outputs: Vec<AlignedValue>,
    /// Read results gathered while executing the circuit's
    /// `Op::Popeq` ops, in the order they fired.
    ///
    /// This is the gather-mode `QueryResults::events` equivalent —
    /// every `Op::Popeq` in `public_transcript` consumes one entry
    /// from this `Vec` when the gather→verify translation runs.
    pub popeq_read_results: Vec<AlignedValue>,
    /// Structured execution trace recorded when tracing was enabled on the
    /// executor; `None` when tracing is [`crate::TraceLevel::Off`].
    pub trace: Option<crate::trace::ExecutionTrace>,
}

/// The result of running a contract's constructor.
#[derive(Debug, Clone)]
pub struct InitializeResult {
    /// The initial contract state the deploy will install.
    pub initial_contract_state: ContractState<InMemoryDB>,
    /// The initial private state. The witness callbacks may rotate
    /// this during constructor execution; the result here is the
    /// final value the host should persist alongside the deploy.
    pub initial_private_state: Value,
    /// The initial zswap local state — any coins the constructor
    /// produced.
    pub initial_zswap_local_state: CompactZswapLocalState,
}

/// Call-site context provided to the executor's `execute_circuit`.
#[derive(Debug, Clone)]
pub struct CircuitCallContext {
    /// The address of the contract whose circuit is being executed.
    pub contract_address: ContractAddress,
    /// The current (pre-execution) contract state.
    pub contract_state: ContractState<InMemoryDB>,
    /// The current private state. The circuit may read and rotate it
    /// via witness callbacks; the rotated value is returned in
    /// [`ExecuteCircuitResult::next_private_state`].
    pub private_state: Value,
    /// Ledger parameters in effect at call time. The VM reads block
    /// time etc. from these.
    pub ledger_parameters: LedgerParameters,
    /// The caller's coin public key. Used when the circuit emits
    /// outputs addressed back to the caller.
    pub coin_public_key: CoinPublicKey,
    /// Block time to stamp on the call context. Defaults to
    /// `Timestamp::from_secs(0)` if the caller has no better value.
    pub tblock: Timestamp,
}

/// Constructor context — the subset of [`CircuitCallContext`] that's
/// meaningful before the contract has been deployed (no address yet,
/// no on-chain ledger parameters to read).
#[derive(Debug, Clone)]
pub struct ConstructorContext {
    /// The initial private state to run the constructor against.
    pub initial_private_state: Value,
    /// The constructor's initial zswap local state — typically empty.
    pub initial_zswap_local_state: CompactZswapLocalState,
    /// The caller's coin public key.
    pub coin_public_key: CoinPublicKey,
    /// The signing key being registered as the contract's maintenance
    /// authority. The constructor embeds this into the initial
    /// contract state.
    pub signing_key: SigningKey,
    /// The block time the constructor sees; `None` is the wall clock, as
    /// compact-runtime defaults to. A simulator passes its own clock.
    pub time: Option<moonproof_ledger::base_crypto::time::Timestamp>,
}