@@ -129,8 +129,12 @@ fn test_set_all_derivation_indices() {
129
129
fn test_lookahead ( ) {
130
130
let external_descriptor = parse_descriptor ( DESCRIPTORS [ 0 ] ) ;
131
131
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
+ ) ;
134
138
135
139
// given:
136
140
// - external lookahead set to 10
@@ -139,7 +143,7 @@ fn test_lookahead() {
139
143
// expect:
140
144
// - scripts cached in spk_txout_index should increase correctly
141
145
// - 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 {
143
147
let ( revealed_spks, revealed_changeset) = txout_index
144
148
. reveal_to_target ( TestKeychain :: External , index)
145
149
. unwrap ( ) ;
@@ -152,12 +156,29 @@ fn test_lookahead() {
152
156
& [ ( external_descriptor. descriptor_id( ) , index) ] . into( )
153
157
) ;
154
158
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
+
161
182
assert_eq ! (
162
183
txout_index
163
184
. revealed_keychain_spks( TestKeychain :: External )
@@ -191,26 +212,33 @@ fn test_lookahead() {
191
212
// - derivation index is set ahead of current derivation index + lookahead
192
213
// expect:
193
214
// - scripts cached in spk_txout_index should increase correctly, a.k.a. no scripts are skipped
215
+ let reveal_to = 24 ;
194
216
let ( revealed_spks, revealed_changeset) = txout_index
195
- . reveal_to_target ( TestKeychain :: Internal , 24 )
217
+ . reveal_to_target ( TestKeychain :: Internal , reveal_to )
196
218
. unwrap ( ) ;
197
219
assert_eq ! (
198
220
revealed_spks,
199
- ( 0 ..=24 )
221
+ ( 0 ..=reveal_to )
200
222
. map( |index| ( index, spk_at_index( & internal_descriptor, index) ) )
201
223
. collect:: <Vec <_>>( ) ,
202
224
) ;
203
225
assert_eq ! (
204
226
& 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( )
213
228
) ;
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
+
214
242
assert_eq ! (
215
243
txout_index
216
244
. revealed_keychain_spks( TestKeychain :: Internal )
@@ -562,15 +590,10 @@ fn lookahead_to_target() {
562
590
None => target,
563
591
} ;
564
592
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 ( ) ;
574
597
assert_eq ! ( keys, exp_keys) ;
575
598
}
576
599
}
0 commit comments