@@ -129,8 +129,12 @@ fn test_set_all_derivation_indices() {
129129fn test_lookahead ( ) {
130130 let external_descriptor = parse_descriptor ( DESCRIPTORS [ 0 ] ) ;
131131 let internal_descriptor = parse_descriptor ( DESCRIPTORS [ 1 ] ) ;
132- let mut txout_index =
133- init_txout_index ( external_descriptor. clone ( ) , internal_descriptor. clone ( ) , 10 ) ;
132+ let lookahead = 10 ;
133+ let mut txout_index = init_txout_index (
134+ external_descriptor. clone ( ) ,
135+ internal_descriptor. clone ( ) ,
136+ lookahead,
137+ ) ;
134138
135139 // given:
136140 // - external lookahead set to 10
@@ -139,7 +143,7 @@ fn test_lookahead() {
139143 // expect:
140144 // - scripts cached in spk_txout_index should increase correctly
141145 // - stored scripts of external keychain should be of expected counts
142- for index in ( 0 ..20 ) . skip_while ( |i| i % 2 == 1 ) {
146+ for index in 0 ..20 {
143147 let ( revealed_spks, revealed_changeset) = txout_index
144148 . reveal_to_target ( TestKeychain :: External , index)
145149 . unwrap ( ) ;
@@ -152,12 +156,29 @@ fn test_lookahead() {
152156 & [ ( external_descriptor. descriptor_id( ) , index) ] . into( )
153157 ) ;
154158
155- assert_eq ! (
156- txout_index. inner( ) . all_spks( ) . len( ) ,
157- 10 /* external lookahead */ +
158- 10 /* internal lookahead */ +
159- index as usize + 1 /* `derived` count */
160- ) ;
159+ // test stored spks are expected
160+ let exp_last_store_index = index + lookahead;
161+ for i in index + 1 ..=exp_last_store_index {
162+ assert_eq ! (
163+ txout_index. spk_at_index( TestKeychain :: External , i) ,
164+ Some ( spk_at_index( & external_descriptor, i) )
165+ ) ;
166+ }
167+ assert ! ( txout_index
168+ . spk_at_index( TestKeychain :: External , exp_last_store_index + 1 )
169+ . is_none( ) ) ;
170+
171+ // internal should only have lookahead
172+ for i in 0 ..lookahead {
173+ assert_eq ! (
174+ txout_index. spk_at_index( TestKeychain :: Internal , i) ,
175+ Some ( spk_at_index( & internal_descriptor, i) )
176+ ) ;
177+ }
178+ assert ! ( txout_index
179+ . spk_at_index( TestKeychain :: Internal , lookahead)
180+ . is_none( ) ) ;
181+
161182 assert_eq ! (
162183 txout_index
163184 . revealed_keychain_spks( TestKeychain :: External )
@@ -191,26 +212,33 @@ fn test_lookahead() {
191212 // - derivation index is set ahead of current derivation index + lookahead
192213 // expect:
193214 // - scripts cached in spk_txout_index should increase correctly, a.k.a. no scripts are skipped
215+ let reveal_to = 24 ;
194216 let ( revealed_spks, revealed_changeset) = txout_index
195- . reveal_to_target ( TestKeychain :: Internal , 24 )
217+ . reveal_to_target ( TestKeychain :: Internal , reveal_to )
196218 . unwrap ( ) ;
197219 assert_eq ! (
198220 revealed_spks,
199- ( 0 ..=24 )
221+ ( 0 ..=reveal_to )
200222 . map( |index| ( index, spk_at_index( & internal_descriptor, index) ) )
201223 . collect:: <Vec <_>>( ) ,
202224 ) ;
203225 assert_eq ! (
204226 & revealed_changeset. last_revealed,
205- & [ ( internal_descriptor. descriptor_id( ) , 24 ) ] . into( )
206- ) ;
207- assert_eq ! (
208- txout_index. inner( ) . all_spks( ) . len( ) ,
209- 10 /* external lookahead */ +
210- 10 /* internal lookahead */ +
211- 20 /* external stored index count */ +
212- 25 /* internal stored index count */
227+ & [ ( internal_descriptor. descriptor_id( ) , reveal_to) ] . into( )
213228 ) ;
229+
230+ // test stored spks are expected
231+ let exp_last_store_index = reveal_to + lookahead;
232+ for index in reveal_to + 1 ..=exp_last_store_index {
233+ assert_eq ! (
234+ txout_index. spk_at_index( TestKeychain :: Internal , index) ,
235+ Some ( spk_at_index( & internal_descriptor, index) )
236+ ) ;
237+ }
238+ assert ! ( txout_index
239+ . spk_at_index( TestKeychain :: Internal , exp_last_store_index + 1 )
240+ . is_none( ) ) ;
241+
214242 assert_eq ! (
215243 txout_index
216244 . revealed_keychain_spks( TestKeychain :: Internal )
@@ -562,15 +590,10 @@ fn lookahead_to_target() {
562590 None => target,
563591 } ;
564592 index. lookahead_to_target ( keychain. clone ( ) , target) ;
565- let keys = index
566- . inner ( )
567- . all_spks ( )
568- . range ( ( keychain. clone ( ) , 0 ) ..=( keychain. clone ( ) , u32:: MAX ) )
569- . map ( |( k, _) | k. clone ( ) )
570- . collect :: < Vec < _ > > ( ) ;
571- let exp_keys = core:: iter:: repeat ( keychain)
572- . zip ( 0_u32 ..=exp_last_stored_index)
573- . collect :: < Vec < _ > > ( ) ;
593+ let keys: Vec < _ > = ( 0 ..)
594+ . take_while ( |& i| index. spk_at_index ( keychain. clone ( ) , i) . is_some ( ) )
595+ . collect ( ) ;
596+ let exp_keys: Vec < _ > = ( 0 ..=exp_last_stored_index) . collect ( ) ;
574597 assert_eq ! ( keys, exp_keys) ;
575598 }
576599 }
0 commit comments