Skip to content

Commit

Permalink
Add Api impl for ListForwardedPayments.
Browse files Browse the repository at this point in the history
  • Loading branch information
G8XSU committed Dec 11, 2024
1 parent 3b788c2 commit 15964ea
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
48 changes: 48 additions & 0 deletions ldk-server/src/api/list_forwarded_payments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::io::{
FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
};
use crate::service::Context;
use bytes::Bytes;
use ldk_server_protos::api::{ListForwardedPaymentsRequest, ListForwardedPaymentsResponse};
use ldk_server_protos::types::{ForwardedPayment, PageToken};
use prost::Message;
use std::sync::Arc;

pub(crate) const LIST_FORWARDED_PAYMENTS_PATH: &str = "ListForwardedPayments";

pub(crate) fn handle_list_forwarded_payments_request(
context: Arc<Context>, request: ListForwardedPaymentsRequest,
) -> Result<ListForwardedPaymentsResponse, ldk_node::NodeError> {
let page_token = request.page_token.map(|p| (p.token, p.index));
let list_response = context
.paginated_kv_store
.list(
FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
page_token,
)
.map_err(|_| ldk_node::NodeError::ConnectionFailed)?;

let mut forwarded_payments: Vec<ForwardedPayment> = vec![];
for key in list_response.keys {
let forwarded_payment_bytes = context
.paginated_kv_store
.read(
FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
&key,
)
.map_err(|_| ldk_node::NodeError::ConnectionFailed)?;
let forwarded_payment = ForwardedPayment::decode(Bytes::from(forwarded_payment_bytes))
.map_err(|_| ldk_node::NodeError::ConnectionFailed)?;
forwarded_payments.push(forwarded_payment);
}
let response = ListForwardedPaymentsResponse {
forwarded_payments,
next_page_token: list_response
.next_page_token
.map(|(token, index)| PageToken { token, index }),
};
Ok(response)
}
1 change: 1 addition & 0 deletions ldk-server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) mod get_balances;
pub(crate) mod get_node_info;
pub(crate) mod get_payment_details;
pub(crate) mod list_channels;
pub(crate) mod list_forwarded_payments;
pub(crate) mod list_payments;
pub(crate) mod onchain_receive;
pub(crate) mod onchain_send;
Expand Down
7 changes: 4 additions & 3 deletions ldk-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ fn main() {
},
};

let paginated_store =
SqliteStore::new(PathBuf::from(config_file.storage_dir_path), None, None).unwrap();
let paginated_store = Arc::new(
SqliteStore::new(PathBuf::from(config_file.storage_dir_path), None, None).unwrap(),
);

println!("Starting up...");
match node.start_with_runtime(Arc::clone(&runtime)) {
Expand Down Expand Up @@ -186,7 +187,7 @@ fn main() {
match res {
Ok((stream, _)) => {
let io_stream = TokioIo::new(stream);
let node_service = NodeService::new(Arc::clone(&node));
let node_service = NodeService::new(Arc::clone(&node), Arc::clone(&paginated_store) as Arc<dyn PaginatedKVStore + Send + Sync>);
runtime.spawn(async move {
if let Err(err) = http1::Builder::new().serve_connection(io_stream, node_service).await {
eprintln!("Failed to serve connection: {}", err);
Expand Down
8 changes: 6 additions & 2 deletions ldk-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::api::open_channel::{handle_open_channel, OPEN_CHANNEL_PATH};
use crate::api::update_channel_config::{
handle_update_channel_config_request, UPDATE_CHANNEL_CONFIG_PATH,
};
use crate::io::paginated_kv_store::PaginatedKVStore;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
Expand All @@ -35,13 +36,16 @@ pub struct NodeService {
}

impl NodeService {
pub(crate) fn new(node: Arc<Node>) -> Self {
Self { context: Arc::new(Context { node }) }
pub(crate) fn new(
node: Arc<Node>, paginated_kv_store: Arc<dyn PaginatedKVStore + Send + Sync>,
) -> Self {
Self { context: Arc::new(Context { node, paginated_kv_store }) }
}
}

pub(crate) struct Context {
pub(crate) node: Arc<Node>,
pub(crate) paginated_kv_store: Arc<dyn PaginatedKVStore + Send + Sync>,
}

impl Service<Request<Incoming>> for NodeService {
Expand Down

0 comments on commit 15964ea

Please sign in to comment.