moonproof-runtime 0.2.2

Wasm-friendly Compact contract runtime core — IR walker, IR-driven circuit executor, contract state, runtime values
Documentation
//! End-to-end test: drive the `synthetic_slice` fixture through the
//! generic [`IrExecutable`].
//!
//! Exercises the slice-family Expression variants no other current
//! fixture reaches: `tuple-slice` (literal-start sub-vector) and
//! `bytes-slice` (sub-byte-string).

use std::sync::Arc;

use moonproof_runtime_ir::RuntimeValue as Value;

use moonproof_ledger::base_crypto::hash::HashOutput;
use moonproof_ledger::base_crypto::time::Timestamp;
use moonproof_ledger::coin_structure::contract::ContractAddress;
use moonproof_ledger::ledger::structure::INITIAL_PARAMETERS;
use moonproof_ledger::signatures::SigningKey;
use moonproof_runtime::executable::{CircuitCallContext, ConstructorContext, InitializeResult};
use moonproof_runtime::types::CoinPublicKey;
use moonproof_runtime::CompactZswapLocalState;
use moonproof_runtime::{IrExecutable, NoWitnesses};

fn synthetic_slice_ir_path() -> std::path::PathBuf {
    let mut p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    p.pop();
    p.push("runtime-ir");
    p.push("tests/fixtures/synthetic_slice.cbor");
    p
}

fn dummy_signing_key() -> SigningKey {
    moonproof_ledger::signatures::schnorr_signing_key(&[0x42u8; 32]).expect("32 bytes parse")
}

fn dummy_coin_public_key() -> CoinPublicKey {
    CoinPublicKey([0u8; 32])
}

async fn loaded() -> (IrExecutable<NoWitnesses>, InitializeResult) {
    let exec = IrExecutable::from_path(synthetic_slice_ir_path(), Arc::new(NoWitnesses))
        .expect("synthetic_slice IR loads");
    let init = exec
        .initialize(
            ConstructorContext {
                initial_private_state: Value::Null,
                initial_zswap_local_state: CompactZswapLocalState::empty(dummy_coin_public_key()),
                coin_public_key: dummy_coin_public_key(),
                signing_key: dummy_signing_key(),
                time: None,
            },
            Value::Null,
        )
        .await
        .expect("synthetic_slice initialize");
    (exec, init)
}

fn fresh_call_ctx(
    state: moonproof_ledger::onchain_runtime::state::ContractState<
        moonproof_ledger::storage::db::InMemoryDB,
    >,
) -> CircuitCallContext {
    CircuitCallContext {
        contract_address: ContractAddress(HashOutput([0x77u8; 32])),
        contract_state: state,
        private_state: Value::Null,
        ledger_parameters: INITIAL_PARAMETERS.clone(),
        coin_public_key: dummy_coin_public_key(),
        tblock: Timestamp::from_secs(0),
    }
}

#[tokio::test]
async fn ir_synthetic_slice_provable_circuits() {
    let exec =
        IrExecutable::from_path(synthetic_slice_ir_path(), Arc::new(NoWitnesses)).expect("loads");
    let names: Vec<String> = exec
        .provable_circuit_ids()
        .iter()
        .map(|c| c.as_str().to_owned())
        .collect();
    assert_eq!(names.len(), 3);
    for name in &["slice_vector", "slice_fields", "slice_bytes"] {
        assert!(names.contains(&(*name).to_owned()));
    }
}

#[tokio::test]
async fn ir_synthetic_slice_vector_executes() {
    let (exec, init) = loaded().await;
    let xs = Value::Array(vec![
        Value::Int(0x10),
        Value::Int(0x20),
        Value::Int(0x30),
        Value::Int(0x40),
        Value::Int(0x50),
    ]);
    let mut before = Vec::new();
    moonproof_ledger::serialize::tagged_serialize(&init.initial_contract_state, &mut before)
        .unwrap();
    let result = exec
        .execute_circuit(
            "slice_vector",
            fresh_call_ctx(init.initial_contract_state),
            xs,
        )
        .await
        .expect("slice_vector executes");
    let mut after = Vec::new();
    moonproof_ledger::serialize::tagged_serialize(&result.next_contract_state, &mut after).unwrap();
    assert_ne!(before, after);
    assert!(after.len() >= before.len() / 2);
}

#[tokio::test]
async fn ir_synthetic_slice_fields_executes() {
    let (exec, init) = loaded().await;
    let to_field_bytes = |n: u64| {
        let mut le = vec![0u8; 32];
        le[..8].copy_from_slice(&n.to_le_bytes());
        Value::Bytes(le)
    };
    let xs = Value::Array(vec![
        to_field_bytes(1),
        to_field_bytes(2),
        to_field_bytes(3),
        to_field_bytes(4),
        to_field_bytes(5),
        to_field_bytes(6),
    ]);
    exec.execute_circuit(
        "slice_fields",
        fresh_call_ctx(init.initial_contract_state),
        xs,
    )
    .await
    .expect("slice_fields executes");
}

#[tokio::test]
async fn ir_synthetic_slice_bytes_executes() {
    let (exec, init) = loaded().await;
    let bytes = Value::Bytes(vec![0xA0u8, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7]);
    exec.execute_circuit(
        "slice_bytes",
        fresh_call_ctx(init.initial_contract_state),
        bytes,
    )
    .await
    .expect("slice_bytes executes");
}