diff --git a/crates/ethereum-engine/src/backends/redis.rs b/crates/ethereum-engine/src/backends/redis.rs index 00b978f..5deb074 100644 --- a/crates/ethereum-engine/src/backends/redis.rs +++ b/crates/ethereum-engine/src/backends/redis.rs @@ -141,6 +141,14 @@ impl LeftoversStore for EthereumLedgerRedisStore { self.redis_store .load_uncredited_settlement_amount(account_id, local_scale) } + + fn clear_uncredited_settlement_amount( + &self, + account_id: Self::AccountId, + ) -> Box + Send> { + self.redis_store + .clear_uncredited_settlement_amount(account_id) + } } impl IdempotentStore for EthereumLedgerRedisStore { @@ -220,6 +228,26 @@ impl EthereumStore for EthereumLedgerRedisStore { ) } + fn delete_accounts( + &self, + account_ids: Vec, + ) -> Box + Send> { + let mut pipe = redis::pipe(); + for account_id in account_ids.iter() { + pipe.del(ethereum_ledger_key(&account_id)); + } + Box::new( + pipe.query_async(self.connection.clone()) + .map_err(move |err| { + error!( + "Error the addresses for accounts: {:?} {:?}", + account_ids, err + ) + }) + .and_then(move |(_conn, _ret): (_, Value)| Ok(())), + ) + } + fn save_account_addresses( &self, data: HashMap, diff --git a/crates/ethereum-engine/src/engine.rs b/crates/ethereum-engine/src/engine.rs index 12f62ab..f4fc629 100644 --- a/crates/ethereum-engine/src/engine.rs +++ b/crates/ethereum-engine/src/engine.rs @@ -855,6 +855,37 @@ where ) } + // Deletes an account from the engine + fn delete_account( + &self, + account_id: String, + ) -> Box + Send> { + let store = self.store.clone(); + let account_id_clone = account_id.clone(); + Box::new(self.load_account(account_id.clone()).map_err(|err| { + let error_msg = format!("Error loading account {:?}", err); + error!("{}", error_msg); + ApiError::internal_server_error().detail(error_msg) + }) + .and_then(move |_| { + // if the load call succeeds, then the account exists and we must: + // 1. delete their addresses + // 2. clear their uncredited settlement amounts + join_all(vec![ + store.clear_uncredited_settlement_amount(account_id_clone.clone()), + store.delete_accounts(vec![account_id]), + ]) + .map_err(move |err| { + let error_msg = format!("Couldn't connect to store {:?}", err); + error!("{}", error_msg); + ApiError::internal_server_error().detail(error_msg) + }) + }) + .and_then(move |_| { + Ok(ApiResponse::Default) + })) + } + /// Settlement Engine's function that corresponds to the /// /accounts/:id/messages endpoint (POST). /// The body is a challenge issued by the peer's settlement engine which we diff --git a/crates/ethereum-engine/src/utils/test_helpers/utils.rs b/crates/ethereum-engine/src/utils/test_helpers/utils.rs index 5f70cd7..cec74b7 100644 --- a/crates/ethereum-engine/src/utils/test_helpers/utils.rs +++ b/crates/ethereum-engine/src/utils/test_helpers/utils.rs @@ -142,6 +142,13 @@ impl LeftoversStore for TestStore { (BigUint::from(0u32), 1) })) } + + fn clear_uncredited_settlement_amount( + &self, + account_id: Self::AccountId, + ) -> Box + Send> { + unreachable!() + } } impl EthereumStore for TestStore { @@ -160,6 +167,13 @@ impl EthereumStore for TestStore { Box::new(ok(())) } + fn delete_accounts( + &self, + _account_ids: Vec, + ) -> Box + Send> { + Box::new(ok(())) + } + fn load_account_addresses( &self, account_ids: Vec, diff --git a/crates/ethereum-engine/src/utils/types.rs b/crates/ethereum-engine/src/utils/types.rs index 7d8d2a6..247b2c3 100644 --- a/crates/ethereum-engine/src/utils/types.rs +++ b/crates/ethereum-engine/src/utils/types.rs @@ -60,6 +60,13 @@ pub trait EthereumStore { account_ids: Vec<::AccountId>, ) -> Box, Error = ()> + Send>; + /// Deletes the Ethereum address associated with this account + /// called when deleting an account on the API. + fn delete_accounts( + &self, + account_ids: Vec<::AccountId>, + ) -> Box + Send>; + /// Saves the latest block number, up to which all /// transactions have been communicated to the connector fn save_recently_observed_block( diff --git a/crates/ethereum-engine/tests/eth_engine_integration_tests/utils.rs b/crates/ethereum-engine/tests/eth_engine_integration_tests/utils.rs index e3e24d9..259db46 100644 --- a/crates/ethereum-engine/tests/eth_engine_integration_tests/utils.rs +++ b/crates/ethereum-engine/tests/eth_engine_integration_tests/utils.rs @@ -149,6 +149,13 @@ impl LeftoversStore for TestStore { (BigUint::from(0u32), 1) })) } + + fn clear_uncredited_settlement_amount( + &self, + account_id: Self::AccountId, + ) -> Box + Send> { + unreachable!() + } } impl EthereumStore for TestStore { @@ -167,6 +174,13 @@ impl EthereumStore for TestStore { Box::new(ok(())) } + fn delete_accounts( + &self, + _account_ids: Vec, + ) -> Box + Send> { + Box::new(ok(())) + } + fn load_account_addresses( &self, account_ids: Vec,