Skip to content

refactor: replace examples with focused rustdoc examples#2006

Open
kwsantiago wants to merge 1 commit intobitcoindevkit:masterfrom
kwsantiago:kwsantiago/1973-simplify-examples
Open

refactor: replace examples with focused rustdoc examples#2006
kwsantiago wants to merge 1 commit intobitcoindevkit:masterfrom
kwsantiago:kwsantiago/1973-simplify-examples

Conversation

@kwsantiago
Copy link
Copy Markdown

Closes #1973

Changes

  • Removed /examples folder containing CLI-heavy examples
  • Added focused rustdoc examples to key functions:
    • IndexedTxGraph::new() - graph initialization
    • BdkElectrumClient::new() - client creation
    • BdkElectrumClient::sync() - blockchain sync
    • BdkElectrumClient::full_scan() - wallet restoration
    • TxGraph::insert_tx() - transaction insertion
    • TxGraph::balance() - balance calculation
    • TxGraph::filter_chain_unspents() - UTXO retrieval
    • KeychainTxOutIndex::reveal_next_spk() - address generation
    • KeychainTxOutIndex::insert_descriptor() - descriptor setup
    • IndexedTxGraph::apply_block_relevant() - block processing
    • EsploraExt::full_scan() - Esplora wallet scanning
  • Updated Cargo.toml workspace members

Rationale

The previous examples contained 300+ lines of CLI boilerplate that obscured the core BDK functionality. The new rustdoc examples are 10-15 lines each and focus purely on API usage, making them much easier for developers to understand and follow.

The maintained bdk-cli tool serves as the comprehensive CLI example.

@kwsantiago kwsantiago mentioned this pull request Aug 1, 2025
@oleonardolima oleonardolima added the documentation Improvements or additions to documentation label Aug 4, 2025
@oleonardolima oleonardolima moved this to In Progress in BDK Chain Aug 4, 2025
@oleonardolima
Copy link
Copy Markdown
Collaborator

Thanks for working on this one.
It's failing on CI. Make sure to use the 'just' commands and try building the docs locally if that helps with debugging the failures.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from bcf49f0 to 7448a99 Compare August 5, 2025 00:35
@kwsantiago
Copy link
Copy Markdown
Author

@oleonardolima thank you. CI "should" be fixed now, at least it is locally. Can we try again?

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch 2 times, most recently from 0e68ca5 to 9a9f9af Compare August 5, 2025 01:27
@oleonardolima
Copy link
Copy Markdown
Collaborator

Thanks, there are some unrelated already merged commits, you probably need to do a rebase on top of master. Also, please note that we follow the conventional commits for commit messages, which would also need an update.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 9a9f9af to dc4e009 Compare August 6, 2025 18:28
@kwsantiago
Copy link
Copy Markdown
Author

@oleonardolima ok should be ready to test CI now. Thanks!

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch 2 times, most recently from 1f81e2c to fa85989 Compare August 13, 2025 18:17
@oleonardolima oleonardolima added this to the Wallet 2.2.0 milestone Aug 22, 2025
@oleonardolima
Copy link
Copy Markdown
Collaborator

You'll need to perform another rebase to incorporate the CI updates/fixes, and remove the unrelated merge commit.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from fa85989 to 6fb8acd Compare August 22, 2025 19:45
@kwsantiago
Copy link
Copy Markdown
Author

@oleonardolima should be good now, let me know if there is anything else here.

Copy link
Copy Markdown
Collaborator

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also remove the example exclusion in build and test CI step.

I'm also wondering if the cfgs are strictly required, but haven't tested anything different yet.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 86defb3 to 92ba283 Compare August 29, 2025 14:10
@kwsantiago kwsantiago changed the title Remove examples folder, add rustdoc examples refactor: replace examples with focused rustdoc examples Aug 29, 2025
@notmandatory
Copy link
Copy Markdown
Member

cACK, but haven't done a close review yet. Thanks for hanging in on this one, but looks like it needs another rebase.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch 2 times, most recently from 7c98357 to 8d6a3c6 Compare October 12, 2025 03:45
@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 7116530 to fe792bf Compare February 19, 2026 19:32
@kwsantiago kwsantiago requested a review from nymius February 19, 2026 19:33
@luisschwab
Copy link
Copy Markdown
Member

@kwsantiago you should run just pre-push to catch and correct any errors before pushing.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from fe792bf to 9b01fda Compare February 19, 2026 20:31
@kwsantiago
Copy link
Copy Markdown
Author

@luisschwab got it, thanks!

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 9b01fda to ca0883f Compare February 19, 2026 22:54
@kwsantiago
Copy link
Copy Markdown
Author

kwsantiago commented Feb 19, 2026

Code Coverage failure is unrelated to this PR: test_chain_update_wrong_network_error fails with ConnectionRefused on the local esplora server. Same failure on master: https://github.com/bitcoindevkit/bdk/actions/runs/22196832097

@notmandatory
Copy link
Copy Markdown
Member

This will need a rebase also since I merged #2123.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from ca0883f to f9ec921 Compare February 20, 2026 21:05
@kwsantiago
Copy link
Copy Markdown
Author

kwsantiago commented Feb 20, 2026

@notmandatory I rebased onto master in f9ec921

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from f9ec921 to fbfe3b5 Compare February 25, 2026 20:54
Copy link
Copy Markdown
Collaborator

@ValuedMammal ValuedMammal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can you change ignore to no_run in bdk_electrum_client.rs, that way we know the example compiles.
  • You can remove the #[allow(deprecated)] from fetch_latest_blocks. Thanks @kwsantiago

@ValuedMammal
Copy link
Copy Markdown
Collaborator

You may need to enable the crate default features for dev-dependencies in electrum/Cargo.toml in order for the doctests to compile.

@ValuedMammal
Copy link
Copy Markdown
Collaborator

In cont_integration.yml we no longer need to --exclude 'example_*' for the build-test job.

@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 012c809 to 35d6980 Compare March 9, 2026 23:19
@kwsantiago
Copy link
Copy Markdown
Author

@ValuedMammal your comments have been addressed in 35d6980

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.
@kwsantiago kwsantiago force-pushed the kwsantiago/1973-simplify-examples branch from 35d6980 to b2a9647 Compare March 9, 2026 23:24
Copy link
Copy Markdown
Contributor

@nymius nymius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is in a very good state, but It's hard to extract from the original issue the requirements to consider this done.

The value of the examples/ was mainly due to the integration of the multiple components bdk implements, and that's hard to replicate by just adding doc examples to the interesting methods.
That's why I think there is no much escape to having to look at bdk-cli for it.

However, there are components missing examples in the docs that will benefit from these changes.

@kwsantiago has already implemented some. The following list is an attempt to extract the provided functionality by the examples/ and identify the missing doc examples:

  • Esplora new: Blocking/AsyncClient::from_builder()

Missing example.

  • Esplora sync: EsploraAsync/EsploraExt::sync()

Missing example.

  • Esplora full scan: EsploraAsync/EsploraExt::full_scan()
  • Electrum new: BdkElectrumClient::new()
  • Electrum sync: BdkElectrumClient::sync()
  • Electrum full scan: BdkElectrumClient::full_scan()
  • Electrum parameter: batch_size.
  • bitcoind_rpc sync/live: Emitter::next_block() and Emitter::mempool

Missing example.

  • TxOut listing: TxGraph::try_canonical_view + filter_outpoints (filter_outpointsalready contains examples)

Missing example.

  • Balance TxGraph::balance()
  • Index setup: KeychainTxOutIndex::insert_descriptor()
  • Address listing: KeychainTxOutIndex::revealed_keychain_spks()
  • Address reveal next: KeychainTxOutIndex::reveal_next_spk()
  • Address reveal unused: KeychainTxOutIndex::next_unused_spk()

Once we have these in place, the added examples together with the bdk-cli project are going to be a better guide than the old examples/.

I left outside of the above list the functionality that cannot be replicated without recreating end to end examples like before.

  • Wallet setup: generate keys, initialize indexes and local chain, set network and database. Trying to replicate this in some way will fall back again to the old examples/ way. It's better to point users to bdk-cli instead.
  • PSBT operations: new, sign and extract: PSBT creation is mainly managed through TxBuilder in bdk_wallet, sign is on its way to be removed and extraction is also done by bdk_wallet. For more fine grained control is better to look at bdk_tx.
  • Coin selection and transaction building: Same than above, most of this is implemented with TxBuilder in bdk_wallet, and for the new interface is better to work on bdk-tx.

/// use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
/// # use std::str::FromStr;
///
/// let mut index = KeychainTxOutIndex::<&str>::new(10, true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to hear other's opinions, but wouldn't it be better to replace all these uses of raw integers as parameters by the name of the parameter itself, as we are talking of documentation?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, having the variable named after the parameter would be more readable than an unexplained value.

@oleonardolima
Copy link
Copy Markdown
Collaborator

I think this is in a very good state, but It's hard to extract from the original issue the requirements to consider this done.

The value of the examples/ was mainly due to the integration of the multiple components bdk implements, and that's hard to replicate by just adding doc examples to the interesting methods. That's why I think there is no much escape to having to look at bdk-cli for it.

[...]

@nymius Thanks for the detailed comment and review! If I may add to it, I would suggest to break this into different PRs if possible, e.g one for bdk_esplora; one for bdk_electrum, and so on.

I left outside of the above list the functionality that cannot be replicated without recreating end to end examples like before.

  • Wallet setup: generate keys, initialize indexes and local chain, set network and database. Trying to replicate this in some way will fall back again to the old examples/ way. It's better to point users to bdk-cli instead.
  • PSBT operations: new, sign and extract: PSBT creation is mainly managed through TxBuilder in bdk_wallet, sign is on its way to be removed and extraction is also done by bdk_wallet. For more fine grained control is better to look at bdk_tx.
  • Coin selection and transaction building: Same than above, most of this is implemented with TxBuilder in bdk_wallet, and for the new interface is better to work on bdk-tx.

Yes, it's fine to left this on the side for now, they're bdk_wallet concerns. And I even feel that some of the examples here and there are some sort of duplicates (needs double-checking).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

Status: Needs Review

Development

Successfully merging this pull request may close these issues.

Simplify /examples

6 participants