saito_core/core/io/
interface_io.rs1use 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#[async_trait]
25pub trait InterfaceIO: Debug {
26 async fn send_message(&self, peer_index: u64, buffer: &[u8]) -> Result<(), Error>;
27
28 async fn send_message_to_all(
44 &self,
45 buffer: &[u8],
46 excluded_peers: Vec<u64>,
47 ) -> Result<(), Error>;
48 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 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 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 async fn read_value(&self, key: &str) -> Result<Vec<u8>, Error>;
119
120 async fn load_block_file_list(&self) -> Result<Vec<String>, Error>;
122 async fn is_existing_file(&self, key: &str) -> bool;
123 async fn remove_value(&self, key: &str) -> Result<(), Error>;
125 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 fn get_my_services(&self) -> Vec<PeerService>;
144}
145
146