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

Fully encapsulate bitcoinconsensus #2278

Merged

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Dec 11, 2023

The bitcoinconsensus crate is not fully under our control because it exposes code from Core, so we cannot guarantee its stability across versions. To make our semver compliance easier we can fully encapsulate the bitcoinconsensus crate so it does not appear in our public API.

Please note that with this applied:

  • The bitcoinconsenus crate is no longer exported at the crate root
  • No bitcoinconsensus types appear in our public API

@tcharding
Copy link
Member Author

I tested this using the following script

#!/usr/bin/env bash
#
# Checks the public API of crates, exits with non-zero if there are currently
# changes to the public API not already committed to in the various api/*.txt
# files.

set -e

export RUSTDOCFLAGS='-A rustdoc::broken-intra-doc-links'
REPO_DIR=$(git rev-parse --show-toplevel)
API_DIR="$REPO_DIR/api"
CMD="cargo +nightly public-api --simplified"

# cargo public-api uses nightly so the toolchain must be available.
if ! cargo +nightly --version > /dev/null; then
    echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
    exit 1
fi

pushd "$REPO_DIR/bitcoin" > /dev/null
$CMD | sort --unique > "$API_DIR/bitcoin/api-default-features.txt"
$CMD --features=all-stable-features | sort --unique  > "$API_DIR/bitcoin/api-all-stable-features.txt"
$CMD --all-features | sort --unique > "$API_DIR/bitcoin/api-all-features.txt"
popd > /dev/null

pushd "$REPO_DIR/hashes" > /dev/null
$CMD --no-default-features | sort --unique > "$API_DIR/hashes/api-no-features.txt"
$CMD | sort --unique > "$API_DIR/hashes/api-default-features.txt"
$CMD --no-default-features --features=alloc | sort --unique > "$API_DIR/hashes/api-alloc.txt"
$CMD --all-features | sort --unique > "$API_DIR/hashes/api-all-features.txt"
popd > /dev/null

pushd "$REPO_DIR" > /dev/null
if [[ $(git status --porcelain api) ]]; then
    echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
else
    echo "No changes to the current public API"
fi
popd > /dev/null

Grepping the output files we get:

  • grep bitcoinconsensus api-all-stable-features.txt returns blank
  • grep bitcoinconsensus api-all-features.txt
    pub extern crate bitcoin::bitcoinconsensus

apoelstra
apoelstra previously approved these changes Dec 11, 2023
Copy link
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 135d143

@tcharding
Copy link
Member Author

Needs #2279

@tcharding tcharding force-pushed the 12-12-encapsulate-bitcoinconsensus branch from 135d143 to 2795b07 Compare December 12, 2023 00:47
@tcharding
Copy link
Member Author

Rebased to pick up #2279, no other changes.

@apoelstra
Copy link
Member

Looks good to me! Would like @Kixunil's opinion on this feature organization to enable gated pub extern crates.

apoelstra
apoelstra previously approved these changes Dec 12, 2023
Copy link
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 2795b07

@junderw
Copy link
Contributor

junderw commented Dec 12, 2023

Out of curiosity, what are some of the differences between why tokio also requires RUSTFLAGS? (Compared to this PR)

https://docs.rs/tokio/latest/tokio/#unstable-features

@tcharding
Copy link
Member Author

My guess is so that --all-features is stable? Also using --cfg instead of a cargo feature make opting into unstable features more explicit, like we do for bench marks. Perhaps we should do this too?

@junderw
Copy link
Contributor

junderw commented Dec 12, 2023

My guess is so that --all-features is stable? Also using --cfg instead of a cargo feature make opting into unstable features more explicit, like we do for bench marks. Perhaps we should do this too?

Yeah that was my thought process too.

@Kixunil
Copy link
Collaborator

Kixunil commented Dec 12, 2023

They explain it here: https://internals.rust-lang.org/t/feature-request-unstable-opt-in-non-transitive-crate-features/16193#why-not-a-crate-feature-2

Concept ACK having an unstable feature (I had some other ideas too) however, I think the reason for not using cargo features is sound so I'd prefer that way and secondly, I think it's very little value here since people can explicitly depend on a specific version of bitcoinconensus and not have to deal with our flag at all. Thus I think simply not exporting is better here.

@apoelstra
Copy link
Member

Yeah, I like their reasoning. Let's use a cfg flag rather than a feature.

The `bitcoinconsensus` crate is not fully under our control because it
exposes code from Core, so we cannot guarantee its stability across
versions. To make our semver compliance easier we can fully encapsulate
the `bitcoinconsensus` crate so it does not appear in our public API.
However, it is useful to have the crate itself exported, here we add an
"unstable" feature and only publicly export the `bitcoinconsensus` crate
if the "unstable" feature is enabled.
@tcharding
Copy link
Member Author

Flagging that this PR does not add any feature, cfg or cargo, because it now just does not re-export the bitcoinconsenus crate. Requires ack from @apoelstra please (who was initially against removing the re-export).

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.

ACK 43b1ed1

/// Bitcoin's libbitcoinconsensus with Rust binding.
pub extern crate bitcoinconsensus;
#[cfg(feature = "bitcoinconsensus")]
extern crate bitcoinconsensus;
Copy link
Collaborator

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 is needed at all now.

Copy link
Member

Choose a reason for hiding this comment

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

I had the same thought, but didn't check. (In Rust post-2018 non-pub extern crates are no-ops, I believe.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

pub are not no-ops (they are reexports) but non-pub are.

@apoelstra
Copy link
Member

Flagging that this PR does not add any feature, cfg or cargo, because it now just does not re-export the bitcoinconsenus crate. Requires ack from @apoelstra please (who was initially against removing the re-export).

This is fine, though I'd like to revisit it at some point.

Copy link
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 43b1ed1

@apoelstra apoelstra merged commit 6b9f927 into rust-bitcoin:master Dec 13, 2023
29 checks passed
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Dec 13, 2023
In rust-bitcoin#2278 I mistakenly left in the `use extern crate` after removing the
`pub` from it - while not incorrect it is unnecessary.
apoelstra added a commit that referenced this pull request Dec 13, 2023
0d9c7ad Remove unnecessary private extern crate (Tobin C. Harding)

Pull request description:

  In #2278 I mistakenly left in the `use extern crate` after removing the `pub` from it - while not incorrect it is unnecessary.

ACKs for top commit:
  apoelstra:
    ACK 0d9c7ad

Tree-SHA512: 60a2638feda2ca5aea234de74fc751c8c140a47946f20cac95d804099f0ba7c78d8eef51912446024e2aafa97008bb93334c0fd3e97267a7f91c4ab86ffcd908
@tcharding tcharding deleted the 12-12-encapsulate-bitcoinconsensus branch December 14, 2023 21:36
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.

None yet

4 participants