Skip to content

Commit 2ec3efa

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 2ec3efa

26 files changed

+155
-41
lines changed

ci/check-compiles.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ set -x
44
echo Testing $(git log -1 --oneline)
55
cargo check
66
cd fuzz && cargo check --features=stdin_fuzz
7+
cd ../lightning && cargo check --features=no_std

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ 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};
55-
use std::{cmp, mem};
56+
#[cfg(feature = "no_std")]
57+
use hashbrown::{HashMap, HashSet, hash_map};
58+
#[cfg(not(feature = "no_std"))]
59+
use std::{cmp, mem, ops::Deref};
60+
#[cfg(feature = "no_std")]
61+
use core::{cmp, mem, ops::Deref};
5662
use std::io::Error;
57-
use std::ops::Deref;
5863
use std::sync::Mutex;
5964

6065
/// An update generated by the underlying Channel itself which contains some new information the

lightning/src/chain/keysinterface.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ 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;
43+
#[cfg(not(feature = "no_std"))]
4044
use std::sync::atomic::{AtomicUsize, Ordering};
45+
#[cfg(feature = "no_std")]
46+
use core::sync::atomic::{AtomicUsize, Ordering};
4147
use std::io::Error;
4248
use ln::msgs::{DecodeError, MAX_VALUE_MSAT};
4349

lightning/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ 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 core;
38+
#[cfg(feature = "no_std")] extern crate hashbrown;
39+
3740
#[macro_use]
3841
pub mod util;
3942
pub mod chain;

lightning/src/ln/chan_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ use bitcoin::secp256k1::{Secp256k1, Signature, Message};
3131
use bitcoin::secp256k1::Error as SecpError;
3232
use bitcoin::secp256k1;
3333

34-
use std::cmp;
34+
#[cfg(not(feature = "no_std"))]
35+
use std::{cmp, ops::Deref};
36+
#[cfg(feature = "no_std")]
37+
use core::{cmp, ops::Deref};
3538
use ln::chan_utils;
3639
use util::transaction_utils::sort_outputs;
3740
use ln::channel::INITIAL_COMMITMENT_NUMBER;
3841
use std::io::Read;
39-
use std::ops::Deref;
4042
use chain;
4143

4244
const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5;

lightning/src/ln/channel.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ use util::logger::Logger;
3838
use util::errors::APIError;
3939
use util::config::{UserConfig,ChannelConfig};
4040

41-
use std;
42-
use std::{cmp,mem,fmt};
43-
use std::ops::Deref;
41+
#[cfg(not(feature = "no_std"))]
42+
use std::{cmp, mem, fmt, ops::Deref};
43+
#[cfg(feature = "no_std")]
44+
use core::{cmp, mem, fmt, ops::Deref};
4445
#[cfg(any(test, feature = "fuzztarget"))]
4546
use std::sync::Mutex;
4647
use bitcoin::hashes::hex::ToHex;

lightning/src/ln/channelmanager.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,32 @@ use util::chacha20::{ChaCha20, ChaChaReader};
5555
use util::logger::Logger;
5656
use util::errors::APIError;
5757

58-
use std::{cmp, mem};
58+
#[cfg(not(feature = "no_std"))]
59+
use std::{
60+
cmp,
61+
mem,
62+
marker::{Sync, Send},
63+
ops::Deref,
64+
sync::atomic::{AtomicUsize, Ordering},
65+
time::Duration,
66+
};
67+
#[cfg(feature = "no_std")]
68+
use core::{
69+
cmp,
70+
mem,
71+
marker::{Sync, Send},
72+
ops::Deref,
73+
sync::atomic::{AtomicUsize, Ordering},
74+
time::Duration,
75+
};
76+
#[cfg(not(feature = "no_std"))]
5977
use std::collections::{HashMap, hash_map, HashSet};
78+
#[cfg(feature = "no_std")]
79+
use hashbrown::{HashMap, HashSet, hash_map};
6080
use std::io::{Cursor, Read};
6181
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
62-
use std::sync::atomic::{AtomicUsize, Ordering};
63-
use std::time::Duration;
6482
#[cfg(any(test, feature = "allow_wallclock_use"))]
6583
use std::time::Instant;
66-
use std::marker::{Sync, Send};
67-
use std::ops::Deref;
6884
use bitcoin::hashes::hex::ToHex;
6985

7086
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
@@ -4306,7 +4322,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
43064322
mod tests {
43074323
use ln::channelmanager::PersistenceNotifier;
43084324
use std::sync::Arc;
4325+
#[cfg(not(feature = "no_std"))]
43094326
use std::sync::atomic::{AtomicBool, Ordering};
4327+
#[cfg(feature = "no_std")]
4328+
use core::sync::atomic::{AtomicBool, Ordering};
43104329
use std::thread;
43114330
use std::time::Duration;
43124331

lightning/src/ln/features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
//! [`Features`]: struct.Features.html
2626
//! [`Context`]: sealed/trait.Context.html
2727
28-
use std::{cmp, fmt};
29-
use std::marker::PhantomData;
28+
#[cfg(not(feature = "no_std"))]
29+
use std::{cmp, fmt, marker::PhantomData};
30+
#[cfg(feature = "no_std")]
31+
use core::{cmp, fmt, marker::PhantomData};
3032

3133
use ln::msgs::DecodeError;
3234
use util::ser::{Readable, Writeable, Writer};

0 commit comments

Comments
 (0)