Skip to content

Commit b16dc89

Browse files
committed
Start storing ForwardedPayments using PaginatedKVStore.
1 parent 25c0e70 commit b16dc89

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

ldk-server/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@ ldk-server-protos = { path = "../ldk-server-protos" }
1616
bytes = "1.4.0"
1717
hex = { package = "hex-conservative", version = "0.2.1", default-features = false }
1818
rusqlite = { version = "0.31.0", features = ["bundled"] }
19-
20-
[dev-dependencies]
2119
rand = "0.8.5"

ldk-server/src/io/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
pub(crate) mod paginated_kv_store;
22
pub(crate) mod sqlite_store;
33
pub(crate) mod utils;
4+
5+
/// The forwarded payments will be persisted under this prefix.
6+
pub(crate) const FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE: &str = "forwarded_payments";
7+
pub(crate) const FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";

ldk-server/src/main.rs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ use tokio::signal::unix::SignalKind;
1313
use hyper::server::conn::http1;
1414
use hyper_util::rt::TokioIo;
1515

16+
use crate::io::paginated_kv_store::PaginatedKVStore;
17+
use crate::io::sqlite_store::SqliteStore;
18+
use crate::io::{
19+
FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
20+
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
21+
};
1622
use crate::util::config::load_config;
23+
use crate::util::proto_adapter::forwarded_payment_to_proto;
24+
use hex::DisplayHex;
1725
use ldk_node::config::Config;
26+
use prost::Message;
27+
use rand::Rng;
1828
use std::fs;
19-
use std::path::Path;
29+
use std::path::{Path, PathBuf};
2030
use std::sync::Arc;
31+
use std::time::{SystemTime, UNIX_EPOCH};
2132

2233
const USAGE_GUIDE: &str = "Usage: ldk-server <config_path>";
2334

@@ -44,7 +55,7 @@ fn main() {
4455
let config_file = load_config(Path::new(arg)).expect("Invalid configuration file.");
4556

4657
ldk_node_config.log_level = LogLevel::Trace;
47-
ldk_node_config.storage_dir_path = config_file.storage_dir_path;
58+
ldk_node_config.storage_dir_path = config_file.storage_dir_path.clone();
4859
ldk_node_config.listening_addresses = Some(vec![config_file.listening_addr]);
4960
ldk_node_config.network = config_file.network;
5061

@@ -75,6 +86,15 @@ fn main() {
7586
},
7687
};
7788

89+
let paginated_store =
90+
Arc::new(match SqliteStore::new(PathBuf::from(config_file.storage_dir_path), None, None) {
91+
Ok(store) => store,
92+
Err(e) => {
93+
eprintln!("Failed to create SqliteStore: {:?}", e);
94+
std::process::exit(-1);
95+
},
96+
});
97+
7898
println!("Starting up...");
7999
match node.start_with_runtime(Arc::clone(&runtime)) {
80100
Ok(()) => {},
@@ -111,22 +131,74 @@ fn main() {
111131
"CHANNEL_PENDING: {} from counterparty {}",
112132
channel_id, counterparty_node_id
113133
);
134+
event_node.event_handled();
114135
},
115136
Event::ChannelReady { channel_id, counterparty_node_id, .. } => {
116137
println!(
117138
"CHANNEL_READY: {} from counterparty {:?}",
118139
channel_id, counterparty_node_id
119140
);
141+
event_node.event_handled();
120142
},
121143
Event::PaymentReceived { payment_id, payment_hash, amount_msat } => {
122144
println!(
123145
"PAYMENT_RECEIVED: with id {:?}, hash {}, amount_msat {}",
124146
payment_id, payment_hash, amount_msat
125147
);
148+
event_node.event_handled();
149+
},
150+
Event::PaymentForwarded {
151+
prev_channel_id,
152+
next_channel_id,
153+
prev_user_channel_id,
154+
next_user_channel_id,
155+
total_fee_earned_msat,
156+
skimmed_fee_msat,
157+
claim_from_onchain_tx,
158+
outbound_amount_forwarded_msat
159+
} => {
160+
161+
println!("PAYMENT_FORWARDED: with outbound_amount_forwarded_msat {}, total_fee_earned_msat: {}, inbound channel: {}, outbound channel: {}",
162+
outbound_amount_forwarded_msat.unwrap_or(0), total_fee_earned_msat.unwrap_or(0), prev_channel_id, next_channel_id
163+
);
164+
165+
let forwarded_payment = forwarded_payment_to_proto(
166+
prev_channel_id,
167+
next_channel_id,
168+
prev_user_channel_id,
169+
next_user_channel_id,
170+
total_fee_earned_msat,
171+
skimmed_fee_msat,
172+
claim_from_onchain_tx,
173+
outbound_amount_forwarded_msat
174+
);
175+
176+
// We don't expose this payment-id to the user, it is a temporary measure to generate
177+
// some unique identifiers until we have forwarded-payment-id available in ldk.
178+
// Currently, this is the expected user handling behaviour for forwarded payments.
179+
let mut forwarded_payment_id = [0u8;32];
180+
rand::thread_rng().fill(&mut forwarded_payment_id);
181+
182+
let forwarded_payment_creation_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs() as i64;
183+
184+
match paginated_store.write(FORWARDED_PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
185+
&forwarded_payment_id.to_lower_hex_string(),
186+
forwarded_payment_creation_time,
187+
&forwarded_payment.encode_to_vec(),
188+
) {
189+
Ok(_) => {
190+
event_node.event_handled();
191+
}
192+
Err(e) => {
193+
println!("Failed to write forwarded payment to persistence: {}", e);
194+
}
195+
}
196+
},
197+
_ => {
198+
event_node.event_handled();
126199
},
127-
_ => {},
128200
}
129-
event_node.event_handled();
201+
130202
},
131203
res = rest_svc_listener.accept() => {
132204
match res {

ldk-server/src/util/proto_adapter.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use bytes::Bytes;
22
use hex::prelude::*;
33
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
4+
use ldk_node::lightning::ln::types::ChannelId;
45
use ldk_node::payment::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus};
5-
use ldk_node::{ChannelDetails, LightningBalance, PendingSweepBalance};
6+
use ldk_node::{ChannelDetails, Event, LightningBalance, PendingSweepBalance, UserChannelId};
67
use ldk_server_protos::types::lightning_balance::BalanceType::{
78
ClaimableAwaitingConfirmations, ClaimableOnChannelClose, ContentiousClaimable,
89
CounterpartyRevokedOutputClaimable, MaybePreimageClaimableHtlc, MaybeTimeoutClaimableHtlc,
@@ -13,7 +14,7 @@ use ldk_server_protos::types::payment_kind::Kind::{
1314
use ldk_server_protos::types::pending_sweep_balance::BalanceType::{
1415
AwaitingThresholdConfirmations, BroadcastAwaitingConfirmation, PendingBroadcast,
1516
};
16-
use ldk_server_protos::types::{Channel, LspFeeLimits, OutPoint, Payment};
17+
use ldk_server_protos::types::{Channel, ForwardedPayment, LspFeeLimits, OutPoint, Payment};
1718

1819
pub(crate) fn channel_to_proto(channel: ChannelDetails) -> Channel {
1920
Channel {
@@ -320,3 +321,21 @@ pub(crate) fn pending_sweep_balance_to_proto(
320321
},
321322
}
322323
}
324+
325+
pub(crate) fn forwarded_payment_to_proto(
326+
prev_channel_id: ChannelId, next_channel_id: ChannelId,
327+
prev_user_channel_id: Option<UserChannelId>, next_user_channel_id: Option<UserChannelId>,
328+
total_fee_earned_msat: Option<u64>, skimmed_fee_msat: Option<u64>, claim_from_onchain_tx: bool,
329+
outbound_amount_forwarded_msat: Option<u64>,
330+
) -> ForwardedPayment {
331+
ForwardedPayment {
332+
prev_channel_id: prev_channel_id.to_string(),
333+
next_channel_id: next_channel_id.to_string(),
334+
prev_user_channel_id: prev_user_channel_id.expect("").0.to_string(),
335+
next_user_channel_id: next_user_channel_id.map(|u| u.0.to_string()),
336+
total_fee_earned_msat,
337+
skimmed_fee_msat,
338+
claim_from_onchain_tx,
339+
outbound_amount_forwarded_msat,
340+
}
341+
}

0 commit comments

Comments
 (0)