From: Gene Ferneau Date: Wed, 19 May 2021 04:21:39 +0000 (+0000) Subject: Use alloc for no_std builds X-Git-Tag: v0.0.98~15^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=12461fcba179a1b6726f5c2557d12cb1f1b33b25;p=rust-lightning Use alloc for no_std builds Replace std structs with alloc equivalents to support no_std builds f use prelude::* credit @devrandom --- diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 8e6ddc76c..a3365f2b6 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -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 { 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() diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index de8268141..e53ce4523 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -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() { diff --git a/lightning/src/chain/keysinterface.rs b/lightning/src/chain/keysinterface.rs index d6fea17d4..1f1185620 100644 --- a/lightning/src/chain/keysinterface.rs +++ b/lightning/src/chain/keysinterface.rs @@ -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; diff --git a/lightning/src/chain/mod.rs b/lightning/src/chain/mod.rs index 856a9e8af..e4bb66c7e 100644 --- a/lightning/src/chain/mod.rs +++ b/lightning/src/chain/mod.rs @@ -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; diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index 4e6a067fc..04de721f9 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -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; diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 2e1e50371..999ddd6c1 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -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; diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index 3c09f3867..a9f46b43a 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -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 diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index 28b5a9f8b..750ee30db 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -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() { diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 2e758aa48..7773ffeac 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -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 diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 518723378..1c1b85ca1 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -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 diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 9d4d55c40..cb75599cd 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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 ChannelMana #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] pub fn get_and_clear_pending_events(&self) -> Vec { - 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() diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index cbe8cedf1..b459baf06 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -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 diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 590f95e22..2b276c928 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -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; diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index d8963e3c3..762fe6688 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -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; diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index 3de0595d2..7500b93c0 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -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}; diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 94136a9ee..038973241 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -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] diff --git a/lightning/src/ln/onion_route_tests.rs b/lightning/src/ln/onion_route_tests.rs index bab78e7a1..3fc01f1bb 100644 --- a/lightning/src/ln/onion_route_tests.rs +++ b/lightning/src/ln/onion_route_tests.rs @@ -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; diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 07686a214..f80fd676c 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -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(secp_ctx: & #[cfg(test)] mod tests { + use prelude::*; use ln::PaymentHash; use ln::features::{ChannelFeatures, NodeFeatures}; use routing::router::{Route, RouteHop}; diff --git a/lightning/src/ln/peer_channel_encryptor.rs b/lightning/src/ln/peer_channel_encryptor.rs index dd8619bf2..76385fc8b 100644 --- a/lightning/src/ln/peer_channel_encryptor.rs +++ b/lightning/src/ln/peer_channel_encryptor.rs @@ -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; diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 097e99286..33c4050b3 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -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; diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index f81c42a36..2e67d790f 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -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; diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 532ebbf31..3433d9e1a 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -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]; diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index 16e0a978a..8e2dc9f7f 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -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, NetGraphMsgHandler, Arc>) { diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 1f77681bf..90618e743 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -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 {} diff --git a/lightning/src/util/chacha20.rs b/lightning/src/util/chacha20.rs index e8c6e229c..69e4ed8d1 100644 --- a/lightning/src/util/chacha20.rs +++ b/lightning/src/util/chacha20.rs @@ -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; diff --git a/lightning/src/util/enforcing_trait_impls.rs b/lightning/src/util/enforcing_trait_impls.rs index 55e45d309..26f9d87bb 100644 --- a/lightning/src/util/enforcing_trait_impls.rs +++ b/lightning/src/util/enforcing_trait_impls.rs @@ -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}; diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs index b47e134ef..ddcc693b3 100644 --- a/lightning/src/util/errors.rs +++ b/lightning/src/util/errors.rs @@ -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 diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index 013e3c4e6..650df285f 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -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. /// diff --git a/lightning/src/util/message_signing.rs b/lightning/src/util/message_signing.rs index 1ad1ea5ed..c39da1d53 100644 --- a/lightning/src/util/message_signing.rs +++ b/lightning/src/util/message_signing.rs @@ -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}; diff --git a/lightning/src/util/poly1305.rs b/lightning/src/util/poly1305.rs index c6dbb43dd..73db1577b 100644 --- a/lightning/src/util/poly1305.rs +++ b/lightning/src/util/poly1305.rs @@ -205,6 +205,7 @@ impl Poly1305 { #[cfg(test)] mod test { + use prelude::*; use core::iter::repeat; use util::poly1305::Poly1305; diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 66c89aa83..da66741db 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -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; diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 87f990a6f..872901893 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -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}; diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index c167c3412..43bb1ef93 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -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}; diff --git a/lightning/src/util/transaction_utils.rs b/lightning/src/util/transaction_utils.rs index 34d0fb275..5c02fe156 100644 --- a/lightning/src/util/transaction_utils.rs +++ b/lightning/src/util/transaction_utils.rs @@ -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 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 { diff --git a/lightning/src/util/zbase32.rs b/lightning/src/util/zbase32.rs index e17ac3e79..454fe4b55 100644 --- a/lightning/src/util/zbase32.rs +++ b/lightning/src/util/zbase32.rs @@ -8,6 +8,8 @@ * at your option. */ +use prelude::*; + const ALPHABET: &'static [u8] = b"ybndrfg8ejkmcpqxot1uwisza345h769"; /// Encodes some bytes as a zbase32 string