Skip to content

Commit e2f293b

Browse files
committed
wire up async receive offer fetch
1 parent d0bd340 commit e2f293b

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ fn build_with_store_internal(
14581458
Arc::clone(&channel_manager),
14591459
message_router,
14601460
Arc::clone(&channel_manager),
1461-
IgnoringMessageHandler {},
1461+
Arc::clone(&channel_manager),
14621462
IgnoringMessageHandler {},
14631463
IgnoringMessageHandler {},
14641464
));

src/event.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use lightning::events::{Event as LdkEvent, PaymentFailureReason};
3737
use lightning::impl_writeable_tlv_based_enum;
3838
use lightning::ln::channelmanager::PaymentId;
3939
use lightning::ln::types::ChannelId;
40+
use lightning::offers::static_invoice::StaticInvoice;
41+
use lightning::onion_message::messenger::Responder;
4042
use lightning::routing::gossip::NodeId;
4143
use lightning::util::config::{
4244
ChannelConfigOverrides, ChannelConfigUpdate, ChannelHandshakeConfigUpdate,
@@ -56,7 +58,7 @@ use rand::{thread_rng, Rng};
5658

5759
use core::future::Future;
5860
use core::task::{Poll, Waker};
59-
use std::collections::VecDeque;
61+
use std::collections::{HashMap, VecDeque};
6062
use std::ops::Deref;
6163
use std::sync::{Arc, Condvar, Mutex};
6264

@@ -458,6 +460,32 @@ where
458460
runtime: Arc<Runtime>,
459461
logger: L,
460462
config: Arc<Config>,
463+
static_invoice_store: StaticInvoiceStore,
464+
}
465+
466+
struct StaticInvoiceStore {
467+
pub static_invoices: Mutex<HashMap<(Vec<u8>, u16), StaticInvoice>>,
468+
}
469+
470+
impl StaticInvoiceStore {
471+
fn new() -> Self {
472+
Self { static_invoices: Mutex::new(HashMap::new()) }
473+
}
474+
475+
fn handle_static_invoice_requested(
476+
&self, recipient_id: Vec<u8>, invoice_slot: u16,
477+
) -> Option<StaticInvoice> {
478+
let map = self.static_invoices.lock().unwrap();
479+
480+
map.get(&(recipient_id.clone(), invoice_slot)).cloned()
481+
}
482+
483+
fn handle_persist_static_invoice(
484+
&self, invoice: StaticInvoice, invoice_slot: u16, recipient_id: Vec<u8>,
485+
) {
486+
let mut map = self.static_invoices.lock().unwrap();
487+
map.insert((recipient_id, invoice_slot), invoice);
488+
}
461489
}
462490

463491
impl<L: Deref + Clone + Sync + Send + 'static> EventHandler<L>
@@ -487,6 +515,7 @@ where
487515
logger,
488516
runtime,
489517
config,
518+
static_invoice_store: StaticInvoiceStore::new(),
490519
}
491520
}
492521

@@ -1494,11 +1523,34 @@ where
14941523
LdkEvent::OnionMessagePeerConnected { .. } => {
14951524
debug_assert!(false, "We currently don't support onion message interception, so this event should never be emitted.");
14961525
},
1497-
LdkEvent::PersistStaticInvoice { .. } => {
1498-
debug_assert!(false, "We currently don't support static invoice persistence, so this event should never be emitted.");
1526+
1527+
LdkEvent::PersistStaticInvoice {
1528+
invoice,
1529+
invoice_slot,
1530+
recipient_id,
1531+
invoice_persisted_path,
1532+
} => {
1533+
self.static_invoice_store.handle_persist_static_invoice(
1534+
invoice,
1535+
invoice_slot,
1536+
recipient_id,
1537+
);
1538+
1539+
self.channel_manager.static_invoice_persisted(invoice_persisted_path);
14991540
},
1500-
LdkEvent::StaticInvoiceRequested { .. } => {
1501-
debug_assert!(false, "We currently don't support static invoice persistence, so this event should never be emitted.");
1541+
LdkEvent::StaticInvoiceRequested { recipient_id, invoice_slot, reply_path } => {
1542+
let invoice = self
1543+
.static_invoice_store
1544+
.handle_static_invoice_requested(recipient_id, invoice_slot);
1545+
1546+
if let Some(invoice) = invoice {
1547+
match self.channel_manager.send_static_invoice(invoice, reply_path) {
1548+
Ok(()) => {},
1549+
Err(_) => {
1550+
log_error!(self.logger, "Failed to send static invoice");
1551+
},
1552+
}
1553+
}
15021554
},
15031555
LdkEvent::FundingTransactionReadyForSigning { .. } => {
15041556
debug_assert!(false, "We currently don't support interactive-tx, so this event should never be emitted.");

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub(crate) type OnionMessenger = lightning::onion_message::messenger::OnionMesse
123123
Arc<ChannelManager>,
124124
Arc<MessageRouter>,
125125
Arc<ChannelManager>,
126-
IgnoringMessageHandler,
126+
Arc<ChannelManager>,
127127
IgnoringMessageHandler,
128128
IgnoringMessageHandler,
129129
>;

tests/integration_tests_rust.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,15 +1171,9 @@ fn static_invoice_server() {
11711171
// Sleep to get the cache filled.
11721172
std::thread::sleep(std::time::Duration::from_secs(1));
11731173

1174-
let mut iter = 0;
1175-
while node_b.bolt12_payment().get_async_receive_offer().is_err() {
1176-
std::thread::sleep(std::time::Duration::from_millis(1000));
1174+
let offer = node_b.bolt12_payment().get_async_receive_offer().unwrap();
11771175

1178-
iter += 1;
1179-
if iter > 10 {
1180-
assert!(false, "Failed to fetch static invoice offer");
1181-
}
1182-
}
1176+
println!("Offer: {}", offer);
11831177
}
11841178

11851179
#[test]

0 commit comments

Comments
 (0)