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};

lightning/src/ln/functional_test_utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ use bitcoin::hash_types::BlockHash;
3838

3939
use bitcoin::secp256k1::key::PublicKey;
4040

41-
use std::cell::RefCell;
4241
use std::rc::Rc;
4342
use std::sync::Mutex;
44-
use std::mem;
43+
#[cfg(not(feature = "no_std"))]
44+
use std::{cell::RefCell, mem};
45+
#[cfg(feature = "no_std")]
46+
use core::{cell::RefCell, mem};
47+
#[cfg(not(feature = "no_std"))]
4548
use std::collections::HashMap;
49+
#[cfg(feature = "no_std")]
50+
use hashbrown::HashMap;
4651

4752
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
4853

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ 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;
57+
#[cfg(not(feature = "no_std"))]
5358
use std::sync::atomic::Ordering;
59+
#[cfg(feature = "no_std")]
60+
use core::sync::atomic::Ordering;
5461

5562
use ln::functional_test_utils::*;
5663
use ln::chan_utils::CommitmentTransaction;

lightning/src/ln/msgs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ use bitcoin::hash_types::{Txid, BlockHash};
3232

3333
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
3434

35-
use std::{cmp, fmt};
36-
use std::fmt::Debug;
35+
#[cfg(not(feature = "no_std"))]
36+
use std::{cmp, fmt::{self, Debug}};
37+
#[cfg(feature = "no_std")]
38+
use core::{cmp, fmt::{self, Debug}};
3739
use std::io::Read;
3840

3941
use util::events::MessageSendEventsProvider;

lightning/src/ln/onchaintx.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ 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};
36-
use std::cmp;
37-
use std::ops::Deref;
38-
use std::mem::replace;
37+
#[cfg(feature = "no_std")]
38+
use hashbrown::{HashMap, hash_map};
39+
#[cfg(not(feature = "no_std"))]
40+
use std::{cmp, mem::replace, ops::Deref};
41+
#[cfg(feature = "no_std")]
42+
use core::{cmp, mem::replace, ops::Deref};
3943

4044
const MAX_ALLOC_SIZE: usize = 64*1024;
4145

lightning/src/ln/onion_route_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::secp256k1::key::SecretKey;
3434

3535
use std::default::Default;
36+
#[cfg(not(feature = "no_std"))]
3637
use std::sync::atomic::Ordering;
38+
#[cfg(feature = "no_std")]
39+
use core::sync::atomic::Ordering;
3740
use std::io;
3841

3942
use ln::functional_test_utils::*;

lightning/src/ln/peer_handler.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ 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};
35-
use std::sync::atomic::{AtomicUsize, Ordering};
36-
use std::{cmp, error, hash, fmt, mem};
37-
use std::ops::Deref;
39+
#[cfg(not(feature = "no_std"))]
40+
use std::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
41+
#[cfg(feature = "no_std")]
42+
use core::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
43+
use std::error;
3844

3945
use bitcoin::hashes::sha256::Hash as Sha256;
4046
use bitcoin::hashes::sha256::HashEngine as Sha256Engine;
@@ -1408,9 +1414,11 @@ mod tests {
14081414
use bitcoin::secp256k1::Secp256k1;
14091415
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
14101416

1411-
use std;
14121417
use std::sync::{Arc, Mutex};
1413-
use std::sync::atomic::Ordering;
1418+
#[cfg(not(feature = "no_std"))]
1419+
use std::{hash::{Hash, Hasher}, sync::atomic::Ordering};
1420+
#[cfg(feature = "no_std")]
1421+
use core::{hash::{Hash, Hasher}, sync::atomic::Ordering};
14141422

14151423
#[derive(Clone)]
14161424
struct FileDescriptor {
@@ -1423,8 +1431,8 @@ mod tests {
14231431
}
14241432
}
14251433
impl Eq for FileDescriptor { }
1426-
impl std::hash::Hash for FileDescriptor {
1427-
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
1434+
impl Hash for FileDescriptor {
1435+
fn hash<H: Hasher>(&self, hasher: &mut H) {
14281436
self.fd.hash(hasher)
14291437
}
14301438
}

lightning/src/ln/reorg_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ 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;
30+
#[cfg(not(feature = "no_std"))]
2731
use std::mem;
32+
#[cfg(feature = "no_std")]
33+
use core::mem;
2834

2935
use ln::functional_test_utils::*;
3036

lightning/src/routing/network_graph.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ use util::ser::{Writeable, Readable, Writer};
3131
use util::logger::Logger;
3232
use util::events::{MessageSendEvent, MessageSendEventsProvider};
3333

34-
use std::{cmp, fmt};
34+
#[cfg(not(feature = "no_std"))]
35+
use std::{cmp, fmt, ops::Deref};
36+
#[cfg(feature = "no_std")]
37+
use core::{cmp, fmt, ops::Deref};
3538
use std::sync::{RwLock, RwLockReadGuard};
39+
#[cfg(not(feature = "no_std"))]
3640
use std::sync::atomic::{AtomicUsize, Ordering};
41+
#[cfg(feature = "no_std")]
42+
use core::sync::atomic::{AtomicUsize, Ordering};
3743
use std::sync::Mutex;
3844
use std::collections::BTreeMap;
3945
use std::collections::btree_map::Entry as BtreeEntry;
40-
use std::ops::Deref;
4146
use bitcoin::hashes::hex::ToHex;
4247

4348
/// The maximum number of extra bytes which we do not understand in a gossip message before we will

lightning/src/routing/router.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ use routing::network_graph::{NetworkGraph, RoutingFees};
2121
use util::ser::{Writeable, Readable};
2222
use util::logger::Logger;
2323

24-
use std::cmp;
25-
use std::collections::{HashMap, BinaryHeap};
26-
use std::ops::Deref;
24+
#[cfg(not(feature = "no_std"))]
25+
use std::{cmp, ops::Deref};
26+
#[cfg(feature = "no_std")]
27+
use core::{cmp, ops::Deref};
28+
use std::collections::BinaryHeap;
29+
#[cfg(not(feature = "no_std"))]
30+
use std::collections::HashMap;
31+
#[cfg(feature = "no_std")]
32+
use hashbrown::HashMap;
2733

2834
/// A hop in a route
2935
#[derive(Clone, PartialEq)]

lightning/src/util/chacha20.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use std::io;
1313

1414
#[cfg(not(feature = "fuzztarget"))]
1515
mod real_chacha {
16+
#[cfg(not(feature = "no_std"))]
1617
use std::cmp;
18+
#[cfg(feature = "no_std")]
19+
use core::cmp;
1720
use util::byte_utils::{slice_to_le32, le32_to_array};
1821

1922
#[derive(Clone, Copy, PartialEq, Eq)]

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitment
1111
use ln::{chan_utils, msgs};
1212
use chain::keysinterface::{Sign, InMemorySigner};
1313

14+
#[cfg(not(feature = "no_std"))]
1415
use std::cmp;
16+
#[cfg(feature = "no_std")]
17+
use core::cmp;
1518
use std::sync::{Mutex, Arc};
1619

1720
use bitcoin::blockdata::transaction::{Transaction, SigHashType};

lightning/src/util/logger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
//! The second one, client-side by implementing check against Record Level field.
1515
//! Each module may have its own Logger or share one.
1616
17-
use std::cmp;
18-
use std::fmt;
17+
#[cfg(not(feature = "no_std"))]
18+
use std::{cmp, fmt};
19+
#[cfg(feature = "no_std")]
20+
use core::{cmp, fmt};
1921

2022
static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
2123

lightning/src/util/poly1305.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
// This is a port of Andrew Moons poly1305-donna
88
// https://github.com/floodyberry/poly1305-donna
99

10+
#[cfg(not(feature = "no_std"))]
1011
use std::cmp::min;
12+
#[cfg(feature = "no_std")]
13+
use core::cmp::min;
1114
use util::byte_utils::{slice_to_le32, le32_to_array};
1215

1316
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)