Skip to content

Commit da96fec

Browse files
committed
Add channel tlv serialization
1 parent 62c5849 commit da96fec

10 files changed

Lines changed: 209 additions & 51 deletions

File tree

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: 179 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.
@@ -232,6 +249,16 @@ enum InboundHTLCState {
232249
LocalRemoved(InboundHTLCRemovalReason),
233250
}
234251

252+
impl_writeable_tlv_based_enum!(InboundHTLCState,
253+
(3, Committed) => {
254+
(0, update_add_htlc_opt, required),
255+
}, // Strangely this one needs to come first?!?
256+
{0, RemoteAnnounced} => (),
257+
{1, AwaitingRemoteRevokeToAnnounce} => (),
258+
{2, AwaitingAnnouncedRemoteRevoke} => (),
259+
{4, LocalRemoved} => (),
260+
);
261+
235262
impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
236263
fn from(state: &InboundHTLCState) -> Option<InboundHTLCStateDetails> {
237264
match state {
@@ -308,7 +335,7 @@ impl InboundHTLCState {
308335
}
309336
}
310337

311-
#[cfg_attr(test, derive(Debug))]
338+
#[derive(Clone, Debug, PartialEq, Eq)]
312339
struct InboundHTLCOutput {
313340
htlc_id: u64,
314341
amount_msat: u64,
@@ -317,8 +344,15 @@ struct InboundHTLCOutput {
317344
state: InboundHTLCState,
318345
}
319346

320-
#[derive(Debug)]
321-
#[cfg_attr(test, derive(Clone, PartialEq))]
347+
impl_writeable_tlv_based!(InboundHTLCOutput, {
348+
(0, htlc_id, required),
349+
(1, amount_msat, required),
350+
(2, cltv_expiry, required),
351+
(3, payment_hash, required),
352+
(4, state, required),
353+
});
354+
355+
#[derive(Debug, Clone, PartialEq, Eq)]
322356
enum OutboundHTLCState {
323357
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
324358
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -351,6 +385,14 @@ enum OutboundHTLCState {
351385
AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome),
352386
}
353387

388+
impl_writeable_tlv_based_enum!(OutboundHTLCState,
389+
(3, Committed) => {}, // Strangely this one needs to come first?!?
390+
{0, LocalAnnounced} => (),
391+
{1, RemoteRemoved} => (),
392+
{2, AwaitingRemoteRevokeToRemove} => (),
393+
{4, AwaitingRemovedRemoteRevoke} => (),
394+
);
395+
354396
impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
355397
fn from(state: &OutboundHTLCState) -> OutboundHTLCStateDetails {
356398
match state {
@@ -417,8 +459,7 @@ impl OutboundHTLCState {
417459
}
418460
}
419461

420-
#[derive(Clone, Debug)]
421-
#[cfg_attr(test, derive(PartialEq))]
462+
#[derive(Clone, Debug, PartialEq, Eq)]
422463
enum OutboundHTLCOutcome {
423464
/// We started always filling in the preimages here in 0.0.105, and the requirement
424465
/// that the preimages always be filled in was added in 0.2.
@@ -429,6 +470,14 @@ enum OutboundHTLCOutcome {
429470
Failure(HTLCFailReason),
430471
}
431472

473+
impl_writeable_tlv_based_enum!(OutboundHTLCOutcome,
474+
(0, Success) => {
475+
(0, preimage, required),
476+
(1, attribution_data, required),
477+
},
478+
{1, Failure} => (),
479+
);
480+
432481
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
433482
fn into(self) -> Option<&'a HTLCFailReason> {
434483
match self {
@@ -438,8 +487,7 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
438487
}
439488
}
440489

441-
#[derive(Debug)]
442-
#[cfg_attr(test, derive(Clone, PartialEq))]
490+
#[derive(Debug, Clone, PartialEq, Eq)]
443491
struct OutboundHTLCOutput {
444492
htlc_id: u64,
445493
amount_msat: u64,
@@ -453,9 +501,21 @@ struct OutboundHTLCOutput {
453501
hold_htlc: Option<()>,
454502
}
455503

504+
impl_writeable_tlv_based!(OutboundHTLCOutput, {
505+
(0, htlc_id, required),
506+
(1, amount_msat, required),
507+
(2, cltv_expiry, required),
508+
(3, payment_hash, required),
509+
(4, state, required),
510+
(5, source, required),
511+
(6, blinding_point, required),
512+
(7, skimmed_fee_msat, required),
513+
(8, send_timestamp, required),
514+
(9, hold_htlc, required),
515+
});
516+
456517
/// See AwaitingRemoteRevoke ChannelState for more info
457-
#[derive(Debug)]
458-
#[cfg_attr(test, derive(Clone, PartialEq))]
518+
#[derive(Debug, Clone, PartialEq, Eq)]
459519
enum HTLCUpdateAwaitingACK {
460520
AddHTLC {
461521
// TODO: Time out if we're getting close to cltv_expiry
@@ -486,6 +546,33 @@ enum HTLCUpdateAwaitingACK {
486546
},
487547
}
488548

549+
impl_writeable_tlv_based_enum!(HTLCUpdateAwaitingACK,
550+
(0, AddHTLC) => {
551+
(0, amount_msat, required),
552+
(1, cltv_expiry, required),
553+
(2, payment_hash, required),
554+
(3, source, required),
555+
(4, onion_routing_packet, required),
556+
(5, skimmed_fee_msat, required),
557+
(6, blinding_point, required),
558+
(7, hold_htlc, required),
559+
},
560+
(1, ClaimHTLC) => {
561+
(0, payment_preimage, required),
562+
(1, attribution_data, required),
563+
(2, htlc_id, required),
564+
},
565+
(2, FailHTLC) => {
566+
(0, htlc_id, required),
567+
(1, err_packet, required),
568+
},
569+
(3, FailMalformedHTLC) => {
570+
(0, htlc_id, required),
571+
(1, failure_code, required),
572+
(2, sha256_of_onion, required),
573+
}
574+
);
575+
489576
macro_rules! define_state_flags {
490577
($flag_type_doc: expr, $flag_type: ident, [$(($flag_doc: expr, $flag: ident, $value: expr, $get: ident, $set: ident, $clear: ident)),*], $extra_flags: expr) => {
491578
#[doc = $flag_type_doc]
@@ -725,6 +812,19 @@ enum ChannelState {
725812
ShutdownComplete,
726813
}
727814

815+
impl Writeable for ChannelState {
816+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
817+
self.to_u32().write(w)
818+
}
819+
}
820+
821+
impl Readable for ChannelState {
822+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
823+
let state_u32 = u32::read(r)?;
824+
ChannelState::from_u32(state_u32).map_err(|_| DecodeError::InvalidValue)
825+
}
826+
}
827+
728828
macro_rules! impl_state_flag {
729829
($get: ident, $set: ident, $clear: ident, [$($state: ident),+]) => {
730830
#[allow(unused)]
@@ -1038,7 +1138,7 @@ macro_rules! secp_check {
10381138
/// spamming the network with updates if the connection is flapping. Instead, we "stage" updates to
10391139
/// our channel_update message and track the current state here.
10401140
/// See implementation at [`super::channelmanager::ChannelManager::timer_tick_occurred`].
1041-
#[derive(Clone, Copy, PartialEq, Debug)]
1141+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10421142
pub(super) enum ChannelUpdateStatus {
10431143
/// We've announced the channel as enabled and are connected to our peer.
10441144
Enabled,
@@ -1051,8 +1151,7 @@ pub(super) enum ChannelUpdateStatus {
10511151
}
10521152

10531153
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
1054-
#[cfg_attr(test, derive(Debug))]
1055-
#[derive(PartialEq)]
1154+
#[derive(PartialEq, Clone, Debug, Eq)]
10561155
pub enum AnnouncementSigsState {
10571156
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
10581157
/// we sent the last `AnnouncementSignatures`.
@@ -1232,7 +1331,7 @@ pub(crate) struct DisconnectResult {
12321331
/// Tracks the transaction number, along with current and next commitment points.
12331332
/// This consolidates the logic to advance our commitment number and request new
12341333
/// commitment points from our signer.
1235-
#[derive(Debug, Copy, Clone)]
1334+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
12361335
struct HolderCommitmentPoint {
12371336
next_transaction_number: u64,
12381337
current_point: Option<PublicKey>,
@@ -1247,6 +1346,15 @@ struct HolderCommitmentPoint {
12471346
last_revoked_point: Option<PublicKey>,
12481347
}
12491348

1349+
impl_writeable_tlv_based!(HolderCommitmentPoint, {
1350+
(0, next_transaction_number, required),
1351+
(1, current_point, required),
1352+
(2, next_point, required),
1353+
(3, pending_next_point, required),
1354+
(4, previous_revoked_point, required),
1355+
(5, last_revoked_point, required),
1356+
});
1357+
12501358
impl HolderCommitmentPoint {
12511359
#[rustfmt::skip]
12521360
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
@@ -1440,7 +1548,7 @@ pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 14 * 24 * 6 * 4;
14401548
#[cfg(test)]
14411549
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 144;
14421550

1443-
#[derive(Debug)]
1551+
#[derive(Debug, Clone, PartialEq, Eq)]
14441552
struct PendingChannelMonitorUpdate {
14451553
update: ChannelMonitorUpdate,
14461554
}
@@ -2391,6 +2499,53 @@ pub(super) struct FundingScope {
23912499
minimum_depth_override: Option<u32>,
23922500
}
23932501

2502+
impl Eq for FundingScope {}
2503+
2504+
impl PartialEq for FundingScope {
2505+
fn eq(&self, other: &Self) -> bool {
2506+
self.value_to_self_msat == other.value_to_self_msat
2507+
&& self.counterparty_selected_channel_reserve_satoshis
2508+
== other.counterparty_selected_channel_reserve_satoshis
2509+
&& self.holder_selected_channel_reserve_satoshis
2510+
== other.holder_selected_channel_reserve_satoshis
2511+
&& self.channel_transaction_parameters == other.channel_transaction_parameters
2512+
&& self.funding_transaction == other.funding_transaction
2513+
&& self.funding_tx_confirmed_in == other.funding_tx_confirmed_in
2514+
&& self.funding_tx_confirmation_height == other.funding_tx_confirmation_height
2515+
&& self.short_channel_id == other.short_channel_id
2516+
&& self.minimum_depth_override == other.minimum_depth_override
2517+
}
2518+
}
2519+
2520+
impl Clone for FundingScope {
2521+
fn clone(&self) -> Self {
2522+
FundingScope {
2523+
value_to_self_msat: self.value_to_self_msat,
2524+
counterparty_selected_channel_reserve_satoshis: self
2525+
.counterparty_selected_channel_reserve_satoshis,
2526+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
2527+
#[cfg(debug_assertions)]
2528+
holder_max_commitment_tx_output: Mutex::new(
2529+
*self.holder_max_commitment_tx_output.lock().unwrap(),
2530+
),
2531+
#[cfg(debug_assertions)]
2532+
counterparty_max_commitment_tx_output: Mutex::new(
2533+
*self.counterparty_max_commitment_tx_output.lock().unwrap(),
2534+
),
2535+
#[cfg(any(test, fuzzing))]
2536+
next_local_fee: Mutex::new(*self.next_local_fee.lock().unwrap()),
2537+
#[cfg(any(test, fuzzing))]
2538+
next_remote_fee: Mutex::new(*self.next_remote_fee.lock().unwrap()),
2539+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
2540+
funding_transaction: self.funding_transaction.clone(),
2541+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
2542+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
2543+
short_channel_id: self.short_channel_id,
2544+
minimum_depth_override: self.minimum_depth_override,
2545+
}
2546+
}
2547+
}
2548+
23942549
impl Writeable for FundingScope {
23952550
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
23962551
write_tlv_fields!(writer, {
@@ -2677,7 +2832,7 @@ impl FundingScope {
26772832
/// Information about pending attempts at funding a channel. This includes funding currently under
26782833
/// negotiation and any negotiated attempts waiting enough on-chain confirmations. More than one
26792834
/// such attempt indicates use of RBF to increase the chances of confirmation.
2680-
#[derive(Debug)]
2835+
#[derive(Debug, Clone, Eq, PartialEq)]
26812836
struct PendingFunding {
26822837
funding_negotiation: Option<FundingNegotiation>,
26832838

@@ -2699,7 +2854,7 @@ impl_writeable_tlv_based!(PendingFunding, {
26992854
(7, received_funding_txid, option),
27002855
});
27012856

2702-
#[derive(Debug)]
2857+
#[derive(Debug, Clone, PartialEq, Eq)]
27032858
enum FundingNegotiation {
27042859
AwaitingAck {
27052860
context: FundingNegotiationContext,
@@ -2779,7 +2934,7 @@ impl PendingFunding {
27792934
}
27802935
}
27812936

2782-
#[derive(Debug)]
2937+
#[derive(Debug, Clone, PartialEq, Eq)]
27832938
pub(crate) struct SpliceInstructions {
27842939
adjusted_funding_contribution: SignedAmount,
27852940
our_funding_inputs: Vec<FundingTxInput>,
@@ -2807,7 +2962,7 @@ impl_writeable_tlv_based!(SpliceInstructions, {
28072962
(11, locktime, required),
28082963
});
28092964

2810-
#[derive(Debug)]
2965+
#[derive(Debug, Clone, PartialEq, Eq)]
28112966
pub(crate) enum QuiescentAction {
28122967
Splice(SpliceInstructions),
28132968
#[cfg(any(test, fuzzing))]
@@ -6624,7 +6779,7 @@ fn check_v2_funding_inputs_sufficient(
66246779
}
66256780

66266781
/// Context for negotiating channels (dual-funded V2 open, splicing)
6627-
#[derive(Debug)]
6782+
#[derive(Debug, Clone, PartialEq, Eq)]
66286783
pub(super) struct FundingNegotiationContext {
66296784
/// Whether we initiated the funding negotiation.
66306785
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)