Skip to content

Commit a595bf4

Browse files
committed
Add channel tlv serialization
1 parent 98c2c4a commit a595bf4

File tree

10 files changed

+205
-51
lines changed

10 files changed

+205
-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: 175 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,14 +1314,21 @@ 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>,
12241321
next_point: PublicKey,
12251322
pending_next_point: Option<PublicKey>,
12261323
}
12271324

1325+
impl_writeable_tlv_based!(HolderCommitmentPoint, {
1326+
(0, next_transaction_number, required),
1327+
(1, current_point, required),
1328+
(2, next_point, required),
1329+
(3, pending_next_point, required),
1330+
});
1331+
12281332
impl HolderCommitmentPoint {
12291333
#[rustfmt::skip]
12301334
pub fn new<SP: Deref>(signer: &ChannelSignerType<SP>, secp_ctx: &Secp256k1<secp256k1::All>) -> Option<Self>
@@ -1406,7 +1510,7 @@ pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 14 * 24 * 6 * 4;
14061510
#[cfg(test)]
14071511
pub(crate) const CHANNEL_ANNOUNCEMENT_PROPAGATION_DELAY: u32 = 144;
14081512

1409-
#[derive(Debug)]
1513+
#[derive(Debug, Clone, PartialEq, Eq)]
14101514
struct PendingChannelMonitorUpdate {
14111515
update: ChannelMonitorUpdate,
14121516
}
@@ -2357,6 +2461,53 @@ pub(super) struct FundingScope {
23572461
minimum_depth_override: Option<u32>,
23582462
}
23592463

2464+
impl Eq for FundingScope {}
2465+
2466+
impl PartialEq for FundingScope {
2467+
fn eq(&self, other: &Self) -> bool {
2468+
self.value_to_self_msat == other.value_to_self_msat
2469+
&& self.counterparty_selected_channel_reserve_satoshis
2470+
== other.counterparty_selected_channel_reserve_satoshis
2471+
&& self.holder_selected_channel_reserve_satoshis
2472+
== other.holder_selected_channel_reserve_satoshis
2473+
&& self.channel_transaction_parameters == other.channel_transaction_parameters
2474+
&& self.funding_transaction == other.funding_transaction
2475+
&& self.funding_tx_confirmed_in == other.funding_tx_confirmed_in
2476+
&& self.funding_tx_confirmation_height == other.funding_tx_confirmation_height
2477+
&& self.short_channel_id == other.short_channel_id
2478+
&& self.minimum_depth_override == other.minimum_depth_override
2479+
}
2480+
}
2481+
2482+
impl Clone for FundingScope {
2483+
fn clone(&self) -> Self {
2484+
FundingScope {
2485+
value_to_self_msat: self.value_to_self_msat,
2486+
counterparty_selected_channel_reserve_satoshis: self
2487+
.counterparty_selected_channel_reserve_satoshis,
2488+
holder_selected_channel_reserve_satoshis: self.holder_selected_channel_reserve_satoshis,
2489+
#[cfg(debug_assertions)]
2490+
holder_max_commitment_tx_output: Mutex::new(
2491+
*self.holder_max_commitment_tx_output.lock().unwrap(),
2492+
),
2493+
#[cfg(debug_assertions)]
2494+
counterparty_max_commitment_tx_output: Mutex::new(
2495+
*self.counterparty_max_commitment_tx_output.lock().unwrap(),
2496+
),
2497+
#[cfg(any(test, fuzzing))]
2498+
next_local_fee: Mutex::new(*self.next_local_fee.lock().unwrap()),
2499+
#[cfg(any(test, fuzzing))]
2500+
next_remote_fee: Mutex::new(*self.next_remote_fee.lock().unwrap()),
2501+
channel_transaction_parameters: self.channel_transaction_parameters.clone(),
2502+
funding_transaction: self.funding_transaction.clone(),
2503+
funding_tx_confirmed_in: self.funding_tx_confirmed_in,
2504+
funding_tx_confirmation_height: self.funding_tx_confirmation_height,
2505+
short_channel_id: self.short_channel_id,
2506+
minimum_depth_override: self.minimum_depth_override,
2507+
}
2508+
}
2509+
}
2510+
23602511
impl Writeable for FundingScope {
23612512
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
23622513
write_tlv_fields!(writer, {
@@ -2643,7 +2794,7 @@ impl FundingScope {
26432794
/// Information about pending attempts at funding a channel. This includes funding currently under
26442795
/// negotiation and any negotiated attempts waiting enough on-chain confirmations. More than one
26452796
/// such attempt indicates use of RBF to increase the chances of confirmation.
2646-
#[derive(Debug)]
2797+
#[derive(Debug, Clone, Eq, PartialEq)]
26472798
struct PendingFunding {
26482799
funding_negotiation: Option<FundingNegotiation>,
26492800

@@ -2665,7 +2816,7 @@ impl_writeable_tlv_based!(PendingFunding, {
26652816
(7, received_funding_txid, option),
26662817
});
26672818

2668-
#[derive(Debug)]
2819+
#[derive(Debug, Clone, PartialEq, Eq)]
26692820
enum FundingNegotiation {
26702821
AwaitingAck {
26712822
context: FundingNegotiationContext,
@@ -2745,7 +2896,7 @@ impl PendingFunding {
27452896
}
27462897
}
27472898

2748-
#[derive(Debug)]
2899+
#[derive(Debug, Clone, PartialEq, Eq)]
27492900
pub(crate) struct SpliceInstructions {
27502901
adjusted_funding_contribution: SignedAmount,
27512902
our_funding_inputs: Vec<FundingTxInput>,
@@ -2773,7 +2924,7 @@ impl_writeable_tlv_based!(SpliceInstructions, {
27732924
(11, locktime, required),
27742925
});
27752926

2776-
#[derive(Debug)]
2927+
#[derive(Debug, Clone, PartialEq, Eq)]
27772928
pub(crate) enum QuiescentAction {
27782929
Splice(SpliceInstructions),
27792930
#[cfg(any(test, fuzzing))]
@@ -6586,7 +6737,7 @@ fn check_v2_funding_inputs_sufficient(
65866737
}
65876738

65886739
/// Context for negotiating channels (dual-funded V2 open, splicing)
6589-
#[derive(Debug)]
6740+
#[derive(Debug, Clone, PartialEq, Eq)]
65906741
pub(super) struct FundingNegotiationContext {
65916742
/// Whether we initiated the funding negotiation.
65926743
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)