Skip to content

Commit 7058aee

Browse files
committed
Add channel tlv serialization
1 parent c1bc967 commit 7058aee

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 { hash: [u8; 32], code: u16 },
145151
Fulfill { preimage: PaymentPreimage, attribution_data: Option<AttributionData> },
146152
}
147153

154+
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,
155+
(1, FailMalformed) => {
156+
(0, hash, required),
157+
(1, 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
#[rustfmt::skip]
230255
fn from(state: &InboundHTLCState) -> Option<InboundHTLCStateDetails> {
@@ -298,7 +323,7 @@ impl InboundHTLCState {
298323
}
299324
}
300325

301-
#[cfg_attr(test, derive(Debug))]
326+
#[derive(Clone, Debug, PartialEq, Eq)]
302327
struct InboundHTLCOutput {
303328
htlc_id: u64,
304329
amount_msat: u64,
@@ -307,8 +332,15 @@ struct InboundHTLCOutput {
307332
state: InboundHTLCState,
308333
}
309334

310-
#[derive(Debug)]
311-
#[cfg_attr(test, derive(Clone, PartialEq))]
335+
impl_writeable_tlv_based!(InboundHTLCOutput, {
336+
(0, htlc_id, required),
337+
(1, amount_msat, required),
338+
(2, cltv_expiry, required),
339+
(3, payment_hash, required),
340+
(4, state, required),
341+
});
342+
343+
#[derive(Debug, Clone, PartialEq, Eq)]
312344
enum OutboundHTLCState {
313345
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
314346
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -341,6 +373,14 @@ enum OutboundHTLCState {
341373
AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome),
342374
}
343375

376+
impl_writeable_tlv_based_enum!(OutboundHTLCState,
377+
(3, Committed) => {}, // Strangely this one needs to come first?!?
378+
{0, LocalAnnounced} => (),
379+
{1, RemoteRemoved} => (),
380+
{2, AwaitingRemoteRevokeToRemove} => (),
381+
{4, AwaitingRemovedRemoteRevoke} => (),
382+
);
383+
344384
impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
345385
#[rustfmt::skip]
346386
fn from(state: &OutboundHTLCState) -> OutboundHTLCStateDetails {
@@ -402,8 +442,7 @@ impl OutboundHTLCState {
402442
}
403443
}
404444

405-
#[derive(Clone, Debug)]
406-
#[cfg_attr(test, derive(PartialEq))]
445+
#[derive(Clone, Debug, PartialEq, Eq)]
407446
enum OutboundHTLCOutcome {
408447
/// We started always filling in the preimages here in 0.0.105, and the requirement
409448
/// that the preimages always be filled in was added in 0.2.
@@ -414,6 +453,14 @@ enum OutboundHTLCOutcome {
414453
Failure(HTLCFailReason),
415454
}
416455

456+
impl_writeable_tlv_based_enum!(OutboundHTLCOutcome,
457+
(0, Success) => {
458+
(0, preimage, required),
459+
(1, attribution_data, required),
460+
},
461+
{1, Failure} => (),
462+
);
463+
417464
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
418465
fn into(self) -> Option<&'a HTLCFailReason> {
419466
match self {
@@ -423,8 +470,7 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
423470
}
424471
}
425472

426-
#[derive(Debug)]
427-
#[cfg_attr(test, derive(Clone, PartialEq))]
473+
#[derive(Debug, Clone, PartialEq, Eq)]
428474
struct OutboundHTLCOutput {
429475
htlc_id: u64,
430476
amount_msat: u64,
@@ -438,9 +484,21 @@ struct OutboundHTLCOutput {
438484
hold_htlc: Option<()>,
439485
}
440486

487+
impl_writeable_tlv_based!(OutboundHTLCOutput, {
488+
(0, htlc_id, required),
489+
(1, amount_msat, required),
490+
(2, cltv_expiry, required),
491+
(3, payment_hash, required),
492+
(4, state, required),
493+
(5, source, required),
494+
(6, blinding_point, required),
495+
(7, skimmed_fee_msat, required),
496+
(8, send_timestamp, required),
497+
(9, hold_htlc, required),
498+
});
499+
441500
/// See AwaitingRemoteRevoke ChannelState for more info
442-
#[derive(Debug)]
443-
#[cfg_attr(test, derive(Clone, PartialEq))]
501+
#[derive(Debug, Clone, PartialEq, Eq)]
444502
enum HTLCUpdateAwaitingACK {
445503
AddHTLC {
446504
// TODO: Time out if we're getting close to cltv_expiry
@@ -471,6 +529,33 @@ enum HTLCUpdateAwaitingACK {
471529
},
472530
}
473531

532+
impl_writeable_tlv_based_enum!(HTLCUpdateAwaitingACK,
533+
(0, AddHTLC) => {
534+
(0, amount_msat, required),
535+
(1, cltv_expiry, required),
536+
(2, payment_hash, required),
537+
(3, source, required),
538+
(4, onion_routing_packet, required),
539+
(5, skimmed_fee_msat, required),
540+
(6, blinding_point, required),
541+
(7, hold_htlc, required),
542+
},
543+
(1, ClaimHTLC) => {
544+
(0, payment_preimage, required),
545+
(1, attribution_data, required),
546+
(2, htlc_id, required),
547+
},
548+
(2, FailHTLC) => {
549+
(0, htlc_id, required),
550+
(1, err_packet, required),
551+
},
552+
(3, FailMalformedHTLC) => {
553+
(0, htlc_id, required),
554+
(1, failure_code, required),
555+
(2, sha256_of_onion, required),
556+
}
557+
);
558+
474559
macro_rules! define_state_flags {
475560
($flag_type_doc: expr, $flag_type: ident, [$(($flag_doc: expr, $flag: ident, $value: expr, $get: ident, $set: ident, $clear: ident)),*], $extra_flags: expr) => {
476561
#[doc = $flag_type_doc]
@@ -710,6 +795,19 @@ enum ChannelState {
710795
ShutdownComplete,
711796
}
712797

798+
impl Writeable for ChannelState {
799+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
800+
self.to_u32().write(w)
801+
}
802+
}
803+
804+
impl Readable for ChannelState {
805+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
806+
let state_u32 = u32::read(r)?;
807+
ChannelState::from_u32(state_u32).map_err(|_| DecodeError::InvalidValue)
808+
}
809+
}
810+
713811
macro_rules! impl_state_flag {
714812
($get: ident, $set: ident, $clear: ident, [$($state: ident),+]) => {
715813
#[allow(unused)]
@@ -1023,7 +1121,7 @@ macro_rules! secp_check {
10231121
/// spamming the network with updates if the connection is flapping. Instead, we "stage" updates to
10241122
/// our channel_update message and track the current state here.
10251123
/// See implementation at [`super::channelmanager::ChannelManager::timer_tick_occurred`].
1026-
#[derive(Clone, Copy, PartialEq, Debug)]
1124+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10271125
pub(super) enum ChannelUpdateStatus {
10281126
/// We've announced the channel as enabled and are connected to our peer.
10291127
Enabled,
@@ -1036,8 +1134,7 @@ pub(super) enum ChannelUpdateStatus {
10361134
}
10371135

10381136
/// We track when we sent an `AnnouncementSignatures` to our peer in a few states, described here.
1039-
#[cfg_attr(test, derive(Debug))]
1040-
#[derive(PartialEq)]
1137+
#[derive(PartialEq, Clone, Debug, Eq)]
10411138
pub enum AnnouncementSigsState {
10421139
/// We have not sent our peer an `AnnouncementSignatures` yet, or our peer disconnected since
10431140
/// we sent the last `AnnouncementSignatures`.
@@ -1217,7 +1314,7 @@ pub(crate) struct DisconnectResult {
12171314
/// Tracks the transaction number, along with current and next commitment points.
12181315
/// This consolidates the logic to advance our commitment number and request new
12191316
/// commitment points from our signer.
1220-
#[derive(Debug, Copy, Clone)]
1317+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
12211318
struct HolderCommitmentPoint {
12221319
next_transaction_number: u64,
12231320
current_point: Option<PublicKey>,
@@ -1232,6 +1329,15 @@ struct HolderCommitmentPoint {
12321329
last_revoked_point: Option<PublicKey>,
12331330
}
12341331

1332+
impl_writeable_tlv_based!(HolderCommitmentPoint, {
1333+
(0, next_transaction_number, required),
1334+
(1, current_point, required),
1335+
(2, next_point, required),
1336+
(3, pending_next_point, required),
1337+
(4, previous_revoked_point, required),
1338+
(5, last_revoked_point, required),
1339+
});
1340+
12351341
impl HolderCommitmentPoint {
12361342
#[rustfmt::skip]
12371343
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
@@ -1425,7 +1531,7 @@ pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 14 * 24 * 6 * 4;
14251531
#[cfg(test)]
14261532
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 144;
14271533

1428-
#[derive(Debug)]
1534+
#[derive(Debug, Clone, PartialEq, Eq)]
14291535
struct PendingChannelMonitorUpdate {
14301536
update: ChannelMonitorUpdate,
14311537
}
@@ -2376,6 +2482,53 @@ pub(super) struct FundingScope {
23762482
minimum_depth_override: Option<u32>,
23772483
}
23782484

2485+
impl Eq for FundingScope {}
2486+
2487+
impl PartialEq for FundingScope {
2488+
fn eq(&self, other: &Self) -> bool {
2489+
self.value_to_self_msat == other.value_to_self_msat
2490+
&& self.counterparty_selected_channel_reserve_satoshis
2491+
== other.counterparty_selected_channel_reserve_satoshis
2492+
&& self.holder_selected_channel_reserve_satoshis
2493+
== other.holder_selected_channel_reserve_satoshis
2494+
&& self.channel_transaction_parameters == other.channel_transaction_parameters
2495+
&& self.funding_transaction == other.funding_transaction
2496+
&& self.funding_tx_confirmed_in == other.funding_tx_confirmed_in
2497+
&& self.funding_tx_confirmation_height == other.funding_tx_confirmation_height
2498+
&& self.short_channel_id == other.short_channel_id
2499+
&& self.minimum_depth_override == other.minimum_depth_override
2500+
}
2501+
}
2502+
2503+
impl Clone for FundingScope {
2504+
fn clone(&self) -> Self {
2505+
FundingScope {
2506+
value_to_self_msat: self.value_to_self_msat,
2507+
counterparty_selected_channel_reserve_satoshis: self
2508+
.counterparty_selected_channel_reserve_satoshis,
2509+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
2510+
#[cfg(debug_assertions)]
2511+
holder_max_commitment_tx_output: Mutex::new(
2512+
*self.holder_max_commitment_tx_output.lock().unwrap(),
2513+
),
2514+
#[cfg(debug_assertions)]
2515+
counterparty_max_commitment_tx_output: Mutex::new(
2516+
*self.counterparty_max_commitment_tx_output.lock().unwrap(),
2517+
),
2518+
#[cfg(any(test, fuzzing))]
2519+
next_local_fee: Mutex::new(*self.next_local_fee.lock().unwrap()),
2520+
#[cfg(any(test, fuzzing))]
2521+
next_remote_fee: Mutex::new(*self.next_remote_fee.lock().unwrap()),
2522+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
2523+
funding_transaction: self.funding_transaction.clone(),
2524+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
2525+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
2526+
short_channel_id: self.short_channel_id,
2527+
minimum_depth_override: self.minimum_depth_override,
2528+
}
2529+
}
2530+
}
2531+
23792532
impl Writeable for FundingScope {
23802533
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
23812534
write_tlv_fields!(writer, {
@@ -2662,7 +2815,7 @@ impl FundingScope {
26622815
/// Information about pending attempts at funding a channel. This includes funding currently under
26632816
/// negotiation and any negotiated attempts waiting enough on-chain confirmations. More than one
26642817
/// such attempt indicates use of RBF to increase the chances of confirmation.
2665-
#[derive(Debug)]
2818+
#[derive(Debug, Clone, Eq, PartialEq)]
26662819
struct PendingFunding {
26672820
funding_negotiation: Option<FundingNegotiation>,
26682821

@@ -2684,7 +2837,7 @@ impl_writeable_tlv_based!(PendingFunding, {
26842837
(7, received_funding_txid, option),
26852838
});
26862839

2687-
#[derive(Debug)]
2840+
#[derive(Debug, Clone, PartialEq, Eq)]
26882841
enum FundingNegotiation {
26892842
AwaitingAck {
26902843
context: FundingNegotiationContext,
@@ -2764,7 +2917,7 @@ impl PendingFunding {
27642917
}
27652918
}
27662919

2767-
#[derive(Debug)]
2920+
#[derive(Debug, Clone, PartialEq, Eq)]
27682921
pub(crate) struct SpliceInstructions {
27692922
adjusted_funding_contribution: SignedAmount,
27702923
our_funding_inputs: Vec<FundingTxInput>,
@@ -2792,7 +2945,7 @@ impl_writeable_tlv_based!(SpliceInstructions, {
27922945
(11, locktime, required),
27932946
});
27942947

2795-
#[derive(Debug)]
2948+
#[derive(Debug, Clone, PartialEq, Eq)]
27962949
pub(crate) enum QuiescentAction {
27972950
Splice(SpliceInstructions),
27982951
#[cfg(any(test, fuzzing))]
@@ -6609,7 +6762,7 @@ fn check_v2_funding_inputs_sufficient(
66096762
}
66106763

66116764
/// Context for negotiating channels (dual-funded V2 open, splicing)
6612-
#[derive(Debug)]
6765+
#[derive(Debug, Clone, PartialEq, Eq)]
66136766
pub(super) struct FundingNegotiationContext {
66146767
/// Whether we initiated the funding negotiation.
66156768
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)