Skip to content

Commit 11ab302

Browse files
committed
Remove uneccessary std bounds on many tests
We never actually build with `#![no_std]` in tests as Rust does not support it. Thus, many tests have spurious `std` feature gates when they run just fine without them. Here we remove those gates, though note that tests that depend on behavior elsewhere in the codebase which is `std`-gated obviously need to retain their feature gates.
1 parent 5412784 commit 11ab302

File tree

7 files changed

+46
-105
lines changed

7 files changed

+46
-105
lines changed

lightning/src/ln/bolt11_payment.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,22 @@ mod tests {
9191
use crate::routing::router::Payee;
9292
use bitcoin::hashes::sha256::Hash as Sha256;
9393
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
94-
use core::time::Duration;
9594
use lightning_invoice::{Currency, InvoiceBuilder};
96-
#[cfg(feature = "std")]
9795
use std::time::SystemTime;
9896

99-
fn duration_since_epoch() -> Duration {
100-
#[cfg(feature = "std")]
101-
let duration_since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
102-
#[cfg(not(feature = "std"))]
103-
let duration_since_epoch = Duration::from_secs(1234567);
104-
duration_since_epoch
105-
}
106-
10797
#[test]
10898
fn invoice_test() {
10999
let payment_hash = Sha256::hash(&[0; 32]);
110100
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
111101
let secp_ctx = Secp256k1::new();
112102
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
113103

104+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
114105
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
115106
.description("test".into())
116107
.payment_hash(payment_hash)
117108
.payment_secret(PaymentSecret([0; 32]))
118-
.duration_since_epoch(duration_since_epoch())
109+
.duration_since_epoch(timestamp)
119110
.min_final_cltv_expiry_delta(144)
120111
.amount_milli_satoshis(128)
121112
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &private_key))
@@ -142,11 +133,12 @@ mod tests {
142133
let secp_ctx = Secp256k1::new();
143134
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
144135

136+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
145137
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
146138
.description("test".into())
147139
.payment_hash(payment_hash)
148140
.payment_secret(PaymentSecret([0; 32]))
149-
.duration_since_epoch(duration_since_epoch())
141+
.duration_since_epoch(timestamp)
150142
.min_final_cltv_expiry_delta(144)
151143
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &private_key))
152144
.unwrap();
@@ -167,12 +159,12 @@ mod tests {
167159
}
168160

169161
#[test]
170-
#[cfg(feature = "std")]
171162
fn payment_metadata_end_to_end() {
172163
use crate::events::Event;
173164
use crate::ln::channelmanager::{PaymentId, Retry};
174165
use crate::ln::functional_test_utils::*;
175166
use crate::ln::msgs::ChannelMessageHandler;
167+
176168
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
177169
// the way out through the `PaymentClaimable` event.
178170
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -188,12 +180,12 @@ mod tests {
188180

189181
let secp_ctx = Secp256k1::new();
190182
let node_secret = nodes[1].keys_manager.backing.get_node_secret_key();
191-
let time = std::time::SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
183+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
192184
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
193185
.description("test".into())
194186
.payment_hash(Sha256::from_slice(&payment_hash.0).unwrap())
195187
.payment_secret(payment_secret)
196-
.duration_since_epoch(time)
188+
.duration_since_epoch(timestamp)
197189
.min_final_cltv_expiry_delta(144)
198190
.amount_milli_satoshis(50_000)
199191
.payment_metadata(payment_metadata.clone())

lightning/src/ln/functional_test_utils.rs

+21-32
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::util::test_channel_signer::TestChannelSigner;
3434
#[cfg(test)]
3535
use crate::util::test_channel_signer::SignerOp;
3636
use crate::util::test_utils;
37-
use crate::util::test_utils::{panicking, TestChainMonitor, TestScorer, TestKeysInterface};
37+
use crate::util::test_utils::{TestChainMonitor, TestScorer, TestKeysInterface};
3838
use crate::util::ser::{ReadableArgs, Writeable};
3939

4040
use bitcoin::amount::Amount;
@@ -194,28 +194,23 @@ impl ConnectStyle {
194194
}
195195

196196
fn random_style() -> ConnectStyle {
197-
#[cfg(feature = "std")] {
198-
use core::hash::{BuildHasher, Hasher};
199-
// Get a random value using the only std API to do so - the DefaultHasher
200-
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
201-
let res = match rand_val % 9 {
202-
0 => ConnectStyle::BestBlockFirst,
203-
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
204-
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
205-
3 => ConnectStyle::TransactionsFirst,
206-
4 => ConnectStyle::TransactionsFirstSkippingBlocks,
207-
5 => ConnectStyle::TransactionsDuplicativelyFirstSkippingBlocks,
208-
6 => ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks,
209-
7 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
210-
8 => ConnectStyle::FullBlockViaListen,
211-
_ => unreachable!(),
212-
};
213-
eprintln!("Using Block Connection Style: {:?}", res);
214-
res
215-
}
216-
#[cfg(not(feature = "std"))] {
217-
ConnectStyle::FullBlockViaListen
218-
}
197+
use core::hash::{BuildHasher, Hasher};
198+
// Get a random value using the only std API to do so - the DefaultHasher
199+
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
200+
let res = match rand_val % 9 {
201+
0 => ConnectStyle::BestBlockFirst,
202+
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
203+
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
204+
3 => ConnectStyle::TransactionsFirst,
205+
4 => ConnectStyle::TransactionsFirstSkippingBlocks,
206+
5 => ConnectStyle::TransactionsDuplicativelyFirstSkippingBlocks,
207+
6 => ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks,
208+
7 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
209+
8 => ConnectStyle::FullBlockViaListen,
210+
_ => unreachable!(),
211+
};
212+
eprintln!("Using Block Connection Style: {:?}", res);
213+
res
219214
}
220215
}
221216

@@ -270,9 +265,7 @@ fn do_connect_block_with_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, '
270265

271266
fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
272267
let height = node.best_block_info().1 + 1;
273-
#[cfg(feature = "std")] {
274-
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
275-
}
268+
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
276269
// Update the block internally before handing it over to LDK, to ensure our assertions regarding
277270
// transaction broadcast are correct.
278271
node.blocks.lock().unwrap().push((block.clone(), height));
@@ -340,9 +333,7 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b
340333

341334
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
342335
call_claimable_balances(node);
343-
#[cfg(feature = "std")] {
344-
eprintln!("Disconnecting {} blocks using Block Connection Style: {:?}", count, *node.connect_style.borrow());
345-
}
336+
eprintln!("Disconnecting {} blocks using Block Connection Style: {:?}", count, *node.connect_style.borrow());
346337
for i in 0..count {
347338
let orig = node.blocks.lock().unwrap().pop().unwrap();
348339
assert!(orig.1 > 0); // Cannot disconnect genesis
@@ -471,9 +462,7 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
471462
}
472463
}
473464

474-
#[cfg(feature = "std")]
475465
impl<'a, 'b, 'c> std::panic::UnwindSafe for Node<'a, 'b, 'c> {}
476-
#[cfg(feature = "std")]
477466
impl<'a, 'b, 'c> std::panic::RefUnwindSafe for Node<'a, 'b, 'c> {}
478467
impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
479468
pub fn best_block_hash(&self) -> BlockHash {
@@ -620,7 +609,7 @@ impl<'a, 'b: 'a, 'c: 'b> NodeHolder for Node<'a, 'b, 'c> {
620609

621610
impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
622611
fn drop(&mut self) {
623-
if !panicking() {
612+
if !std::thread::panicking() {
624613
// Check that we processed all pending events
625614
let msg_events = self.node.get_and_clear_pending_msg_events();
626615
if !msg_events.is_empty() {

lightning/src/ln/invoice_utils.rs

-13
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ mod test {
824824
use crate::sign::PhantomKeysManager;
825825
use crate::events::{MessageSendEvent, MessageSendEventsProvider};
826826
use crate::ln::types::PaymentHash;
827-
#[cfg(feature = "std")]
828827
use crate::ln::types::PaymentPreimage;
829828
use crate::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry};
830829
use crate::ln::functional_test_utils::*;
@@ -1281,13 +1280,11 @@ mod test {
12811280
}
12821281

12831282
#[test]
1284-
#[cfg(feature = "std")]
12851283
fn test_multi_node_receive() {
12861284
do_test_multi_node_receive(true);
12871285
do_test_multi_node_receive(false);
12881286
}
12891287

1290-
#[cfg(feature = "std")]
12911288
fn do_test_multi_node_receive(user_generated_pmt_hash: bool) {
12921289
use crate::events::{Event, EventsProvider};
12931290
use core::cell::RefCell;
@@ -1395,7 +1392,6 @@ mod test {
13951392
}
13961393

13971394
#[test]
1398-
#[cfg(feature = "std")]
13991395
fn test_multi_node_hints_has_htlc_min_max_values() {
14001396
let mut chanmon_cfgs = create_chanmon_cfgs(3);
14011397
let seed_1 = [42u8; 32];
@@ -1432,7 +1428,6 @@ mod test {
14321428
}
14331429

14341430
#[test]
1435-
#[cfg(feature = "std")]
14361431
fn test_create_phantom_invoice_with_description_hash() {
14371432
let chanmon_cfgs = create_chanmon_cfgs(3);
14381433
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -1462,7 +1457,6 @@ mod test {
14621457
}
14631458

14641459
#[test]
1465-
#[cfg(feature = "std")]
14661460
fn create_phantom_invoice_with_custom_payment_hash_and_custom_min_final_cltv_delta() {
14671461
let chanmon_cfgs = create_chanmon_cfgs(3);
14681462
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -1489,7 +1483,6 @@ mod test {
14891483
}
14901484

14911485
#[test]
1492-
#[cfg(feature = "std")]
14931486
fn test_multi_node_hints_includes_single_channels_to_participating_nodes() {
14941487
let mut chanmon_cfgs = create_chanmon_cfgs(3);
14951488
let seed_1 = [42u8; 32];
@@ -1518,7 +1511,6 @@ mod test {
15181511
}
15191512

15201513
#[test]
1521-
#[cfg(feature = "std")]
15221514
fn test_multi_node_hints_includes_one_channel_of_each_counterparty_nodes_per_participating_node() {
15231515
let mut chanmon_cfgs = create_chanmon_cfgs(4);
15241516
let seed_1 = [42u8; 32];
@@ -1549,7 +1541,6 @@ mod test {
15491541
}
15501542

15511543
#[test]
1552-
#[cfg(feature = "std")]
15531544
fn test_multi_node_forwarding_info_not_assigned_channel_excluded_from_hints() {
15541545
let mut chanmon_cfgs = create_chanmon_cfgs(4);
15551546
let seed_1 = [42u8; 32];
@@ -1607,7 +1598,6 @@ mod test {
16071598
}
16081599

16091600
#[test]
1610-
#[cfg(feature = "std")]
16111601
fn test_multi_node_with_only_public_channels_hints_includes_only_phantom_route() {
16121602
let mut chanmon_cfgs = create_chanmon_cfgs(3);
16131603
let seed_1 = [42u8; 32];
@@ -1640,7 +1630,6 @@ mod test {
16401630
}
16411631

16421632
#[test]
1643-
#[cfg(feature = "std")]
16441633
fn test_multi_node_with_mixed_public_and_private_channel_hints_includes_only_phantom_route() {
16451634
let mut chanmon_cfgs = create_chanmon_cfgs(4);
16461635
let seed_1 = [42u8; 32];
@@ -1674,7 +1663,6 @@ mod test {
16741663
}
16751664

16761665
#[test]
1677-
#[cfg(feature = "std")]
16781666
fn test_multi_node_hints_has_only_lowest_inbound_channel_above_minimum() {
16791667
let mut chanmon_cfgs = create_chanmon_cfgs(3);
16801668
let seed_1 = [42u8; 32];
@@ -1705,7 +1693,6 @@ mod test {
17051693
}
17061694

17071695
#[test]
1708-
#[cfg(feature = "std")]
17091696
fn test_multi_node_channels_inbound_capacity_lower_than_invoice_amt_filtering() {
17101697
let mut chanmon_cfgs = create_chanmon_cfgs(4);
17111698
let seed_1 = [42u8; 32];

lightning/src/onion_message/functional_tests.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ struct MessengerNode {
6464

6565
impl Drop for MessengerNode {
6666
fn drop(&mut self) {
67-
#[cfg(feature = "std")] {
68-
if std::thread::panicking() {
69-
return;
70-
}
67+
if std::thread::panicking() {
68+
return;
7169
}
7270
assert!(release_events(self).is_empty());
7371
}
@@ -163,10 +161,8 @@ impl TestCustomMessageHandler {
163161

164162
impl Drop for TestCustomMessageHandler {
165163
fn drop(&mut self) {
166-
#[cfg(feature = "std")] {
167-
if std::thread::panicking() {
168-
return;
169-
}
164+
if std::thread::panicking() {
165+
return;
170166
}
171167
assert!(self.expectations.lock().unwrap().is_empty());
172168
}

lightning/src/routing/router.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -7419,7 +7419,6 @@ mod tests {
74197419
(route.paths[1].hops[1].short_channel_id == 4 && route.paths[0].hops[1].short_channel_id == 13));
74207420
}
74217421

7422-
#[cfg(feature = "std")]
74237422
pub(super) fn random_init_seed() -> u64 {
74247423
// Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG.
74257424
use core::hash::{BuildHasher, Hasher};
@@ -7429,7 +7428,6 @@ mod tests {
74297428
}
74307429

74317430
#[test]
7432-
#[cfg(feature = "std")]
74337431
fn generate_routes() {
74347432
use crate::routing::scoring::ProbabilisticScoringFeeParameters;
74357433

@@ -7449,7 +7447,6 @@ mod tests {
74497447
}
74507448

74517449
#[test]
7452-
#[cfg(feature = "std")]
74537450
fn generate_routes_mpp() {
74547451
use crate::routing::scoring::ProbabilisticScoringFeeParameters;
74557452

@@ -7469,7 +7466,6 @@ mod tests {
74697466
}
74707467

74717468
#[test]
7472-
#[cfg(feature = "std")]
74737469
fn generate_large_mpp_routes() {
74747470
use crate::routing::scoring::ProbabilisticScoringFeeParameters;
74757471

@@ -8696,7 +8692,7 @@ mod tests {
86968692
}
86978693
}
86988694

8699-
#[cfg(all(any(test, ldk_bench), feature = "std"))]
8695+
#[cfg(any(test, ldk_bench))]
87008696
pub(crate) mod bench_utils {
87018697
use super::*;
87028698
use std::fs::File;

0 commit comments

Comments
 (0)