Skip to content

Commit 2eb7db9

Browse files
committed
Convert remaining channel inner structs and enums to TLV-based ser
1 parent 258a238 commit 2eb7db9

File tree

4 files changed

+86
-337
lines changed

4 files changed

+86
-337
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::hash_types::{Txid, PubkeyHash};
2222

2323
use ln::{PaymentHash, PaymentPreimage};
2424
use ln::msgs::DecodeError;
25-
use util::ser::{Readable, Writeable, Writer, MAX_BUF_SIZE};
25+
use util::ser::{Readable, Writeable, Writer};
2626
use util::byte_utils;
2727

2828
use bitcoin::hash_types::WPubkeyHash;
@@ -36,18 +36,11 @@ use core::cmp;
3636
use ln::chan_utils;
3737
use util::transaction_utils::sort_outputs;
3838
use ln::channel::INITIAL_COMMITMENT_NUMBER;
39-
use std::io::Read;
4039
use core::ops::Deref;
4140
use chain;
4241

43-
// Maximum size of a serialized HTLCOutputInCommitment
44-
pub(crate) const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5;
45-
4642
pub(crate) const MAX_HTLCS: u16 = 483;
4743

48-
// This checks that the buffer size is greater than the maximum possible size for serialized HTLCS
49-
const _EXCESS_BUFFER_SIZE: usize = MAX_BUF_SIZE - MAX_HTLCS as usize * HTLC_OUTPUT_IN_COMMITMENT_SIZE;
50-
5144
pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
5245
pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
5346

@@ -866,44 +859,16 @@ impl PartialEq for CommitmentTransaction {
866859
}
867860
}
868861

869-
/// (C-not exported) as users never need to call this directly
870-
impl Writeable for Vec<HTLCOutputInCommitment> {
871-
#[inline]
872-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
873-
(self.len() as u16).write(w)?;
874-
for e in self.iter() {
875-
e.write(w)?;
876-
}
877-
Ok(())
878-
}
879-
}
880-
881-
/// (C-not exported) as users never need to call this directly
882-
impl Readable for Vec<HTLCOutputInCommitment> {
883-
#[inline]
884-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
885-
let len: u16 = Readable::read(r)?;
886-
let byte_size = (len as usize)
887-
.checked_mul(HTLC_OUTPUT_IN_COMMITMENT_SIZE)
888-
.ok_or(DecodeError::BadLengthDescriptor)?;
889-
if byte_size > MAX_BUF_SIZE {
890-
return Err(DecodeError::BadLengthDescriptor);
891-
}
892-
let mut ret = Vec::with_capacity(len as usize);
893-
for _ in 0..len { ret.push(HTLCOutputInCommitment::read(r)?); }
894-
Ok(ret)
895-
}
896-
}
897-
898862
impl_writeable_tlv_based!(CommitmentTransaction, {
899863
(0, commitment_number),
900864
(2, to_broadcaster_value_sat),
901865
(4, to_countersignatory_value_sat),
902866
(6, feerate_per_kw),
903-
(8, htlcs),
904-
(10, keys),
905-
(12, built),
906-
}, {}, {});
867+
(8, keys),
868+
(10, built),
869+
}, {}, {
870+
(12, htlcs),
871+
});
907872

908873
impl CommitmentTransaction {
909874
/// Construct an object of the class while assigning transaction output indices to HTLCs.

lightning/src/ln/channel.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4382,37 +4382,11 @@ fn is_unsupported_shutdown_script(their_features: &InitFeatures, script: &Script
43824382
const SERIALIZATION_VERSION: u8 = 1;
43834383
const MIN_SERIALIZATION_VERSION: u8 = 1;
43844384

4385-
impl Writeable for InboundHTLCRemovalReason {
4386-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
4387-
match self {
4388-
&InboundHTLCRemovalReason::FailRelay(ref error_packet) => {
4389-
0u8.write(writer)?;
4390-
error_packet.write(writer)?;
4391-
},
4392-
&InboundHTLCRemovalReason::FailMalformed((ref onion_hash, ref err_code)) => {
4393-
1u8.write(writer)?;
4394-
onion_hash.write(writer)?;
4395-
err_code.write(writer)?;
4396-
},
4397-
&InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
4398-
2u8.write(writer)?;
4399-
payment_preimage.write(writer)?;
4400-
},
4401-
}
4402-
Ok(())
4403-
}
4404-
}
4405-
4406-
impl Readable for InboundHTLCRemovalReason {
4407-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
4408-
Ok(match <u8 as Readable>::read(reader)? {
4409-
0 => InboundHTLCRemovalReason::FailRelay(Readable::read(reader)?),
4410-
1 => InboundHTLCRemovalReason::FailMalformed((Readable::read(reader)?, Readable::read(reader)?)),
4411-
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
4412-
_ => return Err(DecodeError::InvalidValue),
4413-
})
4414-
}
4415-
}
4385+
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
4386+
(0, FailRelay),
4387+
(1, FailMalformed),
4388+
(2, Fulfill),
4389+
);
44164390

44174391
impl Writeable for ChannelUpdateStatus {
44184392
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {

0 commit comments

Comments
 (0)