Skip to content

Commit 416f410

Browse files
committed
refactor handler for new packet struct
1 parent c53dc7c commit 416f410

File tree

11 files changed

+193
-157
lines changed

11 files changed

+193
-157
lines changed

ibc-eureka-core/ics03-connection/src/handler/conn_open_ack.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use ibc_eureka_core_connection_types::msgs::MsgConnectionOpenAck;
88
use ibc_eureka_core_connection_types::{ConnectionEnd, Counterparty, State};
99
use ibc_eureka_core_handler_types::events::{IbcEvent, MessageEvent};
1010
use ibc_eureka_core_host::types::identifiers::ClientId;
11-
use ibc_eureka_core_host::types::path::{ClientConsensusStatePath, ClientStatePath, ConnectionPath, Path};
11+
use ibc_eureka_core_host::types::path::{
12+
ClientConsensusStatePath, ClientStatePath, ConnectionPath, Path,
13+
};
1214
use ibc_eureka_core_host::{ExecutionContext, ValidationContext};
1315
use ibc_primitives::prelude::*;
1416
use ibc_primitives::proto::{Any, Protobuf};

ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ pub fn acknowledgement_packet_execute<ExecCtx>(
3737
where
3838
ExecCtx: ExecutionContext,
3939
{
40-
let chan_end_path_on_a =
41-
ChannelEndPath::new(&msg.packet.port_id_on_a, &msg.packet.chan_id_on_a);
40+
let payload = &msg.packet.payloads[0];
41+
42+
let port_id_on_a = &payload.header.source_port.1;
43+
let channel_id_on_a = &msg.packet.header.source_client;
44+
let seq_on_a = &msg.packet.header.seq_on_a;
45+
46+
let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);
4247
let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;
4348
let conn_id_on_a = &chan_end_on_a.connection_hops()[0];
4449

@@ -51,11 +56,8 @@ where
5156
ctx_a.emit_ibc_event(IbcEvent::Message(MessageEvent::Channel))?;
5257
ctx_a.emit_ibc_event(event)?;
5358

54-
let commitment_path_on_a = CommitmentPath::new(
55-
&msg.packet.port_id_on_a,
56-
&msg.packet.chan_id_on_a,
57-
msg.packet.seq_on_a,
58-
);
59+
let commitment_path_on_a =
60+
CommitmentPath::new(port_id_on_a, channel_id_on_a, msg.packet.header.seq_on_a);
5961

6062
// check if we're in the NO-OP case
6163
if ctx_a.get_packet_commitment(&commitment_path_on_a).is_err() {
@@ -78,9 +80,8 @@ where
7880
if let Order::Ordered = chan_end_on_a.ordering {
7981
// Note: in validation, we verified that `msg.packet.sequence == nextSeqRecv`
8082
// (where `nextSeqRecv` is the value in the store)
81-
let seq_ack_path_on_a =
82-
SeqAckPath::new(&msg.packet.port_id_on_a, &msg.packet.chan_id_on_a);
83-
ctx_a.store_next_sequence_ack(&seq_ack_path_on_a, msg.packet.seq_on_a.increment())?;
83+
let seq_ack_path_on_a = SeqAckPath::new(port_id_on_a, channel_id_on_a);
84+
ctx_a.store_next_sequence_ack(&seq_ack_path_on_a, (*seq_on_a).increment())?;
8485
}
8586
}
8687

@@ -109,15 +110,21 @@ where
109110
ctx_a.validate_message_signer(&msg.signer)?;
110111

111112
let packet = &msg.packet;
112-
let chan_end_path_on_a = ChannelEndPath::new(&packet.port_id_on_a, &packet.chan_id_on_a);
113+
let payload = &packet.payloads[0];
114+
115+
let port_id_on_a = &payload.header.source_port.1;
116+
let channel_id_on_a = &packet.header.source_client;
117+
let port_id_on_b = &payload.header.target_port.1;
118+
let channel_id_on_b = &packet.header.target_client;
119+
let seq_on_a = &packet.header.seq_on_a;
120+
let data = &payload.data;
121+
122+
let chan_end_path_on_a = ChannelEndPath::new(port_id_on_a, channel_id_on_a);
113123
let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;
114124

115125
chan_end_on_a.verify_state_matches(&ChannelState::Open)?;
116126

117-
let counterparty = Counterparty::new(
118-
packet.port_id_on_b.clone(),
119-
Some(packet.chan_id_on_b.clone()),
120-
);
127+
let counterparty = Counterparty::new(port_id_on_b.clone(), Some(channel_id_on_b.clone()));
121128

122129
chan_end_on_a.verify_counterparty_matches(&counterparty)?;
123130

@@ -126,8 +133,7 @@ where
126133

127134
conn_end_on_a.verify_state_matches(&ConnectionState::Open)?;
128135

129-
let commitment_path_on_a =
130-
CommitmentPath::new(&packet.port_id_on_a, &packet.chan_id_on_a, packet.seq_on_a);
136+
let commitment_path_on_a = CommitmentPath::new(port_id_on_a, channel_id_on_a, *seq_on_a);
131137

132138
// Verify packet commitment
133139
let Ok(commitment_on_a) = ctx_a.get_packet_commitment(&commitment_path_on_a) else {
@@ -139,9 +145,9 @@ where
139145
};
140146

141147
let expected_commitment_on_a = compute_packet_commitment(
142-
&packet.data,
143-
&packet.timeout_height_on_b,
144-
&packet.timeout_timestamp_on_b,
148+
data,
149+
&packet.header.timeout_height_on_b,
150+
&packet.header.timeout_timestamp_on_b,
145151
);
146152

147153
if commitment_on_a != expected_commitment_on_a {
@@ -152,11 +158,11 @@ where
152158
}
153159

154160
if let Order::Ordered = chan_end_on_a.ordering {
155-
let seq_ack_path_on_a = SeqAckPath::new(&packet.port_id_on_a, &packet.chan_id_on_a);
161+
let seq_ack_path_on_a = SeqAckPath::new(port_id_on_a, channel_id_on_a);
156162
let next_seq_ack = ctx_a.get_next_sequence_ack(&seq_ack_path_on_a)?;
157-
if packet.seq_on_a != next_seq_ack {
163+
if seq_on_a != &next_seq_ack {
158164
return Err(ChannelError::MismatchedPacketSequence {
159-
actual: packet.seq_on_a,
165+
actual: *seq_on_a,
160166
expected: next_seq_ack,
161167
});
162168
}
@@ -184,8 +190,7 @@ where
184190
let consensus_state_of_b_on_a =
185191
client_val_ctx_a.consensus_state(&client_cons_state_path_on_a)?;
186192
let ack_commitment = compute_ack_commitment(&msg.acknowledgement);
187-
let ack_path_on_b =
188-
AckPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
193+
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
189194

190195
verify_conn_delay_passed(ctx_a, msg.proof_height_on_b, &conn_end_on_a)?;
191196

ibc-eureka-core/ics04-channel/src/handler/chan_open_ack.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenAck`.
2-
use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State, State as ChannelState};
2+
use ibc_eureka_core_channel_types::channel::{
3+
ChannelEnd, Counterparty, State, State as ChannelState,
4+
};
35
use ibc_eureka_core_channel_types::error::ChannelError;
46
use ibc_eureka_core_channel_types::events::OpenAck;
57
use ibc_eureka_core_channel_types::msgs::MsgChannelOpenAck;

ibc-eureka-core/ics04-channel/src/handler/chan_open_confirm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenConfirm`.
22
3-
use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State, State as ChannelState};
3+
use ibc_eureka_core_channel_types::channel::{
4+
ChannelEnd, Counterparty, State, State as ChannelState,
5+
};
46
use ibc_eureka_core_channel_types::error::ChannelError;
57
use ibc_eureka_core_channel_types::events::OpenConfirm;
68
use ibc_eureka_core_channel_types::msgs::MsgChannelOpenConfirm;

ibc-eureka-core/ics04-channel/src/handler/chan_open_init.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenInit`.
22
3+
use core::str::FromStr;
4+
35
use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State};
46
use ibc_eureka_core_channel_types::error::ChannelError;
57
use ibc_eureka_core_channel_types::events::OpenInit;
@@ -21,7 +23,8 @@ where
2123
ValCtx: ValidationContext,
2224
{
2325
validate(ctx_a, &msg)?;
24-
let chan_id_on_a = ChannelId::new(ctx_a.channel_counter()?);
26+
// todo(rano): hack
27+
let chan_id_on_a = ChannelId::from_str("00-dummy-0")?;
2528

2629
module.on_chan_open_init_validate(
2730
msg.ordering,
@@ -43,7 +46,8 @@ pub fn chan_open_init_execute<ExecCtx>(
4346
where
4447
ExecCtx: ExecutionContext,
4548
{
46-
let chan_id_on_a = ChannelId::new(ctx_a.channel_counter()?);
49+
// todo(rano): hack
50+
let chan_id_on_a = ChannelId::from_str("00-dummy-0")?;
4751
let (extras, version) = module.on_chan_open_init_execute(
4852
msg.ordering,
4953
&msg.connection_hops_on_a,

ibc-eureka-core/ics04-channel/src/handler/chan_open_try.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenTry`.
22
3+
use core::str::FromStr;
4+
35
use ibc_eureka_core_channel_types::channel::{ChannelEnd, Counterparty, State as ChannelState};
46
use ibc_eureka_core_channel_types::error::ChannelError;
57
use ibc_eureka_core_channel_types::events::OpenTry;
@@ -27,7 +29,8 @@ where
2729
{
2830
validate(ctx_b, &msg)?;
2931

30-
let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?);
32+
// todo(rano): hack
33+
let chan_id_on_b = ChannelId::from_str("00-dummy-0")?;
3134

3235
module.on_chan_open_try_validate(
3336
msg.ordering,
@@ -49,7 +52,8 @@ pub fn chan_open_try_execute<ExecCtx>(
4952
where
5053
ExecCtx: ExecutionContext,
5154
{
52-
let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?);
55+
// todo(rano): hack
56+
let chan_id_on_b = ChannelId::from_str("00-dummy-0")?;
5357
let (extras, version) = module.on_chan_open_try_execute(
5458
msg.ordering,
5559
&msg.connection_hops_on_b,

ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ pub fn recv_packet_execute<ExecCtx>(
3737
where
3838
ExecCtx: ExecutionContext,
3939
{
40-
let chan_end_path_on_b =
41-
ChannelEndPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
40+
let packet = &msg.packet;
41+
let payload = &packet.payloads[0];
42+
43+
let port_id_on_b = &payload.header.target_port.1;
44+
let channel_id_on_b = &packet.header.target_client;
45+
let seq_on_a = &packet.header.seq_on_a;
46+
47+
let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, channel_id_on_b);
4248
let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?;
4349

4450
// Check if another relayer already relayed the packet.
@@ -48,19 +54,16 @@ where
4854
// Note: ibc-go doesn't make the check for `Order::None` channels
4955
Order::None => false,
5056
Order::Unordered => {
51-
let packet = &msg.packet;
52-
let receipt_path_on_b =
53-
ReceiptPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
57+
let receipt_path_on_b = ReceiptPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
5458
ctx_b.get_packet_receipt(&receipt_path_on_b)?.is_ok()
5559
}
5660
Order::Ordered => {
57-
let seq_recv_path_on_b =
58-
SeqRecvPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
61+
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
5962
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
6063

6164
// the sequence number has already been incremented, so
6265
// another relayer already relayed the packet
63-
msg.packet.seq_on_a < next_seq_recv
66+
seq_on_a < &next_seq_recv
6467
}
6568
};
6669

@@ -77,26 +80,21 @@ where
7780
match chan_end_on_b.ordering {
7881
Order::Unordered => {
7982
let receipt_path_on_b = ReceiptPath {
80-
port_id: msg.packet.port_id_on_b.clone(),
81-
channel_id: msg.packet.chan_id_on_b.clone(),
82-
sequence: msg.packet.seq_on_a,
83+
port_id: port_id_on_b.clone(),
84+
channel_id: channel_id_on_b.clone(),
85+
sequence: *seq_on_a,
8386
};
8487

8588
ctx_b.store_packet_receipt(&receipt_path_on_b, Receipt::Ok)?;
8689
}
8790
Order::Ordered => {
88-
let seq_recv_path_on_b =
89-
SeqRecvPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
91+
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
9092
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
9193
ctx_b.store_next_sequence_recv(&seq_recv_path_on_b, next_seq_recv.increment())?;
9294
}
9395
_ => {}
9496
}
95-
let ack_path_on_b = AckPath::new(
96-
&msg.packet.port_id_on_b,
97-
&msg.packet.chan_id_on_b,
98-
msg.packet.seq_on_a,
99-
);
97+
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
10098
// `writeAcknowledgement` handler state changes
10199
ctx_b.store_packet_acknowledgement(
102100
&ack_path_on_b,
@@ -143,16 +141,22 @@ where
143141
{
144142
ctx_b.validate_message_signer(&msg.signer)?;
145143

146-
let chan_end_path_on_b =
147-
ChannelEndPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
144+
let packet = &msg.packet;
145+
let payload = &packet.payloads[0];
146+
147+
let port_id_on_a = &payload.header.source_port.1;
148+
let channel_id_on_a = &packet.header.source_client;
149+
let port_id_on_b = &payload.header.target_port.1;
150+
let channel_id_on_b = &packet.header.target_client;
151+
let seq_on_a = &packet.header.seq_on_a;
152+
let data = &payload.data;
153+
154+
let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, channel_id_on_b);
148155
let chan_end_on_b = ctx_b.channel_end(&chan_end_path_on_b)?;
149156

150157
chan_end_on_b.verify_state_matches(&ChannelState::Open)?;
151158

152-
let counterparty = Counterparty::new(
153-
msg.packet.port_id_on_a.clone(),
154-
Some(msg.packet.chan_id_on_a.clone()),
155-
);
159+
let counterparty = Counterparty::new(port_id_on_a.clone(), Some(channel_id_on_a.clone()));
156160

157161
chan_end_on_b.verify_counterparty_matches(&counterparty)?;
158162

@@ -162,16 +166,16 @@ where
162166
conn_end_on_b.verify_state_matches(&ConnectionState::Open)?;
163167

164168
let latest_height = ctx_b.host_height()?;
165-
if msg.packet.timeout_height_on_b.has_expired(latest_height) {
169+
if packet.header.timeout_height_on_b.has_expired(latest_height) {
166170
return Err(ChannelError::InsufficientPacketHeight {
167171
chain_height: latest_height,
168-
timeout_height: msg.packet.timeout_height_on_b,
172+
timeout_height: packet.header.timeout_height_on_b,
169173
});
170174
}
171175

172176
let latest_timestamp = ctx_b.host_timestamp()?;
173-
if msg
174-
.packet
177+
if packet
178+
.header
175179
.timeout_timestamp_on_b
176180
.has_expired(&latest_timestamp)
177181
{
@@ -200,15 +204,11 @@ where
200204
client_val_ctx_b.consensus_state(&client_cons_state_path_on_b)?;
201205

202206
let expected_commitment_on_a = compute_packet_commitment(
203-
&msg.packet.data,
204-
&msg.packet.timeout_height_on_b,
205-
&msg.packet.timeout_timestamp_on_b,
206-
);
207-
let commitment_path_on_a = CommitmentPath::new(
208-
&msg.packet.port_id_on_a,
209-
&msg.packet.chan_id_on_a,
210-
msg.packet.seq_on_a,
207+
data,
208+
&packet.header.timeout_height_on_b,
209+
&packet.header.timeout_timestamp_on_b,
211210
);
211+
let commitment_path_on_a = CommitmentPath::new(port_id_on_a, channel_id_on_a, *seq_on_a);
212212

213213
verify_conn_delay_passed(ctx_b, msg.proof_height_on_a, &conn_end_on_b)?;
214214

@@ -224,17 +224,16 @@ where
224224

225225
match chan_end_on_b.ordering {
226226
Order::Ordered => {
227-
let seq_recv_path_on_b =
228-
SeqRecvPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);
227+
let seq_recv_path_on_b = SeqRecvPath::new(port_id_on_b, channel_id_on_b);
229228
let next_seq_recv = ctx_b.get_next_sequence_recv(&seq_recv_path_on_b)?;
230-
if msg.packet.seq_on_a > next_seq_recv {
229+
if seq_on_a > &next_seq_recv {
231230
return Err(ChannelError::MismatchedPacketSequence {
232-
actual: msg.packet.seq_on_a,
231+
actual: *seq_on_a,
233232
expected: next_seq_recv,
234233
});
235234
}
236235

237-
if msg.packet.seq_on_a == next_seq_recv {
236+
if seq_on_a == &next_seq_recv {
238237
// Case where the recvPacket is successful and an
239238
// acknowledgement will be written (not a no-op)
240239
validate_write_acknowledgement(ctx_b, msg)?;
@@ -265,10 +264,16 @@ fn validate_write_acknowledgement<Ctx>(ctx_b: &Ctx, msg: &MsgRecvPacket) -> Resu
265264
where
266265
Ctx: ValidationContext,
267266
{
268-
let packet = msg.packet.clone();
269-
let ack_path_on_b = AckPath::new(&packet.port_id_on_b, &packet.chan_id_on_b, packet.seq_on_a);
267+
let packet = &msg.packet;
268+
let payload = &packet.payloads[0];
269+
270+
let port_id_on_b = &payload.header.target_port.1;
271+
let channel_id_on_b = &packet.header.target_client;
272+
let seq_on_a = &packet.header.seq_on_a;
273+
274+
let ack_path_on_b = AckPath::new(port_id_on_b, channel_id_on_b, *seq_on_a);
270275
if ctx_b.get_packet_acknowledgement(&ack_path_on_b).is_ok() {
271-
return Err(ChannelError::DuplicateAcknowledgment(msg.packet.seq_on_a));
276+
return Err(ChannelError::DuplicateAcknowledgment(*seq_on_a));
272277
}
273278

274279
Ok(())

0 commit comments

Comments
 (0)