Skip to content

Commit

Permalink
shielded-pool: record events for transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Oct 1, 2024
1 parent d833dcf commit 17ce98c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
68 changes: 60 additions & 8 deletions crates/core/component/shielded-pool/src/component/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use crate::{
component::{AssetRegistry, NoteManager},
Ics20Withdrawal,
event, Ics20Withdrawal,
};
use anyhow::{Context, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -116,6 +116,14 @@ pub trait Ics20TransferWriteExt: StateWrite {
),
new_value_balance,
);
self.record_proto(event::outbound_fungible_token_transfer(
Value {
amount: withdrawal.amount,
asset_id: withdrawal.denom.id(),
},
&withdrawal.return_address,
withdrawal.destination_chain_address.clone(),
));
} else {
// receiver is the source, burn utxos

Expand Down Expand Up @@ -149,6 +157,14 @@ pub trait Ics20TransferWriteExt: StateWrite {
),
new_value_balance,
);
self.record_proto(event::outbound_fungible_token_transfer(
Value {
amount: withdrawal.amount,
asset_id: withdrawal.denom.id(),
},
&withdrawal.return_address,
withdrawal.destination_chain_address.clone(),
));
}

self.send_packet_execute(checked_packet).await;
Expand Down Expand Up @@ -352,6 +368,11 @@ async fn recv_transfer_packet_inner<S: StateWrite>(
state_key::ics20_value_balance::by_asset_id(&msg.packet.chan_on_b, &denom.id()),
new_value_balance,
);
state.record_proto(event::inbound_fungible_token_transfer(
value,
packet_data.sender.clone(),
&receiver_address,
));
} else {
// create new denom:
//
Expand Down Expand Up @@ -403,13 +424,22 @@ async fn recv_transfer_packet_inner<S: StateWrite>(
state_key::ics20_value_balance::by_asset_id(&msg.packet.chan_on_b, &denom.id()),
new_value_balance,
);
state.record_proto(event::inbound_fungible_token_transfer(
value,
packet_data.sender.clone(),
&receiver_address,
));
}

Ok(())
}

// see: https://github.com/cosmos/ibc/blob/8326e26e7e1188b95c32481ff00348a705b23700/spec/app/ics-020-fungible-token-transfer/README.md?plain=1#L297
async fn refund_tokens<S: StateWrite>(mut state: S, packet: &Packet) -> Result<()> {
async fn refund_tokens<S: StateWrite>(
mut state: S,
packet: &Packet,
reason: event::FungibleTokenRefundReason,
) -> Result<()> {
let packet_data: FungibleTokenPacketData = serde_json::from_slice(packet.data.as_slice())?;
let denom: asset::Metadata = packet_data // CRITICAL: verify that this denom is validated in upstream timeout handling
.denom
Expand Down Expand Up @@ -469,6 +499,13 @@ async fn refund_tokens<S: StateWrite>(mut state: S, packet: &Packet) -> Result<(
state_key::ics20_value_balance::by_asset_id(&packet.chan_on_a, &denom.id()),
new_value_balance,
);
// note, order flipped relative to the event.
state.record_proto(event::outbound_fungible_token_refund(
value,
&receiver,
packet_data.sender.clone(),
reason,
));
} else {
let value_balance: Amount = state
.get(&state_key::ics20_value_balance::by_asset_id(
Expand Down Expand Up @@ -497,6 +534,13 @@ async fn refund_tokens<S: StateWrite>(mut state: S, packet: &Packet) -> Result<(
state_key::ics20_value_balance::by_asset_id(&packet.chan_on_a, &denom.id()),
new_value_balance,
);
// note, order flipped relative to the event.
state.record_proto(event::outbound_fungible_token_refund(
value,
&receiver,
packet_data.sender.clone(),
reason,
));
}

Ok(())
Expand Down Expand Up @@ -535,9 +579,13 @@ impl AppHandlerExecute for Ics20Transfer {

async fn timeout_packet_execute<S: StateWrite>(mut state: S, msg: &MsgTimeout) -> Result<()> {
// timeouts may fail due to counterparty chains sending transfers of u128-1
refund_tokens(&mut state, &msg.packet)
.await
.context("able to timeout packet")?;
refund_tokens(
&mut state,
&msg.packet,
event::FungibleTokenRefundReason::Timeout,
)
.await
.context("able to timeout packet")?;

Ok(())
}
Expand All @@ -552,9 +600,13 @@ impl AppHandlerExecute for Ics20Transfer {
// in the case where a counterparty chain acknowledges a packet with an error,
// for example due to a middleware processing issue or other behavior,
// the funds should be unescrowed back to the packet sender.
refund_tokens(&mut state, &msg.packet)
.await
.context("unable to refund packet acknowledgement")?;
refund_tokens(
&mut state,
&msg.packet,
event::FungibleTokenRefundReason::Error,
)
.await
.context("unable to refund packet acknowledgement")?;
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/core/component/shielded-pool/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn output(note_payload: &NotePayload) -> EventOutput {

pub fn outbound_fungible_token_transfer(
value: Value,
sender: Address,
sender: &Address,
receiver: String,
) -> EventOutboundFungibleTokenTransfer {
EventOutboundFungibleTokenTransfer {
Expand All @@ -43,7 +43,7 @@ pub enum FungibleTokenRefundReason {

pub fn outbound_fungible_token_refund(
value: Value,
sender: Address,
sender: &Address,
receiver: String,
reason: FungibleTokenRefundReason,
) -> EventOutboundFungibleTokenRefund {
Expand All @@ -62,7 +62,7 @@ pub fn outbound_fungible_token_refund(
pub fn inbound_fungible_token_transfer(
value: Value,
sender: String,
receiver: Address,
receiver: &Address,
) -> EventInboundFungibleTokenTransfer {
EventInboundFungibleTokenTransfer {
value: Some(value.into()),
Expand Down

0 comments on commit 17ce98c

Please sign in to comment.