mutant_lib/network/
mod.rs

1pub mod client;
2pub mod error;
3pub mod get;
4pub mod put;
5pub mod wallet;
6
7use blsttc::SecretKey;
8use client::Config;
9pub use error::NetworkError;
10
11use self::wallet::create_wallet;
12use crate::index::PadInfo;
13
14// Make this public so other test modules can use it
15pub const DEV_TESTNET_PRIVATE_KEY_HEX: &str =
16    "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
17
18use autonomi::{AttoTokens, Client, ScratchpadAddress, Wallet};
19use log::debug;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
22pub enum NetworkChoice {
23    Mainnet,
24    Devnet,
25    Alphanet,
26}
27
28impl Default for NetworkChoice {
29    fn default() -> Self {
30        NetworkChoice::Mainnet
31    }
32}
33
34/// Represents the result of a get operation on the network.
35#[derive(Debug, Clone)]
36pub struct GetResult {
37    /// The data retrieved from the scratchpad.
38    pub data: Vec<u8>,
39    /// The counter value of the scratchpad.
40    pub counter: u64,
41    /// The encoding of the data in the scratchpad.
42    pub data_encoding: u64,
43}
44
45/// Represents the result of a put operation on the network.
46#[derive(Debug, Clone)]
47pub struct PutResult {
48    /// The cost of the put operation in AttoTokens.
49    pub cost: AttoTokens,
50    /// The address of the scratchpad that was put.
51    pub address: ScratchpadAddress,
52}
53
54lazy_static::lazy_static! {
55    pub static ref NB_CLIENTS: usize = std::env::var("MUTANT_NB_CLIENTS")
56        .unwrap_or_else(|_| "10".to_string())
57        .parse::<usize>()
58        .unwrap_or(10);
59
60    pub static ref BATCH_SIZE: usize = std::env::var("MUTANT_BATCH_SIZE")
61        .unwrap_or_else(|_| "10".to_string())
62        .parse::<usize>()
63        .unwrap_or(10);
64}
65
66/// Provides an interface to interact with the Autonomi network.
67///
68/// This adapter handles client initialization, wallet management, and delegates
69/// network interactions like reading and writing scratchpads to specialized modules.
70pub struct Network {
71    wallet: Wallet,
72    network_choice: NetworkChoice,
73    secret_key: SecretKey,
74}
75
76impl Network {
77    /// Creates a new `AutonomiNetworkAdapter` instance.
78    pub(crate) fn new(
79        private_key_hex: &str,
80        network_choice: NetworkChoice,
81    ) -> Result<Self, NetworkError> {
82        debug!(
83            "Creating AutonomiNetworkAdapter configuration for network: {:?}",
84            network_choice
85        );
86
87        let (wallet, secret_key) = create_wallet(private_key_hex, network_choice)?;
88
89        Ok(Self {
90            wallet,
91            network_choice,
92            secret_key,
93        })
94    }
95
96    /// Retrieves an Autonomi network client.
97    /// This method creates a new client for each call.
98    pub(crate) async fn get_client(
99        &self,
100        config: Config,
101    ) -> Result<Client, NetworkError> {
102        client::create_client(self.network_choice, config).await
103    }
104
105    /// Retrieves the raw content of a scratchpad from the network.
106    /// Delegates to the `get` module.
107    /// Requires PadInfo to reconstruct the SecretKey for decryption.
108    pub(crate) async fn get<C: std::ops::Deref<Target = Client>>(
109        &self,
110        client: C,
111        address: &ScratchpadAddress,
112        owner_sk: Option<&SecretKey>,
113    ) -> Result<GetResult, NetworkError> {
114        get::get(client.deref(), address, owner_sk).await
115    }
116
117    pub(crate) async fn put<C: std::ops::Deref<Target = Client>>(
118        &self,
119        client: C,
120        pad_info: &PadInfo,
121        data: &[u8],
122        data_encoding: u64,
123        is_public: bool,
124    ) -> Result<PutResult, NetworkError> {
125        put::put(
126            client.deref(),
127            self.wallet.clone(),
128            pad_info,
129            data,
130            data_encoding,
131            is_public,
132        )
133        .await
134    }
135
136    pub fn secret_key(&self) -> &SecretKey {
137        &self.secret_key
138    }
139
140    pub fn network_choice(&self) -> NetworkChoice {
141        self.network_choice
142    }
143}
144
145#[cfg(test)]
146pub mod integration_tests;