Skip to content

Commit 41d8d4d

Browse files
authored
Merge pull request #1008 from lightning-signer/2021-07-sync-no-std
Dummy sync implementation for no_std
2 parents ef4bfdc + 2e8f4fe commit 41d8d4d

18 files changed

+163
-33
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ jobs:
9090
if: "matrix.build-no-std && !matrix.coverage"
9191
run: |
9292
cd lightning
93-
cargo test --verbose --color always --features hashbrown
93+
cargo test --verbose --color always --no-default-features --features no_std
94+
# check if there is a conflict between no_std and the default std feature
95+
cargo test --verbose --color always --features no_std
9496
cd ..
9597
- name: Test on no_std bullds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
9698
if: "matrix.build-no-std && matrix.coverage"
9799
run: |
98100
cd lightning
99-
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features hashbrown
101+
RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --no-default-features --features no_std
100102
cd ..
101103
- name: Test on Rust ${{ matrix.toolchain }}
102104
if: "! matrix.build-net-tokio"

lightning/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ max_level_debug = []
2525
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2626
unsafe_revoked_tx_signing = []
2727
unstable = []
28+
2829
no_std = ["hashbrown"]
30+
std = []
31+
32+
default = ["std"]
2933

3034
[dependencies]
3135
bitcoin = "0.26"

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use util::events;
3838
use util::events::EventHandler;
3939

4040
use prelude::*;
41-
use std::sync::RwLock;
41+
use sync::RwLock;
4242
use core::ops::Deref;
4343

4444
/// An implementation of [`chain::Watch`] for monitoring channels.

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use prelude::*;
5555
use core::{cmp, mem};
5656
use std::io::Error;
5757
use core::ops::Deref;
58-
use std::sync::Mutex;
58+
use sync::Mutex;
5959

6060
/// An update generated by the underlying Channel itself which contains some new information the
6161
/// ChannelMonitor should be made aware of.
@@ -2843,7 +2843,7 @@ mod tests {
28432843
use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
28442844
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
28452845
use bitcoin::secp256k1::Secp256k1;
2846-
use std::sync::{Arc, Mutex};
2846+
use sync::{Arc, Mutex};
28472847
use chain::keysinterface::InMemorySigner;
28482848
use prelude::*;
28492849

lightning/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ mod prelude {
5353
#[cfg(feature = "hashbrown")]
5454
pub use self::hashbrown::{HashMap, HashSet, hash_map};
5555
}
56+
57+
#[cfg(feature = "std")]
58+
mod sync {
59+
pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard};
60+
}
61+
62+
#[cfg(not(feature = "std"))]
63+
mod sync;

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use ln::functional_test_utils::*;
4141
use util::test_utils;
4242

4343
use prelude::*;
44-
use std::sync::{Arc, Mutex};
44+
use sync::{Arc, Mutex};
4545

4646
// If persister_fail is true, we have the persister return a PermanentFailure
4747
// instead of the higher-level ChainMonitor.

lightning/src/ln/channel.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use util::scid_utils::scid_from_parts;
4444
use prelude::*;
4545
use core::{cmp,mem,fmt};
4646
use core::ops::Deref;
47-
#[cfg(any(test, feature = "fuzztarget"))]
48-
use std::sync::Mutex;
47+
#[cfg(any(test, feature = "fuzztarget", debug_assertions))]
48+
use sync::Mutex;
4949
use bitcoin::hashes::hex::ToHex;
5050
use bitcoin::blockdata::opcodes::all::OP_PUSHBYTES_0;
5151

@@ -374,10 +374,10 @@ pub(super) struct Channel<Signer: Sign> {
374374

375375
#[cfg(debug_assertions)]
376376
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
377-
holder_max_commitment_tx_output: ::std::sync::Mutex<(u64, u64)>,
377+
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
378378
#[cfg(debug_assertions)]
379379
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
380-
counterparty_max_commitment_tx_output: ::std::sync::Mutex<(u64, u64)>,
380+
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
381381

382382
last_sent_closing_fee: Option<(u32, u64, Signature)>, // (feerate, fee, holder_sig)
383383

@@ -595,9 +595,9 @@ impl<Signer: Sign> Channel<Signer> {
595595
monitor_pending_failures: Vec::new(),
596596

597597
#[cfg(debug_assertions)]
598-
holder_max_commitment_tx_output: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
598+
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
599599
#[cfg(debug_assertions)]
600-
counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
600+
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
601601

602602
last_sent_closing_fee: None,
603603

@@ -836,9 +836,9 @@ impl<Signer: Sign> Channel<Signer> {
836836
monitor_pending_failures: Vec::new(),
837837

838838
#[cfg(debug_assertions)]
839-
holder_max_commitment_tx_output: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
839+
holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
840840
#[cfg(debug_assertions)]
841-
counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
841+
counterparty_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
842842

843843
last_sent_closing_fee: None,
844844

@@ -4943,9 +4943,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
49434943
feerate_per_kw,
49444944

49454945
#[cfg(debug_assertions)]
4946-
holder_max_commitment_tx_output: ::std::sync::Mutex::new((0, 0)),
4946+
holder_max_commitment_tx_output: Mutex::new((0, 0)),
49474947
#[cfg(debug_assertions)]
4948-
counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((0, 0)),
4948+
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
49494949

49504950
last_sent_closing_fee,
49514951

@@ -5023,7 +5023,7 @@ mod tests {
50235023
use bitcoin::hashes::sha256::Hash as Sha256;
50245024
use bitcoin::hashes::Hash;
50255025
use bitcoin::hash_types::{Txid, WPubkeyHash};
5026-
use std::sync::Arc;
5026+
use sync::Arc;
50275027
use prelude::*;
50285028

50295029
struct TestFeeEstimator {

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use prelude::*;
6464
use core::{cmp, mem};
6565
use core::cell::RefCell;
6666
use std::io::{Cursor, Read};
67-
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
67+
use sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
6868
use core::sync::atomic::{AtomicUsize, Ordering};
6969
use core::time::Duration;
7070
#[cfg(any(test, feature = "allow_wallclock_use"))]
@@ -4951,14 +4951,15 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
49514951
#[cfg(test)]
49524952
mod tests {
49534953
use ln::channelmanager::PersistenceNotifier;
4954-
use std::sync::Arc;
4954+
use sync::Arc;
49554955
use core::sync::atomic::{AtomicBool, Ordering};
49564956
use std::thread;
49574957
use core::time::Duration;
49584958
use ln::functional_test_utils::*;
49594959
use ln::features::InitFeatures;
49604960
use ln::msgs::ChannelMessageHandler;
49614961

4962+
#[cfg(feature = "std")]
49624963
#[test]
49634964
fn test_wait_timeout() {
49644965
let persistence_notifier = Arc::new(PersistenceNotifier::new());
@@ -5094,7 +5095,7 @@ pub mod bench {
50945095
use bitcoin::hashes::sha256::Hash as Sha256;
50955096
use bitcoin::{Block, BlockHeader, Transaction, TxOut};
50965097

5097-
use std::sync::{Arc, Mutex};
5098+
use sync::{Arc, Mutex};
50985099

50995100
use test::Bencher;
51005101

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use bitcoin::secp256k1::key::PublicKey;
4242
use prelude::*;
4343
use core::cell::RefCell;
4444
use std::rc::Rc;
45-
use std::sync::{Arc, Mutex};
45+
use sync::{Arc, Mutex};
4646
use core::mem;
4747

4848
pub const CHAN_CONFIRM_DEPTH: u32 = 10;

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use regex;
5353
use prelude::*;
5454
use alloc::collections::BTreeSet;
5555
use core::default::Default;
56-
use std::sync::{Arc, Mutex};
56+
use sync::{Arc, Mutex};
5757

5858
use ln::functional_test_utils::*;
5959
use ln::chan_utils::CommitmentTransaction;

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use routing::network_graph::NetGraphMsgHandler;
3333
use prelude::*;
3434
use alloc::collections::LinkedList;
3535
use alloc::fmt::Debug;
36-
use std::sync::{Arc, Mutex};
36+
use sync::{Arc, Mutex};
3737
use core::sync::atomic::{AtomicUsize, Ordering};
3838
use core::{cmp, hash, fmt, mem};
3939
use core::ops::Deref;
@@ -1447,7 +1447,7 @@ mod tests {
14471447
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
14481448

14491449
use prelude::*;
1450-
use std::sync::{Arc, Mutex};
1450+
use sync::{Arc, Mutex};
14511451
use core::sync::atomic::Ordering;
14521452

14531453
#[derive(Clone)]

lightning/src/routing/network_graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
3535
use prelude::*;
3636
use alloc::collections::{BTreeMap, btree_map::Entry as BtreeEntry};
3737
use core::{cmp, fmt};
38-
use std::sync::{RwLock, RwLockReadGuard};
38+
use sync::{RwLock, RwLockReadGuard};
3939
use core::sync::atomic::{AtomicUsize, Ordering};
40-
use std::sync::Mutex;
40+
use sync::Mutex;
4141
use core::ops::Deref;
4242
use bitcoin::hashes::hex::ToHex;
4343

@@ -1088,7 +1088,7 @@ mod tests {
10881088
use bitcoin::secp256k1::{All, Secp256k1};
10891089

10901090
use prelude::*;
1091-
use std::sync::Arc;
1091+
use sync::Arc;
10921092

10931093
fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
10941094
let secp_ctx = Secp256k1::new();

lightning/src/routing/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ mod tests {
11981198
use bitcoin::secp256k1::{Secp256k1, All};
11991199

12001200
use prelude::*;
1201-
use std::sync::Arc;
1201+
use sync::{self, Arc};
12021202

12031203
fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
12041204
features: InitFeatures, outbound_capacity_msat: u64) -> channelmanager::ChannelDetails {
@@ -1321,7 +1321,7 @@ mod tests {
13211321
}
13221322
}
13231323

1324-
fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<std::sync::Arc<test_utils::TestChainSource>, std::sync::Arc<crate::util::test_utils::TestLogger>>, std::sync::Arc<test_utils::TestChainSource>, std::sync::Arc<test_utils::TestLogger>) {
1324+
fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>) {
13251325
let secp_ctx = Secp256k1::new();
13261326
let logger = Arc::new(test_utils::TestLogger::new());
13271327
let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));

lightning/src/sync.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
pub use ::alloc::sync::Arc;
2+
use core::ops::{Deref, DerefMut};
3+
use core::time::Duration;
4+
use core::cell::{RefCell, Ref, RefMut};
5+
6+
pub type LockResult<Guard> = Result<Guard, ()>;
7+
8+
pub struct Condvar {}
9+
10+
impl Condvar {
11+
pub fn new() -> Condvar {
12+
Condvar { }
13+
}
14+
15+
pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
16+
Ok(guard)
17+
}
18+
19+
#[allow(unused)]
20+
pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
21+
Ok((guard, ()))
22+
}
23+
24+
pub fn notify_all(&self) {}
25+
}
26+
27+
pub struct Mutex<T: ?Sized> {
28+
inner: RefCell<T>
29+
}
30+
31+
#[must_use = "if unused the Mutex will immediately unlock"]
32+
pub struct MutexGuard<'a, T: ?Sized + 'a> {
33+
lock: RefMut<'a, T>,
34+
}
35+
36+
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
37+
type Target = T;
38+
39+
fn deref(&self) -> &T {
40+
&self.lock.deref()
41+
}
42+
}
43+
44+
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
45+
fn deref_mut(&mut self) -> &mut T {
46+
self.lock.deref_mut()
47+
}
48+
}
49+
50+
impl<T> Mutex<T> {
51+
pub fn new(inner: T) -> Mutex<T> {
52+
Mutex { inner: RefCell::new(inner) }
53+
}
54+
55+
pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
56+
Ok(MutexGuard { lock: self.inner.borrow_mut() })
57+
}
58+
59+
pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
60+
Ok(MutexGuard { lock: self.inner.borrow_mut() })
61+
}
62+
}
63+
64+
pub struct RwLock<T: ?Sized> {
65+
inner: RefCell<T>
66+
}
67+
68+
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
69+
lock: Ref<'a, T>,
70+
}
71+
72+
pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
73+
lock: RefMut<'a, T>,
74+
}
75+
76+
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
77+
type Target = T;
78+
79+
fn deref(&self) -> &T {
80+
&self.lock.deref()
81+
}
82+
}
83+
84+
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
85+
type Target = T;
86+
87+
fn deref(&self) -> &T {
88+
&self.lock.deref()
89+
}
90+
}
91+
92+
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
93+
fn deref_mut(&mut self) -> &mut T {
94+
self.lock.deref_mut()
95+
}
96+
}
97+
98+
impl<T> RwLock<T> {
99+
pub fn new(inner: T) -> RwLock<T> {
100+
RwLock { inner: RefCell::new(inner) }
101+
}
102+
103+
pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
104+
Ok(RwLockReadGuard { lock: self.inner.borrow() })
105+
}
106+
107+
pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
108+
Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
109+
}
110+
111+
pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
112+
// There is no try, grasshopper - only used for tests and expected to fail
113+
Err(())
114+
}
115+
}

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
1313

1414
use prelude::*;
1515
use core::cmp;
16-
use std::sync::{Mutex, Arc};
16+
use sync::{Mutex, Arc};
1717

1818
use bitcoin::blockdata::transaction::{Transaction, SigHashType};
1919
use bitcoin::util::bip143;

lightning/src/util/logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub trait Logger {
124124
mod tests {
125125
use util::logger::{Logger, Level};
126126
use util::test_utils::TestLogger;
127-
use std::sync::Arc;
127+
use sync::Arc;
128128

129129
#[test]
130130
fn test_level_show() {

lightning/src/util/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use prelude::*;
1414
use std::io::{Read, Write};
1515
use core::hash::Hash;
16-
use std::sync::Mutex;
16+
use sync::Mutex;
1717
use core::cmp;
1818

1919
use bitcoin::secp256k1::Signature;

0 commit comments

Comments
 (0)