Use hashbrown replacements for std equivalents
authorGene Ferneau <gene@ferneau.link>
Thu, 27 May 2021 04:04:20 +0000 (04:04 +0000)
committerGene Ferneau <gene@ferneau.link>
Fri, 18 Jun 2021 21:54:21 +0000 (21:54 +0000)
17 files changed:
.github/workflows/build.yml
ci/check-compiles.sh
lightning/Cargo.toml
lightning/src/chain/chainmonitor.rs
lightning/src/chain/channelmonitor.rs
lightning/src/chain/keysinterface.rs
lightning/src/chain/onchaintx.rs
lightning/src/lib.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/reorg_tests.rs
lightning/src/routing/router.rs
lightning/src/util/ser.rs
lightning/src/util/test_utils.rs

index 716d4b0b751d5f7c5c1837e3ab5575c913b6b7a2..85aaac6a3a0d378e5cef84a1f7ef307d121a8658 100644 (file)
@@ -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: |
index 9222f609cc082ced5a1ac3806e8c9b05c7531379..79c2d92b761ce409cc31fda64d3667bc29bf1cfe 100755 (executable)
@@ -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
index affe46e3d03e2606bc58ea2a74c9b8e7feb4bbc5..71d85872398037881d5cc46c3f0ea8316b7c247c 100644 (file)
@@ -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 }
 
index a3365f2b64fd58302eef8e292961e53384743896..23fc42f54a4fc16c43d149f4e117ff42ec46d623 100644 (file)
@@ -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;
 
index a0f5eb71d7b3fdf69ef076183fdb1ccc76ff0fc4..0bd212705d5f14009cf0fe19a3e750f8b3f39d96 100644 (file)
@@ -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;
index 84d83196472142d4c4334dcba5bf2d1e0ffafb0b..c5ad6f28b263d85dd8559c0df82b5e6e9e91f1e7 100644 (file)
@@ -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};
index dd4d1d8f0b37f844c8262673cafc6e1b98407585..30493eb56c696ed2f055d477db033a4cecfe8500 100644 (file)
@@ -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;
index a9f46b43a4026f650069cb97f9baf1dd62a0d968..c84e3d2d8da24aaeb826cc2e258410d4c7c79c67 100644 (file)
@@ -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};
+}
index e945f05144bae0532623b21250b8868452bdf52d..fdcf7c2de70ee71edb5bd8e3d2ff4dfdafa67606 100644 (file)
@@ -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
index 13a993fd965596dc41e12a387bc178ea31372b35..ee266849b8bd8d813cf3757279ebacc5018dc60c 100644 (file)
@@ -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};
index fb0435a0d3d94e313502ff15132a3bb162522444..166e943e66ddb1007dafbab2f49c843e6db63467 100644 (file)
@@ -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;
 
index 95d114aab5b6b2b563fb60b2656879b56221adb9..6298f654965f2ddf1fbea336b366315186246646 100644 (file)
@@ -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};
 
index 33c4050b374dd21fa4eeb3486bb179ae91c85f4e..6e05b5498cf284391db623152242a571c57aa346 100644 (file)
@@ -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};
index fb316caf2ef1008012ca57ffc8722ee3e9e412ef..9946cc24a3cb2485597dee37cac9c22d1efa3fa1 100644 (file)
@@ -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::*;
index 9f4b8ac3589a27ef7c713751eb1d98ad18596978..0edeef0c3cb3d9629ddcfd4445dbf2f1fccf3af4 100644 (file)
@@ -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};
index 4e69a37d20e9fcc5e0d093804136ef9f90be1747..b02fef275d2c08d644f1f706c8440f3d688d682c 100644 (file)
@@ -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;
index 60aafd9169a600c8ee19414c8b41f3a19db144b9..b4d274f683cb5b414b9a174e781feb978600e815 100644 (file)
@@ -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<u8>);