From: Elias Rohrer Date: Wed, 14 Sep 2022 08:17:56 +0000 (+0200) Subject: Add `no-std` support for RGS X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=90710d63c557bb26f70ad822b0cc1cd346ada1c6;p=rust-lightning Add `no-std` support for RGS --- diff --git a/lightning-rapid-gossip-sync/Cargo.toml b/lightning-rapid-gossip-sync/Cargo.toml index 776709447..2fa598dc1 100644 --- a/lightning-rapid-gossip-sync/Cargo.toml +++ b/lightning-rapid-gossip-sync/Cargo.toml @@ -10,6 +10,9 @@ Utility to process gossip routing data from Rapid Gossip Sync Server. """ [features] +default = ["std"] +no-std = ["lightning/no-std"] +std = ["lightning/std"] _bench_unstable = [] [dependencies] diff --git a/lightning-rapid-gossip-sync/src/lib.rs b/lightning-rapid-gossip-sync/src/lib.rs index 9142b0c89..d948d447b 100644 --- a/lightning-rapid-gossip-sync/src/lib.rs +++ b/lightning-rapid-gossip-sync/src/lib.rs @@ -65,10 +65,12 @@ #[cfg(all(test, feature = "_bench_unstable"))] extern crate test; +#[cfg(feature = "std")] use std::fs::File; -use std::ops::Deref; -use std::sync::atomic::{AtomicBool, Ordering}; +use core::ops::Deref; +use core::sync::atomic::{AtomicBool, Ordering}; +use lightning::io; use lightning::routing::gossip::NetworkGraph; use lightning::util::logger::Logger; @@ -107,6 +109,7 @@ impl>, L: Deref> RapidGossipSync where L /// /// `sync_path`: Path to the file where the gossip update data is located /// + #[cfg(feature = "std")] pub fn sync_network_graph_with_file_path( &self, sync_path: &str, @@ -115,6 +118,17 @@ impl>, L: Deref> RapidGossipSync where L self.update_network_graph_from_byte_stream(&mut file) } + /// Update network graph from binary data. + /// Returns the last sync timestamp to be used the next time rapid sync data is queried. + /// + /// `network_graph`: network graph to be updated + /// + /// `update_data`: `&[u8]` binary stream that comprises the update data + pub fn update_network_graph(&self, update_data: &[u8]) -> Result { + let mut read_cursor = io::Cursor::new(update_data); + self.update_network_graph_from_byte_stream(&mut read_cursor) + } + /// Gets a reference to the underlying [`NetworkGraph`] which was provided in /// [`RapidGossipSync::new`]. /// diff --git a/lightning-rapid-gossip-sync/src/processing.rs b/lightning-rapid-gossip-sync/src/processing.rs index 818e19fe4..18f991631 100644 --- a/lightning-rapid-gossip-sync/src/processing.rs +++ b/lightning-rapid-gossip-sync/src/processing.rs @@ -1,8 +1,6 @@ -use std::cmp::max; -use std::io; -use std::io::Read; -use std::ops::Deref; -use std::sync::atomic::Ordering; +use core::cmp::max; +use core::ops::Deref; +use core::sync::atomic::Ordering; use bitcoin::BlockHash; use bitcoin::secp256k1::PublicKey; @@ -13,6 +11,7 @@ use lightning::ln::msgs::{ use lightning::routing::gossip::NetworkGraph; use lightning::util::logger::Logger; use lightning::util::ser::{BigSize, Readable}; +use lightning::io; use crate::error::GraphSyncError; use crate::RapidGossipSync; @@ -28,19 +27,7 @@ const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1]; const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000; impl>, L: Deref> RapidGossipSync where L::Target: Logger { - /// Update network graph from binary data. - /// Returns the last sync timestamp to be used the next time rapid sync data is queried. - /// - /// `network_graph`: network graph to be updated - /// - /// `update_data`: `&[u8]` binary stream that comprises the update data - pub fn update_network_graph(&self, update_data: &[u8]) -> Result { - let mut read_cursor = io::Cursor::new(update_data); - self.update_network_graph_from_byte_stream(&mut read_cursor) - } - - - pub(crate) fn update_network_graph_from_byte_stream( + pub(crate) fn update_network_graph_from_byte_stream( &self, mut read_cursor: &mut R, ) -> Result { diff --git a/no-std-check/Cargo.toml b/no-std-check/Cargo.toml index 15d2c196c..947838633 100644 --- a/no-std-check/Cargo.toml +++ b/no-std-check/Cargo.toml @@ -4,8 +4,9 @@ version = "0.1.0" edition = "2018" [features] -default = ["lightning/no-std", "lightning-invoice/no-std"] +default = ["lightning/no-std", "lightning-invoice/no-std", "lightning-rapid-gossip-sync/no-std"] [dependencies] lightning = { path = "../lightning", default-features = false } lightning-invoice = { path = "../lightning-invoice", default-features = false } +lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync", default-features = false }