Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch Polytone callback errors #517

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions packages/interchain/interchain-core/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use crate::tx::TxId;
use crate::{IbcAckParser, InterchainError};
use cosmwasm_std::{Binary, Empty};
use cw_orch_core::environment::CwEnv;
use cw_orch_core::environment::IndexResponse;

/// Trait used for analysis of IBC packet flows
pub trait PacketAnalysis {
pub trait PacketAnalysis: IndexResponse {
/// Result of the Analysis of the packet flows
type AnalysisResult<CustomResult>;
type AnalysisResult<CustomResult>: IndexResponse;

/// Asserts that there is no timeout packet inside the result structure.
fn assert_no_timeout(&self) -> Result<(), InterchainError>;
Expand Down Expand Up @@ -69,6 +70,45 @@ impl<T: PacketAnalysis> PacketAnalysis for IbcPacketOutcome<T> {
ack,
} => {
let ibc_app_result = IbcAckParser::any_standard_app_result(&ack)?;

// In case we have a polytone result, we verify that the execution result is not a failure
if let crate::IbcAppResult::Polytone(_) = ibc_app_result {
// An execution failure in the ack for polytone, has those events :
// "method" --> "reply_callback_error"
// "packet_sequence" --> <packet-sequence>
// "callback_error" --> <callback-error> to
let callback_error = ack_tx
.events()
.into_iter()
.filter(|e| e.ty == "wasm")
.filter_map(|e| {
let method_position = e.attributes.iter().position(|a| {
a.key == "method" && a.value == "reply_callback_error"
})?;
if let Some(packet_sequence) = e.attributes.get(method_position + 1) {
if packet_sequence.key != "packet_sequence" {
return None;
}
} else {
return None;
}
if let Some(packet_sequence) = e.attributes.get(method_position + 2) {
if packet_sequence.key != "callback_error" {
None
} else {
Some(packet_sequence.value.clone())
}
} else {
None
}
})
.next();

if let Some(callback_error) = callback_error {
return Err(InterchainError::CallbackError(callback_error));
}
};

Ok(IbcPacketResult {
receive_tx: receive_tx.assert()?,
ack_tx: ack_tx.assert()?,
Expand Down
3 changes: 3 additions & 0 deletions packages/interchain/interchain-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ pub enum InterchainError {

#[error("Failure acknowledgment received: {0:?}")]
FailedAckReceived(String),

#[error("Callback Failure during acknowledgment : {0:?}")]
CallbackError(String),
}
8 changes: 4 additions & 4 deletions packages/interchain/interchain-core/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub mod success {
/// Success packet outcome. This is the result of a packet analysis.
/// The T generic is used to allow for raw transactions or analyzed transactions to be used
#[derive(Debug, PartialEq, Clone)]
pub struct IbcPacketResult<T, CustomResult = Empty> {
pub struct IbcPacketResult<T: IndexResponse, CustomResult = Empty> {
/// The packets gets transmitted to the dst chain
pub receive_tx: T,
/// The ack is broadcasted back on the src chain
Expand Down Expand Up @@ -238,7 +238,7 @@ pub mod success {
Vec<IbcPacketResult<SuccessNestedPacketsFlow<Chain, CustomResult>, CustomResult>>,
}

impl<T: IndexResponse> IndexResponse for IbcPacketResult<T> {
impl<T: IndexResponse, CustomResult> IndexResponse for IbcPacketResult<T, CustomResult> {
fn events(&self) -> Vec<cosmwasm_std::Event> {
[self.receive_tx.events(), self.ack_tx.events()].concat()
}
Expand Down Expand Up @@ -266,7 +266,7 @@ pub mod success {
}
}

impl<Chain: CwEnv> IndexResponse for SuccessSinglePacketFlow<Chain> {
impl<Chain: CwEnv, CustomResult> IndexResponse for SuccessSinglePacketFlow<Chain, CustomResult> {
fn events(&self) -> Vec<cosmwasm_std::Event> {
let mut events: Vec<_> = self
.send_tx
Expand Down Expand Up @@ -311,7 +311,7 @@ pub mod success {
}
}

impl<Chain: CwEnv> IndexResponse for SuccessNestedPacketsFlow<Chain> {
impl<Chain: CwEnv, CustomResult> IndexResponse for SuccessNestedPacketsFlow<Chain, CustomResult> {
fn events(&self) -> Vec<cosmwasm_std::Event> {
let mut self_events = self.tx_id.response.events();
let other_events = self
Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/interchain-core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<C: CwEnv, CustomResult: std::fmt::Debug> std::fmt::Debug for TxId<C, Custom
}
}

impl<Chain: CwEnv, CustomResult: std::fmt::Debug> IndexResponse for TxId<Chain, CustomResult> {
impl<Chain: CwEnv, CustomResult> IndexResponse for TxId<Chain, CustomResult> {
fn events(&self) -> Vec<cosmwasm_std::Event> {
self.response.events()
}
Expand Down
Loading