Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Problem: (CRO-396) No way to remove a wallet from auto-sync (#360)
Browse files Browse the repository at this point in the history
Solution: Added a RPC call to stop sync for a wallet
  • Loading branch information
devashishdxt authored and tomtau committed Sep 9, 2019
1 parent c3277b0 commit 6c724f1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
13 changes: 12 additions & 1 deletion client-index/src/auto_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::sync::mpsc::Sender<OwnedMessage>>;
Expand Down
29 changes: 24 additions & 5 deletions client-index/src/auto_sync_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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());
Expand All @@ -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]
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions client-index/src/auto_sync_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ pub struct AddWalletCommand {
/// private key
pub private_key: Vec<u8>,
}

/// 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#"
{
Expand Down
15 changes: 15 additions & 0 deletions client-rpc/src/rpc/sync_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, S, C, H>
Expand Down Expand Up @@ -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<T, S, C, H> SyncRpcImpl<T, S, C, H>
Expand Down

0 comments on commit 6c724f1

Please sign in to comment.