Skip to content

Commit 2f4920a

Browse files
feat(ibc-union): yield and index packet hash on subsequent events
- provide packet attributes on send-packet in cosmwasm for tx_search indexing - indexed packet hash on evm events
1 parent 9f49fb8 commit 2f4920a

File tree

4 files changed

+146
-87
lines changed

4 files changed

+146
-87
lines changed

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

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use alloy::sol_types::SolValue;
44
#[cfg(not(feature = "library"))]
55
use cosmwasm_std::entry_point;
66
use cosmwasm_std::{
7-
to_json_binary, wasm_execute, Addr, Binary, Deps, DepsMut, Env, Event, MessageInfo, Response,
8-
StdResult,
7+
to_json_binary, wasm_execute, Addr, Attribute, Binary, Deps, DepsMut, Env, Event, MessageInfo,
8+
Response, StdResult,
99
};
1010
use cw_storage_plus::Item;
1111
use ibc_union_msg::{
@@ -26,8 +26,9 @@ use ibc_union_msg::{
2626
};
2727
use ibc_union_spec::{
2828
path::{
29-
BatchPacketsPath, BatchReceiptsPath, ChannelPath, ClientStatePath, ConnectionPath,
30-
ConsensusStatePath, COMMITMENT_MAGIC, COMMITMENT_MAGIC_ACK, COSMWASM_COMMITMENT_PREFIX,
29+
commit_packets, BatchPacketsPath, BatchReceiptsPath, ChannelPath, ClientStatePath,
30+
ConnectionPath, ConsensusStatePath, COMMITMENT_MAGIC, COMMITMENT_MAGIC_ACK,
31+
COSMWASM_COMMITMENT_PREFIX,
3132
},
3233
types::{Channel, ChannelState, Connection, ConnectionState, Packet},
3334
};
@@ -79,14 +80,20 @@ pub mod events {
7980
pub const TIMEOUT: &str = "packet_timeout";
8081
pub const BATCH_SEND: &str = "batch_send";
8182
pub const BATCH_ACKS: &str = "batch_acks";
83+
pub const WRITE_ACK: &str = "write_ack";
8284
}
8385
pub mod attribute {
8486
pub const CLIENT_ID: &str = "client_id";
8587
pub const CONNECTION_ID: &str = "connection_id";
8688
pub const CHANNEL_ID: &str = "channel_id";
8789
pub const COUNTERPARTY_CHANNEL_ID: &str = "counterparty_channel_id";
8890
pub const COUNTERPARTY_HEIGHT: &str = "counterparty_height";
89-
pub const PACKET: &str = "packet";
91+
pub const PACKET_HASH: &str = "packet_hash";
92+
pub const PACKET_SOURCE_CHANNEL_ID: &str = "packet_source_channel_id";
93+
pub const PACKET_DESTINATION_CHANNEL_ID: &str = "packet_destination_channel_id";
94+
pub const PACKET_DATA: &str = "packet_data";
95+
pub const PACKET_TIMEOUT_HEIGHT: &str = "packet_timeout_height";
96+
pub const PACKET_TIMEOUT_TIMESTAMP: &str = "packet_timeout_timestamp";
9097
pub const PACKETS: &str = "packets";
9198
pub const ACKS: &str = "acks";
9299
pub const MAKER: &str = "maker";
@@ -103,6 +110,39 @@ pub mod events {
103110
}
104111
}
105112

113+
#[must_use]
114+
fn packet_to_attrs(packet: &Packet) -> [Attribute; 5] {
115+
[
116+
(
117+
events::attribute::PACKET_SOURCE_CHANNEL_ID,
118+
packet.source_channel_id.to_string(),
119+
),
120+
(
121+
events::attribute::PACKET_DESTINATION_CHANNEL_ID,
122+
packet.destination_channel_id.to_string(),
123+
),
124+
(events::attribute::PACKET_DATA, packet.data.to_string()),
125+
(
126+
events::attribute::PACKET_TIMEOUT_HEIGHT,
127+
packet.timeout_height.to_string(),
128+
),
129+
(
130+
events::attribute::PACKET_TIMEOUT_TIMESTAMP,
131+
packet.timeout_timestamp.to_string(),
132+
),
133+
]
134+
.map(Into::into)
135+
}
136+
137+
#[must_use]
138+
fn packet_to_attr_hash(packet: &Packet) -> Attribute {
139+
(
140+
events::attribute::PACKET_HASH,
141+
commit_packets(&[packet.clone()]).to_string(),
142+
)
143+
.into()
144+
}
145+
106146
#[cfg_attr(not(feature = "library"), entry_point)]
107147
pub fn execute(
108148
mut deps: DepsMut,
@@ -584,13 +624,11 @@ fn timeout_packet(
584624

585625
let port_id = CHANNEL_OWNER.load(deps.storage, source_channel)?;
586626
Ok(Response::new()
587-
.add_event(Event::new(events::packet::TIMEOUT).add_attributes([
588-
(
589-
events::attribute::PACKET,
590-
serde_json::to_string(&packet).expect("packet serialization is infallible; qed;"),
591-
),
592-
(events::attribute::MAKER, relayer.to_string()),
593-
]))
627+
.add_event(
628+
Event::new(events::packet::TIMEOUT)
629+
.add_attributes([packet_to_attr_hash(&packet)])
630+
.add_attributes([(events::attribute::MAKER, relayer.to_string())]),
631+
)
594632
.add_message(wasm_execute(
595633
port_id,
596634
&ModuleMsg::IbcUnionMsg(IbcUnionMsg::OnTimeoutPacket {
@@ -637,14 +675,14 @@ fn acknowledge_packet(
637675
let mut messages = Vec::with_capacity(packets.len());
638676
for (packet, ack) in packets.into_iter().zip(acknowledgements) {
639677
mark_packet_as_acknowledged(deps.branch(), &packet)?;
640-
events.push(Event::new(events::packet::ACK).add_attributes([
641-
(
642-
events::attribute::PACKET,
643-
serde_json::to_string(&packet).expect("packet serialization is infallible; qed;"),
644-
),
645-
(events::attribute::ACKNOWLEDGEMENT, hex::encode(&ack)),
646-
(events::attribute::MAKER, relayer.clone().to_string()),
647-
]));
678+
events.push(
679+
Event::new(events::packet::ACK)
680+
.add_attributes([packet_to_attr_hash(&packet)])
681+
.add_attributes([
682+
(events::attribute::ACKNOWLEDGEMENT, hex::encode(&ack)),
683+
(events::attribute::MAKER, relayer.clone().to_string()),
684+
]),
685+
);
648686
messages.push(wasm_execute(
649687
port_id.clone(),
650688
&ModuleMsg::IbcUnionMsg(IbcUnionMsg::OnAcknowledgementPacket {
@@ -1464,15 +1502,12 @@ fn process_receive(
14641502
if !set_packet_receive(deps.branch(), commitment_key) {
14651503
if intent {
14661504
events.push(
1467-
Event::new(events::packet::INTENT_RECV).add_attributes([
1468-
(
1469-
events::attribute::PACKET,
1470-
serde_json::to_string(&packet)
1471-
.expect("packet serialization is infallible; qed;"),
1472-
),
1473-
(events::attribute::MAKER, relayer.clone()),
1474-
(events::attribute::MAKER_MSG, hex::encode(&relayer_msg)),
1475-
]),
1505+
Event::new(events::packet::INTENT_RECV)
1506+
.add_attributes([packet_to_attr_hash(&packet)])
1507+
.add_attributes([
1508+
(events::attribute::MAKER, relayer.clone()),
1509+
(events::attribute::MAKER_MSG, hex::encode(&relayer_msg)),
1510+
]),
14761511
);
14771512

14781513
messages.push(wasm_execute(
@@ -1486,15 +1521,12 @@ fn process_receive(
14861521
)?);
14871522
} else {
14881523
events.push(
1489-
Event::new(events::packet::RECV).add_attributes([
1490-
(
1491-
events::attribute::PACKET,
1492-
serde_json::to_string(&packet)
1493-
.expect("packet serialization is infallible; qed;"),
1494-
),
1495-
(events::attribute::MAKER, relayer.clone()),
1496-
(events::attribute::MAKER_MSG, hex::encode(&relayer_msg)),
1497-
]),
1524+
Event::new(events::packet::RECV)
1525+
.add_attributes([packet_to_attr_hash(&packet)])
1526+
.add_attributes([
1527+
(events::attribute::MAKER, relayer.clone()),
1528+
(events::attribute::MAKER_MSG, hex::encode(&relayer_msg)),
1529+
]),
14981530
);
14991531

15001532
messages.push(wasm_execute(
@@ -1546,24 +1578,22 @@ fn write_acknowledgement(
15461578
None => return Err(ContractError::PacketNotReceived),
15471579
}
15481580

1581+
let acknowledgement_serialized = hex::encode(&acknowledgement);
1582+
15491583
store_commit(
15501584
deps.branch(),
15511585
&commitment_key,
1552-
&commit_ack(&acknowledgement.clone().into()),
1586+
&commit_ack(&acknowledgement.into()),
15531587
);
15541588

1555-
Ok(
1556-
Response::new().add_event(Event::new("write_ack").add_attributes([
1557-
(
1558-
events::attribute::PACKET,
1559-
serde_json::to_string(&packet).expect("packet serialization is infallible; qed;"),
1560-
),
1561-
(
1589+
Ok(Response::new().add_event(
1590+
Event::new(events::packet::WRITE_ACK)
1591+
.add_attributes([packet_to_attr_hash(&packet)])
1592+
.add_attributes([(
15621593
events::attribute::ACKNOWLEDGEMENT,
1563-
hex::encode(acknowledgement),
1564-
),
1565-
])),
1566-
)
1594+
acknowledgement_serialized,
1595+
)]),
1596+
))
15671597
}
15681598

15691599
fn send_packet(
@@ -1599,6 +1629,9 @@ fn send_packet(
15991629
let serialized_packet =
16001630
serde_json::to_string(&packet).expect("packet serialization is infallible; qed;");
16011631

1632+
let packet_attrs = packet_to_attrs(&packet);
1633+
let packet_attr_hash = packet_to_attr_hash(&packet);
1634+
16021635
let commitment_key = BatchPacketsPath::from_packets(&[packet]).key();
16031636

16041637
if read_commit(deps.as_ref(), &commitment_key).is_some() {
@@ -1610,7 +1643,8 @@ fn send_packet(
16101643
Ok(Response::new()
16111644
.add_event(
16121645
Event::new(events::packet::SEND)
1613-
.add_attribute(events::attribute::PACKET, &serialized_packet),
1646+
.add_attributes([packet_attr_hash])
1647+
.add_attributes(packet_attrs),
16141648
)
16151649
.set_data(serialized_packet.as_bytes()))
16161650
}

evm/contracts/core/04-channel/IBCPacket.sol

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ library IBCPacketLib {
1515
0x0200000000000000000000000000000000000000000000000000000000000000;
1616
bytes32 public constant COMMITMENT_NULL = bytes32(uint256(0));
1717

18-
event PacketSend(IBCPacket packet);
19-
event PacketRecv(IBCPacket packet, address maker, bytes makerMsg);
20-
event IntentPacketRecv(IBCPacket packet, address maker, bytes makerMsg);
21-
event WriteAck(IBCPacket packet, bytes acknowledgement);
22-
event PacketAck(IBCPacket packet, bytes acknowledgement, address maker);
23-
event PacketTimeout(IBCPacket packet, address maker);
18+
event PacketSend(bytes32 indexed packetHash, IBCPacket packet);
19+
event PacketRecv(bytes32 indexed packetHash, address maker, bytes makerMsg);
20+
event IntentPacketRecv(
21+
bytes32 indexed packetHash, address maker, bytes makerMsg
22+
);
23+
event WriteAck(bytes32 indexed packetHash, bytes acknowledgement);
24+
event PacketAck(
25+
bytes32 indexed packetHash, bytes acknowledgement, address maker
26+
);
27+
event PacketTimeout(bytes32 indexed packetHash, address maker);
2428

2529
function commitAcksMemory(
2630
bytes[] memory acks
@@ -163,15 +167,15 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
163167
timeoutHeight: timeoutHeight,
164168
timeoutTimestamp: timeoutTimestamp
165169
});
166-
bytes32 commitmentKey = IBCCommitment.batchPacketsCommitmentKey(
167-
IBCPacketLib.commitPacket(packet)
168-
);
170+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
171+
bytes32 commitmentKey =
172+
IBCCommitment.batchPacketsCommitmentKey(packetHash);
169173
if (commitments[commitmentKey] != IBCPacketLib.COMMITMENT_NULL) {
170174
revert IBCErrors.ErrPacketAlreadyExist();
171175
}
172176
commitments[commitmentKey] = IBCPacketLib.COMMITMENT_MAGIC;
173177

174-
emit IBCPacketLib.PacketSend(packet);
178+
emit IBCPacketLib.PacketSend(packetHash, packet);
175179

176180
return packet;
177181
}
@@ -239,25 +243,27 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
239243
revert IBCErrors.ErrTimestampTimeout();
240244
}
241245

242-
bytes32 commitmentKey = IBCCommitment.batchReceiptsCommitmentKey(
243-
IBCPacketLib.commitPacket(packet)
244-
);
246+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
247+
bytes32 commitmentKey =
248+
IBCCommitment.batchReceiptsCommitmentKey(packetHash);
245249

246250
if (!setPacketReceive(commitmentKey)) {
247251
bytes memory acknowledgement;
248252
bytes calldata makerMsg = makerMsgs[i];
249253
if (intent) {
250254
acknowledgement =
251255
module.onRecvIntentPacket(packet, maker, makerMsg);
252-
emit IBCPacketLib.IntentPacketRecv(packet, maker, makerMsg);
256+
emit IBCPacketLib.IntentPacketRecv(
257+
packetHash, maker, makerMsg
258+
);
253259
} else {
254260
acknowledgement =
255261
module.onRecvPacket(packet, maker, makerMsg);
256-
emit IBCPacketLib.PacketRecv(packet, maker, makerMsg);
262+
emit IBCPacketLib.PacketRecv(packetHash, maker, makerMsg);
257263
}
258264
if (acknowledgement.length > 0) {
259265
_writeAcknowledgement(commitmentKey, acknowledgement);
260-
emit IBCPacketLib.WriteAck(packet, acknowledgement);
266+
emit IBCPacketLib.WriteAck(packetHash, acknowledgement);
261267
}
262268
}
263269
}
@@ -314,11 +320,11 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
314320
revert IBCErrors.ErrUnauthorized();
315321
}
316322
ensureChannelState(packet.destinationChannelId);
317-
bytes32 commitmentKey = IBCCommitment.batchReceiptsCommitmentKey(
318-
IBCPacketLib.commitPacket(packet)
319-
);
323+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
324+
bytes32 commitmentKey =
325+
IBCCommitment.batchReceiptsCommitmentKey(packetHash);
320326
_writeAcknowledgement(commitmentKey, acknowledgement);
321-
emit IBCPacketLib.WriteAck(packet, acknowledgement);
327+
emit IBCPacketLib.WriteAck(packetHash, acknowledgement);
322328
}
323329

324330
function acknowledgePacket(
@@ -354,7 +360,9 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
354360
module.onAcknowledgementPacket(
355361
packet, acknowledgement, msg_.relayer
356362
);
357-
emit IBCPacketLib.PacketAck(packet, acknowledgement, msg_.relayer);
363+
emit IBCPacketLib.PacketAck(
364+
IBCPacketLib.commitPacket(packet), acknowledgement, msg_.relayer
365+
);
358366
}
359367
}
360368

@@ -371,9 +379,9 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
371379
if (proofTimestamp == 0) {
372380
revert IBCErrors.ErrLatestTimestampNotFound();
373381
}
374-
bytes32 commitmentKey = IBCCommitment.batchReceiptsCommitmentKey(
375-
IBCPacketLib.commitPacket(packet)
376-
);
382+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
383+
bytes32 commitmentKey =
384+
IBCCommitment.batchReceiptsCommitmentKey(packetHash);
377385
if (
378386
!verifyAbsentCommitment(
379387
clientId, msg_.proofHeight, msg_.proof, commitmentKey
@@ -397,7 +405,7 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
397405
revert IBCErrors.ErrTimeoutHeightNotReached();
398406
}
399407
module.onTimeoutPacket(packet, msg_.relayer);
400-
emit IBCPacketLib.PacketTimeout(packet, msg_.relayer);
408+
emit IBCPacketLib.PacketTimeout(packetHash, msg_.relayer);
401409
}
402410

403411
function verifyCommitment(

0 commit comments

Comments
 (0)