Skip to content

Commit e693dd5

Browse files
committed
Implement ConnOpenConfirm datagram
1 parent f63af7b commit e693dd5

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

core/src/ibc/connection_03/manager.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,39 @@ impl<'a> Manager<'a> {
166166
Ok(())
167167
}
168168

169+
pub fn handle_open_confirm(
170+
&mut self,
171+
identifier: Identifier,
172+
proof_ack: Vec<u8>,
173+
proof_height: u64,
174+
) -> Result<(), String> {
175+
let mut connection = self
176+
.query(&identifier)
177+
.ok_or_else(|| format!("Cannot find connection with the identifier: {}", identifier))?;
178+
if connection.state != ConnectionState::TRYOPEN {
179+
return Err(format!("Invalid connection state expected TRYOPEN but found {:?}", connection.state))
180+
}
181+
182+
let expected = ConnectionEnd {
183+
state: ConnectionState::OPEN,
184+
counterparty_connection_identifier: identifier.clone(),
185+
counterparty_prefix: get_commiment_prefix(),
186+
client_identifier: connection.counterparty_client_identifier.clone(),
187+
counterparty_client_identifier: connection.client_identifier.clone(),
188+
};
189+
190+
if !self.verify_connection_state(&connection, proof_height, proof_ack, identifier.clone(), &expected) {
191+
return Err(format!("Counterparty chain's connection state verification fail. expected: {:?}", expected))
192+
}
193+
194+
connection.state = ConnectionState::OPEN;
195+
let kv_store = self.ctx.get_kv_store_mut();
196+
let path = connection_path(&identifier);
197+
kv_store.set(&path, &connection.rlp_bytes());
198+
199+
Ok(())
200+
}
201+
169202
fn query(&mut self, identifier: &str) -> Option<ConnectionEnd> {
170203
let kv_store = self.ctx.get_kv_store();
171204

core/src/ibc/transaction_handler/datagrams.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum DatagramTag {
2424
ConnOpenInit = 3,
2525
ConnOpenTry = 4,
2626
ConnOpenAck = 5,
27+
ConnOpenConfirm = 6,
2728
}
2829

2930
impl Encodable for DatagramTag {
@@ -41,6 +42,7 @@ impl Decodable for DatagramTag {
4142
3 => Ok(DatagramTag::ConnOpenInit),
4243
4 => Ok(DatagramTag::ConnOpenTry),
4344
5 => Ok(DatagramTag::ConnOpenAck),
45+
6 => Ok(DatagramTag::ConnOpenConfirm),
4446
_ => Err(DecoderError::Custom("Unexpected DatagramTag Value")),
4547
}
4648
}
@@ -83,6 +85,11 @@ pub enum Datagram {
8385
proof_height: u64,
8486
consensus_height: u64,
8587
},
88+
ConnOpenConfirm {
89+
identifier: String,
90+
proof_ack: Vec<u8>,
91+
proof_height: u64,
92+
},
8693
}
8794

8895
impl Encodable for Datagram {
@@ -160,6 +167,14 @@ impl Encodable for Datagram {
160167
.append(proof_height)
161168
.append(consensus_height);
162169
}
170+
Datagram::ConnOpenConfirm {
171+
identifier,
172+
proof_ack,
173+
proof_height,
174+
} => {
175+
s.begin_list(4);
176+
s.append(&DatagramTag::ConnOpenConfirm).append(identifier).append(proof_ack).append(proof_height);
177+
}
163178
};
164179
}
165180
}
@@ -248,6 +263,20 @@ impl Decodable for Datagram {
248263
consensus_height: rlp.val_at(5)?,
249264
})
250265
}
266+
DatagramTag::ConnOpenConfirm => {
267+
let item_count = rlp.item_count()?;
268+
if item_count != 4 {
269+
return Err(DecoderError::RlpInvalidLength {
270+
expected: 4,
271+
got: item_count,
272+
})
273+
}
274+
Ok(Datagram::ConnOpenConfirm {
275+
identifier: rlp.val_at(1)?,
276+
proof_ack: rlp.val_at(2)?,
277+
proof_height: rlp.val_at(3)?,
278+
})
279+
}
251280
}
252281
}
253282
}
@@ -296,4 +325,14 @@ mod tests {
296325
};
297326
rlp_encode_and_decode_test!(conn_open_ack);
298327
}
328+
329+
#[test]
330+
fn conn_open_confirm() {
331+
let conn_open_confirm = Datagram::ConnOpenConfirm {
332+
identifier: "identifier".to_owned(),
333+
proof_ack: b"proof_ack".to_vec(),
334+
proof_height: 1,
335+
};
336+
rlp_encode_and_decode_test!(conn_open_confirm);
337+
}
299338
}

core/src/ibc/transaction_handler/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ pub fn execute(
108108
.handle_open_ack(identifier, proof_try, proof_consensus, proof_height, consensus_height)
109109
.map_err(|err| RuntimeError::IBC(format!("ConnOpenAck: {}", err)).into())
110110
}
111+
Datagram::ConnOpenConfirm {
112+
identifier,
113+
proof_ack,
114+
proof_height,
115+
} => {
116+
let mut connection_manager = ibc_connection::Manager::new(&mut context);
117+
connection_manager
118+
.handle_open_confirm(identifier, proof_ack, proof_height)
119+
.map_err(|err| RuntimeError::IBC(format!("ConnOpenConfirm: {}", err)).into())
120+
}
111121
}
112122
}
113123

0 commit comments

Comments
 (0)