Skip to content

Commit a55161b

Browse files
committed
lightning: move no_std conditional into main lib
Move conditional compilation for no_std builds into main lib.rs
1 parent b441165 commit a55161b

30 files changed

+73
-205
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ use util::logger::Logger;
4242
use util::events;
4343
use util::events::Event;
4444

45-
#[cfg(not(feature = "no_std"))]
46-
use std::collections::{HashMap, hash_map};
47-
#[cfg(feature = "no_std")]
48-
use hashbrown::{HashMap, hash_map};
45+
use crate::{HashMap, hash_map, ops::Deref};
4946
use std::sync::RwLock;
50-
use std::ops::Deref;
5147

5248
/// An implementation of [`chain::Watch`] for monitoring channels.
5349
///

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ 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"))]
55-
use std::collections::{HashMap, HashSet, hash_map};
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};
54+
use crate::{HashMap, HashSet, cmp, hash_map, mem, ops::Deref};
6255
use std::io::Error;
6356
use std::sync::Mutex;
6457

lightning/src/chain/keysinterface.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ 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"))]
40-
use std::collections::HashSet;
41-
#[cfg(feature = "no_std")]
42-
use hashbrown::HashSet;
43-
#[cfg(not(feature = "no_std"))]
44-
use std::sync::atomic::{AtomicUsize, Ordering};
45-
#[cfg(feature = "no_std")]
46-
use core::sync::atomic::{AtomicUsize, Ordering};
39+
use crate::{HashSet, atomic::{AtomicUsize, Ordering}};
4740
use std::io::Error;
4841
use ln::msgs::{DecodeError, MAX_VALUE_MSAT};
4942

@@ -839,7 +832,7 @@ impl KeysManager {
839832
/// onchain output detection for which a corresponding delayed_payment_key must be derived.
840833
pub fn derive_channel_keys(&self, channel_value_satoshis: u64, params: &[u8; 32]) -> InMemorySigner {
841834
let chan_id = byte_utils::slice_to_be64(&params[0..8]);
842-
assert!(chan_id <= std::u32::MAX as u64); // Otherwise the params field wasn't created by us
835+
assert!(chan_id <= u32::MAX as u64); // Otherwise the params field wasn't created by us
843836
let mut unique_start = Sha256::engine();
844837
unique_start.input(params);
845838
unique_start.input(&self.seed);
@@ -1021,7 +1014,7 @@ impl KeysInterface for KeysManager {
10211014

10221015
fn get_channel_signer(&self, _inbound: bool, channel_value_satoshis: u64) -> Self::Signer {
10231016
let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
1024-
assert!(child_ix <= std::u32::MAX as usize);
1017+
assert!(child_ix <= u32::MAX as usize);
10251018
let mut id = [0; 32];
10261019
id[0..8].copy_from_slice(&byte_utils::be64_to_array(child_ix as u64));
10271020
id[8..16].copy_from_slice(&byte_utils::be64_to_array(self.starting_time_nanos as u64));

lightning/src/chain/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub trait Filter: Send + Sync {
137137
fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
138138
}
139139

140-
impl<T: Listen> Listen for std::ops::Deref<Target = T> {
140+
impl<T: Listen> Listen for crate::ops::Deref<Target = T> {
141141
fn block_connected(&self, block: &Block, height: u32) {
142142
(**self).block_connected(block, height);
143143
}
@@ -147,7 +147,7 @@ impl<T: Listen> Listen for std::ops::Deref<Target = T> {
147147
}
148148
}
149149

150-
impl<T: std::ops::Deref, U: std::ops::Deref> Listen for (T, U)
150+
impl<T: crate::ops::Deref, U: crate::ops::Deref> Listen for (T, U)
151151
where
152152
T::Target: Listen,
153153
U::Target: Listen,

lightning/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ extern crate bitcoin;
3838
#[cfg(feature = "no_std")] extern crate core;
3939
#[cfg(feature = "no_std")] extern crate hashbrown;
4040

41+
#[cfg(feature = "no_std")]
42+
use core::{cell, cmp, default, fmt, hash, iter, marker, mem, ops, sync::atomic, time};
43+
#[cfg(not(feature = "no_std"))]
44+
use std::{cell, cmp, default, fmt, hash, iter, marker, mem, ops, sync::atomic, time};
45+
46+
#[cfg(feature = "no_std")]
47+
use hashbrown::{HashMap, HashSet, hash_map};
48+
#[cfg(feature = "no_std")]
49+
use alloc::collections::{BinaryHeap, BTreeMap, BTreeSet, LinkedList, btree_map};
50+
#[cfg(not(feature = "no_std"))]
51+
use std::collections::{BinaryHeap, BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, btree_map, hash_map};
52+
4153
#[macro_use]
4254
pub mod util;
4355
pub mod chain;

lightning/src/ln/chan_utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ use bitcoin::secp256k1::{Secp256k1, Signature, Message};
3131
use bitcoin::secp256k1::Error as SecpError;
3232
use bitcoin::secp256k1;
3333

34-
#[cfg(not(feature = "no_std"))]
35-
use std::{cmp, ops::Deref};
36-
#[cfg(feature = "no_std")]
37-
use core::{cmp, ops::Deref};
34+
use crate::{cmp, ops::Deref};
3835
use ln::chan_utils;
3936
use util::transaction_utils::sort_outputs;
4037
use ln::channel::INITIAL_COMMITMENT_NUMBER;

lightning/src/ln/channel.rs

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

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};
41+
use crate::{cmp, mem, fmt, ops::Deref};
4542
#[cfg(any(test, feature = "fuzztarget"))]
4643
use std::sync::Mutex;
4744
use bitcoin::hashes::hex::ToHex;
@@ -1192,7 +1189,7 @@ impl<Signer: Sign> Channel<Signer> {
11921189
// on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
11931190
// these, but for now we just have to treat them as normal.
11941191

1195-
let mut pending_idx = std::usize::MAX;
1192+
let mut pending_idx = usize::MAX;
11961193
for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
11971194
if htlc.htlc_id == htlc_id_arg {
11981195
assert_eq!(htlc.payment_hash, payment_hash_calc);
@@ -1215,7 +1212,7 @@ impl<Signer: Sign> Channel<Signer> {
12151212
break;
12161213
}
12171214
}
1218-
if pending_idx == std::usize::MAX {
1215+
if pending_idx == usize::MAX {
12191216
return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
12201217
}
12211218

@@ -1314,7 +1311,7 @@ impl<Signer: Sign> Channel<Signer> {
13141311
// on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
13151312
// these, but for now we just have to treat them as normal.
13161313

1317-
let mut pending_idx = std::usize::MAX;
1314+
let mut pending_idx = usize::MAX;
13181315
for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
13191316
if htlc.htlc_id == htlc_id_arg {
13201317
match htlc.state {
@@ -1331,7 +1328,7 @@ impl<Signer: Sign> Channel<Signer> {
13311328
pending_idx = idx;
13321329
}
13331330
}
1334-
if pending_idx == std::usize::MAX {
1331+
if pending_idx == usize::MAX {
13351332
return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
13361333
}
13371334

@@ -4284,8 +4281,8 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
42844281

42854282
let mut key_data = VecWriter(Vec::new());
42864283
self.holder_signer.write(&mut key_data)?;
4287-
assert!(key_data.0.len() < std::usize::MAX);
4288-
assert!(key_data.0.len() < std::u32::MAX as usize);
4284+
assert!(key_data.0.len() < usize::MAX);
4285+
assert!(key_data.0.len() < u32::MAX as usize);
42894286
(key_data.0.len() as u32).write(writer)?;
42904287
writer.write_all(&key_data.0[..])?;
42914288

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,17 @@ use util::chacha20::{ChaCha20, ChaChaReader};
5555
use util::logger::Logger;
5656
use util::errors::APIError;
5757

58-
#[cfg(not(feature = "no_std"))]
59-
use std::{
58+
use crate::{
59+
HashMap,
60+
HashSet,
6061
cmp,
62+
hash_map,
6163
mem,
6264
marker::{Sync, Send},
6365
ops::Deref,
64-
sync::atomic::{AtomicUsize, Ordering},
66+
atomic::{AtomicUsize, Ordering},
6567
time::Duration,
6668
};
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"))]
77-
use std::collections::{HashMap, hash_map, HashSet};
78-
#[cfg(feature = "no_std")]
79-
use hashbrown::{HashMap, HashSet, hash_map};
8069
use std::io::{Cursor, Read};
8170
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
8271
#[cfg(any(test, feature = "allow_wallclock_use"))]
@@ -1594,7 +1583,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15941583
// be absurd. We ensure this by checking that at least 500 (our stated public contract on when
15951584
// broadcast_node_announcement panics) of the maximum-length addresses would fit in a 64KB
15961585
// message...
1597-
const HALF_MESSAGE_IS_ADDRS: u32 = ::std::u16::MAX as u32 / (NetAddress::MAX_LEN as u32 + 1) / 2;
1586+
const HALF_MESSAGE_IS_ADDRS: u32 = u16::MAX as u32 / (NetAddress::MAX_LEN as u32 + 1) / 2;
15981587
#[deny(const_err)]
15991588
#[allow(dead_code)]
16001589
// ...by failing to compile if the number of addresses that would be half of a message is
@@ -4322,12 +4311,8 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
43224311
mod tests {
43234312
use ln::channelmanager::PersistenceNotifier;
43244313
use std::sync::Arc;
4325-
#[cfg(not(feature = "no_std"))]
4326-
use std::sync::atomic::{AtomicBool, Ordering};
4327-
#[cfg(feature = "no_std")]
4328-
use core::sync::atomic::{AtomicBool, Ordering};
4314+
use crate::{atomic::{AtomicBool, Ordering}, time::Duration};
43294315
use std::thread;
4330-
use std::time::Duration;
43314316

43324317
#[test]
43334318
fn test_wait_timeout() {

lightning/src/ln/features.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
//! [`Features`]: struct.Features.html
2626
//! [`Context`]: sealed/trait.Context.html
2727
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};
28+
use crate::{cmp, fmt, marker::PhantomData};
3229

3330
use ln::msgs::DecodeError;
3431
use util::ser::{Readable, Writeable, Writer};

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,7 @@ use bitcoin::secp256k1::key::PublicKey;
4040

4141
use std::rc::Rc;
4242
use std::sync::Mutex;
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"))]
48-
use std::collections::HashMap;
49-
#[cfg(feature = "no_std")]
50-
use hashbrown::HashMap;
43+
use crate::{HashMap, cell::RefCell, mem};
5144

5245
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
5346

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,8 @@ use bitcoin::secp256k1::key::{PublicKey,SecretKey};
4747

4848
use regex;
4949

50-
#[cfg(not(feature = "no_std"))]
51-
use std::collections::{BTreeSet, HashMap, HashSet};
52-
#[cfg(feature = "no_std")]
53-
use hashbrown::{HashMap, HashSet};
54-
#[cfg(feature = "no_std")]
55-
use alloc::collections::BTreeSet;
56-
use std::default::Default;
5750
use std::sync::Mutex;
58-
#[cfg(not(feature = "no_std"))]
59-
use std::sync::atomic::Ordering;
60-
#[cfg(feature = "no_std")]
61-
use core::sync::atomic::Ordering;
51+
use crate::{BTreeSet, HashMap, HashSet, atomic::Ordering, default::Default};
6252

6353
use ln::functional_test_utils::*;
6454
use ln::chan_utils::CommitmentTransaction;

lightning/src/ln/msgs.rs

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

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

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}};
35+
use crate::{cmp, fmt::{self, Debug}};
3936
use std::io::Read;
4037

4138
use util::events::MessageSendEventsProvider;

lightning/src/ln/onchaintx.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ 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"))]
36-
use std::collections::{HashMap, hash_map};
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};
35+
use crate::{HashMap, hash_map, cmp, mem::replace, ops::Deref};
4336

4437
const MAX_ALLOC_SIZE: usize = 64*1024;
4538

@@ -303,8 +296,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
303296

304297
let mut key_data = VecWriter(Vec::new());
305298
self.signer.write(&mut key_data)?;
306-
assert!(key_data.0.len() < std::usize::MAX);
307-
assert!(key_data.0.len() < std::u32::MAX as usize);
299+
assert!(key_data.0.len() < usize::MAX);
300+
assert!(key_data.0.len() < u32::MAX as usize);
308301
(key_data.0.len() as u32).write(writer)?;
309302
writer.write_all(&key_data.0[..])?;
310303

@@ -696,7 +689,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
696689
log_trace!(logger, "Updating claims view at height {} with {} matched transactions and {} claim requests", height, txn_matched.len(), claimable_outpoints.len());
697690
let mut new_claims = Vec::new();
698691
let mut aggregated_claim = HashMap::new();
699-
let mut aggregated_soonest = ::std::u32::MAX;
692+
let mut aggregated_soonest = u32::MAX;
700693

701694
// Try to aggregate outputs if their timelock expiration isn't imminent (absolute_timelock
702695
// <= CLTV_SHARED_CLAIM_BUFFER) and they don't require an immediate nLockTime (aggregable).

lightning/src/ln/onion_route_tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ use bitcoin::hashes::Hash;
3232
use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::secp256k1::key::SecretKey;
3434

35-
use std::default::Default;
36-
#[cfg(not(feature = "no_std"))]
37-
use std::sync::atomic::Ordering;
38-
#[cfg(feature = "no_std")]
39-
use core::sync::atomic::Ordering;
35+
use crate::{atomic::Ordering, default::Default};
4036
use std::io;
4137

4238
use ln::functional_test_utils::*;

lightning/src/ln/onion_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
2727
use bitcoin::secp256k1;
2828

2929
use std::io::Cursor;
30-
use std::ops::Deref;
30+
use crate::ops::Deref;
3131

3232
pub(super) struct OnionKeys {
3333
#[cfg(test)]

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use bitcoin::hashes::hex::ToHex;
2525
/// Maximum Lightning message data length according to
2626
/// [BOLT-8](https://github.com/lightningnetwork/lightning-rfc/blob/v1.0/08-transport.md#lightning-message-specification)
2727
/// and [BOLT-1](https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md#lightning-message-format):
28-
pub const LN_MAX_MSG_LEN: usize = ::std::u16::MAX as usize; // Must be equal to 65535
28+
pub const LN_MAX_MSG_LEN: usize = u16::MAX as usize; // Must be equal to 65535
2929

3030
// Sha256("Noise_XK_secp256k1_ChaChaPoly_SHA256")
3131
const NOISE_CK: [u8; 32] = [0x26, 0x40, 0xf5, 0x2e, 0xeb, 0xcd, 0x9e, 0x88, 0x29, 0x58, 0x95, 0x1c, 0x79, 0x42, 0x50, 0xee, 0xdb, 0x28, 0x00, 0x2c, 0x05, 0xd7, 0xdc, 0x2e, 0xa0, 0xf1, 0x95, 0x40, 0x60, 0x42, 0xca, 0xf1];
@@ -715,7 +715,7 @@ mod tests {
715715
#[test]
716716
fn max_msg_len_limit_value() {
717717
assert_eq!(LN_MAX_MSG_LEN, 65535);
718-
assert_eq!(LN_MAX_MSG_LEN, ::std::u16::MAX as usize);
718+
assert_eq!(LN_MAX_MSG_LEN, u16::MAX as usize);
719719
}
720720

721721
#[test]

lightning/src/ln/peer_handler.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,9 @@ use util::events::{MessageSendEvent, MessageSendEventsProvider};
3030
use util::logger::Logger;
3131
use routing::network_graph::NetGraphMsgHandler;
3232

33-
#[cfg(not(feature = "no_std"))]
34-
use std::collections::LinkedList;
35-
#[cfg(feature = "no_std")]
36-
use alloc::collections::LinkedList;
37-
#[cfg(not(feature = "no_std"))]
38-
use std::collections::{HashMap, HashSet, hash_map};
39-
#[cfg(feature = "no_std")]
40-
use hashbrown::{HashMap, HashSet, hash_map};
33+
use crate::{HashMap, HashSet, LinkedList, hash_map};
4134
use std::sync::{Arc, Mutex};
42-
#[cfg(not(feature = "no_std"))]
43-
use std::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
44-
#[cfg(feature = "no_std")]
45-
use core::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
35+
use crate::{cmp, hash, fmt, mem, ops::Deref, atomic::{AtomicUsize, Ordering}};
4636
use std::error;
4737

4838
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -1418,10 +1408,7 @@ mod tests {
14181408
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
14191409

14201410
use std::sync::{Arc, Mutex};
1421-
#[cfg(not(feature = "no_std"))]
1422-
use std::{hash::{Hash, Hasher}, sync::atomic::Ordering};
1423-
#[cfg(feature = "no_std")]
1424-
use core::{hash::{Hash, Hasher}, sync::atomic::Ordering};
1411+
use crate::{hash::{Hash, Hasher}, atomic::Ordering};
14251412

14261413
#[derive(Clone)]
14271414
struct FileDescriptor {

0 commit comments

Comments
 (0)