@@ -224,6 +224,7 @@ struct OutboundHTLCOutput {
224
224
payment_hash: PaymentHash,
225
225
state: OutboundHTLCState,
226
226
source: HTLCSource,
227
+ skimmed_fee_msat: Option<u64>,
227
228
}
228
229
229
230
/// See AwaitingRemoteRevoke ChannelState for more info
@@ -235,6 +236,8 @@ enum HTLCUpdateAwaitingACK {
235
236
payment_hash: PaymentHash,
236
237
source: HTLCSource,
237
238
onion_routing_packet: msgs::OnionPacket,
239
+ // The extra fee we're skimming off the top of this HTLC.
240
+ skimmed_fee_msat: Option<u64>,
238
241
},
239
242
ClaimHTLC {
240
243
payment_preimage: PaymentPreimage,
@@ -3052,8 +3055,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3052
3055
// handling this case better and maybe fulfilling some of the HTLCs while attempting
3053
3056
// to rebalance channels.
3054
3057
match &htlc_update {
3055
- & HTLCUpdateAwaitingACK :: AddHTLC { amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
3056
- match self . send_htlc ( amount_msat, * payment_hash, cltv_expiry, source. clone ( ) , onion_routing_packet. clone ( ) , false , logger) {
3058
+ &HTLCUpdateAwaitingACK::AddHTLC {
3059
+ amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
3060
+ skimmed_fee_msat, ..
3061
+ } => {
3062
+ match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(),
3063
+ onion_routing_packet.clone(), false, skimmed_fee_msat, logger)
3064
+ {
3057
3065
Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
3058
3066
Err(e) => {
3059
3067
match e {
@@ -3695,6 +3703,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3695
3703
payment_hash: htlc.payment_hash,
3696
3704
cltv_expiry: htlc.cltv_expiry,
3697
3705
onion_routing_packet: (**onion_packet).clone(),
3706
+ skimmed_fee_msat: htlc.skimmed_fee_msat,
3698
3707
});
3699
3708
}
3700
3709
}
@@ -5042,11 +5051,13 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5042
5051
/// commitment update.
5043
5052
///
5044
5053
/// `Err`s will only be [`ChannelError::Ignore`].
5045
- pub fn queue_add_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5046
- onion_routing_packet : msgs:: OnionPacket , logger : & L )
5047
- -> Result < ( ) , ChannelError > where L :: Target : Logger {
5054
+ pub fn queue_add_htlc<L: Deref>(
5055
+ &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
5056
+ onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>, logger: &L
5057
+ ) -> Result<(), ChannelError> where L::Target: Logger {
5048
5058
self
5049
- . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true , logger)
5059
+ .send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true,
5060
+ skimmed_fee_msat, logger)
5050
5061
.map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
5051
5062
.map_err(|err| {
5052
5063
if let ChannelError::Ignore(_) = err { /* fine */ }
@@ -5071,9 +5082,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5071
5082
/// on this [`Channel`] if `force_holding_cell` is false.
5072
5083
///
5073
5084
/// `Err`s will only be [`ChannelError::Ignore`].
5074
- fn send_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5075
- onion_routing_packet : msgs:: OnionPacket , mut force_holding_cell : bool , logger : & L )
5076
- -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > where L :: Target : Logger {
5085
+ fn send_htlc<L: Deref>(
5086
+ &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
5087
+ onion_routing_packet: msgs::OnionPacket, mut force_holding_cell: bool,
5088
+ skimmed_fee_msat: Option<u64>, logger: &L
5089
+ ) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> where L::Target: Logger {
5077
5090
if (self.context.channel_state & (ChannelState::ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelReady as u32) {
5078
5091
return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down".to_owned()));
5079
5092
}
@@ -5125,6 +5138,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5125
5138
cltv_expiry,
5126
5139
source,
5127
5140
onion_routing_packet,
5141
+ skimmed_fee_msat,
5128
5142
});
5129
5143
return Ok(None);
5130
5144
}
@@ -5136,6 +5150,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5136
5150
cltv_expiry,
5137
5151
state: OutboundHTLCState::LocalAnnounced(Box::new(onion_routing_packet.clone())),
5138
5152
source,
5153
+ skimmed_fee_msat,
5139
5154
});
5140
5155
5141
5156
let res = msgs::UpdateAddHTLC {
@@ -5145,6 +5160,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5145
5160
payment_hash,
5146
5161
cltv_expiry,
5147
5162
onion_routing_packet,
5163
+ skimmed_fee_msat,
5148
5164
};
5149
5165
self.context.next_holder_htlc_id += 1;
5150
5166
@@ -5283,8 +5299,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5283
5299
///
5284
5300
/// Shorthand for calling [`Self::send_htlc`] followed by a commitment update, see docs on
5285
5301
/// [`Self::send_htlc`] and [`Self::build_commitment_no_state_update`] for more info.
5286
- pub fn send_htlc_and_commit < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource , onion_routing_packet : msgs:: OnionPacket , logger : & L ) -> Result < Option < & ChannelMonitorUpdate > , ChannelError > where L :: Target : Logger {
5287
- let send_res = self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, false , logger) ;
5302
+ pub fn send_htlc_and_commit<L: Deref>(
5303
+ &mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource,
5304
+ onion_routing_packet: msgs::OnionPacket, skimmed_fee_msat: Option<u64>, logger: &L
5305
+ ) -> Result<Option<&ChannelMonitorUpdate>, ChannelError> where L::Target: Logger {
5306
+ let send_res = self.send_htlc(amount_msat, payment_hash, cltv_expiry, source,
5307
+ onion_routing_packet, false, skimmed_fee_msat, logger);
5288
5308
if let Err(e) = &send_res { if let ChannelError::Ignore(_) = e {} else { debug_assert!(false, "Sending cannot trigger channel failure"); } }
5289
5309
match send_res? {
5290
5310
Some(_) => {
@@ -6609,9 +6629,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
6609
6629
}
6610
6630
6611
6631
let mut preimages: Vec<&Option<PaymentPreimage>> = vec![];
6632
+ let mut pending_outbound_skimmed_fees: Vec<Option<u64>> = Vec::new();
6612
6633
6613
6634
(self.context.pending_outbound_htlcs.len() as u64).write(writer)?;
6614
- for htlc in self . context . pending_outbound_htlcs . iter ( ) {
6635
+ for (idx, htlc) in self.context.pending_outbound_htlcs.iter().enumerate () {
6615
6636
htlc.htlc_id.write(writer)?;
6616
6637
htlc.amount_msat.write(writer)?;
6617
6638
htlc.cltv_expiry.write(writer)?;
@@ -6647,18 +6668,37 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
6647
6668
reason.write(writer)?;
6648
6669
}
6649
6670
}
6671
+ if let Some(skimmed_fee) = htlc.skimmed_fee_msat {
6672
+ if pending_outbound_skimmed_fees.is_empty() {
6673
+ for _ in 0..idx { pending_outbound_skimmed_fees.push(None); }
6674
+ }
6675
+ pending_outbound_skimmed_fees.push(Some(skimmed_fee));
6676
+ } else if !pending_outbound_skimmed_fees.is_empty() {
6677
+ pending_outbound_skimmed_fees.push(None);
6678
+ }
6650
6679
}
6651
6680
6681
+ let mut holding_cell_skimmed_fees: Vec<Option<u64>> = Vec::new();
6652
6682
(self.context.holding_cell_htlc_updates.len() as u64).write(writer)?;
6653
- for update in self . context . holding_cell_htlc_updates . iter ( ) {
6683
+ for (idx, update) in self.context.holding_cell_htlc_updates.iter().enumerate () {
6654
6684
match update {
6655
- & HTLCUpdateAwaitingACK :: AddHTLC { ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet } => {
6685
+ &HTLCUpdateAwaitingACK::AddHTLC {
6686
+ ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
6687
+ skimmed_fee_msat,
6688
+ } => {
6656
6689
0u8.write(writer)?;
6657
6690
amount_msat.write(writer)?;
6658
6691
cltv_expiry.write(writer)?;
6659
6692
payment_hash.write(writer)?;
6660
6693
source.write(writer)?;
6661
6694
onion_routing_packet.write(writer)?;
6695
+
6696
+ if let Some(skimmed_fee) = skimmed_fee_msat {
6697
+ if holding_cell_skimmed_fees.is_empty() {
6698
+ for _ in 0..idx { holding_cell_skimmed_fees.push(None); }
6699
+ }
6700
+ holding_cell_skimmed_fees.push(Some(skimmed_fee));
6701
+ } else if !holding_cell_skimmed_fees.is_empty() { holding_cell_skimmed_fees.push(None); }
6662
6702
},
6663
6703
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
6664
6704
1u8.write(writer)?;
@@ -6825,6 +6865,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
6825
6865
(29, self.context.temporary_channel_id, option),
6826
6866
(31, channel_pending_event_emitted, option),
6827
6867
(33, self.context.pending_monitor_updates, vec_type),
6868
+ (35, pending_outbound_skimmed_fees, optional_vec),
6869
+ (37, holding_cell_skimmed_fees, optional_vec),
6828
6870
});
6829
6871
6830
6872
Ok(())
@@ -6935,6 +6977,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
6935
6977
},
6936
6978
_ => return Err(DecodeError::InvalidValue),
6937
6979
},
6980
+ skimmed_fee_msat: None,
6938
6981
});
6939
6982
}
6940
6983
@@ -6948,6 +6991,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
6948
6991
payment_hash: Readable::read(reader)?,
6949
6992
source: Readable::read(reader)?,
6950
6993
onion_routing_packet: Readable::read(reader)?,
6994
+ skimmed_fee_msat: None,
6951
6995
},
6952
6996
1 => HTLCUpdateAwaitingACK::ClaimHTLC {
6953
6997
payment_preimage: Readable::read(reader)?,
@@ -7103,6 +7147,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7103
7147
7104
7148
let mut pending_monitor_updates = Some(Vec::new());
7105
7149
7150
+ let mut pending_outbound_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
7151
+ let mut holding_cell_skimmed_fees_opt: Option<Vec<Option<u64>>> = None;
7152
+
7106
7153
read_tlv_fields!(reader, {
7107
7154
(0, announcement_sigs, option),
7108
7155
(1, minimum_depth, option),
@@ -7126,6 +7173,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7126
7173
(29, temporary_channel_id, option),
7127
7174
(31, channel_pending_event_emitted, option),
7128
7175
(33, pending_monitor_updates, vec_type),
7176
+ (35, pending_outbound_skimmed_fees_opt, optional_vec),
7177
+ (37, holding_cell_skimmed_fees_opt, optional_vec),
7129
7178
});
7130
7179
7131
7180
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -7180,6 +7229,25 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7180
7229
7181
7230
let holder_max_accepted_htlcs = holder_max_accepted_htlcs.unwrap_or(DEFAULT_MAX_HTLCS);
7182
7231
7232
+ if let Some(skimmed_fees) = pending_outbound_skimmed_fees_opt {
7233
+ let mut iter = skimmed_fees.into_iter();
7234
+ for htlc in pending_outbound_htlcs.iter_mut() {
7235
+ htlc.skimmed_fee_msat = iter.next().ok_or(DecodeError::InvalidValue)?;
7236
+ }
7237
+ // We expect all skimmed fees to be consumed above
7238
+ if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
7239
+ }
7240
+ if let Some(skimmed_fees) = holding_cell_skimmed_fees_opt {
7241
+ let mut iter = skimmed_fees.into_iter();
7242
+ for htlc in holding_cell_htlc_updates.iter_mut() {
7243
+ if let HTLCUpdateAwaitingACK::AddHTLC { ref mut skimmed_fee_msat, .. } = htlc {
7244
+ *skimmed_fee_msat = iter.next().ok_or(DecodeError::InvalidValue)?;
7245
+ }
7246
+ }
7247
+ // We expect all skimmed fees to be consumed above
7248
+ if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
7249
+ }
7250
+
7183
7251
Ok(Channel {
7184
7252
context: ChannelContext {
7185
7253
user_id,
@@ -7522,7 +7590,8 @@ mod tests {
7522
7590
session_priv: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
7523
7591
first_hop_htlc_msat: 548,
7524
7592
payment_id: PaymentId([42; 32]),
7525
- }
7593
+ },
7594
+ skimmed_fee_msat: None,
7526
7595
});
7527
7596
7528
7597
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
@@ -8079,6 +8148,7 @@ mod tests {
8079
8148
payment_hash: PaymentHash([0; 32]),
8080
8149
state: OutboundHTLCState::Committed,
8081
8150
source: HTLCSource::dummy(),
8151
+ skimmed_fee_msat: None,
8082
8152
};
8083
8153
out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
8084
8154
out
@@ -8091,6 +8161,7 @@ mod tests {
8091
8161
payment_hash: PaymentHash([0; 32]),
8092
8162
state: OutboundHTLCState::Committed,
8093
8163
source: HTLCSource::dummy(),
8164
+ skimmed_fee_msat: None,
8094
8165
};
8095
8166
out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
8096
8167
out
@@ -8492,6 +8563,7 @@ mod tests {
8492
8563
payment_hash: PaymentHash([0; 32]),
8493
8564
state: OutboundHTLCState::Committed,
8494
8565
source: HTLCSource::dummy(),
8566
+ skimmed_fee_msat: None,
8495
8567
};
8496
8568
out.payment_hash.0 = Sha256::hash(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()).into_inner();
8497
8569
out
@@ -8504,6 +8576,7 @@ mod tests {
8504
8576
payment_hash: PaymentHash([0; 32]),
8505
8577
state: OutboundHTLCState::Committed,
8506
8578
source: HTLCSource::dummy(),
8579
+ skimmed_fee_msat: None,
8507
8580
};
8508
8581
out.payment_hash.0 = Sha256::hash(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()).into_inner();
8509
8582
out
0 commit comments