Skip to content

Commit f26e693

Browse files
committed
Implementing handle_open_init function
1 parent 20135e1 commit f26e693

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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::types::{CommitmentPrefix, Identifier};
18+
use super::{client_connections_path, path as connection_path};
19+
use crate::ibc;
20+
use crate::ibc::client_02::ConsensusState;
21+
use crate::ibc::connection_03::types::{ConnectionEnd, ConnectionState};
22+
use ibc::commitment_23::Prefix;
23+
use rlp::Encodable;
24+
25+
#[derive(Default)]
26+
pub struct Manager {}
27+
28+
impl Manager {
29+
pub fn new() -> Self {
30+
Manager {}
31+
}
32+
33+
pub fn handle_open_init(
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,
51+
counterparty_client_identifier,
52+
};
53+
kv_store.set(&connection_path(&identifier), &connection.rlp_bytes());
54+
// add connection to client
55+
Ok(())
56+
}
57+
}

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
19+
pub enum ConnectionState {
20+
INIT,
21+
TRYOPEN,
22+
OPEN,
23+
}
24+
25+
// FIXME: current commitment_23::Prefix is too generic.
26+
pub type CommitmentPrefix = String;
27+
pub type Identifier = String;
28+
29+
pub struct ConnectionEnd {
30+
state: ConnectionState,
31+
counterparty_connection_identifier: Identifier,
32+
counterparty_prefix: CommitmentPrefix,
33+
client_identifier: Identifier,
34+
counterparty_client_identifier: Identifier,
35+
// FIXME: implement version
36+
}
37+
38+
impl rlp::Encodable for ConnectionEnd {
39+
fn rlp_append(&self, s: &mut rlp::RlpStream) {
40+
s.begin_list(5);
41+
s.append(&self.state);
42+
s.append(&self.counterparty_connection_identifier);
43+
s.append(&self.counterparty_prefix);
44+
s.append(&self.client_identifier);
45+
s.append(&self.counterparty_client_identifier);
46+
}
47+
}

core/src/ibc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod client_02;
1818
#[allow(dead_code)]
1919
#[allow(unused_variables)]
2020
mod commitment_23;
21+
mod connection_03;
2122
pub mod context;
2223
mod kv_store;
2324
mod transaction_handler;

0 commit comments

Comments
 (0)