Skip to content

Commit 2f2e518

Browse files
committed
Implement ConnOpenAck datagrain in the ICS Connection spec
1 parent 52cc5fd commit 2f2e518

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

core/src/ibc/connection_03/manager.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,45 @@ impl<'a> Manager<'a> {
118118
Ok(())
119119
}
120120

121+
pub fn handle_open_ack(
122+
&mut self,
123+
identifier: Identifier,
124+
proof_try: CommitmentProof,
125+
proof_consensus: CommitmentProof,
126+
proof_height: u64,
127+
consensus_height: u64,
128+
) -> Result<(), String> {
129+
let current_height = self.ctx.get_current_height();
130+
if consensus_height > current_height {
131+
return Err(format!(
132+
"Consensus height {} is greater than current height {}",
133+
consensus_height, current_height
134+
))
135+
}
136+
let mut connection = self
137+
.query(&identifier)
138+
.ok_or_else(|| format!("Cannot find connection with the identifier: {}", identifier))?;
139+
140+
if connection.state != ConnectionState::INIT && connection.state != ConnectionState::TRYOPEN {
141+
return Err(format!("Invalid connection state expected INIT or TRYOPEN but found {:?}", connection.state))
142+
}
143+
let expected_connection = ConnectionEnd {
144+
state: ConnectionState::TRYOPEN,
145+
counterparty_connection_identifier: identifier.clone(),
146+
counterparty_prefix: get_commiment_prefix(),
147+
client_identifier: connection.counterparty_client_identifier.clone(),
148+
counterparty_client_identifier: connection.client_identifier.clone(),
149+
};
150+
self.verify_connection_state(&connection, proof_height, proof_try, identifier.clone(), &expected_connection);
151+
152+
connection.state = ConnectionState::OPEN;
153+
let kv_store = self.ctx.get_kv_store();
154+
let path = connection_path(&identifier);
155+
kv_store.set(&path, &connection.rlp_bytes());
156+
157+
Ok(())
158+
}
159+
121160
fn query(&mut self, identifier: &str) -> Option<ConnectionEnd> {
122161
let kv_store = self.ctx.get_kv_store();
123162

core/src/ibc/transaction_handler/datagrams.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum DatagramTag {
2323
UpdateClient = 2,
2424
ConnOpenInit = 3,
2525
ConnOpenTry = 4,
26+
ConnOpenAck = 5,
2627
}
2728

2829
impl Encodable for DatagramTag {
@@ -39,6 +40,7 @@ impl Decodable for DatagramTag {
3940
2 => Ok(DatagramTag::UpdateClient),
4041
3 => Ok(DatagramTag::ConnOpenInit),
4142
4 => Ok(DatagramTag::ConnOpenTry),
43+
5 => Ok(DatagramTag::ConnOpenAck),
4244
_ => Err(DecoderError::Custom("Unexpected DatagramTag Value")),
4345
}
4446
}
@@ -74,6 +76,13 @@ pub enum Datagram {
7476
proof_height: u64,
7577
consensus_height: u64,
7678
},
79+
ConnOpenAck {
80+
identifier: String,
81+
proof_try: String,
82+
proof_consensus: String,
83+
proof_height: u64,
84+
consensus_height: u64,
85+
},
7786
}
7887

7988
impl Encodable for Datagram {
@@ -136,6 +145,21 @@ impl Encodable for Datagram {
136145
.append(proof_height)
137146
.append(consensus_height);
138147
}
148+
Datagram::ConnOpenAck {
149+
identifier,
150+
proof_try,
151+
proof_consensus,
152+
proof_height,
153+
consensus_height,
154+
} => {
155+
s.begin_list(6);
156+
s.append(&DatagramTag::ConnOpenAck)
157+
.append(identifier)
158+
.append(proof_try)
159+
.append(proof_consensus)
160+
.append(proof_height)
161+
.append(consensus_height);
162+
}
139163
};
140164
}
141165
}
@@ -208,6 +232,22 @@ impl Decodable for Datagram {
208232
consensus_height: rlp.val_at(9)?,
209233
})
210234
}
235+
DatagramTag::ConnOpenAck => {
236+
let item_count = rlp.item_count()?;
237+
if item_count != 6 {
238+
return Err(DecoderError::RlpInvalidLength {
239+
expected: 6,
240+
got: item_count,
241+
})
242+
}
243+
Ok(Datagram::ConnOpenAck {
244+
identifier: rlp.val_at(1)?,
245+
proof_try: rlp.val_at(2)?,
246+
proof_consensus: rlp.val_at(3)?,
247+
proof_height: rlp.val_at(4)?,
248+
consensus_height: rlp.val_at(5)?,
249+
})
250+
}
211251
}
212252
}
213253
}

core/src/ibc/transaction_handler/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ pub fn execute(
9696
)
9797
.map_err(|err| RuntimeError::IBC(format!("ConnOpenTry: {}", err)).into())
9898
}
99+
Datagram::ConnOpenAck {
100+
identifier,
101+
proof_try,
102+
proof_consensus,
103+
proof_height,
104+
consensus_height,
105+
} => {
106+
let mut connection_manager = ibc_connection::Manager::new(&mut context);
107+
connection_manager
108+
.handle_open_ack(identifier, proof_try, proof_consensus, proof_height, consensus_height)
109+
.map_err(|err| RuntimeError::IBC(format!("ConnOpenAck: {}", err)).into())
110+
}
99111
}
100112
}
101113

0 commit comments

Comments
 (0)