saito_core/core/io/
interface_io.rs

1use std::fmt::Debug;
2use std::io::Error;
3
4use async_trait::async_trait;
5
6use crate::core::consensus::peers::peer_service::PeerService;
7use crate::core::consensus::wallet::Wallet;
8use crate::core::defs::{BlockId, PeerIndex, SaitoHash, SaitoPublicKey};
9use crate::core::process::version::Version;
10
11pub enum InterfaceEvent {
12    PeerHandshakeComplete(PeerIndex),
13    PeerConnectionDropped(PeerIndex, SaitoPublicKey),
14    PeerConnected(PeerIndex),
15    BlockAddSuccess(SaitoHash, u64),
16    WalletUpdate(),
17    NewVersionDetected(PeerIndex, Version),
18    StunPeerConnected(PeerIndex),
19    StunPeerDisconnected(PeerIndex, SaitoPublicKey),
20    BlockFetchStatus(BlockId),
21}
22
23/// An interface is provided to access the IO functionalities in a platform (Rust/WASM) agnostic way
24#[async_trait]
25pub trait InterfaceIO: Debug {
26    async fn send_message(&self, peer_index: u64, buffer: &[u8]) -> Result<(), Error>;
27
28    /// Sends the given message buffer to all the peers except the ones specified
29    ///
30    /// # Arguments
31    ///
32    /// * `message_name`:
33    /// * `buffer`:
34    /// * `peer_exceptions`: Peer indices for which this message should not be sent
35    ///
36    /// returns: Result<(), Error>
37    ///
38    /// # Examples
39    ///
40    /// ```
41    ///
42    /// ```
43    async fn send_message_to_all(
44        &self,
45        buffer: &[u8],
46        excluded_peers: Vec<u64>,
47    ) -> Result<(), Error>;
48    /// Connects to the peer with given configuration
49    ///
50    /// # Arguments
51    ///
52    /// * `peer`:
53    ///
54    /// returns: Result<(), Error>
55    ///
56    /// # Examples
57    ///
58    /// ```
59    ///
60    /// ```
61    async fn connect_to_peer(&mut self, url: String, peer_index: PeerIndex) -> Result<(), Error>;
62    async fn disconnect_from_peer(&self, peer_index: u64) -> Result<(), Error>;
63
64    /// Fetches a block with given hash from a specific peer
65    ///
66    /// # Arguments
67    ///
68    /// * `block_hash`:
69    /// * `peer_index`:
70    /// * `url`:
71    ///
72    /// returns: Result<(), Error>
73    ///
74    /// # Examples
75    ///
76    /// ```
77    ///
78    /// ```
79    async fn fetch_block_from_peer(
80        &self,
81        block_hash: SaitoHash,
82        peer_index: u64,
83        url: &str,
84        block_id: BlockId,
85    ) -> Result<(), Error>;
86
87    /// Writes a value to a persistent storage with the given key
88    ///
89    /// # Arguments
90    ///
91    /// * `key`:
92    /// * `value`:
93    ///
94    /// returns: Result<(), Error>
95    ///
96    /// # Examples
97    ///
98    /// ```
99    ///
100    /// ```
101    async fn write_value(&self, key: &str, value: &[u8]) -> Result<(), Error>;
102    async fn append_value(&mut self, key: &str, value: &[u8]) -> Result<(), Error>;
103    async fn flush_data(&mut self, key: &str) -> Result<(), Error>;
104
105    /// Reads a value with the given key from a persistent storage
106    ///
107    /// # Arguments
108    ///
109    /// * `key`:
110    ///
111    /// returns: Result<Vec<u8, Global>, Error>
112    ///
113    /// # Examples
114    ///
115    /// ```
116    ///
117    /// ```
118    async fn read_value(&self, key: &str) -> Result<Vec<u8>, Error>;
119
120    /// Loads the block path list from the persistent storage
121    async fn load_block_file_list(&self) -> Result<Vec<String>, Error>;
122    async fn is_existing_file(&self, key: &str) -> bool;
123    /// Removes the value with the given key from the persistent storage
124    async fn remove_value(&self, key: &str) -> Result<(), Error>;
125    /// Retrieve the prefix for all the keys for blocks
126    fn get_block_dir(&self) -> String;
127    fn get_checkpoint_dir(&self) -> String;
128
129    fn ensure_block_directory_exists(&self, block_dir: &str) -> Result<(), Error>;
130
131    async fn process_api_call(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex);
132    async fn process_api_success(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex);
133    async fn process_api_error(&self, buffer: Vec<u8>, msg_index: u32, peer_index: PeerIndex);
134
135    fn send_interface_event(&self, event: InterfaceEvent);
136
137    async fn save_wallet(&self, wallet: &mut Wallet) -> Result<(), Error>;
138    async fn load_wallet(&self, wallet: &mut Wallet) -> Result<(), Error>;
139
140    // async fn save_blockchain(&self) -> Result<(), Error>;
141    // async fn load_blockchain(&self) -> Result<(), Error>;
142
143    fn get_my_services(&self) -> Vec<PeerService>;
144}
145
146// impl Debug for dyn InterfaceIO {
147//     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
148//         f.debug_struct("IoInterface").finish()
149//     }
150// }
151//