Skip to content

Commit 53a2889

Browse files
committed
impl Readable/Writable to Vec<ChannelDetails>
1 parent 5194d73 commit 53a2889

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

bindings/src/channelmanager.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ use crate::{
3333
FFISecret,
3434
FFIOutPoint
3535
},
36-
*,
37-
}
36+
*
37+
},
38+
utils::into_fixed_buffer,
3839
};
3940
use lightning::ln::channelmanager::{PaymentSecret, PaymentPreimage};
4041

@@ -171,6 +172,13 @@ ffi! {
171172
FFIResult::ok()
172173
}
173174

175+
fn list_channels(buf_out: Out<u8>, buf_len: usize, actual_channels_len: Out<usize>, handle: FFIArcChannelManagerHandle) -> FFIResult {
176+
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));
177+
let chan_man: &FFIArcChannelManager = unsafe_block!("We know handle points to valid channel_manager" => handle.as_ref());
178+
let mut channels = chan_man.list_channels();
179+
into_fixed_buffer(&mut channels, buf, &mut actual_channels_len)
180+
}
181+
174182
fn create_channel(their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, handle: FFIArcChannelManagerHandle) -> FFIResult {
175183
create_channel_inner(their_network_key, channel_value_satoshis, push_msat, user_id, None, handle)
176184
}

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,16 @@ impl Writeable for ChannelDetails {
500500
}
501501
}
502502

503+
impl Writeable for Vec<ChannelDetails> {
504+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
505+
(self.len() as u16).write(writer)?;
506+
for i in self.iter() {
507+
i.write(writer)?;
508+
}
509+
Ok(())
510+
}
511+
}
512+
503513
impl Readable for ChannelDetails {
504514
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
505515
let channel_id: [u8; 32] = Readable::read(reader)?;
@@ -518,6 +528,18 @@ impl Readable for ChannelDetails {
518528
}
519529
}
520530

531+
impl Readable for Vec<ChannelDetails> {
532+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
533+
let len: u16 = Readable::read(r)?;
534+
let mut ret = Vec::with_capacity(len as usize);
535+
for i in 0..(len as usize) {
536+
let d: ChannelDetails = Readable::read(r)?;
537+
ret[i] = d;
538+
}
539+
Ok(ret)
540+
}
541+
}
542+
521543
/// If a payment fails to send, it can be in one of several states. This enum is returned as the
522544
/// Err() type describing which state the payment is in, see the description of individual enum
523545
/// states for more.

0 commit comments

Comments
 (0)