Use alloc for no_std builds
authorGene Ferneau <gene@ferneau.link>
Wed, 19 May 2021 04:21:39 +0000 (04:21 +0000)
committerGene Ferneau <gene@ferneau.link>
Thu, 27 May 2021 17:35:20 +0000 (17:35 +0000)
Replace std structs with alloc equivalents to support no_std builds

f use prelude::* credit @devrandom

35 files changed:
lightning/src/chain/chainmonitor.rs
lightning/src/chain/channelmonitor.rs
lightning/src/chain/keysinterface.rs
lightning/src/chain/mod.rs
lightning/src/chain/onchaintx.rs
lightning/src/chain/package.rs
lightning/src/lib.rs
lightning/src/ln/chan_utils.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/features.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/mod.rs
lightning/src/ln/msgs.rs
lightning/src/ln/onion_route_tests.rs
lightning/src/ln/onion_utils.rs
lightning/src/ln/peer_channel_encryptor.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/reorg_tests.rs
lightning/src/ln/wire.rs
lightning/src/routing/network_graph.rs
lightning/src/routing/router.rs
lightning/src/util/chacha20.rs
lightning/src/util/enforcing_trait_impls.rs
lightning/src/util/errors.rs
lightning/src/util/events.rs
lightning/src/util/message_signing.rs
lightning/src/util/poly1305.rs
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs
lightning/src/util/test_utils.rs
lightning/src/util/transaction_utils.rs
lightning/src/util/zbase32.rs

index 8e6ddc76c4911e2d79343e58a199453535e08d76..a3365f2b64fd58302eef8e292961e53384743896 100644 (file)
@@ -37,6 +37,7 @@ use util::logger::Logger;
 use util::events;
 use util::events::EventHandler;
 
+use prelude::*;
 use std::collections::{HashMap, hash_map};
 use std::sync::RwLock;
 use core::ops::Deref;
@@ -143,7 +144,7 @@ where C::Target: chain::Filter,
        #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
        pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
                use util::events::EventsProvider;
-               let events = std::cell::RefCell::new(Vec::new());
+               let events = core::cell::RefCell::new(Vec::new());
                let event_handler = |event| events.borrow_mut().push(event);
                self.process_pending_events(&event_handler);
                events.into_inner()
index de82681415d4edf11b2bd55993584c1787ab6c93..e53ce45234ca35e238624bdf7fd6e770dd03357f 100644 (file)
@@ -51,6 +51,7 @@ use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
 use util::byte_utils;
 use util::events::Event;
 
+use prelude::*;
 use std::collections::{HashMap, HashSet};
 use core::{cmp, mem};
 use std::io::Error;
@@ -2911,6 +2912,7 @@ mod tests {
        use bitcoin::secp256k1::Secp256k1;
        use std::sync::{Arc, Mutex};
        use chain::keysinterface::InMemorySigner;
+       use prelude::*;
 
        #[test]
        fn test_prune_preimages() {
index d6fea17d430583b977fd724559e7b1d32857b095..1f11856200841b011a058a812d2133507b7a9e55 100644 (file)
@@ -37,6 +37,7 @@ use ln::chan_utils;
 use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction};
 use ln::msgs::UnsignedChannelAnnouncement;
 
+use prelude::*;
 use std::collections::HashSet;
 use core::sync::atomic::{AtomicUsize, Ordering};
 use std::io::Error;
index 856a9e8af62287b76f3e174f2543091361eca300..e4bb66c7e755905058fa7b0f44520046773b5601 100644 (file)
@@ -18,6 +18,8 @@ use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitor
 use chain::keysinterface::Sign;
 use chain::transaction::{OutPoint, TransactionData};
 
+use prelude::*;
+
 pub mod chaininterface;
 pub mod chainmonitor;
 pub mod channelmonitor;
index 4e6a067fc72c25e311b8526700dc80410f2ac3e0..04de721f901a3a9e67ea01bca8ce2c6e3df3f362 100644 (file)
@@ -32,6 +32,7 @@ use util::logger::Logger;
 use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter};
 use util::byte_utils;
 
+use prelude::*;
 use std::collections::HashMap;
 use core::cmp;
 use core::ops::Deref;
index 2e1e50371dc569d4a1f22b937da2ec7e661bbae6..999ddd6c164cbca55a858a8b50db23f0ec1ed8da 100644 (file)
@@ -31,9 +31,9 @@ use util::byte_utils;
 use util::logger::Logger;
 use util::ser::{Readable, Writer, Writeable};
 
-use std::cmp;
-use std::mem;
-use std::ops::Deref;
+use core::cmp;
+use core::mem;
+use core::ops::Deref;
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
index 3c09f386753040320c27199086790b022944df26..a9f46b43a4026f650069cb97f9baf1dd62a0d968 100644 (file)
@@ -31,6 +31,7 @@
 #![cfg_attr(all(any(test, feature = "_test_utils"), feature = "unstable"), feature(test))]
 #[cfg(all(any(test, feature = "_test_utils"), feature = "unstable"))] extern crate test;
 
+extern crate alloc;
 extern crate bitcoin;
 extern crate core;
 #[cfg(any(test, feature = "_test_utils"))] extern crate hex;
@@ -41,3 +42,7 @@ pub mod util;
 pub mod chain;
 pub mod ln;
 pub mod routing;
+
+mod prelude {
+       pub use alloc::{vec, vec::Vec, string::String};
+}
\ No newline at end of file
index 28b5a9f8bc9239f15b7a1e0af328de15b3aa2070..750ee30db14d5469d685c6df54cf79d01416dd13 100644 (file)
@@ -31,6 +31,7 @@ use bitcoin::secp256k1::{Secp256k1, Signature, Message};
 use bitcoin::secp256k1::Error as SecpError;
 use bitcoin::secp256k1;
 
+use prelude::*;
 use core::cmp;
 use ln::chan_utils;
 use util::transaction_utils::sort_outputs;
@@ -1235,6 +1236,7 @@ fn script_for_p2wpkh(key: &PublicKey) -> Script {
 mod tests {
        use super::CounterpartyCommitmentSecrets;
        use hex;
+       use prelude::*;
 
        #[test]
        fn test_per_commitment_storage() {
index 2e758aa48c82a0eecf1a9761741cf37e81ecabc6..7773ffeacf246380b233ac5cfc1e27bbac782aa6 100644 (file)
@@ -38,6 +38,7 @@ use ln::functional_test_utils::*;
 
 use util::test_utils;
 
+use prelude::*;
 use std::collections::HashMap;
 
 // If persister_fail is true, we have the persister return a PermanentFailure
index 51872337801559f5d5630ddada69d3952b3952e0..1c1b85ca1727aa90e9902b8130230571a28cc7cf 100644 (file)
@@ -40,6 +40,7 @@ use util::errors::APIError;
 use util::config::{UserConfig,ChannelConfig};
 use util::scid_utils::scid_from_parts;
 
+use prelude::*;
 use core::{cmp,mem,fmt};
 use core::ops::Deref;
 #[cfg(any(test, feature = "fuzztarget"))]
@@ -4928,6 +4929,7 @@ mod tests {
        use bitcoin::hashes::Hash;
        use bitcoin::hash_types::{Txid, WPubkeyHash};
        use std::sync::Arc;
+       use prelude::*;
 
        struct TestFeeEstimator {
                fee_est: u32
index 9d4d55c406f9aad4e84d81b97d3ea7bfacb5bd3f..cb75599cd90037d2b9c95aa03efe8563d498173f 100644 (file)
@@ -61,8 +61,9 @@ use util::chacha20::{ChaCha20, ChaChaReader};
 use util::logger::Logger;
 use util::errors::APIError;
 
+use prelude::*;
 use core::{cmp, mem};
-use std::cell::RefCell;
+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};
@@ -3685,7 +3686,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
 
        #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
        pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
-               let events = std::cell::RefCell::new(Vec::new());
+               let events = core::cell::RefCell::new(Vec::new());
                let event_handler = |event| events.borrow_mut().push(event);
                self.process_pending_events(&event_handler);
                events.into_inner()
index cbe8cedf1dd1c41d958a46b1c3b8a4357e4186cc..b459baf06580bff8414715b82c1845e7334c23ee 100644 (file)
@@ -22,6 +22,7 @@
 //! [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
 //! [messages]: crate::ln::msgs
 
+use prelude::*;
 use core::{cmp, fmt};
 use core::marker::PhantomData;
 
@@ -31,6 +32,7 @@ use ln::msgs::DecodeError;
 use util::ser::{Readable, Writeable, Writer};
 
 mod sealed {
+       use prelude::*;
        use ln::features::Features;
 
        /// The context in which [`Features`] are applicable. Defines which features are required and
index 590f95e22f80852c1371a1dc091ecd2cbc32ac3f..2b276c92898b85b21cf13941a2ae33e40861644d 100644 (file)
@@ -39,6 +39,7 @@ use bitcoin::hash_types::BlockHash;
 
 use bitcoin::secp256k1::key::PublicKey;
 
+use prelude::*;
 use core::cell::RefCell;
 use std::rc::Rc;
 use std::sync::Mutex;
index d8963e3c3ef16e34661bf265c481f7383f57b321..762fe668807be845f7933ce079dd00737cd061eb 100644 (file)
@@ -50,7 +50,9 @@ use bitcoin::secp256k1::key::{PublicKey,SecretKey};
 
 use regex;
 
-use std::collections::{BTreeSet, HashMap, HashSet};
+use prelude::*;
+use alloc::collections::BTreeSet;
+use std::collections::{HashMap, HashSet};
 use core::default::Default;
 use std::sync::Mutex;
 
index 3de0595d295e037d7810ff8ff31899ae29a6dafc..7500b93c0050400ae1bba70dd78a819336a5afbd 100644 (file)
@@ -68,6 +68,7 @@ pub struct PaymentPreimage(pub [u8;32]);
 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
 pub struct PaymentSecret(pub [u8;32]);
 
+use prelude::*;
 use bitcoin::bech32;
 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
 
index 94136a9ee65cb16fb66d292ea0f5e4bcf714b9b8..03897324150a2ffc8150502c184ff2c71367a916 100644 (file)
@@ -32,6 +32,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
 
 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
 
+use prelude::*;
 use core::{cmp, fmt};
 use core::fmt::Debug;
 use std::io::Read;
@@ -867,6 +868,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
 }
 
 mod fuzzy_internal_msgs {
+       use prelude::*;
        use ln::PaymentSecret;
 
        // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
@@ -1841,6 +1843,7 @@ mod tests {
        use bitcoin::secp256k1::key::{PublicKey,SecretKey};
        use bitcoin::secp256k1::{Secp256k1, Message};
 
+       use prelude::*;
        use std::io::Cursor;
 
        #[test]
index bab78e7a1c75504c94452401a77732a1956a9a7f..3fc01f1bb8b4ea7ce285091d9c2a4aea7170ec5d 100644 (file)
@@ -32,6 +32,7 @@ use bitcoin::hashes::Hash;
 use bitcoin::secp256k1::Secp256k1;
 use bitcoin::secp256k1::key::SecretKey;
 
+use prelude::*;
 use core::default::Default;
 use std::io;
 
index 07686a214e23c90c3e1166bca19580da55309777..f80fd676c75d14397ac2e4c693f64cc3b2fffa50 100644 (file)
@@ -27,6 +27,7 @@ use bitcoin::secp256k1::Secp256k1;
 use bitcoin::secp256k1::ecdh::SharedSecret;
 use bitcoin::secp256k1;
 
+use prelude::*;
 use std::io::Cursor;
 use core::ops::Deref;
 
@@ -478,6 +479,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
 
 #[cfg(test)]
 mod tests {
+       use prelude::*;
        use ln::PaymentHash;
        use ln::features::{ChannelFeatures, NodeFeatures};
        use routing::router::{Route, RouteHop};
index dd8619bf275b424263b014d349540ef68456238c..76385fc8b063221259fbe3b99d791e49e28cfa15 100644 (file)
@@ -7,6 +7,8 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
+use prelude::*;
+
 use ln::msgs::LightningError;
 use ln::msgs;
 
index 097e9928677bb4a288e8d44820a1fb7df9567e42..33c4050b374dd21fa4eeb3486bb179ae91c85f4e 100644 (file)
@@ -30,7 +30,9 @@ use util::events::{MessageSendEvent, MessageSendEventsProvider};
 use util::logger::Logger;
 use routing::network_graph::NetGraphMsgHandler;
 
-use std::collections::{HashMap,hash_map,HashSet,LinkedList};
+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};
@@ -1421,6 +1423,7 @@ mod tests {
        use bitcoin::secp256k1::Secp256k1;
        use bitcoin::secp256k1::key::{SecretKey, PublicKey};
 
+       use prelude::*;
        use std::sync::{Arc, Mutex};
        use core::sync::atomic::Ordering;
 
index f81c42a36f710a1515f092bc7f000a1d79085f03..2e67d790ffb50f008cccfa54f9d94f4acdb9aea8 100644 (file)
@@ -22,6 +22,7 @@ use util::ser::{ReadableArgs, Writeable};
 use bitcoin::blockdata::block::{Block, BlockHeader};
 use bitcoin::hash_types::BlockHash;
 
+use prelude::*;
 use std::collections::HashMap;
 use core::mem;
 
index 532ebbf312511da55c6fc1f1c021b4d64d41d1e3..3433d9e1a3968681d2fb9356807cfeeed26ebc2c 100644 (file)
@@ -351,6 +351,7 @@ impl Encode for msgs::GossipTimestampFilter {
 mod tests {
        use super::*;
        use util::byte_utils;
+       use prelude::*;
 
        // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
        const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
index 16e0a978a91802c24dbb9ed1db49f960b5be0725..8e2dc9f7fa10c364e27dcd88eaf6bb9621936979 100644 (file)
@@ -32,12 +32,12 @@ use util::logger::Logger;
 use util::events::{MessageSendEvent, MessageSendEventsProvider};
 use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
 
+use prelude::*;
+use alloc::collections::{BTreeMap, btree_map::Entry as BtreeEntry};
 use core::{cmp, fmt};
 use std::sync::{RwLock, RwLockReadGuard};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Mutex;
-use std::collections::BTreeMap;
-use std::collections::btree_map::Entry as BtreeEntry;
 use core::ops::Deref;
 use bitcoin::hashes::hex::ToHex;
 
@@ -1140,6 +1140,7 @@ mod tests {
        use bitcoin::secp256k1::key::{PublicKey, SecretKey};
        use bitcoin::secp256k1::{All, Secp256k1};
 
+       use prelude::*;
        use std::sync::Arc;
 
        fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
index 1f77681bf06b673de4d44e23f4050ceb6b1beefa..90618e743001e23ac53390253bb5569e2ac82ad5 100644 (file)
@@ -21,8 +21,10 @@ use routing::network_graph::{NetworkGraph, RoutingFees};
 use util::ser::{Writeable, Readable};
 use util::logger::Logger;
 
+use prelude::*;
+use alloc::collections::BinaryHeap;
 use core::cmp;
-use std::collections::{HashMap, BinaryHeap};
+use std::collections::HashMap;
 use core::ops::Deref;
 
 /// A hop in a route
@@ -1193,6 +1195,7 @@ mod tests {
        use bitcoin::secp256k1::key::{PublicKey,SecretKey};
        use bitcoin::secp256k1::{Secp256k1, All};
 
+       use prelude::*;
        use std::sync::Arc;
 
        // Using the same keys for LN and BTC ids
@@ -3945,6 +3948,7 @@ mod benches {
        use super::*;
        use util::logger::{Logger, Record};
 
+       use prelude::*;
        use test::Bencher;
 
        struct DummyLogger {}
index e8c6e229c1c00129cc22ace046845c1d6b5e5922..69e4ed8d113d1cdb5a101a5fa1b60338a68d13be 100644 (file)
@@ -318,6 +318,7 @@ impl<'a, R: io::Read> io::Read for ChaChaReader<'a, R> {
 
 #[cfg(test)]
 mod test {
+       use prelude::*;
        use core::iter::repeat;
 
        use super::ChaCha20;
index 55e45d309454dd286a8b357a65dbd61b9fccfe9c..26f9d87bba9bc40c03d6f5439c4828d500be0be3 100644 (file)
@@ -11,6 +11,7 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitment
 use ln::{chan_utils, msgs};
 use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
 
+use prelude::*;
 use core::cmp;
 use std::sync::{Mutex, Arc};
 
index b47e134efab29524c220c7781bb2d610d72dbf2e..ddcc693b3398e969b364c89b4ee4a73684d33153 100644 (file)
@@ -9,6 +9,7 @@
 
 //! Error types live here.
 
+use alloc::string::String;
 use core::fmt;
 
 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
index 013e3c4e611bbdfb130ca85094911499e65a6da5..650df285f1d7dbd37131f6d0ca0364cf4327a1b9 100644 (file)
@@ -23,8 +23,9 @@ use bitcoin::blockdata::script::Script;
 
 use bitcoin::secp256k1::key::PublicKey;
 
+use prelude::*;
 use core::time::Duration;
-use std::ops::Deref;
+use core::ops::Deref;
 
 /// An Event which you should probably take some action in response to.
 ///
index 1ad1ea5ed3571582dc67e8e4187c7d06175b2680..c39da1d533e51b7fbc0f94f30b1947040139be54 100644 (file)
@@ -20,6 +20,7 @@
 //! https://lightning.readthedocs.io/lightning-signmessage.7.html
 //! https://api.lightning.community/#signmessage
 
+use prelude::*;
 use crate::util::zbase32;
 use bitcoin::hashes::{sha256d, Hash};
 use bitcoin::secp256k1::recovery::{RecoverableSignature, RecoveryId};
index c6dbb43dd0d455f3538bcfb5d6edc9af101d6688..73db1577bb6df75feac3f2a26ed8047d21b2c0f7 100644 (file)
@@ -205,6 +205,7 @@ impl Poly1305 {
 
 #[cfg(test)]
 mod test {
+       use prelude::*;
        use core::iter::repeat;
 
        use util::poly1305::Poly1305;
index 66c89aa83913177bf6b31c9d5abb90c94c022884..da66741dbf43137966c750554fd034e913d9db5d 100644 (file)
@@ -10,6 +10,7 @@
 //! A very simple serialization framework which is used to serialize/deserialize messages as well
 //! as ChannelsManagers and ChannelMonitors.
 
+use prelude::*;
 use std::io::{Read, Write};
 use std::collections::HashMap;
 use core::hash::Hash;
index 87f990a6f0677ee1f2a12735d0d8176ecf3af4d9..8729018935c944b10a24acefa24e52bf8debbf49 100644 (file)
@@ -277,6 +277,7 @@ macro_rules! read_tlv_fields {
 
 #[cfg(test)]
 mod tests {
+       use prelude::*;
        use std::io::{Cursor, Read};
        use ln::msgs::DecodeError;
        use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter};
index c167c3412637408ae4008921ee9360dfc9b7f932..43bb1ef930391d7966af82308d74aef1ad3e6073 100644 (file)
@@ -36,6 +36,7 @@ use bitcoin::secp256k1::recovery::RecoverableSignature;
 
 use regex;
 
+use prelude::*;
 use core::time::Duration;
 use std::sync::{Mutex, Arc};
 use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
index 34d0fb275764a339ce13c9b50bef0eaebda43683..5c02fe156aaeffad0355c9f95aaafc3fbba26c88 100644 (file)
@@ -14,6 +14,7 @@ use bitcoin::consensus::encode::VarInt;
 
 use ln::msgs::MAX_VALUE_MSAT;
 
+use prelude::*;
 use core::cmp::Ordering;
 
 pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
@@ -83,6 +84,8 @@ mod tests {
 
        use hex::decode;
 
+       use alloc::vec;
+
        #[test]
        fn sort_output_by_value() {
                let txout1 = TxOut {
index e17ac3e793786e76a3c8cb0427e24fd67a246335..454fe4b55440a9844edf2ea15098eef18c3071f5 100644 (file)
@@ -8,6 +8,8 @@
  * at your option.
 */
 
+use prelude::*;
+
 const ALPHABET: &'static [u8] = b"ybndrfg8ejkmcpqxot1uwisza345h769";
 
 /// Encodes some bytes as a zbase32 string