Skip to content

Commit 559ed20

Browse files
Merge pull request #1756 from TheBlueMatt/2022-10-rgs-no-std
Fix `rapid-gossip-sync` `no-std` and properly test no-std in CI
2 parents a626828 + ada0df2 commit 559ed20

File tree

7 files changed

+47
-36
lines changed

7 files changed

+47
-36
lines changed

.github/workflows/build.yml

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,28 @@ jobs:
113113
if: "matrix.build-no-std && !matrix.coverage"
114114
shell: bash # Default on Winblows is powershell
115115
run: |
116-
cd lightning
117-
cargo test --verbose --color always --no-default-features --features no-std
118-
# check if there is a conflict between no-std and the default std feature
119-
cargo test --verbose --color always --features no-std
120-
# check that things still pass without grind_signatures
121-
# note that outbound_commitment_test only runs in this mode, because of hardcoded signature values
122-
cargo test --verbose --color always --no-default-features --features std
123-
# check if there is a conflict between no-std and the c_bindings cfg
124-
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
125-
cd ..
126-
cd lightning-invoice
127-
cargo test --verbose --color always --no-default-features --features no-std
128-
# check if there is a conflict between no-std and the default std feature
129-
cargo test --verbose --color always --features no-std
130-
# check if there is a conflict between no-std and the c_bindings cfg
131-
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
116+
for DIR in lightning lightning-invoice lightning-rapid-gossip-sync; do
117+
cd $DIR
118+
cargo test --verbose --color always --no-default-features --features no-std
119+
# check if there is a conflict between no-std and the default std feature
120+
cargo test --verbose --color always --features no-std
121+
# check that things still pass without grind_signatures
122+
# note that outbound_commitment_test only runs in this mode, because of hardcoded signature values
123+
cargo test --verbose --color always --no-default-features --features std
124+
# check if there is a conflict between no-std and the c_bindings cfg
125+
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
126+
cd ..
127+
done
132128
# check no-std compatibility across dependencies
133-
cd ..
134129
cd no-std-check
135130
cargo check --verbose --color always
136-
cd ..
131+
- name: Build no-std-check on Rust ${{ matrix.toolchain }} for ARM Embedded
132+
if: "matrix.build-no-std && matrix.platform == 'ubuntu-latest'"
133+
run: |
134+
cd no-std-check
135+
rustup target add thumbv7m-none-eabi
136+
sudo apt-get -y install gcc-arm-none-eabi
137+
cargo build --target=thumbv7m-none-eabi
137138
- name: Test on no-std builds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
138139
if: "matrix.build-no-std && matrix.coverage"
139140
run: |

lightning-rapid-gossip-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ std = ["lightning/std"]
1616
_bench_unstable = []
1717

1818
[dependencies]
19-
lightning = { version = "0.0.111", path = "../lightning" }
19+
lightning = { version = "0.0.111", path = "../lightning", default-features = false }
2020
bitcoin = { version = "0.29.0", default-features = false }
2121

2222
[dev-dependencies]

lightning-rapid-gossip-sync/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt::Debug;
2-
use std::fmt::Formatter;
2+
use core::fmt::Formatter;
33
use lightning::ln::msgs::{DecodeError, LightningError};
44

55
/// All-encompassing standard error type that processing can return
@@ -12,8 +12,8 @@ pub enum GraphSyncError {
1212
LightningError(LightningError),
1313
}
1414

15-
impl From<std::io::Error> for GraphSyncError {
16-
fn from(error: std::io::Error) -> Self {
15+
impl From<lightning::io::Error> for GraphSyncError {
16+
fn from(error: lightning::io::Error) -> Self {
1717
Self::DecodeError(DecodeError::Io(error.kind()))
1818
}
1919
}
@@ -31,7 +31,7 @@ impl From<LightningError> for GraphSyncError {
3131
}
3232

3333
impl Debug for GraphSyncError {
34-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
3535
match self {
3636
GraphSyncError::DecodeError(e) => f.write_fmt(format_args!("DecodeError: {:?}", e)),
3737
GraphSyncError::LightningError(e) => f.write_fmt(format_args!("LightningError: {:?}", e))

lightning-rapid-gossip-sync/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//!
3939
//! After the gossip data snapshot has been downloaded, one of the client's graph processing
4040
//! functions needs to be called. In this example, we process the update by reading its contents
41-
//! from disk, which we do by calling [sync_network_graph_with_file_path]:
41+
//! from disk, which we do by calling [`RapidGossipSync::update_network_graph`]:
4242
//!
4343
//! ```
4444
//! use bitcoin::blockdata::constants::genesis_block;
@@ -56,15 +56,20 @@
5656
//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
5757
//! let network_graph = NetworkGraph::new(block_hash, &logger);
5858
//! let rapid_sync = RapidGossipSync::new(&network_graph);
59-
//! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path("./rapid_sync.lngossip");
59+
//! let snapshot_contents: &[u8] = &[0; 0];
60+
//! let new_last_sync_timestamp_result = rapid_sync.update_network_graph(snapshot_contents);
6061
//! ```
61-
//! [sync_network_graph_with_file_path]: RapidGossipSync::sync_network_graph_with_file_path
62+
63+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
6264

6365
// Allow and import test features for benching
6466
#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
6567
#[cfg(all(test, feature = "_bench_unstable"))]
6668
extern crate test;
6769

70+
#[cfg(not(feature = "std"))]
71+
extern crate alloc;
72+
6873
#[cfg(feature = "std")]
6974
use std::fs::File;
7075
use core::ops::Deref;
@@ -143,6 +148,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
143148
}
144149
}
145150

151+
#[cfg(feature = "std")]
146152
#[cfg(test)]
147153
mod tests {
148154
use std::fs;

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use lightning::io;
1616
use crate::error::GraphSyncError;
1717
use crate::RapidGossipSync;
1818

19+
#[cfg(not(feature = "std"))]
20+
use alloc::{vec::Vec, borrow::ToOwned};
21+
1922
/// The purpose of this prefix is to identify the serialization format, should other rapid gossip
2023
/// sync formats arise in the future.
2124
///
@@ -47,7 +50,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
4750
let backdated_timestamp = latest_seen_timestamp.saturating_sub(24 * 3600 * 7);
4851

4952
let node_id_count: u32 = Readable::read(read_cursor)?;
50-
let mut node_ids: Vec<PublicKey> = Vec::with_capacity(std::cmp::min(
53+
let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
5154
node_id_count,
5255
MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
5356
) as usize);
@@ -132,7 +135,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
132135
htlc_maximum_msat: default_htlc_maximum_msat,
133136
fee_base_msat: default_fee_base_msat,
134137
fee_proportional_millionths: default_fee_proportional_millionths,
135-
excess_data: vec![],
138+
excess_data: Vec::new(),
136139
}
137140
} else {
138141
// 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
162165
htlc_maximum_msat: directional_info.htlc_maximum_msat,
163166
fee_base_msat: directional_info.fees.base_msat,
164167
fee_proportional_millionths: directional_info.fees.proportional_millionths,
165-
excess_data: vec![],
168+
excess_data: Vec::new(),
166169
}
167170
};
168171

lightning/src/ln/peer_handler.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use prelude::*;
3636
use io;
3737
use alloc::collections::LinkedList;
3838
use sync::{Arc, Mutex, MutexGuard, FairRwLock};
39-
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
39+
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
4040
use core::{cmp, hash, fmt, mem};
4141
use core::ops::Deref;
4242
use core::convert::Infallible;
@@ -540,7 +540,7 @@ pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: D
540540

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

545545
our_node_secret: SecretKey,
546546
ephemeral_key_midstate: Sha256Engine,
@@ -594,7 +594,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, OM: Deref, L: Deref> PeerManager<D
594594
/// minute should suffice.
595595
///
596596
/// (C-not exported) as we can't export a PeerManager with a dummy route handler
597-
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 {
597+
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 {
598598
Self::new(MessageHandler {
599599
chan_handler: channel_message_handler,
600600
route_handler: IgnoringMessageHandler{},
@@ -620,7 +620,7 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
620620
/// cryptographically secure random bytes.
621621
///
622622
/// (C-not exported) as we can't export a PeerManager with a dummy channel handler
623-
pub fn new_routing_only(routing_message_handler: RM, our_node_secret: SecretKey, current_time: u64, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
623+
pub fn new_routing_only(routing_message_handler: RM, our_node_secret: SecretKey, current_time: u32, ephemeral_random_data: &[u8; 32], logger: L) -> Self {
624624
Self::new(MessageHandler {
625625
chan_handler: ErroringMessageHandler::new(),
626626
route_handler: routing_message_handler,
@@ -684,7 +684,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
684684
/// incremented irregularly internally. In general it is best to simply use the current UNIX
685685
/// timestamp, however if it is not available a persistent counter that increases once per
686686
/// minute should suffice.
687-
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 {
687+
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 {
688688
let mut ephemeral_key_midstate = Sha256::engine();
689689
ephemeral_key_midstate.input(ephemeral_random_data);
690690

@@ -701,7 +701,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
701701
our_node_secret,
702702
ephemeral_key_midstate,
703703
peer_counter: AtomicCounter::new(),
704-
last_node_announcement_serial: AtomicU64::new(current_time),
704+
last_node_announcement_serial: AtomicU32::new(current_time),
705705
logger,
706706
custom_message_handler,
707707
secp_ctx,
@@ -2001,7 +2001,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20012001
.or(self.message_handler.onion_message_handler.provided_node_features());
20022002
let announcement = msgs::UnsignedNodeAnnouncement {
20032003
features,
2004-
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,
2004+
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel),
20052005
node_id: PublicKey::from_secret_key(&self.secp_ctx, &self.our_node_secret),
20062006
rgb, alias, addresses,
20072007
excess_address_data: Vec::new(),

no-std-check/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#![no_std]

0 commit comments

Comments
 (0)