@@ -260,12 +260,60 @@ 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+ proof
270+ . key_values ( )
271+ . windows ( 2 )
272+ . all ( |pair| pair[ 0 ] . 0 . as_ref ( ) < pair[ 1 ] . 0 . as_ref ( ) ) ;
273+
274+ // check that the start and end proofs are valid
275+ let left = proof
276+ . key_values ( )
277+ . first ( )
278+ . ok_or ( api:: Error :: ProofError ( ProofError :: Empty ) ) ?;
279+
280+ // Verify that first_key (if provided) is <= the first key in the proof
281+ if let Some ( ref requested_first) = first_key {
282+ if requested_first. as_ref ( ) > left. 0 . as_ref ( ) {
283+ return Err ( api:: Error :: InvalidRange {
284+ start_key : requested_first. as_ref ( ) . to_vec ( ) . into ( ) ,
285+ end_key : left. 0 . as_ref ( ) . to_vec ( ) . into ( ) ,
286+ } ) ;
287+ }
288+ }
289+
290+ proof
291+ . start_proof ( )
292+ . verify ( & left. 0 , Some ( & left. 1 ) , root_hash) ?;
293+
294+ let right = proof
295+ . key_values ( )
296+ . last ( )
297+ . ok_or ( api:: Error :: ProofError ( ProofError :: Empty ) ) ?;
298+
299+ // Verify that last_key (if provided) is >= the last key in the proof
300+ if let Some ( ref requested_last) = last_key {
301+ if requested_last. as_ref ( ) < right. 0 . as_ref ( ) {
302+ return Err ( api:: Error :: InvalidRange {
303+ start_key : right. 0 . as_ref ( ) . to_vec ( ) . into ( ) ,
304+ end_key : requested_last. as_ref ( ) . to_vec ( ) . into ( ) ,
305+ } ) ;
306+ }
307+ }
308+
309+ proof
310+ . end_proof ( )
311+ . verify ( & right. 0 , Some ( & right. 1 ) , root_hash) ?;
312+
313+ // TODO: build a merkle and reshape it, filling in hashes from the
314+ // provided proofs on the left and right edges, then verify the root hash
315+
316+ Ok ( ( ) )
269317 }
270318
271319 /// Merges a sequence of key-value pairs with the base merkle trie, yielding
0 commit comments