Skip to content

Commit 43ab6ee

Browse files
committed
Use TLV serialization for Commitment transaction structures
1 parent f39f5c2 commit 43ab6ee

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

lightning/src/chain/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl HolderHTLCOutput {
225225
}
226226

227227
impl_writeable_tlv_based!(HolderHTLCOutput, {
228-
(0, amount, 0),
228+
(0, amount),
229229
}, {
230230
(2, preimage),
231231
}, {});

lightning/src/ln/chan_utils.rs

Lines changed: 51 additions & 50 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,23 +434,11 @@ pub struct HTLCOutputInCommitment {
429434
pub transaction_output_index: Option<u32>,
430435
}
431436

432-
impl HTLCOutputInCommitment {
433-
pub(crate) fn deserialization_dummy() -> Self {
434-
Self {
435-
offered: false,
436-
amount_msat: 0,
437-
cltv_expiry: 0,
438-
payment_hash: PaymentHash([0; 32]),
439-
transaction_output_index: None,
440-
}
441-
}
442-
}
443-
444437
impl_writeable_tlv_based!(HTLCOutputInCommitment, {
445-
(0, offered, false),
446-
(2, amount_msat, 0),
447-
(4, cltv_expiry, 0),
448-
(6, payment_hash, PaymentHash([0; 32])),
438+
(0, offered),
439+
(2, amount_msat),
440+
(4, cltv_expiry),
441+
(6, payment_hash),
449442
}, {
450443
(8, transaction_output_index)
451444
}, {});
@@ -632,18 +625,19 @@ impl ChannelTransactionParameters {
632625
}
633626
}
634627

635-
impl_writeable!(CounterpartyChannelTransactionParameters, 0, {
636-
pubkeys,
637-
selected_contest_delay
638-
});
628+
impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
629+
(0, pubkeys),
630+
(2, selected_contest_delay),
631+
}, {}, {});
639632

640-
impl_writeable!(ChannelTransactionParameters, 0, {
641-
holder_pubkeys,
642-
holder_selected_contest_delay,
643-
is_outbound_from_holder,
644-
counterparty_parameters,
645-
funding_outpoint
646-
});
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+
}, {});
647641

648642
/// Static channel fields used to build transactions given per-commitment fields, organized by
649643
/// broadcaster/countersignatory.
@@ -725,8 +719,12 @@ impl PartialEq for HolderCommitmentTransaction {
725719
}
726720
}
727721

728-
impl_writeable!(HolderCommitmentTransaction, 0, {
729-
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),
730728
});
731729

732730
impl HolderCommitmentTransaction {
@@ -810,7 +808,10 @@ pub struct BuiltCommitmentTransaction {
810808
pub txid: Txid,
811809
}
812810

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

815816
impl BuiltCommitmentTransaction {
816817
/// Get the SIGHASH_ALL sighash value of the transaction.
@@ -893,15 +894,15 @@ impl Readable for Vec<HTLCOutputInCommitment> {
893894
}
894895
}
895896

896-
impl_writeable!(CommitmentTransaction, 0, {
897-
commitment_number,
898-
to_broadcaster_value_sat,
899-
to_countersignatory_value_sat,
900-
feerate_per_kw,
901-
htlcs,
902-
keys,
903-
built
904-
});
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+
}, {}, {});
905906

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

0 commit comments

Comments
 (0)