Skip to content

Commit 3044520

Browse files
fix: refactor unnecessary replies (#647)
* fix: make reply on success * fix: clear callback data * fix: chan open init remove reply * fix: remove reply chan open try * fix: remove reply from open confirm * fix: remove close init reply * fix: fix tests * fix: add unit tests * fix: add unit tests * fix: unit tests * fix: remove reply on ack * fix: fix format * fix: remove receive packet callback * chore: pass buils * fix: add tests * fix: refactor test setup * fix: cleanup test * fix: receive test * fix: refactor test * chore: pass build * fix: fix close init test * fix: add test for send packet * fix: test for close confirm * chore: pass build * fix: cargo fmt * fix: timeout packet reply never * fix: fix lint --------- Co-authored-by: sabinchitrakar <immortal.infidel@gmail.com>
1 parent d6f270d commit 3044520

32 files changed

+1642
-2366
lines changed

contracts/cosmwasm-vm/cw-common/src/raw_types.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use ibc_proto::protobuf::Protobuf;
5353
use self::channel::RawPacket;
5454
use crate::cw_types::CwPacket;
5555

56-
pub fn to_raw_packet(packet: CwPacket) -> RawPacket {
56+
pub fn to_raw_packet(packet: &CwPacket) -> RawPacket {
5757
let timestamp = packet.timeout.timestamp().map(|t| t.nanos()).unwrap_or(0);
5858

5959
let timeout_height = packet.timeout.block().map(|b| RawHeight {
@@ -62,11 +62,11 @@ pub fn to_raw_packet(packet: CwPacket) -> RawPacket {
6262
});
6363
RawPacket {
6464
sequence: packet.sequence,
65-
source_port: packet.src.port_id,
66-
source_channel: packet.src.channel_id,
67-
destination_port: packet.dest.port_id,
68-
destination_channel: packet.dest.channel_id,
69-
data: packet.data.0,
65+
source_port: packet.src.port_id.to_string(),
66+
source_channel: packet.src.channel_id.to_string(),
67+
destination_port: packet.dest.port_id.to_string(),
68+
destination_channel: packet.dest.channel_id.to_string(),
69+
data: packet.data.0.clone(),
7070
timeout_height,
7171
timeout_timestamp: timestamp,
7272
}

contracts/cosmwasm-vm/cw-ibc-core/src/constants.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const EXECUTE_ON_CHANNEL_OPEN_TRY: u64 = 422;
1515
pub const EXECUTE_ON_CHANNEL_OPEN_ACK_ON_MODULE: u64 = 432;
1616

1717
pub const EXECUTE_ON_CHANNEL_OPEN_CONFIRM_ON_MODULE: u64 = 442;
18+
1819
pub const EXECUTE_ON_CHANNEL_CLOSE_INIT: u64 = 45;
1920

2021
pub const EXECUTE_ON_CHANNEL_CLOSE_CONFIRM_ON_MODULE: u64 = 462;

contracts/cosmwasm-vm/cw-ibc-core/src/context.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ impl<'a> CwIbcCoreContext<'a> {
7575
data: &T,
7676
) -> Result<(), ContractError>
7777
where
78-
T: Serialize + ?Sized,
78+
T: DeserializeOwned + Serialize + ?Sized,
7979
{
80+
if self.has_callback_data(store, id) {
81+
return Err(ContractError::CallAlreadyInProgress);
82+
}
83+
8084
let bytes = to_vec(data).map_err(ContractError::Std)?;
8185
return self
8286
.cw_ibc_store
@@ -85,6 +89,14 @@ impl<'a> CwIbcCoreContext<'a> {
8589
.map_err(ContractError::Std);
8690
}
8791

92+
pub fn has_callback_data(&self, store: &dyn Storage, id: u64) -> bool {
93+
return self.ibc_store().callback_data().load(store, id).is_ok();
94+
}
95+
96+
pub fn clear_callback_data(&self, store: &mut dyn Storage, id: u64) {
97+
return self.ibc_store().callback_data().remove(store, id);
98+
}
99+
88100
pub fn get_callback_data<T: DeserializeOwned>(
89101
&self,
90102
store: &dyn Storage,

contracts/cosmwasm-vm/cw-ibc-core/src/contract.rs

-14
Original file line numberDiff line numberDiff line change
@@ -525,21 +525,7 @@ impl<'a> CwIbcCoreContext<'a> {
525525
EXECUTE_UPDATE_CLIENT => self.execute_update_client_reply(deps, env, message),
526526
EXECUTE_UPGRADE_CLIENT => self.execute_upgrade_client_reply(deps, env, message),
527527
MISBEHAVIOUR => self.execute_misbehaviour_reply(deps, env, message),
528-
EXECUTE_ON_CHANNEL_OPEN_INIT => self.execute_channel_open_init(deps, message),
529-
EXECUTE_ON_CHANNEL_OPEN_TRY => self.execute_channel_open_try(deps, message),
530-
EXECUTE_ON_CHANNEL_OPEN_ACK_ON_MODULE => self.execute_channel_open_ack(deps, message),
531-
EXECUTE_ON_CHANNEL_OPEN_CONFIRM_ON_MODULE => {
532-
self.execute_channel_open_confirm(deps, message)
533-
}
534-
EXECUTE_ON_CHANNEL_CLOSE_INIT => self.execute_channel_close_init(deps, message),
535-
EXECUTE_ON_CHANNEL_CLOSE_CONFIRM_ON_MODULE => {
536-
self.execute_channel_close_confirm(deps, message)
537-
}
538-
VALIDATE_ON_PACKET_TIMEOUT_ON_MODULE => self.execute_timeout_packet(deps, message),
539528
VALIDATE_ON_PACKET_RECEIVE_ON_MODULE => self.execute_receive_packet(deps, message),
540-
VALIDATE_ON_PACKET_ACKNOWLEDGEMENT_ON_MODULE => {
541-
self.acknowledgement_packet_execute(deps, message)
542-
}
543529

544530
_ => Err(ContractError::ReplyError {
545531
code: message.id,

contracts/cosmwasm-vm/cw-ibc-core/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ pub enum ContractError {
7777

7878
#[error("InvalidHeight")]
7979
InvalidHeight,
80+
81+
#[error("CallAlreadyInProgress")]
82+
CallAlreadyInProgress,
8083
}
8184

8285
impl From<FromHexError> for ContractError {

contracts/cosmwasm-vm/cw-ibc-core/src/ics02_client/handler.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ impl<'a> IbcClient for CwIbcCoreContext<'a> {
7272
funds: info.funds,
7373
});
7474

75-
let sub_msg: SubMsg = SubMsg::reply_always(create_client_message, EXECUTE_CREATE_CLIENT);
75+
let sub_msg: SubMsg =
76+
SubMsg::reply_on_success(create_client_message, EXECUTE_CREATE_CLIENT);
7677

7778
Ok(Response::new()
7879
.add_submessage(sub_msg)
@@ -214,7 +215,7 @@ impl<'a> IbcClient for CwIbcCoreContext<'a> {
214215
funds: info.funds,
215216
});
216217

217-
let sub_message = SubMsg::reply_always(wasm_msg, EXECUTE_UPGRADE_CLIENT);
218+
let sub_message = SubMsg::reply_on_success(wasm_msg, EXECUTE_UPGRADE_CLIENT);
218219

219220
Ok(Response::new()
220221
.add_submessage(sub_message)
@@ -532,7 +533,7 @@ impl<'a> IbcClient for CwIbcCoreContext<'a> {
532533
funds: info.funds,
533534
});
534535

535-
let sub_message = SubMsg::reply_always(wasm_exec_message, MISBEHAVIOUR);
536+
let sub_message = SubMsg::reply_on_success(wasm_exec_message, MISBEHAVIOUR);
536537

537538
Ok(Response::new()
538539
.add_submessage(sub_message)

contracts/cosmwasm-vm/cw-ibc-core/src/ics04_channel/events.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ pub fn create_channel_id_generated_event(channel_id: ChannelId) -> Event {
7979

8080
pub fn create_packet_event(
8181
event_type: IbcEventType,
82-
packet: RawPacket,
82+
packet: &RawPacket,
8383
channel_order: &Order,
8484
connection_id: &IbcConnectionId,
8585
ack: Option<Vec<u8>>,
8686
) -> Result<Event, ContractError> {
8787
let timeout_height = packet
8888
.timeout_height
89+
.as_ref()
8990
.map(|h| format!("{}-{}", h.revision_number, h.revision_height))
9091
.unwrap_or("0-0".to_string());
9192
let hex_data = hex::encode(&packet.data);

0 commit comments

Comments
 (0)