@@ -260,12 +260,64 @@ impl<T: TrieReader> Merkle<T> {
260260 /// incremental range proof verification
261261 pub fn verify_range_proof (
262262 & self ,
263- _first_key : Option < impl KeyType > ,
264- _last_key : Option < impl KeyType > ,
265- _root_hash : & TrieHash ,
266- _proof : & RangeProof < impl KeyType , impl ValueType , impl ProofCollection > ,
263+ first_key : Option < impl KeyType > ,
264+ last_key : Option < impl KeyType > ,
265+ root_hash : & TrieHash ,
266+ proof : & RangeProof < impl KeyType , impl ValueType , impl ProofCollection > ,
267267 ) -> Result < ( ) , api:: Error > {
268- todo ! ( )
268+ // check that the keys are in ascending order
269+ let key_values = proof. key_values ( ) ;
270+ if !key_values
271+ . iter ( )
272+ . zip ( key_values. iter ( ) . skip ( 1 ) )
273+ . all ( |( a, b) | a. 0 . as_ref ( ) < b. 0 . as_ref ( ) )
274+ {
275+ return Err ( api:: Error :: ProofError (
276+ ProofError :: NonMonotonicIncreaseRange ,
277+ ) ) ;
278+ }
279+
280+ // check that the start and end proofs are valid
281+ let left = key_values
282+ . first ( )
283+ . ok_or ( api:: Error :: ProofError ( ProofError :: Empty ) ) ?;
284+
285+ // Verify that first_key (if provided) is <= the first key in the proof
286+ if let Some ( ref requested_first) = first_key
287+ && requested_first. as_ref ( ) > left. 0 . as_ref ( )
288+ {
289+ return Err ( api:: Error :: InvalidRange {
290+ start_key : requested_first. as_ref ( ) . to_vec ( ) . into ( ) ,
291+ end_key : left. 0 . as_ref ( ) . to_vec ( ) . into ( ) ,
292+ } ) ;
293+ }
294+
295+ proof
296+ . start_proof ( )
297+ . verify ( & left. 0 , Some ( & left. 1 ) , root_hash) ?;
298+
299+ let right = key_values
300+ . last ( )
301+ . ok_or ( api:: Error :: ProofError ( ProofError :: Empty ) ) ?;
302+
303+ // Verify that last_key (if provided) is >= the last key in the proof
304+ if let Some ( ref requested_last) = last_key
305+ && requested_last. as_ref ( ) < right. 0 . as_ref ( )
306+ {
307+ return Err ( api:: Error :: InvalidRange {
308+ start_key : right. 0 . as_ref ( ) . to_vec ( ) . into ( ) ,
309+ end_key : requested_last. as_ref ( ) . to_vec ( ) . into ( ) ,
310+ } ) ;
311+ }
312+
313+ proof
314+ . end_proof ( )
315+ . verify ( & right. 0 , Some ( & right. 1 ) , root_hash) ?;
316+
317+ // TODO: build a merkle and reshape it, filling in hashes from the
318+ // provided proofs on the left and right edges, then verify the root hash
319+
320+ Ok ( ( ) )
269321 }
270322
271323 /// Merges a sequence of key-value pairs with the base merkle trie, yielding
0 commit comments