Skip to content

Commit 12989ab

Browse files
committed
add entrpoint for getting pending events from chanman
1 parent c289caf commit 12989ab

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use lightning::ln::channelmonitor::{ManyChannelMonitor, ChannelMonitor, HTLCUpda
1717

1818
pub mod primitives;
1919
use primitives::*;
20+
use lightning::util::ser::{Writeable, Writer};
21+
use std::io::Error;
2022

2123
pub mod broadcaster_fn {
2224
use crate::adaptors::primitives::FFITransaction;
@@ -224,5 +226,3 @@ impl ChainWatchInterface for FFIChainWatchInterface {
224226
}
225227
}
226228

227-
pub mod block_notifier_fn {
228-
}

bindings/src/adaptors/primitives.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use crate::FFIResult;
77
use lightning::chain::chaininterface::ChainError;
88
use lightning::ln::channelmanager::PaymentHash;
99
use lightning::ln::router::{RouteHop, Route};
10-
use lightning::util::ser::Readable;
10+
use lightning::util::ser::{Readable, Writeable};
1111
use bitcoin_hashes::core::fmt::{Formatter, Error};
12+
use lightning::util::events::Event;
13+
use lightning::ln::msgs::DecodeError;
1214

1315
macro_rules! array_struct{
1416
(
@@ -36,6 +38,16 @@ macro_rules! array_struct{
3638
}
3739
}
3840

41+
impl<'a> From<Box<[$ty]>> for $name<'a> {
42+
fn from(slice: Box<[$ty]>) -> Self {
43+
$name::new(
44+
slice.as_ptr(),
45+
slice.len(),
46+
)
47+
}
48+
}
49+
50+
3951
impl<'a> std::fmt::Debug for $name<'a> {
4052
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4153
hex::encode(self).fmt(f)
@@ -47,7 +59,6 @@ macro_rules! array_struct{
4759
unsafe_block!("" => std::slice::from_raw_parts(self.ptr, self.len))
4860
}
4961
}
50-
5162
}
5263
}
5364

@@ -153,3 +164,19 @@ impl<'a> TryFrom<FFIRoute<'a>> for Route {
153164

154165
array_struct!(FFITransaction, u8);
155166
array_struct!(FFIBlock, u8);
167+
array_struct!(FFIEvents, u8);
168+
169+
170+
impl<'a> TryFrom<Vec<Event>> for FFIEvents<'a> {
171+
type Error = lightning::ln::msgs::DecodeError;
172+
173+
fn try_from(value: Vec<Event>) -> Result<Self, Self::Error> {
174+
let len = value.len();
175+
let mut result_vec: Vec<u8> = Vec::with_capacity(len * std::mem::size_of::<Event>());
176+
for e in value {
177+
result_vec.extend(e.encode());
178+
}
179+
let r = result_vec.into_boxed_slice();
180+
Ok(r.into())
181+
}
182+
}

bindings/src/channelmanager.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ use crate::{
1515
error::FFIResult,
1616
handle::{Out, Ref, HandleShared},
1717
adaptors::*,
18-
adaptors::primitives::{FFISha256dHash, FFIRoute},
18+
adaptors::primitives::{FFISha256dHash, FFIRoute, FFIEvents},
1919
channelmonitor::{FFIManyChannelMonitor},
2020
};
21+
use lightning::util::events::EventsProvider;
2122

2223
type FFIArcChannelManager = ChannelManager<InMemoryChannelKeys, Arc<FFIManyChannelMonitor>, Arc<FFIBroadCaster>, Arc<KeysManager>, Arc<FFIFeeEstimator>>;
2324
type FFIArcChannelManagerHandle<'a> = HandleShared<'a, FFIArcChannelManager>;
2425

26+
2527
ffi! {
2628

2729
fn create_channel_manager(
@@ -103,6 +105,13 @@ ffi! {
103105
FFIResult::ok()
104106
}
105107

108+
fn get_and_clear_pending_events(handle: FFIArcChannelManagerHandle, events: Out<FFIEvents>) -> FFIResult {
109+
let chan_man: &FFIArcChannelManager = unsafe_block!("We know handle points to valid channel_manager" => handle.as_ref());
110+
let e = chan_man.get_and_clear_pending_events().try_into()?;
111+
unsafe_block!("We know out parameter is writable" => events.init(e));
112+
FFIResult::ok()
113+
}
114+
106115
fn release_ffi_channel_manager(handle: FFIArcChannelManagerHandle) -> FFIResult {
107116
unsafe_block!("The upstream caller guarantees the handle will not be accessed after being freed" => FFIArcChannelManagerHandle::dealloc(handle, |mut handle| {
108117
FFIResult::ok()

0 commit comments

Comments
 (0)