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 13, 2024
1 parent 2d39edc commit 5f34539
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
47 changes: 47 additions & 0 deletions ldk-server/src/api/list_forwarded_payments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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;

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

pub(crate) fn handle_list_forwarded_payments_request(
context: 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
2 changes: 1 addition & 1 deletion ldk-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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
20 changes: 17 additions & 3 deletions ldk-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,38 @@ use crate::api::get_payment_details::{
handle_get_payment_details_request, GET_PAYMENT_DETAILS_PATH,
};
use crate::api::list_channels::{handle_list_channels_request, LIST_CHANNELS_PATH};
use crate::api::list_forwarded_payments::{
handle_list_forwarded_payments_request, LIST_FORWARDED_PAYMENTS_PATH,
};
use crate::api::list_payments::{handle_list_payments_request, LIST_PAYMENTS_PATH};
use crate::api::onchain_receive::{handle_onchain_receive_request, ONCHAIN_RECEIVE_PATH};
use crate::api::onchain_send::{handle_onchain_send_request, ONCHAIN_SEND_PATH};
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;

#[derive(Clone)]
pub struct NodeService {
node: Arc<Node>,
paginated_kv_store: Arc<dyn PaginatedKVStore + Send + Sync>,
}

impl NodeService {
pub(crate) fn new(node: Arc<Node>) -> Self {
Self { node }
pub(crate) fn new(
node: Arc<Node>, paginated_kv_store: Arc<dyn PaginatedKVStore + Send + Sync>,
) -> Self {
Self { 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 All @@ -50,7 +58,10 @@ impl Service<Request<Incoming>> for NodeService {
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

fn call(&self, req: Request<Incoming>) -> Self::Future {
let context = Context { node: Arc::clone(&self.node) };
let context = Context {
node: Arc::clone(&self.node),
paginated_kv_store: Arc::clone(&self.paginated_kv_store),
};
// Exclude '/' from path pattern matching.
match &req.uri().path()[1..] {
GET_NODE_INFO => Box::pin(handle_request(context, req, handle_get_node_info_request)),
Expand Down Expand Up @@ -85,6 +96,9 @@ impl Service<Request<Incoming>> for NodeService {
LIST_PAYMENTS_PATH => {
Box::pin(handle_request(context, req, handle_list_payments_request))
},
LIST_FORWARDED_PAYMENTS_PATH => {
Box::pin(handle_request(context, req, handle_list_forwarded_payments_request))
},
path => {
let error = format!("Unknown request: {}", path).into_bytes();
Box::pin(async {
Expand Down

0 comments on commit 5f34539

Please sign in to comment.