Skip to content

Remove VarInt and use ReadExt and WriteExt trait methods instead #2931

Merged
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:06-27-var-int
Sep 30, 2024
Merged

Remove VarInt and use ReadExt and WriteExt trait methods instead #2931
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:06-27-var-int

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Jun 27, 2024

This the meat and potatoes out of Steven's work in #2133 and also closes #1016

@tcharding
Copy link
Copy Markdown
Member Author

CC @stevenroose, I picked up your work man, hope you don't mind.

@tcharding
Copy link
Copy Markdown
Member Author

Might close #1016

@tcharding
Copy link
Copy Markdown
Member Author

Name might be wrong, converting to draft to work it out. ref : #1016 (comment)

@tcharding tcharding marked this pull request as draft June 27, 2024 05:47
@github-actions github-actions bot added C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate C-units PRs modifying the units crate C-internals PRs modifying the internals crate C-io PRs modifying the io crate C-base58 PRs modifying the base58 crate labels Jun 27, 2024
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.

The 9fe420d2210bfcfab4d0913c9dd94f633496f95a commit message makes it sound like getting the length is impossible without returning it. That's a complete BS.

Instead the message should be something like "Returning usize is more convenient for consumers that need to add up the length of multiple encoded items."

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.

This should be ToU64 which was added in fc03492d8d45a54da0567db8c46e6f84cb5d0148

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ugh, sloppy work by me.

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.

Not const, it should say varint_size_u64.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

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.

Alternative design: add varint_size method to ToU64, make this accept u64 and make it const, delete varint_size_const. Then users will be able to call .varint_size on integers. (In that case, the trait must be public and stable, not private/internals as I suggested elsewhere.) There's potential silliness that some other crate will define consensus-encoding-related method. We could have NumExt: ToU64 trait to move it away.

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.

You can't make trait methods const.

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 mean make this fn const by being non-generic and use methods for convenience instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Flagging that I did not action this.

@tcharding tcharding mentioned this pull request Jun 27, 2024
@github-actions github-actions bot removed C-hashes PRs modifying the hashes crate C-units PRs modifying the units crate C-io PRs modifying the io crate C-base58 PRs modifying the base58 crate labels Aug 6, 2024
@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 6, 2024

🚨 API BREAKING CHANGE DETECTED

@github-actions github-actions bot added the API break This PR requires a version bump for the next release label Aug 6, 2024
@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 6, 2024

🚨 API BREAKING CHANGE DETECTED

@tcharding
Copy link
Copy Markdown
Member Author

Rebased, added a new final patch, and re-wrote PR description.

@tcharding tcharding marked this pull request as ready for review August 27, 2024 02:16
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

@tcharding
Copy link
Copy Markdown
Member Author

Perhaps we should instead use get_size_of_comact_size to mirror GetSizeOfCompactSize from Core

ref: https://github.com/bitcoin/bitcoin/blob/37cdb5f2483cd57077497925caf69a82b0d39471/src/serialize.h#L295C31-L295C35

@tcharding tcharding force-pushed the 06-27-var-int branch 2 times, most recently from 8b387ae to ae96162 Compare September 17, 2024 23:03
@tcharding tcharding marked this pull request as ready for review September 17, 2024 23:04
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

1 similar comment
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@apoelstra
Copy link
Copy Markdown
Member

Needs rebase

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Sep 18, 2024

Rebased, then got lost fixing my emacs, not sure if I touched anything other than rebase but I don't think so.

@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@apoelstra
Copy link
Copy Markdown
Member

Will ACK. FWIW I'm happy with this API and would be willing to pull it out of internals (though I guess it'd most sensibly live with ReadExt/WriteExt which also don't have homes yet).

apoelstra
apoelstra previously approved these changes Sep 20, 2024
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 1599db253574a97191ee63146ce57040845800e1 successfully ran local tests; nice, though sad that we need a separate non-generic compact_size_const method

@tcharding
Copy link
Copy Markdown
Member Author

though sad that we need a separate non-generic compact_size_const method

Agreed.

@tcharding
Copy link
Copy Markdown
Member Author

Re-based, during which arbitrary import moved locations, I think that is why the range-diff shows changed lines.

@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@tcharding
Copy link
Copy Markdown
Member Author

Needs #3395

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 b58dd27c42d56df8a3e05ff719022e240ea0ff81 successfully ran local tests; nice, though sad that we need a separate non-generic compact_size_const method

@tcharding
Copy link
Copy Markdown
Member Author

lol, amusingly, your re-post of the 'sad that we ...' confused me, I was sure I'd read that before :)

@apoelstra
Copy link
Copy Markdown
Member

Yeah :) I'm not sure what the best way for me to "refresh an ACK" with a nontrivial message is. I want to repeat the message so it shows up in the commit message, but it's weird to read for people on the PR.

@tcharding
Copy link
Copy Markdown
Member Author

Oh, that is ok, now I know it won't surprise me.

We would like to add a `emit_varint` function, however doing so requires
that we can get access to the length of a slice when we are encoding it
so we can use `emit_slice` to implement `emit_varint`. It would be
easier to do so if `emit_slice` returned the length of the slice.

In preparation for adding `emit_varint` (and removing the `VarInt` type)
return the encoded length of a slice from `WriteExt::emit_slice`.

(Patch originally written by Steven, cherry-pick and patch description
written by Tobin.)

Co-developed-by: Tobin C. Harding <me@tobin.cc>
At some stage we named the compact encoding `VarInt` (which makes sense
because the compact size encoding is a variable length integer encoding).
However it turns out the term "varint" is used in Core for a different
encoding so this may lead to confusion.

While we fix this naming thing observe also that the `VarInt` type is
unnecessarily complicated, all we need to be able to do is encode and
decode integers in compact form as specified by Core. We can do this
simply by extending our `WriteExt` and `ReadExt` traits.

Add `emit_compact_size` and `read_compact_size` to emit and read compact
endcodings respectively.

Includes addition of `internals::compact_size::encoded_size_const`.

Patch originally written by Steven, Tobin cherry-picked and did a bunch
of impovements after the varint vs compact_size thing (rust-bitcoin#1016).

ref: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer

Co-developed-by: Tobin C. Harding <me@tobin.cc>
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

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 18d8b0e successfully ran local tests

@tcharding
Copy link
Copy Markdown
Member Author

I rekon we can merge this, all @Kixunil's concerns from last month have been either addressed or commented on.

@apoelstra apoelstra merged commit dfa8692 into rust-bitcoin:master Sep 30, 2024
@tcharding tcharding deleted the 06-27-var-int branch October 14, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API break This PR requires a version bump for the next release C-bitcoin PRs modifying the bitcoin crate C-internals PRs modifying the internals crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VarInt in this project is CompactSize in Bitcoin Core

4 participants