Skip to content
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
94 changes: 94 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,100 @@ mod tests {
assert!(matches!(result, Err(BuildError::ReadFailed)));
}

#[test]
fn unstarted_node_rejects_live_node_apis() {
use std::str::FromStr;

use bitcoin::secp256k1::PublicKey;
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::msgs::SocketAddress;
use lightning_types::payment::PaymentPreimage;

use crate::{Error, UserChannelId};

let builder = NodeBuilder::new();
let logger = Arc::new(Logger::new_log_facade());
#[cfg(not(feature = "uniffi"))]
let node_entropy = NodeEntropy::from_seed_bytes([42; 64]);
#[cfg(feature = "uniffi")]
let node_entropy = NodeEntropy::from_seed_bytes(vec![42; 64]).unwrap();
let node = builder
.build_with_store_and_logger(node_entropy, InMemoryStore::new(), logger)
.unwrap();

let node_id = PublicKey::from_str(
"0276607124ebe6a6c9338517b6f485825b27c2dcc0b9fc2aa6a4c0df91194e5993",
)
.unwrap();
let payment_id = PaymentId([0; 32]);
let user_channel_id = UserChannelId(0);

assert_eq!(node.onchain_payment().bump_fee_rbf(payment_id, None), Err(Error::NotRunning));
assert_eq!(node.close_channel(&user_channel_id, node_id), Err(Error::NotRunning));
assert_eq!(
node.force_close_channel(&user_channel_id, node_id, None),
Err(Error::NotRunning)
);
assert_eq!(
node.update_channel_config(&user_channel_id, node_id, Default::default()),
Err(Error::NotRunning)
);
assert_eq!(node.splice_in(&user_channel_id, node_id, 1_000), Err(Error::NotRunning));
assert_eq!(
node.bump_channel_funding_fee(&user_channel_id, node_id),
Err(Error::NotRunning)
);
assert_eq!(node.bolt11_payment().fail_for_id(payment_id), Err(Error::NotRunning));
assert_eq!(
node.bolt11_payment().claim_for_id(payment_id, 0, PaymentPreimage([0; 32])),
Err(Error::NotRunning)
);

let address = SocketAddress::from_str("127.0.0.1:9735").unwrap();
assert_eq!(
node.open_channel(node_id, address.clone(), 1_000, None, None, None),
Err(Error::NotRunning)
);
assert_eq!(
node.open_channel_with_all(node_id, address.clone(), None, None, None),
Err(Error::NotRunning)
);
assert_eq!(
node.open_announced_channel(node_id, address.clone(), 1_000, None, None, None),
Err(Error::NotRunning)
);
assert_eq!(
node.open_announced_channel_with_all(node_id, address.clone(), None, None, None),
Err(Error::NotRunning)
);
assert_eq!(
node.open_0reserve_channel(node_id, address.clone(), 1_000, None, None, None),
Err(Error::NotRunning)
);
assert_eq!(
node.open_0reserve_channel_with_all(node_id, address, None, None, None),
Err(Error::NotRunning)
);

// Async receive state is persisted via the background processor.
assert_eq!(node.bolt12_payment().receive_async().err(), Some(Error::NotRunning));
#[cfg(not(feature = "uniffi"))]
let paths = Vec::new();
#[cfg(feature = "uniffi")]
let paths = Vec::<u8>::new();
assert_eq!(
node.bolt12_payment().set_paths_to_static_invoice_server(paths),
Err(Error::NotRunning)
);

// The running state is checked before any URI or HRN resolution happens.
#[cfg(feature = "unified-payments")]
{
let res = node.runtime.block_on(node.unified_payment().send("not a uri", None, None));
assert_eq!(res.err(), Some(Error::NotRunning));
}
}

#[test]
fn sanitize_empty_node_alias() {
// Empty node alias
Expand Down
53 changes: 53 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ impl Node {
self.bolt11_payment().into(),
self.bolt12_payment().into(),
Arc::clone(&self.config),
Arc::clone(&self.is_running),
Arc::clone(&self.logger),
self.hrn_resolver.clone(),
)
Expand All @@ -1199,6 +1200,7 @@ impl Node {
self.bolt11_payment(),
self.bolt12_payment(),
Arc::clone(&self.config),
Arc::clone(&self.is_running),
Arc::clone(&self.logger),
self.hrn_resolver.clone(),
))
Expand Down Expand Up @@ -1254,6 +1256,7 @@ impl Node {
Arc::clone(&self.wallet),
Arc::clone(&self.connection_manager),
Arc::clone(&self.liquidity_source),
Arc::clone(&self.is_running),
Arc::clone(&self.logger),
)
}
Expand All @@ -1266,6 +1269,7 @@ impl Node {
Arc::clone(&self.wallet),
Arc::clone(&self.connection_manager),
Arc::clone(&self.liquidity_source),
Arc::clone(&self.is_running),
Arc::clone(&self.logger),
))
}
Expand Down Expand Up @@ -1538,6 +1542,10 @@ impl Node {
push_to_counterparty_msat: Option<u64>, fee_rate: Option<FfiFeeRate>,
channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let fee_rate_opt = maybe_map_fee_rate_opt(fee_rate);
self.open_channel_inner(
node_id,
Expand Down Expand Up @@ -1580,6 +1588,10 @@ impl Node {
push_to_counterparty_msat: Option<u64>, fee_rate: Option<FfiFeeRate>,
channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

if let Err(err) = may_announce_channel(&self.config) {
log_error!(self.logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node: {}", err);
return Err(Error::ChannelCreationFailed);
Expand Down Expand Up @@ -1619,6 +1631,10 @@ impl Node {
&self, node_id: PublicKey, address: SocketAddress, push_to_counterparty_msat: Option<u64>,
fee_rate: Option<FfiFeeRate>, channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let fee_rate_opt = maybe_map_fee_rate_opt(fee_rate);
self.open_channel_inner(
node_id,
Expand Down Expand Up @@ -1657,6 +1673,10 @@ impl Node {
&self, node_id: PublicKey, address: SocketAddress, push_to_counterparty_msat: Option<u64>,
fee_rate: Option<FfiFeeRate>, channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

if let Err(err) = may_announce_channel(&self.config) {
log_error!(self.logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node: {err}");
return Err(Error::ChannelCreationFailed);
Expand Down Expand Up @@ -1702,6 +1722,10 @@ impl Node {
push_to_counterparty_msat: Option<u64>, fee_rate: Option<FfiFeeRate>,
channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let fee_rate_opt = maybe_map_fee_rate_opt(fee_rate);
self.open_channel_inner(
node_id,
Expand Down Expand Up @@ -1736,6 +1760,10 @@ impl Node {
&self, node_id: PublicKey, address: SocketAddress, push_to_counterparty_msat: Option<u64>,
fee_rate: Option<FfiFeeRate>, channel_config: Option<ChannelConfig>,
) -> Result<UserChannelId, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let fee_rate_opt = maybe_map_fee_rate_opt(fee_rate);
self.open_channel_inner(
node_id,
Expand Down Expand Up @@ -1893,6 +1921,10 @@ impl Node {
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
splice_amount_sats: u64,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

self.splice_in_inner(
user_channel_id,
counterparty_node_id,
Expand All @@ -1916,6 +1948,9 @@ impl Node {
pub fn splice_in_with_all(
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}
self.splice_in_inner(user_channel_id, counterparty_node_id, FundingAmount::Max)
}

Expand All @@ -1934,6 +1969,10 @@ impl Node {
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey, address: &Address,
splice_amount_sats: u64,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let open_channels =
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
if let Some(channel_details) =
Expand Down Expand Up @@ -2015,6 +2054,10 @@ impl Node {
pub fn bump_channel_funding_fee(
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let open_channels =
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
if let Some(channel_details) =
Expand Down Expand Up @@ -2130,6 +2173,9 @@ impl Node {
pub fn close_channel(
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}
self.close_channel_internal(user_channel_id, counterparty_node_id, false, None)
}

Expand All @@ -2150,6 +2196,9 @@ impl Node {
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
reason: Option<String>,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}
self.close_channel_internal(user_channel_id, counterparty_node_id, true, reason)
}

Expand Down Expand Up @@ -2210,6 +2259,10 @@ impl Node {
&self, user_channel_id: &UserChannelId, counterparty_node_id: PublicKey,
channel_config: ChannelConfig,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let open_channels: Vec<LdkChannelDetails> =
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
if let Some(channel_details) =
Expand Down
14 changes: 12 additions & 2 deletions src/liquidity/client/lsps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,18 @@ pub struct LSPS1Liquidity {
wallet: Arc<Wallet>,
connection_manager: Arc<ConnectionManager<Arc<Logger>>>,
liquidity_source: Arc<LSPS1Client<Arc<Logger>>>,
is_running: Arc<RwLock<bool>>,
logger: Arc<Logger>,
}

impl LSPS1Liquidity {
pub(crate) fn new(
runtime: Arc<Runtime>, wallet: Arc<Wallet>,
connection_manager: Arc<ConnectionManager<Arc<Logger>>>,
liquidity_source: Arc<LSPS1Client<Arc<Logger>>>, logger: Arc<Logger>,
liquidity_source: Arc<LSPS1Client<Arc<Logger>>>, is_running: Arc<RwLock<bool>>,
logger: Arc<Logger>,
) -> Self {
Self { runtime, wallet, connection_manager, liquidity_source, logger }
Self { runtime, wallet, connection_manager, liquidity_source, is_running, logger }
}
}

Expand All @@ -489,6 +491,10 @@ impl LSPS1Liquidity {
&self, lsp_balance_sat: u64, client_balance_sat: u64, channel_expiry_blocks: u32,
announce_channel: bool, node_id: Option<PublicKey>,
) -> Result<LSPS1OrderStatus, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let lsps1_node = self
.runtime
.block_on(async { self.liquidity_source.get_lsps1_node(node_id.as_ref()).await })?;
Expand Down Expand Up @@ -528,6 +534,10 @@ impl LSPS1Liquidity {
pub fn check_order_status(
&self, order_id: LSPS1OrderId, lsp_node_id: PublicKey,
) -> Result<LSPS1OrderStatus, Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

let lsps1_node = self
.runtime
.block_on(async { self.liquidity_source.get_lsps1_node(Some(&lsp_node_id)).await })?;
Expand Down
11 changes: 9 additions & 2 deletions src/liquidity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,18 @@ pub struct Liquidity {
wallet: Arc<Wallet>,
connection_manager: Arc<ConnectionManager<Arc<Logger>>>,
liquidity_source: Arc<LiquiditySource<Arc<Logger>>>,
is_running: Arc<RwLock<bool>>,
logger: Arc<Logger>,
}

impl Liquidity {
pub(crate) fn new(
runtime: Arc<Runtime>, wallet: Arc<Wallet>,
connection_manager: Arc<ConnectionManager<Arc<Logger>>>,
liquidity_source: Arc<LiquiditySource<Arc<Logger>>>, logger: Arc<Logger>,
liquidity_source: Arc<LiquiditySource<Arc<Logger>>>, is_running: Arc<RwLock<bool>>,
logger: Arc<Logger>,
) -> Self {
Self { runtime, wallet, connection_manager, liquidity_source, logger }
Self { runtime, wallet, connection_manager, liquidity_source, is_running, logger }
}
}

Expand All @@ -159,6 +161,10 @@ impl Liquidity {
&self, node_id: PublicKey, address: SocketAddress, token: Option<String>,
trust_peer_0conf: bool,
) -> Result<(), Error> {
if !*self.is_running.read().expect("lock") {
return Err(Error::NotRunning);
}

{
let mut lsp_nodes = self.liquidity_source.lsp_nodes.write().expect("lock");
if lsp_nodes.iter().any(|n| n.node_id == node_id) {
Expand Down Expand Up @@ -212,6 +218,7 @@ impl Liquidity {
Arc::clone(&self.wallet),
Arc::clone(&self.connection_manager),
self.liquidity_source.lsps1_client(),
Arc::clone(&self.is_running),
Arc::clone(&self.logger),
)
}
Expand Down
Loading
Loading