Skip to content

Commit 1ce710f

Browse files
committed
f: Build splice funding scopes after validate_splice_contributions
1 parent 08989a2 commit 1ce710f

File tree

1 file changed

+64
-67
lines changed

1 file changed

+64
-67
lines changed

lightning/src/ln/channel.rs

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10960,18 +10960,9 @@ where
1096010960
// Note: post-splice channel value is not yet known at this point, counterparty contribution is not known
1096110961
// (Cannot test for miminum required post-splice channel value)
1096210962
let their_funding_contribution = SignedAmount::ZERO;
10963-
let counterparty_public_key = self
10964-
.funding
10965-
.channel_transaction_parameters
10966-
.counterparty_parameters
10967-
.as_ref()
10968-
.expect("counterparty_parameters should be set")
10969-
.pubkeys
10970-
.funding_pubkey;
1097110963
self.validate_splice_contributions(
1097210964
adjusted_funding_contribution,
1097310965
their_funding_contribution,
10974-
counterparty_public_key,
1097510966
)
1097610967
.map_err(|err| APIError::APIMisuseError { err })?;
1097710968

@@ -11068,19 +11059,22 @@ where
1106811059
)));
1106911060
}
1107011061

11071-
self.validate_splice_contributions(
11062+
self.validate_splice_contributions(our_funding_contribution, their_funding_contribution)
11063+
.map_err(|e| ChannelError::WarnAndDisconnect(e))?;
11064+
11065+
Ok(FundingScope::for_splice(
11066+
&self.funding,
11067+
&self.context,
1107211068
our_funding_contribution,
1107311069
their_funding_contribution,
1107411070
msg.funding_pubkey,
11075-
)
11076-
.map_err(|e| ChannelError::WarnAndDisconnect(e))
11071+
))
1107711072
}
1107811073

1107911074
#[cfg(splicing)]
1108011075
fn validate_splice_contributions(
1108111076
&self, our_funding_contribution: SignedAmount, their_funding_contribution: SignedAmount,
11082-
counterparty_funding_pubkey: PublicKey,
11083-
) -> Result<FundingScope, String> {
11077+
) -> Result<(), String> {
1108411078
if our_funding_contribution.abs() > SignedAmount::MAX_MONEY {
1108511079
return Err(format!(
1108611080
"Channel {} cannot be spliced; our {} contribution exceeds the total bitcoin supply",
@@ -11097,77 +11091,76 @@ where
1109711091
));
1109811092
}
1109911093

11100-
// Sanity check all funding contributions here; we need to do this before building a `FundingScope`
11094+
let (holder_balance_remaining, counterparty_balance_remaining) =
11095+
self.get_holder_counterparty_balances_floor_incl_fee(&self.funding).map_err(|e| {
11096+
format!("Channel {} cannot be spliced; {}", self.context.channel_id(), e)
11097+
})?;
1110111098

11102-
let our_channel_balance = Amount::from_sat(self.funding.get_value_to_self_msat() / 1000);
11103-
AddSigned::checked_add_signed(
11104-
our_channel_balance.to_sat(),
11099+
let post_channel_value = self.funding.compute_post_splice_value(
1110511100
our_funding_contribution.to_sat(),
11106-
)
11107-
.ok_or(format!(
11108-
"Channel {} cannot be spliced out; our {} contribution exhausts our channel balance: {}",
11109-
self.context.channel_id(),
11110-
our_funding_contribution,
11111-
our_channel_balance,
11112-
))?;
11113-
11114-
let their_channel_balance = Amount::from_sat(
11115-
self.funding.get_value_satoshis() - self.funding.get_value_to_self_msat() / 1000,
11116-
);
11117-
AddSigned::checked_add_signed(
11118-
their_channel_balance.to_sat(),
1111911101
their_funding_contribution.to_sat(),
11120-
)
11121-
.ok_or(format!(
11122-
"Channel {} cannot be spliced out; their {} contribution exhausts their channel balance: {}",
11123-
self.context.channel_id(),
11124-
their_funding_contribution,
11125-
their_channel_balance,
11126-
))?;
11127-
11128-
let splice_funding = FundingScope::for_splice(
11129-
&self.funding,
11130-
&self.context,
11131-
our_funding_contribution,
11132-
their_funding_contribution,
11133-
counterparty_funding_pubkey,
1113411102
);
11135-
11136-
let (holder_balance_remaining, counterparty_balance_remaining) =
11137-
self.get_holder_counterparty_balances_floor_incl_fee(&splice_funding).map_err(|e| {
11138-
format!("Channel {} cannot be spliced; {}", self.context.channel_id(), e)
11139-
})?;
11103+
let counterparty_selected_channel_reserve = Amount::from_sat(
11104+
get_v2_channel_reserve_satoshis(post_channel_value, MIN_CHAN_DUST_LIMIT_SATOSHIS),
11105+
);
11106+
let holder_selected_channel_reserve = Amount::from_sat(get_v2_channel_reserve_satoshis(
11107+
post_channel_value,
11108+
self.context.counterparty_dust_limit_satoshis,
11109+
));
1114011110

1114111111
// We allow parties to draw from their previous reserve, as long as they satisfy their v2 reserve
1114211112

1114311113
if our_funding_contribution != SignedAmount::ZERO {
11144-
let counterparty_selected_channel_reserve_satoshis = splice_funding
11145-
.counterparty_selected_channel_reserve_satoshis
11146-
.expect("counterparty_selected_channel_reserve_satoshis should be set");
11147-
holder_balance_remaining
11148-
.checked_sub(Amount::from_sat(counterparty_selected_channel_reserve_satoshis))
11114+
let post_splice_holder_balance = Amount::from_sat(
11115+
AddSigned::checked_add_signed(
11116+
holder_balance_remaining.to_sat(),
11117+
our_funding_contribution.to_sat(),
11118+
)
11119+
.ok_or(format!(
11120+
"Channel {} cannot be {}; our remaining balance {} does not cover our negative funding contribution {}",
11121+
self.context.channel_id(),
11122+
if our_funding_contribution.is_positive() { "spliced in" } else { "spliced out" },
11123+
holder_balance_remaining,
11124+
our_funding_contribution,
11125+
))?,
11126+
);
11127+
11128+
post_splice_holder_balance.checked_sub(counterparty_selected_channel_reserve)
1114911129
.ok_or(format!(
11150-
"Channel {} cannot be {}; We cannot afford the new counterparty mandated reserve {} vs {}",
11130+
"Channel {} cannot be {}; Our post-splice channel balance {} is smaller than their selected v2 reserve {}",
1115111131
self.context.channel_id(),
1115211132
if our_funding_contribution.is_positive() { "spliced in" } else { "spliced out" },
11153-
holder_balance_remaining, counterparty_selected_channel_reserve_satoshis,
11133+
post_splice_holder_balance,
11134+
counterparty_selected_channel_reserve,
1115411135
))?;
1115511136
}
1115611137

1115711138
if their_funding_contribution != SignedAmount::ZERO {
11158-
let holder_selected_channel_reserve_satoshis =
11159-
splice_funding.holder_selected_channel_reserve_satoshis;
11160-
counterparty_balance_remaining
11161-
.checked_sub(Amount::from_sat(holder_selected_channel_reserve_satoshis))
11139+
let post_splice_counterparty_balance = Amount::from_sat(
11140+
AddSigned::checked_add_signed(
11141+
counterparty_balance_remaining.to_sat(),
11142+
their_funding_contribution.to_sat(),
11143+
)
11144+
.ok_or(format!(
11145+
"Channel {} cannot be {}; their remaining balance {} does not cover their negative funding contribution {}",
11146+
self.context.channel_id(),
11147+
if their_funding_contribution.is_positive() { "spliced in" } else { "spliced out" },
11148+
counterparty_balance_remaining,
11149+
their_funding_contribution,
11150+
))?,
11151+
);
11152+
11153+
post_splice_counterparty_balance.checked_sub(holder_selected_channel_reserve)
1116211154
.ok_or(format!(
11163-
"Channel {} cannot be {}; They cannot afford the new holder mandated reserve {} vs {}",
11155+
"Channel {} cannot be {}; Their post-splice channel balance {} is smaller than our selected v2 reserve {}",
1116411156
self.context.channel_id(),
1116511157
if their_funding_contribution.is_positive() { "spliced in" } else { "spliced out" },
11166-
counterparty_balance_remaining, holder_selected_channel_reserve_satoshis,
11158+
post_splice_counterparty_balance,
11159+
holder_selected_channel_reserve,
1116711160
))?;
1116811161
}
1116911162

11170-
Ok(splice_funding)
11163+
Ok(())
1117111164
}
1117211165

1117311166
#[cfg(splicing)]
@@ -11320,12 +11313,16 @@ where
1132011313

1132111314
let our_funding_contribution = funding_negotiation_context.our_funding_contribution;
1132211315
let their_funding_contribution = SignedAmount::from_sat(msg.funding_contribution_satoshis);
11323-
self.validate_splice_contributions(
11316+
self.validate_splice_contributions(our_funding_contribution, their_funding_contribution)
11317+
.map_err(|e| ChannelError::WarnAndDisconnect(e))?;
11318+
11319+
Ok(FundingScope::for_splice(
11320+
&self.funding,
11321+
&self.context,
1132411322
our_funding_contribution,
1132511323
their_funding_contribution,
1132611324
msg.funding_pubkey,
11327-
)
11328-
.map_err(|e| ChannelError::WarnAndDisconnect(e))
11325+
))
1132911326
}
1133011327

1133111328
#[cfg(splicing)]

0 commit comments

Comments
 (0)