Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop KVStore type parameter from Node #244

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ interface Builder {
[Throws=BuildError]
void set_listening_addresses(sequence<SocketAddress> listening_addresses);
[Throws=BuildError]
LDKNode build();
Node build();
[Throws=BuildError]
Node build_with_fs_store();
};

interface LDKNode {
interface Node {
[Throws=NodeError]
void start();
[Throws=NodeError]
Expand Down
42 changes: 21 additions & 21 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::payment_store::PaymentStore;
use crate::peer_store::PeerStore;
use crate::tx_broadcaster::TransactionBroadcaster;
use crate::types::{
ChainMonitor, ChannelManager, GossipSync, KeysManager, MessageRouter, NetworkGraph,
ChainMonitor, ChannelManager, DynStore, GossipSync, KeysManager, MessageRouter, NetworkGraph,
OnionMessenger, PeerManager,
};
use crate::wallet::Wallet;
Expand All @@ -32,7 +32,7 @@ use lightning::sign::EntropySource;

use lightning::util::config::UserConfig;
use lightning::util::persist::{
read_channel_monitors, KVStore, CHANNEL_MANAGER_PERSISTENCE_KEY,
read_channel_monitors, CHANNEL_MANAGER_PERSISTENCE_KEY,
CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE,
};
use lightning::util::ser::ReadableArgs;
Expand Down Expand Up @@ -115,12 +115,18 @@ pub enum BuildError {
/// The given listening addresses are invalid, e.g. too many were passed.
InvalidListeningAddresses,
/// We failed to read data from the [`KVStore`].
///
/// [`KVStore`]: lightning::util::persist::KVStore
ReadFailed,
/// We failed to write data to the [`KVStore`].
///
/// [`KVStore`]: lightning::util::persist::KVStore
WriteFailed,
/// We failed to access the given `storage_dir_path`.
StoragePathAccessFailed,
/// We failed to setup our [`KVStore`].
///
/// [`KVStore`]: lightning::util::persist::KVStore
KVStoreSetupFailed,
/// We failed to setup the onchain wallet.
WalletSetupFailed,
Expand Down Expand Up @@ -299,7 +305,7 @@ impl NodeBuilder {

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
pub fn build(&self) -> Result<Node<SqliteStore>, BuildError> {
pub fn build(&self) -> Result<Node, BuildError> {
let storage_dir_path = self.config.storage_dir_path.clone();
fs::create_dir_all(storage_dir_path.clone())
.map_err(|_| BuildError::StoragePathAccessFailed)?;
Expand All @@ -316,7 +322,7 @@ impl NodeBuilder {

/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
/// previously configured.
pub fn build_with_fs_store(&self) -> Result<Node<FilesystemStore>, BuildError> {
pub fn build_with_fs_store(&self) -> Result<Node, BuildError> {
let mut storage_dir_path: PathBuf = self.config.storage_dir_path.clone().into();
storage_dir_path.push("fs_store");

Expand All @@ -329,9 +335,7 @@ impl NodeBuilder {
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
/// previously configured.
#[cfg(any(vss, vss_test))]
pub fn build_with_vss_store(
&self, url: String, store_id: String,
) -> Result<Node<VssStore>, BuildError> {
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Node, BuildError> {
let logger = setup_logger(&self.config)?;

let seed_bytes = seed_bytes_from_config(
Expand Down Expand Up @@ -369,9 +373,7 @@ impl NodeBuilder {
}

/// Builds a [`Node`] instance according to the options previously configured.
pub fn build_with_store<K: KVStore + Sync + Send + 'static>(
&self, kv_store: Arc<K>,
) -> Result<Node<K>, BuildError> {
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Node, BuildError> {
let logger = setup_logger(&self.config)?;
let seed_bytes = seed_bytes_from_config(
&self.config,
Expand Down Expand Up @@ -500,31 +502,29 @@ impl ArcedNodeBuilder {

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
pub fn build(&self) -> Result<Arc<Node<SqliteStore>>, BuildError> {
pub fn build(&self) -> Result<Arc<Node>, BuildError> {
self.inner.read().unwrap().build().map(Arc::new)
}

/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
/// previously configured.
pub fn build_with_fs_store(&self) -> Result<Arc<Node<FilesystemStore>>, BuildError> {
pub fn build_with_fs_store(&self) -> Result<Arc<Node>, BuildError> {
self.inner.read().unwrap().build_with_fs_store().map(Arc::new)
}

/// Builds a [`Node`] instance according to the options previously configured.
pub fn build_with_store<K: KVStore + Sync + Send + 'static>(
&self, kv_store: Arc<K>,
) -> Result<Arc<Node<K>>, BuildError> {
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Arc<Node>, BuildError> {
self.inner.read().unwrap().build_with_store(kv_store).map(Arc::new)
}
}

/// Builds a [`Node`] instance according to the options previously configured.
fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
fn build_with_store_internal(
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
gossip_source_config: Option<&GossipSourceConfig>,
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
logger: Arc<FilesystemLogger>, kv_store: Arc<K>,
) -> Result<Node<K>, BuildError> {
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>,
) -> Result<Node, BuildError> {
// Initialize the on-chain wallet and chain access
let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes)
.map_err(|e| {
Expand Down Expand Up @@ -604,7 +604,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
));

// Initialize the ChainMonitor
let chain_monitor: Arc<ChainMonitor<K>> = Arc::new(chainmonitor::ChainMonitor::new(
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
Some(Arc::clone(&tx_sync)),
Arc::clone(&tx_broadcaster),
Arc::clone(&logger),
Expand Down Expand Up @@ -735,7 +735,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
channel_monitor_references,
);
let (_hash, channel_manager) =
<(BlockHash, ChannelManager<K>)>::read(&mut reader, read_args).map_err(|e| {
<(BlockHash, ChannelManager)>::read(&mut reader, read_args).map_err(|e| {
log_error!(logger, "Failed to read channel manager from KVStore: {}", e);
BuildError::ReadFailed
})?;
Expand Down Expand Up @@ -779,7 +779,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
let message_router = MessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager));

// Initialize the PeerManager
let onion_messenger: Arc<OnionMessenger<K>> = Arc::new(OnionMessenger::new(
let onion_messenger: Arc<OnionMessenger> = Arc::new(OnionMessenger::new(
Arc::clone(&keys_manager),
Arc::clone(&keys_manager),
Arc::clone(&logger),
Expand Down
42 changes: 20 additions & 22 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{Sweeper, Wallet};
use crate::types::{DynStore, Sweeper, Wallet};
use crate::{
hex_utils, ChannelManager, Config, Error, NetworkGraph, PeerInfo, PeerStore, UserChannelId,
};
Expand All @@ -20,7 +20,6 @@ use lightning::impl_writeable_tlv_based_enum;
use lightning::ln::{ChannelId, PaymentHash};
use lightning::routing::gossip::NodeId;
use lightning::util::errors::APIError;
use lightning::util::persist::KVStore;
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};

use lightning_liquidity::lsps2::utils::compute_opening_fee;
Expand Down Expand Up @@ -138,22 +137,22 @@ impl_writeable_tlv_based_enum!(Event,
};
);

pub struct EventQueue<K: KVStore + Sync + Send, L: Deref>
pub struct EventQueue<L: Deref>
where
L::Target: Logger,
{
queue: Arc<Mutex<VecDeque<Event>>>,
waker: Arc<Mutex<Option<Waker>>>,
notifier: Condvar,
kv_store: Arc<K>,
kv_store: Arc<DynStore>,
logger: L,
}

impl<K: KVStore + Sync + Send, L: Deref> EventQueue<K, L>
impl<L: Deref> EventQueue<L>
where
L::Target: Logger,
{
pub(crate) fn new(kv_store: Arc<K>, logger: L) -> Self {
pub(crate) fn new(kv_store: Arc<DynStore>, logger: L) -> Self {
let queue = Arc::new(Mutex::new(VecDeque::new()));
let waker = Arc::new(Mutex::new(None));
let notifier = Condvar::new();
Expand Down Expand Up @@ -228,13 +227,13 @@ where
}
}

impl<K: KVStore + Sync + Send, L: Deref> ReadableArgs<(Arc<K>, L)> for EventQueue<K, L>
impl<L: Deref> ReadableArgs<(Arc<DynStore>, L)> for EventQueue<L>
where
L::Target: Logger,
{
#[inline]
fn read<R: lightning::io::Read>(
reader: &mut R, args: (Arc<K>, L),
reader: &mut R, args: (Arc<DynStore>, L),
) -> Result<Self, lightning::ln::msgs::DecodeError> {
let (kv_store, logger) = args;
let read_queue: EventQueueDeserWrapper = Readable::read(reader)?;
Expand Down Expand Up @@ -292,32 +291,31 @@ impl Future for EventFuture {
}
}

pub(crate) struct EventHandler<K: KVStore + Sync + Send, L: Deref>
pub(crate) struct EventHandler<L: Deref>
where
L::Target: Logger,
{
event_queue: Arc<EventQueue<K, L>>,
event_queue: Arc<EventQueue<L>>,
wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager<K>>,
output_sweeper: Arc<Sweeper<K>>,
channel_manager: Arc<ChannelManager>,
output_sweeper: Arc<Sweeper>,
network_graph: Arc<NetworkGraph>,
payment_store: Arc<PaymentStore<K, L>>,
peer_store: Arc<PeerStore<K, L>>,
payment_store: Arc<PaymentStore<L>>,
peer_store: Arc<PeerStore<L>>,
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
logger: L,
config: Arc<Config>,
}

impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
impl<L: Deref> EventHandler<L>
where
L::Target: Logger,
{
pub fn new(
event_queue: Arc<EventQueue<K, L>>, wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager<K>>, output_sweeper: Arc<Sweeper<K>>,
network_graph: Arc<NetworkGraph>, payment_store: Arc<PaymentStore<K, L>>,
peer_store: Arc<PeerStore<K, L>>, runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
logger: L, config: Arc<Config>,
event_queue: Arc<EventQueue<L>>, wallet: Arc<Wallet>, channel_manager: Arc<ChannelManager>,
output_sweeper: Arc<Sweeper>, network_graph: Arc<NetworkGraph>,
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
) -> Self {
Self {
event_queue,
Expand Down Expand Up @@ -933,7 +931,7 @@ mod tests {

#[tokio::test]
async fn event_queue_persistence() {
let store = Arc::new(TestStore::new(false));
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
let logger = Arc::new(TestLogger::new());
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
assert_eq!(event_queue.next_event(), None);
Expand Down Expand Up @@ -970,7 +968,7 @@ mod tests {

#[tokio::test]
async fn event_queue_concurrency() {
let store = Arc::new(TestStore::new(false));
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
let logger = Arc::new(TestLogger::new());
let event_queue = Arc::new(EventQueue::new(Arc::clone(&store), Arc::clone(&logger)));
assert_eq!(event_queue.next_event(), None);
Expand Down
Loading
Loading