mutant_lib/network/
mod.rs1pub 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
14pub 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#[derive(Debug, Clone)]
36pub struct GetResult {
37 pub data: Vec<u8>,
39 pub counter: u64,
41 pub data_encoding: u64,
43}
44
45#[derive(Debug, Clone)]
47pub struct PutResult {
48 pub cost: AttoTokens,
50 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
66pub struct Network {
71 wallet: Wallet,
72 network_choice: NetworkChoice,
73 secret_key: SecretKey,
74}
75
76impl Network {
77 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 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 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;