Skip to content

Commit 452a550

Browse files
committed
Add no_std feature and hashbrown dependency
Add no_std feature as an initial step to supporting embedded platforms. Use hashbrown for HashMap, HashSet, and hash_map replacements for their std::collections counterparts.
1 parent 2cb5b1a commit 452a550

20 files changed

+61
-4
lines changed

ci/check-compiles.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ set -e
33
set -x
44
echo Testing $(git log -1 --oneline)
55
cargo check
6-
cd fuzz && cargo check --features=stdin_fuzz
6+
cd fuzz && cargo check --features=stdin_fuzz && cargo check --features=no_std
7+
cd ../lightning && cargo check --features=no_std

fuzz/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ afl_fuzz = ["afl"]
1515
honggfuzz_fuzz = ["honggfuzz"]
1616
libfuzzer_fuzz = ["libfuzzer-sys"]
1717
stdin_fuzz = []
18+
no_std = ["hashbrown"]
1819

1920
[dependencies]
2021
afl = { version = "0.4", optional = true }
@@ -23,6 +24,7 @@ bitcoin = { version = "0.26", features = ["fuzztarget", "secp-lowmemory"] }
2324
hex = "0.3"
2425
honggfuzz = { version = "0.5", optional = true }
2526
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }
27+
hashbrown = { version = "0.9", optional = true }
2628

2729
[patch.crates-io]
2830
# Rust-Secp256k1 PR 282. This patch should be dropped once that is merged.

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ use bitcoin::secp256k1::Secp256k1;
5858

5959
use std::mem;
6060
use std::cmp::Ordering;
61+
#[cfg(not(feature = "no_std"))]
6162
use std::collections::{HashSet, hash_map, HashMap};
63+
#[cfg(feature = "no_std")]
64+
use hashbrown::{HashMap, HashSet, hash_map};
6265
use std::sync::{Arc,Mutex};
6366
use std::sync::atomic;
6467
use std::io::Cursor;

fuzz/src/full_stack.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ use bitcoin::secp256k1::key::{PublicKey,SecretKey};
4949
use bitcoin::secp256k1::Secp256k1;
5050

5151
use std::cell::RefCell;
52+
#[cfg(not(feature = "no_std"))]
5253
use std::collections::{HashMap, hash_map};
54+
#[cfg(feature = "no_std")]
55+
use hashbrown::{HashMap, hash_map};
5356
use std::cmp;
5457
use std::sync::Arc;
5558
use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};

fuzz/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ pub mod peer_crypt;
2020
pub mod router;
2121

2222
pub mod msg_targets;
23+
24+
#[cfg(feature = "no_std")] extern crate hashbrown;

fuzz/src/router.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use bitcoin::blockdata::constants::genesis_block;
2626

2727
use utils::test_logger;
2828

29+
#[cfg(not(feature = "no_std"))]
2930
use std::collections::HashSet;
31+
#[cfg(feature = "no_std")]
32+
use hashbrown::HashSet;
3033
use std::sync::Arc;
3134
use std::sync::atomic::{AtomicUsize, Ordering};
3235

lightning/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ max_level_error = []
2121
max_level_warn = []
2222
max_level_info = []
2323
max_level_debug = []
24+
no_std = ["hashbrown"]
2425
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2526
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2627
unsafe_revoked_tx_signing = []
@@ -29,6 +30,7 @@ unstable = []
2930
[dependencies]
3031
bitcoin = "0.26"
3132

33+
hashbrown = { version = "0.9", optional = true }
3234
hex = { version = "0.3", optional = true }
3335
regex = { version = "0.1.80", optional = true }
3436

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ use util::logger::Logger;
4242
use util::events;
4343
use util::events::Event;
4444

45+
#[cfg(not(feature = "no_std"))]
4546
use std::collections::{HashMap, hash_map};
47+
#[cfg(feature = "no_std")]
48+
use hashbrown::{HashMap, hash_map};
4649
use std::sync::RwLock;
4750
use std::ops::Deref;
4851

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
5151
use util::byte_utils;
5252
use util::events::Event;
5353

54+
#[cfg(not(feature = "no_std"))]
5455
use std::collections::{HashMap, HashSet, hash_map};
56+
#[cfg(feature = "no_std")]
57+
use hashbrown::{HashMap, HashSet, hash_map};
5558
use std::{cmp, mem};
5659
use std::io::Error;
5760
use std::ops::Deref;

lightning/src/chain/keysinterface.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ use ln::chan_utils;
3636
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction};
3737
use ln::msgs::UnsignedChannelAnnouncement;
3838

39+
#[cfg(not(feature = "no_std"))]
3940
use std::collections::HashSet;
41+
#[cfg(feature = "no_std")]
42+
use hashbrown::HashSet;
4043
use std::sync::atomic::{AtomicUsize, Ordering};
4144
use std::io::Error;
4245
use ln::msgs::{DecodeError, MAX_VALUE_MSAT};

lightning/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern crate bitcoin;
3434
#[cfg(any(test, feature = "_test_utils"))] extern crate hex;
3535
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex;
3636

37+
#[cfg(feature = "no_std")] extern crate hashbrown;
38+
3739
#[macro_use]
3840
pub mod util;
3941
pub mod chain;

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ use util::logger::Logger;
5656
use util::errors::APIError;
5757

5858
use std::{cmp, mem};
59+
#[cfg(not(feature = "no_std"))]
5960
use std::collections::{HashMap, hash_map, HashSet};
61+
#[cfg(feature = "no_std")]
62+
use hashbrown::{HashMap, HashSet, hash_map};
6063
use std::io::{Cursor, Read};
6164
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
6265
use std::sync::atomic::{AtomicUsize, Ordering};

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ use std::cell::RefCell;
4242
use std::rc::Rc;
4343
use std::sync::Mutex;
4444
use std::mem;
45+
#[cfg(not(feature = "no_std"))]
4546
use std::collections::HashMap;
47+
#[cfg(feature = "no_std")]
48+
use hashbrown::HashMap;
4649

4750
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
4851

lightning/src/ln/functional_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ use bitcoin::secp256k1::key::{PublicKey,SecretKey};
4747

4848
use regex;
4949

50-
use std::collections::{BTreeSet, HashMap, HashSet};
50+
use std::collections::BTreeSet;
51+
#[cfg(not(feature = "no_std"))]
52+
use std::collections::{HashMap, HashSet};
53+
#[cfg(feature = "no_std")]
54+
use hashbrown::{HashMap, HashSet};
5155
use std::default::Default;
5256
use std::sync::Mutex;
5357
use std::sync::atomic::Ordering;

lightning/src/ln/onchaintx.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use util::logger::Logger;
3232
use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter};
3333
use util::byte_utils;
3434

35+
#[cfg(not(feature = "no_std"))]
3536
use std::collections::{HashMap, hash_map};
37+
#[cfg(feature = "no_std")]
38+
use hashbrown::{HashMap, hash_map};
3639
use std::cmp;
3740
use std::ops::Deref;
3841
use std::mem::replace;

lightning/src/ln/peer_handler.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ use util::events::{MessageSendEvent, MessageSendEventsProvider};
3030
use util::logger::Logger;
3131
use routing::network_graph::NetGraphMsgHandler;
3232

33-
use std::collections::{HashMap,hash_map,HashSet,LinkedList};
33+
use std::collections::LinkedList;
34+
#[cfg(not(feature = "no_std"))]
35+
use std::collections::{HashMap, HashSet, hash_map};
36+
#[cfg(feature = "no_std")]
37+
use hashbrown::{HashMap, HashSet, hash_map};
3438
use std::sync::{Arc, Mutex};
3539
use std::sync::atomic::{AtomicUsize, Ordering};
3640
use std::{cmp, error, hash, fmt, mem};

lightning/src/ln/reorg_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ use util::ser::{ReadableArgs, Writeable};
2323
use bitcoin::blockdata::block::{Block, BlockHeader};
2424
use bitcoin::hash_types::BlockHash;
2525

26+
#[cfg(not(feature = "no_std"))]
2627
use std::collections::HashMap;
28+
#[cfg(feature = "no_std")]
29+
use hashbrown::HashMap;
2730
use std::mem;
2831

2932
use ln::functional_test_utils::*;

lightning/src/routing/router.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ use util::ser::{Writeable, Readable};
2222
use util::logger::Logger;
2323

2424
use std::cmp;
25-
use std::collections::{HashMap, BinaryHeap};
25+
use std::collections::BinaryHeap;
26+
#[cfg(not(feature = "no_std"))]
27+
use std::collections::HashMap;
28+
#[cfg(feature = "no_std")]
29+
use hashbrown::HashMap;
2630
use std::ops::Deref;
2731

2832
/// A hop in a route

lightning/src/util/ser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
//! as ChannelsManagers and ChannelMonitors.
1212
1313
use std::io::{Read, Write};
14+
#[cfg(not(feature = "no_std"))]
1415
use std::collections::HashMap;
16+
#[cfg(feature = "no_std")]
17+
use hashbrown::HashMap;
1518
use std::hash::Hash;
1619
use std::sync::Mutex;
1720
use std::cmp;

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ use std::time::Duration;
3838
use std::sync::{Mutex, Arc};
3939
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
4040
use std::{cmp, mem};
41+
#[cfg(not(feature = "no_std"))]
4142
use std::collections::{HashMap, HashSet};
43+
#[cfg(feature = "no_std")]
44+
use hashbrown::{HashMap, HashSet};
4245
use chain::keysinterface::InMemorySigner;
4346

4447
pub struct TestVecWriter(pub Vec<u8>);

0 commit comments

Comments
 (0)