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: add further bitcoin::Amount usage on public APIs #1426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oleonardolima
Copy link
Contributor

@oleonardolima oleonardolima commented May 5, 2024

builds on top of #1411, further improves #823

Description

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

Notes to the reviewers

Open for comments and discussions.

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.

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

@oleonardolima
Copy link
Contributor Author

@ValuedMammal Please let me know what are your thoughts on these changes.

@oleonardolima oleonardolima marked this pull request as ready for review May 6, 2024 19:45
@oleonardolima oleonardolima force-pushed the feat/add-further-amount-usage branch from b15d01d to 1f9728b Compare May 7, 2024 19:22
@oleonardolima
Copy link
Contributor Author

@ValuedMammal Please let me know what are your thoughts on these changes.

I did remove the updates where it's pub(crate) APIs, and kept only the ones for explicitly pub APIs.

crates/bdk/src/wallet/tx_builder.rs Outdated Show resolved Hide resolved
Comment on lines 207 to 210
/// The fee_absolute method refers to the absolute transaction fee in [`Amount`].
/// If anyone sets both the `fee_absolute` method and the `fee_rate` method,
/// the `FeePolicy` enum will be set by whichever method was called last,
/// as the [`FeeRate`] and `FeeAmount` are mutually exclusive.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this. Now that I'm looking at it, maybe referring to the "FeePolicy enum" is a bit too much detail, but we can state it more generally.

/// Set an absolute fee [`Amount`].
///
/// The `fee_absolute` method refers to the absolute transaction fee. If anyone sets both
/// this method and the [`fee_rate`] method, the fee policy for building the transaction
/// will be set by whichever method was called last, as the feerate and fee amount are
/// mutually exclusive.
///
/// Note that this is really a minimum absolute fee -- it's possible to
/// overshoot it slightly since adding a change output to drain the remaining
/// excess might not be viable.
///
/// [`fee_rate`]: Self::fee_rate 

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't agree. I always think that these links are useful.
They are "hover" only and avoids you having to go through the codebase, both in an IDE/LSP and also in the Docs.

I would agree with you if only the thing we are referring is not public, which is not the case here

#[derive(Debug, Clone, Copy)]
pub(crate) enum FeePolicy {
FeeRate(FeeRate),
FeeAmount(u64),
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for fixing this. Now that I'm looking at it, maybe referring to the "FeePolicy enum" is a bit too much detail, but we can state it more generally.

I agree with Jose's point, and that's what I had in mind and also why I updated it.

It gives the ergonomic for the developer to know exactly which type is being used, and to navigate easily on the code, at least I use it a lot with rust-analyzer (even navigating into the dependencies and so on).

[...]
I would agree with you if only the thing we are referring is not public, which is not the case here

Exactly, I updated it only for the ones that are explicitly public, the compiler does not even allow you to reference non-public APIs.

Copy link
Contributor

Choose a reason for hiding this comment

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

I based this #1426 (comment) suggestion on the observation that FeePolicy won't be visible to someone outside the crate reading the docs for example on docs.rs, which is why I think it doesn't belong here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, but still for us and other BDK developers I think it helps to navigate the codebase through IDE tools 🤔.

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

ValuedMammal commented May 8, 2024

Here's how I would approach this PR with fewer lines touched ValuedMammal/bdk@0c72958

However I think it's worth having a discussion about where we can continue to get benefits from the Amount type so I opened an issue #1432

--

edit: @oleonardolima Just checking, is the plan to go ahead and complete #1432 in this PR?

@oleonardolima oleonardolima force-pushed the feat/add-further-amount-usage branch 2 times, most recently from 8f0b685 to 80deac6 Compare May 15, 2024 02:15
@oleonardolima
Copy link
Contributor Author

[...]

--

edit: @oleonardolima Just checking, is the plan to go ahead and complete #1432 in this PR?

I think I can continue the work on non-public code (everywhere) on another PR, and keep this one only for the public APIs that remained from #1411. wdyt @ValuedMammal ?

Also, I was waiting if some new discussion would occur on #1432 before working on it :)

@oleonardolima oleonardolima changed the title feat: add further bitcoin::Amount usage feat: add further bitcoin::Amount usage on public APIs May 22, 2024
@oleonardolima oleonardolima force-pushed the feat/add-further-amount-usage branch from 80deac6 to 6c572d7 Compare May 22, 2024 18:54
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.

Thanks for working on this. There's a small typo in the changelog notice - FeePolicy::fee_absolute should say TxBuilder::fee_absolute.

crates/wallet/src/wallet/error.rs Show resolved Hide resolved
…nd others

- update to use `bitcoin::Amount` on `CreateTxError::FeeTooLow` variant.
- update to use `bitcoin::Amount` on `Wallet::calculate_fee()`.
- update to use `bitcoin::Amount` on `FeePolicy::fee_absolute()`.
- update to use `bitcoin::SignedAmount` on
  `CalculateFeeError::NegativeFee` variant.
- update to use `bitcoin::Amount` on `TxGraph::calculate_fee()`.
- update to use `bitcoin::Amount` on `PsbUtils::fee_amount()`
@oleonardolima oleonardolima force-pushed the feat/add-further-amount-usage branch from 6c572d7 to 5f2d6cb Compare May 23, 2024 01:59
@oleonardolima
Copy link
Contributor Author

Thanks for working on this. There's a small typo in the changelog notice - FeePolicy::fee_absolute should say TxBuilder::fee_absolute.

Oh, thanks! Sorry for the oversight, fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

None yet

4 participants