Skip to content

Commit 04a3c30

Browse files
committed
f: review comments
1 parent 16ad843 commit 04a3c30

File tree

10 files changed

+51
-48
lines changed

10 files changed

+51
-48
lines changed

bindings/ldk_node.udl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ interface Node {
158158
[Throws=NodeError]
159159
bytes export_pathfinding_scores();
160160
[Throws=NodeError]
161-
void set_paths_to_static_invoice_server(sequence<u8> paths);
161+
void set_paths_to_static_invoice_server(bytes paths);
162162
[Throws=NodeError]
163-
sequence<u8> blinded_paths_for_async_recipient(sequence<u8> recipient_id);
163+
bytes blinded_paths_for_async_recipient(bytes recipient_id);
164164
};
165165

166166
[Enum]
@@ -214,7 +214,7 @@ interface Bolt12Payment {
214214
[Throws=NodeError]
215215
Refund initiate_refund(u64 amount_msat, u32 expiry_secs, u64? quantity, string? payer_note);
216216
[Throws=NodeError]
217-
Offer get_async_receive_offer();
217+
Offer receive_async();
218218
};
219219

220220
interface SpontaneousPayment {
@@ -318,7 +318,6 @@ enum NodeError {
318318
"LiquiditySourceUnavailable",
319319
"LiquidityFeeTooHigh",
320320
"InvalidBlindedPaths",
321-
"OperationFailed",
322321
};
323322

324323
dictionary NodeStatus {

src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ pub enum Error {
122122
LiquidityFeeTooHigh,
123123
/// The given blinded paths are invalid.
124124
InvalidBlindedPaths,
125-
/// The requested operation failed.
126-
OperationFailed,
127125
}
128126

129127
impl fmt::Display for Error {
@@ -198,7 +196,6 @@ impl fmt::Display for Error {
198196
write!(f, "The given operation failed due to the LSP's required opening fee being too high.")
199197
},
200198
Self::InvalidBlindedPaths => write!(f, "The given blinded paths are invalid."),
201-
Self::OperationFailed => write!(f, "The requested operation failed."),
202199
}
203200
}
204201
}

src/event.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use crate::static_invoice_store::StaticInvoiceStore;
8+
use crate::payment::static_invoice_store::StaticInvoiceStore;
99
use crate::types::{CustomTlvRecord, DynStore, PaymentStore, Sweeper, Wallet};
10-
1110
use crate::{
1211
hex_utils, BumpTransactionEventHandler, ChannelManager, Error, Graph, PeerInfo, PeerStore,
1312
UserChannelId,
@@ -1526,10 +1525,10 @@ where
15261525

15271526
match invoice {
15281527
Ok(Some(invoice)) => {
1529-
if let Err(_) =
1528+
if let Err(e) =
15301529
self.channel_manager.send_static_invoice(invoice, reply_path)
15311530
{
1532-
log_error!(self.logger, "Failed to send static invoice");
1531+
log_error!(self.logger, "Failed to send static invoice: {:?}", e);
15331532
}
15341533
},
15351534
Ok(None) => {},

src/io/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ pub(crate) const BDK_WALLET_TX_GRAPH_KEY: &str = "tx_graph";
7373
pub(crate) const BDK_WALLET_INDEXER_PRIMARY_NAMESPACE: &str = "bdk_wallet";
7474
pub(crate) const BDK_WALLET_INDEXER_SECONDARY_NAMESPACE: &str = "";
7575
pub(crate) const BDK_WALLET_INDEXER_KEY: &str = "indexer";
76+
77+
// Static invoices will be persisted at "static_invoices/<sha256(recipient_id)>/<invoice_slot>".
78+
//
79+
// Example: static_invoices/039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81/001f
80+
pub(crate) const STATIC_INVOICES_PRIMARY_NAMESPACE: &str = "static_invoices";

src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ pub mod logger;
9494
mod message_handler;
9595
pub mod payment;
9696
mod peer_store;
97-
mod rate_limiter;
9897
mod runtime;
99-
mod static_invoice_store;
10098
mod tx_broadcaster;
10199
mod types;
102100
mod wallet;
@@ -138,6 +136,7 @@ use gossip::GossipSource;
138136
use graph::NetworkGraph;
139137
use io::utils::write_node_metrics;
140138
use liquidity::{LSPS1Liquidity, LiquiditySource};
139+
use payment::static_invoice_store::StaticInvoiceStore;
141140
use payment::{
142141
Bolt11Payment, Bolt12Payment, OnchainPayment, PaymentDetails, SpontaneousPayment,
143142
UnifiedQrPayment,
@@ -175,8 +174,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
175174
use std::sync::{Arc, Mutex, RwLock};
176175
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
177176

178-
use crate::static_invoice_store::StaticInvoiceStore;
179-
180177
#[cfg(feature = "uniffi")]
181178
uniffi::include_scaffolding!("ldk_node");
182179

@@ -1512,10 +1509,10 @@ impl Node {
15121509
let paths = self
15131510
.channel_manager
15141511
.blinded_paths_for_async_recipient(recipient_id, None)
1515-
.or(Err(Error::OperationFailed))?;
1512+
.or(Err(Error::InvalidBlindedPaths))?;
15161513

15171514
let mut bytes = Vec::new();
1518-
paths.write(&mut bytes).or(Err(Error::OperationFailed))?;
1515+
paths.write(&mut bytes).or(Err(Error::InvalidBlindedPaths))?;
15191516

15201517
Ok(bytes)
15211518
}

src/payment/bolt12.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,16 @@ impl Bolt12Payment {
451451
Ok(maybe_wrap(refund))
452452
}
453453

454-
/// Retrieve an [`Offer`] for receiving async payments as an often-offline recipient. Will only return an offer if
455-
/// [`Node::set_paths_to_static_invoice_server`] was called and we succeeded in interactively building a
456-
/// [`StaticInvoice`] with the static invoice server.
454+
/// Retrieve an [`Offer`] for receiving async payments as an often-offline recipient.
455+
///
456+
/// Will only return an offer if [`Node::set_paths_to_static_invoice_server`] was called and we succeeded in
457+
/// interactively building a [`StaticInvoice`] with the static invoice server.
457458
///
458459
/// Useful for posting offers to receive payments later, such as posting an offer on a website.
459460
///
460461
/// [`Node::set_paths_to_static_invoice_server`]: crate::Node::set_paths_to_static_invoice_server
461462
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
462-
pub fn get_async_receive_offer(&self) -> Result<Offer, Error> {
463+
pub fn receive_async(&self) -> Result<Offer, Error> {
463464
self.channel_manager
464465
.get_async_receive_offer()
465466
.map(maybe_wrap)

src/payment/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
mod bolt11;
1111
mod bolt12;
1212
mod onchain;
13+
mod rate_limiter;
1314
mod spontaneous;
15+
pub(crate) mod static_invoice_store;
1416
pub(crate) mod store;
1517
mod unified_qr;
1618

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use std::collections::HashMap;
22
use std::time::{Duration, Instant};
33

44
/// Implements a leaky-bucket style rate limiter parameterized by the max capacity of the bucket, the refill interval,
5-
/// and the max idle duration. For every passing of the refill interval, one token is added to the bucket, up to the
6-
/// maximum capacity. When the bucket has remained at the maximum capacity for longer than the max idle duration, it is
7-
/// removed to prevent memory leakage.
5+
/// and the max idle duration.
6+
///
7+
/// For every passing of the refill interval, one token is added to the bucket, up to the maximum capacity. When the
8+
/// bucket has remained at the maximum capacity for longer than the max idle duration, it is removed to prevent memory
9+
/// leakage.
810
pub(crate) struct RateLimiter {
911
users: HashMap<Vec<u8>, Bucket>,
1012
capacity: u32,
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
use crate::{hex_utils, rate_limiter::RateLimiter, types::DynStore};
2-
use bitcoin::hashes::{sha256, Hash};
1+
use crate::hex_utils;
2+
use crate::io::STATIC_INVOICES_PRIMARY_NAMESPACE;
3+
use crate::payment::rate_limiter::RateLimiter;
4+
use crate::types::DynStore;
5+
6+
use bitcoin::hashes::sha256::Hash as Sha256;
7+
use bitcoin::hashes::Hash;
8+
39
use lightning::{offers::static_invoice::StaticInvoice, util::ser::Writeable};
4-
use std::{
5-
sync::{Arc, Mutex},
6-
time::Duration,
7-
};
10+
11+
use std::sync::{Arc, Mutex};
12+
use std::time::Duration;
813

914
pub(crate) struct StaticInvoiceStore {
1015
kv_store: Arc<DynStore>,
@@ -13,12 +18,6 @@ pub(crate) struct StaticInvoiceStore {
1318
}
1419

1520
impl StaticInvoiceStore {
16-
// Static invoices are stored at "static_invoices/<sha256(recipient_id)>/<invoice_slot>".
17-
//
18-
// Example:
19-
// static_invoices/039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81/001f
20-
const PRIMARY_NAMESPACE: &str = "static_invoices";
21-
2221
const RATE_LIMITER_BUCKET_CAPACITY: u32 = 5;
2322
const RATE_LIMITER_REFILL_INTERVAL: Duration = Duration::from_millis(100);
2423
const RATE_LIMITER_MAX_IDLE: Duration = Duration::from_secs(600);
@@ -57,14 +56,16 @@ impl StaticInvoiceStore {
5756

5857
let (secondary_namespace, key) = Self::get_storage_location(invoice_slot, recipient_id);
5958

60-
self.kv_store.read(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key).and_then(|data| {
61-
data.try_into().map(Some).map_err(|e| {
62-
lightning::io::Error::new(
63-
lightning::io::ErrorKind::InvalidData,
64-
format!("Failed to parse static invoice: {:?}", e),
65-
)
66-
})
67-
})
59+
self.kv_store.read(STATIC_INVOICES_PRIMARY_NAMESPACE, &secondary_namespace, &key).and_then(
60+
|data| {
61+
data.try_into().map(Some).map_err(|e| {
62+
lightning::io::Error::new(
63+
lightning::io::ErrorKind::InvalidData,
64+
format!("Failed to parse static invoice: {:?}", e),
65+
)
66+
})
67+
},
68+
)
6869
}
6970

7071
pub(crate) async fn handle_persist_static_invoice(
@@ -77,14 +78,14 @@ impl StaticInvoiceStore {
7778
let mut buf = Vec::new();
7879
invoice.write(&mut buf)?;
7980

80-
self.kv_store.write(Self::PRIMARY_NAMESPACE, &secondary_namespace, &key, buf)
81+
self.kv_store.write(STATIC_INVOICES_PRIMARY_NAMESPACE, &secondary_namespace, &key, buf)
8182
}
8283

8384
fn get_storage_location(invoice_slot: u16, recipient_id: Vec<u8>) -> (String, String) {
84-
let hash = sha256::Hash::hash(&recipient_id).to_byte_array();
85+
let hash = Sha256::hash(&recipient_id).to_byte_array();
8586
let secondary_namespace = hex_utils::to_string(&hash);
8687

87-
let key = hex_utils::to_string(&invoice_slot.to_be_bytes());
88+
let key = format!("{:05}", invoice_slot);
8889
(secondary_namespace, key)
8990
}
9091
}

tests/integration_tests_rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ fn static_invoice_server() {
12141214
node_receiver.set_paths_to_static_invoice_server(blinded_paths).unwrap();
12151215

12161216
let offer = loop {
1217-
if let Ok(offer) = node_receiver.bolt12_payment().get_async_receive_offer() {
1217+
if let Ok(offer) = node_receiver.bolt12_payment().receive_async() {
12181218
break offer;
12191219
}
12201220

0 commit comments

Comments
 (0)