72 Commits

Author SHA1 Message Date
Antoine Poinsot
4fd539b647
feat(chain)!: KeychainTxOutIndex uses a universal lookahead
The wallet is currently created without setting any lookahead value for
the keychain. This implicitly makes it a lookahead of 0. As this is a
high-level interface we should avoid footguns and aim for a reasonable
default.

Instead of simply patching it for wallet, we alter `KeychainTxOutIndex`
to have a default lookahead value. Additionally, instead of a
per-keychain lookahead, the constructor asks for a `lookahead` value.
This avoids the footguns of having methods which allows the caller the
decrease the `lookahead` (and therefore panicing). This also simplifies
the API.

Co-authored-by: Antoine Poisot <darosior@protonmail.com>
Co-authored-by: 志宇 <hello@evanlinjin.me>
2023-12-28 12:51:11 +08:00
Steve Myers
77f9977c02
feat(wallet): Add infallible Wallet get_address(), get_internal_address functions
refactor!(wallet)!: rename fallible Wallet try_get_address(), try_get_internal_address functions
2023-11-16 11:18:11 -06:00
Steve Myers
9e7d99e3bf
refactor(bdk)!: add context specific error types, remove top level error mod
refactor(bdk)!: remove impl_error macro
refactor(wallet)!: add MiniscriptPsbtError, CreateTxError, BuildFeeBumpError error enums
refactor(coin_selection)!: add module Error enum
test(bdk): use anyhow dev-dependency for all tests
2023-11-16 10:24:35 -06:00
Einherjar
27a63abd1e
chore: typos fixed 2023-11-16 07:25:20 -06:00
志宇
06a956ad20
feat!: change load_from_persistence to return an option
`PersistBackend::is_empty` is removed. Instead, `load_from_persistence`
returns an option of the changeset. `None` means persistence is empty.
This is a better API than a separate method. We can now differentiate
between a persisted single changeset and nothing persisted.

`Store::aggregate_changeset` is refactored to return a `Result` instead
of a tuple. A new error type (`AggregateChangesetsError`) is introduced
to include the partially-aggregated changeset in the error. This is a
more idiomatic API.
2023-11-16 07:07:49 +08:00
志宇
1886dc4fe7
chore(examples): use Wallet::new_or_load method where appropriate 2023-11-16 07:07:48 +08:00
志宇
24994a3ed4
feat(file_store)!: have separate methods for creating and opening Store 2023-11-16 07:07:48 +08:00
志宇
d294e2e318
feat(wallet)!: add new_or_load methods
These methods try to load wallet from persistence and initializes the
wallet instead if non-existant.

An internal helper method `create_signers` is added to reuse code.
Documentation is also improved.
2023-11-16 07:07:48 +08:00
志宇
6cf3963c6c
feat(bdk)!: have separate methods for creating and loading Wallet
`Wallet::new` now creates a new wallet. `Wallet::load` loads an existing
wallet. The network type is now recoverable from persistence. Error
types have been simplified.
2023-11-16 07:04:08 +08:00
志宇
5998a22819
feat!: LocalChain with hardwired genesis checkpoint
This ensures that `LocalChain` will always have a tip. The `ChainOracle`
trait's `get_chain_tip` method no longer needs to return an option.
2023-11-16 06:41:18 +08:00
Wei Chen
0112c67b60
chore: rename ConfirmationTimeAnchor to ConfirmationTimeHeightAnchor
An `Anchor` implementation that records both height and time should have
both attributes included in the name.
2023-11-12 21:31:47 +08:00
Steve Myers
38d69c947c
Merge bitcoindevkit/bdk#1041: Add bitcoind_rpc chain source module.
85c62532a55cfc94eade4d20ca3075dc4cd4882e docs(bitcoind_rpc): better `Emitter::mempool` explanation (志宇)
b69c13ddf6aa7cfb9be8c841b255e7f5f13ad328 example_bitcoind_rpc: tweaks (志宇)
5f34df8489fedae2aa3fd001036cc9ef6abe9a7a bitcoind_rpc!: bring back `CheckPoint`s to `Emitter` (志宇)
57590e0a1f2dad09a63fadb11f01e9f704cdcffb bitcoind_rpc: rm `BlockHash` from `Emitter::last_mempool_tip` (志宇)
6d4b33ef91a6c3e3443f6321cf3e3d186f77c595 chain: split `IndexedTxGraph::insert_tx` into 3 methods (志宇)
4f5695d43add3eab37ab12e897ac7c49f0d0787e chain: improvements to `IndexedTxGraph` and `TxGraph` APIs (志宇)
150d6f8ab6cd1eb1c9448d61e7bd71db0dd32a01 feat(example_bitcoind_rpc_polling): add example for RPC polling (志宇)
4f10463d9eaad9365b87dd99d49f0ddb8be673ee test(bitcoind_rpc): add no_agreement_point test (志宇)
a73dac2d91b29c4ba05f606f81e511fbf1f9ec7f test(bitcoind_rpc): initial tests for `Emitter` (志宇)
bb7424d11d3a4bd837ddde0f42f9abd93d56aee1 feat(bitcoind_rpc): introduce `bitcoind_rpc` crate (志宇)
240657b1674ad901c77090ee1fa96dc0d71e91d7 chain: add batch-insert methods for `IndexedTxGraph` (志宇)
43bc813c6498b5b021d70a6127ff7e57d7337813 chain: add helper methods on `CheckPoint` (志宇)
b3db5ca9df4302fddcd5474569849371f2be7e2d feat(chain): add `AnchorFromBlockPosition` trait (志宇)
f795a43cc72fdb4ef26ca349c4cb4f4bfd3b90b5 feat(example_cli): allow chain specific args in examples (志宇)

Pull request description:

  ### Description

  This PR builds on top of #1034 and adds the `bitcoind_rpc` chain-src module and example.

  ### Notes to the reviewers

  Don't merge this until #1034 is in!

  ### Changelog notice

  * Add `bitcoind_rpc` chain-source module.
  * Add `example_bitcoind_rpc` example module.
  * Add `AnchorFromBlockPosition` trait which are for anchors that can be constructed from a given block, height and position in block.
  * Add helper methods to `IndexedTxGraph` and `TxGraph` for batch operations and applying blocks directly.
  * Add helper methods to `CheckPoint` for easier construction from a block `Header`.

  ### Checklists

  * [x] Add test: we should detect when an initially-confirmed transaction is "unconfirmed" during a reorg.
  * [x] Improve `example_bitcoind_rpc`: add `live` command.
  * [x] Improve docs.
  * [x] Reintroduce `CheckPoint`.

  #### 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

  #### New Features:

  * [x] I've added tests for the new feature
  * [x] I've added docs for the new feature

ACKs for top commit:
  notmandatory:
    Re ACK 85c62532a55cfc94eade4d20ca3075dc4cd4882e

Tree-SHA512: 88dbafbebaf227b18c69f2ea884e3e586bf9c11e5e450eb4872ade1d1ccd5cf1e33ce9930a6f5aa918baa3e92add7503858b039b8c9d553a281ad6d833f08a49
2023-10-09 15:49:57 -05:00
志宇
b69c13ddf6
example_bitcoind_rpc: tweaks
* avoid holding mutex lock over io
* document `CHANNEL_BOUND` const
* use the `relevant` variant of `batch_insert_unconfirmed`
* print elapsed time in stdout for various updates
2023-10-10 01:04:30 +08:00
志宇
5f34df8489
bitcoind_rpc!: bring back CheckPoints to Emitter
* `bdk_chain` dependency is added. In the future, we will introduce a
  separate `bdk_core` crate to contain shared types.
* replace `Emitter::new` with `from_height` and `from_checkpoint`
  * `from_height` emits from the given start height
  * `from_checkpoint` uses the provided cp to find agreement point
* introduce logic that ensures emitted blocks can connect with
  receiver's `LocalChain`
* in our rpc example, we can now `expect()` chain updates to always
  since we are using checkpoints and receiving blocks in order
2023-10-09 22:14:04 +08:00
志宇
6d4b33ef91
chain: split IndexedTxGraph::insert_tx into 3 methods
Instead of inserting anchors and seen_at timestamp in the same method,
we have three separate methods. This makes the API easier to understand
and makes `IndexedTxGraph` more consistent with the `TxGraph` API.
2023-10-09 22:14:04 +08:00
志宇
4f5695d43a
chain: improvements to IndexedTxGraph and TxGraph APIs
For `IndexedTxGraph`:
- Remove `InsertTxItem` type (this is too complex).
    - `batch_insert_relevant` now uses a simple tuple `(&tx, anchors)`.
    - `batch_insert` is now also removed, as the same functionality can be
      done elsewhere.
- Add internal helper method `index_tx_graph_changeset` so we don't need
  to create a seprate `TxGraph` update in each method.
- `batch_insert_<relevant>_unconfirmed` no longer takes in an option of
  last_seen.
- `batch_insert_unconfirmed` no longer takes a reference of a
  transaction (since we apply all transactions anyway, so there is no
  need to clone).

For `TxGraph`:
- Add `batch_insert_unconfirmed` method.
2023-10-09 22:14:04 +08:00
志宇
150d6f8ab6
feat(example_bitcoind_rpc_polling): add example for RPC polling 2023-10-09 22:14:03 +08:00
志宇
f795a43cc7
feat(example_cli): allow chain specific args in examples
So you can pass in the esplora/electrum/bitcoind_rpc server details in
the example.

Co-authored-by: LLFourn <lloyd.fourn@gmail.com>
2023-10-09 22:14:02 +08:00
Daniela Brozzoni
b1461f05d0
fix(wallet_esplora): missing_heights uses the...
...graph update

Fixes #1151.
When wallet_esplora_* was used to sync a wallet containing a transaction
confirmed some time ago (more than 10-15 blocks ago), the transaction would
be stuck in an "unconfirmed" state forever.

At the first scan time, `update_local_chain` would just fetch the last 10 to
15 blocks (depending on the server used), and `tx_graph.missing_heights`
wouldn't return the tx's confirmation block as it was called on the
original, non-updated tx_graph.
So, after the first scan, we would have a transaction in memory with an
anchor that doesn't exist in our local_chain, and try_get_chain_position
would return unconfirmed.

When scanning again, missing_heights would find the missing anchor, but
`update_local_chain` wouldn't include it as it's older than
ASSUME_FINAL_DEPTH.

The missing block would be downloaded every time, but never included in
the local_chain, and the transaction would remain unconfirmed forever.

Here we call missing_heights on the updated graph, so that it can
correctly return the anchor height, and `update_local_chain` can
fetch it and include it in the chain.
2023-10-09 15:09:14 +02:00
志宇
c20a4da9fc
Merge bitcoindevkit/bdk#1084: Enhance bdk chain structures
1ff806c67f4da9ba58b7c7689fde0fe41a34a6f5 fix(chain)!: rm weird `From` impl (志宇)
d43ae0231fa4670b98780cad84466c14ae087292 refactor: improve docs, cleanup unnecessary types and improve code (Vladimir Fomene)
41042069809e3eeb4a8cc8a5a8db1af2c57c4a11 feat: impl Append for lots of tuples (LLFourn)
c56728ff1315e0deaf256af07fd1ff5e18fced8a refactor: Remove `scan` and `scan_txout` from SpkTxoutIndex and KeychainTxoutIndex (Vladimir Fomene)
32c40ac939bc514ac7f1d3f1d7cd1080011e20ba feat(electrum)!: change signature of `ElectrumExt` (志宇)
a28748c33976312b9e6671636ab7e305323efb03 refactor: Implement Default for WalletUpdate (Vladimir Fomene)
f42f8b8ff19c2e67888b476487e4e5c9edb0d0ff refactor: Allow for no chain update (Vladimir Fomene)
68572bfd2e32efdeefaa46618e8e248d3a87f143 refactor: move WalletChangeset to wallet module (Vladimir Fomene)
2392e50fd9793902d480556caa4ec225085c82d6 refactor: Move WalletUpdate to wallet module (Vladimir Fomene)
7c12dc994242bf2d7e35c2723f6e7000de97a388 refactor: Remove ForEachTxout trait (Vladimir Fomene)
6bcbb93233824ec391689191b8ca1f5459cec930 refactor: Edit ElectrumExt not to use WalletUpdate (Vladimir Fomene)

Pull request description:

  ### Description

  Fixes #1061

  ### Changelog notice

  - Move WalletUpdate to the wallet module
  - Remove ForEachTxout trait completely
  - Refactor ElectrumExt to not use WalletUpdate.

  ### 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

ACKs for top commit:
  evanlinjin:
    ACK 1ff806c67f4da9ba58b7c7689fde0fe41a34a6f5

Tree-SHA512: 05349713af9d2efa14a522ceaabb7513bb437d786adf2f93055765589a67e4eb68bda36ff415aeba07816c4d30988d4d55bac018e7697019270a219105ed65a2
2023-09-15 09:24:53 +08:00
志宇
1ff806c67f
fix(chain)!: rm weird From impl
And signature of `example_cli::KeychainChangeSet` is changed.
2023-09-14 20:16:00 +08:00
Vladimir Fomene
d43ae0231f
refactor: improve docs, cleanup unnecessary types and improve code 2023-09-13 13:57:58 +03:00
Vladimir Fomene
20900218ce
refactor: rename methods in EsploraExt and EsploraExtAsync 2023-09-12 16:38:49 +03:00
志宇
32c40ac939
feat(electrum)!: change signature of ElectrumExt
We remove `ElectrumUpdate` and return tuples instead for `ElectrumExt`
methods. We introduce the `IncompleteTxGraph` structure to specifically
hodl the incomplete `TxGraph`.

This change is motivated by @LLFourn's comment: 794bf37e63 (r1305432603)
2023-09-03 01:51:20 +08:00
Vladimir Fomene
a28748c339
refactor: Implement Default for WalletUpdate 2023-09-03 01:51:20 +08:00
Vladimir Fomene
f42f8b8ff1
refactor: Allow for no chain update 2023-09-03 01:51:20 +08:00
Vladimir Fomene
68572bfd2e
refactor: move WalletChangeset to wallet module
Consequently, remove the `WalletChangeset` dependency from
`example_electrum` and `example_esplora` examples.
2023-09-03 01:48:01 +08:00
Vladimir Fomene
2392e50fd9
refactor: Move WalletUpdate to wallet module 2023-09-03 00:54:23 +08:00
Vladimir Fomene
6bcbb93233
refactor: Edit ElectrumExt not to use WalletUpdate 2023-09-03 00:54:23 +08:00
LLFourn
f41cc1cb37
fix: s/index_tx_graph/indexed_tx_graph/g 2023-08-31 13:07:19 +02:00
志宇
da8cfd39e9
feat: add cli example for esplora
Co-authored-by: remix <remix7531@pm.me>
Co-authored-by: LLFourn <lloyd.fourn@gmail.com>
Co-authored-by: Daniela Brozzoni <danielabrozzoni@protonmail.com>
2023-08-31 13:07:18 +02:00
Steve Myers
b4c31cd5ba
feat(wallet)!: remove TransactionDetails from bdk::Wallet API
Added
- Wallet::sent_and_received function
- Wallet::calculate_fee and Wallet::calculate_fee_rate functions
- Wallet::error::CalculateFeeError

BREAKING CHANGES:

Removed
- TransactionDetails struct

Changed
- Wallet::get_tx now returns CanonicalTx instead of TransactionDetails
- TxBuilder::finish now returns only a PartiallySignedTransaction
2023-08-30 11:55:15 -05:00
Casey Bowman
21206fe773
fix: Correct the coin type in the derivation path for the three wallet examples
fix: Correct the coin type in the derivation path for the three wallet examples
2023-08-25 02:24:14 -05:00
Daniela Brozzoni
2263a58448
refactor: Rename wallet_esplora to...
...`wallet_esplora_blocking`
2023-08-23 16:02:44 +02:00
Daniela Brozzoni
ea50b6a932
refactor!(chain): Unify ChangeSet nomenclature
This commit renames:

- indexed_tx_graph::IndexedAdditions -> indexed_tx_graph::ChangeSet
- indexed_tx_graph::IndexedAdditions::graph_additions -> indexed_tx_graph::ChangeSet::graph
- indexed_tx_graph::IndexedAdditions::index_additions -> indexed_tx_graph::ChangeSet::indexer
- tx_graph::Additions -> tx_graph::ChangeSet
- keychain::DerivationAdditions -> keychain::ChangeSet
- CanonicalTx::node -> CanonicalTx::tx_node
- CanonicalTx::observed_as -> CanonicalTx::chain_position
- LocalChangeSet -> WalletChangeSet
- LocalChangeSet::chain_changeset -> WalletChangeSet::chain
- LocalChangeSet::indexed_additions -> WalletChangeSet::indexed_tx_graph
- LocalUpdate -> WalletUpdate

This commit also changes the visibility of TxGraph::determine_changeset
to be pub(crate), and the method accepts a TxGraph instead of &TxGraph

This commit removes:
- `TxGraph::insert_txout_preview`
- `TxGraph::insert_tx_preview`
- `insert_anchor_preview`
- `insert_seen_at_preview`

Solves #1022
2023-08-18 11:51:45 +02:00
Daniela Brozzoni
b73385dbd2
Update wallet_electrum to rust-bitcoin 0.30.0 2023-08-03 10:59:12 +02:00
Daniela Brozzoni
3dac3f9bba
Update example_electrum to rust-bitcoin 0.30.0 2023-08-03 10:59:11 +02:00
Daniela Brozzoni
2949bdc7b8
Update example_cli to rust-bitcoin 0.30.0 2023-08-03 10:59:10 +02:00
Daniela Brozzoni
6c29e53ee8
Update wallet_esplora and wallet_esplora_async to...
...rust-bitcoin 0.30.0
2023-08-03 10:59:06 +02:00
志宇
b206a985cf
fix: Even more refactoring to code and documentation
Thank you to @LLFourn and @danielabrozzoni for these suggestions.
2023-08-01 18:31:22 +08:00
志宇
bea8e5aff4
fix: TxGraph::missing_blocks logic
Added additional tests for unnecessary duplicate heights
2023-07-28 11:37:17 +08:00
志宇
db15e03bdc
fix: improve more docs and more refactoring
Thank you @vladimirfomene for these suggestions.

* Rename `LocalUpdate::keychain` to `LocalUpdate::last_active_indices`.
* Change docs for `CheckPoint` to be more descriptive.
* Fix incorrect logic in `update_local_chain` for `EsploraExt` and
  `EsploraAsyncExt`.
2023-07-28 11:37:17 +08:00
志宇
eabeb6ccb1
Implement linked-list LocalChain and update chain-src crates/examples
This commit changes the `LocalChain` implementation to have blocks
stored as a linked-list. This allows the data-src thread to hold a
shared ref to a single checkpoint and have access to the whole history
of checkpoints without cloning or keeping a lock on `LocalChain`.

The APIs of `bdk::Wallet`, `esplora` and `electrum` are also updated to
reflect these changes. Note that the `esplora` crate is rewritten to
anchor txs in the confirmation block (using the esplora API's tx status
block_hash). This guarantees 100% consistency between anchor blocks and
their transactions (instead of anchoring txs to the latest tip).
`ExploraExt` now has separate methods for updating the `TxGraph` and
`LocalChain`.

A new method `TxGraph::missing_blocks` is introduced for finding
"floating anchors" of a `TxGraph` update (given a chain).

Additional changes:

* `test_local_chain.rs` is refactored to make test cases easier to
  write. Additional tests are also added.
* Examples are updated.
* Fix `tempfile` dev dependency of `bdk_file_store` to work with MSRV

Co-authored-by: LLFourn <lloyd.fourn@gmail.com>
2023-07-28 11:30:16 +08:00
Steve Myers
f4d2a76661
Bump version to 1.0.0-alpha.1 2023-07-04 12:28:06 -05:00
志宇
1c3cbefa4d
[chain_redesign] Remove old structures
Other changes:

* The `async-https` feature of `bdk_esplora` is no longer default.
* Rename `ObservedAs` to `ChainPosition`.
* Set temporary MSRV to 1.60.0 to compile all workspace members will all
  features.
2023-06-04 03:32:18 +08:00
志宇
5860704b2d
Implement redesigned versions of EsploraExt and EsploraAsyncExt
All associated examples are also updated.
2023-06-04 03:32:18 +08:00
志宇
2952341e52
Update the wallet_electrum example 2023-06-04 03:32:18 +08:00
志宇
78a7920ba3
bdk_electrum API improvements and simplifications
* `ElectrumUpdate::missing_full_txs` now returns a `Vec<Txid>` so we
  don't keep a reference to the passed-in `graph`.

* `ElectrumUpdate::finalize*` methods now takes in `missing` txids
  instead of `full_txs`. `Client::batch_transaction_get` is called
within the methods.

Other changes:

* `wallet::ChangeSet` is now made public externally. This is required as
  a wallet db should implement `PersistBackend<wallet::ChangeSet>`.
2023-06-04 03:32:18 +08:00
志宇
92709d03ce
Various tweaks to code arrangement and documentation
As per reviews by @danielabrozzoni and @LLFourn
2023-06-04 03:32:18 +08:00
志宇
50425e979b
Introduce keychain::LocalChangeSet
This corresponds to `keychain::KeychainChangeSet` but for the redesigned
structures with `LocalChain`.

This structure is now used in `Wallet` as well as the examples.
2023-06-04 03:32:18 +08:00