-
Notifications
You must be signed in to change notification settings - Fork 12
Implement connOpenInit datagram in the ICS connection spec #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| use super::path as connection_path; | ||
| use super::types::{CommitmentPrefix, Identifier}; | ||
| use crate::ibc; | ||
| use crate::ibc::connection_03::client_connections_path; | ||
| use crate::ibc::connection_03::types::{ConnectionEnd, ConnectionIdentifiersInClient, ConnectionState}; | ||
| use rlp::{Encodable, Rlp}; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct Manager {} | ||
|
|
||
| impl Manager { | ||
| pub fn new() -> Self { | ||
| Manager {} | ||
| } | ||
|
|
||
| pub fn handle_open_init( | ||
| &self, | ||
| ctx: &mut dyn ibc::Context, | ||
| identifier: Identifier, | ||
| desired_counterparty_connection_identifier: Identifier, | ||
| counterparty_prefix: CommitmentPrefix, | ||
| client_identifier: Identifier, | ||
| counterparty_client_identifier: Identifier, | ||
| ) -> Result<(), String> { | ||
| let kv_store = ctx.get_kv_store(); | ||
| if kv_store.has(&connection_path(&identifier)) { | ||
| return Err("Connection exist".to_owned()) | ||
| } | ||
| let state = ConnectionState::INIT; | ||
| let connection = ConnectionEnd { | ||
| state, | ||
| counterparty_connection_identifier: desired_counterparty_connection_identifier, | ||
| counterparty_prefix, | ||
| client_identifier: client_identifier.clone(), | ||
| counterparty_client_identifier, | ||
| }; | ||
| kv_store.set(&connection_path(&identifier), &connection.rlp_bytes()); | ||
| self.add_connection_to_client(ctx, client_identifier, identifier)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn add_connection_to_client( | ||
| &self, | ||
| ctx: &mut dyn ibc::Context, | ||
| client_identifier: Identifier, | ||
| connection_identifier: Identifier, | ||
| ) -> Result<(), String> { | ||
| let kv_store = ctx.get_kv_store(); | ||
| if kv_store.has(&connection_path(&connection_identifier)) { | ||
| return Err("Connection exist".to_owned()) | ||
| } | ||
| let bytes = kv_store.get(&client_connections_path(&client_identifier)); | ||
| let rlp = Rlp::new(&bytes); | ||
| let mut conns: ConnectionIdentifiersInClient = rlp.as_val().expect("data from DB"); | ||
|
|
||
| conns.add(connection_identifier); | ||
|
|
||
| kv_store.set(&client_connections_path(&client_identifier), &rlp::encode(&conns)); | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| mod manager; | ||
| mod types; | ||
|
|
||
| pub fn path(id: &str) -> String { | ||
| format!("connections/{}", id) | ||
| } | ||
|
|
||
| pub fn client_connections_path(client_id: &str) -> String { | ||
| format!("clients/{}/connections", client_id) | ||
| } | ||
|
|
||
| pub use manager::Manager; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2020 Kodebox, Inc. | ||
| // This file is part of CodeChain. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| use rlp; | ||
| use rlp::{DecoderError, Rlp, RlpStream}; | ||
|
|
||
| #[repr(u8)] | ||
| #[derive(Copy, Clone, PartialEq, Debug)] | ||
| pub enum ConnectionState { | ||
| INIT = 0, | ||
| TRYOPEN = 1, | ||
| OPEN = 2, | ||
| } | ||
|
|
||
| impl rlp::Encodable for ConnectionState { | ||
| fn rlp_append(&self, s: &mut RlpStream) { | ||
| s.append_single_value(&(*self as u8)); | ||
| } | ||
| } | ||
|
|
||
| impl rlp::Decodable for ConnectionState { | ||
| fn decode(rlp: &Rlp) -> Result<Self, DecoderError> { | ||
| let byte: u8 = rlp.as_val()?; | ||
| match byte { | ||
| 0 => Ok(ConnectionState::INIT), | ||
| 1 => Ok(ConnectionState::TRYOPEN), | ||
| 2 => Ok(ConnectionState::OPEN), | ||
| _ => Err(DecoderError::Custom("Unexpected ConsensusState Value")), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // FIXME: current commitment_23::Prefix is too generic. | ||
| pub type CommitmentPrefix = String; | ||
| pub type Identifier = String; | ||
|
|
||
| #[derive(RlpEncodable, RlpDecodable, PartialEq, Debug)] | ||
| pub struct ConnectionEnd { | ||
| pub state: ConnectionState, | ||
| pub counterparty_connection_identifier: Identifier, | ||
| pub counterparty_prefix: CommitmentPrefix, | ||
| pub client_identifier: Identifier, | ||
| pub counterparty_client_identifier: Identifier, | ||
| // FIXME: implement version | ||
| } | ||
|
|
||
| #[derive(RlpEncodableWrapper, RlpDecodableWrapper, PartialEq, Debug)] | ||
| pub struct ConnectionIdentifiersInClient(Vec<Identifier>); | ||
|
|
||
| impl ConnectionIdentifiersInClient { | ||
| pub fn add(&mut self, identifier: Identifier) { | ||
| self.0.push(identifier); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rlp::{self, rlp_encode_and_decode_test}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn connection_state() { | ||
| rlp_encode_and_decode_test!(ConnectionState::INIT); | ||
| } | ||
|
|
||
| #[test] | ||
| fn connection_end() { | ||
| let connection_end = ConnectionEnd { | ||
| state: ConnectionState::INIT, | ||
| counterparty_connection_identifier: "counterparty_connection_identifier".to_owned(), | ||
| counterparty_prefix: "counterparty_prefix".to_owned(), | ||
| client_identifier: "client_identifier".to_owned(), | ||
| counterparty_client_identifier: "counterparty_client_identifier".to_owned(), | ||
| }; | ||
| rlp_encode_and_decode_test!(connection_end); | ||
| } | ||
|
|
||
| #[test] | ||
| fn connection_identifiers_in_client() { | ||
| let identifiers = ConnectionIdentifiersInClient(vec!["a".to_owned(), "b".to_owned()]); | ||
| rlp_encode_and_decode_test!(identifiers); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it. Could you check it again?