|
| 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 | +} |
0 commit comments