Skip to content

Commit b9aa71f

Browse files
authored
Merge pull request #964 from valentinewallace/2021-06-tlv-ser
Fix TLV serialization to work with large types
2 parents bfd9646 + 40959b7 commit b9aa71f

File tree

12 files changed

+462
-497
lines changed

12 files changed

+462
-497
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Writeable for ChannelMonitorUpdate {
9595
for update_step in self.updates.iter() {
9696
update_step.write(w)?;
9797
}
98-
write_tlv_fields!(w, {}, {});
98+
write_tlv_fields!(w, {});
9999
Ok(())
100100
}
101101
}
@@ -108,7 +108,7 @@ impl Readable for ChannelMonitorUpdate {
108108
for _ in 0..len {
109109
updates.push(Readable::read(r)?);
110110
}
111-
read_tlv_fields!(r, {}, {});
111+
read_tlv_fields!(r, {});
112112
Ok(Self { update_id, updates })
113113
}
114114
}
@@ -202,11 +202,10 @@ pub struct HTLCUpdate {
202202
pub(crate) source: HTLCSource
203203
}
204204
impl_writeable_tlv_based!(HTLCUpdate, {
205-
(0, payment_hash),
206-
(2, source),
207-
}, {
208-
(4, payment_preimage)
209-
}, {});
205+
(0, payment_hash, required),
206+
(2, source, required),
207+
(4, payment_preimage, option),
208+
});
210209

211210
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
212211
/// instead claiming it in its own individual transaction.
@@ -273,15 +272,14 @@ struct HolderSignedTx {
273272
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
274273
}
275274
impl_writeable_tlv_based!(HolderSignedTx, {
276-
(0, txid),
277-
(2, revocation_key),
278-
(4, a_htlc_key),
279-
(6, b_htlc_key),
280-
(8, delayed_payment_key),
281-
(10, per_commitment_point),
282-
(12, feerate_per_kw),
283-
}, {}, {
284-
(14, htlc_outputs)
275+
(0, txid, required),
276+
(2, revocation_key, required),
277+
(4, a_htlc_key, required),
278+
(6, b_htlc_key, required),
279+
(8, delayed_payment_key, required),
280+
(10, per_commitment_point, required),
281+
(12, feerate_per_kw, required),
282+
(14, htlc_outputs, vec_type)
285283
});
286284

287285
/// We use this to track counterparty commitment transactions and htlcs outputs and
@@ -305,10 +303,10 @@ impl Writeable for CounterpartyCommitmentTransaction {
305303
}
306304
}
307305
write_tlv_fields!(w, {
308-
(0, self.counterparty_delayed_payment_base_key),
309-
(2, self.counterparty_htlc_base_key),
310-
(4, self.on_counterparty_tx_csv),
311-
}, {});
306+
(0, self.counterparty_delayed_payment_base_key, required),
307+
(2, self.counterparty_htlc_base_key, required),
308+
(4, self.on_counterparty_tx_csv, required),
309+
});
312310
Ok(())
313311
}
314312
}
@@ -333,10 +331,10 @@ impl Readable for CounterpartyCommitmentTransaction {
333331
let mut counterparty_htlc_base_key = OptionDeserWrapper(None);
334332
let mut on_counterparty_tx_csv: u16 = 0;
335333
read_tlv_fields!(r, {
336-
(0, counterparty_delayed_payment_base_key),
337-
(2, counterparty_htlc_base_key),
338-
(4, on_counterparty_tx_csv),
339-
}, {});
334+
(0, counterparty_delayed_payment_base_key, required),
335+
(2, counterparty_htlc_base_key, required),
336+
(4, on_counterparty_tx_csv, required),
337+
});
340338
CounterpartyCommitmentTransaction {
341339
counterparty_delayed_payment_base_key: counterparty_delayed_payment_base_key.0.unwrap(),
342340
counterparty_htlc_base_key: counterparty_htlc_base_key.0.unwrap(),
@@ -394,19 +392,19 @@ enum OnchainEvent {
394392
}
395393

396394
impl_writeable_tlv_based!(OnchainEventEntry, {
397-
(0, txid),
398-
(2, height),
399-
(4, event),
400-
}, {}, {});
395+
(0, txid, required),
396+
(2, height, required),
397+
(4, event, required),
398+
});
401399

402400
impl_writeable_tlv_based_enum!(OnchainEvent,
403401
(0, HTLCUpdate) => {
404-
(0, source),
405-
(2, payment_hash),
406-
}, {}, {},
402+
(0, source, required),
403+
(2, payment_hash, required),
404+
},
407405
(1, MaturingOutput) => {
408-
(0, descriptor),
409-
}, {}, {},
406+
(0, descriptor, required),
407+
},
410408
;);
411409

412410
#[cfg_attr(any(test, feature = "fuzztarget", feature = "_test_utils"), derive(PartialEq))]
@@ -440,27 +438,25 @@ pub(crate) enum ChannelMonitorUpdateStep {
440438

441439
impl_writeable_tlv_based_enum!(ChannelMonitorUpdateStep,
442440
(0, LatestHolderCommitmentTXInfo) => {
443-
(0, commitment_tx),
444-
}, {}, {
445-
(2, htlc_outputs),
441+
(0, commitment_tx, required),
442+
(2, htlc_outputs, vec_type),
446443
},
447444
(1, LatestCounterpartyCommitmentTXInfo) => {
448-
(0, commitment_txid),
449-
(2, commitment_number),
450-
(4, their_revocation_point),
451-
}, {}, {
452-
(6, htlc_outputs),
445+
(0, commitment_txid, required),
446+
(2, commitment_number, required),
447+
(4, their_revocation_point, required),
448+
(6, htlc_outputs, vec_type),
453449
},
454450
(2, PaymentPreimage) => {
455-
(0, payment_preimage),
456-
}, {}, {},
451+
(0, payment_preimage, required),
452+
},
457453
(3, CommitmentSecret) => {
458-
(0, idx),
459-
(2, secret),
460-
}, {}, {},
454+
(0, idx, required),
455+
(2, secret, required),
456+
},
461457
(4, ChannelForceClosed) => {
462-
(0, should_broadcast),
463-
}, {}, {},
458+
(0, should_broadcast, required),
459+
},
464460
;);
465461

466462
/// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
@@ -792,7 +788,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
792788
self.lockdown_from_offchain.write(writer)?;
793789
self.holder_tx_signed.write(writer)?;
794790

795-
write_tlv_fields!(writer, {}, {});
791+
write_tlv_fields!(writer, {});
796792

797793
Ok(())
798794
}
@@ -2740,7 +2736,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
27402736
let lockdown_from_offchain = Readable::read(reader)?;
27412737
let holder_tx_signed = Readable::read(reader)?;
27422738

2743-
read_tlv_fields!(reader, {}, {});
2739+
read_tlv_fields!(reader, {});
27442740

27452741
let mut secp_ctx = Secp256k1::new();
27462742
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());

lightning/src/chain/keysinterface.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ impl DelayedPaymentOutputDescriptor {
7373
}
7474

7575
impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
76-
(0, outpoint),
77-
(2, per_commitment_point),
78-
(4, to_self_delay),
79-
(6, output),
80-
(8, revocation_pubkey),
81-
(10, channel_keys_id),
82-
(12, channel_value_satoshis),
83-
}, {}, {});
76+
(0, outpoint, required),
77+
(2, per_commitment_point, required),
78+
(4, to_self_delay, required),
79+
(6, output, required),
80+
(8, revocation_pubkey, required),
81+
(10, channel_keys_id, required),
82+
(12, channel_value_satoshis, required),
83+
});
8484

8585
/// Information about a spendable output to our "payment key". See
8686
/// SpendableOutputDescriptor::StaticPaymentOutput for more details on how to spend this.
@@ -104,11 +104,11 @@ impl StaticPaymentOutputDescriptor {
104104
pub const MAX_WITNESS_LENGTH: usize = 1 + 73 + 34;
105105
}
106106
impl_writeable_tlv_based!(StaticPaymentOutputDescriptor, {
107-
(0, outpoint),
108-
(2, output),
109-
(4, channel_keys_id),
110-
(6, channel_value_satoshis),
111-
}, {}, {});
107+
(0, outpoint, required),
108+
(2, output, required),
109+
(4, channel_keys_id, required),
110+
(6, channel_value_satoshis, required),
111+
});
112112

113113
/// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
114114
/// claim at any point in the future) an event is generated which you must track and be able to
@@ -169,9 +169,9 @@ pub enum SpendableOutputDescriptor {
169169

170170
impl_writeable_tlv_based_enum!(SpendableOutputDescriptor,
171171
(0, StaticOutput) => {
172-
(0, outpoint),
173-
(2, output),
174-
}, {}, {},
172+
(0, outpoint, required),
173+
(2, output, required),
174+
},
175175
;
176176
(1, DelayedPaymentOutput),
177177
(2, StaticPaymentOutput),
@@ -692,7 +692,7 @@ impl Writeable for InMemorySigner {
692692
self.channel_value_satoshis.write(writer)?;
693693
self.channel_keys_id.write(writer)?;
694694

695-
write_tlv_fields!(writer, {}, {});
695+
write_tlv_fields!(writer, {});
696696

697697
Ok(())
698698
}
@@ -717,7 +717,7 @@ impl Readable for InMemorySigner {
717717
&htlc_base_key);
718718
let keys_id = Readable::read(reader)?;
719719

720-
read_tlv_fields!(reader, {}, {});
720+
read_tlv_fields!(reader, {});
721721

722722
Ok(InMemorySigner {
723723
funding_key,

lightning/src/chain/onchaintx.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ enum OnchainEvent {
7979
}
8080

8181
impl_writeable_tlv_based!(OnchainEventEntry, {
82-
(0, txid),
83-
(2, height),
84-
(4, event),
85-
}, {}, {});
82+
(0, txid, required),
83+
(2, height, required),
84+
(4, event, required),
85+
});
8686

8787
impl_writeable_tlv_based_enum!(OnchainEvent,
8888
(0, Claim) => {
89-
(0, claim_request),
90-
}, {}, {},
89+
(0, claim_request, required),
90+
},
9191
(1, ContentiousOutpoint) => {
92-
(0, package),
93-
}, {}, {},
92+
(0, package, required),
93+
},
9494
;);
9595

9696
impl Readable for Option<Vec<Option<(usize, Signature)>>> {
@@ -236,7 +236,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
236236
entry.write(writer)?;
237237
}
238238

239-
write_tlv_fields!(writer, {}, {});
239+
write_tlv_fields!(writer, {});
240240
Ok(())
241241
}
242242
}
@@ -298,7 +298,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
298298
onchain_events_awaiting_threshold_conf.push(Readable::read(reader)?);
299299
}
300300

301-
read_tlv_fields!(reader, {}, {});
301+
read_tlv_fields!(reader, {});
302302

303303
let mut secp_ctx = Secp256k1::new();
304304
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());

0 commit comments

Comments
 (0)