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

Introduce a primitive types crate #1818

Closed
wants to merge 12 commits into from

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Apr 27, 2023

Create a crate that holds primitive types used by the rust-bitcoin ecosystem.

bitcoin-primitives already exists on crates.io, use rust-bitcoin-primitives instead.

Just the lock times at the moment.

@tcharding tcharding marked this pull request as ready for review April 27, 2023 06:46
@tcharding
Copy link
Member Author

Flagging that this needs the name to be agreed upon: #1809 (comment)

@tcharding tcharding marked this pull request as draft April 28, 2023 04:15
@Kixunil
Copy link
Collaborator

Kixunil commented Apr 28, 2023

rust-bitcoin-primitives

NACK this name (unless it's temporary). rust- is not recommended by crate guidelines and may become illegal (I guess unlikely but why make it worse?). Also it's hard to understand the difference between bitcoin-primitives and rust-bitcoin-primitives.

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Network would be nice too.

repository = "https://github.com/rust-bitcoin/rust-bitcoin"
documentation = "https://docs.rs/rust-bitcoin-primitives/"
description = "Primitive types used by the rust-bitcoin eccosystem"
categories = ["cryptography::cryptocurrencies"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should have also no-std and no-alloc

}

impl FromStr for WitnessVersion {
type Err = ConversionError;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume you copied this so I'd accept this as a stepping stone but this is not great. We should have ParseError != ConversionError. ParseError may internally store ConversionError but not vice-versa.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't copy it I wrote this, I'll have to come back and read this again (just going through my notifications quickly), I can't solve parse vs convert nuance right now. Will re-work the error though, thanks.

@yancyribbens
Copy link
Contributor

Just curious, what's the advantage of including this crate as another workspace member instead of part of the rust-bitcoin organization?

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 28, 2023

@yancyribbens mainly allows us to update the API and then immediately use the new API in downstream crate. This way we can start testing out the new API sooner. It also has its own problems though - see: #1553

@apoelstra
Copy link
Member

@yancyribbens in general, I think life is easier in Rust-land if we have a monorepo, at least for the "core" crates. rust-secp is the only real exception because it has such a different review/maintainership model ... though we may wind up pulling it in nonetheless because of what a PITA it is to have it separate.

A similar strategy is taken by the num crates, tokio crates, etc.

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 28, 2023

@apoelstra if merging is an option I think I'm quite capable of dealing with "weird C crap, alignment issues and synchronization and FFI" as you put it if it helps. But I suspect the main obstacle is the other maintainers don't want all the notifications from bitcoin? (Which I totally understand. I'm subbed to LDK out of curiosity and it's crazy.)

@apoelstra
Copy link
Member

@Kixunil hmm, good point. I was strongly considering bring rust-secp in because, as you say, I trust you (and the other rust-bitcoin maintainers) to be able to deal with the C crap ... or at least to avoid it when it's sufficiently obscure. But if merging the repos would cause a shitload of notifications for e.g. elichai or bluematt, which they could otherwise avoid, that's a harder burden to take.

Another observation is that rust-secp has a one-ACK policy while rust-bitcoin has a two-ACK policy, because of the lack of maintainers on rust-secp. I worry that if rust-secp were part of this repo, we'd need two ACKs for everything but often be in a position where there weren't two people available who were comfortable ACKing. Especially if this led to bluematt or elichai being even less active on the crate..

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 28, 2023

Not sure about the last part but the theoretical number of total maintainers increasing should improve with merging so two ACKs might improve quality without significantly degrading progress. And the C crap there isn't that much of "rocket science" actually the only thing I found confusing is double panicking supposedly being UB across FFI.

@apoelstra
Copy link
Member

I'm happy to hear it, but I think everybody except you finds the conversation around rust-bitcoin/rust-secp256k1#539 (comment) pretty confusing :)

Kidding aside, I think you're probably right. 99% of what goes into rust-secp is pretty straightforward. Even the C vendoring stuff is nicely wrapped up so you can just use the vendoring scripts. You don't even need to know C.

@stevenroose
Copy link
Collaborator

Network would be nice too.

Not sure I agree Network is a primitive.

But yeah agree on the crate name being annoying. I contacted the bitcoin-rs guy to see if he'd be willing to discuss handing over crate names if we want them. His project seems stale.

@stevenroose
Copy link
Collaborator

@yancyribbens in general, I think life is easier in Rust-land if we have a monorepo, at least for the "core" crates. rust-secp is the only real exception because it has such a different review/maintainership model ... though we may wind up pulling it in nonetheless because of what a PITA it is to have it separate.

A similar strategy is taken by the num crates, tokio crates, etc.

Also because rust-secp256k1 is not bitcoin related.. I mean it's used in bitcoin but in principle it's way more general. (Subtle hint at hex crate 😅 )

@klebs6
Copy link

klebs6 commented May 6, 2023

Network would be nice too.

Not sure I agree Network is a primitive.

But yeah agree on the crate name being annoying. I contacted the bitcoin-rs guy to see if he'd be willing to discuss handing over crate names if we want them. His project seems stale.

greetings folks -- I am happy to transfer crate names where it is helpful to you.

I am currently bringing up a c++ to rust transpiler which will be able to expedite the rest of the translation. (This is why the project seems stale. It is not abandoned or stale, but awaiting the transpiler.)

When this bringup is done, we should be able to zip pretty quickly through any remaining translation work.

Are you open to the possibility of merging? For example, bitcoin-primitives crate name can be transferred (bitcoin-network etc, too, whatever you need), the contents merged, redundant functionality pruned away.

On my end, the transpiler is already good enough to do the remainder of both bitcoin-primitives and bitcoin-network from the workspace I provided without much trouble. I could use it to finish these pieces relatively easily during a merge. This way, all development tracks strengthen while the transpiler is being hardened.

I suppose that when it is finished, the transpiler may be helpful to you folks as well -- is this statement correct? On my end, at least, it is one of the only remaining bottlenecks.

@Kixunil
Copy link
Collaborator

Kixunil commented May 7, 2023

@klebs6 that sounds great! I'm not sure which parts can be meaningfully merged but I'd be happy to look into it. Especially for primitives that could be shared between projects.

Also transpiler sounds very interesting, you might get recognition in wider Rust community.

@apoelstra
Copy link
Member

Agreed that this sounds great! Though I also agree that I'm unsure, specifically, where there's an opportunity for unification :).

In general this crate is trying to move toward a "pure Rust" model of Bitcoin and associated types -- so for things users or wallets might care about we provide our own data structures that increasingly differ from the internal libbitcoinconsensus/Core ones, and for consensus-critical things we try to avoid providing any functionality at all. So I'm unsure that we'll be able to directly use a C++ transpiler. But it's an interesting thought. It would be magical if we could somehow have a bitcoinconsensus library that was "written" in pure Rust and would compile to WASM etc..

@tcharding
Copy link
Member Author

tcharding commented May 9, 2023

It looks like WitnessVersion is not going to be used any time soon by rust-bech32, this raises the question: Do we still want to create this crate as a place holder for primitive types as we continue crates smashing?

Some benefits in creating an empty crate:

  • We can bikeshed the naming
  • We can grab the name of crates.io or do the crate transfer if we are going to request it for bitcoin-primitives

@Kixunil
Copy link
Collaborator

Kixunil commented May 10, 2023

I'd prefer waiting until we have a stronger reason. Especially if we move address/bitcoin-specific stuff to bitcoin.

@klebs6
Copy link

klebs6 commented May 11, 2023

  • glad to be here

  • soon we should get something like:
    impl TryFrom<(CppAstStmt,&ParseContext)> for RustAstStmt

  • ParseContext will be a wrapper around what
    rust-analyzer provides for the full workspace,
    hooks for doing type inference, name lookup,
    etc.

  • we are just a few steps away from getting that, but
    there are a few things i have to see to first

  • once there, the rest of the untranslated code
    should be pretty easy to finish

  • in the meantime, please feel free to ask for any
    of the crate names. i am happy to transfer
    them. my intent is not to "reserve" these names
    for any purpose.

  • i do want to make sure that whatever is already
    inside the crate will still have a home. it is
    possible that in some cases this material could
    be shifted into a neighboring crate in the
    workspace. a new crate at the same place within
    the crate dependency hierarchy might need to be
    created. but maybe it is possible to merge.

  • if there is material already translated which is
    useful to you, you are welcome to it. just let
    me know and we can figure out what to do.

@tcharding
Copy link
Member Author

Thanks man!

@tcharding
Copy link
Member Author

On ice until we need it.

@stevenroose
Copy link
Collaborator

It's too late now I guess, but for your project, I think it would have been better if you had prefixed all your crate with something more specific to your effort. Like bitcoin-cores2s-xxx or something. But well, thanks for being nice about potential naming conflicts.

tcharding and others added 5 commits February 14, 2024 11:50
Test the soon-to-be-released `hex` crate.

Built depending on this branch:

    rust-bitcoin/hex-conservative#64

Currently does not work because of the recent changes we did to the
`DisplayArray` type in PR rust-bitcoin#52

    rust-bitcoin/hex-conservative#52

I'm throwing this up so others can see the error without having to run
it locally.
Fixes a gap in the API of the taproot module. Callers can now use
TapTree::root_hash or NodeInfo::node_hash to extract the taproot
tree merkle root hash for fast validation without any ECC overhead.
Make the `From` impls conform to our convention.

Refactor only, no logic changes.
We do not need to expose the hex error types in our public API, doing so
is just asking for a headache later on.

We add duplicate types to `bitcoin` and `hashes` to achieve the same
thing in each crate.

Fully encapsulate all `hex` module errors.
We don't currently use the `OperationError` type, remove it.
We need this error type if we are to move absolute/relative `Height` and
`Time` types to `units`.

Move the `ParseIntError` type to the `units` crate. Add re-expots to
rust-bitcoin including a public re-export of the whole `units crate.

Remove `parse` module because it was private, keep the `errors`
re-export but deprecate it because it was public.
The `relative` module has a single general error type, we are moving
away from this style to specific error types.

Split the `relative::Error` up into three error structs.

I forget the policy on public inner fields.
Move the bitcoin relative and absolute height and time types over to the
units crate.

This patch introduces a regression because it removes the ability to
parse height or time types from hex. This is because the `hex` crate is
being actively worked on to handle the prefix stuff that is currently in
`bitcoin::string` and which was used by the height and time types.
Everything is now in place to move these two types across to units.
Create an empty crate that will hold primitive types used by the
`rust-bitcoin` ecosystem.
@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 test doc C-primitives labels Feb 14, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 7896620507

Details

  • -218 of 368 (40.76%) changed or added relevant lines in 24 files are covered.
  • 288 unchanged lines in 21 files lost coverage.
  • Overall coverage decreased (-0.8%) to 83.248%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bitcoin/src/blockdata/script/witness_version.rs 0 1 0.0%
bitcoin/src/blockdata/witness.rs 5 7 71.43%
bitcoin/src/taproot/mod.rs 0 2 0.0%
bitcoin/src/taproot/serialized_signature.rs 0 2 0.0%
bitcoin/src/crypto/ecdsa.rs 1 4 25.0%
hashes/src/error.rs 0 3 0.0%
hashes/src/sha256.rs 0 3 0.0%
bitcoin/src/blockdata/transaction.rs 12 16 75.0%
bitcoin/src/consensus/serde.rs 1 5 20.0%
primitives/src/locktime/relative.rs 10 14 71.43%
Files with Coverage Reduction New Missed Lines %
bitcoin/src/bip32.rs 4 88.78%
bitcoin/src/consensus/serde.rs 4 42.75%
hashes/src/util.rs 4 88.44%
hashes/src/hash160.rs 5 93.83%
hashes/src/ripemd160.rs 5 97.94%
hashes/src/sha1.rs 5 93.96%
hashes/src/sha256d.rs 5 90.91%
hashes/src/sha256.rs 5 84.22%
hashes/src/sha512_256.rs 5 91.58%
hashes/src/sha512.rs 5 97.15%
Totals Coverage Status
Change from base Build 7892264908: -0.8%
Covered Lines: 19410
Relevant Lines: 23316

💛 - Coveralls

@tcharding
Copy link
Member Author

tcharding commented Feb 14, 2024

Just a demo to see if we have the current units crate correct. This is for after the next rust-bitcoin release.

Re-done on top of #2477, was pretty easy to do. The only complexity was the FromHexStr. I note that I have got the code wrong for parsing hex strings, here and in another place I wrote today. Flagging here because I don't know which of the branch I was working on has it. @Kixunil you will definitely notice it :) The problem is that my code can't parse AB in requires 000000AB.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 15, 2024

The crate is supposed to contain blockdata but I don't see it moved.

@tcharding
Copy link
Member Author

I just did locktimes as a quick and dirty check that it would work. But yes there are loads more things, I can do more if it adds value to do it now, probably need to do something that depends on weight and fee rate once those are in units.

@Kixunil
Copy link
Collaborator

Kixunil commented Feb 15, 2024

Sure wait for other changes.

@tcharding tcharding closed this May 15, 2024
@tcharding tcharding deleted the 04-27-primitives branch May 15, 2024 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate C-primitives C-units PRs modifying the units crate doc test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants