Skip to content

Commit fa69c27

Browse files
committed
fix Define serialization on Option<TransactionU16LenLimited>
1 parent 51e1e7c commit fa69c27

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lightning/src/ln/msgs.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,11 +2667,11 @@ impl_writeable_msg!(SpliceLocked, {
26672667
impl_writeable_msg!(TxAddInput, {
26682668
channel_id,
26692669
serial_id,
2670+
prevtx,
26702671
prevtx_out,
26712672
sequence,
26722673
}, {
2673-
(0, prevtx, option),
2674-
(1, shared_input_txid, option), // `funding_txid`
2674+
(0, shared_input_txid, option), // `funding_txid`
26752675
});
26762676

26772677
impl_writeable_msg!(TxAddOutput, {
@@ -5230,10 +5230,28 @@ mod tests {
52305230
}).unwrap()),
52315231
prevtx_out: 305419896,
52325232
sequence: 305419896,
5233-
shared_input_txid: Some(Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap()),
5233+
shared_input_txid: None,
52345234
};
52355235
let encoded_value = tx_add_input.encode();
5236-
let target_value = "02020202020202020202020202020202020202020202020202020202020202020000000123456789123456781234567800e000de02000000000101779ced6c148293f86b60cb222108553d22c89207326bb7b6b897e23e64ab5b300200000000fdffffff0236dbc1000000000016001417d29e4dd454bac3b1cde50d1926da80cfc5287b9cbd03000000000016001436ec78d514df462da95e6a00c24daa8915362d420247304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701210301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd69440000000001206e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c2";
5236+
let target_value = "0202020202020202020202020202020202020202020202020202020202020202000000012345678900de02000000000101779ced6c148293f86b60cb222108553d22c89207326bb7b6b897e23e64ab5b300200000000fdffffff0236dbc1000000000016001417d29e4dd454bac3b1cde50d1926da80cfc5287b9cbd03000000000016001436ec78d514df462da95e6a00c24daa8915362d420247304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701210301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944000000001234567812345678";
5237+
assert_eq!(encoded_value.as_hex().to_string(), target_value);
5238+
}
5239+
5240+
#[test]
5241+
fn encoding_tx_add_input_shared() {
5242+
let tx_add_input = msgs::TxAddInput {
5243+
channel_id: ChannelId::from_bytes([2; 32]),
5244+
serial_id: 4886718345,
5245+
prevtx: None,
5246+
prevtx_out: 305419896,
5247+
sequence: 305419896,
5248+
shared_input_txid: Some(
5249+
Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e")
5250+
.unwrap(),
5251+
),
5252+
};
5253+
let encoded_value = tx_add_input.encode();
5254+
let target_value = "020202020202020202020202020202020202020202020202020202020202020200000001234567890000123456781234567800206e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c2";
52375255
assert_eq!(encoded_value.as_hex().to_string(), target_value);
52385256
}
52395257

lightning/src/util/ser.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,22 +1691,30 @@ impl TransactionU16LenLimited {
16911691
}
16921692
}
16931693

1694-
impl Writeable for TransactionU16LenLimited {
1694+
impl Writeable for Option<TransactionU16LenLimited> {
16951695
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1696-
(self.0.serialized_length() as u16).write(w)?;
1697-
self.0.write(w)
1696+
match self {
1697+
Some(tx) => {
1698+
(tx.0.serialized_length() as u16).write(w)?;
1699+
tx.0.write(w)
1700+
},
1701+
None => 0u16.write(w),
1702+
}
16981703
}
16991704
}
17001705

1701-
impl Readable for TransactionU16LenLimited {
1706+
impl Readable for Option<TransactionU16LenLimited> {
17021707
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
17031708
let len = <u16 as Readable>::read(r)?;
1709+
if len == 0 {
1710+
return Ok(None);
1711+
}
17041712
let mut tx_reader = FixedLengthReader::new(r, len as u64);
17051713
let tx: Transaction = Readable::read(&mut tx_reader)?;
17061714
if tx_reader.bytes_remain() {
17071715
Err(DecodeError::BadLengthDescriptor)
17081716
} else {
1709-
Ok(Self(tx))
1717+
Ok(Some(TransactionU16LenLimited(tx)))
17101718
}
17111719
}
17121720
}

0 commit comments

Comments
 (0)