Skip to content

Commit

Permalink
feat(eth-redis): implement missing store methods when deleting an acc…
Browse files Browse the repository at this point in the history
…ount
  • Loading branch information
gakonst committed Nov 19, 2019
1 parent 04f0ecc commit 4de734e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
28 changes: 28 additions & 0 deletions crates/ethereum-engine/src/backends/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Future<Item = (), Error = ()> + Send> {
self.redis_store
.clear_uncredited_settlement_amount(account_id)
}
}

impl IdempotentStore for EthereumLedgerRedisStore {
Expand Down Expand Up @@ -220,6 +228,26 @@ impl EthereumStore for EthereumLedgerRedisStore {
)
}

fn delete_accounts(
&self,
account_ids: Vec<String>,
) -> Box<dyn Future<Item = (), Error = ()> + 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<String, EthereumAddresses>,
Expand Down
31 changes: 31 additions & 0 deletions crates/ethereum-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,37 @@ where
)
}

// Deletes an account from the engine
fn delete_account(
&self,
account_id: String,
) -> Box<dyn Future<Item = ApiResponse, Error = ApiError> + 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
Expand Down
14 changes: 14 additions & 0 deletions crates/ethereum-engine/src/utils/test_helpers/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ impl LeftoversStore for TestStore {
(BigUint::from(0u32), 1)
}))
}

fn clear_uncredited_settlement_amount(
&self,
account_id: Self::AccountId,
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
unreachable!()
}
}

impl EthereumStore for TestStore {
Expand All @@ -160,6 +167,13 @@ impl EthereumStore for TestStore {
Box::new(ok(()))
}

fn delete_accounts(
&self,
_account_ids: Vec<String>,
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
Box::new(ok(()))
}

fn load_account_addresses(
&self,
account_ids: Vec<String>,
Expand Down
7 changes: 7 additions & 0 deletions crates/ethereum-engine/src/utils/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ pub trait EthereumStore {
account_ids: Vec<<Self::Account as EthereumAccount>::AccountId>,
) -> Box<dyn Future<Item = Vec<Addresses>, 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<<Self::Account as EthereumAccount>::AccountId>,
) -> Box<dyn Future<Item = (), Error = ()> + Send>;

/// Saves the latest block number, up to which all
/// transactions have been communicated to the connector
fn save_recently_observed_block(
Expand Down
14 changes: 14 additions & 0 deletions crates/ethereum-engine/tests/eth_engine_integration_tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ impl LeftoversStore for TestStore {
(BigUint::from(0u32), 1)
}))
}

fn clear_uncredited_settlement_amount(
&self,
account_id: Self::AccountId,
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
unreachable!()
}
}

impl EthereumStore for TestStore {
Expand All @@ -167,6 +174,13 @@ impl EthereumStore for TestStore {
Box::new(ok(()))
}

fn delete_accounts(
&self,
_account_ids: Vec<String>,
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
Box::new(ok(()))
}

fn load_account_addresses(
&self,
account_ids: Vec<String>,
Expand Down

0 comments on commit 4de734e

Please sign in to comment.