@@ -4130,33 +4130,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4130
4130
}
4131
4131
}
4132
4132
4133
- /// Check that a balance value meets the channel reserve requirements or violates them (below reserve).
4134
- /// The channel value is an input as opposed to using from self, so that this can be used in case of splicing
4135
- /// to checks with new channel value (before being comitted to it).
4136
- #[cfg(splicing)]
4137
- pub fn check_balance_meets_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> {
4138
- if balance == 0 {
4139
- return Ok(());
4140
- }
4141
- let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4142
- channel_value, self.holder_dust_limit_satoshis);
4143
- if balance < holder_selected_channel_reserve_satoshis {
4144
- return Err(ChannelError::Warn(format!(
4145
- "Balance below reserve mandated by holder, {} vs {}",
4146
- balance, holder_selected_channel_reserve_satoshis,
4147
- )));
4148
- }
4149
- let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
4150
- channel_value, self.counterparty_dust_limit_satoshis);
4151
- if balance < counterparty_selected_channel_reserve_satoshis {
4152
- return Err(ChannelError::Warn(format!(
4153
- "Balance below reserve mandated by counterparty, {} vs {}",
4154
- balance, counterparty_selected_channel_reserve_satoshis,
4155
- )));
4156
- }
4157
- Ok(())
4158
- }
4159
-
4160
4133
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
4161
4134
/// number of pending HTLCs that are on track to be in our next commitment tx.
4162
4135
///
@@ -8378,74 +8351,66 @@ impl<SP: Deref> FundedChannel<SP> where
8378
8351
}
8379
8352
}
8380
8353
8381
- /// Initiate splicing
8354
+ /// Initiate splicing.
8355
+ /// - our_funding_inputs: the inputs we contribute to the new funding transaction.
8356
+ /// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
8382
8357
#[cfg(splicing)]
8383
8358
pub fn splice_channel(&mut self, our_funding_contribution_satoshis: i64,
8384
- our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, funding_feerate_perkw: u32, locktime: u32,
8385
- ) -> Result<msgs::SpliceInit, ChannelError> {
8359
+ _our_funding_inputs: &Vec<(TxIn, Transaction, Weight)>,
8360
+ funding_feerate_per_kw: u32, locktime: u32,
8361
+ ) -> Result<msgs::SpliceInit, APIError> {
8386
8362
// Check if a splice has been initiated already.
8387
- // Note: this could be handled more nicely, and support multiple outstanding splice's, the incoming splice_ack matters anyways.
8363
+ // Note: only a single outstanding splice is supported (per spec)
8388
8364
if let Some(splice_info) = &self.pending_splice_pre {
8389
- return Err(ChannelError::Warn(format!(
8390
- "Channel has already a splice pending, contribution {}", splice_info.our_funding_contribution
8391
- )));
8365
+ return Err(APIError::APIMisuseError { err: format!(
8366
+ "Channel {} cannot be spliced, as it has already a splice pending (contribution {})",
8367
+ self.context.channel_id(), splice_info.our_funding_contribution
8368
+ )});
8392
8369
}
8393
8370
8394
- if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
8395
- return Err(ChannelError::Warn(format!("Cannot initiate splicing, as channel is not Ready")));
8371
+ if !self.context.is_live() {
8372
+ return Err(APIError::APIMisuseError { err: format!(
8373
+ "Channel {} cannot be spliced, as channel is not live",
8374
+ self.context.channel_id()
8375
+ )});
8396
8376
}
8397
8377
8398
- let pre_channel_value = self.funding.get_value_satoshis();
8399
- // Sanity check: capacity cannot decrease below 0
8400
- if (pre_channel_value as i64).saturating_add(our_funding_contribution_satoshis) < 0 {
8401
- return Err(ChannelError::Warn(format!(
8402
- "Post-splicing channel value cannot be negative. It was {} + {}",
8403
- pre_channel_value, our_funding_contribution_satoshis
8404
- )));
8405
- }
8378
+ // TODO(splicing): check for quiescence
8406
8379
8407
8380
if our_funding_contribution_satoshis < 0 {
8408
- return Err(ChannelError::Warn( format!(
8409
- "TODO(splicing): Splice-out not supported, only splice in, contribution {}",
8410
- our_funding_contribution_satoshis,
8411
- )) );
8381
+ return Err(APIError::APIMisuseError { err: format!(
8382
+ "TODO(splicing): Splice-out not supported, only splice in; channel ID {} , contribution {}",
8383
+ self.context.channel_id(), our_funding_contribution_satoshis,
8384
+ )} );
8412
8385
}
8413
8386
8414
- // Note: post-splice channel value is not yet known at this point, counterpary contribution is not known
8387
+ // TODO(splicing): Once splice-out is supported, check that channel balance does not go below 0
8388
+ // (or below channel reserve)
8389
+
8390
+ // Note: post-splice channel value is not yet known at this point, counterparty contribution is not known
8415
8391
// (Cannot test for miminum required post-splice channel value)
8416
8392
8417
- // Check that inputs are sufficient to cover our contribution
8418
- let sum_input: i64 = our_funding_inputs.into_iter().map(
8419
- |(txin, tx, _)| tx.output.get(txin.previous_output.vout as usize).map(|tx| tx.value.to_sat() as i64).unwrap_or(0)
8420
- ).sum();
8421
- if sum_input < our_funding_contribution_satoshis {
8422
- return Err(ChannelError::Warn(format!(
8423
- "Provided inputs are insufficient for our contribution, {} {}",
8424
- sum_input, our_funding_contribution_satoshis,
8425
- )));
8426
- }
8427
8393
8428
8394
self.pending_splice_pre = Some(PendingSplice {
8429
8395
our_funding_contribution: our_funding_contribution_satoshis,
8430
8396
});
8431
8397
8432
- let msg = self.get_splice_init(our_funding_contribution_satoshis, funding_feerate_perkw , locktime);
8398
+ let msg = self.get_splice_init(our_funding_contribution_satoshis, funding_feerate_per_kw , locktime);
8433
8399
Ok(msg)
8434
8400
}
8435
8401
8436
8402
/// Get the splice message that can be sent during splice initiation.
8437
8403
#[cfg(splicing)]
8438
8404
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
8439
- funding_feerate_perkw : u32, locktime: u32,
8405
+ funding_feerate_per_kw : u32, locktime: u32,
8440
8406
) -> msgs::SpliceInit {
8441
- // Reuse the existing funding pubkey, in spite of the channel value changing
8442
- // (though at this point we don't know the new value yet, due tue the optional counterparty contribution)
8407
+ // TODO(splicing): The exisiting pubkey is reused, but a new one should be generated. See #3542.
8443
8408
// Note that channel_keys_id is supposed NOT to change
8444
8409
let funding_pubkey = self.funding.get_holder_pubkeys().funding_pubkey.clone();
8445
8410
msgs::SpliceInit {
8446
8411
channel_id: self.context.channel_id,
8447
8412
funding_contribution_satoshis: our_funding_contribution_satoshis,
8448
- funding_feerate_perkw ,
8413
+ funding_feerate_per_kw ,
8449
8414
locktime,
8450
8415
funding_pubkey,
8451
8416
require_confirmed_inputs: None,
@@ -8467,20 +8432,11 @@ impl<SP: Deref> FundedChannel<SP> where
8467
8432
)));
8468
8433
}
8469
8434
8470
- if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
8471
- return Err(ChannelError::Warn(format!("Splicing requested on a channel that is not Ready")));
8472
- }
8473
-
8474
- let pre_channel_value = self.funding.get_value_satoshis();
8475
- // Sanity check: capacity cannot decrease below 0
8476
- if (pre_channel_value as i64)
8477
- .saturating_add(their_funding_contribution_satoshis)
8478
- .saturating_add(our_funding_contribution_satoshis) < 0
8479
- {
8480
- return Err(ChannelError::Warn(format!(
8481
- "Post-splicing channel value cannot be negative. It was {} + {} + {}",
8482
- pre_channel_value, their_funding_contribution_satoshis, our_funding_contribution_satoshis,
8483
- )));
8435
+ // - If it has received shutdown:
8436
+ // MUST send a warning and close the connection or send an error
8437
+ // and fail the channel.
8438
+ if !self.context.is_live() {
8439
+ return Err(ChannelError::Warn(format!("Splicing requested on a channel that is not live")));
8484
8440
}
8485
8441
8486
8442
if their_funding_contribution_satoshis.saturating_add(our_funding_contribution_satoshis) < 0 {
@@ -8490,11 +8446,12 @@ impl<SP: Deref> FundedChannel<SP> where
8490
8446
)));
8491
8447
}
8492
8448
8493
- let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, their_funding_contribution_satoshis, our_funding_contribution_satoshis);
8494
- let post_balance = PendingSplice::add_checked(self.funding.value_to_self_msat, our_funding_contribution_satoshis);
8495
- // Early check for reserve requirement, assuming maximum balance of full channel value
8496
- // This will also be checked later at tx_complete
8497
- let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
8449
+ // TODO(splicing): Once splice acceptor can contribute, check that inputs are sufficient,
8450
+ // similarly to the check in `splice_channel`.
8451
+
8452
+ // Note on channel reserve requirement pre-check: as the splice acceptor does not contribute,
8453
+ // it can't go below reserve, therefore no pre-check is done here.
8454
+ // TODO(splicing): Once splice acceptor can contribute, add reserve pre-check, similar to the one in `splice_ack`.
8498
8455
8499
8456
// TODO(splicing): Store msg.funding_pubkey
8500
8457
// TODO(splicing): Apply start of splice (splice_start)
@@ -8507,7 +8464,8 @@ impl<SP: Deref> FundedChannel<SP> where
8507
8464
/// Get the splice_ack message that can be sent in response to splice initiation.
8508
8465
#[cfg(splicing)]
8509
8466
pub fn get_splice_ack(&self, our_funding_contribution_satoshis: i64) -> msgs::SpliceAck {
8510
- // Reuse the existing funding pubkey, in spite of the channel value changing
8467
+ // TODO(splicing): The exisiting pubkey is reused, but a new one should be generated. See #3542.
8468
+ // Note that channel_keys_id is supposed NOT to change
8511
8469
let funding_pubkey = self.funding.get_holder_pubkeys().funding_pubkey;
8512
8470
msgs::SpliceAck {
8513
8471
channel_id: self.context.channel_id,
@@ -8519,24 +8477,14 @@ impl<SP: Deref> FundedChannel<SP> where
8519
8477
8520
8478
/// Handle splice_ack
8521
8479
#[cfg(splicing)]
8522
- pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8523
- let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
8524
-
8480
+ pub fn splice_ack(&mut self, _msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8525
8481
// check if splice is pending
8526
- let pending_splice = if let Some(pending_splice) = &self.pending_splice_pre {
8527
- pending_splice
8528
- } else {
8482
+ if self.pending_splice_pre.is_none() {
8529
8483
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
8530
8484
};
8531
8485
8532
- let our_funding_contribution = pending_splice.our_funding_contribution;
8533
-
8534
- let pre_channel_value = self.funding.get_value_satoshis();
8535
- let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
8536
- let post_balance = PendingSplice::add_checked(self.funding.value_to_self_msat, our_funding_contribution);
8537
- // Early check for reserve requirement, assuming maximum balance of full channel value
8538
- // This will also be checked later at tx_complete
8539
- let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
8486
+ // TODO(splicing): Pre-check for reserve requirement
8487
+ // (Note: It should also be checked later at tx_complete)
8540
8488
Ok(())
8541
8489
}
8542
8490
@@ -12907,15 +12855,15 @@ mod tests {
12907
12855
);
12908
12856
}
12909
12857
12910
- #[cfg(all(test, splicing) )]
12858
+ #[cfg(splicing)]
12911
12859
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
12912
12860
use crate::ln::channel::PendingSplice;
12913
12861
12914
12862
let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution);
12915
12863
(pre_channel_value, post_channel_value)
12916
12864
}
12917
12865
12918
- #[cfg(all(test, splicing) )]
12866
+ #[cfg(splicing)]
12919
12867
#[test]
12920
12868
fn test_splice_compute_post_value() {
12921
12869
{
0 commit comments