moonproof-contracts 0.2.2

Node-side host orchestration for the moonproof contract pipeline — providers, deploy/call/maintenance lifecycle, fee balancing, submission, indexer-driven sync
Documentation
//! Capability traits — let functions declare exactly which providers
//! they need from a [`ContractProviders`]-shaped bundle.
//!
//! Instead of taking `&ContractProviders` (the whole bag), call sites
//! constrain on `impl HasProof + HasWallet + HasMidnight` (or whatever
//! subset they actually use). Tests mock only the slice the
//! system-under-test exercises.
//!
//! The associated types are `dyn FooProvider`, matching the
//! `Arc<dyn Trait>` storage in [`super::ContractProviders`].

use std::sync::Arc;

use super::{
    ContractModuleProvider, LoggerProvider, MidnightProvider, PrivateStateProvider, ProofProvider,
    PublicDataProvider, WalletProvider, ZkConfigProvider,
};

/// Bundle exposes a [`ProofProvider`].
pub trait HasProof: Send + Sync {
    type Proof: ProofProvider + ?Sized;
    fn proof(&self) -> &Self::Proof;
}

/// Bundle exposes a [`WalletProvider`].
pub trait HasWallet: Send + Sync {
    type Wallet: WalletProvider + ?Sized;
    fn wallet(&self) -> &Self::Wallet;
}

/// Bundle exposes a [`MidnightProvider`].
pub trait HasMidnight: Send + Sync {
    type Midnight: MidnightProvider + ?Sized;
    fn midnight(&self) -> &Self::Midnight;
}

/// Bundle exposes a [`PublicDataProvider`].
pub trait HasPublicData: Send + Sync {
    type PublicData: PublicDataProvider + ?Sized;
    fn public_data(&self) -> &Self::PublicData;
}

/// Bundle exposes a [`PrivateStateProvider`].
pub trait HasPrivateState: Send + Sync {
    type PrivateState: PrivateStateProvider + ?Sized;
    fn private_state(&self) -> &Self::PrivateState;
}

/// Bundle exposes a [`ZkConfigProvider`].
pub trait HasZkConfig: Send + Sync {
    type ZkConfig: ZkConfigProvider + ?Sized;
    fn zk_config(&self) -> &Self::ZkConfig;
}

/// Bundle optionally exposes a [`LoggerProvider`]. Returns `None` when
/// the bundle was built without a logger — call sites that only want
/// to emit debug breadcrumbs can ignore the absence cheaply.
pub trait HasLogger: Send + Sync {
    type Logger: LoggerProvider + ?Sized;
    fn logger(&self) -> Option<&Self::Logger>;
}

pub trait HasContractModules: Send + Sync {
    type ContractModules: ContractModuleProvider + ?Sized;
    fn contract_modules(&self) -> Option<&Self::ContractModules>;
}

/// Convenience super-trait for code paths that legitimately need every
/// capability the runtime exposes — `submit_call_tx`, `submit_deploy_tx`,
/// `find_deployed_contract`, etc. The slice traits above are still the
/// preferred surface when a function only needs a subset (e.g. a
/// read-only query that touches `HasPublicData` only).
///
/// Auto-blanket-implemented for any `T` that satisfies the seven
/// member traits, so callers don't have to opt in.
pub trait Providers:
    HasProof
    + HasWallet
    + HasMidnight
    + HasPublicData
    + HasPrivateState
    + HasZkConfig
    + HasLogger
    + HasContractModules
{
}

impl<T> Providers for T where
    T: HasProof
        + HasWallet
        + HasMidnight
        + HasPublicData
        + HasPrivateState
        + HasZkConfig
        + HasLogger
        + HasContractModules
        + ?Sized
{
}

// -- Blanket impls for borrowed / Arc-wrapped bundles --------------------
//
// Call sites that hold an `Arc<ContractProviders>` (the common pattern
// once a bundle is shared across tasks) want to pass `&providers`
// directly to functions bounded on the capability traits. Same for
// `&ContractProviders` already-borrowed values, which the compiler
// would otherwise refuse to coerce to a fresh `&C: HasFoo`.

macro_rules! capability_blanket {
    ($Trait:ident, $Assoc:ident, $accessor:ident, ret $ret:ty) => {
        impl<T: $Trait + ?Sized> $Trait for &T {
            type $Assoc = T::$Assoc;
            fn $accessor(&self) -> $ret {
                (**self).$accessor()
            }
        }

        impl<T: $Trait + ?Sized> $Trait for Arc<T> {
            type $Assoc = T::$Assoc;
            fn $accessor(&self) -> $ret {
                (**self).$accessor()
            }
        }
    };
}

capability_blanket!(HasProof, Proof, proof, ret & Self::Proof);
capability_blanket!(HasWallet, Wallet, wallet, ret & Self::Wallet);
capability_blanket!(HasMidnight, Midnight, midnight, ret & Self::Midnight);
capability_blanket!(
    HasPublicData,
    PublicData,
    public_data,
    ret & Self::PublicData
);
capability_blanket!(
    HasPrivateState,
    PrivateState,
    private_state,
    ret & Self::PrivateState
);
capability_blanket!(HasZkConfig, ZkConfig, zk_config, ret & Self::ZkConfig);
capability_blanket!(HasLogger, Logger, logger, ret Option<&Self::Logger>);
capability_blanket!(
    HasContractModules,
    ContractModules,
    contract_modules,
    ret Option<&Self::ContractModules>
);