Skip to content

Commit

Permalink
Refactor/interchain core (#503)
Browse files Browse the repository at this point in the history
* refactor interchain core package

* better types and interfaces

* Nit

* formatting

* Added support for custom Outcomes

* Better doc comments

* Better comments for packet results

* Fix collaterals

* Changed names

* Changelog + doc tests

* Published new cw-orch-interchain

---------

Co-authored-by: cyberhoward <cyberhoward@protonmail.com>
  • Loading branch information
Kayanski and CyberHoward authored Oct 8, 2024
1 parent 0577cd3 commit b9f0957
Show file tree
Hide file tree
Showing 31 changed files with 793 additions and 548 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Breaking

- [cw-orch-interchain-core] Modify the structure and the names of the IBC analysis and following structure.

## 0.26.0 [8. October 2024]

Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ cw-orch-fns-derive = { path = "packages/macros/cw-orch-fns-derive", version = "0
cw-orch-clone-testing = { version = "0.8.0", path = "packages/clone-testing" }

# Interchain
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.6.0" }
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.7.0" }
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.7.0" }
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.7.0" }
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.7.0" }
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.8.0" }
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.8.0" }
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.8.0" }
cw-orch-starship = { path = "packages/interchain/starship", version = "0.6.0" }
cw-orch-proto = { path = "packages/interchain/proto", version = "0.7.0" }
cw-orch-proto = { path = "packages/interchain/proto", version = "0.8.0" }


thiserror = { version = "1.0.63" }
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-interchain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain"
version = "0.6.0"
version = "0.7.0"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions cw-orch-interchain/examples/doc_daemon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use cw_orch::prelude::networks::{LOCAL_JUNO, LOCAL_MIGALOO, LOCAL_OSMO};
use cw_orch::prelude::*;
use cw_orch_interchain::{
ChannelCreationValidator, ChannelCreator, DaemonInterchain, InterchainEnv, Starship,
};
use cw_orch_interchain::prelude::*;

fn create_daemon_env() -> cw_orch::anyhow::Result<DaemonInterchain> {
// ANCHOR: DAEMON_INTERCHAIN_CREATION
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-interchain/examples/doc_mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cw_orch::prelude::*;
use cw_orch_interchain::{InterchainEnv, MockInterchainEnv};
use cw_orch_interchain::prelude::*;
use ibc_relayer_types::core::ics24_host::identifier::PortId;

fn crate_mock_env() -> cw_orch::anyhow::Result<MockInterchainEnv> {
Expand Down
4 changes: 2 additions & 2 deletions cw-orch-interchain/examples/follow_packets_txhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cw_orch::{
environment::{ChainInfo, NetworkInfo},
prelude::networks::osmosis::OSMOSIS_1,
};
use cw_orch_interchain_daemon::{ChannelCreationValidator, DaemonInterchain};
use cw_orch_interchain::prelude::*;

pub const NOBLE: NetworkInfo = NetworkInfo {
chain_name: "noble",
Expand Down Expand Up @@ -37,7 +37,7 @@ fn follow_by_tx_hash() -> cw_orch::anyhow::Result<()> {
src_chain.chain_id,
"D2C5459C54B394C168B8DFA214670FF9E2A0349CCBEF149CF5CB508A5B3BCB84".to_string(),
)?
.into_result()?;
.assert()?;

Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions cw-orch-interchain/examples/timeout_packet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmos_sdk_proto::traits::{Message, Name};
use cw_orch::{environment::QueryHandler, prelude::*};
use cw_orch_interchain_core::InterchainEnv;
use cw_orch_interchain_daemon::ChannelCreator as _;
use cw_orch_interchain::prelude::*;
use cw_orch_interchain_core::IbcPacketOutcome;
use cw_orch_starship::Starship;
use ibc_proto::ibc::{
applications::transfer::v1::{MsgTransfer, MsgTransferResponse},
Expand Down Expand Up @@ -59,9 +59,9 @@ fn main() -> cw_orch::anyhow::Result<()> {

let result = interchain.await_packets("juno-1", tx_resp)?;

match &result.packets[0].outcome {
cw_orch_interchain_core::types::IbcPacketOutcome::Timeout { .. } => {}
cw_orch_interchain_core::types::IbcPacketOutcome::Success { .. } => {
match &result.packets[0] {
IbcPacketOutcome::Timeout { .. } => {}
IbcPacketOutcome::Success { .. } => {
panic!("Expected timeout")
}
}
Expand Down
18 changes: 13 additions & 5 deletions cw-orch-interchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[cfg(not(target_arch = "wasm32"))]
pub mod prelude {
pub use cw_orch_interchain_core::{
types::ChannelCreationResult, IbcQueryHandler, InterchainEnv,
results::ChannelCreationResult, IbcQueryHandler, InterchainEnv, PacketAnalysis,
};
pub use cw_orch_interchain_mock::{MockBech32InterchainEnv, MockInterchainEnv};

Expand All @@ -20,15 +20,23 @@ pub mod prelude {
}

#[cfg(not(target_arch = "wasm32"))]
pub use cw_orch_interchain_core::*;
pub mod core {
pub use cw_orch_interchain_core::*;
}

#[cfg(not(target_arch = "wasm32"))]
pub use cw_orch_interchain_mock::*;
pub mod mock {
pub use cw_orch_interchain_mock::*;
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "daemon")]
pub use cw_orch_interchain_daemon::*;
pub mod daemon {
pub use cw_orch_interchain_daemon::*;
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "daemon")]
pub use cw_orch_starship::*;
pub mod starship {
pub use cw_orch_starship::*;
}
6 changes: 3 additions & 3 deletions cw-orch-interchain/tests/timeout_packet_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn timeout_packet_mock() -> cw_orch::anyhow::Result<()> {

let result = interchain.await_packets("juno-1", tx_resp)?;

match &result.packets[0].outcome {
cw_orch_interchain_core::types::IbcPacketOutcome::Timeout { .. } => {}
cw_orch_interchain_core::types::IbcPacketOutcome::Success { .. } => {
match &result.packets[0] {
cw_orch_interchain_core::IbcPacketOutcome::Timeout { .. } => {}
cw_orch_interchain_core::IbcPacketOutcome::Success { .. } => {
panic!("Expected timeout")
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/cw-plus/tests/interface_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ mod cw20_ics {
msg::AllowedInfo,
};
use cw_orch::prelude::*;
use cw_orch_interchain::{env::contract_port, prelude::*};
use cw_orch_interchain::{core::contract_port, prelude::*};
use cw_plus_orch::{
cw20_base::{Cw20Base, ExecuteMsgInterfaceFns as _},
cw20_ics20::{
Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/interchain-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain-core"
version = "0.7.0"
version = "0.8.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
75 changes: 41 additions & 34 deletions packages/interchain/interchain-core/src/ack_parser.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{from_json, Binary};
use cw_orch_core::environment::CwEnv;
use prost::Message;
// TODO: when polytone updates to cosmwasm v2 use polytone::ack::Callback;
use polytone_callback::Callback;

use crate::{
env::decode_ack_error,
types::{parse::SuccessIbcPacket, IbcTxAnalysis},
InterchainError,
};
use crate::{packet::success::IbcAppResult, InterchainError};

use self::acknowledgement::{Acknowledgement, Response};

/// Struct used to centralize all the pre-defined ack types
pub enum IbcAckParser {}

impl IbcAckParser {
/// Verifies if the given ack is an Polytone type and returns the acknowledgement if it is
/// Verifies if the given ack is an Polytone type and returns the parsed acknowledgement if it is
///
/// Returns an error if there was an error in the process
pub fn polytone_ack(ack: &Binary) -> Result<Callback, InterchainError> {
Expand Down Expand Up @@ -83,6 +78,44 @@ impl IbcAckParser {
}
Err(decode_ack_error(ack))
}

/// Verifies if the given ack is a standard acknowledgement type
///
/// Returns an error if there was an error in the parsing process
pub fn any_standard_app_result(ack: &Binary) -> Result<IbcAppResult, InterchainError> {
if let Ok(ack) = IbcAckParser::polytone_ack(ack) {
Ok(IbcAppResult::Polytone(ack))
} else if IbcAckParser::ics20_ack(ack).is_ok() {
Ok(IbcAppResult::Ics20)
} else if let Ok(ack) = IbcAckParser::ics004_ack(ack) {
Ok(IbcAppResult::Ics004(ack))
} else {
Err(InterchainError::AckDecodingFailed(
ack.clone(),
String::from_utf8_lossy(ack.as_slice()).to_string(),
))
}
}

/// Verifies if the given ack custom acknowledgement type.
/// If it fails, tries to parse into standard ack types
///
/// Returns an error if there was an error in the parsing process
pub fn any_standard_app_result_with_custom<CustomResult>(
ack: &Binary,
parsing_func: fn(&Binary) -> Result<CustomResult, InterchainError>,
) -> Result<IbcAppResult<CustomResult>, InterchainError> {
parsing_func(ack)
.map(IbcAppResult::Custom)
.or_else(|_| Self::any_standard_app_result(ack).map(|ack| ack.into_custom()))
}
}

pub(crate) fn decode_ack_error(ack: &Binary) -> InterchainError {
InterchainError::AckDecodingFailed(
ack.clone(),
String::from_utf8_lossy(ack.as_slice()).to_string(),
)
}

#[cw_serde]
Expand All @@ -94,32 +127,6 @@ pub enum FungibleTokenPacketAcknowledgement {
Error(String),
}

impl<Chain: CwEnv> IbcTxAnalysis<Chain> {
/// Assert that all packets were not timeout
pub fn assert_no_timeout(&self) -> Result<Vec<SuccessIbcPacket<Chain>>, InterchainError> {
Ok(self
.packets
.iter()
.map(|p| p.assert_no_timeout())
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect())
}

/// Returns all packets that were successful without asserting there was no timeout
pub fn get_success_packets(&self) -> Result<Vec<SuccessIbcPacket<Chain>>, InterchainError> {
Ok(self
.packets
.iter()
.map(|p| p.get_success_packets())
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.collect())
}
}

/// This is copied from https://github.com/cosmos/cosmos-rust/blob/4f2e3bbf9c67c8ffef44ef1e485a327fd66f060a/cosmos-sdk-proto/src/prost/ibc-go/ibc.core.channel.v1.rs#L164
/// This is the ICS-004 standard proposal
pub mod acknowledgement {
Expand Down Expand Up @@ -149,7 +156,7 @@ pub mod acknowledgement {
}
}

mod polytone_callback {
pub mod polytone_callback {
use super::*;

use cosmwasm_std::{SubMsgResponse, Uint64};
Expand Down
Loading

0 comments on commit b9f0957

Please sign in to comment.