Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update keychain::Balance to use bitcoin::Amount #1411

Conversation

oleonardolima
Copy link
Contributor

@oleonardolima oleonardolima commented Apr 24, 2024

fixes #823

Description

It's being used on Balance, and throughout the code, an u64 represents the amount, which relies on the user to infer its sats, not millisats, or any other representation.

It updates the usage of u64 on Balance, and other APIs:

  • TxParams::add_recipient
  • KeyChainTxOutIndex::sent_and_received, KeyChainTxOutIndex::net_value
  • SpkTxOutIndex::sent_and_received, SpkTxOutIndex::net_value

Notes to the reviewers

It updates some of the APIs to expect the bitcoin::Amount, but it does not update internal usage of u64, such as TxParams still expects and uses u64, please see the PR comments for related discussion.

Changelog notice

  • Changed the keychain::Balance struct fields to use Amount instead of u64.
  • Changed the add_recipient method on TxBuilder implementation to expect bitcoin::Amount.
  • Changed the sent_and_received, and net_value methods on KeyChainTxOutIndex to expect bitcoin::Amount.
  • Changed the sent_and_received, and net_value methods on SpkTxOutIndex to expect bitcoin::Amount.

Checklists

All Submissions:

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

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • 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

@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 0e6d7e5 to ae54551 Compare April 25, 2024 00:08
@oleonardolima oleonardolima changed the title wip(feat): update wallet::Balance to use bitcoin::Amount wip(feat): update keychain::Balance to use bitcoin::Amount Apr 25, 2024
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 736495f to fe5e313 Compare April 26, 2024 14:22
@notmandatory notmandatory added module-wallet api A breaking API change labels Apr 28, 2024
@notmandatory notmandatory added this to the 1.0.0-alpha milestone Apr 28, 2024
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch 4 times, most recently from 5aa1990 to 82b2ae5 Compare April 29, 2024 14:42
@oleonardolima oleonardolima changed the title wip(feat): update keychain::Balance to use bitcoin::Amount feat: update keychain::Balance to use bitcoin::Amount Apr 29, 2024
@oleonardolima
Copy link
Contributor Author

I think it's ready for a new round of reviews

I'll move the previous conversations to resolved after it, and if it's everything looks good on this new review round)

@storopoli
Copy link
Contributor

I think it's ready for a new round of reviews

Can you move it from Draft PR to a full PR?

@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 82b2ae5 to 06d3d96 Compare April 29, 2024 16:03
@oleonardolima oleonardolima marked this pull request as ready for review April 29, 2024 16:03
@oleonardolima
Copy link
Contributor Author

I think it's ready for a new round of reviews

Can you move it from Draft PR to a full PR?

Yes, forgot to do it

@oleonardolima
Copy link
Contributor Author

oleonardolima commented Apr 29, 2024

Oh, I forgot to run the tests on docs, working on it 😅
Done!

@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch 2 times, most recently from 423aed4 to df4cdfe Compare April 29, 2024 20:42
Copy link
Contributor

@storopoli storopoli left a comment

Choose a reason for hiding this comment

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

Some suggestions.

Most important, why we are using SignedAmount in some places?

crates/bdk/src/wallet/coin_selection.rs Outdated Show resolved Hide resolved
crates/bdk/src/wallet/mod.rs Outdated Show resolved Hide resolved
crates/bdk/src/wallet/mod.rs Outdated Show resolved Hide resolved
crates/bdk/src/wallet/mod.rs Outdated Show resolved Hide resolved
crates/bdk/src/wallet/tx_builder.rs Outdated Show resolved Hide resolved
crates/chain/src/keychain/txout_index.rs Show resolved Hide resolved
crates/chain/src/spk_txout_index.rs Show resolved Hide resolved
crates/chain/src/tx_graph.rs Outdated Show resolved Hide resolved
crates/chain/tests/test_spk_txout_index.rs Show resolved Hide resolved
crates/chain/tests/test_spk_txout_index.rs Show resolved Hide resolved
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from df4cdfe to 362a67a Compare April 30, 2024 17:29
@oleonardolima
Copy link
Contributor Author

oleonardolima commented Apr 30, 2024

Some suggestions.

Most important, why we are using SignedAmount in some places?

It's because net_value used i64 previously, and to keep the standard (and makes sense in my opinion given the fn name) I used the SignedAmount, same for the other comments, they are all net_value references.

(replying on the overall comment too)

Copy link
Contributor

@storopoli storopoli left a comment

Choose a reason for hiding this comment

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

ACK 362a67a

Great work!

@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 362a67a to c0b0e14 Compare April 30, 2024 20:40
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from c0b0e14 to bd41a0c Compare May 1, 2024 23:00
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from bd41a0c to df92624 Compare May 2, 2024 16:59
Copy link
Contributor

@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.

Sorry my reviews have been intermittent. I think this is good.

The commit history could be cleaned up slightly:

  • In df92624 we want to refer to TxBuilder::add_recipient (not CoinSelectionAlgorithm)

  • Some changes in later commits (like fixing imports and wallet examples) could be squashed into earlier ones, but this not a blocker in my opinion.

Some other thoughts: I agree there is potentially more to do like updating calculate_fee and Psbt::fee_amount to use Amount. I'm still unsure how we should handle amounts internally. Honestly I don't mind leaving it as u64. If we decide that all amounts should be strongly typed, then Amount seems like a logical choice, my only concern is we should avoid the possibility of mixing up denominations.

crates/bdk/src/wallet/mod.rs Show resolved Hide resolved
crates/bdk/tests/wallet.rs Outdated Show resolved Hide resolved
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch 2 times, most recently from 86bbd79 to 234ab88 Compare May 5, 2024 00:54
- update all fields `immature`, ` trusted_pending`, `unstrusted_pending`
  and `confirmed` to use the `bitcoin::Amount` instead of `u64`
- update all `impl Balance` methods to use `bitcoin::Amount`
- update all tests that relies on `keychain::Balance`
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 234ab88 to 2e6aeb7 Compare May 5, 2024 01:00
@oleonardolima
Copy link
Contributor Author

Sorry my reviews have been intermittent. I think this is good.

The commit history could be cleaned up slightly:

  • In df92624 we want to refer to TxBuilder::add_recipient (not CoinSelectionAlgorithm)
  • Some changes in later commits (like fixing imports and wallet examples) could be squashed into earlier ones, but this not a blocker in my opinion.

Some other thoughts: I agree there is potentially more to do like updating calculate_fee and Psbt::fee_amount to use Amount. I'm still unsure how we should handle amounts internally. Honestly I don't mind leaving it as u64. If we decide that all amounts should be strongly typed, then Amount seems like a logical choice, my only concern is we should avoid the possibility of mixing up denominations.

@ValuedMammal No worries, thanks for the comments and suggestions! I tried to address them, please let me know if something remains or if new needed changes appear.

Copy link
Contributor

@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.

ACK 2e6aeb7

crates/bdk/src/wallet/mod.rs Outdated Show resolved Hide resolved
@ValuedMammal
Copy link
Contributor

Actually, changing calculate_fee and Psbt::fee_amount should be fairly easy to add to this PR but I'll let you decide if you want to tackle it here or later. Besides that, I'm not sure if there's still more places in the public API that would benefit from using Amount.

- update `wallet.rs` fns: `sent_and_received` fn
- update `keychain` `txout_index` fn: `sent_and_received and `net_value`
@oleonardolima oleonardolima force-pushed the feat/use-rust-bitcoin-balance-instead branch from 2e6aeb7 to 22aa534 Compare May 5, 2024 15:07
@oleonardolima
Copy link
Contributor Author

Actually, changing calculate_fee and Psbt::fee_amount should be fairly easy to add to this PR but I'll let you decide if you want to tackle it here or later. Besides that, I'm not sure if there's still more places in the public API that would benefit from using Amount.

I updated calculate_fee and fee_amount on ac762f4, but ended up touching other parts as well.

I would rather move this commit ac762f4 to another PR, and keep only the previous ones here, wdyt ?

@oleonardolima
Copy link
Contributor Author

Actually, changing calculate_fee and Psbt::fee_amount should be fairly easy to add to this PR but I'll let you decide if you want to tackle it here or later. Besides that, I'm not sure if there's still more places in the public API that would benefit from using Amount.

I updated calculate_fee and fee_amount on ac762f4, but ended up touching other parts as well.

I would rather move this commit ac762f4 to another PR, and keep only the previous ones here, wdyt ?

I moved it to the other PR #1426, we can start discussing the changes on that one after this one is merged.

Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

ACK 22aa534

@evanlinjin evanlinjin merged commit dcd2d47 into bitcoindevkit:master May 6, 2024
23 checks passed
@oleonardolima oleonardolima deleted the feat/use-rust-bitcoin-balance-instead branch May 6, 2024 13:48
@notmandatory notmandatory mentioned this pull request May 10, 2024
28 tasks
notmandatory added a commit that referenced this pull request Jun 4, 2024
a03949a feat: use `Amount` on `calculate_fee`, `fee_absolute`, `fee_amount` and others (Leonardo Lima)

Pull request description:

  builds on top of #1411, further improves #823

  <!-- You can erase any parts of this template not applicable to your Pull Request. -->

  ### Description

  It further updates and adds the usage of `bitcoin::Amount` instead of `u64`.

  <!-- Describe the purpose of this PR, what's being adding and/or fixed -->

  ### Notes to the reviewers

  Open for comments and discussions.

  <!-- In this section you can include notes directed to the reviewers, like explaining why some parts
  of the PR were done in a specific way -->

  ### Changelog notice
  - Updated `CreateTxError::FeeTooLow` to use `bitcoin::Amount`.
  - Updated `Wallet::calculate_fee()`. to use `bitcoin::Amount`
  - Updated `TxBuilder::fee_absolute()`. to use `bitcoin::Amount`.
  - Updated `CalculateFeeError::NegativeFee` to use `bitcoin::SignedAmount`.
  - Updated `TxGraph::calculate_fee()`. to use `bitcoin::Amount`.
  - Updated `PsbUtils::fee_amount()` to use `bitcoin::Amount`.

  <!-- Notice the release manager should include in the release tag message changelog -->
  <!-- See https://keepachangelog.com/en/1.0.0/ for examples -->

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

  #### New Features:

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

ACKs for top commit:
  storopoli:
    ACK a03949a

Tree-SHA512: abfbf7d48ec004eb448ed0a928e7e64b5f82a2ab51ec278fede4ddbff4eaba16469a7403f78dfeba1aba693b0132a528bc7c4ef072983cbbcc2592993230e40f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api A breaking API change module-wallet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Balance should use bitcoin::Amount
5 participants