@@ -155,7 +155,8 @@ impl HashChainState {
155155#[ cfg( test) ]
156156mod test {
157157 use {
158- crate :: state:: PebbleHashChain ,
158+ crate :: state:: { HashChainState , PebbleHashChain } ,
159+ anyhow:: Result ,
159160 sha3:: { Digest , Keccak256 } ,
160161 } ;
161162
@@ -202,4 +203,95 @@ mod test {
202203 run_hash_chain_test ( [ 0u8 ; 32 ] , 100 , 50 ) ;
203204 run_hash_chain_test ( [ 0u8 ; 32 ] , 100 , 55 ) ;
204205 }
206+
207+ #[ test]
208+ fn test_hash_chain_state_from_chain_at_offset ( ) {
209+ let chain = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
210+ let hash_chain_state = HashChainState :: from_chain_at_offset ( 5 , chain) ;
211+
212+ assert_eq ! ( hash_chain_state. offsets. len( ) , 1 ) ;
213+ assert_eq ! ( hash_chain_state. hash_chains. len( ) , 1 ) ;
214+ assert_eq ! ( hash_chain_state. offsets[ 0 ] , 5 ) ;
215+ }
216+
217+ #[ test]
218+ fn test_hash_chain_state_reveal_valid_sequence ( ) -> Result < ( ) > {
219+ let chain = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
220+ let expected_hash = chain. reveal_ith ( 3 ) ?;
221+
222+ let hash_chain_state = HashChainState :: from_chain_at_offset ( 5 , chain) ;
223+ let result = hash_chain_state. reveal ( 8 ) ?;
224+
225+ assert_eq ! ( result, expected_hash) ;
226+ Ok ( ( ) )
227+ }
228+
229+ #[ test]
230+ fn test_hash_chain_state_reveal_sequence_too_small ( ) {
231+ let chain = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
232+ let hash_chain_state = HashChainState :: from_chain_at_offset ( 5 , chain) ;
233+
234+ let result = hash_chain_state. reveal ( 4 ) ;
235+ assert ! ( result. is_err( ) ) ;
236+ }
237+
238+ #[ test]
239+ fn test_hash_chain_state_reveal_sequence_too_large ( ) {
240+ let chain = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
241+ let hash_chain_state = HashChainState :: from_chain_at_offset ( 5 , chain) ;
242+
243+ let result = hash_chain_state. reveal ( 15 ) ;
244+ assert ! ( result. is_err( ) ) ;
245+ }
246+
247+ #[ test]
248+ fn test_hash_chain_state_multiple_chains ( ) -> Result < ( ) > {
249+ let chain1 = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
250+ let expected_hash1 = chain1. reveal_ith ( 3 ) ?;
251+
252+ let chain2 = PebbleHashChain :: new ( [ 1u8 ; 32 ] , 10 , 1 ) ;
253+ let expected_hash2 = chain2. reveal_ith ( 3 ) ?;
254+
255+ let hash_chain_state = HashChainState {
256+ offsets : vec ! [ 5 , 20 ] ,
257+ hash_chains : vec ! [ chain1, chain2] ,
258+ } ;
259+
260+ let result1 = hash_chain_state. reveal ( 8 ) ?;
261+ assert_eq ! ( result1, expected_hash1) ;
262+
263+ let result2 = hash_chain_state. reveal ( 23 ) ?;
264+ assert_eq ! ( result2, expected_hash2) ;
265+
266+ let result_boundary = hash_chain_state. reveal ( 20 ) ?;
267+ let expected_boundary = hash_chain_state. hash_chains [ 1 ] . reveal_ith ( 0 ) ?;
268+ assert_eq ! ( result_boundary, expected_boundary) ;
269+
270+ let result3 = hash_chain_state. reveal ( 15 ) ;
271+ assert ! ( result3. is_err( ) ) ;
272+
273+ Ok ( ( ) )
274+ }
275+
276+ #[ test]
277+ fn test_hash_chain_state_overlapping_chains ( ) -> Result < ( ) > {
278+ let chain1 = PebbleHashChain :: new ( [ 0u8 ; 32 ] , 10 , 1 ) ;
279+ let chain2 = PebbleHashChain :: new ( [ 1u8 ; 32 ] , 10 , 1 ) ;
280+
281+ let hash_chain_state = HashChainState {
282+ offsets : vec ! [ 5 , 10 ] ,
283+ hash_chains : vec ! [ chain1, chain2] ,
284+ } ;
285+
286+ let result1 = hash_chain_state. reveal ( 8 ) ?;
287+ let expected1 = hash_chain_state. hash_chains [ 0 ] . reveal_ith ( 3 ) ?;
288+ assert_eq ! ( result1, expected1) ;
289+
290+ // returns the first offset that is > sequence_number)
291+ let result2 = hash_chain_state. reveal ( 12 ) ?;
292+ let expected2 = hash_chain_state. hash_chains [ 1 ] . reveal_ith ( 2 ) ?;
293+ assert_eq ! ( result2, expected2) ;
294+
295+ Ok ( ( ) )
296+ }
205297}
0 commit comments