moonproof-contracts 0.2.1

Node-side host orchestration for the moonproof contract pipeline — providers, deploy/call/maintenance lifecycle, fee balancing, submission, indexer-driven sync
Documentation
//! Rust port of midnight-js 5's `contractModuleProvider`: the code a
//! cross-contract call runs for each callee address, with the ZK
//! artifacts its proofs need.

use std::collections::HashMap;
use std::sync::Arc;

use moonproof_ledger::coin_structure::contract::ContractAddress;
use moonproof_macros::async_trait;
use moonproof_runtime_ir::ir::MoonIr;

use crate::error::Result;
use crate::providers::zk_config::ZkConfigProvider;

/// A compiled contract: its runtime IR and its ZK artifacts.
#[derive(Debug, Clone)]
pub struct ContractModule {
    pub ir: Arc<MoonIr>,
    pub zk_config: Arc<dyn ZkConfigProvider>,
}

/// Binds callee addresses to the code deployed there.
///
/// A binding is checked before it is used: the module must implement
/// the contract type the caller declares, and its verifier keys must be
/// the ones deployed at the address.
#[async_trait]
pub trait ContractModuleProvider: Send + Sync + std::fmt::Debug {
    /// The module for `address`, or `Ok(None)` when this provider binds
    /// nothing there.
    async fn resolve(&self, address: &ContractAddress) -> Result<Option<ContractModule>>;
}

#[async_trait]
impl<T: ContractModuleProvider + ?Sized> ContractModuleProvider for Arc<T> {
    async fn resolve(&self, address: &ContractAddress) -> Result<Option<ContractModule>> {
        (**self).resolve(address).await
    }
}

/// A fixed address-to-module map.
#[derive(Debug, Clone, Default)]
pub struct ContractModuleRegistry {
    modules: HashMap<ContractAddress, ContractModule>,
}

impl ContractModuleRegistry {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn with(mut self, address: ContractAddress, module: ContractModule) -> Self {
        self.insert(address, module);
        self
    }

    pub fn insert(&mut self, address: ContractAddress, module: ContractModule) {
        self.modules.insert(address, module);
    }
}

#[async_trait]
impl ContractModuleProvider for ContractModuleRegistry {
    async fn resolve(&self, address: &ContractAddress) -> Result<Option<ContractModule>> {
        Ok(self.modules.get(address).cloned())
    }
}