moonproof-runtime 0.2.1

Wasm-friendly Compact contract runtime core — IR walker, IR-driven circuit executor, contract state, runtime values
Documentation
//! Build a `StateValue` from the IR's `StateValue` JSON tree.
//!
//! The IR's `constructor.initial-state` is a `StateValue` tree with
//! `kind: "null" | "cell" | "array" | "map" | "merkle-tree"` leaves.
//! This module translates that into the ledger's
//! `moonproof_ledger::onchain_runtime::state::StateValue` representation.
//!
//! Today this handles `null` and `array` (the registry-shape skeleton —
//! Array of Null leaves). `cell` with literal value, `map`, and
//! `merkle-tree` are planned next slices.

use moonproof_ledger::onchain_runtime::state::StateValue;
use moonproof_ledger::storage::db::InMemoryDB;
use moonproof_ledger::storage::storage::Array;
use moonproof_runtime_ir::ir::StateValue as IrStateValue;

use super::error::{IrDriverError, Result};

/// Build a ledger `StateValue` from the IR's `StateValue` description.
pub fn build_state_value(ir: &IrStateValue) -> Result<StateValue<InMemoryDB>> {
    match ir {
        IrStateValue::Null => Ok(StateValue::Null),
        IrStateValue::Array { items } => {
            let inner: Result<Vec<StateValue<InMemoryDB>>> =
                items.iter().map(build_state_value).collect();
            Ok(StateValue::Array(Array::from(inner?)))
        }
        IrStateValue::Cell { value, descriptor } => {
            // For descriptor-typed cells the IR emitter writes the
            // descriptor id into the Cell node so consumers can
            // recover the alignment shape. Today we only see
            // descriptor-less Cells in `init-ops`' VM op `push.value`
            // form (which doesn't reach this code path); the
            // constructor's initial-state is always an Array of
            // Nulls for the contract shapes we cover. Defensive
            // bail: surface the case if it shows up.
            Err(IrDriverError::unimplemented(format!(
                "build_state_value for kind=cell (descriptor={descriptor:?}, value={value}); \
                 covered shapes only need null + array"
            )))
        }
        IrStateValue::CellExpr { .. } => Err(IrDriverError::unimplemented(
            "build_state_value for kind=cell-expr",
        )),
        IrStateValue::Map { .. } => Err(IrDriverError::unimplemented(
            "build_state_value for kind=map",
        )),
        IrStateValue::MerkleTree { .. } => Err(IrDriverError::unimplemented(
            "build_state_value for kind=merkle-tree",
        )),
    }
}