Skip to content

Commit a2691e3

Browse files
committed
Make route_handler and chan_handler Option<>al in MessageHandler
We currently "support" not having a router or channel in memory by forcing users to implement the handler trait and ignore every message. Instead, we can just do that ourselves to simplify the API.
1 parent 9fba7c9 commit a2691e3

File tree

5 files changed

+266
-87
lines changed

5 files changed

+266
-87
lines changed

lightning-c-bindings/include/lightning.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5246,6 +5246,8 @@ struct LDKCResult_HTLCUpdateDecodeErrorZ HTLCUpdate_read(struct LDKu8slice ser);
52465246

52475247
void ChannelMonitor_free(struct LDKChannelMonitor this_ptr);
52485248

5249+
struct LDKChannelMonitor ChannelMonitor_clone(const struct LDKChannelMonitor *NONNULL_PTR orig);
5250+
52495251
struct LDKCVec_u8Z ChannelMonitor_write(const struct LDKChannelMonitor *NONNULL_PTR obj);
52505252

52515253
/**

lightning-c-bindings/src/c_types/derived.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,6 +3540,14 @@ impl Drop for CVec_ChannelMonitorZ {
35403540
unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) };
35413541
}
35423542
}
3543+
impl Clone for CVec_ChannelMonitorZ {
3544+
fn clone(&self) -> Self {
3545+
let mut res = Vec::new();
3546+
if self.datalen == 0 { return Self::from(res); }
3547+
res.extend_from_slice(unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) });
3548+
Self::from(res)
3549+
}
3550+
}
35433551
#[repr(C)]
35443552
pub struct C2Tuple_BlockHashChannelManagerZ {
35453553
pub a: crate::c_types::ThirtyTwoBytes,

lightning-c-bindings/src/chain/channelmonitor.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,24 @@ impl ChannelMonitor {
493493
ret
494494
}
495495
}
496+
impl Clone for ChannelMonitor {
497+
fn clone(&self) -> Self {
498+
Self {
499+
inner: if self.inner.is_null() { std::ptr::null_mut() } else {
500+
Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
501+
is_owned: true,
502+
}
503+
}
504+
}
505+
#[allow(unused)]
506+
/// Used only if an object of this type is returned as a trait impl by a method
507+
pub(crate) extern "C" fn ChannelMonitor_clone_void(this_ptr: *const c_void) -> *mut c_void {
508+
Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelMonitor)).clone() })) as *mut c_void
509+
}
510+
#[no_mangle]
511+
pub extern "C" fn ChannelMonitor_clone(orig: &ChannelMonitor) -> ChannelMonitor {
512+
orig.clone()
513+
}
496514
#[no_mangle]
497515
pub extern "C" fn ChannelMonitor_write(obj: &ChannelMonitor) -> crate::c_types::derived::CVec_u8Z {
498516
crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })

lightning-net-tokio/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ mod tests {
600600
msg_events: Mutex::new(Vec::new()),
601601
});
602602
let a_manager = Arc::new(PeerManager::new(MessageHandler {
603-
chan_handler: Arc::clone(&a_handler),
604-
route_handler: Arc::clone(&a_handler),
603+
chan_handler: Some(Arc::clone(&a_handler)),
604+
route_handler: Some(Arc::clone(&a_handler)),
605605
}, a_key.clone(), &[1; 32], Arc::new(TestLogger())));
606606

607607
let (b_connected_sender, mut b_connected) = mpsc::channel(1);
@@ -614,8 +614,8 @@ mod tests {
614614
msg_events: Mutex::new(Vec::new()),
615615
});
616616
let b_manager = Arc::new(PeerManager::new(MessageHandler {
617-
chan_handler: Arc::clone(&b_handler),
618-
route_handler: Arc::clone(&b_handler),
617+
chan_handler: Some(Arc::clone(&b_handler)),
618+
route_handler: Some(Arc::clone(&b_handler)),
619619
}, b_key.clone(), &[2; 32], Arc::new(TestLogger())));
620620

621621
// We bind on localhost, hoping the environment is properly configured with a local

0 commit comments

Comments
 (0)