Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
revert fallible compute_escrow_address
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed May 6, 2024
1 parent 18929bf commit 7fd2918
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
22 changes: 6 additions & 16 deletions modules/sov-ibc-transfer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,21 @@ impl<'ws, S: Spec> IbcTransferContext<'ws, S> {
/// Obtains the escrow address for a given port and channel pair by looking
/// up the cache. If the cache does not contain the address, it is computed
/// and stored in the cache.
fn obtain_escrow_address(
&self,
port_id: &PortId,
channel_id: &ChannelId,
) -> Result<S::Address, TokenTransferError> {
fn obtain_escrow_address(&self, port_id: &PortId, channel_id: &ChannelId) -> S::Address {
let mut working_set = self.working_set.borrow_mut();

let escrow_account = self
.ibc_transfer
.escrow_address_cache
.get(&(port_id.clone(), channel_id.clone()), *working_set)
.map_or_else(
|| {
compute_escrow_address::<S>(port_id, channel_id)
.map_err(|e| TokenTransferError::Other(e.to_string()))
},
Ok,
)?;
.unwrap_or_else(|| compute_escrow_address::<S>(port_id, channel_id));

self.ibc_transfer.escrow_address_cache.set(
&(port_id.clone(), channel_id.clone()),
&escrow_account,
*working_set,
);
Ok(escrow_account)
escrow_account
}

/// Validates that the sender has sufficient balance to perform the
Expand Down Expand Up @@ -364,7 +354,7 @@ where
) -> Result<(), TokenTransferError> {
let token_id = self.get_native_token_id(coin, port_id, channel_id)?;

let escrow_address = self.obtain_escrow_address(port_id, channel_id)?;
let escrow_address = self.obtain_escrow_address(port_id, channel_id);

self.validate_balance(token_id, &escrow_address, coin.amount)?;

Expand Down Expand Up @@ -454,7 +444,7 @@ impl<'ws, S: Spec> TokenTransferExecutionContext for IbcTransferContext<'ws, S>
}
})?;

let escrow_account = self.obtain_escrow_address(port_id, channel_id)?;
let escrow_account = self.obtain_escrow_address(port_id, channel_id);

// transfer coins to escrow account
self.transfer(
Expand Down Expand Up @@ -483,7 +473,7 @@ impl<'ws, S: Spec> TokenTransferExecutionContext for IbcTransferContext<'ws, S>
}
})?;

let escrow_account = self.obtain_escrow_address(port_id, channel_id)?;
let escrow_account = self.obtain_escrow_address(port_id, channel_id);

// transfer coins out of escrow account to `to_account`
self.transfer(token_id, &escrow_account, &to_account.address, &coin.amount)?;
Expand Down
7 changes: 2 additions & 5 deletions modules/sov-ibc-transfer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use sov_modules_api::{CryptoSpec, Spec};
/// <https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-028-public-key-addresses.md/>
/// except that the `Hasher` function mandated by the `CryptoSpec` trait in the
/// rollup implementation.
pub fn compute_escrow_address<S: Spec>(
port_id: &PortId,
channel_id: &ChannelId,
) -> Result<S::Address, anyhow::Error> {
pub fn compute_escrow_address<S: Spec>(port_id: &PortId, channel_id: &ChannelId) -> S::Address {
let escrow_account_bytes: [u8; 32] = {
let mut hasher = <S::CryptoSpec as CryptoSpec>::Hasher::new();
hasher.update(VERSION);
Expand All @@ -22,5 +19,5 @@ pub fn compute_escrow_address<S: Spec>(
};

// FIXME(rano): this should be infallible.
escrow_account_bytes.as_ref().try_into()
escrow_account_bytes.as_ref().try_into().unwrap()
}

0 comments on commit 7fd2918

Please sign in to comment.