From 8351ae292f7441c2d4f508a57c9f417fc3e8f32f Mon Sep 17 00:00:00 2001 From: Jeff Bencin Date: Wed, 1 Jan 2025 22:56:04 -0500 Subject: [PATCH 1/4] chore(stacks-common): Remove unused imports and enable warning --- stacks-common/src/address/b58.rs | 2 +- stacks-common/src/address/mod.rs | 3 +-- stacks-common/src/bitvec.rs | 1 - .../src/deps_common/bitcoin/blockdata/block.rs | 1 - .../src/deps_common/bitcoin/blockdata/script.rs | 1 - .../src/deps_common/bitcoin/blockdata/transaction.rs | 4 +--- .../deps_common/bitcoin/network/message_network.rs | 2 -- stacks-common/src/deps_common/bitcoin/util/hash.rs | 2 +- stacks-common/src/deps_common/httparse/mod.rs | 4 +--- stacks-common/src/libcommon.rs | 4 +--- stacks-common/src/types/chainstate.rs | 12 +++++------- stacks-common/src/types/mod.rs | 1 - stacks-common/src/util/chunked_encoding.rs | 3 ++- stacks-common/src/util/db.rs | 2 +- stacks-common/src/util/hash.rs | 7 ++----- stacks-common/src/util/log.rs | 3 +-- stacks-common/src/util/mod.rs | 1 - stacks-common/src/util/pipe.rs | 4 ---- stacks-common/src/util/retry.rs | 6 +----- stacks-common/src/util/secp256k1.rs | 6 ++---- stacks-common/src/util/vrf.rs | 7 ------- 21 files changed, 20 insertions(+), 56 deletions(-) diff --git a/stacks-common/src/address/b58.rs b/stacks-common/src/address/b58.rs index 6a135392e5..ffba441de6 100644 --- a/stacks-common/src/address/b58.rs +++ b/stacks-common/src/address/b58.rs @@ -14,7 +14,7 @@ //! Base58 encoder and decoder -use std::{error, fmt, str}; +use std::{fmt, str}; use crate::address::Error; use crate::util::hash::DoubleSha256; diff --git a/stacks-common/src/address/mod.rs b/stacks-common/src/address/mod.rs index 381456f661..8377d0087d 100644 --- a/stacks-common/src/address/mod.rs +++ b/stacks-common/src/address/mod.rs @@ -19,7 +19,7 @@ use std::{error, fmt}; use sha2::{Digest, Sha256}; use crate::deps_common::bitcoin::blockdata::opcodes::All as btc_opcodes; -use crate::deps_common::bitcoin::blockdata::script::{Builder, Instruction, Script}; +use crate::deps_common::bitcoin::blockdata::script::Builder; use crate::types::PublicKey; use crate::util::hash::Hash160; @@ -220,7 +220,6 @@ pub fn public_keys_to_address_hash( mod test { use super::*; use crate::util::hash::*; - use crate::util::log; use crate::util::secp256k1::Secp256k1PublicKey as PubKey; struct PubkeyFixture { diff --git a/stacks-common/src/bitvec.rs b/stacks-common/src/bitvec.rs index 7c77e5da32..065dd5e814 100644 --- a/stacks-common/src/bitvec.rs +++ b/stacks-common/src/bitvec.rs @@ -260,7 +260,6 @@ mod test { use super::BitVec; use crate::codec::StacksMessageCodec; - use crate::util::hash::to_hex; fn check_set_get(mut input: BitVec<{ u16::MAX }>) { let original_input = input.clone(); diff --git a/stacks-common/src/deps_common/bitcoin/blockdata/block.rs b/stacks-common/src/deps_common/bitcoin/blockdata/block.rs index af064511b5..9a797fd846 100644 --- a/stacks-common/src/deps_common/bitcoin/blockdata/block.rs +++ b/stacks-common/src/deps_common/bitcoin/blockdata/block.rs @@ -25,7 +25,6 @@ use crate::deps_common::bitcoin::blockdata::transaction::Transaction; use crate::deps_common::bitcoin::network::constants::Network; use crate::deps_common::bitcoin::network::encodable::VarInt; use crate::deps_common::bitcoin::network::serialize::BitcoinHash; -use crate::deps_common::bitcoin::util; use crate::deps_common::bitcoin::util::hash::Sha256dHash; use crate::deps_common::bitcoin::util::Error; use crate::deps_common::bitcoin::util::Error::{SpvBadProofOfWork, SpvBadTarget}; diff --git a/stacks-common/src/deps_common/bitcoin/blockdata/script.rs b/stacks-common/src/deps_common/bitcoin/blockdata/script.rs index 34ee5897c3..cf0e3296b1 100644 --- a/stacks-common/src/deps_common/bitcoin/blockdata/script.rs +++ b/stacks-common/src/deps_common/bitcoin/blockdata/script.rs @@ -27,7 +27,6 @@ use std::mem::size_of; use std::{error, fmt}; -use serde; use sha2::{Digest, Sha256}; use crate::deps_common::bitcoin::blockdata::opcodes; diff --git a/stacks-common/src/deps_common/bitcoin/blockdata/transaction.rs b/stacks-common/src/deps_common/bitcoin/blockdata/transaction.rs index c2d4c4e0a2..6dbf49bd5d 100644 --- a/stacks-common/src/deps_common/bitcoin/blockdata/transaction.rs +++ b/stacks-common/src/deps_common/bitcoin/blockdata/transaction.rs @@ -34,7 +34,6 @@ use crate::deps_common::bitcoin::network::serialize::{ self, serialize, BitcoinHash, SimpleDecoder, SimpleEncoder, }; use crate::deps_common::bitcoin::util::hash::Sha256dHash; -use crate::util::hash::to_hex; /// A reference to a transaction output #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] @@ -675,7 +674,7 @@ impl SigHashType { #[cfg(test)] mod tests { - use super::{SigHashType, Transaction, TxIn}; + use super::{Transaction, TxIn}; use crate::deps_common; use crate::deps_common::bitcoin::blockdata::script::Script; use crate::deps_common::bitcoin::network::serialize::{deserialize, BitcoinHash}; @@ -690,7 +689,6 @@ mod tests { #[test] fn test_is_coinbase() { - use crate::deps_common::bitcoin::blockdata::constants; use crate::deps_common::bitcoin::network::constants::Network; let genesis = deps_common::bitcoin::blockdata::constants::genesis_block(Network::Bitcoin); diff --git a/stacks-common/src/deps_common/bitcoin/network/message_network.rs b/stacks-common/src/deps_common/bitcoin/network/message_network.rs index 0cf486ba85..a42eb47aea 100644 --- a/stacks-common/src/deps_common/bitcoin/network/message_network.rs +++ b/stacks-common/src/deps_common/bitcoin/network/message_network.rs @@ -19,8 +19,6 @@ //! use crate::deps_common::bitcoin::network::address::Address; -use crate::deps_common::bitcoin::network::constants; -use crate::util; // Some simple messages diff --git a/stacks-common/src/deps_common/bitcoin/util/hash.rs b/stacks-common/src/deps_common/bitcoin/util/hash.rs index e1a9455e99..abfce8349f 100644 --- a/stacks-common/src/deps_common/bitcoin/util/hash.rs +++ b/stacks-common/src/deps_common/bitcoin/util/hash.rs @@ -18,7 +18,7 @@ use std::char::from_digit; use std::cmp::min; use std::io::{Cursor, Write}; -use std::{error, fmt, mem}; +use std::{fmt, mem}; use ripemd::Ripemd160; #[cfg(feature = "serde")] diff --git a/stacks-common/src/deps_common/httparse/mod.rs b/stacks-common/src/deps_common/httparse/mod.rs index b4c9250546..364fe0f8a7 100644 --- a/stacks-common/src/deps_common/httparse/mod.rs +++ b/stacks-common/src/deps_common/httparse/mod.rs @@ -30,7 +30,7 @@ //! Originally written by Sean McArthur. //! //! Modified by Jude Nelson to remove all unsafe code. -use std::{error, fmt, mem, result, str}; +use std::{fmt, mem, result, str}; macro_rules! next { ($bytes:ident) => {{ @@ -1282,8 +1282,6 @@ mod tests { #[test] fn test_std_error() { - use std::error::Error as StdError; - use super::Error; let err = Error::HeaderName; assert_eq!(err.to_string(), err.description_str()); diff --git a/stacks-common/src/libcommon.rs b/stacks-common/src/libcommon.rs index 04c3acc1ea..34705bebda 100644 --- a/stacks-common/src/libcommon.rs +++ b/stacks-common/src/libcommon.rs @@ -1,5 +1,4 @@ #![allow(unused_macros)] -#![allow(unused_imports)] #![allow(dead_code)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] @@ -7,6 +6,7 @@ #![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)] extern crate slog; @@ -33,8 +33,6 @@ pub mod deps_common; pub mod bitvec; -use crate::types::chainstate::{BlockHeaderHash, BurnchainHeaderHash, SortitionId, StacksBlockId}; - pub mod consts { use crate::types::chainstate::{BlockHeaderHash, ConsensusHash}; pub use crate::types::MINING_COMMITMENT_WINDOW; diff --git a/stacks-common/src/types/chainstate.rs b/stacks-common/src/types/chainstate.rs index 630ce70c9d..e07e37f27a 100644 --- a/stacks-common/src/types/chainstate.rs +++ b/stacks-common/src/types/chainstate.rs @@ -2,24 +2,22 @@ use std::fmt::{self, Display}; use std::io::{Read, Write}; use std::str::FromStr; -use curve25519_dalek::digest::Digest; -use rand::{Rng, SeedableRng}; -use serde::de::{Deserialize, Error as de_Error}; -use serde::ser::Error as ser_Error; use serde::Serialize; -use sha2::{Digest as Sha2Digest, Sha256, Sha512_256}; +use sha2::{Digest as Sha2Digest, Sha512_256}; use crate::codec::{read_next, write_next, Error as CodecError, StacksMessageCodec}; use crate::consts::{FIRST_BURNCHAIN_CONSENSUS_HASH, FIRST_STACKS_BLOCK_HASH}; use crate::deps_common::bitcoin::util::hash::Sha256dHash; -use crate::util::hash::{to_hex, DoubleSha256, Hash160, Sha512Trunc256Sum, HASH160_ENCODED_SIZE}; +use crate::util::hash::{Hash160, Sha512Trunc256Sum, HASH160_ENCODED_SIZE}; use crate::util::secp256k1::{MessageSignature, Secp256k1PrivateKey, Secp256k1PublicKey}; -use crate::util::uint::Uint256; use crate::util::vrf::{VRFProof, VRF_PROOF_ENCODED_SIZE}; pub type StacksPublicKey = Secp256k1PublicKey; pub type StacksPrivateKey = Secp256k1PrivateKey; +#[cfg(any(test, feature = "testing"))] +use crate::util::hash::DoubleSha256; + /// Hash of a Trie node. This is a SHA2-512/256. #[derive(Default)] pub struct TrieHash(pub [u8; 32]); diff --git a/stacks-common/src/types/mod.rs b/stacks-common/src/types/mod.rs index 93ebd17bc0..297d629254 100644 --- a/stacks-common/src/types/mod.rs +++ b/stacks-common/src/types/mod.rs @@ -30,7 +30,6 @@ use crate::address::{ C32_ADDRESS_VERSION_TESTNET_MULTISIG, C32_ADDRESS_VERSION_TESTNET_SINGLESIG, }; use crate::consts::MICROSTACKS_PER_STACKS; -use crate::deps_common::bitcoin::blockdata::transaction::TxOut; use crate::types::chainstate::{StacksAddress, StacksPublicKey}; use crate::util::hash::Hash160; use crate::util::secp256k1::{MessageSignature, Secp256k1PublicKey}; diff --git a/stacks-common/src/util/chunked_encoding.rs b/stacks-common/src/util/chunked_encoding.rs index 445ec5a831..328f94514f 100644 --- a/stacks-common/src/util/chunked_encoding.rs +++ b/stacks-common/src/util/chunked_encoding.rs @@ -445,9 +445,10 @@ impl Write for HttpChunkedTransferWriter<'_, '_, W> { } } +#[cfg(test)] mod test { use std::io; - use std::io::{Read, Write}; + use std::io::Read; use rand::RngCore; diff --git a/stacks-common/src/util/db.rs b/stacks-common/src/util/db.rs index 53564af597..3a463df4f8 100644 --- a/stacks-common/src/util/db.rs +++ b/stacks-common/src/util/db.rs @@ -17,7 +17,7 @@ use std::backtrace::Backtrace; use std::sync::{LazyLock, Mutex}; use std::thread; -use std::time::{Duration, Instant}; +use std::time::Instant; use hashbrown::HashMap; use rand::{thread_rng, Rng}; diff --git a/stacks-common/src/util/hash.rs b/stacks-common/src/util/hash.rs index 666e72c8e2..85f357d21a 100644 --- a/stacks-common/src/util/hash.rs +++ b/stacks-common/src/util/hash.rs @@ -20,7 +20,6 @@ use std::{fmt, mem}; use ripemd::Ripemd160; use serde::de::{Deserialize, Error as de_Error}; -use serde::ser::Error as ser_Error; use serde::Serialize; use sha2::{Digest, Sha256, Sha512, Sha512_256}; use sha3::Keccak256; @@ -29,7 +28,7 @@ use crate::types::StacksPublicKeyBuffer; use crate::util::pair::*; use crate::util::secp256k1::Secp256k1PublicKey; use crate::util::uint::Uint256; -use crate::util::{log, HexError}; +use crate::util::HexError; // hash function for Merkle trees pub trait MerkleHashFunc { @@ -659,9 +658,7 @@ pub fn bytes_to_hex(s: &[u8]) -> String { #[cfg(test)] mod test { - use super::{ - bin_bytes, hex_bytes, to_bin, DoubleSha256, MerkleHashFunc, MerklePath, MerkleTree, - }; + use super::{bin_bytes, hex_bytes, to_bin, DoubleSha256, MerkleHashFunc, MerkleTree}; struct MerkleTreeFixture { data: Vec>, diff --git a/stacks-common/src/util/log.rs b/stacks-common/src/util/log.rs index b0ac704f0c..1699aa6f54 100644 --- a/stacks-common/src/util/log.rs +++ b/stacks-common/src/util/log.rs @@ -15,13 +15,12 @@ // along with this program. If not, see . use std::io::Write; -use std::sync::Mutex; use std::time::{Duration, SystemTime}; use std::{env, io, thread}; use chrono::prelude::*; use lazy_static::lazy_static; -use slog::{BorrowedKV, Drain, FnValue, Level, Logger, OwnedKVList, Record, KV}; +use slog::{Drain, Level, Logger, OwnedKVList, Record, KV}; use slog_term::{CountingWriter, Decorator, RecordDecorator, Serializer}; lazy_static! { diff --git a/stacks-common/src/util/mod.rs b/stacks-common/src/util/mod.rs index 8fc9499b2f..0e9ff49cca 100644 --- a/stacks-common/src/util/mod.rs +++ b/stacks-common/src/util/mod.rs @@ -28,7 +28,6 @@ pub mod secp256k1; pub mod uint; pub mod vrf; -use std::collections::HashMap; use std::fs::File; use std::io::{BufReader, BufWriter, Write}; use std::path::{Path, PathBuf}; diff --git a/stacks-common/src/util/pipe.rs b/stacks-common/src/util/pipe.rs index 86d92abd61..4407fee71f 100644 --- a/stacks-common/src/util/pipe.rs +++ b/stacks-common/src/util/pipe.rs @@ -21,8 +21,6 @@ use std::io; use std::io::{Read, Write}; use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TryRecvError, TrySendError}; -use crate::util::log; - /// Inter-thread pipe for streaming messages, built on channels. /// Used mainly in conjunction with networking. /// @@ -316,7 +314,6 @@ impl Write for PipeWrite { #[cfg(test)] mod test { - use std::io::prelude::*; use std::io::{Read, Write}; use std::{io, thread}; @@ -324,7 +321,6 @@ mod test { use rand::RngCore; use super::*; - use crate::util::*; #[test] fn test_connection_pipe_oneshot() { diff --git a/stacks-common/src/util/retry.rs b/stacks-common/src/util/retry.rs index e7f6c0b140..47801289a3 100644 --- a/stacks-common/src/util/retry.rs +++ b/stacks-common/src/util/retry.rs @@ -18,11 +18,7 @@ */ use std::io; -use std::io::prelude::*; -use std::io::{Read, Write}; - -use crate::util::hash::to_hex; -use crate::util::log; +use std::io::Read; /// Wrap a Read so that we store a copy of what was read. /// Used for re-trying reads when we don't know what to expect from the stream. diff --git a/stacks-common/src/util/secp256k1.rs b/stacks-common/src/util/secp256k1.rs index 5c64838855..353c58c428 100644 --- a/stacks-common/src/util/secp256k1.rs +++ b/stacks-common/src/util/secp256k1.rs @@ -13,7 +13,7 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use rand::{thread_rng, RngCore}; +use rand::RngCore; use secp256k1; use secp256k1::ecdsa::{ RecoverableSignature as LibSecp256k1RecoverableSignature, RecoveryId as LibSecp256k1RecoveryID, @@ -24,11 +24,9 @@ use secp256k1::{ PublicKey as LibSecp256k1PublicKey, Secp256k1, SecretKey as LibSecp256k1PrivateKey, }; use serde::de::{Deserialize, Error as de_Error}; -use serde::ser::Error as ser_Error; use serde::Serialize; use super::hash::Sha256Sum; -use crate::impl_byte_array_message_codec; use crate::types::{PrivateKey, PublicKey}; use crate::util::hash::{hex_bytes, to_hex}; @@ -442,8 +440,8 @@ mod tests { use secp256k1::{PublicKey as LibSecp256k1PublicKey, Secp256k1}; use super::*; + use crate::util::get_epoch_time_ms; use crate::util::hash::hex_bytes; - use crate::util::{get_epoch_time_ms, log}; struct KeyFixture { input: I, diff --git a/stacks-common/src/util/vrf.rs b/stacks-common/src/util/vrf.rs index 0c2b2c3dad..5c7439daf9 100644 --- a/stacks-common/src/util/vrf.rs +++ b/stacks-common/src/util/vrf.rs @@ -22,16 +22,11 @@ use std::fmt::Debug; use std::hash::{Hash, Hasher}; /// This codebase is based on routines defined in the IETF draft for verifiable random functions /// over elliptic curves (https://tools.ietf.org/id/draft-irtf-cfrg-vrf-02.html). -use std::ops::Deref; -use std::ops::DerefMut; use std::{error, fmt}; use curve25519_dalek::constants::ED25519_BASEPOINT_POINT; use curve25519_dalek::edwards::{CompressedEdwardsY, EdwardsPoint}; use curve25519_dalek::scalar::Scalar as ed25519_Scalar; -use ed25519_dalek::{ - SecretKey as EdDalekSecretKeyBytes, SigningKey as EdPrivateKey, VerifyingKey as EdPublicKey, -}; use rand; use sha2::{Digest, Sha512}; @@ -535,10 +530,8 @@ impl VRF { #[cfg(test)] mod tests { - use curve25519_dalek::scalar::Scalar as ed25519_Scalar; use rand; use rand::RngCore; - use sha2::Sha512; use super::*; use crate::util::hash::hex_bytes; From 7a15524b4955dc3c79123b29641866d5b299f57c Mon Sep 17 00:00:00 2001 From: Jeff Bencin Date: Wed, 1 Jan 2025 23:10:32 -0500 Subject: [PATCH 2/4] chore: Move feature-gated import inside function --- stacks-common/src/types/chainstate.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stacks-common/src/types/chainstate.rs b/stacks-common/src/types/chainstate.rs index e07e37f27a..89b244ae42 100644 --- a/stacks-common/src/types/chainstate.rs +++ b/stacks-common/src/types/chainstate.rs @@ -15,9 +15,6 @@ use crate::util::vrf::{VRFProof, VRF_PROOF_ENCODED_SIZE}; pub type StacksPublicKey = Secp256k1PublicKey; pub type StacksPrivateKey = Secp256k1PrivateKey; -#[cfg(any(test, feature = "testing"))] -use crate::util::hash::DoubleSha256; - /// Hash of a Trie node. This is a SHA2-512/256. #[derive(Default)] pub struct TrieHash(pub [u8; 32]); @@ -450,6 +447,8 @@ impl BurnchainHeaderHash { index_root: &TrieHash, noise: u64, ) -> BurnchainHeaderHash { + use crate::util::hash::DoubleSha256; + let mut bytes = vec![]; bytes.extend_from_slice(&block_height.to_be_bytes()); bytes.extend_from_slice(index_root.as_bytes()); From 8d335291c39c51c324cbc0ad47c45f05e3598b23 Mon Sep 17 00:00:00 2001 From: Jeff Bencin Date: Thu, 2 Jan 2025 15:59:52 -0500 Subject: [PATCH 3/4] fix: Feature flag `slog_json` working again --- stacks-common/src/util/log.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stacks-common/src/util/log.rs b/stacks-common/src/util/log.rs index 1699aa6f54..77a4950f81 100644 --- a/stacks-common/src/util/log.rs +++ b/stacks-common/src/util/log.rs @@ -190,6 +190,10 @@ impl TermFormat { #[cfg(feature = "slog_json")] fn make_json_logger() -> Logger { + use std::sync::Mutex; + + use slog::FnValue; + let def_keys = o!("file" => FnValue(move |info| { info.file() }), From 10e6c6d639eecd004311d0b699ee272e9fa2c59c Mon Sep 17 00:00:00 2001 From: Jeff Bencin Date: Fri, 3 Jan 2025 11:49:47 -0500 Subject: [PATCH 4/4] fix(stacks-common): Remove remaining unused imports --- stacks-common/src/types/chainstate.rs | 2 +- stacks-common/src/types/mod.rs | 1 - stacks-common/src/util/mod.rs | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/stacks-common/src/types/chainstate.rs b/stacks-common/src/types/chainstate.rs index 89b244ae42..b7ecae66c4 100644 --- a/stacks-common/src/types/chainstate.rs +++ b/stacks-common/src/types/chainstate.rs @@ -1,4 +1,4 @@ -use std::fmt::{self, Display}; +use std::fmt; use std::io::{Read, Write}; use std::str::FromStr; diff --git a/stacks-common/src/types/mod.rs b/stacks-common/src/types/mod.rs index 297d629254..acb838f9c8 100644 --- a/stacks-common/src/types/mod.rs +++ b/stacks-common/src/types/mod.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::cell::LazyCell; use std::cmp::Ordering; use std::fmt; use std::ops::{Deref, DerefMut, Index, IndexMut}; diff --git a/stacks-common/src/util/mod.rs b/stacks-common/src/util/mod.rs index 0e9ff49cca..46158d2f4f 100644 --- a/stacks-common/src/util/mod.rs +++ b/stacks-common/src/util/mod.rs @@ -30,13 +30,13 @@ pub mod vrf; use std::fs::File; use std::io::{BufReader, BufWriter, Write}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::time::{SystemTime, UNIX_EPOCH}; use std::{error, fmt, thread, time}; /// Given a relative path inside the Cargo workspace, return the absolute path #[cfg(any(test, feature = "testing"))] -pub fn cargo_workspace

(relative_path: P) -> PathBuf +pub fn cargo_workspace

(relative_path: P) -> std::path::PathBuf where P: AsRef, {