Skip to content

Commit 78e88e9

Browse files
authored
Merge pull request #2735 from valentinewallace/2023-11-skimmed-fee-ser
Fix skimmed fee serialization in `Channel`
2 parents 9942994 + 633be23 commit 78e88e9

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

lightning/src/ln/channel.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7152,7 +7152,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
71527152
let mut pending_outbound_blinding_points: Vec<Option<PublicKey>> = Vec::new();
71537153

71547154
(self.context.pending_outbound_htlcs.len() as u64).write(writer)?;
7155-
for (idx, htlc) in self.context.pending_outbound_htlcs.iter().enumerate() {
7155+
for htlc in self.context.pending_outbound_htlcs.iter() {
71567156
htlc.htlc_id.write(writer)?;
71577157
htlc.amount_msat.write(writer)?;
71587158
htlc.cltv_expiry.write(writer)?;
@@ -7188,21 +7188,14 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
71887188
reason.write(writer)?;
71897189
}
71907190
}
7191-
if let Some(skimmed_fee) = htlc.skimmed_fee_msat {
7192-
if pending_outbound_skimmed_fees.is_empty() {
7193-
for _ in 0..idx { pending_outbound_skimmed_fees.push(None); }
7194-
}
7195-
pending_outbound_skimmed_fees.push(Some(skimmed_fee));
7196-
} else if !pending_outbound_skimmed_fees.is_empty() {
7197-
pending_outbound_skimmed_fees.push(None);
7198-
}
7191+
pending_outbound_skimmed_fees.push(htlc.skimmed_fee_msat);
71997192
pending_outbound_blinding_points.push(htlc.blinding_point);
72007193
}
72017194

72027195
let mut holding_cell_skimmed_fees: Vec<Option<u64>> = Vec::new();
72037196
let mut holding_cell_blinding_points: Vec<Option<PublicKey>> = Vec::new();
72047197
(self.context.holding_cell_htlc_updates.len() as u64).write(writer)?;
7205-
for (idx, update) in self.context.holding_cell_htlc_updates.iter().enumerate() {
7198+
for update in self.context.holding_cell_htlc_updates.iter() {
72067199
match update {
72077200
&HTLCUpdateAwaitingACK::AddHTLC {
72087201
ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
@@ -7215,13 +7208,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
72157208
source.write(writer)?;
72167209
onion_routing_packet.write(writer)?;
72177210

7218-
if let Some(skimmed_fee) = skimmed_fee_msat {
7219-
if holding_cell_skimmed_fees.is_empty() {
7220-
for _ in 0..idx { holding_cell_skimmed_fees.push(None); }
7221-
}
7222-
holding_cell_skimmed_fees.push(Some(skimmed_fee));
7223-
} else if !holding_cell_skimmed_fees.is_empty() { holding_cell_skimmed_fees.push(None); }
7224-
7211+
holding_cell_skimmed_fees.push(skimmed_fee_msat);
72257212
holding_cell_blinding_points.push(blinding_point);
72267213
},
72277214
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
@@ -8474,8 +8461,8 @@ mod tests {
84748461
}
84758462

84768463
#[test]
8477-
fn blinding_point_ser() {
8478-
// Ensure that channel blinding points are (de)serialized properly.
8464+
fn blinding_point_skimmed_fee_ser() {
8465+
// Ensure that channel blinding points and skimmed fees are (de)serialized properly.
84798466
let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
84808467
let secp_ctx = Secp256k1::new();
84818468
let seed = [42; 32];
@@ -8516,6 +8503,9 @@ mod tests {
85168503
if idx % 2 == 0 {
85178504
htlc.blinding_point = Some(test_utils::pubkey(42 + idx as u8));
85188505
}
8506+
if idx % 3 == 0 {
8507+
htlc.skimmed_fee_msat = Some(1);
8508+
}
85198509
}
85208510
chan.context.pending_outbound_htlcs = pending_outbound_htlcs.clone();
85218511

@@ -8545,8 +8535,11 @@ mod tests {
85458535
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc.clone());
85468536
} else {
85478537
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
8548-
if let HTLCUpdateAwaitingACK::AddHTLC { ref mut blinding_point, .. } = &mut dummy_add {
8538+
if let HTLCUpdateAwaitingACK::AddHTLC {
8539+
ref mut blinding_point, ref mut skimmed_fee_msat, ..
8540+
} = &mut dummy_add {
85498541
*blinding_point = Some(test_utils::pubkey(42 + i));
8542+
*skimmed_fee_msat = Some(42);
85508543
} else { panic!() }
85518544
holding_cell_htlc_updates.push(dummy_add);
85528545
}

pending_changelog/skimmed_fee_ser.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Bug fixes
2+
3+
* In LDK versions 0.0.116 through 0.0.118, in rare cases where skimmed fees are present on shutdown
4+
the `ChannelManager` may fail to deserialize on startup.

0 commit comments

Comments
 (0)