Skip to content

Commit b220d61

Browse files
committed
Merge #1652: refactor(chain)!: remove inner method from KeychainTxOutIndex
8494c12 refactor(chain)!: remove `inner` method from KeychainTxOutIndex (valued mammal) Pull request description: Fix #1353 by removing the `inner` method from `keychain_txout` module. See commit message for details. ### Changelog notice - `bdk_chain`: Removed method `KeychainTxOutIndex::inner` ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing * [x] This pull request breaks the existing API * [x] I'm linking the issue being fixed by this PR ACKs for top commit: oleonardolima: ACK 8494c12 notmandatory: ACK 8494c12 Tree-SHA512: f0f6864faa16ffcd49f63c7a38cba11f28ad8f7c7aecc8e7538b7d5862a51d43ee75bea81561236ba7876b5e3d6357a021d17212295c5c9ff9a5525aea586953
2 parents 28c8074 + 8494c12 commit b220d61

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,6 @@ impl<K> KeychainTxOutIndex<K> {
204204

205205
/// Methods that are *re-exposed* from the internal [`SpkTxOutIndex`].
206206
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
207-
/// Return a reference to the internal [`SpkTxOutIndex`].
208-
///
209-
/// **WARNING**: The internal index will contain lookahead spks. Refer to
210-
/// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
211-
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
212-
&self.inner
213-
}
214-
215207
/// Get the set of indexed outpoints, corresponding to tracked keychains.
216208
pub fn outpoints(&self) -> &BTreeSet<KeychainIndexed<K, OutPoint>> {
217209
self.inner.outpoints()

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ fn test_set_all_derivation_indices() {
129129
fn 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

Comments
 (0)