Skip to content

Commit c8bc1b6

Browse files
committed
Use TLV serialization for Commitment transaction structures
1 parent a749422 commit c8bc1b6

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl Writeable for CounterpartyCommitmentSecrets {
178178
writer.write_all(secret)?;
179179
writer.write_all(&byte_utils::be64_to_array(*idx))?;
180180
}
181+
write_tlv_fields!(writer, {}, {});
181182
Ok(())
182183
}
183184
}
@@ -188,7 +189,7 @@ impl Readable for CounterpartyCommitmentSecrets {
188189
*secret = Readable::read(reader)?;
189190
*idx = Readable::read(reader)?;
190191
}
191-
192+
read_tlv_fields!(reader, {}, {});
192193
Ok(Self { old_secrets })
193194
}
194195
}
@@ -322,8 +323,13 @@ pub struct TxCreationKeys {
322323
pub broadcaster_delayed_payment_key: PublicKey,
323324
}
324325

325-
impl_writeable!(TxCreationKeys, 33*5,
326-
{ per_commitment_point, revocation_key, broadcaster_htlc_key, countersignatory_htlc_key, broadcaster_delayed_payment_key });
326+
impl_writeable_tlv_based!(TxCreationKeys, {
327+
(0, per_commitment_point),
328+
(2, revocation_key),
329+
(4, broadcaster_htlc_key),
330+
(6, countersignatory_htlc_key),
331+
(8, broadcaster_delayed_payment_key),
332+
}, {}, {});
327333

328334
/// One counterparty's public keys which do not change over the life of a channel.
329335
#[derive(Clone, PartialEq)]
@@ -349,14 +355,13 @@ pub struct ChannelPublicKeys {
349355
pub htlc_basepoint: PublicKey,
350356
}
351357

352-
impl_writeable!(ChannelPublicKeys, 33*5, {
353-
funding_pubkey,
354-
revocation_basepoint,
355-
payment_point,
356-
delayed_payment_basepoint,
357-
htlc_basepoint
358-
});
359-
358+
impl_writeable_tlv_based!(ChannelPublicKeys, {
359+
(0, funding_pubkey),
360+
(2, revocation_basepoint),
361+
(4, payment_point),
362+
(6, delayed_payment_basepoint),
363+
(8, htlc_basepoint),
364+
}, {}, {});
360365

361366
impl TxCreationKeys {
362367
/// Create per-state keys from channel base points and the per-commitment point.
@@ -429,16 +434,14 @@ pub struct HTLCOutputInCommitment {
429434
pub transaction_output_index: Option<u32>,
430435
}
431436

432-
impl_writeable_len_match!(HTLCOutputInCommitment, {
433-
{ HTLCOutputInCommitment { transaction_output_index: None, .. }, HTLC_OUTPUT_IN_COMMITMENT_SIZE - 4 },
434-
{ _, HTLC_OUTPUT_IN_COMMITMENT_SIZE }
435-
}, {
436-
offered,
437-
amount_msat,
438-
cltv_expiry,
439-
payment_hash,
440-
transaction_output_index
441-
});
437+
impl_writeable_tlv_based!(HTLCOutputInCommitment, {
438+
(0, offered),
439+
(2, amount_msat),
440+
(4, cltv_expiry),
441+
(6, payment_hash),
442+
}, {
443+
(8, transaction_output_index)
444+
}, {});
442445

443446
#[inline]
444447
pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, broadcaster_htlc_key: &PublicKey, countersignatory_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
@@ -622,18 +625,19 @@ impl ChannelTransactionParameters {
622625
}
623626
}
624627

625-
impl_writeable!(CounterpartyChannelTransactionParameters, 0, {
626-
pubkeys,
627-
selected_contest_delay
628-
});
628+
impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
629+
(0, pubkeys),
630+
(2, selected_contest_delay),
631+
}, {}, {});
629632

630-
impl_writeable!(ChannelTransactionParameters, 0, {
631-
holder_pubkeys,
632-
holder_selected_contest_delay,
633-
is_outbound_from_holder,
634-
counterparty_parameters,
635-
funding_outpoint
636-
});
633+
impl_writeable_tlv_based!(ChannelTransactionParameters, {
634+
(0, holder_pubkeys),
635+
(2, holder_selected_contest_delay),
636+
(4, is_outbound_from_holder),
637+
}, {
638+
(6, counterparty_parameters),
639+
(8, funding_outpoint),
640+
}, {});
637641

638642
/// Static channel fields used to build transactions given per-commitment fields, organized by
639643
/// broadcaster/countersignatory.
@@ -715,8 +719,12 @@ impl PartialEq for HolderCommitmentTransaction {
715719
}
716720
}
717721

718-
impl_writeable!(HolderCommitmentTransaction, 0, {
719-
inner, counterparty_sig, counterparty_htlc_sigs, holder_sig_first
722+
impl_writeable_tlv_based!(HolderCommitmentTransaction, {
723+
(0, inner),
724+
(2, counterparty_sig),
725+
(4, holder_sig_first),
726+
}, {}, {
727+
(6, counterparty_htlc_sigs),
720728
});
721729

722730
impl HolderCommitmentTransaction {
@@ -800,7 +808,10 @@ pub struct BuiltCommitmentTransaction {
800808
pub txid: Txid,
801809
}
802810

803-
impl_writeable!(BuiltCommitmentTransaction, 0, { transaction, txid });
811+
impl_writeable_tlv_based!(BuiltCommitmentTransaction, {
812+
(0, transaction),
813+
(2, txid)
814+
}, {}, {});
804815

805816
impl BuiltCommitmentTransaction {
806817
/// Get the SIGHASH_ALL sighash value of the transaction.
@@ -883,15 +894,15 @@ impl Readable for Vec<HTLCOutputInCommitment> {
883894
}
884895
}
885896

886-
impl_writeable!(CommitmentTransaction, 0, {
887-
commitment_number,
888-
to_broadcaster_value_sat,
889-
to_countersignatory_value_sat,
890-
feerate_per_kw,
891-
htlcs,
892-
keys,
893-
built
894-
});
897+
impl_writeable_tlv_based!(CommitmentTransaction, {
898+
(0, commitment_number),
899+
(2, to_broadcaster_value_sat),
900+
(4, to_countersignatory_value_sat),
901+
(6, feerate_per_kw),
902+
(8, htlcs),
903+
(10, keys),
904+
(12, built),
905+
}, {}, {});
895906

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

0 commit comments

Comments
 (0)