Skip to content

Commit 7a44c0e

Browse files
feat(ibc-union): normalize batch events (#4048)
2 parents 813a3c7 + 9ecabcf commit 7a44c0e

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ pub mod events {
9191
pub const PACKET_DATA: &str = "packet_data";
9292
pub const PACKET_TIMEOUT_HEIGHT: &str = "packet_timeout_height";
9393
pub const PACKET_TIMEOUT_TIMESTAMP: &str = "packet_timeout_timestamp";
94-
pub const PACKETS: &str = "packets";
95-
pub const ACKS: &str = "acks";
94+
pub const BATCH_HASH: &str = "batch_hash";
9695
pub const MAKER: &str = "maker";
9796
pub const MAKER_MSG: &str = "maker_msg";
9897
pub const ACKNOWLEDGEMENT: &str = "acknowledgement";
98+
pub const ACKNOWLEDGEMENT_HASH: &str = "acknowledgement_hash";
9999
pub const CLIENT_TYPE: &str = "client_type";
100100
pub const CLIENT_ADDRESS: &str = "client_address";
101101
pub const COUNTERPARTY_CHAIN_ID: &str = "counterparty_chain_id";
@@ -520,10 +520,15 @@ fn batch_send(deps: DepsMut, packets: Vec<Packet>) -> ContractResult {
520520
return Err(ContractError::NotEnoughPackets);
521521
}
522522
let channel_id = packets[0].source_channel_id;
523-
let serialized_packets =
524-
serde_json_wasm::to_string(&packets).expect("packet serialization is infallible; qed;");
523+
let batch_hash = commit_packets(&packets);
525524
let batch_commitment_key = BatchPacketsPath::from_packets(&packets).key();
525+
let mut events = Vec::new();
526526
for packet in packets {
527+
events.push(
528+
Event::new(events::packet::BATCH_SEND)
529+
.add_attributes([packet_to_attr_hash(&packet)])
530+
.add_attribute(events::attribute::BATCH_HASH, batch_hash.to_string()),
531+
);
527532
if packet.source_channel_id != channel_id {
528533
return Err(ContractError::BatchSameChannelOnly);
529534
}
@@ -537,53 +542,45 @@ fn batch_send(deps: DepsMut, packets: Vec<Packet>) -> ContractResult {
537542
}
538543
}
539544
store_commit(deps, &batch_commitment_key, &COMMITMENT_MAGIC);
540-
Ok(
541-
Response::new().add_event(Event::new(events::packet::BATCH_SEND).add_attributes([
542-
(events::attribute::CHANNEL_ID, channel_id.to_string()),
543-
(events::attribute::PACKETS, serialized_packets),
544-
])),
545-
)
545+
Ok(Response::new().add_events(events))
546546
}
547547

548548
fn batch_acks(deps: DepsMut, packets: Vec<Packet>, acks: Vec<Bytes>) -> ContractResult {
549549
if packets.len() < 2 {
550550
return Err(ContractError::NotEnoughPackets);
551551
}
552552
let channel_id = packets[0].destination_channel_id;
553+
let batch_hash = commit_packets(&packets);
553554
let batch_commitment_key = BatchReceiptsPath::from_packets(&packets).key();
554-
let serialized_packets =
555-
serde_json_wasm::to_string(&packets).expect("packet serialization is infallible; qed;");
556-
let serialized_acks =
557-
serde_json_wasm::to_string(&acks).expect("bytes serialization is infallible; qed;");
555+
let mut events = Vec::new();
558556
for (packet, ack) in packets.into_iter().zip(acks.iter()) {
557+
events.push(
558+
Event::new(events::packet::BATCH_ACKS)
559+
.add_attributes([packet_to_attr_hash(&packet)])
560+
.add_attribute(events::attribute::BATCH_HASH, batch_hash.to_string()),
561+
);
559562
if packet.destination_channel_id != channel_id {
560563
return Err(ContractError::BatchSameChannelOnly);
561564
}
562565
let commitment_key = BatchReceiptsPath::from_packets(&[packet]).key();
563566
let commitment = read_commit(deps.as_ref(), &commitment_key);
564567
match commitment {
565-
Some(acknowledgement) => {
566-
let expected_ack = commit_ack(ack);
567-
if acknowledgement == COMMITMENT_MAGIC {
568+
Some(ack_commitment) => {
569+
let ack_commitment_expected = commit_ack(ack);
570+
if ack_commitment == COMMITMENT_MAGIC {
568571
return Err(ContractError::AcknowledgementIsEmpty);
569-
} else if acknowledgement != expected_ack {
572+
} else if ack_commitment != ack_commitment_expected {
570573
return Err(ContractError::AcknowledgementMismatch {
571-
found: acknowledgement.into(),
572-
expected: expected_ack.into(),
574+
found: ack_commitment.into(),
575+
expected: ack_commitment_expected.into(),
573576
});
574577
}
575578
}
576579
None => return Err(ContractError::PacketCommitmentNotFound),
577580
}
578581
}
579582
store_commit(deps, &batch_commitment_key, &commit_acks(&acks));
580-
Ok(
581-
Response::new().add_event(Event::new(events::packet::BATCH_ACKS).add_attributes([
582-
(events::attribute::CHANNEL_ID, channel_id.to_string()),
583-
(events::attribute::PACKETS, serialized_packets),
584-
(events::attribute::ACKS, serialized_acks),
585-
])),
586-
)
583+
Ok(Response::new().add_events(events))
587584
}
588585

589586
fn timeout_packet(

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

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

1818
event PacketSend(bytes32 indexed packetHash, IBCPacket packet);
19-
event PacketRecv(bytes32 indexed packetHash, address maker, bytes makerMsg);
19+
event PacketRecv(
20+
bytes32 indexed packetHash, address indexed maker, bytes makerMsg
21+
);
2022
event IntentPacketRecv(
21-
bytes32 indexed packetHash, address maker, bytes makerMsg
23+
bytes32 indexed packetHash, address indexed maker, bytes makerMsg
2224
);
2325
event WriteAck(bytes32 indexed packetHash, bytes acknowledgement);
2426
event PacketAck(
25-
bytes32 indexed packetHash, bytes acknowledgement, address maker
27+
bytes32 indexed packetHash, bytes acknowledgement, address indexed maker
28+
);
29+
event PacketTimeout(bytes32 indexed packetHash, address indexed maker);
30+
event BatchedPreviouslySent(
31+
bytes32 indexed batchHash, bytes32 indexed packetHash
32+
);
33+
event BatchedPreviouslyAcked(
34+
bytes32 indexed batchHash, bytes32 indexed packetHash
2635
);
27-
event PacketTimeout(bytes32 indexed packetHash, address maker);
2836

2937
function commitAcksMemory(
3038
bytes[] memory acks
@@ -90,6 +98,7 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
9098
revert IBCErrors.ErrNotEnoughPackets();
9199
}
92100
uint32 channelId = msg_.packets[0].sourceChannelId;
101+
bytes32 batchHash = IBCPacketLib.commitPackets(msg_.packets);
93102
for (uint256 i = 0; i < l; i++) {
94103
IBCPacket calldata packet = msg_.packets[i];
95104
if (i > 0) {
@@ -98,16 +107,17 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
98107
}
99108
}
100109
// If the channel mismatch, the commitment will be zero
101-
bytes32 commitment = commitments[IBCCommitment
102-
.batchPacketsCommitmentKey(IBCPacketLib.commitPacket(packet))];
110+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
111+
bytes32 commitment =
112+
commitments[IBCCommitment.batchPacketsCommitmentKey(packetHash)];
103113
// Every packet must have been previously sent to be batched
104114
if (commitment != IBCPacketLib.COMMITMENT_MAGIC) {
105115
revert IBCErrors.ErrPacketCommitmentNotFound();
106116
}
117+
emit IBCPacketLib.BatchedPreviouslySent(batchHash, packetHash);
107118
}
108-
commitments[IBCCommitment.batchPacketsCommitmentKey(
109-
IBCPacketLib.commitPackets(msg_.packets)
110-
)] = IBCPacketLib.COMMITMENT_MAGIC;
119+
commitments[IBCCommitment.batchPacketsCommitmentKey(batchHash)] =
120+
IBCPacketLib.COMMITMENT_MAGIC;
111121
}
112122

113123
function batchAcks(
@@ -119,6 +129,7 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
119129
revert IBCErrors.ErrNotEnoughPackets();
120130
}
121131
uint32 channelId = msg_.packets[0].destinationChannelId;
132+
bytes32 batchHash = IBCPacketLib.commitPackets(msg_.packets);
122133
for (uint256 i = 0; i < l; i++) {
123134
IBCPacket calldata packet = msg_.packets[i];
124135
if (i > 0) {
@@ -128,8 +139,9 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
128139
}
129140
bytes calldata ack = msg_.acks[i];
130141
// If the channel mismatch, the commitment will be zero.
142+
bytes32 packetHash = IBCPacketLib.commitPacket(packet);
131143
bytes32 commitment = commitments[IBCCommitment
132-
.batchReceiptsCommitmentKey(IBCPacketLib.commitPacket(packet))];
144+
.batchReceiptsCommitmentKey(packetHash)];
133145
// Can't batch an empty ack.
134146
if (
135147
commitment == IBCPacketLib.COMMITMENT_NULL
@@ -141,6 +153,7 @@ abstract contract IBCPacketImpl is IBCStore, IIBCPacket {
141153
if (commitment != IBCPacketLib.commitAck(ack)) {
142154
revert IBCErrors.ErrCommittedAckNotPresent();
143155
}
156+
emit IBCPacketLib.BatchedPreviouslyAcked(batchHash, packetHash);
144157
}
145158
commitments[IBCCommitment.batchReceiptsCommitmentKey(
146159
IBCPacketLib.commitPackets(msg_.packets)

0 commit comments

Comments
 (0)