Skip to content

Commit 6a5eb08

Browse files
feat(ibc-union): alpha omega (#4043)
- simplify commitment for packets/acks (we no longer branch on 1 vs N packets, 1 packet is considered as a single element list of packets) - remove channel_id prefix - batch_send/batch_acks ensures packets are batched for same channel - fix forward ack/timeout incorrectly checking packet hash instead of salt - prefix cosmwasm store and forbids direct call to deps.storage.get, prefer read_commit - refactor voyager for the new interface - index important fields in evm - only yield packet hash on subsequent events after send
2 parents 35dd88b + a9ea673 commit 6a5eb08

File tree

28 files changed

+480
-577
lines changed

28 files changed

+480
-577
lines changed

cosmwasm/ibc-union/app/ucs03-zkgm/src/contract.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,12 @@ fn timeout_packet(
269269
packet: Packet,
270270
relayer: Addr,
271271
) -> Result<Response, ContractError> {
272+
let zkgm_packet = ZkgmPacket::abi_decode_params(&packet.data, true)?;
272273
// Check if this is an in-flight packet (forwarded packet)
273-
let packet_hash = keccak256(packet.abi_encode());
274-
let is_forward_tinted = is_salt_forward_tinted(packet_hash);
275-
276-
if is_forward_tinted {
274+
if is_forwarded_packet(zkgm_packet.salt.0.into()) {
277275
// This is a forwarded packet timeout
278276
// Find the parent packet that initiated the forward
279-
let commitment_key = BatchPacketsPath {
280-
channel_id: packet.source_channel_id,
281-
batch_hash: packet_hash,
282-
}
283-
.key();
277+
let commitment_key = BatchPacketsPath::from_packets(&[packet.clone()]).key();
284278

285279
if IN_FLIGHT_PACKET
286280
.may_load(deps.storage, commitment_key.into_bytes().into())?
@@ -293,9 +287,6 @@ fn timeout_packet(
293287
return Ok(Response::new());
294288
}
295289
}
296-
297-
// Normal packet timeout
298-
let zkgm_packet = ZkgmPacket::abi_decode_params(&packet.data, true)?;
299290
timeout_internal(
300291
deps,
301292
env,
@@ -427,18 +418,12 @@ fn acknowledge_packet(
427418
relayer: Addr,
428419
ack: Bytes,
429420
) -> Result<Response, ContractError> {
421+
let zkgm_packet = ZkgmPacket::abi_decode_params(&packet.data, true)?;
430422
// Check if this is an in-flight packet (forwarded packet)
431-
let packet_hash = keccak256(packet.abi_encode());
432-
let is_forward_tinted = is_salt_forward_tinted(packet_hash);
433-
434-
if is_forward_tinted {
423+
if is_forwarded_packet(zkgm_packet.salt.0.into()) {
435424
// This is a forwarded packet acknowledgement
436425
// Find the parent packet that initiated the forward
437-
let commitment_key = BatchPacketsPath {
438-
channel_id: packet.source_channel_id,
439-
batch_hash: packet_hash,
440-
}
441-
.key();
426+
let commitment_key = BatchPacketsPath::from_packets(&[packet.clone()]).key();
442427

443428
if let Some(parent_packet) =
444429
IN_FLIGHT_PACKET.may_load(deps.storage, commitment_key.into_bytes().into())?
@@ -458,9 +443,6 @@ fn acknowledge_packet(
458443
)?));
459444
}
460445
}
461-
462-
// Normal packet acknowledgement
463-
let zkgm_packet = ZkgmPacket::abi_decode_params(&packet.data, true)?;
464446
let ack = Ack::abi_decode_params(&ack, true)?;
465447
acknowledge_internal(
466448
deps,
@@ -1382,11 +1364,7 @@ pub fn reply(deps: DepsMut, env: Env, reply: Reply) -> Result<Response, Contract
13821364
#[allow(deprecated)]
13831365
sent_packet_data: Vec::from(reply_data.data.unwrap_or_default()).into(),
13841366
})?;
1385-
let commitment_key = BatchPacketsPath {
1386-
channel_id: sent_packet.source_channel_id,
1387-
batch_hash: keccak256(sent_packet.abi_encode()),
1388-
}
1389-
.key();
1367+
let commitment_key = BatchPacketsPath::from_packets(&[sent_packet.clone()]).key();
13901368
IN_FLIGHT_PACKET.save(
13911369
deps.storage,
13921370
commitment_key.into_bytes().into(),
@@ -2030,7 +2008,7 @@ pub fn tint_forward_salt(salt: H256) -> H256 {
20302008
.into()
20312009
}
20322010

2033-
pub fn is_salt_forward_tinted(salt: H256) -> bool {
2011+
pub fn is_forwarded_packet(salt: H256) -> bool {
20342012
(U256::from_be_bytes(*salt.get()) & FORWARD_SALT_MAGIC) == FORWARD_SALT_MAGIC
20352013
}
20362014

cosmwasm/ibc-union/app/ucs03-zkgm/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod tests {
1010
use crate::{
1111
com::{Instruction, Multiplex, FORWARD_SALT_MAGIC, INSTR_VERSION_0, OP_MULTIPLEX},
1212
contract::{
13-
dequeue_channel_from_path, is_salt_forward_tinted, pop_channel_from_path,
13+
dequeue_channel_from_path, is_forwarded_packet, pop_channel_from_path,
1414
reverse_channel_path, tint_forward_salt, update_channel_path, verify_internal,
1515
verify_multiplex,
1616
},
@@ -338,8 +338,8 @@ mod tests {
338338
0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78,
339339
0x90, 0xAB, 0xCD, 0xEF,
340340
]);
341-
assert!(!is_salt_forward_tinted(salt));
342-
assert!(is_salt_forward_tinted(tint_forward_salt(salt)));
341+
assert!(!is_forwarded_packet(salt));
342+
assert!(is_forwarded_packet(tint_forward_salt(salt)));
343343
}
344344

345345
#[test]

cosmwasm/ibc-union/core/msg/src/msg.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,12 @@ pub struct MsgIntentPacketRecv {
202202
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
203203
#[serde(deny_unknown_fields)]
204204
pub struct MsgBatchSend {
205-
pub source_channel: u32,
206205
pub packets: Vec<Packet>,
207206
}
208207

209208
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
210209
#[serde(deny_unknown_fields)]
211210
pub struct MsgBatchAcks {
212-
pub source_channel: u32,
213211
pub packets: Vec<Packet>,
214212
pub acks: Vec<Bytes>,
215213
}

cosmwasm/ibc-union/core/msg/src/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub enum QueryMsg {
1212
GetConnection { connection_id: u32 },
1313
GetChannel { channel_id: u32 },
1414
GetChannels { contract: String },
15-
GetBatchPackets { channel_id: u32, batch_hash: H256 },
16-
GetBatchReceipts { channel_id: u32, batch_hash: H256 },
15+
GetBatchPackets { batch_hash: H256 },
16+
GetBatchReceipts { batch_hash: H256 },
1717
GetClientImpl { client_id: u32 },
1818
GetRegisteredClientType { client_type: String },
1919
}

0 commit comments

Comments
 (0)