From da7a851d4785891190b70ff03c21fa6340a7ec5a Mon Sep 17 00:00:00 2001 From: Gene Ferneau Date: Thu, 27 May 2021 04:04:20 +0000 Subject: [PATCH] Use hashbrown replacements for std equivalents --- .github/workflows/build.yml | 42 +++++++++++++++---- ci/check-compiles.sh | 1 + lightning/Cargo.toml | 2 + lightning/src/chain/chainmonitor.rs | 1 - lightning/src/chain/channelmonitor.rs | 1 - lightning/src/chain/keysinterface.rs | 1 - lightning/src/chain/onchaintx.rs | 1 - lightning/src/lib.rs | 11 ++++- lightning/src/ln/chanmon_update_fail_tests.rs | 1 - lightning/src/ln/channelmanager.rs | 1 - lightning/src/ln/functional_test_utils.rs | 1 - lightning/src/ln/functional_tests.rs | 1 - lightning/src/ln/peer_handler.rs | 1 - lightning/src/ln/reorg_tests.rs | 1 - lightning/src/routing/router.rs | 9 ++-- lightning/src/util/ser.rs | 1 - lightning/src/util/test_utils.rs | 1 - 17 files changed, 53 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 716d4b0b..85aaac6a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,21 +14,34 @@ jobs: # 1.41.0 is Debian stable 1.41.0, # 1.45.2 is MSRV for lightning-net-tokio, lightning-block-sync, and coverage generation - 1.45.2] + 1.45.2, + # 1.49.0 is MSRV for no_std builds using hashbrown + 1.49.0] include: - toolchain: stable build-net-tokio: true + build-no-std: true - toolchain: stable platform: macos-latest build-net-tokio: true + build-no-std: true - toolchain: stable platform: windows-latest build-net-tokio: true + build-no-std: true - toolchain: beta build-net-tokio: true + build-no-std: true + - toolchain: 1.36.0 + build-no-std: false + - toolchain: 1.41.0 + build-no-std: false - toolchain: 1.45.2 build-net-tokio: true + build-no-std: false coverage: true + - toolchain: 1.49.0 + build-no-std: true runs-on: ${{ matrix.platform }} steps: - name: Checkout source code @@ -47,7 +60,10 @@ jobs: run: RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always - name: Build on Rust ${{ matrix.toolchain }} if: "! matrix.build-net-tokio" - run: cargo build --verbose --color always -p lightning && cargo build --verbose --color always -p lightning-invoice && cargo build --verbose --color always -p lightning-persister + run: | + cargo build --verbose --color always -p lightning + cargo build --verbose --color always -p lightning-invoice + cargo build --verbose --color always -p lightning-persister - name: Build Block Sync Clients on Rust ${{ matrix.toolchain }} with features if: "matrix.build-net-tokio && !matrix.coverage" run: | @@ -56,7 +72,6 @@ jobs: cargo build --verbose --color always --features rpc-client cargo build --verbose --color always --features rpc-client,rest-client cargo build --verbose --color always --features rpc-client,rest-client,tokio - cd .. - name: Build Block Sync Clients on Rust ${{ matrix.toolchain }} with features and full code-linking for coverage generation if: matrix.coverage run: | @@ -65,16 +80,30 @@ jobs: RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always --features rpc-client RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always --features rpc-client,rest-client RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always --features rpc-client,rest-client,tokio - cd .. - name: Test on Rust ${{ matrix.toolchain }} with net-tokio if: "matrix.build-net-tokio && !matrix.coverage" run: cargo test --verbose --color always - name: Test on Rust ${{ matrix.toolchain }} with net-tokio and full code-linking for coverage generation if: matrix.coverage run: RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always + - name: Test on no_std bullds Rust ${{ matrix.toolchain }} + if: "matrix.build-no-std && !matrix.coverage" + run: | + cd lightning + cargo test --verbose --color always --features hashbrown + cd .. + - name: Test on no_std bullds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation + if: "matrix.build-no-std && matrix.coverage" + run: | + cd lightning + RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features hashbrown + cd .. - name: Test on Rust ${{ matrix.toolchain }} if: "! matrix.build-net-tokio" - run: cargo test --verbose --color always -p lightning && cargo test --verbose --color always -p lightning-invoice && cargo build --verbose --color always -p lightning-persister + run: | + cargo test --verbose --color always -p lightning + cargo test --verbose --color always -p lightning-invoice + cargo build --verbose --color always -p lightning-persister - name: Test Block Sync Clients on Rust ${{ matrix.toolchain }} with features if: "matrix.build-net-tokio && !matrix.coverage" run: | @@ -83,7 +112,6 @@ jobs: cargo test --verbose --color always --features rpc-client cargo test --verbose --color always --features rpc-client,rest-client cargo test --verbose --color always --features rpc-client,rest-client,tokio - cd .. - name: Test Block Sync Clients on Rust ${{ matrix.toolchain }} with features and full code-linking for coverage generation if: matrix.coverage run: | @@ -92,7 +120,6 @@ jobs: RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client,rest-client RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features rpc-client,rest-client,tokio - cd .. - name: Install deps for kcov if: matrix.coverage run: | @@ -157,6 +184,7 @@ jobs: run: | cd lightning RUSTFLAGS="--cfg=require_route_graph_test" cargo test + RUSTFLAGS="--cfg=require_route_graph_test" cargo test --features hashbrown cd .. - name: Run benchmarks on Rust ${{ matrix.toolchain }} run: | diff --git a/ci/check-compiles.sh b/ci/check-compiles.sh index 9222f609..79c2d92b 100755 --- a/ci/check-compiles.sh +++ b/ci/check-compiles.sh @@ -6,3 +6,4 @@ cargo check cargo doc cargo doc --document-private-items cd fuzz && cargo check --features=stdin_fuzz +cd ../lightning && cargo check --no-default-features --features=no_std diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index affe46e3..71d85872 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -25,10 +25,12 @@ max_level_debug = [] # This is unsafe to use in production because it may result in the counterparty publishing taking our funds. unsafe_revoked_tx_signing = [] unstable = [] +no_std = ["hashbrown"] [dependencies] bitcoin = "0.26" +hashbrown = { version = "0.11", optional = true } hex = { version = "0.3", optional = true } regex = { version = "0.1.80", optional = true } diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index a3365f2b..23fc42f5 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -38,7 +38,6 @@ use util::events; use util::events::EventHandler; use prelude::*; -use std::collections::{HashMap, hash_map}; use std::sync::RwLock; use core::ops::Deref; diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index a0f5eb71..0bd21270 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -52,7 +52,6 @@ use util::byte_utils; use util::events::Event; use prelude::*; -use std::collections::{HashMap, HashSet}; use core::{cmp, mem}; use std::io::Error; use core::ops::Deref; diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index 84d83196..c5ad6f28 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -38,7 +38,6 @@ use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelP use ln::msgs::UnsignedChannelAnnouncement; use prelude::*; -use std::collections::HashSet; use core::sync::atomic::{AtomicUsize, Ordering}; use std::io::Error; use ln::msgs::{DecodeError, MAX_VALUE_MSAT}; diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index dd4d1d8f..30493eb5 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -34,7 +34,6 @@ use util::byte_utils; use prelude::*; use alloc::collections::BTreeMap; -use std::collections::HashMap; use core::cmp; use core::ops::Deref; use core::mem::replace; diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index a9f46b43..c84e3d2d 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -44,5 +44,12 @@ pub mod ln; pub mod routing; mod prelude { - pub use alloc::{vec, vec::Vec, string::String}; -} \ No newline at end of file + #[cfg(feature = "hashbrown")] + extern crate hashbrown; + + pub use alloc::{vec, vec::Vec, string::String, collections::VecDeque}; + #[cfg(not(feature = "hashbrown"))] + pub use std::collections::{HashMap, HashSet, hash_map}; + #[cfg(feature = "hashbrown")] + pub use self::hashbrown::{HashMap, HashSet, hash_map}; +} diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index e945f051..fdcf7c2d 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -41,7 +41,6 @@ use ln::functional_test_utils::*; use util::test_utils; use prelude::*; -use std::collections::HashMap; use std::sync::{Arc, Mutex}; // If persister_fail is true, we have the persister return a PermanentFailure diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 13a993fd..ee266849 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -64,7 +64,6 @@ use util::errors::APIError; use prelude::*; use core::{cmp, mem}; use core::cell::RefCell; -use std::collections::{HashMap, hash_map, HashSet}; use std::io::{Cursor, Read}; use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard}; use core::sync::atomic::{AtomicUsize, Ordering}; diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index fb0435a0..166e943e 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -44,7 +44,6 @@ use core::cell::RefCell; use std::rc::Rc; use std::sync::{Arc, Mutex}; use core::mem; -use std::collections::HashMap; pub const CHAN_CONFIRM_DEPTH: u32 = 10; diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 95d114aa..6298f654 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -52,7 +52,6 @@ use regex; use prelude::*; use alloc::collections::BTreeSet; -use std::collections::{HashMap, HashSet}; use core::default::Default; use std::sync::{Arc, Mutex}; diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 33c4050b..6e05b549 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -32,7 +32,6 @@ use routing::network_graph::NetGraphMsgHandler; use prelude::*; use alloc::collections::LinkedList; -use std::collections::{HashMap,hash_map,HashSet}; use std::sync::{Arc, Mutex}; use core::sync::atomic::{AtomicUsize, Ordering}; use core::{cmp, hash, fmt, mem}; diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index fb316caf..9946cc24 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -23,7 +23,6 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; use prelude::*; -use std::collections::HashMap; use core::mem; use ln::functional_test_utils::*; diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 9f4b8ac3..0edeef0c 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -24,7 +24,6 @@ use util::logger::Logger; use prelude::*; use alloc::collections::BinaryHeap; use core::cmp; -use std::collections::HashMap; use core::ops::Deref; /// A hop in a route @@ -3843,6 +3842,7 @@ mod tests { } } + #[cfg(not(feature = "no_std"))] pub(super) fn random_init_seed() -> u64 { // Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG. use core::hash::{BuildHasher, Hasher}; @@ -3850,9 +3850,11 @@ mod tests { println!("Using seed of {}", seed); seed } + #[cfg(not(feature = "no_std"))] use util::ser::Readable; #[test] + #[cfg(not(feature = "no_std"))] fn generate_routes() { let mut d = match super::test_utils::get_route_file() { Ok(f) => f, @@ -3880,6 +3882,7 @@ mod tests { } #[test] + #[cfg(not(feature = "no_std"))] fn generate_routes_mpp() { let mut d = match super::test_utils::get_route_file() { Ok(f) => f, @@ -3907,7 +3910,7 @@ mod tests { } } -#[cfg(test)] +#[cfg(all(test, not(feature = "no_std")))] pub(crate) mod test_utils { use std::fs::File; /// Tries to open a network graph file, or panics with a URL to fetch it. @@ -3934,7 +3937,7 @@ pub(crate) mod test_utils { } } -#[cfg(all(test, feature = "unstable"))] +#[cfg(all(test, feature = "unstable", not(feature = "no_std")))] mod benches { use super::*; use util::logger::{Logger, Record}; diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 4e69a37d..b02fef27 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -12,7 +12,6 @@ use prelude::*; use std::io::{Read, Write}; -use std::collections::HashMap; use core::hash::Hash; use std::sync::Mutex; use core::cmp; diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 60aafd91..b4d274f6 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -42,7 +42,6 @@ use core::time::Duration; use std::sync::{Mutex, Arc}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::{cmp, mem}; -use std::collections::{HashMap, HashSet, VecDeque}; use chain::keysinterface::InMemorySigner; pub struct TestVecWriter(pub Vec); -- 2.30.2