]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Correct `rapid-gossip-sync` `no-std` build and test 2022-10-rgs-111-no-std
authorMatt Corallo <git@bluematt.me>
Thu, 6 Oct 2022 15:42:41 +0000 (15:42 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 6 Oct 2022 17:08:39 +0000 (17:08 +0000)
While `rapid-gossip-sync` recently gained a `no-std` feature, it
didn't actually work, as there were still dangling references to
`std` and prelude assumptions. This makes `rapid-gossip-sync`
build (and test) properly in `no-std`.

lightning-rapid-gossip-sync/Cargo.toml
lightning-rapid-gossip-sync/src/error.rs
lightning-rapid-gossip-sync/src/lib.rs
lightning-rapid-gossip-sync/src/processing.rs

index 2fa598dc1a22c11be6c84e4938d7fba6be4b98d9..4dec0aa85784ea318c7c5e591a21a62f05668a83 100644 (file)
@@ -16,7 +16,7 @@ std = ["lightning/std"]
 _bench_unstable = []
 
 [dependencies]
-lightning = { version = "0.0.111", path = "../lightning" }
+lightning = { version = "0.0.111", path = "../lightning", default-features = false }
 bitcoin = { version = "0.29.0", default-features = false }
 
 [dev-dependencies]
index fee8feafc32e48512be7976465d8089ad0b54872..ffd6760f8a9edda95b1c8ca33017e0f80c6d0fa0 100644 (file)
@@ -1,5 +1,5 @@
 use core::fmt::Debug;
-use std::fmt::Formatter;
+use core::fmt::Formatter;
 use lightning::ln::msgs::{DecodeError, LightningError};
 
 /// All-encompassing standard error type that processing can return
@@ -12,8 +12,8 @@ pub enum GraphSyncError {
        LightningError(LightningError),
 }
 
-impl From<std::io::Error> for GraphSyncError {
-       fn from(error: std::io::Error) -> Self {
+impl From<lightning::io::Error> for GraphSyncError {
+       fn from(error: lightning::io::Error) -> Self {
                Self::DecodeError(DecodeError::Io(error.kind()))
        }
 }
@@ -31,7 +31,7 @@ impl From<LightningError> for GraphSyncError {
 }
 
 impl Debug for GraphSyncError {
-       fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+       fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
                match self {
                        GraphSyncError::DecodeError(e) => f.write_fmt(format_args!("DecodeError: {:?}", e)),
                        GraphSyncError::LightningError(e) => f.write_fmt(format_args!("LightningError: {:?}", e))
index d948d447bfcafa7f67bd59b59fb5f91adc765c59..72fc1f719473b28933491f7dfba1b2b33485856c 100644 (file)
 //! ```
 //! [sync_network_graph_with_file_path]: RapidGossipSync::sync_network_graph_with_file_path
 
+#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
+
 // Allow and import test features for benching
 #![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
 #[cfg(all(test, feature = "_bench_unstable"))]
 extern crate test;
 
+#[cfg(not(feature = "std"))]
+extern crate alloc;
+
 #[cfg(feature = "std")]
 use std::fs::File;
 use core::ops::Deref;
@@ -156,6 +161,7 @@ mod tests {
        use crate::RapidGossipSync;
 
        #[test]
+       #[cfg(feature = "std")]
        fn test_sync_from_file() {
                struct FileSyncTest {
                        directory: String,
@@ -243,6 +249,7 @@ mod tests {
        }
 
        #[test]
+       #[cfg(feature = "std")]
        fn measure_native_read_from_file() {
                let block_hash = genesis_block(Network::Bitcoin).block_hash();
                let logger = TestLogger::new();
index 18f9916316a5ffe9c7e0bf4dde7231bfea626789..3a60b7c986c85354ee5789f8dadeaf3f3e6d519f 100644 (file)
@@ -16,6 +16,9 @@ use lightning::io;
 use crate::error::GraphSyncError;
 use crate::RapidGossipSync;
 
+#[cfg(not(feature = "std"))]
+use alloc::{vec::Vec, borrow::ToOwned};
+
 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
 /// sync formats arise in the future.
 ///
@@ -47,7 +50,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
                let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
 
                let node_id_count: u32 = Readable::read(read_cursor)?;
-               let mut node_ids: Vec<PublicKey> = Vec::with_capacity(std::cmp::min(
+               let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
                        node_id_count,
                        MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
                ) as usize);
@@ -132,7 +135,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
                                        htlc_maximum_msat: default_htlc_maximum_msat,
                                        fee_base_msat: default_fee_base_msat,
                                        fee_proportional_millionths: default_fee_proportional_millionths,
-                                       excess_data: vec![],
+                                       excess_data: Vec::new(),
                                }
                        } else {
                                // incremental update, field flags will indicate mutated values
@@ -162,7 +165,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
                                        htlc_maximum_msat: directional_info.htlc_maximum_msat,
                                        fee_base_msat: directional_info.fees.base_msat,
                                        fee_proportional_millionths: directional_info.fees.proportional_millionths,
-                                       excess_data: vec![],
+                                       excess_data: Vec::new(),
                                }
                        };