From c40e34c09402d7db9469a08cf167c679bcd0a8d8 Mon Sep 17 00:00:00 2001 From: ghassmo Date: Thu, 14 Oct 2021 14:49:13 +0300 Subject: [PATCH] cashierd: catch bridge messages and send proper error message to the user --- src/bin/cashierd.rs | 23 +++++++++------ src/service/bridge.rs | 67 +++++++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/bin/cashierd.rs b/src/bin/cashierd.rs index b0feb8873..6623fb883 100644 --- a/src/bin/cashierd.rs +++ b/src/bin/cashierd.rs @@ -32,6 +32,8 @@ use drk::{ fn handle_bridge_error(error_code: u32) -> Result<()> { match error_code { 1 => Err(Error::BridgeError("Not Supported Client".into())), + 2 => Err(Error::BridgeError("Unable to watch the deposit address".into())), + 3 => Err(Error::BridgeError("Unable to send the token".into())), _ => Err(Error::BridgeError("Unknown error_code".into())), } } @@ -168,16 +170,21 @@ impl Cashierd { // check the response's error let error_code = res.error as u32; - if error_code == 0 { - match res.payload { - bridge::BridgeResponsePayload::Send => { - cashier_wallet.confirm_withdraw_key_record(&addr, &network)?; - } - _ => {} - } - } else { + + if error_code != 0 { return handle_bridge_error(error_code); } + + match res.payload { + bridge::BridgeResponsePayload::Send => { + cashier_wallet.confirm_withdraw_key_record(&addr, &network)?; + } + _ => { + return Err(Error::BridgeError( + "Receive unknown value from Subscription".into(), + )); + } + } } Ok(()) diff --git a/src/service/bridge.rs b/src/service/bridge.rs index 9d26e6872..fcca67f56 100644 --- a/src/service/bridge.rs +++ b/src/service/bridge.rs @@ -35,6 +35,8 @@ pub enum BridgeResponsePayload { pub enum BridgeResponseError { NoError, NotSupportedClient, + BridgeWatchSubscribtionError, + BridgeSendSubscribtionError, } pub struct BridgeSubscribtion { @@ -42,6 +44,7 @@ pub struct BridgeSubscribtion { pub receiver: async_channel::Receiver, } +#[derive(Debug)] pub struct TokenSubscribtion { pub secret_key: Vec, pub public_key: String, @@ -156,37 +159,65 @@ impl Bridge { client = c.clone(); } + let res: BridgeResponse; + match req.payload { BridgeRequestsPayload::Watch(val) => match val { Some((private_key, public_key)) => { let pub_key = client .subscribe_with_keypair(private_key, public_key, drk_pub_key, mint_address) - .await?; - let res = BridgeResponse { - error: BridgeResponseError::NoError, - payload: BridgeResponsePayload::Address(pub_key), - }; - rep.send(res).await?; + .await; + + if pub_key.is_err() { + error!(target: "BRIDGE", "{}", pub_key.unwrap_err().to_string()); + res = BridgeResponse { + error: BridgeResponseError::BridgeWatchSubscribtionError, + payload: BridgeResponsePayload::Empty, + }; + } else { + res = BridgeResponse { + error: BridgeResponseError::NoError, + payload: BridgeResponsePayload::Address(pub_key?), + }; + } } None => { - let sub = client.subscribe(drk_pub_key, mint_address).await?; - let res = BridgeResponse { - error: BridgeResponseError::NoError, - payload: BridgeResponsePayload::Watch(sub.secret_key, sub.public_key), - }; - rep.send(res).await?; + let sub = client.subscribe(drk_pub_key, mint_address).await; + if sub.is_err() { + error!(target: "BRIDGE", "{}", sub.unwrap_err().to_string()); + res = BridgeResponse { + error: BridgeResponseError::BridgeWatchSubscribtionError, + payload: BridgeResponsePayload::Empty, + }; + } else { + let sub = sub?; + res = BridgeResponse { + error: BridgeResponseError::NoError, + payload: BridgeResponsePayload::Watch(sub.secret_key, sub.public_key), + }; + } } }, BridgeRequestsPayload::Send(addr, amount) => { - client.send(addr, mint_address, amount).await?; - let res = BridgeResponse { - error: BridgeResponseError::NoError, - payload: BridgeResponsePayload::Send, - }; - rep.send(res).await?; + let result = client.send(addr, mint_address, amount).await; + + if result.is_err() { + error!(target: "BRIDGE", "{}", result.unwrap_err().to_string()); + res = BridgeResponse { + error: BridgeResponseError::BridgeSendSubscribtionError, + payload: BridgeResponsePayload::Empty, + }; + } else { + res = BridgeResponse { + error: BridgeResponseError::NoError, + payload: BridgeResponsePayload::Send, + }; + } } } + rep.send(res).await?; + Ok(()) } }