Skip to content

Commit 14c99e2

Browse files
committed
refactor: replace examples with focused rustdoc examples
Remove standalone examples in favor of focused, contextual rustdoc examples that are easier to maintain and provide better documentation. This makes the codebase more maintainable while improving the developer experience with examples that are tested alongside the code they document.
1 parent 8149e1b commit 14c99e2

File tree

16 files changed

+145
-2104
lines changed

16 files changed

+145
-2104
lines changed

.github/workflows/cont_integration.yml

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ jobs:
6565
MATRIX_RUST_VERSION: ${{ matrix.rust.version }}
6666
run: |
6767
if [ $MATRIX_RUST_VERSION = '1.63.0' ]; then
68-
cargo build --workspace --exclude 'example_*' --exclude 'bdk_electrum' ${{ matrix.features }}
69-
cargo test --workspace --exclude 'example_*' --exclude 'bdk_electrum' ${{ matrix.features }}
68+
cargo build --workspace --exclude 'bdk_electrum' ${{ matrix.features }}
69+
cargo test --workspace --exclude 'bdk_electrum' ${{ matrix.features }}
7070
else
71-
cargo build --workspace --exclude 'example_*' ${{ matrix.features }}
72-
cargo test --workspace --exclude 'example_*' ${{ matrix.features }}
71+
cargo build --workspace ${{ matrix.features }}
72+
cargo test --workspace ${{ matrix.features }}
7373
fi
7474
7575
check-no-std:
@@ -169,30 +169,3 @@ jobs:
169169
name: Clippy Results
170170
args: --all-features --all-targets -- -D warnings
171171

172-
build-examples:
173-
needs: prepare
174-
name: Build & Test Examples
175-
runs-on: ubuntu-latest
176-
strategy:
177-
matrix:
178-
example-dir:
179-
- example_cli
180-
- example_bitcoind_rpc_polling
181-
- example_electrum
182-
- example_esplora
183-
steps:
184-
- name: checkout
185-
uses: actions/checkout@v4
186-
with:
187-
persist-credentials: false
188-
- name: Install Rust toolchain
189-
uses: actions-rs/toolchain@v1
190-
with:
191-
toolchain: ${{ needs.prepare.outputs.rust_version }}
192-
override: true
193-
profile: minimal
194-
- name: Rust Cache
195-
uses: Swatinem/rust-cache@v2.7.8
196-
- name: Build
197-
working-directory: examples/${{ matrix.example-dir }}
198-
run: cargo build

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ members = [
88
"crates/esplora",
99
"crates/bitcoind_rpc",
1010
"crates/testenv",
11-
"examples/example_cli",
12-
"examples/example_electrum",
13-
"examples/example_esplora",
14-
"examples/example_bitcoind_rpc_polling",
1511
]
1612

1713
[workspace.package]

crates/chain/src/indexed_tx_graph.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ where
8383
///
8484
/// The underlying `TxGraph` is initialized with `TxGraph::default()`, and the provided
8585
/// `index`er is used as‐is (since there are no existing transactions to process).
86+
///
87+
/// # Example
88+
///
89+
/// ```
90+
/// use bdk_chain::{keychain_txout::KeychainTxOutIndex, BlockId, IndexedTxGraph};
91+
///
92+
/// let index = KeychainTxOutIndex::<&str>::new(10, true);
93+
/// let graph = IndexedTxGraph::<BlockId, _>::new(index);
94+
/// ```
8695
pub fn new(index: I) -> Self {
8796
Self {
8897
index,
@@ -363,6 +372,21 @@ where
363372
/// Relevancy is determined by the internal [`Indexer::is_tx_relevant`] implementation of `I`.
364373
/// A transaction that conflicts with a relevant transaction is also considered relevant.
365374
/// Irrelevant transactions in `block` will be ignored.
375+
<<<<<<< HEAD
376+
=======
377+
///
378+
/// # Example
379+
///
380+
/// ```no_run
381+
/// use bdk_chain::{IndexedTxGraph, keychain_txout::KeychainTxOutIndex, BlockId};
382+
/// use bitcoin::Block;
383+
///
384+
/// let mut graph = IndexedTxGraph::<BlockId, _>::new(KeychainTxOutIndex::<&str>::new(10, true));
385+
/// # let block = Block { header: bitcoin::block::Header::from(bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).header), txdata: vec![] };
386+
///
387+
/// let changeset = graph.apply_block_relevant(&block, 100);
388+
/// ```
389+
>>>>>>> 8d6a3c64 (refactor: replace examples with focused rustdoc examples)
366390
pub fn apply_block_relevant(
367391
&mut self,
368392
block: &Block,

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,22 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
456456
/// (one keychain just becomes the defacto owner of that spk arbitrarily) but this may have
457457
/// subtle implications up the application stack like one UTXO being missing from one keychain
458458
/// because it has been assigned to another which produces the same script pubkey.
459+
///
460+
/// # Example
461+
///
462+
/// ```
463+
/// use bdk_chain::keychain_txout::KeychainTxOutIndex;
464+
/// use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
465+
/// # use std::str::FromStr;
466+
///
467+
/// let mut index = KeychainTxOutIndex::<&str>::new(10, true);
468+
/// let desc = Descriptor::<DescriptorPublicKey>::from_str(
469+
/// "wpkh([d34db33f/84h/0h/0h]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0/*)"
470+
/// )?;
471+
///
472+
/// index.insert_descriptor("external", desc)?;
473+
/// # Ok::<_, Box<dyn std::error::Error>>(())
474+
/// ```
459475
pub fn insert_descriptor(
460476
&mut self,
461477
keychain: K,
@@ -837,6 +853,22 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
837853
/// 1. The descriptor has no wildcard and already has one script revealed.
838854
/// 2. The descriptor has already revealed scripts up to the numeric bound.
839855
/// 3. There is no descriptor associated with the given keychain.
856+
///
857+
/// # Example
858+
///
859+
/// ```
860+
/// use bdk_chain::keychain_txout::KeychainTxOutIndex;
861+
/// use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
862+
/// # use std::str::FromStr;
863+
///
864+
/// let mut index = KeychainTxOutIndex::<&str>::new(10, true);
865+
/// let desc = Descriptor::<DescriptorPublicKey>::from_str(
866+
/// "wpkh([d34db33f/84h/0h/0h]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0/*)"
867+
/// ).unwrap();
868+
/// index.insert_descriptor("external", desc).unwrap();
869+
/// let (spk, changeset) = index.reveal_next_spk("external").unwrap();
870+
/// assert_eq!(spk.0, 0);
871+
/// ```
840872
pub fn reveal_next_spk(&mut self, keychain: K) -> Option<(Indexed<ScriptBuf>, ChangeSet)> {
841873
let mut changeset = ChangeSet::default();
842874
let indexed_spk = self._reveal_next_spk(&mut changeset, keychain)?;

crates/chain/src/tx_graph.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,24 @@ impl<A: Anchor> TxGraph<A> {
627627
/// * A smaller witness has precedence over a larger witness.
628628
/// * If the witness sizes are the same, we prioritize the two witnesses with lexicographical
629629
/// order.
630+
///
631+
/// # Example
632+
///
633+
/// ```
634+
/// use bdk_chain::{tx_graph::TxGraph, BlockId};
635+
/// use bitcoin::Transaction;
636+
///
637+
/// let mut graph = TxGraph::<BlockId>::default();
638+
/// let tx = Transaction {
639+
/// version: bitcoin::transaction::Version::ONE,
640+
/// lock_time: bitcoin::locktime::absolute::LockTime::ZERO,
641+
/// input: vec![],
642+
/// output: vec![],
643+
/// };
644+
///
645+
/// let changeset = graph.insert_tx(tx.clone());
646+
/// assert_eq!(changeset.txs.len(), 1);
647+
/// ```
630648
pub fn insert_tx<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> ChangeSet<A> {
631649
// This returns `Some` only if the merged tx is different to the `original_tx`.
632650
fn _merge_tx_witnesses(

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ pub struct BdkElectrumClient<E> {
2929

3030
impl<E: ElectrumApi> BdkElectrumClient<E> {
3131
/// Creates a new bdk client from a [`electrum_client::ElectrumApi`]
32+
///
33+
/// # Example
34+
/// ```no_run
35+
/// use bdk_electrum::{electrum_client, BdkElectrumClient};
36+
///
37+
/// let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
38+
/// let bdk_client = BdkElectrumClient::new(client);
39+
/// # Ok::<_, electrum_client::Error>(())
40+
/// ```
3241
pub fn new(client: E) -> Self {
3342
Self {
3443
inner: client,
@@ -107,6 +116,26 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
107116
/// [`CalculateFeeError::MissingTxOut`] error if those `TxOut`s are not present in the
108117
/// transaction graph.
109118
///
119+
/// # Example
120+
/// ```no_run
121+
/// use bdk_core::{spk_client::FullScanRequest, BlockId, CheckPoint};
122+
/// use bdk_electrum::BdkElectrumClient;
123+
/// # use bdk_electrum::electrum_client;
124+
/// # use electrum_client::bitcoin::{constants, Network};
125+
///
126+
/// # let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
127+
/// # let bdk_client = BdkElectrumClient::new(client);
128+
/// let request = FullScanRequest::<&str>::builder()
129+
/// .chain_tip(CheckPoint::new(BlockId {
130+
/// height: 0,
131+
/// hash: constants::genesis_block(Network::Bitcoin).block_hash(),
132+
/// }))
133+
/// .build();
134+
///
135+
/// let response = bdk_client.full_scan(request, 10, 50, false)?;
136+
/// # Ok::<_, electrum_client::Error>(())
137+
/// ```
138+
///
110139
/// [`bdk_chain`]: ../bdk_chain/index.html
111140
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
112141
/// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
@@ -190,6 +219,23 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
190219
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
191220
/// may include scripts that have been used, use [`full_scan`] with the keychain.
192221
///
222+
/// # Example
223+
/// ```no_run
224+
/// use bdk_core::bitcoin::ScriptBuf;
225+
/// use bdk_core::spk_client::SyncRequest;
226+
/// use bdk_electrum::BdkElectrumClient;
227+
/// # use bdk_electrum::electrum_client;
228+
///
229+
/// # let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
230+
/// # let bdk_client = BdkElectrumClient::new(client);
231+
/// let request = SyncRequest::builder()
232+
/// .spks([ScriptBuf::new_op_return(&[0x00; 20])])
233+
/// .build();
234+
///
235+
/// let response = bdk_client.sync(request, 50, false)?;
236+
/// # Ok::<_, electrum_client::Error>(())
237+
/// ```
238+
///
193239
/// [`full_scan`]: Self::full_scan
194240
/// [`bdk_chain`]: ../bdk_chain/index.html
195241
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut

crates/esplora/src/blocking_ext.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ pub trait EsploraExt {
2626
/// `stop_gap` script pubkeys with no associated transactions. `parallel_requests` specifies
2727
/// the maximum number of HTTP requests to make in parallel.
2828
///
29+
/// # Example
30+
///
31+
/// ```no_run
32+
/// use bdk_core::bitcoin::{constants, Network};
33+
/// use bdk_core::spk_client::FullScanRequest;
34+
/// use bdk_core::{BlockId, CheckPoint};
35+
/// use bdk_esplora::{esplora_client, EsploraExt};
36+
///
37+
/// let client = esplora_client::Builder::new("https://blockstream.info/api").build_blocking();
38+
///
39+
/// let request = FullScanRequest::<&str>::builder()
40+
/// .chain_tip(CheckPoint::new(BlockId {
41+
/// height: 0,
42+
/// hash: constants::genesis_block(Network::Bitcoin).block_hash(),
43+
/// }))
44+
/// .build();
45+
///
46+
/// let response = client.full_scan(request, 10, 5)?;
47+
/// # Ok::<_, Box<dyn std::error::Error>>(())
48+
/// ```
49+
///
2950
/// Refer to [crate-level docs](crate) for more.
3051
fn full_scan<K: Ord + Clone, R: Into<FullScanRequest<K>>>(
3152
&self,

examples/example_bitcoind_rpc_polling/Cargo.toml

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/example_bitcoind_rpc_polling/README.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)