1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
1717use super :: path as connection_path;
18- use super :: types:: { CommitmentPrefix , Identifier } ;
18+ use super :: types:: { CommitmentPrefix , CommitmentProof , Identifier } ;
1919use crate :: ibc;
2020use crate :: ibc:: connection_03:: client_connections_path;
2121use crate :: ibc:: connection_03:: types:: { ConnectionEnd , ConnectionIdentifiersInClient , ConnectionState } ;
@@ -24,6 +24,11 @@ use rlp::{Encodable, Rlp};
2424#[ derive( Default ) ]
2525pub struct Manager { }
2626
27+ // FIXME: this will be changed after implementing Vector commitment
28+ fn get_commiment_prefix ( ) -> String {
29+ "" . to_owned ( )
30+ }
31+
2732impl Manager {
2833 pub fn new ( ) -> Self {
2934 Manager { }
@@ -55,6 +60,105 @@ impl Manager {
5560 Ok ( ( ) )
5661 }
5762
63+ // We all following ICS spec.
64+ #[ allow( clippy:: too_many_arguments) ]
65+ pub fn handle_open_try (
66+ & self ,
67+ ctx : & mut dyn ibc:: Context ,
68+ desired_identifier : Identifier ,
69+ counterparty_connection_identifier : Identifier ,
70+ counterparty_prefix : CommitmentPrefix ,
71+ counterparty_client_identifier : Identifier ,
72+ client_identifier : Identifier ,
73+ proof_init : CommitmentProof ,
74+ proof_consensus : CommitmentProof ,
75+ proof_height : u64 ,
76+ consensus_height : u64 ,
77+ ) -> Result < ( ) , String > {
78+ let current_height = ctx. get_current_height ( ) ;
79+ if consensus_height > current_height {
80+ return Err ( format ! (
81+ "Consensus height {} is greater than current height {}" ,
82+ consensus_height, current_height
83+ ) )
84+ }
85+ let expected = ConnectionEnd {
86+ state : ConnectionState :: INIT ,
87+ counterparty_connection_identifier : desired_identifier. clone ( ) ,
88+ counterparty_prefix : get_commiment_prefix ( ) ,
89+ client_identifier : counterparty_client_identifier. clone ( ) ,
90+ counterparty_client_identifier : client_identifier. clone ( ) ,
91+ } ;
92+
93+ let connection = ConnectionEnd {
94+ state : ConnectionState :: TRYOPEN ,
95+ counterparty_connection_identifier : counterparty_client_identifier. clone ( ) ,
96+ counterparty_prefix : counterparty_prefix. clone ( ) ,
97+ client_identifier : client_identifier. clone ( ) ,
98+ counterparty_client_identifier : counterparty_client_identifier. clone ( ) ,
99+ } ;
100+
101+ self . verify_connection_state ( ctx, & connection, proof_height, proof_init, desired_identifier. clone ( ) , & expected) ;
102+
103+ if let Some ( previous_connection_end) = self . query ( ctx, & desired_identifier) {
104+ let expected_init = ConnectionEnd {
105+ state : ConnectionState :: INIT ,
106+ counterparty_connection_identifier,
107+ counterparty_prefix,
108+ client_identifier,
109+ counterparty_client_identifier,
110+ } ;
111+ if previous_connection_end != expected_init {
112+ return Err ( format ! (
113+ "Invalid previous connection status: previous: {:?}, expected: {:?}" ,
114+ previous_connection_end, expected_init
115+ ) )
116+ }
117+ }
118+
119+ let kv_store = ctx. get_kv_store ( ) ;
120+ kv_store. set ( & connection_path ( & desired_identifier) , & connection. rlp_bytes ( ) ) ;
121+ Ok ( ( ) )
122+ }
123+
124+ fn query ( & self , ctx : & mut dyn ibc:: Context , identifier : & str ) -> Option < ConnectionEnd > {
125+ let kv_store = ctx. get_kv_store ( ) ;
126+
127+ let path = connection_path ( & identifier) ;
128+ if kv_store. has ( & path) {
129+ let raw = kv_store. get ( & path) ;
130+ let connection_end = rlp:: decode ( & raw ) . expect ( "Only the connection code can save the code" ) ;
131+ return Some ( connection_end)
132+ }
133+
134+ None
135+ }
136+
137+ fn verify_connection_state (
138+ & self ,
139+ ctx : & mut dyn ibc:: Context ,
140+ connection : & ConnectionEnd ,
141+ proof_height : u64 ,
142+ proof : CommitmentProof ,
143+ connection_identifier : Identifier ,
144+ connection_end : & ConnectionEnd ,
145+ ) -> bool {
146+ // check values in the connection_end
147+ let path = format ! ( "connections/{}" , connection_identifier) ;
148+ self . client_verify_membership ( proof_height, proof, path, & rlp:: encode ( connection_end) )
149+ }
150+
151+ fn client_verify_membership (
152+ & self ,
153+ _height : u64 ,
154+ _commitment_proof : CommitmentProof ,
155+ _path : String ,
156+ _value : & [ u8 ] ,
157+ ) -> bool {
158+ // FIXME
159+ true
160+ }
161+
58162 fn add_connection_to_client (
59163 & self ,
60164 ctx : & mut dyn ibc:: Context ,
0 commit comments