Skip to content

Commit

Permalink
new is_receiving_success
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed May 27, 2024
1 parent 54c555d commit 98bf4e4
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ use namada_core::ibc::apps::transfer::handler::{
};
use namada_core::ibc::apps::transfer::types::error::TokenTransferError;
use namada_core::ibc::apps::transfer::types::{
is_receiver_chain_source, TracePrefix,
ack_success_b64, is_receiver_chain_source, TracePrefix,
};
use namada_core::ibc::core::channel::types::acknowledgement::{
Acknowledgement, AcknowledgementStatus,
};
use namada_core::ibc::core::channel::types::msgs::PacketMsg;
use namada_core::ibc::core::channel::types::commitment::compute_ack_commitment;
use namada_core::ibc::core::channel::types::msgs::{
MsgRecvPacket as IbcMsgRecvPacket, PacketMsg,
};
use namada_core::ibc::core::entrypoint::{execute, validate};
use namada_core::ibc::core::handler::types::error::ContextError;
use namada_core::ibc::core::handler::types::events::Error as RawIbcEventError;
Expand Down Expand Up @@ -169,7 +172,13 @@ where
MsgEnvelope::Packet(PacketMsg::Recv(msg.message.clone()));
execute(&mut self.ctx, &mut self.router, envelope)
.map_err(|e| Error::Context(Box::new(e)))?;
Ok(msg.transfer.clone())
let transfer = if self.is_receiving_success(&msg.message)? {
// For receiving the token to a shielded address
msg.transfer.clone()
} else {
None
};
Ok(transfer)
}
IbcMessage::AckPacket(msg) => {
let envelope =
Expand Down Expand Up @@ -201,6 +210,28 @@ where
}
}

/// Check the result of receiving the packet by checking the packet
/// acknowledgement
fn is_receiving_success(
&self,
msg: &IbcMsgRecvPacket,
) -> Result<bool, Error> {
let packet_ack = self
.ctx
.inner
.borrow()
.packet_ack(
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
msg.packet.seq_on_a,
)
.map_err(|e| Error::Context(Box::new(e)))?;
let success_ack_commitment = compute_ack_commitment(
&AcknowledgementStatus::success(ack_success_b64()).into(),
);
Ok(packet_ack == success_ack_commitment)
}

/// Validate according to the message in IBC VP
pub fn validate(&self, tx_data: &[u8]) -> Result<(), Error> {
// Use an empty verifiers set placeholder for validation, this is only
Expand Down

0 comments on commit 98bf4e4

Please sign in to comment.