Skip to content

Commit a348209

Browse files
committed
Add option_without_length case to TLV macros
This allows us to handle `WithoutLength` fields to prevent the duplicate encoding of lengths in TLVs for some types that already encode length, such as `Script` in this case. This is needed so that we can move `shutdown_scriptpubkey` into the TLV stream of `OpenChannel` and `AcceptChannel`.
1 parent 16d0f2f commit a348209

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lightning/src/util/ser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SI
2929
use bitcoin::secp256k1::ecdsa;
3030
use bitcoin::secp256k1::schnorr;
3131
use bitcoin::blockdata::constants::ChainHash;
32-
use bitcoin::blockdata::script::Script;
32+
use bitcoin::blockdata::script::{Script, self};
3333
use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
3434
use bitcoin::consensus;
3535
use bitcoin::consensus::Encodable;
@@ -657,6 +657,21 @@ impl<'a, T> From<&'a Vec<T>> for WithoutLength<&'a Vec<T>> {
657657
fn from(v: &'a Vec<T>) -> Self { Self(v) }
658658
}
659659

660+
impl Writeable for WithoutLength<&Script> {
661+
#[inline]
662+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
663+
writer.write_all(self.0.as_bytes())
664+
}
665+
}
666+
667+
impl Readable for WithoutLength<Script> {
668+
#[inline]
669+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
670+
let v: WithoutLength<Vec<u8>> = Readable::read(r)?;
671+
Ok(WithoutLength(script::Builder::from(v.0).into_script()))
672+
}
673+
}
674+
660675
#[derive(Debug)]
661676
pub(crate) struct Iterable<'a, I: Iterator<Item = &'a T> + Clone, T: 'a>(pub I);
662677

lightning/src/util/ser_macros.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ macro_rules! _encode_tlv {
6060
// Just a read-mapped type
6161
$crate::_encode_tlv!($stream, $type, $field, option);
6262
};
63+
($stream: expr, $type: expr, $field: expr, option_without_length) => {
64+
$crate::_encode_tlv!($stream, $type, $field.as_ref().map(|x| $crate::util::ser::WithoutLength(x)), option);
65+
};
6366
}
6467

6568
/// Panics if the last seen TLV type is not numerically less than the TLV type currently being checked.
@@ -233,6 +236,9 @@ macro_rules! _check_decoded_tlv_order {
233236
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
234237
// no-op
235238
}};
239+
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option_without_length) => {{
240+
// no-op
241+
}};
236242
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{
237243
// no-op
238244
}};
@@ -284,6 +290,9 @@ macro_rules! _check_missing_tlv {
284290
($last_seen_type: expr, $type: expr, $field: ident, option) => {{
285291
// no-op
286292
}};
293+
($last_seen_type: expr, $type: expr, $field: ident, option_without_length) => {{
294+
// no-op
295+
}};
287296
($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{
288297
// no-op
289298
}};
@@ -324,6 +333,10 @@ macro_rules! _decode_tlv {
324333
($reader: expr, $field: ident, option) => {{
325334
$field = Some($crate::util::ser::Readable::read(&mut $reader)?);
326335
}};
336+
($reader: expr, $field: ident, option_without_length) => {{
337+
let f: $crate::util::ser::WithoutLength<_> = $crate::util::ser::Readable::read(&mut $reader)?;
338+
$field = Some(f.0);
339+
}};
327340
($reader: expr, $field: ident, optional_vec) => {{
328341
$crate::_decode_tlv!($reader, $field, vec_type);
329342
}};
@@ -679,6 +692,9 @@ macro_rules! _init_tlv_based_struct_field {
679692
($field: ident, option) => {
680693
$field
681694
};
695+
($field: ident, option_without_length) => {
696+
WithoutLength($field)
697+
};
682698
($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {
683699
$crate::_init_tlv_based_struct_field!($field, option)
684700
};
@@ -723,6 +739,9 @@ macro_rules! _init_tlv_field_var {
723739
($field: ident, option) => {
724740
let mut $field = None;
725741
};
742+
($field: ident, option_without_length) => {
743+
let mut $field = None;
744+
};
726745
($field: ident, optional_vec) => {
727746
let mut $field = Some(Vec::new());
728747
};

0 commit comments

Comments
 (0)