Skip to content

refactor: centralize dependencies and feature-gate stacks-common #6238

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@ members = [

# Dependencies we want to keep the same between workspace members
[workspace.dependencies]
ed25519-dalek = { version = "2.1.1", features = ["serde", "rand_core"] }
ed25519-dalek = { version = "2.1.1", default-features = false }
hashbrown = { version = "0.15.2", features = ["serde"] }
lazy_static = "1.4.0"
rand_core = "0.6.4"
rand = "0.8"
rand_chacha = "0.3.1"
tikv-jemallocator = "0.5.4"
serde = "1"
serde_derive = "1"
serde_json = { version = "1.0", features = ["arbitrary_precision", "unbounded_depth"] }
slog = { version = "2.5.2", features = [ "max_level_trace" ] }
rusqlite = { version = "0.31.0", features = ["blob", "serde_json", "i128_blob", "bundled", "trace"] }
tikv-jemallocator = "0.5.4"
thiserror = "1.0.65"
toml = "0.5.6"

# Use a bit more than default optimization for
# dev builds to speed up test execution
# dev builds to speed up test execution
[profile.dev]
opt-level = 1

# Use release-level optimization for dependencies
# This slows down "first" builds on development environments,
# but won't impact subsequent builds.
# but won't impact subsequent builds.
[profile.dev.package."*"]
opt-level = 3

Expand Down
86 changes: 45 additions & 41 deletions stacks-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,69 +30,73 @@ name = "stacks_common"
path = "./src/libcommon.rs"

[dependencies]
rand = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1"
sha3 = "0.10.1"
ripemd = "0.1.1"
lazy_static = "1.4.0"
slog = { version = "2.5.2", features = ["max_level_trace"] }
slog-term = "2.6.0"
slog-json = { version = "2.3.0", optional = true }
chrono = "0.4.19"
chrono = { version = "0.4.41", default-features = false, features = ["clock"] }
curve25519-dalek = { version = "4.1.3", default-features = false, features = ["serde"] }
ed25519-dalek = { workspace = true }
hashbrown = { workspace = true }
rusqlite = { workspace = true, optional = true }
lazy_static = { workspace = true }
ripemd = { version = "0.1.1", default-features = false }
serde = { workspace = true , features = ["derive"] }
serde_derive = { workspace = true }
serde_json = { workspace = true }
sha3 = { version = "0.10.1", default-features = false }
slog = { workspace = true }
slog-term = { version = "2.6.0", default-features = false }

# Optional dependencies
getrandom = { version = "0.2", default-features = false, optional = true }
rand = { workspace = true, optional = true }
slog-json = { version = "2.3.0", default-features = false, optional = true }

[target.'cfg(unix)'.dependencies]
nix = "0.23"
nix = {version = "0.23", default-features = false, optional = true}

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = [
"consoleapi",
"handleapi",
"synchapi",
"winbase",
] }

[target.'cfg(windows)'.dev-dependencies]
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }

[dependencies.serde_json]
version = "1.0"
features = ["arbitrary_precision", "unbounded_depth"]

[dependencies.ed25519-dalek]
workspace = true

[dependencies.curve25519-dalek]
version = "4.1.3"
features = ["serde"]
], optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }
secp256k1 = { version = "0.24.3", default-features = false, features = ["std","serde", "recovery"] }
rusqlite = { workspace = true, optional = true }

[target.'cfg(target_family = "wasm")'.dependencies]
libsecp256k1 = { version = "0.7.0" }
libsecp256k1 = { version = "0.7.0", default-features = false, features = ["hmac"] }

[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
sha2 = { version = "0.10", features = ["asm"] }

[target.'cfg(any(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")), any(target_os="windows")))'.dependencies]
sha2 = { version = "0.10" }

[dev-dependencies]
rand_core = { workspace = true }
proptest = "1.6.0"

[target.'cfg(windows)'.dev-dependencies]
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }

[build-dependencies]
toml = { workspace = true }

[features]
default = ["developer-mode"]
default = ["developer-mode", "ctrlc-handler", "rand"]
developer-mode = []
# Enables graceful shutdown handling for Ctrl+C (SIGINT) signals.
# This pulls in the `nix` or `winapi` dependency.
ctrlc-handler = ["dep:nix", "dep:winapi"]
slog_json = ["slog-json"]
rusqlite = ["dep:rusqlite"]
testing = []
rusqlite = ["dep:rusqlite", "rand"]
# Enables the rand module. This flag must be off on deterministic
# platforms such as CosmWasm
rand = ["dep:rand"]
serde = []
testing = ["rand"]
bech32_std = []
bech32_strict = []

[build-dependencies]
toml = "0.5.6"

[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
sha2 = { version = "0.10", features = ["asm"] }

[target.'cfg(any(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")), any(target_os="windows")))'.dependencies]
sha2 = { version = "0.10" }
# Wasm-specific features for easier configuration
wasm-web = ["rand", "getrandom/js", "libsecp256k1/static-context"]
wasm-deterministic = ["getrandom/custom"]
3 changes: 0 additions & 3 deletions stacks-common/src/deps_common/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
//! software.
//!

// Clippy flags
#![allow(clippy::needless_range_loop)] // suggests making a big mess of array newtypes

// Coding conventions
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
Expand Down
20 changes: 0 additions & 20 deletions stacks-common/src/deps_common/bitcoin/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ pub mod hash;

use std::{error, fmt};

use secp256k1;

use crate::deps_common::bitcoin::network;
use crate::deps_common::bitcoin::network::serialize;

Expand Down Expand Up @@ -50,8 +48,6 @@ pub trait BitArray {
/// if appropriate.
#[derive(Debug)]
pub enum Error {
/// secp-related error
Secp256k1(secp256k1::Error),
/// Serialization error
Serialize(serialize::Error),
/// Network error
Expand All @@ -65,7 +61,6 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
Error::Serialize(ref e) => fmt::Display::fmt(e, f),
Error::Network(ref e) => fmt::Display::fmt(e, f),
Error::SpvBadProofOfWork => f.write_str("target correct but not attained"),
Expand All @@ -77,28 +72,13 @@ impl fmt::Display for Error {
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Secp256k1(ref e) => Some(e),
Error::Serialize(ref e) => Some(e),
Error::Network(ref e) => Some(e),
Error::SpvBadProofOfWork | Error::SpvBadTarget => None,
}
}
}

#[doc(hidden)]
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}

#[doc(hidden)]
impl From<serialize::Error> for Error {
fn from(e: serialize::Error) -> Error {
Error::Serialize(e)
}
}

#[doc(hidden)]
impl From<network::Error> for Error {
fn from(e: network::Error) -> Error {
Expand Down
14 changes: 10 additions & 4 deletions stacks-common/src/libcommon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![cfg_attr(test, allow(unused_variables, unused_assignments))]
#![allow(clippy::assertions_on_constants)]

#[allow(unused_imports)]
#[macro_use(o, slog_log, slog_trace, slog_debug, slog_info, slog_warn, slog_error)]
Expand All @@ -13,10 +12,10 @@ extern crate slog;
#[macro_use]
extern crate serde_derive;

#[cfg(unix)]
#[cfg(all(unix, feature = "ctrlc-handler"))]
extern crate nix;

#[cfg(windows)]
#[cfg(all(windows, feature = "ctrlc-handler"))]
extern crate winapi;

#[macro_use]
Expand All @@ -29,7 +28,14 @@ pub mod types;

pub mod address;

pub mod deps_common;
pub mod deps_common {
pub mod bech32;
pub mod bitcoin;
pub mod httparse;

#[cfg(all(not(target_family = "wasm"), feature = "ctrlc-handler"))]
pub mod ctrlc;
}

pub mod bitvec;

Expand Down
8 changes: 6 additions & 2 deletions stacks-common/src/types/chainstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ impl SortitionId {
write!(hasher, "{pox}").expect("Failed to deserialize PoX ID into the hasher");
let h = Sha512Trunc256Sum::from_hasher(hasher);
let s = SortitionId(h.0);
test_debug!("SortitionId({}) = {} + {}", &s, bhh, pox);
s
// The `test_debug!` macro will expand to nothing on release builds.
#[allow(clippy::let_and_return)]
{
test_debug!("SortitionId({}) = {} + {}", &s, bhh, pox);
s
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stacks-common/src/util/chunked_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ mod test {
use std::io;
use std::io::Read;

use rand::RngCore;
use rand::RngCore as _;

use super::*;

Expand Down
2 changes: 2 additions & 0 deletions stacks-common/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,5 @@ where
let reader = BufReader::new(file);
serde_json::from_reader::<_, J>(reader).map_err(std::io::Error::from)
}
#[cfg(all(feature = "rusqlite", target_family = "wasm"))]
compile_error!("The `rusqlite` feature is not supported for wasm targets");
3 changes: 1 addition & 2 deletions stacks-common/src/util/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ mod test {
use std::io::{Read, Write};
use std::{io, thread};

use rand;
use rand::RngCore;
use rand::RngCore as _;

use super::*;

Expand Down
11 changes: 6 additions & 5 deletions stacks-common/src/util/secp256k1/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use ::secp256k1::{
constants as LibSecp256k1Constants, Error as LibSecp256k1Error, Message as LibSecp256k1Message,
PublicKey as LibSecp256k1PublicKey, Secp256k1, SecretKey as LibSecp256k1PrivateKey,
};
use rand::RngCore;
use serde::de::{Deserialize, Error as de_Error};
use serde::Serialize;

Expand Down Expand Up @@ -240,7 +239,10 @@ impl PublicKey for Secp256k1PublicKey {
}

impl Secp256k1PrivateKey {
#[cfg(feature = "rand")]
pub fn random() -> Secp256k1PrivateKey {
use rand::RngCore as _;

let mut rng = rand::thread_rng();
loop {
// keep trying to generate valid bytes
Expand Down Expand Up @@ -422,6 +424,7 @@ pub fn secp256k1_verify(

#[cfg(test)]
mod tests {
use rand::RngCore as _;
use secp256k1;
use secp256k1::{PublicKey as LibSecp256k1PublicKey, Secp256k1};

Expand Down Expand Up @@ -612,15 +615,13 @@ mod tests {
(Err(e1), Err(e2)) => assert_eq!(e1, e2),
(Err(e1), _) => {
test_debug!("Failed to verify signature: {}", e1);
assert!(
false,
panic!(
"failed fixture (verification: {:?}): {:#?}",
&ver_res, &fixture
);
}
(_, _) => {
assert!(
false,
panic!(
"failed fixture (verification: {:?}): {:#?}",
&ver_res, &fixture
);
Expand Down
Loading
Loading