I see that we must implement Readable and Writable for wire messages (+ other things) at rust-lightning/lightning/src/ln/msgs.rs#L3673-L3698.
But I did not get the difference between (using existing WarningMessage as example):
impl Writeable for WarningMessage {
fn write(&self, w: &mut W) -> Result<(), io::Error> {
self.channel_id.write(w)?;
(self.data.len() as u16).write(w)?;
w.write_all(self.data.as_bytes())?;
Ok(())
}
}
impl Readable for WarningMessage {
fn read(r: &mut R) -> Result {
Ok(Self {
channel_id: Readable::read(r)?,
data: {
let sz: usize = ::read(r)? as usize;
let mut data = Vec::with_capacity(sz);
data.resize(sz, 0);
r.read_exact(&mut data)?;
match String::from_utf8(data) {
Ok(s) => s,
Err(_) => return Err(DecodeError::InvalidValue),
}
}
})
}
}
And:
impl_writeable_msg!(WarningMessage, {
channel_id,
data,
}, {});
Both passes the test:
cargo test --package lightning --lib -- ln::msgs::tests::encoding_warning --exact --show-output








