Merge rust-bitcoin/rust-bitcoin#1239: Introduce `bitcoin-internals` crate

834bbf461f Introduce bitcoin-internals crate (Tobin C. Harding)
12c5fb042e Refactor import statements (Tobin C. Harding)
022730bd8d Add a workspace to the top level directory. (Tobin C. Harding)
a59e0544ec Remove trailing whitespace (Tobin C. Harding)

Pull request description:

  Add a new crate `bitcoin-internals` to be used for internal code needed by multiple soon-to-be-created crates. Add the `write_err` macro to `bitcoin-internals`, nothing else.

  ### Notes

  This PR uses a `path` dependency which means `rust-bitcoin` cannot be released in its current state, will need to be changed once we release the `bitcoin-internals` crate on `crates.io`.

  Please note also the testing strategy imposed by this PR - each new crate to have its own `contrib/test.sh` run from the top level `test.sh`.

  Please excuse the mid-series import statement refactor, I had a moment of weakness and got uncontrollably triggered by the messy imports in one file, I managed to keep myself together on the others :)

ACKs for top commit:
  apoelstra:
    ACK 834bbf461f
  Kixunil:
    ACK 834bbf461f

Tree-SHA512: 327cced077b6d8bf6b4407a930ff8c0a6a8488256453cab168fc0d172ebb03976f8e64ff26a9b178f856c2ffa4342b5ef1a2c6eabcadf9bfa3cf787157a1d1ec
This commit is contained in:
Andrew Poelstra 2022-09-13 13:22:58 +00:00
commit 4e85cd0065
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
80 changed files with 326 additions and 227 deletions

View File

@ -1,62 +1,3 @@
[package] [workspace]
name = "bitcoin" members = ["bitcoin", "internals"]
version = "0.29.0" exclude = ["embedded", "fuzz"]
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bitcoin/"
repository = "https://github.com/rust-bitcoin/rust-bitcoin/"
documentation = "https://docs.rs/bitcoin/"
description = "General purpose library for using and interoperating with Bitcoin and other cryptocurrencies."
keywords = [ "crypto", "bitcoin" ]
readme = "README.md"
exclude = ["./test_data"]
edition = "2018"
# Please don't forget to add relevant features to docs.rs below
[features]
default = [ "std", "secp-recovery" ]
rand = ["secp256k1/rand-std"]
serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"]
secp-lowmemory = ["secp256k1/lowmemory"]
secp-recovery = ["secp256k1/recovery"]
# At least one of std, no-std must be enabled.
#
# The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default.
# Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict.
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std"]
no-std = ["hashbrown", "core2/alloc", "bitcoin_hashes/alloc", "secp256k1/alloc"]
[package.metadata.docs.rs]
features = [ "std", "secp-recovery", "base64", "rand", "serde", "bitcoinconsensus" ]
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
bech32 = { version = "0.9.0", default-features = false }
bitcoin_hashes = { version = "0.11.0", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] }
core2 = { version = "0.3.0", optional = true, default-features = false }
base64 = { version = "0.13.0", optional = true }
bitcoinconsensus = { version = "0.20.2-0.5.0", optional = true }
# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1", default-features = false, features = [ "derive", "alloc" ], optional = true }
hashbrown = { version = "0.8", optional = true }
[dev-dependencies]
serde_json = "<1.0.45"
serde_test = "1"
secp256k1 = { version = "0.24.0", features = [ "recovery", "rand-std" ] }
bincode = "1.3.1"
[[example]]
name = "bip32"
[[example]]
name = "handshake"
required-features = ["std"]
[[example]]
name = "ecdsa-psbt"
required-features = ["std", "bitcoinconsensus"]

63
bitcoin/Cargo.toml Normal file
View File

@ -0,0 +1,63 @@
[package]
name = "bitcoin"
version = "0.29.0"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bitcoin/"
repository = "https://github.com/rust-bitcoin/rust-bitcoin/"
documentation = "https://docs.rs/bitcoin/"
description = "General purpose library for using and interoperating with Bitcoin and other cryptocurrencies."
keywords = [ "crypto", "bitcoin" ]
readme = "README.md"
exclude = ["./test_data"]
edition = "2018"
# Please don't forget to add relevant features to docs.rs below
[features]
default = [ "std", "secp-recovery" ]
rand = ["secp256k1/rand-std"]
serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"]
secp-lowmemory = ["secp256k1/lowmemory"]
secp-recovery = ["secp256k1/recovery"]
# At least one of std, no-std must be enabled.
#
# The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default.
# Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict.
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std", "bitcoin-internals/std"]
no-std = ["hashbrown", "core2/alloc", "bitcoin_hashes/alloc", "secp256k1/alloc"]
[package.metadata.docs.rs]
features = [ "std", "secp-recovery", "base64", "rand", "serde", "bitcoinconsensus" ]
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
bitcoin-internals = { path = "../internals" }
bech32 = { version = "0.9.0", default-features = false }
bitcoin_hashes = { version = "0.11.0", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] }
core2 = { version = "0.3.0", optional = true, default-features = false }
base64 = { version = "0.13.0", optional = true }
bitcoinconsensus = { version = "0.20.2-0.5.0", optional = true }
# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1", default-features = false, features = [ "derive", "alloc" ], optional = true }
hashbrown = { version = "0.8", optional = true }
[dev-dependencies]
serde_json = "<1.0.45"
serde_test = "1"
secp256k1 = { version = "0.24.0", features = [ "recovery", "rand-std" ] }
bincode = "1.3.1"
[[example]]
name = "bip32"
[[example]]
name = "handshake"
required-features = ["std"]
[[example]]
name = "ecdsa-psbt"
required-features = ["std", "bitcoinconsensus"]

127
bitcoin/contrib/test.sh Executable file
View File

@ -0,0 +1,127 @@
#!/bin/sh
set -ex
FEATURES="base64 bitcoinconsensus serde rand secp-recovery"
if [ "$DO_COV" = true ]
then
export RUSTFLAGS="-C link-dead-code"
fi
cargo --version
rustc --version
# Work out if we are using a nightly toolchain.
NIGHTLY=false
if cargo --version | grep nightly; then
NIGHTLY=true
fi
# We should not have any duplicate dependencies. This catches mistakes made upgrading dependencies
# in one crate and not in another (e.g. upgrade bitcoin_hashes in bitcoin but not in secp).
cargo update -p serde --precise 1.0.142
cargo update -p serde_test --precise 1.0.142
cargo update -p serde_derive --precise 1.0.142
duplicate_dependencies=$(cargo tree --target=all --all-features --duplicates | wc -l)
if [ "$duplicate_dependencies" -ne 0 ]; then
echo "Dependency tree is broken, contains duplicates"
cargo tree --target=all --all-features --duplicates
exit 1
fi
if [ "$DO_LINT" = true ]
then
cargo clippy --all-features --all-targets -- -D warnings
cargo clippy --example bip32 -- -D warnings
cargo clippy --example handshake -- -D warnings
cargo clippy --example ecdsa-psbt --features=bitcoinconsensus -- -D warnings
fi
echo "********* Testing std *************"
# Test without any features other than std first
cargo test --verbose --no-default-features --features="std"
echo "********* Testing default *************"
# Then test with the default features
cargo test --verbose
if [ "$DO_NO_STD" = true ]
then
echo "********* Testing no-std build *************"
# Build no_std, to make sure that cfg(test) doesn't hide any issues
cargo build --verbose --features="no-std" --no-default-features
# Build std + no_std, to make sure they are not incompatible
cargo build --verbose --features="no-std"
# Test no_std
cargo test --verbose --features="no-std" --no-default-features
# Build all features
cargo build --verbose --features="no-std $FEATURES" --no-default-features
# Build specific features
for feature in ${FEATURES}
do
cargo build --verbose --features="no-std $feature"
done
cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
cargo run --no-default-features --features no-std --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
fi
# Test each feature
for feature in ${FEATURES}
do
echo "********* Testing $feature *************"
cargo test --verbose --features="$feature"
done
cargo run --example ecdsa-psbt --features=bitcoinconsensus
# Build the docs if told to (this only works with the nightly toolchain)
if [ "$DO_DOCS" = true ]; then
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly rustdoc --features="$FEATURES" -- -D rustdoc::broken-intra-doc-links
fi
# Fuzz if told to
if [ "$DO_FUZZ" = true ]
then
(
cd fuzz
cargo test --verbose
./travis-fuzz.sh
)
fi
# Bench if told to, only works with non-stable toolchain (nightly, beta).
if [ "$DO_BENCH" = true ]
then
if [ "$NIGHTLY" = false ]
then
if [ -n "$RUSTUP_TOOLCHAIN" ]
then
echo "RUSTUP_TOOLCHAIN is set to a non-nightly toolchain but DO_BENCH requires a nightly toolchain"
else
echo "DO_BENCH requires a nightly toolchain"
fi
exit 1
fi
RUSTFLAGS='--cfg=bench' cargo bench
fi
# Use as dependency if told to
if [ "$AS_DEPENDENCY" = true ]
then
cargo new dep_test 2> /dev/null # Mute warning about workspace, fixed below.
cd dep_test
echo 'bitcoin = { path = "..", features = ["serde"] }\n\n' >> Cargo.toml
# Adding an empty workspace section excludes this crate from the rust-bitcoin workspace.
echo '[workspace]\n\n' >> Cargo.toml
cargo update -p serde --precise 1.0.142
cargo update -p serde_derive --precise 1.0.142
cargo test --verbose
fi

View File

@ -27,6 +27,7 @@ use core::fmt;
use core::str::FromStr; use core::str::FromStr;
use bech32; use bech32;
use bitcoin_internals::write_err;
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey}; use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
use crate::blockdata::constants::{ use crate::blockdata::constants::{
@ -38,7 +39,7 @@ use crate::blockdata::{opcodes, script};
use crate::error::ParseIntError; use crate::error::ParseIntError;
use crate::hash_types::{PubkeyHash, ScriptHash}; use crate::hash_types::{PubkeyHash, ScriptHash};
use crate::hashes::{sha256, Hash, HashEngine}; use crate::hashes::{sha256, Hash, HashEngine};
use crate::internal_macros::{serde_string_impl, write_err}; use crate::internal_macros::serde_string_impl;
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::prelude::*; use crate::prelude::*;
use crate::util::base58; use crate::util::base58;

View File

@ -42,6 +42,8 @@
use core::cmp::{self, Ordering}; use core::cmp::{self, Ordering};
use core::fmt::{self, Display, Formatter}; use core::fmt::{self, Display, Formatter};
use bitcoin_internals::write_err;
use crate::blockdata::block::Block; use crate::blockdata::block::Block;
use crate::blockdata::script::Script; use crate::blockdata::script::Script;
use crate::blockdata::transaction::OutPoint; use crate::blockdata::transaction::OutPoint;
@ -49,7 +51,6 @@ use crate::consensus::encode::VarInt;
use crate::consensus::{Decodable, Encodable}; use crate::consensus::{Decodable, Encodable};
use crate::hash_types::{BlockHash, FilterHash, FilterHeader}; use crate::hash_types::{BlockHash, FilterHash, FilterHeader};
use crate::hashes::{siphash24, Hash}; use crate::hashes::{siphash24, Hash};
use crate::internal_macros::write_err;
use crate::io; use crate::io;
use crate::prelude::*; use crate::prelude::*;
use crate::util::endian; use crate::util::endian;
@ -273,7 +274,8 @@ impl GcsFilterReader {
let reader = &mut decoder; let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>(); let mut mapped =
query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort // sort
mapped.sort_unstable(); mapped.sort_unstable();
if mapped.is_empty() { if mapped.is_empty() {
@ -317,7 +319,8 @@ impl GcsFilterReader {
let reader = &mut decoder; let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>(); let mut mapped =
query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort // sort
mapped.sort_unstable(); mapped.sort_unstable();
mapped.dedup(); mapped.dedup();

View File

@ -179,7 +179,7 @@ impl BlockVersion {
/// BIP-9 soft fork signal bits mask. /// BIP-9 soft fork signal bits mask.
const VERSION_BITS_MASK: u32 = 0x1FFF_FFFF; const VERSION_BITS_MASK: u32 = 0x1FFF_FFFF;
/// 32bit value starting with `001` to use version bits. /// 32bit value starting with `001` to use version bits.
/// ///
/// The value has the top three bits `001` which enables the use of version bits to signal for soft forks. /// The value has the top three bits `001` which enables the use of version bits to signal for soft forks.
const USE_VERSION_BITS: u32 = 0x2000_0000; const USE_VERSION_BITS: u32 = 0x2000_0000;

View File

@ -11,14 +11,14 @@ use core::{mem, fmt};
use core::cmp::{PartialOrd, Ordering}; use core::cmp::{PartialOrd, Ordering};
use core::convert::TryFrom; use core::convert::TryFrom;
use core::str::FromStr; use core::str::FromStr;
use crate::error::ParseIntError;
use crate::parse; use bitcoin_internals::write_err;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::error::ParseIntError;
use crate::io::{self, Read, Write}; use crate::io::{self, Read, Write};
use crate::prelude::*; use crate::prelude::*;
use crate::internal_macros::write_err; use crate::parse::{self, impl_parse_str_through_int};
use crate::parse::impl_parse_str_through_int;
/// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]). /// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]).
/// ///

View File

@ -20,7 +20,7 @@ use core::{fmt, default::Default};
use core::ops::Index; use core::ops::Index;
use crate::internal_macros::debug_from_display; use crate::internal_macros::debug_from_display;
#[cfg(feature = "bitcoinconsensus")] #[cfg(feature = "bitcoinconsensus")]
use crate::internal_macros::write_err; use bitcoin_internals::write_err;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;

View File

@ -18,6 +18,8 @@ use crate::io;
use core::{fmt, str, default::Default}; use core::{fmt, str, default::Default};
use core::convert::TryFrom; use core::convert::TryFrom;
use bitcoin_internals::write_err;
use crate::hashes::{self, Hash, sha256d}; use crate::hashes::{self, Hash, sha256d};
use crate::hashes::hex::FromHex; use crate::hashes::hex::FromHex;
@ -30,7 +32,7 @@ use crate::blockdata::locktime::relative;
use crate::consensus::{encode, Decodable, Encodable}; use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{Sighash, Txid, Wtxid}; use crate::hash_types::{Sighash, Txid, Wtxid};
use crate::VarInt; use crate::VarInt;
use crate::internal_macros::{impl_consensus_encoding, serde_struct_human_string_impl, write_err}; use crate::internal_macros::{impl_consensus_encoding, serde_struct_human_string_impl};
use crate::parse::impl_parse_str_through_int; use crate::parse::impl_parse_str_through_int;
#[cfg(doc)] #[cfg(doc)]

View File

@ -20,9 +20,10 @@ use crate::prelude::*;
use core::{fmt, mem, u32, convert::From}; use core::{fmt, mem, u32, convert::From};
use bitcoin_internals::write_err;
use crate::hashes::{sha256d, Hash, sha256}; use crate::hashes::{sha256d, Hash, sha256};
use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader}; use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader};
use crate::internal_macros::write_err;
use crate::io::{self, Cursor, Read}; use crate::io::{self, Cursor, Read};
use crate::util::endian; use crate::util::endian;

View File

@ -626,26 +626,6 @@ macro_rules! user_enum {
} }
pub(crate) use user_enum; pub(crate) use user_enum;
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
/// because `e.source()` is only available in std builds, without this macro the error source is
/// lost for no-std builds.
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}
pub(crate) use write_err;
/// Asserts a boolean expression at compile time. /// Asserts a boolean expression at compile time.
macro_rules! const_assert { macro_rules! const_assert {
($x:expr) => {{ ($x:expr) => {{

View File

@ -2,8 +2,9 @@ use core::convert::TryFrom;
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
use bitcoin_internals::write_err;
use crate::error::impl_std_error; use crate::error::impl_std_error;
use crate::internal_macros::write_err;
use crate::prelude::*; use crate::prelude::*;
/// Error with rich context returned when a string can't be parsed as an integer. /// Error with rich context returned when a string can't be parsed as an integer.

View File

@ -11,11 +11,11 @@ use crate::prelude::*;
use core::{fmt, str, iter, slice}; use core::{fmt, str, iter, slice};
use bitcoin_internals::write_err;
use crate::hashes::{sha256d, Hash, hex}; use crate::hashes::{sha256d, Hash, hex};
use secp256k1; use secp256k1;
use crate::util::{endian, key}; use crate::util::{endian, key};
use crate::internal_macros::write_err;
/// An error that might occur during base58 decoding /// An error that might occur during base58 decoding
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]

View File

@ -14,6 +14,7 @@ use core::{fmt, str::FromStr, default::Default};
use core::ops::Index; use core::ops::Index;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
use bitcoin_internals::write_err;
use crate::hash_types::XpubIdentifier; use crate::hash_types::XpubIdentifier;
use crate::hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine, hex}; use crate::hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine, hex};
use secp256k1::{self, Secp256k1, XOnlyPublicKey}; use secp256k1::{self, Secp256k1, XOnlyPublicKey};
@ -21,7 +22,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::util::{base58, endian, key}; use crate::util::{base58, endian, key};
use crate::util::key::{PublicKey, PrivateKey, KeyPair}; use crate::util::key::{PublicKey, PrivateKey, KeyPair};
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl, write_err}; use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl};
/// A chain code /// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View File

@ -8,11 +8,11 @@
use core::str::FromStr; use core::str::FromStr;
use core::{fmt, iter}; use core::{fmt, iter};
use bitcoin_internals::write_err;
use secp256k1; use secp256k1;
use crate::prelude::*; use crate::prelude::*;
use crate::hashes::hex::{self, FromHex}; use crate::hashes::hex::{self, FromHex};
use crate::internal_macros::write_err;
use crate::util::sighash::{EcdsaSighashType, NonStandardSighashType}; use crate::util::sighash::{EcdsaSighashType, NonStandardSighashType};
/// An ECDSA signature with the corresponding hash type. /// An ECDSA signature with the corresponding hash type.

View File

@ -11,6 +11,7 @@ use crate::prelude::*;
use core::{ops, str::FromStr}; use core::{ops, str::FromStr};
use core::fmt::{self, Write}; use core::fmt::{self, Write};
use bitcoin_internals::write_err;
pub use secp256k1::{self, Secp256k1, XOnlyPublicKey, KeyPair}; pub use secp256k1::{self, Secp256k1, XOnlyPublicKey, KeyPair};
use crate::io; use crate::io;
@ -18,7 +19,6 @@ use crate::network::constants::Network;
use crate::hashes::{Hash, hash160, hex, hex::FromHex}; use crate::hashes::{Hash, hash160, hex, hex::FromHex};
use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::util::base58; use crate::util::base58;
use crate::internal_macros::write_err;
/// A key-related error. /// A key-related error.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -27,13 +27,13 @@ mod message_signing {
use core::fmt; use core::fmt;
use bitcoin_internals::write_err;
use crate::hashes::sha256d; use crate::hashes::sha256d;
use secp256k1; use secp256k1;
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
use crate::util::key::PublicKey; use crate::util::key::PublicKey;
use crate::address::{Address, AddressType}; use crate::address::{Address, AddressType};
use crate::internal_macros::write_err;
/// An error used for dealing with Bitcoin Signed Messages. /// An error used for dealing with Bitcoin Signed Messages.
#[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))] #[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))]

View File

@ -28,8 +28,9 @@ use crate::prelude::*;
use crate::io; use crate::io;
use core::fmt; use core::fmt;
use bitcoin_internals::write_err;
use crate::consensus::encode; use crate::consensus::encode;
use crate::internal_macros::write_err;
/// A trait which allows numbers to act as fixed-size bit arrays /// A trait which allows numbers to act as fixed-size bit arrays
pub trait BitArray { pub trait BitArray {

View File

@ -4,13 +4,14 @@ use crate::prelude::*;
use core::fmt; use core::fmt;
use bitcoin_internals::write_err;
use crate::blockdata::transaction::Transaction; use crate::blockdata::transaction::Transaction;
use crate::consensus::encode; use crate::consensus::encode;
use crate::util::psbt::raw; use crate::util::psbt::raw;
use crate::hashes; use crate::hashes;
use crate::util::bip32::ExtendedPubKey; use crate::util::bip32::ExtendedPubKey;
use crate::internal_macros::write_err;
/// Enum for marking psbt hash error. /// Enum for marking psbt hash error.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -207,7 +207,7 @@ mod display_from_str {
use core::str::FromStr; use core::str::FromStr;
use crate::consensus::encode::{Error, self}; use crate::consensus::encode::{Error, self};
use base64::display::Base64Display; use base64::display::Base64Display;
use crate::internal_macros::write_err; use bitcoin_internals::write_err;
/// Error encountered during PSBT decoding from Base64 string. /// Error encountered during PSBT decoding from Base64 string.
#[derive(Debug)] #[derive(Debug)]

View File

@ -9,12 +9,13 @@
use core::fmt; use core::fmt;
use bitcoin_internals::write_err;
use crate::prelude::*; use crate::prelude::*;
use secp256k1::{self, Secp256k1, Verification, constants}; use secp256k1::{self, Secp256k1, Verification, constants};
use crate::util::taproot::{TapBranchHash, TapTweakHash}; use crate::util::taproot::{TapBranchHash, TapTweakHash};
use crate::SchnorrSighashType; use crate::SchnorrSighashType;
use crate::internal_macros::write_err;
/// Deprecated re-export of [`secp256k1::XOnlyPublicKey`] /// Deprecated re-export of [`secp256k1::XOnlyPublicKey`]
#[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")] #[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")]

View File

@ -7,6 +7,7 @@
use crate::prelude::*; use crate::prelude::*;
use crate::io; use crate::io;
use bitcoin_internals::write_err;
use secp256k1::{self, Secp256k1, Scalar}; use secp256k1::{self, Secp256k1, Scalar};
use core::convert::TryFrom; use core::convert::TryFrom;
@ -17,7 +18,6 @@ use crate::hashes::{sha256, sha256t_hash_newtype, Hash, HashEngine};
use crate::schnorr::{TweakedPublicKey, UntweakedPublicKey, TapTweak}; use crate::schnorr::{TweakedPublicKey, UntweakedPublicKey, TapTweak};
use crate::util::key::XOnlyPublicKey; use crate::util::key::XOnlyPublicKey;
use crate::Script; use crate::Script;
use crate::internal_macros::write_err;
use crate::consensus::Encodable; use crate::consensus::Encodable;
/// The SHA-256 midstate value for the TapLeaf hash. /// The SHA-256 midstate value for the TapLeaf hash.

View File

@ -2,123 +2,14 @@
set -ex set -ex
FEATURES="base64 bitcoinconsensus serde rand secp-recovery" CRATES="bitcoin"
if [ "$DO_COV" = true ] for crate in ${CRATES}
then
export RUSTFLAGS="-C link-dead-code"
fi
cargo --version
rustc --version
# Work out if we are using a nightly toolchain.
NIGHTLY=false
if cargo --version | grep nightly; then
NIGHTLY=true
fi
# We should not have any duplicate dependencies. This catches mistakes made upgrading dependencies
# in one crate and not in another (e.g. upgrade bitcoin_hashes in bitcoin but not in secp).
cargo update -p serde --precise 1.0.142
cargo update -p serde_test --precise 1.0.142
cargo update -p serde_derive --precise 1.0.142
duplicate_dependencies=$(cargo tree --target=all --all-features --duplicates | wc -l)
if [ "$duplicate_dependencies" -ne 0 ]; then
echo "Dependency tree is broken, contains duplicates"
cargo tree --target=all --all-features --duplicates
exit 1
fi
if [ "$DO_LINT" = true ]
then
cargo clippy --all-features --all-targets -- -D warnings
cargo clippy --example bip32 -- -D warnings
cargo clippy --example handshake -- -D warnings
cargo clippy --example ecdsa-psbt --features=bitcoinconsensus -- -D warnings
fi
echo "********* Testing std *************"
# Test without any features other than std first
cargo test --verbose --no-default-features --features="std"
echo "********* Testing default *************"
# Then test with the default features
cargo test --verbose
if [ "$DO_NO_STD" = true ]
then
echo "********* Testing no-std build *************"
# Build no_std, to make sure that cfg(test) doesn't hide any issues
cargo build --verbose --features="no-std" --no-default-features
# Build std + no_std, to make sure they are not incompatible
cargo build --verbose --features="no-std"
# Test no_std
cargo test --verbose --features="no-std" --no-default-features
# Build all features
cargo build --verbose --features="no-std $FEATURES" --no-default-features
# Build specific features
for feature in ${FEATURES}
do
cargo build --verbose --features="no-std $feature"
done
cargo run --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
cargo run --no-default-features --features no-std --example bip32 7934c09359b234e076b9fa5a1abfd38e3dc2a9939745b7cc3c22a48d831d14bd
fi
# Test each feature
for feature in ${FEATURES}
do do
echo "********* Testing $feature *************" (
cargo test --verbose --features="$feature" cd "$crate"
./contrib/test.sh
)
done done
cargo run --example ecdsa-psbt --features=bitcoinconsensus exit 0
# Build the docs if told to (this only works with the nightly toolchain)
if [ "$DO_DOCS" = true ]; then
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly rustdoc --features="$FEATURES" -- -D rustdoc::broken-intra-doc-links
fi
# Fuzz if told to
if [ "$DO_FUZZ" = true ]
then
(
cd fuzz
cargo test --verbose
./travis-fuzz.sh
)
fi
# Bench if told to, only works with non-stable toolchain (nightly, beta).
if [ "$DO_BENCH" = true ]
then
if [ "$NIGHTLY" = false ]
then
if [ -n "$RUSTUP_TOOLCHAIN" ]
then
echo "RUSTUP_TOOLCHAIN is set to a non-nightly toolchain but DO_BENCH requires a nightly toolchain"
else
echo "DO_BENCH requires a nightly toolchain"
fi
exit 1
fi
RUSTFLAGS='--cfg=bench' cargo bench
fi
# Use as dependency if told to
if [ "$AS_DEPENDENCY" = true ]
then
cargo new dep_test
cd dep_test
echo 'bitcoin = { path = "..", features = ["serde"] }' >> Cargo.toml
cargo update -p serde --precise 1.0.142
cargo update -p serde_derive --precise 1.0.142
cargo test --verbose
fi

View File

@ -11,7 +11,7 @@ cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3" cortex-m-semihosting = "0.3.3"
panic-halt = "0.2.0" panic-halt = "0.2.0"
alloc-cortex-m = "0.4.1" alloc-cortex-m = "0.4.1"
bitcoin = { path="../", default-features = false, features = ["no-std", "secp-lowmemory"] } bitcoin = { path="../bitcoin", default-features = false, features = ["no-std", "secp-lowmemory"] }
[[bin]] [[bin]]
name = "embedded" name = "embedded"

View File

@ -14,7 +14,7 @@ honggfuzz_fuzz = ["honggfuzz"]
[dependencies] [dependencies]
honggfuzz = { version = "0.5", optional = true, default-features = false } honggfuzz = { version = "0.5", optional = true, default-features = false }
afl = { version = "0.4", optional = true } afl = { version = "0.4", optional = true }
bitcoin = { path = ".." } bitcoin = { path = "../bitcoin" }
# Prevent this from interfering with workspaces # Prevent this from interfering with workspaces
[workspace] [workspace]

25
internals/Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "bitcoin-internals"
version = "0.1.0"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>", "The Rust Bitcoin developers"]
license = "CC0-1.0"
repository = "https://github.com/rust-bitcoin/rust-bitcoin/"
documentation = "https://docs.rs/bitcoin-internals"
description = "Internal types and macros used by rust-bitcoin ecosystem"
categories = ["cryptography::cryptocurrencies"]
keywords = ["internal"]
readme = "README.md"
edition = "2018"
# Please don't forget to add relevant features to docs.rs below.
[features]
default = []
std = []
[package.metadata.docs.rs]
features = ["std"]
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
[dev-dependencies]

7
internals/README.md Normal file
View File

@ -0,0 +1,7 @@
Rust Bitcoin Internals
======================
This crate is only meant to be used internally by crates in the
[rust-bitcoin](https://github.com/rust-bitcoin) ecosystem.
This crate will never be stabilized, depend on it at your own risk.

29
internals/src/error.rs Normal file
View File

@ -0,0 +1,29 @@
// Written by the Rust Bitcoin developers.
// SPDX-License-Identifier: CC0-1.0
//! # Error
//!
//! Error handling macros and helpers.
//!
/// Formats error.
///
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
/// `e.source()` is only available in std builds, without this macro the error source is lost for
/// no-std builds.
#[macro_export]
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}

24
internals/src/lib.rs Normal file
View File

@ -0,0 +1,24 @@
// Written by the Rust Bitcoin developers.
// SPDX-License-Identifier: CC0-1.0
//! # Rust Bitcoin Internal
//!
//! This crate is only meant to be used internally by crates in the
//! [rust-bitcoin](https://github.com/rust-bitcoin) ecosystem.
//!
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_cfg))]
// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]
#![deny(unused_must_use)]
pub mod error;

View File

@ -1,9 +1,9 @@
# Eventually this shoud be: ignore = [] # Eventually this shoud be: ignore = []
ignore = [ ignore = [
"src/blockdata", "bitcoin/src/blockdata",
"src/consensus", "bitcoin/src/consensus",
"src/network", "bitcoin/src/network",
"src/util", "bitcoin/src/util",
] ]
hard_tabs = false hard_tabs = false