Skip to content

Use Amount type for TxOut value field#1811

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
yancyribbens:txout-use-amount-type
May 5, 2023
Merged

Use Amount type for TxOut value field#1811
apoelstra merged 1 commit intorust-bitcoin:masterfrom
yancyribbens:txout-use-amount-type

Conversation

@yancyribbens
Copy link
Copy Markdown
Contributor

Propose using Amount type for the TxOut value field. I only implemented Decodable and Encodable enough to compile but this needs to completed obviously if using Amount seems like a good idea.

@yancyribbens
Copy link
Copy Markdown
Contributor Author

Also the tests would need to be updated as well to use Amount

@RCasatta RCasatta marked this pull request as ready for review April 24, 2023 15:34
@RCasatta
Copy link
Copy Markdown
Collaborator

I inadvertently pressed ready for review on mobile and now I can't find how to revert, sorry

@Kixunil Kixunil marked this pull request as draft April 24, 2023 16:02
@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Apr 24, 2023

Reverted back to draft.

Concept ACK, I originally wanted to do this myself, happy to see I won't have to! :)

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.

Unrelated but looks like this should be an associated constant TxOut::NULL

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.

Agreed. And we probably shouldn't use default to construct it because it's actually a pretty obscure thing.

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.

Heads up that this is not yet addressed, but I'm going to merge this anyway since it's a pretty big PR that'll be hard to re-review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think this can be made an associated constant because ScriptBuf::new() is not a const.

impl TxOut {
    pub const NULL: Self = TxOut { 
        value: Amount::from_sat(0xffffffffffffffff), 
        script_pubkey: ScriptBuf::new() } }
    }
    ..
}

Is there a reason this needs to be an associated const?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@yancyribbens yancyribbens force-pushed the txout-use-amount-type branch from f987255 to 35eedb9 Compare April 26, 2023 08:36
@yancyribbens yancyribbens marked this pull request as ready for review April 26, 2023 08:37
@yancyribbens yancyribbens force-pushed the txout-use-amount-type branch from 35eedb9 to c8f2428 Compare April 26, 2023 08:42
@yancyribbens yancyribbens changed the title WIP: Use Amount type for TxOut value field Use Amount type for TxOut value field Apr 26, 2023
@yancyribbens
Copy link
Copy Markdown
Contributor Author

hmm almost ready. Looks like there are some changes for nightly and 1.48.0 needed still.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Apr 26, 2023

Looks like it's not 1.48-related but serde-related.

@yancyribbens yancyribbens force-pushed the txout-use-amount-type branch 4 times, most recently from b49da67 to 2c4b8e8 Compare April 27, 2023 09:13
@yancyribbens
Copy link
Copy Markdown
Contributor Author

I believe the CI failure is unrelated to this PR. Seems like cross rs can't find the docker container currently.

@yancyribbens
Copy link
Copy Markdown
Contributor Author

Otherwise, this should be good to ship imo

tcharding
tcharding previously approved these changes Apr 27, 2023
Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 2c4b8e82c45a499e12f774db7623f94ff0214f5d

Comment on lines 1065 to 1066
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.

Suggested change
/// use bitcoin::Amount;
/// use bitcoin::{absolute, Transaction, Script};
/// use bitcoin::{absolute, Amount, Transaction, Script};

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.

consensus_decode is inlined, might as well do this one too.

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.

Recently @Kixunil made a push for using a custom integer format for bitcoin values 1_000_000_00, requires

    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.

You can see a bunch of these in amount.rs. You could use it if you like it.

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.

I guess we should have Amount::from_whole_btc accepting u64. But also 50 * Amount::ONE_BTC should work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried both 50 * Amount::ONE_BTC and Amount::from_str("50 BTC").unwrap() here but it's not currently possible because this is a const so the compiler doesn't allow it.

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.

We should probably have #![allow(clippy::inconsistent_digit_grouping)] on the whole crate. This lint is stupid and has a 100% false positive rate for me.

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.

Also to avoid const issues we should have constants not only for 1 btc, but also 10, 25, 50, 100 and 21 million. And .1 and .01. Probably also 10, 100, 1000, 10000, 100000 sats (1_000_000 sats is already covered as .01 BTC).

These constants are all 1 LOC (plus a comment plus a whitespace) and will never change, so there's no harm in having a pile of them.

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.

@Kixunil in rust-bech32 we are panicking in a constfn; see discussion here rust-bitcoin/rust-bech32#103 (comment)

I think in 1.48 it's ok?

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.

Panicking is only const since 1.57.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But we could conditionally use it to provide a better error message in higher versions.

Do we have examples of version conditionals already in the code base? It looks like things such as rustversion require an added dependency.

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.

@yancyribbens yep, grep the codebase for rust_v_1_53 to see how we've configured some things that show up only on Rust 1.53 and above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a PR here for this #1870

@yancyribbens
Copy link
Copy Markdown
Contributor Author

Is there some considerations that need to made since this is a breaking API change? Downstream crates that use u64 for TxOut value should get a deprecation warning?

@apoelstra
Copy link
Copy Markdown
Member

@yancyribbens unfortunately AFAICT there is no way to make this a "nice" breaking change. But FWIW I think it will get into the first pre-release as the only major thing, and the fix is nearly a search-and-replace operation, so I think it might be okay.

tcharding
tcharding previously approved these changes May 4, 2023
Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 104d206bbb1fb1d778c956f4d313929b4f1d69cc

@tcharding
Copy link
Copy Markdown
Member

We are going to get merge conflicts if #1829 merges ahead of this.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented May 4, 2023

This will actually simplify tons of code that contain conversions between u64 and Amount.

@yancyribbens
Copy link
Copy Markdown
Contributor Author

rebased to fix merge conflicts.

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK d57ec01

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK d57ec01

@apoelstra apoelstra merged commit 25f569a into rust-bitcoin:master May 5, 2023
apoelstra added a commit that referenced this pull request Jun 14, 2023
9f7449b Use from_int_btc function for const context (yancy)
f93e679 Add from_int_btc function to Amount (yancy)

Pull request description:

  Followup PR from #1811

  Added a `const` associated function `from_int_btc()` for Amount.  `panic()` in const context is only available after 1.57+ so a work around is provided.

ACKs for top commit:
  tcharding:
    ACK 9f7449b
  apoelstra:
    ACK 9f7449b

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants