@@ -29,25 +29,6 @@ pub(crate) fn check_header_and_update_state(
2929 client_id : ClientId ,
3030 header : Any ,
3131) -> Result < UpdatedState , ClientError > {
32- fn maybe_consensus_state (
33- ctx : & dyn ValidationContext ,
34- client_cons_state_path : & ClientConsensusStatePath ,
35- ) -> Result < Option < Box < dyn ConsensusState > > , ClientError > {
36- match ctx. consensus_state ( client_cons_state_path) {
37- Ok ( cs) => Ok ( Some ( cs) ) ,
38- Err ( e) => match e {
39- ContextError :: ClientError ( ClientError :: ConsensusStateNotFound {
40- client_id : _,
41- height : _,
42- } ) => Ok ( None ) ,
43- ContextError :: ClientError ( e) => Err ( e) ,
44- _ => Err ( ClientError :: Other {
45- description : e. to_string ( ) ,
46- } ) ,
47- } ,
48- }
49- }
50-
5132 let client_state = downcast_tm_client_state ( client_state) ?. clone ( ) ;
5233 let header = TmHeader :: try_from ( header) ?;
5334
@@ -61,28 +42,6 @@ pub(crate) fn check_header_and_update_state(
6142 } ) ;
6243 }
6344
64- // Check if a consensus state is already installed; if so it should
65- // match the untrusted header.
66- let header_consensus_state = TmConsensusState :: from ( header. clone ( ) ) ;
67- let client_cons_state_path = ClientConsensusStatePath :: new ( & client_id, & header. height ( ) ) ;
68- let existing_consensus_state = match maybe_consensus_state ( ctx, & client_cons_state_path) ? {
69- Some ( cs) => {
70- let cs = downcast_tm_consensus_state ( cs. as_ref ( ) ) ?;
71- // If this consensus state matches, skip verification
72- // (optimization)
73- if cs == header_consensus_state {
74- // Header is already installed and matches the incoming
75- // header (already verified)
76- return Ok ( UpdatedState {
77- client_state : client_state. into_box ( ) ,
78- consensus_state : cs. into_box ( ) ,
79- } ) ;
80- }
81- Some ( cs)
82- }
83- None => None ,
84- } ;
85-
8645 let trusted_client_cons_state_path =
8746 ClientConsensusStatePath :: new ( & client_id, & header. trusted_height ) ;
8847 let trusted_consensus_state = downcast_tm_consensus_state (
@@ -137,77 +96,6 @@ pub(crate) fn check_header_and_update_state(
13796 Verdict :: Invalid ( detail) => Err ( Error :: VerificationError { detail } ) ,
13897 } ?;
13998
140- // If the header has verified, but its corresponding consensus state
141- // differs from the existing consensus state for that height, freeze the
142- // client and return the installed consensus state.
143- if let Some ( cs) = existing_consensus_state {
144- if cs != header_consensus_state {
145- return Ok ( UpdatedState {
146- client_state : client_state. with_frozen_height ( header. height ( ) ) . into_box ( ) ,
147- consensus_state : cs. into_box ( ) ,
148- } ) ;
149- }
150- }
151-
152- // Monotonicity checks for timestamps for in-the-middle updates
153- // (cs-new, cs-next, cs-latest)
154- if header. height ( ) < client_state. latest_height ( ) {
155- let maybe_next_cs = ctx
156- . next_consensus_state ( & client_id, & header. height ( ) )
157- . map_err ( |e| match e {
158- ContextError :: ClientError ( e) => e,
159- _ => ClientError :: Other {
160- description : e. to_string ( ) ,
161- } ,
162- } ) ?
163- . as_ref ( )
164- . map ( |cs| downcast_tm_consensus_state ( cs. as_ref ( ) ) )
165- . transpose ( ) ?;
166-
167- if let Some ( next_cs) = maybe_next_cs {
168- // New (untrusted) header timestamp cannot occur after next
169- // consensus state's height
170- if header. signed_header . header ( ) . time > next_cs. timestamp {
171- return Err ( ClientError :: ClientSpecific {
172- description : Error :: HeaderTimestampTooHigh {
173- actual : header. signed_header . header ( ) . time . to_string ( ) ,
174- max : next_cs. timestamp . to_string ( ) ,
175- }
176- . to_string ( ) ,
177- } ) ;
178- }
179- }
180- }
181-
182- // (cs-trusted, cs-prev, cs-new)
183- if header. trusted_height < header. height ( ) {
184- let maybe_prev_cs = ctx
185- . prev_consensus_state ( & client_id, & header. height ( ) )
186- . map_err ( |e| match e {
187- ContextError :: ClientError ( e) => e,
188- _ => ClientError :: Other {
189- description : e. to_string ( ) ,
190- } ,
191- } ) ?
192- . as_ref ( )
193- . map ( |cs| downcast_tm_consensus_state ( cs. as_ref ( ) ) )
194- . transpose ( ) ?;
195-
196- if let Some ( prev_cs) = maybe_prev_cs {
197- // New (untrusted) header timestamp cannot occur before the
198- // previous consensus state's height
199- if header. signed_header . header ( ) . time < prev_cs. timestamp {
200- return Err ( ClientError :: ClientSpecific {
201- description : Error :: HeaderTimestampTooLow {
202- actual : header. signed_header . header ( ) . time . to_string ( ) ,
203- min : prev_cs. timestamp . to_string ( ) ,
204- }
205- . to_string ( ) ,
206- } ) ;
207- }
208- }
209- }
210-
21199 Ok ( UpdatedState {
212100 client_state : client_state. with_header ( header. clone ( ) ) ?. into_box ( ) ,
213101 consensus_state : TmConsensusState :: from ( header) . into_box ( ) ,
0 commit comments