From 6a8cc0ede84ce5710dd8e8a491c95f41ca637f70 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:47:18 +0200 Subject: [PATCH] fix: compilation with `default-features = false` --- .github/workflows/build.yml | 110 +++++++++++++++++++++++------------- Cargo.toml | 21 ++++--- src/keys/combined.rs | 2 +- src/keys/ed25519.rs | 2 +- src/keys/k256_key.rs | 2 +- src/lib.rs | 12 +++- src/node_id.rs | 3 +- 7 files changed, 98 insertions(+), 54 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e8afa62..4b77af1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,49 +7,81 @@ on: pull_request: jobs: - code-check: + audit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Get latest version of stable Rust - uses: moonrepo/setup-rust@v1 - with: - channel: stable - cache-target: release - components: rustfmt, clippy - bins: cargo-audit - - name: Check formatting with cargofmt - run: cargo fmt -- --check - - name: Check for lint warnings - run: cargo clippy --all-features -- -D warnings - - name: Run cargo audit to identify known security vulnerabilities reported to the RustSec Advisory Database - run: cargo audit - release-tests-ubuntu: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: taiki-e/install-action@cargo-audit + - name: + Run cargo audit to identify known security vulnerabilities reported to the RustSec + Advisory Database + run: cargo audit + + test: + name: test ${{ matrix.flags }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + flags: ["--no-default-features", "", "--features ed25519", "--features rust-secp256k1"] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - name: test + run: cargo test ${{ matrix.flags }} + + feature-checks: + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: taiki-e/install-action@cargo-hack + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - name: cargo hack + run: cargo hack check --feature-powerset --depth 2 + + clippy: + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + components: clippy + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - run: cargo clippy --workspace --all-targets --all-features + env: + RUSTFLAGS: -Dwarnings + + docs: runs-on: ubuntu-latest + timeout-minutes: 30 steps: - - uses: actions/checkout@v3 - - name: Get latest version of stable Rust - uses: moonrepo/setup-rust@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - uses: Swatinem/rust-cache@v2 with: - channel: stable - cache-target: release - bins: cargo-nextest - - name: Run tests in release - run: cargo nextest run --all --release --tests - - name: Run tests in release ed25519 - run: cargo nextest run --all --release --features "ed25519" --tests - - name: Run tests in release secp256k1 - run: cargo nextest run --all --release --features "secp256k1" --tests - - name: Run tests in release rust-secp256k1 - run: cargo nextest run --all --release --features "rust-secp256k1" --tests - - name: Run tests in release all features - run: cargo nextest run --all --release --all-features - check-rustdoc-links: - name: Check rustdoc intra-doc links + cache-on-failure: true + - run: cargo doc --workspace --all-features --no-deps --document-private-items + env: + RUSTDOCFLAGS: "--cfg docsrs -D warnings" + + fmt: runs-on: ubuntu-latest - container: - image: rust + timeout-minutes: 30 steps: - - uses: actions/checkout@v3 - - name: Check rustdoc links - run: RUSTDOCFLAGS="--deny broken_intra_doc_links" cargo doc --verbose --workspace --no-deps --document-private-items + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - run: cargo fmt --all --check diff --git a/Cargo.toml b/Cargo.toml index 92f0a54..ca9bf1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "enr" authors = ["Age Manning "] -edition = "2018" +edition = "2021" version = "0.12.0" description = "Rust implementation of Ethereum Node Record (ENR) EIP778" readme = "./README.md" @@ -12,16 +12,16 @@ license = "MIT" exclude = [".gitignore", ".github/*"] [dependencies] -alloy-rlp = { version = "0.3.4", default-features = true } -base64 = "0.21.7" +alloy-rlp = "0.3.4" +base64 = "0.21" bytes = "1" -hex = { version = "0.4.3" } +hex = "0.4.3" log = "0.4.21" rand = "0.8" zeroize = "1.7.0" sha3 = "0.10" k256 = { version = "0.13", features = ["ecdsa"], optional = true } -serde = { version = "1.0.197", features = ["derive"], optional = true } +serde = { version = "1.0", features = ["derive"], optional = true } ed25519-dalek = { version = "2.1.1", optional = true, features = ["rand_core"] } secp256k1 = { version = "0.28", optional = true, default-features = false, features = [ "global-context", @@ -29,13 +29,16 @@ secp256k1 = { version = "0.28", optional = true, default-features = false, featu [dev-dependencies] alloy-rlp = { version = "0.3.4", features = ["derive"] } -secp256k1 = { features = ["rand-std"], version = "0.28" } -serde_json = { version = "1.0.114" } +secp256k1 = { version = "0.28", features = ["rand-std"] } +serde_json = "1.0" [features] default = ["serde", "k256"] -ed25519 = ["ed25519-dalek"] -rust-secp256k1 = ["secp256k1"] +serde = ["dep:serde"] +k256 = ["dep:k256"] +ed25519 = ["dep:ed25519-dalek"] +secp256k1 = ["rust-secp256k1"] +rust-secp256k1 = ["dep:secp256k1"] [lib] name = "enr" diff --git a/src/keys/combined.rs b/src/keys/combined.rs index f382289..15ee47e 100644 --- a/src/keys/combined.rs +++ b/src/keys/combined.rs @@ -7,7 +7,7 @@ use super::{ed25519_dalek as ed25519, EnrKey, EnrPublicKey, SigningError}; use crate::Key; use alloy_rlp::Error as DecoderError; use bytes::Bytes; -use std::{collections::BTreeMap, convert::TryFrom}; +use std::collections::BTreeMap; use zeroize::Zeroize; /// A standard implementation of the `EnrKey` trait used to sign and modify ENR records. The variants here represent the currently diff --git a/src/keys/ed25519.rs b/src/keys/ed25519.rs index 8603417..e625361 100644 --- a/src/keys/ed25519.rs +++ b/src/keys/ed25519.rs @@ -5,7 +5,7 @@ use super::{ use crate::Key; use alloy_rlp::{Decodable, Error as DecoderError}; use bytes::Bytes; -use std::{collections::BTreeMap, convert::TryFrom}; +use std::collections::BTreeMap; /// The ENR key that stores the public key in the ENR record. pub const ENR_KEY: &str = "ed25519"; diff --git a/src/keys/k256_key.rs b/src/keys/k256_key.rs index 6e58116..0194311 100644 --- a/src/keys/k256_key.rs +++ b/src/keys/k256_key.rs @@ -18,7 +18,7 @@ use k256::{ }; use rand::rngs::OsRng; use sha3::{Digest, Keccak256}; -use std::{collections::BTreeMap, convert::TryFrom}; +use std::collections::BTreeMap; /// The ENR key that stores the public key in the ENR record. pub const ENR_KEY: &str = "secp256k1"; diff --git a/src/lib.rs b/src/lib.rs index cb0bf74..b69a4b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,10 @@ //! //! ### Building an ENR with the default `k256` `secp256k1` key type //! +//! Note the `k256` feature flag must be set (default). +//! //! ```rust +//! # #[cfg(feature = "k256")] { //! use enr::{Enr, k256}; //! use std::net::Ipv4Addr; //! use rand::thread_rng; @@ -61,6 +64,7 @@ //! //! assert_eq!(enr.ip4(), Some("192.168.0.1".parse().unwrap())); //! assert_eq!(enr.id(), Some("v4".into())); +//! # } //! ``` //! //! ### Building an ENR with the `CombinedKey` type (support for multiple signing @@ -92,6 +96,7 @@ //! can be added using [`insert`] and retrieved with [`get`]. //! //! ```rust +//! # #[cfg(feature = "k256")] { //! use enr::{k256::ecdsa::SigningKey, Enr}; //! use std::net::Ipv4Addr; //! use rand::thread_rng; @@ -120,6 +125,7 @@ //! assert_eq!(decoded_enr.id(), Some("v4".into())); //! assert_eq!(decoded_enr.tcp4(), Some(8001)); //! assert_eq!(decoded_enr.get("custom_key").as_ref().map(AsRef::as_ref), Some(vec![0,0,1]).as_deref()); +//! # } //! ``` //! //! ### Encoding/Decoding ENR's of various key types @@ -167,7 +173,7 @@ //! [`insert`]: struct.Enr.html#method.insert //! [`get`]: struct.Enr.html#method.get -#![warn(clippy::all)] +#![warn(clippy::all, rustdoc::all)] #![allow( clippy::map_err_ignore, clippy::missing_errors_doc, @@ -1124,7 +1130,10 @@ fn check_spec_reserved_keys(key: &[u8], mut value: &[u8]) -> Result<(), Error> { Ipv6Addr::decode(&mut value)?; } b"secp256k1" => { + #[cfg(all(feature = "k256", not(feature = "rust-secp256k1")))] >::decode(&mut value)?; + #[cfg(feature = "rust-secp256k1")] + >::decode(&mut value)?; } _ => return Ok(()), }; @@ -1136,7 +1145,6 @@ fn check_spec_reserved_keys(key: &[u8], mut value: &[u8]) -> Result<(), Error> { mod tests { use super::*; use alloy_rlp::{RlpDecodable, RlpEncodable}; - use std::convert::TryFrom; type DefaultEnr = Enr; diff --git a/src/node_id.rs b/src/node_id.rs index 192206e..c9fdf6f 100644 --- a/src/node_id.rs +++ b/src/node_id.rs @@ -140,7 +140,6 @@ mod serde_hex_prfx { #[cfg(test)] mod tests { use super::*; - use std::collections::HashMap; #[test] fn test_eq_node_raw_node() { @@ -194,6 +193,8 @@ mod tests { #[cfg(feature = "serde")] #[test] fn test_serde_as_hashmap_key() { + use std::collections::HashMap; + let mut responses: HashMap = HashMap::default(); responses.insert(NodeId::random(), 1); let _ = serde_json::json!(responses);