Skip to content

refactor(wallet): use Amount everywhere#1595

Merged
notmandatory merged 1 commit intobitcoindevkit:masterfrom
ValuedMammal:refactor/Amount
Sep 9, 2024
Merged

refactor(wallet): use Amount everywhere#1595
notmandatory merged 1 commit intobitcoindevkit:masterfrom
ValuedMammal:refactor/Amount

Conversation

@ValuedMammal
Copy link
Copy Markdown
Collaborator

This is a followup to #1426 that refactors wallet internals to use bitcoin::Amount (nearly) everywhere. I chose not to change any public types in coin_selection.rs that still use u64 as that might require more discussion.

partially addresses #1432
fixes #1434

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

@ValuedMammal ValuedMammal self-assigned this Sep 9, 2024
@ValuedMammal ValuedMammal added this to the 1.0.0-beta milestone Sep 9, 2024
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.

ACK 292ec3c

Comment on lines 35 to 39
impl IsDust for u64 {
fn is_dust(&self, script: &Script) -> bool {
*self < script.minimal_non_dust().to_sat()
Amount::from_sat(*self).is_dust(script)
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: I guess this could be moved to coin_selection, it's the only place still using is_dust with a u64, right ?

let mut tx_builder = wallet.build_tx();
tx_builder
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
.add_recipient(address.script_pubkey(), SEND_AMOUNT)
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.

👍 thanks for cleaning this up!

Copy link
Copy Markdown
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

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

ACK 292ec3c

@notmandatory notmandatory merged commit 90a89bf into bitcoindevkit:master Sep 9, 2024
@ValuedMammal ValuedMammal deleted the refactor/Amount branch September 10, 2024 04:26
This was referenced Sep 13, 2024
notmandatory added a commit that referenced this pull request Dec 11, 2024
…se Amount type

2a9eeed ci: pin msrv dep version for rustls (Steve Myers)
20789ec refactor(chain)!: use Amount for DescriptorExt::dust_value() (Steve Myers)
58a8435 refactor(coin_selection)!: use Amount and SignedAmount for API and internally (Steve Myers)

Pull request description:

  ### Description

  refactor(coin_selection)!: use Amount and SignedAmount for API and internally
  refactor(chain)!: use Amount for DescriptorExt::dust_value()

  Using named types make the API and internal code easier to read and reason about since it makes it clear that the values are bitcoin amounts. Also to create these types the units (ie .from_sat()) must be specified.

  ### Notes to the reviewers

  For coin_selection using Amount and SignedAmount makes internal code safer against overflow errors. In particular because these types will panic if an amount overflow occurs. Using u64/i64 on the other hand can silently rollover. See: https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow

  This is a continuation of the work done in #1595.  Since this is an API breaking change I would like to include it in the 1.0.0-beta milestone if possible.

  ### Changelog notice

  - Change coin_selection to use Amount instead of u64 for all bitcoin amounts.
  - Change DescriptorExt::dust_value() to return an Amount instead of u64.

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

  #### Bugfixes:

  * [x] This pull request breaks the existing API
  * [ ] I've added tests to reproduce the issue which are now passing
  * [ ] I'm linking the issue being fixed by this PR

ACKs for top commit:
  ValuedMammal:
    ACK 2a9eeed

Tree-SHA512: 9dd1b31d0f8d3d8c383c7aae7ec0fffb55bfcfe49c804e273faa740d30efde7efb7c9504e87cceb56798ea14a3e34fc8a7b65a8ad5e52ea38b8523150c9b6bc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

example: Fix wallet examples

3 participants