mutant_lib/ops/
mod.rs

1pub use crate::error::Error;
2
3mod get;
4mod health_check;
5mod purge;
6mod put;
7mod sync;
8mod utils;
9pub mod worker;
10
11use crate::{
12    events::{GetCallback, PurgeCallback, SyncCallback},
13    index::master_index::MasterIndex,
14    network::Network,
15};
16use autonomi::ScratchpadAddress;
17use std::{sync::Arc, time::Duration};
18use tokio::sync::RwLock;
19
20use mutant_protocol::{
21    HealthCheckCallback, HealthCheckResult, PurgeResult, PutCallback, StorageMode, SyncResult,
22};
23
24pub const DATA_ENCODING_MASTER_INDEX: u64 = 0;
25pub const DATA_ENCODING_PRIVATE_DATA: u64 = 1;
26pub const DATA_ENCODING_PUBLIC_INDEX: u64 = 2;
27pub const DATA_ENCODING_PUBLIC_DATA: u64 = 3;
28
29pub const PAD_RECYCLING_RETRIES: usize = 3;
30
31const MAX_CONFIRMATION_DURATION: Duration = Duration::from_secs(60 * 5);
32
33pub struct Data {
34    network: Arc<Network>,
35    index: Arc<RwLock<MasterIndex>>,
36}
37
38impl Data {
39    pub fn new(network: Arc<Network>, index: Arc<RwLock<MasterIndex>>) -> Self {
40        Self { network, index }
41    }
42
43    pub async fn put(
44        &self,
45        key_name: &str,
46        content: Arc<Vec<u8>>,
47        mode: StorageMode,
48        public: bool,
49        no_verify: bool,
50        put_callback: Option<PutCallback>,
51    ) -> Result<ScratchpadAddress, Error> {
52        put::put(
53            self.index.clone(),
54            self.network.clone(),
55            key_name,
56            content,
57            mode,
58            public,
59            no_verify,
60            put_callback,
61        )
62        .await
63    }
64
65    pub async fn get_public(
66        &self,
67        address: &ScratchpadAddress,
68        get_callback: Option<GetCallback>,
69    ) -> Result<Vec<u8>, Error> {
70        get::get_public(self.network.clone(), address, get_callback).await
71    }
72
73    pub async fn get(
74        &self,
75        name: &str,
76        get_callback: Option<GetCallback>,
77    ) -> Result<Vec<u8>, Error> {
78        get::get(self.index.clone(), self.network.clone(), name, get_callback).await
79    }
80
81    pub async fn purge(
82        &self,
83        aggressive: bool,
84        purge_callback: Option<PurgeCallback>,
85    ) -> Result<PurgeResult, Error> {
86        purge::purge(
87            self.index.clone(),
88            self.network.clone(),
89            aggressive,
90            purge_callback,
91        )
92        .await
93    }
94
95    pub async fn health_check(
96        &self,
97        key_name: &str,
98        recycle: bool,
99        health_check_callback: Option<HealthCheckCallback>,
100    ) -> Result<HealthCheckResult, Error> {
101        health_check::health_check(
102            self.index.clone(),
103            self.network.clone(),
104            key_name,
105            recycle,
106            health_check_callback,
107        )
108        .await
109    }
110
111    pub async fn sync(
112        &self,
113        force: bool,
114        sync_callback: Option<SyncCallback>,
115    ) -> Result<SyncResult, Error> {
116        sync::sync(
117            self.index.clone(),
118            self.network.clone(),
119            force,
120            sync_callback,
121        )
122        .await
123    }
124}
125
126// fn derive_master_index_info(
127//     private_key_hex: &str,
128// ) -> Result<(ScratchpadAddress, SecretKey), Error> {
129//     debug!("Deriving master index key and address...");
130//     let hex_to_decode = private_key_hex
131//         .strip_prefix("0x")
132//         .unwrap_or(private_key_hex);
133
134//     let input_key_bytes = hex::decode(hex_to_decode)
135//         .map_err(|e| Error::Config(format!("Failed to decode private key hex: {}", e)))?;
136
137//     let mut hasher = Sha256::new();
138//     hasher.update(&input_key_bytes);
139//     let hash_result = hasher.finalize();
140//     let key_array: [u8; 32] = hash_result.into();
141
142//     let derived_key = SecretKey::from_bytes(key_array)
143//         .map_err(|e| Error::Internal(format!("Failed to create SecretKey from HASH: {:?}", e)))?;
144//     let derived_public_key = derived_key.public_key();
145//     let address = ScratchpadAddress::new(derived_public_key);
146//     info!("Derived Master Index Address: {}", address);
147//     Ok((address, derived_key))
148// }