Skip to content

Commit

Permalink
Move Arc<Node> to Context.
Browse files Browse the repository at this point in the history
It will become easier to support multiple members in Context.
In future commits, we will add PaginatedKvStore to Context.
  • Loading branch information
G8XSU committed Dec 13, 2024
1 parent b16dc89 commit 2d39edc
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 96 deletions.
16 changes: 9 additions & 7 deletions ldk-server/src/api/bolt11_receive.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use ldk_node::Node;
use crate::service::Context;
use ldk_server_protos::api::{Bolt11ReceiveRequest, Bolt11ReceiveResponse};
use std::sync::Arc;

pub(crate) const BOLT11_RECEIVE_PATH: &str = "Bolt11Receive";

pub(crate) fn handle_bolt11_receive_request(
node: Arc<Node>, request: Bolt11ReceiveRequest,
context: Context, request: Bolt11ReceiveRequest,
) -> Result<Bolt11ReceiveResponse, ldk_node::NodeError> {
let invoice = match request.amount_msat {
Some(amount_msat) => {
node.bolt11_payment().receive(amount_msat, &request.description, request.expiry_secs)?
},
None => node
Some(amount_msat) => context.node.bolt11_payment().receive(
amount_msat,
&request.description,
request.expiry_secs,
)?,
None => context
.node
.bolt11_payment()
.receive_variable_amount(&request.description, request.expiry_secs)?,
};
Expand Down
11 changes: 6 additions & 5 deletions ldk-server/src/api/bolt11_send.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::service::Context;
use bytes::Bytes;
use ldk_node::lightning_invoice::Bolt11Invoice;
use ldk_node::Node;
use ldk_server_protos::api::{Bolt11SendRequest, Bolt11SendResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const BOLT11_SEND_PATH: &str = "Bolt11Send";

pub(crate) fn handle_bolt11_send_request(
node: Arc<Node>, request: Bolt11SendRequest,
context: Context, request: Bolt11SendRequest,
) -> Result<Bolt11SendResponse, ldk_node::NodeError> {
let invoice = Bolt11Invoice::from_str(&request.invoice.as_str())
.map_err(|_| ldk_node::NodeError::InvalidInvoice)?;

let payment_id = match request.amount_msat {
None => node.bolt11_payment().send(&invoice, None),
Some(amount_msat) => node.bolt11_payment().send_using_amount(&invoice, amount_msat, None),
None => context.node.bolt11_payment().send(&invoice, None),
Some(amount_msat) => {
context.node.bolt11_payment().send_using_amount(&invoice, amount_msat, None)
},
}?;

let response = Bolt11SendResponse { payment_id: Bytes::from(payment_id.0.to_vec()) };
Expand Down
10 changes: 5 additions & 5 deletions ldk-server/src/api/bolt12_receive.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use ldk_node::Node;
use crate::service::Context;
use ldk_server_protos::api::{Bolt12ReceiveRequest, Bolt12ReceiveResponse};
use std::sync::Arc;

pub(crate) const BOLT12_RECEIVE_PATH: &str = "Bolt12Receive";

pub(crate) fn handle_bolt12_receive_request(
node: Arc<Node>, request: Bolt12ReceiveRequest,
context: Context, request: Bolt12ReceiveRequest,
) -> Result<Bolt12ReceiveResponse, ldk_node::NodeError> {
let offer = match request.amount_msat {
Some(amount_msat) => node.bolt12_payment().receive(
Some(amount_msat) => context.node.bolt12_payment().receive(
amount_msat,
&request.description,
request.expiry_secs,
request.quantity,
)?,
None => node
None => context
.node
.bolt12_payment()
.receive_variable_amount(&request.description, request.expiry_secs)?,
};
Expand Down
9 changes: 4 additions & 5 deletions ldk-server/src/api/bolt12_send.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use crate::service::Context;
use bytes::Bytes;
use ldk_node::lightning::offers::offer::Offer;
use ldk_node::Node;
use ldk_server_protos::api::{Bolt12SendRequest, Bolt12SendResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const BOLT12_SEND_PATH: &str = "Bolt12Send";

pub(crate) fn handle_bolt12_send_request(
node: Arc<Node>, request: Bolt12SendRequest,
context: Context, request: Bolt12SendRequest,
) -> Result<Bolt12SendResponse, ldk_node::NodeError> {
let offer =
Offer::from_str(&request.offer.as_str()).map_err(|_| ldk_node::NodeError::InvalidOffer)?;

let payment_id = match request.amount_msat {
None => node.bolt12_payment().send(&offer, request.quantity, request.payer_note),
Some(amount_msat) => node.bolt12_payment().send_using_amount(
None => context.node.bolt12_payment().send(&offer, request.quantity, request.payer_note),
Some(amount_msat) => context.node.bolt12_payment().send_using_amount(
&offer,
amount_msat,
request.quantity,
Expand Down
10 changes: 5 additions & 5 deletions ldk-server/src/api/close_channel.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::service::Context;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::{Node, UserChannelId};
use ldk_node::UserChannelId;
use ldk_server_protos::api::{CloseChannelRequest, CloseChannelResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const CLOSE_CHANNEL_PATH: &str = "CloseChannel";

pub(crate) fn handle_close_channel_request(
node: Arc<Node>, request: CloseChannelRequest,
context: Context, request: CloseChannelRequest,
) -> Result<CloseChannelResponse, ldk_node::NodeError> {
//TODO: Should this be string?
let mut user_channel_id_bytes = [0u8; 16];
Expand All @@ -17,12 +17,12 @@ pub(crate) fn handle_close_channel_request(
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;

match request.force_close {
Some(true) => node.force_close_channel(
Some(true) => context.node.force_close_channel(
&user_channel_id,
counterparty_node_id,
request.force_close_reason,
)?,
_ => node.close_channel(&user_channel_id, counterparty_node_id)?,
_ => context.node.close_channel(&user_channel_id, counterparty_node_id)?,
};

let response = CloseChannelResponse {};
Expand Down
7 changes: 3 additions & 4 deletions ldk-server/src/api/get_balances.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::service::Context;
use crate::util::proto_adapter::{lightning_balance_to_proto, pending_sweep_balance_to_proto};
use ldk_node::Node;
use ldk_server_protos::api::{GetBalancesRequest, GetBalancesResponse};
use std::sync::Arc;

pub(crate) const GET_BALANCES: &str = "GetBalances";

pub(crate) fn handle_get_balances_request(
node: Arc<Node>, _request: GetBalancesRequest,
context: Context, _request: GetBalancesRequest,
) -> Result<GetBalancesResponse, ldk_node::NodeError> {
let balance_details = node.list_balances();
let balance_details = context.node.list_balances();

let response = GetBalancesResponse {
total_onchain_balance_sats: balance_details.total_onchain_balance_sats,
Expand Down
9 changes: 4 additions & 5 deletions ldk-server/src/api/get_node_info.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use ldk_node::Node;
use crate::service::Context;
use ldk_server_protos::api::{GetNodeInfoRequest, GetNodeInfoResponse};
use ldk_server_protos::types::BestBlock;
use std::sync::Arc;

pub(crate) const GET_NODE_INFO: &str = "GetNodeInfo";

pub(crate) fn handle_get_node_info_request(
node: Arc<Node>, _request: GetNodeInfoRequest,
context: Context, _request: GetNodeInfoRequest,
) -> Result<GetNodeInfoResponse, ldk_node::NodeError> {
let node_status = node.status();
let node_status = context.node.status();

let best_block = BestBlock {
block_hash: node_status.current_best_block.block_hash.to_string(),
height: node_status.current_best_block.height,
};

let response = GetNodeInfoResponse {
node_id: node.node_id().to_string(),
node_id: context.node.node_id().to_string(),
current_best_block: Some(best_block),
latest_lightning_wallet_sync_timestamp: node_status.latest_lightning_wallet_sync_timestamp,
latest_onchain_wallet_sync_timestamp: node_status.latest_onchain_wallet_sync_timestamp,
Expand Down
7 changes: 3 additions & 4 deletions ldk-server/src/api/get_payment_details.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::service::Context;
use crate::util::proto_adapter::payment_to_proto;
use hex::FromHex;
use ldk_node::lightning::ln::channelmanager::PaymentId;
use ldk_node::Node;
use ldk_server_protos::api::{GetPaymentDetailsRequest, GetPaymentDetailsResponse};
use std::sync::Arc;

pub(crate) const GET_PAYMENT_DETAILS_PATH: &str = "GetPaymentDetails";

pub(crate) fn handle_get_payment_details_request(
node: Arc<Node>, request: GetPaymentDetailsRequest,
context: Context, request: GetPaymentDetailsRequest,
) -> Result<GetPaymentDetailsResponse, ldk_node::NodeError> {
let payment_id_bytes = <[u8; PaymentId::LENGTH]>::from_hex(&request.payment_id)
.map_err(|_| ldk_node::NodeError::InvalidPaymentId)?;

let payment_details = node.payment(&PaymentId(payment_id_bytes));
let payment_details = context.node.payment(&PaymentId(payment_id_bytes));

let response = GetPaymentDetailsResponse {
payment: payment_details.map(|payment| payment_to_proto(payment)),
Expand Down
7 changes: 3 additions & 4 deletions ldk-server/src/api/list_channels.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::service::Context;
use crate::util::proto_adapter::channel_to_proto;
use ldk_node::Node;
use ldk_server_protos::api::{ListChannelsRequest, ListChannelsResponse};
use std::sync::Arc;

pub(crate) const LIST_CHANNELS_PATH: &str = "ListChannels";

pub(crate) fn handle_list_channels_request(
node: Arc<Node>, _request: ListChannelsRequest,
context: Context, _request: ListChannelsRequest,
) -> Result<ListChannelsResponse, ldk_node::NodeError> {
let channels = node.list_channels().into_iter().map(|c| channel_to_proto(c)).collect();
let channels = context.node.list_channels().into_iter().map(|c| channel_to_proto(c)).collect();

let response = ListChannelsResponse { channels };
Ok(response)
Expand Down
7 changes: 3 additions & 4 deletions ldk-server/src/api/list_payments.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::service::Context;
use crate::util::proto_adapter::payment_to_proto;
use ldk_node::Node;
use ldk_server_protos::api::{ListPaymentsRequest, ListPaymentsResponse};
use std::sync::Arc;

pub(crate) const LIST_PAYMENTS_PATH: &str = "ListPayments";

pub(crate) fn handle_list_payments_request(
node: Arc<Node>, _request: ListPaymentsRequest,
context: Context, _request: ListPaymentsRequest,
) -> Result<ListPaymentsResponse, ldk_node::NodeError> {
let payments = node.list_payments().into_iter().map(|p| payment_to_proto(p)).collect();
let payments = context.node.list_payments().into_iter().map(|p| payment_to_proto(p)).collect();

let response = ListPaymentsResponse { payments };
Ok(response)
Expand Down
10 changes: 5 additions & 5 deletions ldk-server/src/api/onchain_receive.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use ldk_node::Node;
use crate::service::Context;
use ldk_server_protos::api::{OnchainReceiveRequest, OnchainReceiveResponse};
use std::sync::Arc;

pub(crate) const ONCHAIN_RECEIVE_PATH: &str = "OnchainReceive";
pub(crate) fn handle_onchain_receive_request(
node: Arc<Node>, _request: OnchainReceiveRequest,
context: Context, _request: OnchainReceiveRequest,
) -> Result<OnchainReceiveResponse, ldk_node::NodeError> {
let response =
OnchainReceiveResponse { address: node.onchain_payment().new_address()?.to_string() };
let response = OnchainReceiveResponse {
address: context.node.onchain_payment().new_address()?.to_string(),
};
Ok(response)
}
13 changes: 7 additions & 6 deletions ldk-server/src/api/onchain_send.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use crate::service::Context;
use ldk_node::bitcoin::Address;
use ldk_node::Node;
use ldk_server_protos::api::{OnchainSendRequest, OnchainSendResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const ONCHAIN_SEND_PATH: &str = "OnchainSend";

pub(crate) fn handle_onchain_send_request(
node: Arc<Node>, request: OnchainSendRequest,
context: Context, request: OnchainSendRequest,
) -> Result<OnchainSendResponse, ldk_node::NodeError> {
let address = Address::from_str(&request.address)
.map_err(|_| ldk_node::NodeError::InvalidAddress)?
.require_network(node.config().network)
.require_network(context.node.config().network)
.map_err(|_| ldk_node::NodeError::InvalidAddress)?;
let txid = match (request.amount_sats, request.send_all) {
(Some(amount_sats), None) => {
node.onchain_payment().send_to_address(&address, amount_sats)?
context.node.onchain_payment().send_to_address(&address, amount_sats)?
},
// Retain existing api behaviour to not retain reserves on `send_all_to_address`.
(None, Some(true)) => node.onchain_payment().send_all_to_address(&address, false)?,
(None, Some(true)) => {
context.node.onchain_payment().send_all_to_address(&address, false)?
},
_ => return Err(ldk_node::NodeError::InvalidAmount),
};
let response = OnchainSendResponse { txid: txid.to_string() };
Expand Down
9 changes: 4 additions & 5 deletions ldk-server/src/api/open_channel.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use crate::service::Context;
use bytes::Bytes;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::lightning::ln::msgs::SocketAddress;
use ldk_node::Node;
use ldk_server_protos::api::{OpenChannelRequest, OpenChannelResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const OPEN_CHANNEL_PATH: &str = "OpenChannel";

pub(crate) fn handle_open_channel(
node: Arc<Node>, request: OpenChannelRequest,
context: Context, request: OpenChannelRequest,
) -> Result<OpenChannelResponse, ldk_node::NodeError> {
let node_id = PublicKey::from_str(&request.node_pubkey)
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;
let address = SocketAddress::from_str(&request.address)
.map_err(|_| ldk_node::NodeError::InvalidSocketAddress)?;

let user_channel_id = if request.announce_channel {
node.open_announced_channel(
context.node.open_announced_channel(
node_id,
address,
request.channel_amount_sats,
Expand All @@ -26,7 +25,7 @@ pub(crate) fn handle_open_channel(
None,
)?
} else {
node.open_channel(
context.node.open_channel(
node_id,
address,
request.channel_amount_sats,
Expand Down
24 changes: 14 additions & 10 deletions ldk-server/src/api/update_channel_config.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::service::Context;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
use ldk_node::{Node, UserChannelId};
use ldk_node::UserChannelId;
use ldk_server_protos::api::{UpdateChannelConfigRequest, UpdateChannelConfigResponse};
use ldk_server_protos::types::channel_config::MaxDustHtlcExposure;
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const UPDATE_CHANNEL_CONFIG_PATH: &str = "UpdateChannelConfig";

pub(crate) fn handle_update_channel_config_request(
node: Arc<Node>, request: UpdateChannelConfigRequest,
context: Context, request: UpdateChannelConfigRequest,
) -> Result<UpdateChannelConfigResponse, ldk_node::NodeError> {
let user_channel_id: u128 =
request.user_channel_id.parse().map_err(|_| ldk_node::NodeError::InvalidChannelId)?;

//FIXME: Use ldk/ldk-node's partial config update api.
let current_config = node
let current_config = context
.node
.list_channels()
.into_iter()
.find(|c| c.user_channel_id.0 == user_channel_id)
Expand All @@ -27,12 +28,15 @@ pub(crate) fn handle_update_channel_config_request(

let counterparty_node_id = PublicKey::from_str(&request.counterparty_node_id)
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;
node.update_channel_config(
&UserChannelId(user_channel_id),
counterparty_node_id,
updated_channel_config,
)
.map_err(ldk_node::NodeError::from)?;

context
.node
.update_channel_config(
&UserChannelId(user_channel_id),
counterparty_node_id,
updated_channel_config,
)
.map_err(ldk_node::NodeError::from)?;

Ok(UpdateChannelConfigResponse {})
}
Expand Down
Loading

0 comments on commit 2d39edc

Please sign in to comment.