Skip to content

Fix rapid-gossip-sync no-std and properly test no-std in CI #1756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,28 @@ jobs:
if: "matrix.build-no-std && !matrix.coverage"
shell: bash # Default on Winblows is powershell
run: |
cd lightning
cargo test --verbose --color always --no-default-features --features no-std
# check if there is a conflict between no-std and the default std feature
cargo test --verbose --color always --features no-std
# check that things still pass without grind_signatures
# note that outbound_commitment_test only runs in this mode, because of hardcoded signature values
cargo test --verbose --color always --no-default-features --features std
# check if there is a conflict between no-std and the c_bindings cfg
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
cd ..
cd lightning-invoice
cargo test --verbose --color always --no-default-features --features no-std
# check if there is a conflict between no-std and the default std feature
cargo test --verbose --color always --features no-std
# check if there is a conflict between no-std and the c_bindings cfg
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
for DIR in lightning lightning-invoice lightning-rapid-gossip-sync; do
cd $DIR
cargo test --verbose --color always --no-default-features --features no-std
# check if there is a conflict between no-std and the default std feature
cargo test --verbose --color always --features no-std
# check that things still pass without grind_signatures
# note that outbound_commitment_test only runs in this mode, because of hardcoded signature values
cargo test --verbose --color always --no-default-features --features std
# check if there is a conflict between no-std and the c_bindings cfg
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
cd ..
done
# check no-std compatibility across dependencies
cd ..
cd no-std-check
cargo check --verbose --color always
cd ..
- name: Build no-std-check on Rust ${{ matrix.toolchain }} for ARM Embedded
if: "matrix.build-no-std && matrix.platform == 'ubuntu-latest'"
run: |
cd no-std-check
rustup target add thumbv7m-none-eabi
sudo apt-get -y install gcc-arm-none-eabi
cargo build --target=thumbv7m-none-eabi
- name: Test on no-std builds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
if: "matrix.build-no-std && matrix.coverage"
run: |
Expand Down
2 changes: 1 addition & 1 deletion lightning-rapid-gossip-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions lightning-rapid-gossip-sync/src/error.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()))
}
}
Expand All @@ -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))
Expand Down
12 changes: 9 additions & 3 deletions lightning-rapid-gossip-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//!
//! After the gossip data snapshot has been downloaded, one of the client's graph processing
//! functions needs to be called. In this example, we process the update by reading its contents
//! from disk, which we do by calling [sync_network_graph_with_file_path]:
//! from disk, which we do by calling [`RapidGossipSync::update_network_graph`]:
//!
//! ```
//! use bitcoin::blockdata::constants::genesis_block;
Expand All @@ -56,15 +56,20 @@
//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
//! let network_graph = NetworkGraph::new(block_hash, &logger);
//! let rapid_sync = RapidGossipSync::new(&network_graph);
//! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path("./rapid_sync.lngossip");
//! let snapshot_contents: &[u8] = &[0; 0];
//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph(snapshot_contents);
//! ```
//! [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;
Expand Down Expand Up @@ -143,6 +148,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
}
}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use std::fs;
Expand Down
9 changes: 6 additions & 3 deletions lightning-rapid-gossip-sync/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
}
};

Expand Down
14 changes: 7 additions & 7 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use prelude::*;
use io;
use alloc::collections::LinkedList;
use sync::{Arc, Mutex, MutexGuard, FairRwLock};
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use core::{cmp, hash, fmt, mem};
use core::ops::Deref;
use core::convert::Infallible;
Expand Down Expand Up @@ -540,7 +540,7 @@ pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: D

/// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
/// value increases strictly since we don't assume access to a time source.
last_node_announcement_serial: AtomicU64,
last_node_announcement_serial: AtomicU32,

our_node_secret: SecretKey,
ephemeral_key_midstate: Sha256Engine,
Expand Down Expand Up @@ -594,7 +594,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, OM: Deref, L: Deref> PeerManager<D
/// minute should suffice.
///
/// (C-not exported) as we can't export a PeerManager with a dummy route handler
pub fn new_channel_only(channel_message_handler: CM, onion_message_handler: OM, our_node_secret: SecretKey, current_time: u64, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
pub fn new_channel_only(channel_message_handler: CM, onion_message_handler: OM, our_node_secret: SecretKey, current_time: u32, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
Self::new(MessageHandler {
chan_handler: channel_message_handler,
route_handler: IgnoringMessageHandler{},
Expand All @@ -620,7 +620,7 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
/// cryptographically secure random bytes.
///
/// (C-not exported) as we can't export a PeerManager with a dummy channel handler
pub fn new_routing_only(routing_message_handler: RM, our_node_secret: SecretKey, current_time: u64, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
pub fn new_routing_only(routing_message_handler: RM, our_node_secret: SecretKey, current_time: u32, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
Self::new(MessageHandler {
chan_handler: ErroringMessageHandler::new(),
route_handler: routing_message_handler,
Expand Down Expand Up @@ -684,7 +684,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
/// incremented irregularly internally. In general it is best to simply use the current UNIX
/// timestamp, however if it is not available a persistent counter that increases once per
/// minute should suffice.
pub fn new(message_handler: MessageHandler<CM, RM, OM>, our_node_secret: SecretKey, current_time: u64, ephemeral_random_data: &[u8; 32], logger: L, custom_message_handler: CMH) -> Self {
pub fn new(message_handler: MessageHandler<CM, RM, OM>, our_node_secret: SecretKey, current_time: u32, ephemeral_random_data: &[u8; 32], logger: L, custom_message_handler: CMH) -> Self {
let mut ephemeral_key_midstate = Sha256::engine();
ephemeral_key_midstate.input(ephemeral_random_data);

Expand All @@ -701,7 +701,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
our_node_secret,
ephemeral_key_midstate,
peer_counter: AtomicCounter::new(),
last_node_announcement_serial: AtomicU64::new(current_time),
last_node_announcement_serial: AtomicU32::new(current_time),
logger,
custom_message_handler,
secp_ctx,
Expand Down Expand Up @@ -2001,7 +2001,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
.or(self.message_handler.onion_message_handler.provided_node_features());
let announcement = msgs::UnsignedNodeAnnouncement {
features,
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel),
node_id: PublicKey::from_secret_key(&self.secp_ctx, &self.our_node_secret),
rgb, alias, addresses,
excess_address_data: Vec::new(),
Expand Down
1 change: 1 addition & 0 deletions no-std-check/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#![no_std]