Skip to content

Commit 165f06b

Browse files
committed
chore(chain): misc docs and insert_descriptor fixes
1 parent 3f91af3 commit 165f06b

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ impl<K> Default for ChangeSet<K> {
9595
}
9696
}
9797

98-
const DEFAULT_LOOKAHEAD: u32 = 25;
98+
/// The default lookahead for a [`KeychainTxOutIndex`]
99+
pub const DEFAULT_LOOKAHEAD: u32 = 25;
99100

100101
/// [`KeychainTxOutIndex`] controls how script pubkeys are revealed for multiple keychains, and
101102
/// indexes [`TxOut`]s with them.
@@ -121,15 +122,17 @@ const DEFAULT_LOOKAHEAD: u32 = 25;
121122
/// above the last revealed index. These additionally-derived script pubkeys are called the
122123
/// lookahead.
123124
///
124-
/// The [`KeychainTxOutIndex`] is constructed with the `lookahead` and cannot be altered. The
125-
/// default `lookahead` count is 1000. Use [`new`] to set a custom `lookahead`.
125+
/// The [`KeychainTxOutIndex`] is constructed with the `lookahead` and cannot be altered. See
126+
/// [`DEFAULT_LOOKAHEAD`] for the value used in the `Default` implementation. Use [`new`] to set a
127+
/// custom `lookahead`.
126128
///
127129
/// # Unbounded script pubkey iterator
128130
///
129131
/// For script-pubkey-based chain sources (such as Electrum/Esplora), an initial scan is best done
130132
/// by iterating though derived script pubkeys one by one and requesting transaction histories for
131133
/// each script pubkey. We will stop after x-number of script pubkeys have empty histories. An
132-
/// unbounded script pubkey iterator is useful to pass to such a chain source.
134+
/// unbounded script pubkey iterator is useful to pass to such a chain source because it doesn't
135+
/// require holding a reference to the index.
133136
///
134137
/// Call [`unbounded_spk_iter`] to get an unbounded script pubkey iterator for a given keychain.
135138
/// Call [`all_unbounded_spk_iters`] to get unbounded script pubkey iterators for all keychains.
@@ -162,42 +165,14 @@ const DEFAULT_LOOKAHEAD: u32 = 25;
162165
/// # let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
163166
/// # let (internal_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap();
164167
/// # let (descriptor_42, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/2/*)").unwrap();
165-
/// let _ = txout_index.insert_descriptor(MyKeychain::External, external_descriptor);
166-
/// let _ = txout_index.insert_descriptor(MyKeychain::Internal, internal_descriptor);
167-
/// let _ = txout_index.insert_descriptor(MyKeychain::MyAppUser { user_id: 42 }, descriptor_42);
168+
/// let _ = txout_index.insert_descriptor(MyKeychain::External, external_descriptor)?;
169+
/// let _ = txout_index.insert_descriptor(MyKeychain::Internal, internal_descriptor)?;
170+
/// let _ = txout_index.insert_descriptor(MyKeychain::MyAppUser { user_id: 42 }, descriptor_42)?;
168171
///
169172
/// let new_spk_for_user = txout_index.reveal_next_spk(&MyKeychain::MyAppUser{ user_id: 42 });
173+
/// # Ok::<_, bdk_chain::keychain::InsertDescriptorError<_>>(())
170174
/// ```
171175
///
172-
/// # Non-recommend keychain to descriptor assignments
173-
///
174-
/// A keychain (`K`) is used to identify a descriptor. However, the following keychain to descriptor
175-
/// arrangements result in behavior that is harder to reason about and is not recommended.
176-
///
177-
/// ## Multiple keychains identifying the same descriptor
178-
///
179-
/// Although a single keychain variant can only identify a single descriptor, multiple keychain
180-
/// variants can identify the same descriptor.
181-
///
182-
/// If multiple keychains identify the same descriptor:
183-
/// 1. Methods that take in a keychain (such as [`reveal_next_spk`]) will work normally when any
184-
/// keychain (that identifies that descriptor) is passed in.
185-
/// 2. Methods that return data which associates with a descriptor (such as [`outpoints`],
186-
/// [`txouts`], [`unused_spks`], etc.) the method will return the highest-ranked keychain variant
187-
/// that identifies the descriptor. Rank is determined by the [`Ord`] implementation of the keychain
188-
/// type.
189-
///
190-
/// This arrangement is not recommended since some methods will return a single keychain variant
191-
/// even though multiple keychain variants identify the same descriptor.
192-
///
193-
/// ## Reassigning the descriptor of a single keychain
194-
///
195-
/// Descriptors added to [`KeychainTxOutIndex`] are never removed. However, a keychain that
196-
/// identifies a descriptor can be reassigned to identify a different descriptor. This may result in
197-
/// a situation where a descriptor has no associated keychain(s), and relevant [`TxOut`]s,
198-
/// [`OutPoint`]s and [`Script`]s (of that descriptor) will not be return by [`KeychainTxOutIndex`].
199-
/// Therefore, reassigning the descriptor of a single keychain is not recommended.
200-
///
201176
/// [`Ord`]: core::cmp::Ord
202177
/// [`SpkTxOutIndex`]: crate::spk_txout_index::SpkTxOutIndex
203178
/// [`Descriptor`]: crate::miniscript::Descriptor

crates/chain/src/spk_iter.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,12 @@ mod test {
158158
let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
159159
let (internal_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap();
160160

161-
let _ = txout_index.insert_descriptor(TestKeychain::External, external_descriptor.clone());
162-
let _ = txout_index.insert_descriptor(TestKeychain::Internal, internal_descriptor.clone());
161+
let _ = txout_index
162+
.insert_descriptor(TestKeychain::External, external_descriptor.clone())
163+
.unwrap();
164+
let _ = txout_index
165+
.insert_descriptor(TestKeychain::Internal, internal_descriptor.clone())
166+
.unwrap();
163167

164168
(txout_index, external_descriptor, internal_descriptor)
165169
}

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ fn insert_relevant_txs() {
3434
let mut graph = IndexedTxGraph::<ConfirmationHeightAnchor, KeychainTxOutIndex<()>>::new(
3535
KeychainTxOutIndex::new(10),
3636
);
37-
let _ = graph.index.insert_descriptor((), descriptor.clone());
37+
let _ = graph
38+
.index
39+
.insert_descriptor((), descriptor.clone())
40+
.unwrap();
3841

3942
let tx_a = Transaction {
4043
output: vec![

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ fn init_txout_index(
3434
) -> bdk_chain::keychain::KeychainTxOutIndex<TestKeychain> {
3535
let mut txout_index = bdk_chain::keychain::KeychainTxOutIndex::<TestKeychain>::new(lookahead);
3636

37-
let _ = txout_index.insert_descriptor(TestKeychain::External, external_descriptor);
38-
let _ = txout_index.insert_descriptor(TestKeychain::Internal, internal_descriptor);
37+
let _ = txout_index
38+
.insert_descriptor(TestKeychain::External, external_descriptor)
39+
.unwrap();
40+
let _ = txout_index
41+
.insert_descriptor(TestKeychain::Internal, internal_descriptor)
42+
.unwrap();
3943

4044
txout_index
4145
}
@@ -458,7 +462,9 @@ fn test_non_wildcard_derivations() {
458462
.unwrap()
459463
.script_pubkey();
460464

461-
let _ = txout_index.insert_descriptor(TestKeychain::External, no_wildcard_descriptor.clone());
465+
let _ = txout_index
466+
.insert_descriptor(TestKeychain::External, no_wildcard_descriptor.clone())
467+
.unwrap();
462468

463469
// given:
464470
// - `txout_index` with no stored scripts
@@ -702,7 +708,9 @@ fn applying_changesets_one_by_one_vs_aggregate_must_have_same_result() {
702708
fn assigning_same_descriptor_to_multiple_keychains_should_error() {
703709
let desc = parse_descriptor(DESCRIPTORS[0]);
704710
let mut indexer = KeychainTxOutIndex::<TestKeychain>::new(0);
705-
let _ = indexer.insert_descriptor(TestKeychain::Internal, desc.clone());
711+
let _ = indexer
712+
.insert_descriptor(TestKeychain::Internal, desc.clone())
713+
.unwrap();
706714
assert!(indexer
707715
.insert_descriptor(TestKeychain::External, desc)
708716
.is_err())
@@ -726,7 +734,7 @@ fn when_querying_over_a_range_of_keychains_the_utxos_should_show_up() {
726734

727735
for (i, descriptor) in DESCRIPTORS.iter().enumerate() {
728736
let descriptor = parse_descriptor(descriptor);
729-
let _ = indexer.insert_descriptor(i, descriptor.clone());
737+
let _ = indexer.insert_descriptor(i, descriptor.clone()).unwrap();
730738
if i != 4 {
731739
// skip one in the middle to see if uncovers any bugs
732740
indexer.reveal_next_spk(&i);

example-crates/example_cli/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ where
709709
// them in the index here. However, the keymap is not stored in the database.
710710
let (descriptor, mut keymap) =
711711
Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, &args.descriptor)?;
712-
let _ = index.insert_descriptor(Keychain::External, descriptor);
712+
let _ = index.insert_descriptor(Keychain::External, descriptor)?;
713713

714714
if let Some((internal_descriptor, internal_keymap)) = args
715715
.change_descriptor
@@ -718,7 +718,7 @@ where
718718
.transpose()?
719719
{
720720
keymap.extend(internal_keymap);
721-
let _ = index.insert_descriptor(Keychain::Internal, internal_descriptor);
721+
let _ = index.insert_descriptor(Keychain::Internal, internal_descriptor)?;
722722
}
723723

724724
let mut db_backend = match Store::<C>::open_or_create_new(db_magic, &args.db_path) {

0 commit comments

Comments
 (0)