Skip to content

Commit f54dd3c

Browse files
committed
Implement handle_open_init function
1 parent 74472f8 commit f54dd3c

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2020 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use super::path as connection_path;
18+
use super::types::{CommitmentPrefix, Identifier};
19+
use crate::ibc;
20+
use crate::ibc::connection_03::client_connections_path;
21+
use crate::ibc::connection_03::types::{ConnectionEnd, ConnectionIdentifiersInClient, ConnectionState};
22+
use rlp::{Encodable, Rlp};
23+
24+
#[derive(Default)]
25+
pub struct Manager {}
26+
27+
impl Manager {
28+
pub fn new() -> Self {
29+
Manager {}
30+
}
31+
32+
pub fn handle_open_init(
33+
&self,
34+
ctx: &mut dyn ibc::Context,
35+
identifier: Identifier,
36+
desired_counterparty_connection_identifier: Identifier,
37+
counterparty_prefix: CommitmentPrefix,
38+
client_identifier: Identifier,
39+
counterparty_client_identifier: Identifier,
40+
) -> Result<(), String> {
41+
let kv_store = ctx.get_kv_store();
42+
if kv_store.has(&connection_path(&identifier)) {
43+
return Err("Connection exist".to_owned())
44+
}
45+
let state = ConnectionState::INIT;
46+
let connection = ConnectionEnd {
47+
state,
48+
counterparty_connection_identifier: desired_counterparty_connection_identifier,
49+
counterparty_prefix,
50+
client_identifier: client_identifier.clone(),
51+
counterparty_client_identifier,
52+
};
53+
kv_store.set(&connection_path(&identifier), &connection.rlp_bytes());
54+
self.add_connection_to_client(ctx, client_identifier, identifier)?;
55+
Ok(())
56+
}
57+
58+
fn add_connection_to_client(
59+
&self,
60+
ctx: &mut dyn ibc::Context,
61+
client_identifier: Identifier,
62+
connection_identifier: Identifier,
63+
) -> Result<(), String> {
64+
let kv_store = ctx.get_kv_store();
65+
if kv_store.has(&connection_path(&connection_identifier)) {
66+
return Err("Connection exist".to_owned())
67+
}
68+
let bytes = kv_store.get(&client_connections_path(&client_identifier));
69+
let rlp = Rlp::new(&bytes);
70+
let mut conns: ConnectionIdentifiersInClient = rlp.as_val().expect("data from DB");
71+
72+
conns.add(connection_identifier);
73+
74+
kv_store.set(&client_connections_path(&client_identifier), &rlp::encode(&conns));
75+
Ok(())
76+
}
77+
}

core/src/ibc/connection_03/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2020 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
mod manager;
18+
mod types;
19+
20+
pub fn path(id: &str) -> String {
21+
format!("connections/{}", id)
22+
}
23+
24+
pub fn client_connections_path(client_id: &str) -> String {
25+
format!("clients/{}/connections", client_id)
26+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2020 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use rlp;
18+
use rlp::{DecoderError, Rlp, RlpStream};
19+
20+
#[repr(u8)]
21+
#[derive(Copy, Clone)]
22+
pub enum ConnectionState {
23+
INIT = 0,
24+
TRYOPEN = 1,
25+
OPEN = 2,
26+
}
27+
28+
impl rlp::Encodable for ConnectionState {
29+
fn rlp_append(&self, s: &mut RlpStream) {
30+
s.append(&(*self as u8));
31+
}
32+
}
33+
34+
// FIXME: current commitment_23::Prefix is too generic.
35+
pub type CommitmentPrefix = String;
36+
pub type Identifier = String;
37+
38+
pub struct ConnectionEnd {
39+
pub state: ConnectionState,
40+
pub counterparty_connection_identifier: Identifier,
41+
pub counterparty_prefix: CommitmentPrefix,
42+
pub client_identifier: Identifier,
43+
pub counterparty_client_identifier: Identifier,
44+
// FIXME: implement version
45+
}
46+
47+
impl rlp::Encodable for ConnectionEnd {
48+
fn rlp_append(&self, s: &mut rlp::RlpStream) {
49+
s.begin_list(5);
50+
s.append(&self.state);
51+
s.append(&self.counterparty_connection_identifier);
52+
s.append(&self.counterparty_prefix);
53+
s.append(&self.client_identifier);
54+
s.append(&self.counterparty_client_identifier);
55+
}
56+
}
57+
58+
pub struct ConnectionIdentifiersInClient(Vec<Identifier>);
59+
60+
impl ConnectionIdentifiersInClient {
61+
pub fn add(&mut self, identifier: Identifier) {
62+
self.0.push(identifier);
63+
}
64+
}
65+
66+
impl rlp::Encodable for ConnectionIdentifiersInClient {
67+
fn rlp_append(&self, s: &mut RlpStream) {
68+
s.append_list::<String, String>(&self.0);
69+
}
70+
}
71+
72+
impl rlp::Decodable for ConnectionIdentifiersInClient {
73+
fn decode<'a>(rlp: &Rlp<'a>) -> Result<Self, DecoderError> {
74+
Ok(Self(rlp.as_list()?))
75+
}
76+
}

core/src/ibc/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub mod client_02;
1818
#[allow(dead_code)]
1919
#[allow(unused_variables)]
2020
mod commitment_23;
21+
#[allow(dead_code)]
22+
#[allow(unused_variables)]
23+
mod connection_03;
2124
pub mod context;
2225
mod kv_store;
2326
mod transaction_handler;

0 commit comments

Comments
 (0)