Skip to content

Commit df829a8

Browse files
authored
Merge pull request #928 from TheBlueMatt/2021-05-really-tlv-ser
Migrate some inner structs to TLVs
2 parents f30694b + ad20bc7 commit df829a8

File tree

10 files changed

+384
-266
lines changed

10 files changed

+384
-266
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ jobs:
143143
id: cache-graph
144144
uses: actions/cache@v2
145145
with:
146-
path: lightning/net_graph-2021-02-12.bin
147-
key: ldk-net_graph-05f0c5a0d772-2020-02-12.bin
146+
path: lightning/net_graph-2021-05-27.bin
147+
key: ldk-net_graph-45d86ead641d-2021-05-27.bin
148148
- name: Fetch routing graph snapshot
149149
if: steps.cache-graph.outputs.cache-hit != 'true'
150150
run: |
151-
wget -O lightning/net_graph-2021-02-12.bin https://bitcoin.ninja/ldk-net_graph-05f0c5a0d772-2020-02-12.bin
152-
if [ "$(sha256sum lightning/net_graph-2021-02-12.bin | awk '{ print $1 }')" != "7116fca78551fedc714a604cec0ad1ca66caa77bb4d0051290258e7a10e0c6e7" ]; then
151+
wget -O lightning/net_graph-2021-05-27.bin https://bitcoin.ninja/ldk-net_graph-45d86ead641d-2021-05-27.bin
152+
if [ "$(sha256sum lightning/net_graph-2021-05-27.bin | awk '{ print $1 }')" != "3d6261187cfa583255d978efb908b51c2f4dc4ad9a7160cd2c5263c9a4830121" ]; then
153153
echo "Bad hash"
154154
exit 1
155155
fi

lightning/src/chain/package.rs

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use bitcoin::hash_types::Txid;
2121
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
2222

2323
use ln::PaymentPreimage;
24-
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, HTLC_OUTPUT_IN_COMMITMENT_SIZE};
24+
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
2525
use ln::chan_utils;
2626
use ln::msgs::DecodeError;
2727
use chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
@@ -86,15 +86,15 @@ impl RevokedOutput {
8686
}
8787
}
8888

89-
impl_writeable!(RevokedOutput, 33*3 + 32 + 8 + 8 + 2, {
90-
per_commitment_point,
91-
counterparty_delayed_payment_base_key,
92-
counterparty_htlc_base_key,
93-
per_commitment_key,
94-
weight,
95-
amount,
96-
on_counterparty_tx_csv
97-
});
89+
impl_writeable_tlv_based!(RevokedOutput, {
90+
(0, per_commitment_point),
91+
(2, counterparty_delayed_payment_base_key),
92+
(4, counterparty_htlc_base_key),
93+
(6, per_commitment_key),
94+
(8, weight),
95+
(10, amount),
96+
(12, on_counterparty_tx_csv),
97+
}, {}, {});
9898

9999
/// A struct to describe a revoked offered output and corresponding information to generate a
100100
/// solving witness.
@@ -130,15 +130,15 @@ impl RevokedHTLCOutput {
130130
}
131131
}
132132

133-
impl_writeable!(RevokedHTLCOutput, 33*3 + 32 + 8 + 8 + HTLC_OUTPUT_IN_COMMITMENT_SIZE, {
134-
per_commitment_point,
135-
counterparty_delayed_payment_base_key,
136-
counterparty_htlc_base_key,
137-
per_commitment_key,
138-
weight,
139-
amount,
140-
htlc
141-
});
133+
impl_writeable_tlv_based!(RevokedHTLCOutput, {
134+
(0, per_commitment_point),
135+
(2, counterparty_delayed_payment_base_key),
136+
(4, counterparty_htlc_base_key),
137+
(6, per_commitment_key),
138+
(8, weight),
139+
(10, amount),
140+
(12, htlc),
141+
}, {}, {});
142142

143143
/// A struct to describe a HTLC output on a counterparty commitment transaction.
144144
///
@@ -167,13 +167,13 @@ impl CounterpartyOfferedHTLCOutput {
167167
}
168168
}
169169

170-
impl_writeable!(CounterpartyOfferedHTLCOutput, 33*3 + 32 + HTLC_OUTPUT_IN_COMMITMENT_SIZE, {
171-
per_commitment_point,
172-
counterparty_delayed_payment_base_key,
173-
counterparty_htlc_base_key,
174-
preimage,
175-
htlc
176-
});
170+
impl_writeable_tlv_based!(CounterpartyOfferedHTLCOutput, {
171+
(0, per_commitment_point),
172+
(2, counterparty_delayed_payment_base_key),
173+
(4, counterparty_htlc_base_key),
174+
(6, preimage),
175+
(8, htlc),
176+
}, {}, {});
177177

178178
/// A struct to describe a HTLC output on a counterparty commitment transaction.
179179
///
@@ -198,12 +198,12 @@ impl CounterpartyReceivedHTLCOutput {
198198
}
199199
}
200200

201-
impl_writeable!(CounterpartyReceivedHTLCOutput, 33*3 + HTLC_OUTPUT_IN_COMMITMENT_SIZE, {
202-
per_commitment_point,
203-
counterparty_delayed_payment_base_key,
204-
counterparty_htlc_base_key,
205-
htlc
206-
});
201+
impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
202+
(0, per_commitment_point),
203+
(2, counterparty_delayed_payment_base_key),
204+
(4, counterparty_htlc_base_key),
205+
(6, htlc),
206+
}, {}, {});
207207

208208
/// A struct to describe a HTLC output on holder commitment transaction.
209209
///
@@ -224,10 +224,11 @@ impl HolderHTLCOutput {
224224
}
225225
}
226226

227-
impl_writeable!(HolderHTLCOutput, 0, {
228-
preimage,
229-
amount
230-
});
227+
impl_writeable_tlv_based!(HolderHTLCOutput, {
228+
(0, amount),
229+
}, {
230+
(2, preimage),
231+
}, {});
231232

232233
/// A struct to describe the channel output on the funding transaction.
233234
///
@@ -245,9 +246,9 @@ impl HolderFundingOutput {
245246
}
246247
}
247248

248-
impl_writeable!(HolderFundingOutput, 0, {
249-
funding_redeemscript
250-
});
249+
impl_writeable_tlv_based!(HolderFundingOutput, {
250+
(0, funding_redeemscript),
251+
}, {}, {});
251252

252253
/// A wrapper encapsulating all in-protocol differing outputs types.
253254
///
@@ -703,10 +704,11 @@ impl Writeable for PackageTemplate {
703704
outpoint.write(writer)?;
704705
rev_outp.write(writer)?;
705706
}
706-
self.soonest_conf_deadline.write(writer)?;
707-
self.feerate_previous.write(writer)?;
708-
self.height_timer.write(writer)?;
709-
self.height_original.write(writer)?;
707+
write_tlv_fields!(writer, {
708+
(0, self.soonest_conf_deadline),
709+
(2, self.feerate_previous),
710+
(4, self.height_original),
711+
}, { (6, self.height_timer) });
710712
Ok(())
711713
}
712714
}
@@ -730,10 +732,15 @@ impl Readable for PackageTemplate {
730732
PackageSolvingData::HolderFundingOutput(..) => { (PackageMalleability::Untractable, false) },
731733
}
732734
} else { return Err(DecodeError::InvalidValue); };
733-
let soonest_conf_deadline = Readable::read(reader)?;
734-
let feerate_previous = Readable::read(reader)?;
735-
let height_timer = Readable::read(reader)?;
736-
let height_original = Readable::read(reader)?;
735+
let mut soonest_conf_deadline = 0;
736+
let mut feerate_previous = 0;
737+
let mut height_timer = None;
738+
let mut height_original = 0;
739+
read_tlv_fields!(reader, {
740+
(0, soonest_conf_deadline),
741+
(2, feerate_previous),
742+
(4, height_original)
743+
}, { (6, height_timer) });
737744
Ok(PackageTemplate {
738745
inputs,
739746
malleability,

lightning/src/chain/transaction.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ impl OutPoint {
7373
vout: self.index as u32,
7474
}
7575
}
76+
77+
/// Creates a dummy BitcoinOutPoint, useful for deserializing into.
78+
pub(crate) fn null() -> Self {
79+
Self {
80+
txid: Default::default(),
81+
index: 0
82+
}
83+
}
7684
}
7785

7886
impl_writeable!(OutPoint, 0, { txid, index });

lightning/src/ln/chan_utils.rs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl Writeable for CounterpartyCommitmentSecrets {
179179
writer.write_all(secret)?;
180180
writer.write_all(&byte_utils::be64_to_array(*idx))?;
181181
}
182+
write_tlv_fields!(writer, {}, {});
182183
Ok(())
183184
}
184185
}
@@ -189,7 +190,7 @@ impl Readable for CounterpartyCommitmentSecrets {
189190
*secret = Readable::read(reader)?;
190191
*idx = Readable::read(reader)?;
191192
}
192-
193+
read_tlv_fields!(reader, {}, {});
193194
Ok(Self { old_secrets })
194195
}
195196
}
@@ -323,8 +324,13 @@ pub struct TxCreationKeys {
323324
pub broadcaster_delayed_payment_key: PublicKey,
324325
}
325326

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

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

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

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

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

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

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

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

639643
/// Static channel fields used to build transactions given per-commitment fields, organized by
640644
/// broadcaster/countersignatory.
@@ -716,8 +720,12 @@ impl PartialEq for HolderCommitmentTransaction {
716720
}
717721
}
718722

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

723731
impl HolderCommitmentTransaction {
@@ -801,7 +809,10 @@ pub struct BuiltCommitmentTransaction {
801809
pub txid: Txid,
802810
}
803811

804-
impl_writeable!(BuiltCommitmentTransaction, 0, { transaction, txid });
812+
impl_writeable_tlv_based!(BuiltCommitmentTransaction, {
813+
(0, transaction),
814+
(2, txid)
815+
}, {}, {});
805816

806817
impl BuiltCommitmentTransaction {
807818
/// Get the SIGHASH_ALL sighash value of the transaction.
@@ -884,15 +895,15 @@ impl Readable for Vec<HTLCOutputInCommitment> {
884895
}
885896
}
886897

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

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

0 commit comments

Comments
 (0)