Skip to content

Commit b9f0957

Browse files
Refactor/interchain core (#503)
* 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 <[email protected]>
1 parent 0577cd3 commit b9f0957

31 files changed

+793
-548
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
### Breaking
77

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

910
## 0.26.0 [8. October 2024]
1011

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ cw-orch-fns-derive = { path = "packages/macros/cw-orch-fns-derive", version = "0
6464
cw-orch-clone-testing = { version = "0.8.0", path = "packages/clone-testing" }
6565

6666
# Interchain
67-
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.6.0" }
68-
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.7.0" }
69-
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.7.0" }
70-
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.7.0" }
67+
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.7.0" }
68+
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.8.0" }
69+
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.8.0" }
70+
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.8.0" }
7171
cw-orch-starship = { path = "packages/interchain/starship", version = "0.6.0" }
72-
cw-orch-proto = { path = "packages/interchain/proto", version = "0.7.0" }
72+
cw-orch-proto = { path = "packages/interchain/proto", version = "0.8.0" }
7373

7474

7575
thiserror = { version = "1.0.63" }

cw-orch-interchain/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cw-orch-interchain"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
authors = { workspace = true }
55
edition = { workspace = true }
66
license = { workspace = true }

cw-orch-interchain/examples/doc_daemon.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use cw_orch::prelude::networks::{LOCAL_JUNO, LOCAL_MIGALOO, LOCAL_OSMO};
22
use cw_orch::prelude::*;
3-
use cw_orch_interchain::{
4-
ChannelCreationValidator, ChannelCreator, DaemonInterchain, InterchainEnv, Starship,
5-
};
3+
use cw_orch_interchain::prelude::*;
64

75
fn create_daemon_env() -> cw_orch::anyhow::Result<DaemonInterchain> {
86
// ANCHOR: DAEMON_INTERCHAIN_CREATION

cw-orch-interchain/examples/doc_mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cw_orch::prelude::*;
2-
use cw_orch_interchain::{InterchainEnv, MockInterchainEnv};
2+
use cw_orch_interchain::prelude::*;
33
use ibc_relayer_types::core::ics24_host::identifier::PortId;
44

55
fn crate_mock_env() -> cw_orch::anyhow::Result<MockInterchainEnv> {

cw-orch-interchain/examples/follow_packets_txhash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use cw_orch::{
33
environment::{ChainInfo, NetworkInfo},
44
prelude::networks::osmosis::OSMOSIS_1,
55
};
6-
use cw_orch_interchain_daemon::{ChannelCreationValidator, DaemonInterchain};
6+
use cw_orch_interchain::prelude::*;
77

88
pub const NOBLE: NetworkInfo = NetworkInfo {
99
chain_name: "noble",
@@ -37,7 +37,7 @@ fn follow_by_tx_hash() -> cw_orch::anyhow::Result<()> {
3737
src_chain.chain_id,
3838
"D2C5459C54B394C168B8DFA214670FF9E2A0349CCBEF149CF5CB508A5B3BCB84".to_string(),
3939
)?
40-
.into_result()?;
40+
.assert()?;
4141

4242
Ok(())
4343
}

cw-orch-interchain/examples/timeout_packet.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cosmos_sdk_proto::traits::{Message, Name};
22
use cw_orch::{environment::QueryHandler, prelude::*};
3-
use cw_orch_interchain_core::InterchainEnv;
4-
use cw_orch_interchain_daemon::ChannelCreator as _;
3+
use cw_orch_interchain::prelude::*;
4+
use cw_orch_interchain_core::IbcPacketOutcome;
55
use cw_orch_starship::Starship;
66
use ibc_proto::ibc::{
77
applications::transfer::v1::{MsgTransfer, MsgTransferResponse},
@@ -59,9 +59,9 @@ fn main() -> cw_orch::anyhow::Result<()> {
5959

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

62-
match &result.packets[0].outcome {
63-
cw_orch_interchain_core::types::IbcPacketOutcome::Timeout { .. } => {}
64-
cw_orch_interchain_core::types::IbcPacketOutcome::Success { .. } => {
62+
match &result.packets[0] {
63+
IbcPacketOutcome::Timeout { .. } => {}
64+
IbcPacketOutcome::Success { .. } => {
6565
panic!("Expected timeout")
6666
}
6767
}

cw-orch-interchain/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#[cfg(not(target_arch = "wasm32"))]
33
pub mod prelude {
44
pub use cw_orch_interchain_core::{
5-
types::ChannelCreationResult, IbcQueryHandler, InterchainEnv,
5+
results::ChannelCreationResult, IbcQueryHandler, InterchainEnv, PacketAnalysis,
66
};
77
pub use cw_orch_interchain_mock::{MockBech32InterchainEnv, MockInterchainEnv};
88

@@ -20,15 +20,23 @@ pub mod prelude {
2020
}
2121

2222
#[cfg(not(target_arch = "wasm32"))]
23-
pub use cw_orch_interchain_core::*;
23+
pub mod core {
24+
pub use cw_orch_interchain_core::*;
25+
}
2426

2527
#[cfg(not(target_arch = "wasm32"))]
26-
pub use cw_orch_interchain_mock::*;
28+
pub mod mock {
29+
pub use cw_orch_interchain_mock::*;
30+
}
2731

2832
#[cfg(not(target_arch = "wasm32"))]
2933
#[cfg(feature = "daemon")]
30-
pub use cw_orch_interchain_daemon::*;
34+
pub mod daemon {
35+
pub use cw_orch_interchain_daemon::*;
36+
}
3137

3238
#[cfg(not(target_arch = "wasm32"))]
3339
#[cfg(feature = "daemon")]
34-
pub use cw_orch_starship::*;
40+
pub mod starship {
41+
pub use cw_orch_starship::*;
42+
}

cw-orch-interchain/tests/timeout_packet_mock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ fn timeout_packet_mock() -> cw_orch::anyhow::Result<()> {
4646

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

49-
match &result.packets[0].outcome {
50-
cw_orch_interchain_core::types::IbcPacketOutcome::Timeout { .. } => {}
51-
cw_orch_interchain_core::types::IbcPacketOutcome::Success { .. } => {
49+
match &result.packets[0] {
50+
cw_orch_interchain_core::IbcPacketOutcome::Timeout { .. } => {}
51+
cw_orch_interchain_core::IbcPacketOutcome::Success { .. } => {
5252
panic!("Expected timeout")
5353
}
5454
}

packages/integrations/cw-plus/tests/interface_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ mod cw20_ics {
321321
msg::AllowedInfo,
322322
};
323323
use cw_orch::prelude::*;
324-
use cw_orch_interchain::{env::contract_port, prelude::*};
324+
use cw_orch_interchain::{core::contract_port, prelude::*};
325325
use cw_plus_orch::{
326326
cw20_base::{Cw20Base, ExecuteMsgInterfaceFns as _},
327327
cw20_ics20::{

packages/interchain/interchain-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cw-orch-interchain-core"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors.workspace = true
55
edition.workspace = true
66
license.workspace = true

packages/interchain/interchain-core/src/ack_parser.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
use cosmwasm_schema::cw_serde;
22
use cosmwasm_std::{from_json, Binary};
3-
use cw_orch_core::environment::CwEnv;
43
use prost::Message;
54
// TODO: when polytone updates to cosmwasm v2 use polytone::ack::Callback;
65
use polytone_callback::Callback;
76

8-
use crate::{
9-
env::decode_ack_error,
10-
types::{parse::SuccessIbcPacket, IbcTxAnalysis},
11-
InterchainError,
12-
};
7+
use crate::{packet::success::IbcAppResult, InterchainError};
138

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

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

1914
impl IbcAckParser {
20-
/// Verifies if the given ack is an Polytone type and returns the acknowledgement if it is
15+
/// Verifies if the given ack is an Polytone type and returns the parsed acknowledgement if it is
2116
///
2217
/// Returns an error if there was an error in the process
2318
pub fn polytone_ack(ack: &Binary) -> Result<Callback, InterchainError> {
@@ -83,6 +78,44 @@ impl IbcAckParser {
8378
}
8479
Err(decode_ack_error(ack))
8580
}
81+
82+
/// Verifies if the given ack is a standard acknowledgement type
83+
///
84+
/// Returns an error if there was an error in the parsing process
85+
pub fn any_standard_app_result(ack: &Binary) -> Result<IbcAppResult, InterchainError> {
86+
if let Ok(ack) = IbcAckParser::polytone_ack(ack) {
87+
Ok(IbcAppResult::Polytone(ack))
88+
} else if IbcAckParser::ics20_ack(ack).is_ok() {
89+
Ok(IbcAppResult::Ics20)
90+
} else if let Ok(ack) = IbcAckParser::ics004_ack(ack) {
91+
Ok(IbcAppResult::Ics004(ack))
92+
} else {
93+
Err(InterchainError::AckDecodingFailed(
94+
ack.clone(),
95+
String::from_utf8_lossy(ack.as_slice()).to_string(),
96+
))
97+
}
98+
}
99+
100+
/// Verifies if the given ack custom acknowledgement type.
101+
/// If it fails, tries to parse into standard ack types
102+
///
103+
/// Returns an error if there was an error in the parsing process
104+
pub fn any_standard_app_result_with_custom<CustomResult>(
105+
ack: &Binary,
106+
parsing_func: fn(&Binary) -> Result<CustomResult, InterchainError>,
107+
) -> Result<IbcAppResult<CustomResult>, InterchainError> {
108+
parsing_func(ack)
109+
.map(IbcAppResult::Custom)
110+
.or_else(|_| Self::any_standard_app_result(ack).map(|ack| ack.into_custom()))
111+
}
112+
}
113+
114+
pub(crate) fn decode_ack_error(ack: &Binary) -> InterchainError {
115+
InterchainError::AckDecodingFailed(
116+
ack.clone(),
117+
String::from_utf8_lossy(ack.as_slice()).to_string(),
118+
)
86119
}
87120

88121
#[cw_serde]
@@ -94,32 +127,6 @@ pub enum FungibleTokenPacketAcknowledgement {
94127
Error(String),
95128
}
96129

97-
impl<Chain: CwEnv> IbcTxAnalysis<Chain> {
98-
/// Assert that all packets were not timeout
99-
pub fn assert_no_timeout(&self) -> Result<Vec<SuccessIbcPacket<Chain>>, InterchainError> {
100-
Ok(self
101-
.packets
102-
.iter()
103-
.map(|p| p.assert_no_timeout())
104-
.collect::<Result<Vec<_>, _>>()?
105-
.into_iter()
106-
.flatten()
107-
.collect())
108-
}
109-
110-
/// Returns all packets that were successful without asserting there was no timeout
111-
pub fn get_success_packets(&self) -> Result<Vec<SuccessIbcPacket<Chain>>, InterchainError> {
112-
Ok(self
113-
.packets
114-
.iter()
115-
.map(|p| p.get_success_packets())
116-
.collect::<Result<Vec<_>, _>>()?
117-
.into_iter()
118-
.flatten()
119-
.collect())
120-
}
121-
}
122-
123130
/// 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
124131
/// This is the ICS-004 standard proposal
125132
pub mod acknowledgement {
@@ -149,7 +156,7 @@ pub mod acknowledgement {
149156
}
150157
}
151158

152-
mod polytone_callback {
159+
pub mod polytone_callback {
153160
use super::*;
154161

155162
use cosmwasm_std::{SubMsgResponse, Uint64};

0 commit comments

Comments
 (0)