diff --git a/client-index/src/auto_sync.rs b/client-index/src/auto_sync.rs index b26a7f45c..9bc09697f 100644 --- a/client-index/src/auto_sync.rs +++ b/client-index/src/auto_sync.rs @@ -10,7 +10,7 @@ //! autosync.add_wallet(request.name, view_key, private_key, staking_addresses); //! -use crate::auto_sync_data::{AddWalletCommand, AutoSyncDataShared}; +use crate::auto_sync_data::{AddWalletCommand, AutoSyncDataShared, RemoveWalletCommand}; use crate::auto_synchronizer::AutoSynchronizer; use crate::BlockHandler; use chain_core::state::account::StakedStateAddress; @@ -76,6 +76,17 @@ impl AutoSync { self.send_json(data) } + + /// Removes a wallet from auto-sync + pub fn remove_wallet(&self, name: String) -> Result<()> { + let data = json!(RemoveWalletCommand { + id: "remove_wallet".to_string(), + name + }); + + self.send_json(data) + } + /// send json pub fn send_json(&self, json: serde_json::Value) -> Result<()> { let send_queue: Option>; diff --git a/client-index/src/auto_sync_core.rs b/client-index/src/auto_sync_core.rs index f797f4c50..4e653c740 100644 --- a/client-index/src/auto_sync_core.rs +++ b/client-index/src/auto_sync_core.rs @@ -4,8 +4,8 @@ use crate::auto_sync_data::WalletInfo; use crate::auto_sync_data::{ - AddWalletCommand, AutoSyncDataShared, AutoSyncSendQueueShared, WalletInfos, BLOCK_REQUEST_TIME, - CMD_BLOCK, CMD_STATUS, RECEIVE_TIMEOUT, WAIT_PROCESS_TIME, + AddWalletCommand, AutoSyncDataShared, AutoSyncSendQueueShared, RemoveWalletCommand, + WalletInfos, BLOCK_REQUEST_TIME, CMD_BLOCK, CMD_STATUS, RECEIVE_TIMEOUT, WAIT_PROCESS_TIME, }; use crate::service::GlobalStateService; @@ -253,6 +253,12 @@ where Ok(()) } + /// Removes a wallet from auto-sync + #[inline] + pub fn remove_wallet(&mut self, name: &str) { + let _ = self.wallets.remove(name); + } + /// Value is given from websocket_rpc /// received fn do_parse(&mut self, value: Value) -> Result<()> { @@ -281,6 +287,16 @@ where private_key, ); } + "remove_wallet" => { + let info: RemoveWalletCommand = serde_json::from_value(value).chain(|| { + ( + ErrorKind::DeserializationError, + "Unable to deserialize remove_wallet from json value", + ) + })?; + + self.remove_wallet(&info.name); + } "subscribe_reply#event" => { let new_block: Block = serde_json::from_value( value["result"]["data"]["value"].clone(), @@ -582,7 +598,7 @@ mod tests { let data = Arc::new(Mutex::new(AutoSyncData::new())); let channel = futures::sync::mpsc::channel(0); let (channel_tx, _channel_rx) = channel; - let mut send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); + let send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); { let mut data = send_queue.lock().unwrap(); data.queue = Some(channel_tx.clone()); @@ -605,6 +621,9 @@ mod tests { core.change_wallet(); assert!(core.current_wallet == 0); assert!(core.get_current_wallet().name == "a".to_string()); + + core.remove_wallet("a".into()); + assert!(core.wallets.is_empty()); } #[test] @@ -615,7 +634,7 @@ mod tests { let data = Arc::new(Mutex::new(AutoSyncData::new())); let channel = futures::sync::mpsc::channel(0); let (channel_tx, _channel_rx) = channel; - let mut send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); + let send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); { let mut data = send_queue.lock().unwrap(); data.queue = Some(channel_tx.clone()); @@ -645,7 +664,7 @@ mod tests { let data = Arc::new(Mutex::new(AutoSyncData::new())); let channel = futures::sync::mpsc::channel(0); let (channel_tx, _channel_rx) = channel; - let mut send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); + let send_queue = Arc::new(Mutex::new(AutoSyncSendQueue::new())); { let mut data = send_queue.lock().unwrap(); data.queue = Some(channel_tx.clone()); diff --git a/client-index/src/auto_sync_data.rs b/client-index/src/auto_sync_data.rs index c97af3eef..50cf9eb5c 100644 --- a/client-index/src/auto_sync_data.rs +++ b/client-index/src/auto_sync_data.rs @@ -81,6 +81,16 @@ pub struct AddWalletCommand { /// private key pub private_key: Vec, } + +/// Command to remove wallet from auto-sync +#[derive(Serialize, Deserialize)] +pub struct RemoveWalletCommand { + /// ID of this command + pub id: String, + /// Wallet name + pub name: String, +} + /// subscribe command pub const CMD_SUBSCRIBE: &str = r#" { diff --git a/client-rpc/src/rpc/sync_rpc.rs b/client-rpc/src/rpc/sync_rpc.rs index 1e97a18b7..b29f38591 100644 --- a/client-rpc/src/rpc/sync_rpc.rs +++ b/client-rpc/src/rpc/sync_rpc.rs @@ -21,6 +21,9 @@ pub trait SyncRpc: Send + Sync { // sync continuously #[rpc(name = "sync_unlockWallet")] fn sync_unlock_wallet(&self, request: WalletRequest) -> Result<()>; + + #[rpc(name = "sync_stop")] + fn sync_stop(&self, request: WalletRequest) -> Result<()>; } pub struct SyncRpcImpl @@ -67,6 +70,18 @@ where .add_wallet(request.name, view_key, private_key, staking_addresses) .map_err(to_rpc_error) } + + fn sync_stop(&self, request: WalletRequest) -> Result<()> { + // Just to check if passphrase is correct + let _ = self + .client + .view_key(&request.name, &request.passphrase) + .map_err(to_rpc_error)?; + + self.auto_synchronizer + .remove_wallet(request.name) + .map_err(to_rpc_error) + } } impl SyncRpcImpl