moonproof_runtime/ir/state_value.rs
1//! Build a `StateValue` from the IR's `StateValue` JSON tree.
2//!
3//! The IR's `constructor.initial-state` is a `StateValue` tree with
4//! `kind: "null" | "cell" | "array" | "map" | "merkle-tree"` leaves.
5//! This module translates that into the ledger's
6//! `moonproof_ledger::onchain_runtime::state::StateValue` representation.
7//!
8//! Today this handles `null` and `array` (the registry-shape skeleton —
9//! Array of Null leaves). `cell` with literal value, `map`, and
10//! `merkle-tree` are planned next slices.
11
12use moonproof_ledger::onchain_runtime::state::StateValue;
13use moonproof_ledger::storage::db::InMemoryDB;
14use moonproof_ledger::storage::storage::Array;
15use moonproof_runtime_ir::ir::StateValue as IrStateValue;
16
17use super::error::{IrDriverError, Result};
18
19/// Build a ledger `StateValue` from the IR's `StateValue` description.
20pub fn build_state_value(ir: &IrStateValue) -> Result<StateValue<InMemoryDB>> {
21 match ir {
22 IrStateValue::Null => Ok(StateValue::Null),
23 IrStateValue::Array { items } => {
24 let inner: Result<Vec<StateValue<InMemoryDB>>> =
25 items.iter().map(build_state_value).collect();
26 Ok(StateValue::Array(Array::from(inner?)))
27 }
28 IrStateValue::Cell { value, descriptor } => {
29 // For descriptor-typed cells the IR emitter writes the
30 // descriptor id into the Cell node so consumers can
31 // recover the alignment shape. Today we only see
32 // descriptor-less Cells in `init-ops`' VM op `push.value`
33 // form (which doesn't reach this code path); the
34 // constructor's initial-state is always an Array of
35 // Nulls for the contract shapes we cover. Defensive
36 // bail: surface the case if it shows up.
37 Err(IrDriverError::unimplemented(format!(
38 "build_state_value for kind=cell (descriptor={descriptor:?}, value={value}); \
39 covered shapes only need null + array"
40 )))
41 }
42 IrStateValue::CellExpr { .. } => Err(IrDriverError::unimplemented(
43 "build_state_value for kind=cell-expr",
44 )),
45 IrStateValue::Map { .. } => Err(IrDriverError::unimplemented(
46 "build_state_value for kind=map",
47 )),
48 IrStateValue::MerkleTree { .. } => Err(IrDriverError::unimplemented(
49 "build_state_value for kind=merkle-tree",
50 )),
51 }
52}