Skip to content

Commit 81313cd

Browse files
Move TimeProvider to utils::time module for better organization
1 parent ff279d6 commit 81313cd

File tree

12 files changed

+52
-39
lines changed

12 files changed

+52
-39
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ use futures_util::{dummy_waker, OptionalSelector, Selector, SelectorOutput};
735735
/// # use lightning_background_processor::{process_events_async, GossipSync};
736736
/// # use core::future::Future;
737737
/// # use core::pin::Pin;
738-
/// # use lightning_liquidity::lsps5::service::TimeProvider;
738+
/// # use lightning_liquidity::utils::time::TimeProvider;
739739
/// # struct Logger {}
740740
/// # impl lightning::util::logger::Logger for Logger {
741741
/// # fn log(&self, _record: lightning::util::logger::Record) {}
@@ -1401,7 +1401,7 @@ mod tests {
14011401
use lightning::util::sweep::{OutputSpendStatus, OutputSweeperSync, PRUNE_DELAY_BLOCKS};
14021402
use lightning::util::test_utils;
14031403
use lightning::{get_event, get_event_msg};
1404-
use lightning_liquidity::lsps5::service::DefaultTimeProvider;
1404+
use lightning_liquidity::utils::time::DefaultTimeProvider;
14051405
use lightning_liquidity::LiquidityManager;
14061406
use lightning_persister::fs_store::FilesystemStore;
14071407
use lightning_rapid_gossip_sync::RapidGossipSync;

lightning-liquidity/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub mod message_queue;
7070
mod sync;
7171
#[cfg(test)]
7272
mod tests;
73-
mod utils;
73+
pub mod utils;
7474

7575
pub use manager::{
7676
ALiquidityManager, LiquidityClientConfig, LiquidityManager, LiquidityServiceConfig,

lightning-liquidity/src/lsps5/client.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use crate::message_queue::MessageQueue;
2222
use crate::prelude::{new_hash_map, HashMap};
2323
use crate::sync::{Arc, Mutex, RwLock};
2424
use crate::utils::generate_request_id;
25+
use crate::utils::time::TimeProvider;
2526

2627
use super::msgs::{LSPS5AppName, LSPS5Error, LSPS5WebhookUrl};
27-
use super::service::TimeProvider;
2828

2929
use bitcoin::secp256k1::PublicKey;
3030

@@ -439,9 +439,8 @@ mod tests {
439439

440440
use super::*;
441441
use crate::{
442-
lsps0::ser::LSPSRequestId,
443-
lsps5::{msgs::SetWebhookResponse, service::DefaultTimeProvider},
444-
tests::utils::TestEntropy,
442+
lsps0::ser::LSPSRequestId, lsps5::msgs::SetWebhookResponse, tests::utils::TestEntropy,
443+
utils::time::DefaultTimeProvider,
445444
};
446445
use bitcoin::{key::Secp256k1, secp256k1::SecretKey};
447446

lightning-liquidity/src/lsps5/service.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::message_queue::MessageQueue;
2020
use crate::prelude::hash_map::Entry;
2121
use crate::prelude::*;
2222
use crate::sync::{Arc, Mutex};
23+
use crate::utils::time::TimeProvider;
2324

2425
use bitcoin::secp256k1::PublicKey;
2526

@@ -54,28 +55,6 @@ struct StoredWebhook {
5455
last_notification_sent: HashMap<WebhookNotificationMethod, LSPSDateTime>,
5556
}
5657

57-
/// Trait defining a time provider for LSPS5 service.
58-
///
59-
/// This trait is used to provide the current time for LSPS5 service operations
60-
/// and to convert between timestamps and durations.
61-
pub trait TimeProvider {
62-
/// Get the current time as a duration since the Unix epoch.
63-
fn duration_since_epoch(&self) -> Duration;
64-
}
65-
66-
/// Default time provider using the system clock.
67-
#[derive(Clone, Debug)]
68-
#[cfg(feature = "time")]
69-
pub struct DefaultTimeProvider;
70-
71-
#[cfg(feature = "time")]
72-
impl TimeProvider for DefaultTimeProvider {
73-
fn duration_since_epoch(&self) -> Duration {
74-
use std::time::{SystemTime, UNIX_EPOCH};
75-
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
76-
}
77-
}
78-
7958
/// Server-side configuration options for LSPS5 Webhook Registration.
8059
#[derive(Clone, Debug)]
8160
pub struct LSPS5ServiceConfig {

lightning-liquidity/src/lsps5/validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
//! LSPS5 Validator
1111
1212
use super::msgs::LSPS5ClientError;
13-
use super::service::TimeProvider;
1413

1514
use crate::alloc::string::ToString;
1615
use crate::lsps0::ser::LSPSDateTime;
1716
use crate::lsps5::msgs::WebhookNotification;
1817
use crate::sync::Mutex;
18+
use crate::utils::time::TimeProvider;
1919

2020
use lightning::util::message_signing;
2121

lightning-liquidity/src/manager.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use crate::lsps0::ser::{
1212
use crate::lsps0::service::LSPS0ServiceHandler;
1313
use crate::lsps5::client::{LSPS5ClientConfig, LSPS5ClientHandler};
1414
use crate::lsps5::msgs::LSPS5Message;
15-
#[cfg(feature = "time")]
16-
use crate::lsps5::service::DefaultTimeProvider;
17-
use crate::lsps5::service::{LSPS5ServiceConfig, LSPS5ServiceHandler, TimeProvider};
15+
use crate::lsps5::service::{LSPS5ServiceConfig, LSPS5ServiceHandler};
1816
use crate::message_queue::MessageQueue;
1917

2018
use crate::lsps1::client::{LSPS1ClientConfig, LSPS1ClientHandler};
@@ -27,6 +25,9 @@ use crate::lsps2::msgs::LSPS2Message;
2725
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
2826
use crate::prelude::{new_hash_map, new_hash_set, HashMap, HashSet};
2927
use crate::sync::{Arc, Mutex, RwLock};
28+
#[cfg(feature = "time")]
29+
use crate::utils::time::DefaultTimeProvider;
30+
use crate::utils::time::TimeProvider;
3031

3132
use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
3233
use lightning::ln::channelmanager::{AChannelManager, ChainParameters};

lightning-liquidity/src/utils.rs renamed to lightning-liquidity/src/utils/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
//! Utilities for LSPS5 service.
2+
13
use alloc::string::String;
24
use core::{fmt::Write, ops::Deref};
35

46
use lightning::sign::EntropySource;
57

68
use crate::lsps0::ser::LSPSRequestId;
79

10+
/// Converts a human-readable string representation of a short channel ID (SCID)
811
pub fn scid_from_human_readable_string(human_readable_scid: &str) -> Result<u64, ()> {
912
let mut parts = human_readable_scid.split('x');
1013

@@ -24,6 +27,7 @@ where
2427
}
2528

2629
#[inline]
30+
/// Converts a byte slice to a hexadecimal string representation.
2731
pub fn hex_str(value: &[u8]) -> String {
2832
let mut res = String::with_capacity(2 * value.len());
2933
for v in value {
@@ -52,3 +56,5 @@ mod tests {
5256
assert_eq!(vout_from_scid(scid), vout);
5357
}
5458
}
59+
60+
pub mod time;

lightning-liquidity/src/utils/time.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Utilities for time handling in LSPS5 service.
2+
3+
use core::time::Duration;
4+
5+
/// Trait defining a time provider for LSPS5 service.
6+
///
7+
/// This trait is used to provide the current time for LSPS5 service operations
8+
/// and to convert between timestamps and durations.
9+
pub trait TimeProvider {
10+
/// Get the current time as a duration since the Unix epoch.
11+
fn duration_since_epoch(&self) -> Duration;
12+
}
13+
14+
/// Default time provider using the system clock.
15+
#[derive(Clone, Debug)]
16+
#[cfg(feature = "time")]
17+
pub struct DefaultTimeProvider;
18+
19+
#[cfg(feature = "time")]
20+
impl TimeProvider for DefaultTimeProvider {
21+
fn duration_since_epoch(&self) -> Duration {
22+
use std::time::{SystemTime, UNIX_EPOCH};
23+
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
24+
}
25+
}

lightning-liquidity/tests/common/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#![cfg(test)]
22

3-
use bitcoin::Network;
4-
use lightning::ln::channelmanager::ChainParameters;
5-
use lightning_liquidity::lsps5::service::TimeProvider;
3+
use lightning_liquidity::utils::time::TimeProvider;
64
use lightning_liquidity::{LiquidityClientConfig, LiquidityManager, LiquidityServiceConfig};
75

86
use lightning::chain::{BestBlock, Filter};
7+
use lightning::ln::channelmanager::ChainParameters;
98
use lightning::ln::functional_test_utils::{Node, TestChannelManager};
109
use lightning::util::test_utils::TestKeysInterface;
1110

11+
use bitcoin::Network;
12+
1213
use core::ops::Deref;
1314

1415
use std::sync::Arc;

lightning-liquidity/tests/lsps0_integration_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use lightning_liquidity::lsps1::service::LSPS1ServiceConfig;
1313
use lightning_liquidity::lsps2::client::LSPS2ClientConfig;
1414
use lightning_liquidity::lsps2::service::LSPS2ServiceConfig;
1515
use lightning_liquidity::lsps5::client::LSPS5ClientConfig;
16-
use lightning_liquidity::lsps5::service::{DefaultTimeProvider, LSPS5ServiceConfig};
16+
use lightning_liquidity::lsps5::service::LSPS5ServiceConfig;
17+
use lightning_liquidity::utils::time::DefaultTimeProvider;
1718
use lightning_liquidity::{LiquidityClientConfig, LiquidityServiceConfig};
1819

1920
use lightning::ln::functional_test_utils::{

0 commit comments

Comments
 (0)