|
1 | 1 | //! Protocol logic specific to processing ICS3 messages of type `MsgConnectionOpenAck`.
|
2 | 2 |
|
| 3 | +use crate::core::context::ContextError; |
3 | 4 | use crate::core::ics03_connection::connection::{ConnectionEnd, Counterparty, State};
|
4 | 5 | use crate::core::ics03_connection::context::ConnectionReader;
|
5 | 6 | use crate::core::ics03_connection::error::ConnectionError;
|
6 | 7 | use crate::core::ics03_connection::events::OpenAck;
|
7 | 8 | use crate::core::ics03_connection::handler::ConnectionResult;
|
8 | 9 | use crate::core::ics03_connection::msgs::conn_open_ack::MsgConnectionOpenAck;
|
| 10 | +use crate::core::ics24_host::identifier::ClientId; |
| 11 | +use crate::core::ics24_host::path::ConnectionsPath; |
| 12 | +use crate::core::{ExecutionContext, ValidationContext}; |
9 | 13 | use crate::events::IbcEvent;
|
10 | 14 | use crate::handler::{HandlerOutput, HandlerResult};
|
11 | 15 | use crate::prelude::*;
|
12 | 16 |
|
13 | 17 | use super::ConnectionIdState;
|
14 | 18 |
|
| 19 | +pub(crate) fn validate<Ctx>(ctx_a: &Ctx, msg: MsgConnectionOpenAck) -> Result<(), ContextError> |
| 20 | +where |
| 21 | + Ctx: ValidationContext, |
| 22 | +{ |
| 23 | + let vars = LocalVars::new(ctx_a, &msg)?; |
| 24 | + validate_impl(ctx_a, &msg, &vars) |
| 25 | +} |
| 26 | + |
| 27 | +fn validate_impl<Ctx>( |
| 28 | + ctx_a: &Ctx, |
| 29 | + msg: &MsgConnectionOpenAck, |
| 30 | + vars: &LocalVars, |
| 31 | +) -> Result<(), ContextError> |
| 32 | +where |
| 33 | + Ctx: ValidationContext, |
| 34 | +{ |
| 35 | + let host_height = ctx_a.host_height().map_err(|_| ConnectionError::Other { |
| 36 | + description: "failed to get host height".to_string(), |
| 37 | + })?; |
| 38 | + if msg.consensus_height_of_a_on_b > host_height { |
| 39 | + return Err(ConnectionError::InvalidConsensusHeight { |
| 40 | + target_height: msg.consensus_height_of_a_on_b, |
| 41 | + current_height: host_height, |
| 42 | + } |
| 43 | + .into()); |
| 44 | + } |
| 45 | + |
| 46 | + ctx_a.validate_self_client(msg.client_state_of_a_on_b.clone())?; |
| 47 | + |
| 48 | + if !(vars.conn_end_on_a.state_matches(&State::Init) |
| 49 | + && vars.conn_end_on_a.versions().contains(&msg.version)) |
| 50 | + { |
| 51 | + return Err(ConnectionError::ConnectionMismatch { |
| 52 | + connection_id: msg.conn_id_on_a.clone(), |
| 53 | + } |
| 54 | + .into()); |
| 55 | + } |
| 56 | + |
| 57 | + // Proof verification. |
| 58 | + { |
| 59 | + let client_state_of_b_on_a = |
| 60 | + ctx_a |
| 61 | + .client_state(vars.client_id_on_a()) |
| 62 | + .map_err(|_| ConnectionError::Other { |
| 63 | + description: "failed to fetch client state".to_string(), |
| 64 | + })?; |
| 65 | + let consensus_state_of_b_on_a = ctx_a |
| 66 | + .consensus_state(vars.client_id_on_a(), msg.proofs_height_on_b) |
| 67 | + .map_err(|_| ConnectionError::Other { |
| 68 | + description: "failed to fetch client consensus state".to_string(), |
| 69 | + })?; |
| 70 | + |
| 71 | + let prefix_on_a = ctx_a.commitment_prefix(); |
| 72 | + let prefix_on_b = vars.conn_end_on_a.counterparty().prefix(); |
| 73 | + |
| 74 | + { |
| 75 | + let expected_conn_end_on_b = ConnectionEnd::new( |
| 76 | + State::TryOpen, |
| 77 | + vars.client_id_on_b().clone(), |
| 78 | + Counterparty::new( |
| 79 | + vars.client_id_on_a().clone(), |
| 80 | + Some(msg.conn_id_on_a.clone()), |
| 81 | + prefix_on_a, |
| 82 | + ), |
| 83 | + vec![msg.version.clone()], |
| 84 | + vars.conn_end_on_a.delay_period(), |
| 85 | + ); |
| 86 | + |
| 87 | + client_state_of_b_on_a |
| 88 | + .verify_connection_state( |
| 89 | + msg.proofs_height_on_b, |
| 90 | + prefix_on_b, |
| 91 | + &msg.proof_conn_end_on_b, |
| 92 | + consensus_state_of_b_on_a.root(), |
| 93 | + &msg.conn_id_on_b, |
| 94 | + &expected_conn_end_on_b, |
| 95 | + ) |
| 96 | + .map_err(ConnectionError::VerifyConnectionState)?; |
| 97 | + } |
| 98 | + |
| 99 | + client_state_of_b_on_a |
| 100 | + .verify_client_full_state( |
| 101 | + msg.proofs_height_on_b, |
| 102 | + prefix_on_b, |
| 103 | + &msg.proof_client_state_of_a_on_b, |
| 104 | + consensus_state_of_b_on_a.root(), |
| 105 | + vars.client_id_on_b(), |
| 106 | + msg.client_state_of_a_on_b.clone(), |
| 107 | + ) |
| 108 | + .map_err(|e| ConnectionError::ClientStateVerificationFailure { |
| 109 | + client_id: vars.client_id_on_a().clone(), |
| 110 | + client_error: e, |
| 111 | + })?; |
| 112 | + |
| 113 | + let expected_consensus_state_of_a_on_b = ctx_a |
| 114 | + .host_consensus_state(msg.consensus_height_of_a_on_b) |
| 115 | + .map_err(|_| ConnectionError::Other { |
| 116 | + description: "failed to fetch host consensus state".to_string(), |
| 117 | + })?; |
| 118 | + |
| 119 | + client_state_of_b_on_a |
| 120 | + .verify_client_consensus_state( |
| 121 | + msg.proofs_height_on_b, |
| 122 | + prefix_on_b, |
| 123 | + &msg.proof_consensus_state_of_a_on_b, |
| 124 | + consensus_state_of_b_on_a.root(), |
| 125 | + vars.client_id_on_b(), |
| 126 | + msg.consensus_height_of_a_on_b, |
| 127 | + expected_consensus_state_of_a_on_b.as_ref(), |
| 128 | + ) |
| 129 | + .map_err(|e| ConnectionError::ConsensusStateVerificationFailure { |
| 130 | + height: msg.proofs_height_on_b, |
| 131 | + client_error: e, |
| 132 | + })?; |
| 133 | + } |
| 134 | + |
| 135 | + Ok(()) |
| 136 | +} |
| 137 | + |
| 138 | +pub(crate) fn execute<Ctx>(ctx_a: &mut Ctx, msg: MsgConnectionOpenAck) -> Result<(), ContextError> |
| 139 | +where |
| 140 | + Ctx: ExecutionContext, |
| 141 | +{ |
| 142 | + let vars = LocalVars::new(ctx_a, &msg)?; |
| 143 | + execute_impl(ctx_a, msg, vars) |
| 144 | +} |
| 145 | + |
| 146 | +fn execute_impl<Ctx>( |
| 147 | + ctx_a: &mut Ctx, |
| 148 | + msg: MsgConnectionOpenAck, |
| 149 | + vars: LocalVars, |
| 150 | +) -> Result<(), ContextError> |
| 151 | +where |
| 152 | + Ctx: ExecutionContext, |
| 153 | +{ |
| 154 | + ctx_a.emit_ibc_event(IbcEvent::OpenAckConnection(OpenAck::new( |
| 155 | + msg.conn_id_on_a.clone(), |
| 156 | + vars.client_id_on_a().clone(), |
| 157 | + msg.conn_id_on_b.clone(), |
| 158 | + vars.client_id_on_b().clone(), |
| 159 | + ))); |
| 160 | + |
| 161 | + ctx_a.log_message("success: conn_open_ack verification passed".to_string()); |
| 162 | + |
| 163 | + { |
| 164 | + let new_conn_end_on_a = { |
| 165 | + let mut counterparty = vars.conn_end_on_a.counterparty().clone(); |
| 166 | + counterparty.connection_id = Some(msg.conn_id_on_b.clone()); |
| 167 | + |
| 168 | + let mut new_conn_end_on_a = vars.conn_end_on_a; |
| 169 | + new_conn_end_on_a.set_state(State::Open); |
| 170 | + new_conn_end_on_a.set_version(msg.version.clone()); |
| 171 | + new_conn_end_on_a.set_counterparty(counterparty); |
| 172 | + new_conn_end_on_a |
| 173 | + }; |
| 174 | + |
| 175 | + ctx_a.store_connection(ConnectionsPath(msg.conn_id_on_a), &new_conn_end_on_a)?; |
| 176 | + } |
| 177 | + |
| 178 | + Ok(()) |
| 179 | +} |
| 180 | + |
| 181 | +struct LocalVars { |
| 182 | + conn_end_on_a: ConnectionEnd, |
| 183 | +} |
| 184 | + |
| 185 | +impl LocalVars { |
| 186 | + fn new<Ctx>(ctx_a: &Ctx, msg: &MsgConnectionOpenAck) -> Result<Self, ContextError> |
| 187 | + where |
| 188 | + Ctx: ValidationContext, |
| 189 | + { |
| 190 | + Ok(LocalVars { |
| 191 | + conn_end_on_a: ctx_a.connection_end(&msg.conn_id_on_a)?, |
| 192 | + }) |
| 193 | + } |
| 194 | + |
| 195 | + fn client_id_on_a(&self) -> &ClientId { |
| 196 | + self.conn_end_on_a.client_id() |
| 197 | + } |
| 198 | + |
| 199 | + fn client_id_on_b(&self) -> &ClientId { |
| 200 | + self.conn_end_on_a.counterparty().client_id() |
| 201 | + } |
| 202 | +} |
| 203 | + |
15 | 204 | /// Per our convention, this message is processed on chain A.
|
16 | 205 | pub(crate) fn process(
|
17 | 206 | ctx_a: &dyn ConnectionReader,
|
@@ -139,6 +328,8 @@ pub(crate) fn process(
|
139 | 328 |
|
140 | 329 | #[cfg(test)]
|
141 | 330 | mod tests {
|
| 331 | + use crate::core::ics26_routing::msgs::MsgEnvelope; |
| 332 | + use crate::core::ValidationContext; |
142 | 333 | use crate::prelude::*;
|
143 | 334 |
|
144 | 335 | use core::str::FromStr;
|
@@ -267,6 +458,33 @@ mod tests {
|
267 | 458 | ];
|
268 | 459 |
|
269 | 460 | for test in tests {
|
| 461 | + let res = ValidationContext::validate( |
| 462 | + &test.ctx, |
| 463 | + MsgEnvelope::ConnectionMsg(test.msg.clone()), |
| 464 | + ); |
| 465 | + |
| 466 | + match res { |
| 467 | + Ok(_) => { |
| 468 | + assert!( |
| 469 | + test.want_pass, |
| 470 | + "conn_open_ack: test passed but was supposed to fail for test: {}, \nparams {:?} {:?}", |
| 471 | + test.name, |
| 472 | + test.msg.clone(), |
| 473 | + test.ctx.clone() |
| 474 | + ) |
| 475 | + } |
| 476 | + Err(e) => { |
| 477 | + assert!( |
| 478 | + !test.want_pass, |
| 479 | + "conn_open_ack: did not pass test: {}, \nparams {:?} {:?} error: {:?}", |
| 480 | + test.name, |
| 481 | + test.msg, |
| 482 | + test.ctx.clone(), |
| 483 | + e, |
| 484 | + ); |
| 485 | + } |
| 486 | + } |
| 487 | + |
270 | 488 | let res = dispatch(&test.ctx, test.msg.clone());
|
271 | 489 | // Additionally check the events and the output objects in the result.
|
272 | 490 | match res {
|
|
0 commit comments