Skip to content

Commit 3111508

Browse files
authored
Merge pull request #3250 from TheBlueMatt/2024-08-feature-cleanup
2 parents 7cf4a19 + 11ab302 commit 3111508

File tree

14 files changed

+90
-261
lines changed

14 files changed

+90
-261
lines changed

lightning/src/ln/bolt11_payment.rs

Lines changed: 7 additions & 15 deletions
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

Lines changed: 21 additions & 32 deletions
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

Lines changed: 0 additions & 13 deletions
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/ln/outbound_payment.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, PaymentParameters
2626
use crate::sign::{EntropySource, NodeSigner, Recipient};
2727
use crate::util::errors::APIError;
2828
use crate::util::logger::Logger;
29-
use crate::util::time::Time;
30-
#[cfg(all(feature = "std", test))]
31-
use crate::util::time::tests::SinceEpoch;
29+
#[cfg(feature = "std")]
30+
use crate::util::time::Instant;
3231
use crate::util::ser::ReadableArgs;
3332

3433
use core::fmt::{self, Display, Formatter};
@@ -319,12 +318,9 @@ impl Retry {
319318
(Retry::Attempts(max_retry_count), PaymentAttempts { count, .. }) => {
320319
max_retry_count > count
321320
},
322-
#[cfg(all(feature = "std", not(test)))]
323-
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
324-
*max_duration >= crate::util::time::MonotonicTime::now().duration_since(*first_attempted_at),
325-
#[cfg(all(feature = "std", test))]
321+
#[cfg(feature = "std")]
326322
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
327-
*max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
323+
*max_duration >= Instant::now().duration_since(*first_attempted_at),
328324
}
329325
}
330326
}
@@ -339,42 +335,28 @@ pub(super) fn has_expired(route_params: &RouteParameters) -> bool {
339335
false
340336
}
341337

342-
pub(crate) type PaymentAttempts = PaymentAttemptsUsingTime<ConfiguredTime>;
343-
344338
/// Storing minimal payment attempts information required for determining if a outbound payment can
345339
/// be retried.
346-
pub(crate) struct PaymentAttemptsUsingTime<T: Time> {
340+
pub(crate) struct PaymentAttempts {
347341
/// This count will be incremented only after the result of the attempt is known. When it's 0,
348342
/// it means the result of the first attempt is not known yet.
349343
pub(crate) count: u32,
350344
/// This field is only used when retry is `Retry::Timeout` which is only build with feature std
351345
#[cfg(feature = "std")]
352-
first_attempted_at: T,
353-
#[cfg(not(feature = "std"))]
354-
phantom: core::marker::PhantomData<T>,
355-
346+
first_attempted_at: Instant,
356347
}
357348

358-
#[cfg(not(feature = "std"))]
359-
type ConfiguredTime = crate::util::time::Eternity;
360-
#[cfg(all(feature = "std", not(test)))]
361-
type ConfiguredTime = crate::util::time::MonotonicTime;
362-
#[cfg(all(feature = "std", test))]
363-
type ConfiguredTime = SinceEpoch;
364-
365-
impl<T: Time> PaymentAttemptsUsingTime<T> {
349+
impl PaymentAttempts {
366350
pub(crate) fn new() -> Self {
367-
PaymentAttemptsUsingTime {
351+
PaymentAttempts {
368352
count: 0,
369353
#[cfg(feature = "std")]
370-
first_attempted_at: T::now(),
371-
#[cfg(not(feature = "std"))]
372-
phantom: core::marker::PhantomData,
354+
first_attempted_at: Instant::now(),
373355
}
374356
}
375357
}
376358

377-
impl<T: Time> Display for PaymentAttemptsUsingTime<T> {
359+
impl Display for PaymentAttempts {
378360
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
379361
#[cfg(not(feature = "std"))]
380362
return write!(f, "attempts: {}", self.count);
@@ -383,7 +365,7 @@ impl<T: Time> Display for PaymentAttemptsUsingTime<T> {
383365
f,
384366
"attempts: {}, duration: {}s",
385367
self.count,
386-
T::now().duration_since(self.first_attempted_at).as_secs()
368+
Instant::now().duration_since(self.first_attempted_at).as_secs()
387369
);
388370
}
389371
}

lightning/src/ln/payment_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::routing::gossip::NodeId;
4646

4747
#[cfg(feature = "std")]
4848
use {
49-
crate::util::time::tests::SinceEpoch,
49+
crate::util::time::Instant as TestTime,
5050
std::time::{SystemTime, Instant, Duration},
5151
};
5252

@@ -2340,7 +2340,7 @@ fn do_automatic_retries(test: AutoRetry) {
23402340
pass_failed_attempt_with_retry_along_path!(channel_id_2, true);
23412341

23422342
// Advance the time so the second attempt fails due to timeout.
2343-
SinceEpoch::advance(Duration::from_secs(61));
2343+
TestTime::advance(Duration::from_secs(61));
23442344

23452345
// Make sure we don't retry again.
23462346
nodes[0].node.process_pending_htlc_forwards();

0 commit comments

Comments
 (0)