Skip to content

Commit 7d63003

Browse files
authored
Merge pull request #6238 from Jiloc/chore/stacks-common-optional-requirements
refactor: centralize dependencies and feature-gate stacks-common
2 parents 381ee9b + 222096b commit 7d63003

File tree

14 files changed

+126
-99
lines changed

14 files changed

+126
-99
lines changed

Cargo.lock

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@ members = [
1414

1515
# Dependencies we want to keep the same between workspace members
1616
[workspace.dependencies]
17-
ed25519-dalek = { version = "2.1.1", features = ["serde", "rand_core"] }
17+
ed25519-dalek = { version = "2.1.1", default-features = false }
1818
hashbrown = { version = "0.15.2", features = ["serde"] }
19+
lazy_static = "1.4.0"
1920
rand_core = "0.6.4"
2021
rand = "0.8"
2122
rand_chacha = "0.3.1"
22-
tikv-jemallocator = "0.5.4"
23+
serde = "1"
24+
serde_derive = "1"
25+
serde_json = { version = "1.0", features = ["arbitrary_precision", "unbounded_depth"] }
26+
slog = { version = "2.5.2", features = [ "max_level_trace" ] }
2327
rusqlite = { version = "0.31.0", features = ["blob", "serde_json", "i128_blob", "bundled", "trace"] }
28+
tikv-jemallocator = "0.5.4"
2429
thiserror = "1.0.65"
2530
toml = "0.5.6"
2631

2732
# Use a bit more than default optimization for
28-
# dev builds to speed up test execution
33+
# dev builds to speed up test execution
2934
[profile.dev]
3035
opt-level = 1
3136

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

stacks-common/Cargo.toml

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,69 +30,73 @@ name = "stacks_common"
3030
path = "./src/libcommon.rs"
3131

3232
[dependencies]
33-
rand = { workspace = true }
34-
serde = { version = "1.0", features = ["derive"] }
35-
serde_derive = "1"
36-
sha3 = "0.10.1"
37-
ripemd = "0.1.1"
38-
lazy_static = "1.4.0"
39-
slog = { version = "2.5.2", features = ["max_level_trace"] }
40-
slog-term = "2.6.0"
41-
slog-json = { version = "2.3.0", optional = true }
42-
chrono = "0.4.19"
33+
chrono = { version = "0.4.41", default-features = false, features = ["clock"] }
34+
curve25519-dalek = { version = "4.1.3", default-features = false, features = ["serde"] }
35+
ed25519-dalek = { workspace = true }
4336
hashbrown = { workspace = true }
44-
rusqlite = { workspace = true, optional = true }
37+
lazy_static = { workspace = true }
38+
ripemd = { version = "0.1.1", default-features = false }
39+
serde = { workspace = true , features = ["derive"] }
40+
serde_derive = { workspace = true }
41+
serde_json = { workspace = true }
42+
sha3 = { version = "0.10.1", default-features = false }
43+
slog = { workspace = true }
44+
slog-term = { version = "2.6.0", default-features = false }
45+
46+
# Optional dependencies
47+
getrandom = { version = "0.2", default-features = false, optional = true }
48+
rand = { workspace = true, optional = true }
49+
slog-json = { version = "2.3.0", default-features = false, optional = true }
4550

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

4954
[target.'cfg(windows)'.dependencies]
5055
winapi = { version = "0.3", features = [
5156
"consoleapi",
5257
"handleapi",
5358
"synchapi",
5459
"winbase",
55-
] }
56-
57-
[target.'cfg(windows)'.dev-dependencies]
58-
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
59-
60-
[dependencies.serde_json]
61-
version = "1.0"
62-
features = ["arbitrary_precision", "unbounded_depth"]
63-
64-
[dependencies.ed25519-dalek]
65-
workspace = true
66-
67-
[dependencies.curve25519-dalek]
68-
version = "4.1.3"
69-
features = ["serde"]
60+
], optional = true }
7061

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

7466
[target.'cfg(target_family = "wasm")'.dependencies]
75-
libsecp256k1 = { version = "0.7.0" }
67+
libsecp256k1 = { version = "0.7.0", default-features = false, features = ["hmac"] }
68+
69+
[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
70+
sha2 = { version = "0.10", features = ["asm"] }
71+
72+
[target.'cfg(any(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")), any(target_os="windows")))'.dependencies]
73+
sha2 = { version = "0.10" }
7674

7775
[dev-dependencies]
78-
rand_core = { workspace = true }
7976
proptest = "1.6.0"
8077

78+
[target.'cfg(windows)'.dev-dependencies]
79+
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
80+
81+
[build-dependencies]
82+
toml = { workspace = true }
83+
8184
[features]
82-
default = ["developer-mode"]
85+
default = ["developer-mode", "ctrlc-handler", "rand"]
8386
developer-mode = []
87+
# Enables graceful shutdown handling for Ctrl+C (SIGINT) signals.
88+
# This pulls in the `nix` or `winapi` dependency.
89+
ctrlc-handler = ["dep:nix", "dep:winapi"]
8490
slog_json = ["slog-json"]
85-
rusqlite = ["dep:rusqlite"]
86-
testing = []
91+
rusqlite = ["dep:rusqlite", "rand"]
92+
# Enables the rand module. This flag must be off on deterministic
93+
# platforms such as CosmWasm
94+
rand = ["dep:rand"]
8795
serde = []
96+
testing = ["rand"]
8897
bech32_std = []
8998
bech32_strict = []
9099

91-
[build-dependencies]
92-
toml = "0.5.6"
93-
94-
[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
95-
sha2 = { version = "0.10", features = ["asm"] }
96-
97-
[target.'cfg(any(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")), any(target_os="windows")))'.dependencies]
98-
sha2 = { version = "0.10" }
100+
# Wasm-specific features for easier configuration
101+
wasm-web = ["rand", "getrandom/js", "libsecp256k1/static-context"]
102+
wasm-deterministic = ["getrandom/custom"]

stacks-common/src/deps_common/bitcoin/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
//! software.
2626
//!
2727
28-
// Clippy flags
29-
#![allow(clippy::needless_range_loop)] // suggests making a big mess of array newtypes
30-
3128
// Coding conventions
3229
#![deny(non_upper_case_globals)]
3330
#![deny(non_camel_case_types)]

stacks-common/src/deps_common/bitcoin/util/mod.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ pub mod hash;
2020

2121
use std::{error, fmt};
2222

23-
use secp256k1;
24-
2523
use crate::deps_common::bitcoin::network;
2624
use crate::deps_common::bitcoin::network::serialize;
2725

@@ -50,8 +48,6 @@ pub trait BitArray {
5048
/// if appropriate.
5149
#[derive(Debug)]
5250
pub enum Error {
53-
/// secp-related error
54-
Secp256k1(secp256k1::Error),
5551
/// Serialization error
5652
Serialize(serialize::Error),
5753
/// Network error
@@ -65,7 +61,6 @@ pub enum Error {
6561
impl fmt::Display for Error {
6662
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6763
match *self {
68-
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
6964
Error::Serialize(ref e) => fmt::Display::fmt(e, f),
7065
Error::Network(ref e) => fmt::Display::fmt(e, f),
7166
Error::SpvBadProofOfWork => f.write_str("target correct but not attained"),
@@ -77,28 +72,13 @@ impl fmt::Display for Error {
7772
impl error::Error for Error {
7873
fn cause(&self) -> Option<&dyn error::Error> {
7974
match *self {
80-
Error::Secp256k1(ref e) => Some(e),
8175
Error::Serialize(ref e) => Some(e),
8276
Error::Network(ref e) => Some(e),
8377
Error::SpvBadProofOfWork | Error::SpvBadTarget => None,
8478
}
8579
}
8680
}
8781

88-
#[doc(hidden)]
89-
impl From<secp256k1::Error> for Error {
90-
fn from(e: secp256k1::Error) -> Error {
91-
Error::Secp256k1(e)
92-
}
93-
}
94-
95-
#[doc(hidden)]
96-
impl From<serialize::Error> for Error {
97-
fn from(e: serialize::Error) -> Error {
98-
Error::Serialize(e)
99-
}
100-
}
101-
10282
#[doc(hidden)]
10383
impl From<network::Error> for Error {
10484
fn from(e: network::Error) -> Error {

stacks-common/src/libcommon.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![allow(non_snake_case)]
55
#![allow(non_upper_case_globals)]
66
#![cfg_attr(test, allow(unused_variables, unused_assignments))]
7-
#![allow(clippy::assertions_on_constants)]
87

98
#[allow(unused_imports)]
109
#[macro_use(o, slog_log, slog_trace, slog_debug, slog_info, slog_warn, slog_error)]
@@ -13,10 +12,10 @@ extern crate slog;
1312
#[macro_use]
1413
extern crate serde_derive;
1514

16-
#[cfg(unix)]
15+
#[cfg(all(unix, feature = "ctrlc-handler"))]
1716
extern crate nix;
1817

19-
#[cfg(windows)]
18+
#[cfg(all(windows, feature = "ctrlc-handler"))]
2019
extern crate winapi;
2120

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

3029
pub mod address;
3130

32-
pub mod deps_common;
31+
pub mod deps_common {
32+
pub mod bech32;
33+
pub mod bitcoin;
34+
pub mod httparse;
35+
36+
#[cfg(all(not(target_family = "wasm"), feature = "ctrlc-handler"))]
37+
pub mod ctrlc;
38+
}
3339

3440
pub mod bitvec;
3541

stacks-common/src/types/chainstate.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,12 @@ impl SortitionId {
168168
write!(hasher, "{pox}").expect("Failed to deserialize PoX ID into the hasher");
169169
let h = Sha512Trunc256Sum::from_hasher(hasher);
170170
let s = SortitionId(h.0);
171-
test_debug!("SortitionId({}) = {} + {}", &s, bhh, pox);
172-
s
171+
// The `test_debug!` macro will expand to nothing on release builds.
172+
#[allow(clippy::let_and_return)]
173+
{
174+
test_debug!("SortitionId({}) = {} + {}", &s, bhh, pox);
175+
s
176+
}
173177
}
174178
}
175179
}

stacks-common/src/util/chunked_encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ mod test {
447447
use std::io;
448448
use std::io::Read;
449449

450-
use rand::RngCore;
450+
use rand::RngCore as _;
451451

452452
use super::*;
453453

stacks-common/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ where
147147
let reader = BufReader::new(file);
148148
serde_json::from_reader::<_, J>(reader).map_err(std::io::Error::from)
149149
}
150+
#[cfg(all(feature = "rusqlite", target_family = "wasm"))]
151+
compile_error!("The `rusqlite` feature is not supported for wasm targets");

stacks-common/src/util/pipe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ mod test {
317317
use std::io::{Read, Write};
318318
use std::{io, thread};
319319

320-
use rand;
321-
use rand::RngCore;
320+
use rand::RngCore as _;
322321

323322
use super::*;
324323

0 commit comments

Comments
 (0)