Skip to content

Commit 5071a96

Browse files
committed
return variable length buffer as events
1 parent 5afde89 commit 5071a96

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

bindings/src/adaptors/primitives.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ use crate::{
2323
is_null::IsNull
2424
};
2525
use lightning::ln::channelmanager::PaymentPreimage;
26+
use std::io::{Error, Read};
27+
use lightning::ln::msgs::DecodeError;
28+
use lightning::util::ser::Writer;
2629

2730
#[derive(Debug, Clone)]
2831
#[repr(C)]
@@ -259,7 +262,6 @@ impl TryFrom<FFIRoute> for Route {
259262

260263
array_struct!(FFITransaction);
261264
array_struct!(FFIBlock);
262-
array_struct!(FFIEvents);
263265

264266
// General purpose byte array which has to cross ffi-boundary
265267
array_struct!(FFIBytes);
@@ -273,15 +275,29 @@ impl TryFrom<FFIBytes> for (Vec<&Transaction>, Vec<u32>) {
273275
}
274276
}
275277

276-
impl From<Vec<Event>> for FFIEvents {
278+
pub struct FFIEvents {
279+
pub events: Vec<Event>,
280+
}
277281

278-
fn from(value: Vec<Event>) -> Self {
279-
let len = value.len();
280-
let mut result_vec: Vec<u8> = Vec::with_capacity(len * std::mem::size_of::<Event>());
281-
for e in value {
282-
result_vec.extend(e.encode());
282+
impl Writeable for FFIEvents {
283+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
284+
(self.events.len() as u16).write(writer);
285+
for e in &self.events {
286+
match e {
287+
Event::FundingGenerationReady {ref temporary_channel_id, ref channel_value_satoshis, ref output_script, ref user_channel_id} => {
288+
temporary_channel_id.write(writer)?;
289+
channel_value_satoshis.write(writer)?;
290+
output_script.write(writer)?;
291+
user_channel_id.write(writer)?
292+
}
293+
Event::PendingHTLCsForwardable { ref time_forwardable } => {
294+
let milli = time_forwardable.as_millis() as u64;
295+
milli.write(writer)?;
296+
},
297+
x => x.write(writer)?,
298+
}
283299
}
284-
let r = result_vec.into_boxed_slice();
285-
r.into()
300+
Ok(())
286301
}
287302
}
303+

bindings/src/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ ffi! {
267267
FFIResult::ok()
268268
}
269269

270-
fn get_and_clear_pending_events(handle: FFIArcChannelManagerHandle, events: Out<FFIEvents>) -> FFIResult {
270+
fn get_and_clear_pending_events(handle: FFIArcChannelManagerHandle, buf_out: Out<u8>, buf_len: usize, actual_channels_len: Out<usize>) -> FFIResult {
271+
let buf = unsafe_block!("The buffer lives as long as this function, the length is within the buffer and the buffer won't be read before initialization" => buf_out.as_uninit_bytes_mut(buf_len));
271272
let chan_man: &FFIArcChannelManager = unsafe_block!("We know handle points to valid channel_manager" => handle.as_ref());
272-
let e = chan_man.get_and_clear_pending_events().try_into()?;
273-
unsafe_block!("We know out parameter is writable" => events.init(e));
274-
FFIResult::ok()
273+
let mut e = FFIEvents{ events: chan_man.get_and_clear_pending_events() };
274+
into_fixed_buffer(&mut e, buf, &mut actual_channels_len)
275275
}
276276

277277
fn release_ffi_channel_manager(handle: FFIArcChannelManagerHandle) -> FFIResult {

0 commit comments

Comments
 (0)