1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use crate::core::ics03_connection::connection::State as ConnectionState;
use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, State};
use crate::core::ics04_channel::error::ChannelError;
use crate::core::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck;
use crate::core::ics24_host::path::{ChannelEndPath, ClientConsensusStatePath};
use crate::prelude::*;
use crate::core::{ContextError, ValidationContext};
pub fn validate<Ctx>(ctx_a: &Ctx, msg: &MsgChannelOpenAck) -> Result<(), ContextError>
where
Ctx: ValidationContext,
{
let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;
if !chan_end_on_a.state_matches(&State::Init) {
return Err(ChannelError::InvalidChannelState {
channel_id: msg.chan_id_on_a.clone(),
state: chan_end_on_a.state,
}
.into());
}
if chan_end_on_a.connection_hops().len() != 1 {
return Err(ChannelError::InvalidConnectionHopsLength {
expected: 1,
actual: chan_end_on_a.connection_hops().len(),
}
.into());
}
let conn_end_on_a = ctx_a.connection_end(&chan_end_on_a.connection_hops()[0])?;
if !conn_end_on_a.state_matches(&ConnectionState::Open) {
return Err(ChannelError::ConnectionNotOpen {
connection_id: chan_end_on_a.connection_hops()[0].clone(),
}
.into());
}
{
let client_id_on_a = conn_end_on_a.client_id();
let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;
let client_cons_state_path_on_a =
ClientConsensusStatePath::new(client_id_on_a, &msg.proof_height_on_b);
let consensus_state_of_b_on_a = ctx_a.consensus_state(&client_cons_state_path_on_a)?;
let prefix_on_b = conn_end_on_a.counterparty().prefix();
let port_id_on_b = &chan_end_on_a.counterparty().port_id;
let conn_id_on_b = conn_end_on_a.counterparty().connection_id().ok_or(
ChannelError::UndefinedConnectionCounterparty {
connection_id: chan_end_on_a.connection_hops()[0].clone(),
},
)?;
if client_state_of_b_on_a.is_frozen() {
return Err(ChannelError::FrozenClient {
client_id: client_id_on_a.clone(),
}
.into());
}
let expected_chan_end_on_b = ChannelEnd::new(
State::TryOpen,
*chan_end_on_a.ordering(),
Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_a.clone())),
vec![conn_id_on_b.clone()],
msg.version_on_b.clone(),
);
let chan_end_path_on_b = ChannelEndPath::new(port_id_on_b, &msg.chan_id_on_b);
client_state_of_b_on_a
.verify_channel_state(
msg.proof_height_on_b,
prefix_on_b,
&msg.proof_chan_end_on_b,
consensus_state_of_b_on_a.root(),
&chan_end_path_on_b,
&expected_chan_end_on_b,
)
.map_err(ChannelError::VerifyChannelFailed)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::core::ics03_connection::msgs::test_util::get_dummy_raw_counterparty;
use crate::core::ics04_channel::channel::Order;
use crate::core::ics04_channel::handler::chan_open_ack::validate;
use crate::core::ics24_host::identifier::ClientId;
use crate::prelude::*;
use crate::timestamp::ZERO_DURATION;
use rstest::*;
use test_log::test;
use crate::core::ics03_connection::connection::ConnectionEnd;
use crate::core::ics03_connection::connection::Counterparty as ConnectionCounterparty;
use crate::core::ics03_connection::connection::State as ConnectionState;
use crate::core::ics03_connection::version::get_compatible_versions;
use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, State};
use crate::core::ics04_channel::msgs::chan_open_ack::test_util::get_dummy_raw_msg_chan_open_ack;
use crate::core::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck;
use crate::core::ics24_host::identifier::ConnectionId;
use crate::mock::client_state::client_type as mock_client_type;
use crate::mock::context::MockContext;
use crate::Height;
pub struct Fixture {
pub context: MockContext,
pub msg: MsgChannelOpenAck,
pub client_id_on_a: ClientId,
pub conn_id_on_a: ConnectionId,
pub conn_end_on_a: ConnectionEnd,
pub chan_end_on_a: ChannelEnd,
pub proof_height: u64,
}
#[fixture]
fn fixture() -> Fixture {
let proof_height = 10;
let context = MockContext::default();
let client_id_on_a = ClientId::new(mock_client_type(), 45).unwrap();
let conn_id_on_a = ConnectionId::new(2);
let conn_end_on_a = ConnectionEnd::new(
ConnectionState::Open,
client_id_on_a.clone(),
ConnectionCounterparty::try_from(get_dummy_raw_counterparty(Some(0))).unwrap(),
get_compatible_versions(),
ZERO_DURATION,
);
let msg =
MsgChannelOpenAck::try_from(get_dummy_raw_msg_chan_open_ack(proof_height)).unwrap();
let chan_end_on_a = ChannelEnd::new(
State::Init,
Order::Unordered,
Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_b.clone())),
vec![conn_id_on_a.clone()],
msg.version_on_b.clone(),
);
Fixture {
context,
msg,
client_id_on_a,
conn_id_on_a,
conn_end_on_a,
chan_end_on_a,
proof_height,
}
}
#[rstest]
fn chan_open_ack_fail_no_channel(fixture: Fixture) {
let Fixture {
context,
msg,
client_id_on_a,
conn_id_on_a,
conn_end_on_a,
proof_height,
..
} = fixture;
let context = context
.with_client(&client_id_on_a, Height::new(0, proof_height).unwrap())
.with_connection(conn_id_on_a, conn_end_on_a);
let res = validate(&context, &msg);
assert!(
res.is_err(),
"Validation fails because no channel exists in the context"
)
}
#[rstest]
fn chan_open_ack_fail_channel_wrong_state(fixture: Fixture) {
let Fixture {
context,
msg,
client_id_on_a,
conn_id_on_a,
conn_end_on_a,
proof_height,
..
} = fixture;
let wrong_chan_end = ChannelEnd::new(
State::Open,
Order::Unordered,
Counterparty::new(msg.port_id_on_a.clone(), Some(msg.chan_id_on_b.clone())),
vec![conn_id_on_a.clone()],
msg.version_on_b.clone(),
);
let context = context
.with_client(&client_id_on_a, Height::new(0, proof_height).unwrap())
.with_connection(conn_id_on_a, conn_end_on_a)
.with_channel(
msg.port_id_on_a.clone(),
msg.chan_id_on_a.clone(),
wrong_chan_end,
);
let res = validate(&context, &msg);
assert!(
res.is_err(),
"Validation fails because channel is in the wrong state"
)
}
#[rstest]
fn chan_open_ack_fail_no_connection(fixture: Fixture) {
let Fixture {
context,
msg,
client_id_on_a,
chan_end_on_a,
proof_height,
..
} = fixture;
let context = context
.with_client(&client_id_on_a, Height::new(0, proof_height).unwrap())
.with_channel(
msg.port_id_on_a.clone(),
msg.chan_id_on_a.clone(),
chan_end_on_a,
);
let res = validate(&context, &msg);
assert!(
res.is_err(),
"Validation fails because no connection exists in the context"
)
}
#[rstest]
fn chan_open_ack_happy_path(fixture: Fixture) {
let Fixture {
context,
msg,
client_id_on_a,
conn_id_on_a,
conn_end_on_a,
chan_end_on_a,
proof_height,
..
} = fixture;
let context = context
.with_client(&client_id_on_a, Height::new(0, proof_height).unwrap())
.with_connection(conn_id_on_a, conn_end_on_a)
.with_channel(
msg.port_id_on_a.clone(),
msg.chan_id_on_a.clone(),
chan_end_on_a,
);
let res = validate(&context, &msg);
assert!(res.is_ok(), "Validation happy path")
}
}