Skip to content

Commit 83e7b7e

Browse files
committed
docs(chain): improve KeychainTxOutIndex docs
More clarity for revealed/lookahead spks and give usecases.
1 parent 9294e30 commit 83e7b7e

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,46 @@ use crate::Append;
1212

1313
const DEFAULT_LOOKAHEAD: u32 = 1_000;
1414

15-
/// A convenient wrapper around [`SpkTxOutIndex`] that relates script pubkeys to miniscript public
16-
/// [`Descriptor`]s.
15+
/// [`KeychainTxOutIndex`] controls how script pubkeys are revealed for multiple keychains, and
16+
/// indexes [`TxOut`]s with them.
1717
///
18-
/// Descriptors are referenced by the provided keychain generic (`K`).
18+
/// A single keychain is a chain of script pubkeys derived from a single [`Descriptor`]. Keychains
19+
/// are identified using the `K` generic. Script pubkeys are identified by the keychain that they
20+
/// are derived from `K`, as well as the derivation index `u32`.
1921
///
20-
/// Script pubkeys for a descriptor are revealed chronologically from index 0. I.e., If the last
21-
/// revealed index of a descriptor is 5; scripts of indices 0 to 4 are guaranteed to be already
22-
/// revealed. In addition to revealed scripts, we have a `lookahead` parameter for each keychain,
23-
/// which defines the number of script pubkeys to store ahead of the last revealed index.
22+
/// # Revealed script pubkeys
2423
///
25-
/// Methods that could update the last revealed index will return [`super::ChangeSet`] to report
24+
/// Tracking how script pubkeys are revealed is useful for collecting chain data. For example, if
25+
/// the user has requested 5 script pubkeys (to receive money with), we only need to use those
26+
/// script pubkeys to scan for chain data.
27+
///
28+
/// Call [`reveal_to_target`] or [`reveal_next_spk`] to reveal more script pubkeys.
29+
/// Call [`revealed_keychain_spks`] or [`revealed_spks`] to iterate through revealed script pubkeys.
30+
///
31+
/// # Lookahead script pubkeys
32+
///
33+
/// When an user first recovers a wallet (i.e. from a recovery phrase and/or descriptor), we will
34+
/// NOT have knowledge of which script pubkeys are revealed. So when we index a transaction or
35+
/// txout (using [`index_tx`]/[`index_txout`]) we scan the txouts against script pubkeys derived
36+
/// above the last revealed index. These additionally-derived script pubkeys are called the
37+
/// lookahead.
38+
///
39+
/// The [`KeychainTxOutIndex`] is constructed with the `lookahead` and cannot be altered. The
40+
/// default `lookahead` count is 1000. Use [`new`] to set a custom `lookahead`.
41+
///
42+
/// # Unbounded script pubkey iterator
43+
///
44+
/// For script-pubkey-based chain sources (such as Electrum/Esplora), an initial scan is best done
45+
/// by iterating though derived script pubkeys one by one and requesting transaction histories for
46+
/// each script pubkey. We will stop after x-number of script pubkeys have empty histories. An
47+
/// unbounded script pubkey iterator is useful to pass to such a chain source.
48+
///
49+
/// Call [`unbounded_spk_iter`] to get an unbounded script pubkey iterator for a given keychain.
50+
/// Call [`all_unbounded_spk_iters`] to get unbounded script pubkey iterators for all keychains.
51+
///
52+
/// # Change sets
53+
///
54+
/// Methods that can update the last revealed index will return [`super::ChangeSet`] to report
2655
/// these changes. This can be persisted for future recovery.
2756
///
2857
/// ## Synopsis
@@ -58,6 +87,15 @@ const DEFAULT_LOOKAHEAD: u32 = 1_000;
5887
/// [`Ord`]: core::cmp::Ord
5988
/// [`SpkTxOutIndex`]: crate::spk_txout_index::SpkTxOutIndex
6089
/// [`Descriptor`]: crate::miniscript::Descriptor
90+
/// [`reveal_to_target`]: KeychainTxOutIndex::reveal_to_target
91+
/// [`reveal_next_spk`]: KeychainTxOutIndex::reveal_next_spk
92+
/// [`revealed_keychain_spks`]: KeychainTxOutIndex::revealed_keychain_spks
93+
/// [`revealed_spks`]: KeychainTxOutIndex::revealed_spks
94+
/// [`index_tx`]: KeychainTxOutIndex::index_tx
95+
/// [`index_txout`]: KeychainTxOutIndex::index_txout
96+
/// [`new`]: KeychainTxOutIndex::new
97+
/// [`unbounded_spk_iter`]: KeychainTxOutIndex::unbounded_spk_iter
98+
/// [`all_unbounded_spk_iters`]: KeychainTxOutIndex::all_unbounded_spk_iters
6199
#[derive(Clone, Debug)]
62100
pub struct KeychainTxOutIndex<K> {
63101
inner: SpkTxOutIndex<(K, u32)>,
@@ -115,6 +153,8 @@ impl<K> KeychainTxOutIndex<K> {
115153
/// beyond the last revealed index. In certain situations, such as when performing an initial
116154
/// scan of the blockchain during wallet import, it may be uncertain or unknown what the index
117155
/// of the last revealed script pubkey actually is.
156+
///
157+
/// Refer to [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
118158
pub fn new(lookahead: u32) -> Self {
119159
Self {
120160
inner: SpkTxOutIndex::default(),
@@ -129,7 +169,8 @@ impl<K> KeychainTxOutIndex<K> {
129169
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
130170
/// Return a reference to the internal [`SpkTxOutIndex`].
131171
///
132-
/// **WARNING:** The internal index will contain lookahead spks.
172+
/// **WARNING:** The internal index will contain lookahead spks. Refer to
173+
/// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
133174
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
134175
&self.inner
135176
}

0 commit comments

Comments
 (0)