Skip to content

Commit d121653

Browse files
committed
Add channel tlv serialization
1 parent 8979f3f commit d121653

File tree

10 files changed

+207
-51
lines changed

10 files changed

+207
-51
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ pub fn build_closing_transaction(to_holder_value_sat: Amount, to_counterparty_va
400400
///
401401
/// Allows us to keep track of all of the revocation secrets of our counterparty in just 50*32 bytes
402402
/// or so.
403-
#[derive(Clone)]
404-
#[cfg_attr(test, derive(Debug))]
403+
#[derive(Clone, Debug)]
405404
pub struct CounterpartyCommitmentSecrets {
406405
old_secrets: [([u8; 32], u64); 49],
407406
}

lightning/src/ln/channel.rs

Lines changed: 177 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub struct AvailableBalances {
123123
pub next_outbound_htlc_minimum_msat: u64,
124124
}
125125

126-
#[derive(Debug, Clone, Copy, PartialEq)]
126+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127127
enum FeeUpdateState {
128128
// Inbound states mirroring InboundHTLCState
129129
RemoteAnnounced,
@@ -138,16 +138,33 @@ enum FeeUpdateState {
138138
Outbound,
139139
}
140140

141-
#[derive(Debug)]
141+
impl_writeable_tlv_based_enum!(FeeUpdateState,
142+
(0, RemoteAnnounced) => {},
143+
(1, AwaitingRemoteRevokeToAnnounce) => {},
144+
(2, Outbound) => {},
145+
);
146+
147+
#[derive(Debug, Clone, PartialEq, Eq)]
142148
enum InboundHTLCRemovalReason {
143149
FailRelay(msgs::OnionErrorPacket),
144150
FailMalformed { sha256_of_onion: [u8; 32], failure_code: u16 },
145151
Fulfill { preimage: PaymentPreimage, attribution_data: Option<AttributionData> },
146152
}
147153

154+
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,
155+
(1, FailMalformed) => {
156+
(0, sha256_of_onion, required),
157+
(1, failure_code, required),
158+
},
159+
(2, Fulfill) => {
160+
(0, preimage, required),
161+
(1, attribution_data, required),
162+
},
163+
{0, FailRelay} => (),
164+
);
165+
148166
/// Represents the resolution status of an inbound HTLC.
149-
#[cfg_attr(test, derive(Debug))]
150-
#[derive(Clone)]
167+
#[derive(Clone, Debug, PartialEq, Eq)]
151168
enum InboundHTLCResolution {
152169
/// Resolved implies the action we must take with the inbound HTLC has already been determined,
153170
/// i.e., we already know whether it must be failed back or forwarded.
@@ -170,7 +187,7 @@ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
170187
},
171188
);
172189

173-
#[cfg_attr(test, derive(Debug))]
190+
#[derive(Clone, Debug, PartialEq, Eq)]
174191
enum InboundHTLCState {
175192
/// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
176193
/// update_add_htlc message for this HTLC.
@@ -225,6 +242,14 @@ enum InboundHTLCState {
225242
LocalRemoved(InboundHTLCRemovalReason),
226243
}
227244

245+
impl_writeable_tlv_based_enum!(InboundHTLCState,
246+
(3, Committed) => {}, // Strangely this one needs to come first?!?
247+
{0, RemoteAnnounced} => (),
248+
{1, AwaitingRemoteRevokeToAnnounce} => (),
249+
{2, AwaitingAnnouncedRemoteRevoke} => (),
250+
{4, LocalRemoved} => (),
251+
);
252+
228253
impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
229254
fn from(state: &InboundHTLCState) -> Option<InboundHTLCStateDetails> {
230255
match state {
@@ -301,7 +326,7 @@ impl InboundHTLCState {
301326
}
302327
}
303328

304-
#[cfg_attr(test, derive(Debug))]
329+
#[derive(Clone, Debug, PartialEq, Eq)]
305330
struct InboundHTLCOutput {
306331
htlc_id: u64,
307332
amount_msat: u64,
@@ -310,8 +335,15 @@ struct InboundHTLCOutput {
310335
state: InboundHTLCState,
311336
}
312337

313-
#[derive(Debug)]
314-
#[cfg_attr(test, derive(Clone, PartialEq))]
338+
impl_writeable_tlv_based!(InboundHTLCOutput, {
339+
(0, htlc_id, required),
340+
(1, amount_msat, required),
341+
(2, cltv_expiry, required),
342+
(3, payment_hash, required),
343+
(4, state, required),
344+
});
345+
346+
#[derive(Debug, Clone, PartialEq, Eq)]
315347
enum OutboundHTLCState {
316348
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
317349
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -344,6 +376,14 @@ enum OutboundHTLCState {
344376
AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome),
345377
}
346378

379+
impl_writeable_tlv_based_enum!(OutboundHTLCState,
380+
(3, Committed) => {}, // Strangely this one needs to come first?!?
381+
{0, LocalAnnounced} => (),
382+
{1, RemoteRemoved} => (),
383+
{2, AwaitingRemoteRevokeToRemove} => (),
384+
{4, AwaitingRemovedRemoteRevoke} => (),
385+
);
386+
347387
impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
348388
fn from(state: &OutboundHTLCState) -> OutboundHTLCStateDetails {
349389
match state {
@@ -410,8 +450,7 @@ impl OutboundHTLCState {
410450
}
411451
}
412452

413-
#[derive(Clone, Debug)]
414-
#[cfg_attr(test, derive(PartialEq))]
453+
#[derive(Clone, Debug, PartialEq, Eq)]
415454
enum OutboundHTLCOutcome {
416455
/// We started always filling in the preimages here in 0.0.105, and the requirement
417456
/// that the preimages always be filled in was added in 0.2.
@@ -422,6 +461,14 @@ enum OutboundHTLCOutcome {
422461
Failure(HTLCFailReason),
423462
}
424463

464+
impl_writeable_tlv_based_enum!(OutboundHTLCOutcome,
465+
(0, Success) => {
466+
(0, preimage, required),
467+
(1, attribution_data, required),
468+
},
469+
{1, Failure} => (),
470+
);
471+
425472
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
426473
fn into(self) -> Option<&'a HTLCFailReason> {
427474
match self {
@@ -431,8 +478,7 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
431478
}
432479
}
433480

434-
#[derive(Debug)]
435-
#[cfg_attr(test, derive(Clone, PartialEq))]
481+
#[derive(Debug, Clone, PartialEq, Eq)]
436482
struct OutboundHTLCOutput {
437483
htlc_id: u64,
438484
amount_msat: u64,
@@ -446,9 +492,21 @@ struct OutboundHTLCOutput {
446492
hold_htlc: Option<()>,
447493
}
448494

495+
impl_writeable_tlv_based!(OutboundHTLCOutput, {
496+
(0, htlc_id, required),
497+
(1, amount_msat, required),
498+
(2, cltv_expiry, required),
499+
(3, payment_hash, required),
500+
(4, state, required),
501+
(5, source, required),
502+
(6, blinding_point, required),
503+
(7, skimmed_fee_msat, required),
504+
(8, send_timestamp, required),
505+
(9, hold_htlc, required),
506+
});
507+
449508
/// See AwaitingRemoteRevoke ChannelState for more info
450-
#[derive(Debug)]
451-
#[cfg_attr(test, derive(Clone, PartialEq))]
509+
#[derive(Debug, Clone, PartialEq, Eq)]
452510
enum HTLCUpdateAwaitingACK {
453511
AddHTLC {
454512
// TODO: Time out if we're getting close to cltv_expiry
@@ -479,6 +537,33 @@ enum HTLCUpdateAwaitingACK {
479537
},
480538
}
481539

540+
impl_writeable_tlv_based_enum!(HTLCUpdateAwaitingACK,
541+
(0, AddHTLC) => {
542+
(0, amount_msat, required),
543+
(1, cltv_expiry, required),
544+
(2, payment_hash, required),
545+
(3, source, required),
546+
(4, onion_routing_packet, required),
547+
(5, skimmed_fee_msat, required),
548+
(6, blinding_point, required),
549+
(7, hold_htlc, required),
550+
},
551+
(1, ClaimHTLC) => {
552+
(0, payment_preimage, required),
553+
(1, attribution_data, required),
554+
(2, htlc_id, required),
555+
},
556+
(2, FailHTLC) => {
557+
(0, htlc_id, required),
558+
(1, err_packet, required),
559+
},
560+
(3, FailMalformedHTLC) => {
561+
(0, htlc_id, required),
562+
(1, failure_code, required),
563+
(2, sha256_of_onion, required),
564+
}
565+
);
566+
482567
macro_rules! define_state_flags {
483568
($flag_type_doc: expr, $flag_type: ident, [$(($flag_doc: expr, $flag: ident, $value: expr, $get: ident, $set: ident, $clear: ident)),*], $extra_flags: expr) => {
484569
#[doc = $flag_type_doc]
@@ -718,6 +803,19 @@ enum ChannelState {
718803
ShutdownComplete,
719804
}
720805

806+
impl Writeable for ChannelState {
807+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
808+
self.to_u32().write(w)
809+
}
810+
}
811+
812+
impl Readable for ChannelState {
813+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
814+
let state_u32 = u32::read(r)?;
815+
ChannelState::from_u32(state_u32).map_err(|_| DecodeError::InvalidValue)
816+
}
817+
}
818+
721819
macro_rules! impl_state_flag {
722820
($get: ident, $set: ident, $clear: ident, [$($state: ident),+]) => {
723821
#[allow(unused)]
@@ -1031,7 +1129,7 @@ macro_rules! secp_check {
10311129
/// spamming the network with updates if the connection is flapping. Instead, we "stage" updates to
10321130
/// our channel_update message and track the current state here.
10331131
/// See implementation at [`super::channelmanager::ChannelManager::timer_tick_occurred`].
1034-
#[derive(Clone, Copy, PartialEq, Debug)]
1132+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10351133
pub(super) enum ChannelUpdateStatus {
10361134
/// We've announced the channel as enabled and are connected to our peer.
10371135
Enabled,
@@ -1044,8 +1142,7 @@ pub(super) enum ChannelUpdateStatus {
10441142
}
10451143

10461144
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
1047-
#[cfg_attr(test, derive(Debug))]
1048-
#[derive(PartialEq)]
1145+
#[derive(PartialEq, Clone, Debug, Eq)]
10491146
pub enum AnnouncementSigsState {
10501147
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
10511148
/// we sent the last `AnnouncementSignatures`.
@@ -1225,7 +1322,7 @@ pub(crate) struct DisconnectResult {
12251322
/// Tracks the transaction number, along with current and next commitment points.
12261323
/// This consolidates the logic to advance our commitment number and request new
12271324
/// commitment points from our signer.
1228-
#[derive(Debug, Copy, Clone)]
1325+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
12291326
struct HolderCommitmentPoint {
12301327
next_transaction_number: u64,
12311328
current_point: Option<PublicKey>,
@@ -1240,6 +1337,15 @@ struct HolderCommitmentPoint {
12401337
last_revoked_point: Option<PublicKey>,
12411338
}
12421339

1340+
impl_writeable_tlv_based!(HolderCommitmentPoint, {
1341+
(0, next_transaction_number, required),
1342+
(1, current_point, required),
1343+
(2, next_point, required),
1344+
(3, pending_next_point, required),
1345+
(4, previous_revoked_point, required),
1346+
(5, last_revoked_point, required),
1347+
});
1348+
12431349
impl HolderCommitmentPoint {
12441350
#[rustfmt::skip]
12451351
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
@@ -1433,7 +1539,7 @@ pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 14 * 24 * 6 * 4;
14331539
#[cfg(test)]
14341540
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 144;
14351541

1436-
#[derive(Debug)]
1542+
#[derive(Debug, Clone, PartialEq, Eq)]
14371543
struct PendingChannelMonitorUpdate {
14381544
update: ChannelMonitorUpdate,
14391545
}
@@ -2384,6 +2490,53 @@ pub(super) struct FundingScope {
23842490
minimum_depth_override: Option<u32>,
23852491
}
23862492

2493+
impl Eq for FundingScope {}
2494+
2495+
impl PartialEq for FundingScope {
2496+
fn eq(&self, other: &Self) -> bool {
2497+
self.value_to_self_msat == other.value_to_self_msat
2498+
&& self.counterparty_selected_channel_reserve_satoshis
2499+
== other.counterparty_selected_channel_reserve_satoshis
2500+
&& self.holder_selected_channel_reserve_satoshis
2501+
== other.holder_selected_channel_reserve_satoshis
2502+
&& self.channel_transaction_parameters == other.channel_transaction_parameters
2503+
&& self.funding_transaction == other.funding_transaction
2504+
&& self.funding_tx_confirmed_in == other.funding_tx_confirmed_in
2505+
&& self.funding_tx_confirmation_height == other.funding_tx_confirmation_height
2506+
&& self.short_channel_id == other.short_channel_id
2507+
&& self.minimum_depth_override == other.minimum_depth_override
2508+
}
2509+
}
2510+
2511+
impl Clone for FundingScope {
2512+
fn clone(&self) -> Self {
2513+
FundingScope {
2514+
value_to_self_msat: self.value_to_self_msat,
2515+
counterparty_selected_channel_reserve_satoshis: self
2516+
.counterparty_selected_channel_reserve_satoshis,
2517+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
2518+
#[cfg(debug_assertions)]
2519+
holder_max_commitment_tx_output: Mutex::new(
2520+
*self.holder_max_commitment_tx_output.lock().unwrap(),
2521+
),
2522+
#[cfg(debug_assertions)]
2523+
counterparty_max_commitment_tx_output: Mutex::new(
2524+
*self.counterparty_max_commitment_tx_output.lock().unwrap(),
2525+
),
2526+
#[cfg(any(test, fuzzing))]
2527+
next_local_fee: Mutex::new(*self.next_local_fee.lock().unwrap()),
2528+
#[cfg(any(test, fuzzing))]
2529+
next_remote_fee: Mutex::new(*self.next_remote_fee.lock().unwrap()),
2530+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
2531+
funding_transaction: self.funding_transaction.clone(),
2532+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
2533+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
2534+
short_channel_id: self.short_channel_id,
2535+
minimum_depth_override: self.minimum_depth_override,
2536+
}
2537+
}
2538+
}
2539+
23872540
impl Writeable for FundingScope {
23882541
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
23892542
write_tlv_fields!(writer, {
@@ -2670,7 +2823,7 @@ impl FundingScope {
26702823
/// Information about pending attempts at funding a channel. This includes funding currently under
26712824
/// negotiation and any negotiated attempts waiting enough on-chain confirmations. More than one
26722825
/// such attempt indicates use of RBF to increase the chances of confirmation.
2673-
#[derive(Debug)]
2826+
#[derive(Debug, Clone, Eq, PartialEq)]
26742827
struct PendingFunding {
26752828
funding_negotiation: Option<FundingNegotiation>,
26762829

@@ -2692,7 +2845,7 @@ impl_writeable_tlv_based!(PendingFunding, {
26922845
(7, received_funding_txid, option),
26932846
});
26942847

2695-
#[derive(Debug)]
2848+
#[derive(Debug, Clone, PartialEq, Eq)]
26962849
enum FundingNegotiation {
26972850
AwaitingAck {
26982851
context: FundingNegotiationContext,
@@ -2772,7 +2925,7 @@ impl PendingFunding {
27722925
}
27732926
}
27742927

2775-
#[derive(Debug)]
2928+
#[derive(Debug, Clone, PartialEq, Eq)]
27762929
pub(crate) struct SpliceInstructions {
27772930
adjusted_funding_contribution: SignedAmount,
27782931
our_funding_inputs: Vec<FundingTxInput>,
@@ -2800,7 +2953,7 @@ impl_writeable_tlv_based!(SpliceInstructions, {
28002953
(11, locktime, required),
28012954
});
28022955

2803-
#[derive(Debug)]
2956+
#[derive(Debug, Clone, PartialEq, Eq)]
28042957
pub(crate) enum QuiescentAction {
28052958
Splice(SpliceInstructions),
28062959
#[cfg(any(test, fuzzing))]
@@ -6617,7 +6770,7 @@ fn check_v2_funding_inputs_sufficient(
66176770
}
66186771

66196772
/// Context for negotiating channels (dual-funded V2 open, splicing)
6620-
#[derive(Debug)]
6773+
#[derive(Debug, Clone, PartialEq, Eq)]
66216774
pub(super) struct FundingNegotiationContext {
66226775
/// Whether we initiated the funding negotiation.
66236776
pub is_initiator: bool,

lightning/src/ln/channel_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl_writeable_tlv_based!(OutboundHTLCDetails, {
213213
});
214214

215215
/// Information needed for constructing an invoice route hint for this channel.
216-
#[derive(Clone, Debug, PartialEq)]
216+
#[derive(Clone, Debug, PartialEq, Eq)]
217217
pub struct CounterpartyForwardingInfo {
218218
/// Base routing fee in millisatoshis.
219219
pub fee_base_msat: u32,

0 commit comments

Comments
 (0)