Skip to main content

nox_core/traits/
interfaces.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use async_trait::async_trait;
5use thiserror::Error;
6use tokio::sync::broadcast;
7
8use crate::events::NoxEvent;
9
10#[derive(Debug, Error)]
11pub enum InfrastructureError {
12    #[error("Database error: {0}")]
13    Database(String),
14    #[error("Network error: {0}")]
15    Network(String),
16    #[error("Blockchain error: {0}")]
17    Blockchain(String),
18}
19
20#[async_trait]
21pub trait IStorageRepository: Send + Sync {
22    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, InfrastructureError>;
23    async fn put(&self, key: &[u8], value: &[u8]) -> Result<(), InfrastructureError>;
24    async fn exists(&self, key: &[u8]) -> Result<bool, InfrastructureError>;
25    async fn delete(&self, key: &[u8]) -> Result<(), InfrastructureError>;
26    async fn scan(&self, prefix: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>, InfrastructureError>;
27}
28
29#[async_trait]
30pub trait IChainService: Send + Sync {
31    async fn get_block_number(&self) -> Result<u64, InfrastructureError>;
32}
33
34#[async_trait]
35pub trait INetworkService: Send + Sync {
36    async fn start(&self) -> Result<(), InfrastructureError>;
37    async fn broadcast(&self, topic: &str, msg: &[u8]) -> Result<(), InfrastructureError>;
38}
39
40#[derive(Debug, Error)]
41pub enum EventBusError {
42    #[error("Failed to broadcast event: {0}")]
43    BroadcastFailed(String),
44    #[error("Lagged receiver skipped {0} messages")]
45    Lagged(u64),
46}
47
48#[async_trait]
49pub trait IEventPublisher: Send + Sync {
50    fn publish(&self, event: NoxEvent) -> Result<usize, EventBusError>;
51}
52
53pub trait IEventSubscriber: Send + Sync {
54    fn subscribe(&self) -> broadcast::Receiver<NoxEvent>;
55}
56
57pub trait IEventBus: IEventPublisher + IEventSubscriber {}
58
59#[async_trait]
60pub trait IReplayProtection: Send + Sync {
61    async fn check_and_tag(
62        &self,
63        tag: &[u8],
64        ttl_seconds: u64,
65    ) -> Result<bool, InfrastructureError>;
66    async fn prune_expired(&self) -> Result<usize, InfrastructureError>;
67}
68
69pub trait IMixStrategy: Send + Sync {
70    fn get_delay(&self) -> Duration;
71}
72
73#[async_trait]
74pub trait IChainClient: Send + Sync {
75    async fn submit_tx(&self, to: &str, data: &[u8]) -> Result<String, InfrastructureError>;
76    async fn simulate_tx(&self, to: &str, data: &[u8]) -> Result<Vec<u8>, InfrastructureError>;
77}
78
79pub use darkpool_crypto::IPoseidonHasher;
80
81/// No-op `IEventPublisher` that silently discards all events.
82pub struct NoopPublisher;
83
84impl NoopPublisher {
85    #[must_use]
86    pub fn arc() -> Arc<dyn IEventPublisher> {
87        Arc::new(Self)
88    }
89}
90
91impl IEventPublisher for NoopPublisher {
92    fn publish(&self, _event: NoxEvent) -> Result<usize, EventBusError> {
93        Ok(0)
94    }
95}
96
97#[async_trait]
98pub trait IProverService: Send + Sync {
99    async fn prove(
100        &self,
101        circuit_name: &str,
102        inputs: std::collections::HashMap<String, String>,
103    ) -> Result<ZKProofData, InfrastructureError>;
104}
105
106#[derive(Debug, Clone)]
107pub struct ZKProofData {
108    pub proof: Vec<u8>,
109    pub public_inputs: Vec<String>,
110}