Skip to content

IBC implementation review #234

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

Draft
wants to merge 38 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
063e317
Prepared develop branch for IBC support.
DariuszDepta Jun 11, 2024
b115e83
Merge remote-tracking branch 'origin' into develop
DariuszDepta Jun 12, 2024
cd1fdda
Merge remote-tracking branch 'origin' into develop
DariuszDepta Jun 12, 2024
d246f37
Add IBC capabilities
Kayanski Jul 2, 2024
34693c2
Merged last updates from main.
DariuszDepta Jul 2, 2024
e1a2f58
Fixed formatting.
DariuszDepta Jul 2, 2024
cf44cba
Merged changes from main.
DariuszDepta Jul 10, 2024
a7c16b4
Updated with changes from main.
DariuszDepta Jul 10, 2024
0793fdb
Prepared version 3.0.0-ibc-aplha.0
DariuszDepta Jul 10, 2024
a715965
Updated changelog.
DariuszDepta Aug 6, 2024
5da82d6
Upgraded dependencies.
DariuszDepta Aug 6, 2024
3cc2498
Upgraded dependencies.
DariuszDepta Jan 23, 2025
0d4ffc4
Updates.
DariuszDepta Jan 23, 2025
992ba34
Refactoring.
DariuszDepta Jan 23, 2025
66ea3e6
Upgraded CI configuration.
DariuszDepta Jan 23, 2025
8264805
Upgraded CI configuration.
DariuszDepta Jan 23, 2025
e6c9bc8
Updates.
DariuszDepta Jan 23, 2025
7c66fc6
Updates.
DariuszDepta Jan 23, 2025
40dd77f
Updated README.
DariuszDepta Jan 23, 2025
dfe38fb
Updates.
DariuszDepta Jan 23, 2025
f57fe42
Updates.
DariuszDepta Jan 23, 2025
b148f8f
Merged changes from main.
DariuszDepta Jan 23, 2025
c830fc7
Updated README.
DariuszDepta Jan 23, 2025
0bbadba
Updates.
DariuszDepta Jan 23, 2025
9a25d02
Refactored contract trait.
DariuszDepta Jan 23, 2025
2b0c160
Updates.
DariuszDepta Jan 23, 2025
4d12d7f
Added IBC callbacks to Contract trait.
DariuszDepta Jan 23, 2025
11fa994
Updates.
DariuszDepta Jan 23, 2025
1c1c4e1
Refactored dependencies.
DariuszDepta Jan 23, 2025
73851a0
Merged changes from main.
DariuszDepta Jan 24, 2025
efcbe24
Updated table in documentation. (#237)
DariuszDepta Jan 24, 2025
f7ce727
Updates.
DariuszDepta Jan 24, 2025
2aa4276
Merge remote-tracking branch 'origin' into develop
DariuszDepta Jan 24, 2025
8285832
Merge remote-tracking branch 'origin' into develop
DariuszDepta Jan 28, 2025
09443dc
Merged changes from main.
DariuszDepta Jan 30, 2025
faaac1f
Merge remote-tracking branch 'origin' into develop
DariuszDepta Feb 3, 2025
81b969a
Merged changes from main.
DariuszDepta Mar 6, 2025
36d42aa
Merged changes from main.
DariuszDepta Apr 4, 2025
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
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ cosmwasm-schema = "2.2.2"
cosmwasm-std = "2.2.2"
cw-storage-plus = "2.0.0"
cw-utils = "2.0.0"
cw20-ics20 = "2.0.0"
itertools = "0.14.0"
prost = "0.13.5"
schemars = "0.8.22"
serde = "1.0.219"
serde_json = "1.0.140"
sha2 = "0.10.8"
thiserror = "2.0.12"

[dev-dependencies]
base64 = "0.22.1"
hex = "0.4.3"
hex-literal = "0.4.1"
serde_json = "1.0.140"
156 changes: 149 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
Distribution, DistributionKeeper, StakeKeeper, Staking, StakingSudo,
};
use crate::gov::Gov;
use crate::ibc::Ibc;
use crate::ibc::{
types::IbcResponse, types::MockIbcQuery, IbcModuleMsg, IbcPacketRelayingMsg as IbcSudo,
};
use crate::ibc::{Ibc, IbcSimpleModule};
use crate::module::{FailingModule, Module};
use crate::prefixed_storage::{
prefixed, prefixed_multilevel, prefixed_multilevel_read, prefixed_read,
};
use crate::transactions::transactional;
use crate::wasm::{ContractData, Wasm, WasmKeeper, WasmSudo};
use crate::{AppBuilder, GovFailingModule, IbcFailingModule, Stargate, StargateFailing};
use crate::{AppBuilder, GovFailingModule, Stargate, StargateFailing};
use cosmwasm_std::testing::{MockApi, MockStorage};
use cosmwasm_std::{
from_json, to_json_binary, Addr, Api, Binary, BlockInfo, ContractResult, CosmosMsg, CustomMsg,
Expand Down Expand Up @@ -41,7 +44,7 @@
WasmKeeper<ExecC, QueryC>,
StakeKeeper,
DistributionKeeper,
IbcFailingModule,
IbcSimpleModule,
GovFailingModule,
StargateFailing,
>;
Expand All @@ -58,7 +61,7 @@
Wasm = WasmKeeper<Empty, Empty>,
Staking = StakeKeeper,
Distr = DistributionKeeper,
Ibc = IbcFailingModule,
Ibc = IbcSimpleModule,
Gov = GovFailingModule,
Stargate = StargateFailing,
> {
Expand Down Expand Up @@ -94,7 +97,7 @@
WasmKeeper<Empty, Empty>,
StakeKeeper,
DistributionKeeper,
IbcFailingModule,
IbcSimpleModule,
GovFailingModule,
StargateFailing,
>,
Expand All @@ -119,7 +122,7 @@
WasmKeeper<ExecC, QueryC>,
StakeKeeper,
DistributionKeeper,
IbcFailingModule,
IbcSimpleModule,
GovFailingModule,
StargateFailing,
>,
Expand Down Expand Up @@ -484,6 +487,20 @@
})
}

/// Queries the IBC module
pub fn ibc_query(&self, query: MockIbcQuery) -> AnyResult<Binary> {
let Self {
block,
router,
api,
storage,
} = self;

let querier = router.querier(api, storage, block);

router.ibc.query(api, storage, &querier, block, query)
}

/// Runs arbitrary SudoMsg.
/// This will create a cache before the execution, so no state changes are persisted if this
/// returns an error, but all are persisted on success.
Expand Down Expand Up @@ -566,6 +583,8 @@
Staking(StakingSudo),
/// Wasm privileged actions.
Wasm(WasmSudo),
/// Ibc actions, used namely to create channels and relay packets
Ibc(IbcSudo),
}

impl From<WasmSudo> for SudoMsg {
Expand All @@ -585,6 +604,20 @@
SudoMsg::Staking(staking)
}
}

/// We use it to allow calling into modules from the ibc module. This is used for receiving packets
pub struct IbcRouterMsg {
pub module: IbcModule,
pub msg: IbcModuleMsg,
}

#[cosmwasm_schema::cw_serde]
pub enum IbcModule {
Wasm(Addr), // The wasm module needs to contain the wasm contract address (usually decoded from the port)
Bank,
Staking,
}

/// A trait representing the Cosmos based chain's router.
///
/// This trait is designed for routing messages within the Cosmos ecosystem.
Expand Down Expand Up @@ -623,6 +656,15 @@
block: &BlockInfo,
msg: SudoMsg,
) -> AnyResult<AppResponse>;

/// Evaluates all ibc related actions
fn ibc(
&self,
api: &dyn Api,
storage: &mut dyn Storage,
block: &BlockInfo,
msg: IbcRouterMsg,
) -> AnyResult<IbcResponse>;
}

impl<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT> CosmosRouter
Expand Down Expand Up @@ -695,7 +737,7 @@
#[cfg(feature = "staking")]
QueryRequest::Staking(req) => self.staking.query(api, storage, &querier, block, req),
#[cfg(feature = "stargate")]
QueryRequest::Ibc(req) => self.ibc.query(api, storage, &querier, block, req),
QueryRequest::Ibc(req) => self.ibc.query(api, storage, &querier, block, req.into()),
#[allow(deprecated)]
#[cfg(feature = "stargate")]
QueryRequest::Stargate { path, data } => self
Expand All @@ -719,9 +761,99 @@
SudoMsg::Bank(msg) => self.bank.sudo(api, storage, self, block, msg),
#[cfg(feature = "staking")]
SudoMsg::Staking(msg) => self.staking.sudo(api, storage, self, block, msg),
SudoMsg::Ibc(msg) => self.ibc.sudo(api, storage, self, block, msg),
_ => unimplemented!(),
}
}

fn ibc(
&self,
api: &dyn Api,
storage: &mut dyn Storage,
block: &BlockInfo,
msg: IbcRouterMsg,
) -> AnyResult<IbcResponse> {
match msg.module {
IbcModule::Bank => match msg.msg {
IbcModuleMsg::ChannelOpen(m) => self
.bank
.ibc_channel_open(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelConnect(m) => self
.bank
.ibc_channel_connect(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelClose(m) => self
.bank
.ibc_channel_close(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketReceive(m) => self
.bank
.ibc_packet_receive(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketAcknowledgement(m) => self
.bank
.ibc_packet_acknowledge(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketTimeout(m) => self
.bank
.ibc_packet_timeout(api, storage, self, block, m)
.map(Into::into),
},
IbcModule::Staking => match msg.msg {
IbcModuleMsg::ChannelOpen(m) => self
.staking
.ibc_channel_open(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelConnect(m) => self
.staking
.ibc_channel_connect(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelClose(m) => self
.staking
.ibc_channel_close(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketReceive(m) => self
.staking
.ibc_packet_receive(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketAcknowledgement(m) => self
.staking
.ibc_packet_acknowledge(api, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketTimeout(m) => self
.staking
.ibc_packet_timeout(api, storage, self, block, m)
.map(Into::into),

Check warning on line 827 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L803-L827

Added lines #L803 - L827 were not covered by tests
},
IbcModule::Wasm(contract_addr) => match msg.msg {
IbcModuleMsg::ChannelOpen(m) => self
.wasm
.ibc_channel_open(api, contract_addr, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelConnect(m) => self
.wasm
.ibc_channel_connect(api, contract_addr, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::ChannelClose(m) => self
.wasm
.ibc_channel_close(api, contract_addr, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketReceive(m) => self
.wasm
.ibc_packet_receive(api, contract_addr, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketAcknowledgement(m) => self
.wasm
.ibc_packet_acknowledge(api, contract_addr, storage, self, block, m)
.map(Into::into),
IbcModuleMsg::PacketTimeout(m) => self
.wasm
.ibc_packet_timeout(api, contract_addr, storage, self, block, m)
.map(Into::into),

Check warning on line 853 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L829-L853

Added lines #L829 - L853 were not covered by tests
},
}
}
}

pub struct MockRouter<ExecC, QueryC>(PhantomData<(ExecC, QueryC)>);
Expand Down Expand Up @@ -779,6 +911,16 @@
) -> AnyResult<AppResponse> {
panic!("Cannot sudo MockRouters");
}

fn ibc(

Check warning on line 915 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L915

Added line #L915 was not covered by tests
&self,
_api: &dyn Api,
_storage: &mut dyn Storage,
_block: &BlockInfo,
_msg: IbcRouterMsg,
) -> AnyResult<IbcResponse> {
panic!("Cannot ibc MockRouters");

Check warning on line 922 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L922

Added line #L922 was not covered by tests
}
}

pub struct RouterQuerier<'a, ExecC, QueryC> {
Expand Down
Loading