From c47b2454fc5698e6a5f098187fc67c342cf7eee9 Mon Sep 17 00:00:00 2001 From: Ava Howell Date: Sat, 3 Aug 2024 12:54:28 -0700 Subject: [PATCH] avoid extraneous state query and improve denom handling --- .../shielded-pool/src/component/transfer.rs | 26 ++++--------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/crates/core/component/shielded-pool/src/component/transfer.rs b/crates/core/component/shielded-pool/src/component/transfer.rs index 3814b7f1d4..e8e6ad5372 100644 --- a/crates/core/component/shielded-pool/src/component/transfer.rs +++ b/crates/core/component/shielded-pool/src/component/transfer.rs @@ -266,7 +266,7 @@ async fn recv_transfer_packet_inner( // NOTE: spec says proto but this is actually JSON according to the ibc-go implementation let packet_data: FungibleTokenPacketData = serde_json::from_slice(msg.packet.data.as_slice()) .with_context(|| "failed to decode FTPD packet")?; - let denom: asset::Metadata = packet_data + let packet_denom: asset::Metadata = packet_data .denom .as_str() .try_into() @@ -280,7 +280,7 @@ async fn recv_transfer_packet_inner( // NOTE: here we assume we are chain A. // 2. check if we are the source chain for the denom. - if is_source(&msg.packet.port_on_a, &msg.packet.chan_on_a, &denom, false) { + if is_source(&msg.packet.port_on_a, &msg.packet.chan_on_a, &packet_denom, false) { // mint tokens to receiver in the amount of packet_data.amount in the denom of denom (with // the source removed, since we're the source) let prefix = format!( @@ -289,7 +289,7 @@ async fn recv_transfer_packet_inner( source_chan = msg.packet.chan_on_a ); - let unprefixed_denom: asset::Metadata = packet_data + let denom: asset::Metadata = packet_data .denom .strip_prefix(&prefix) .context(format!( @@ -301,7 +301,7 @@ async fn recv_transfer_packet_inner( let value: Value = Value { amount: receiver_amount, - asset_id: unprefixed_denom.id(), + asset_id: denom.id(), }; // assume AppHandlerCheck has already been called, and we have enough balance to mint tokens to receiver @@ -309,7 +309,7 @@ async fn recv_transfer_packet_inner( let value_balance: Amount = state .get(&state_key::ics20_value_balance::by_asset_id( &msg.packet.chan_on_b, - &unprefixed_denom.id(), + &denom.id(), )) .await? .unwrap_or_else(Amount::zero); @@ -334,14 +334,6 @@ async fn recv_transfer_packet_inner( .context("unable to mint note when receiving ics20 transfer packet")?; // update the value balance - let value_balance: Amount = state - .get(&state_key::ics20_value_balance::by_asset_id( - &msg.packet.chan_on_b, - &unprefixed_denom.id(), - )) - .await? - .unwrap_or_else(Amount::zero); - // note: this arithmetic was checked above, but we do it again anyway. let new_value_balance = value_balance .checked_sub(&receiver_amount) @@ -459,14 +451,6 @@ async fn refund_tokens(mut state: S, packet: &Packet) -> Result<( .context("couldn't mint note in timeout_packet_inner")?; // update the value balance - let value_balance: Amount = state - .get(&state_key::ics20_value_balance::by_asset_id( - &packet.chan_on_a, - &denom.id(), - )) - .await? - .unwrap_or_else(Amount::zero); - // note: this arithmetic was checked above, but we do it again anyway. let new_value_balance = value_balance .checked_sub(&amount)