Skip to content

Commit 4005d59

Browse files
committed
Remove format skips in channel holder commitment validation functions
1 parent b3773a7 commit 4005d59

File tree

1 file changed

+141
-56
lines changed

1 file changed

+141
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 141 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,49 +2511,79 @@ where
25112511

25122512
fn received_msg(&self) -> &'static str;
25132513

2514-
#[rustfmt::skip]
25152514
fn initial_commitment_signed<L: Deref>(
2516-
&mut self, channel_id: ChannelId, counterparty_signature: Signature, holder_commitment_point: &mut HolderCommitmentPoint,
2517-
best_block: BestBlock, signer_provider: &SP, logger: &L,
2518-
) -> Result<(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, CommitmentTransaction), ChannelError>
2515+
&mut self, channel_id: ChannelId, counterparty_signature: Signature,
2516+
holder_commitment_point: &mut HolderCommitmentPoint, best_block: BestBlock,
2517+
signer_provider: &SP, logger: &L,
2518+
) -> Result<
2519+
(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, CommitmentTransaction),
2520+
ChannelError,
2521+
>
25192522
where
2520-
L::Target: Logger
2523+
L::Target: Logger,
25212524
{
25222525
let context = self.context();
25232526

2524-
let initial_commitment_tx = context.build_commitment_transaction(self.funding(),
2525-
holder_commitment_point.transaction_number(), &holder_commitment_point.current_point(),
2526-
true, false, logger).tx;
2527+
let initial_commitment_tx = context
2528+
.build_commitment_transaction(
2529+
self.funding(),
2530+
holder_commitment_point.transaction_number(),
2531+
&holder_commitment_point.current_point(),
2532+
true,
2533+
false,
2534+
logger,
2535+
)
2536+
.tx;
25272537
let holder_commitment_tx = HolderCommitmentTransaction::new(
25282538
initial_commitment_tx,
25292539
counterparty_signature,
25302540
Vec::new(),
25312541
&self.funding().get_holder_pubkeys().funding_pubkey,
2532-
&self.funding().counterparty_funding_pubkey()
2542+
&self.funding().counterparty_funding_pubkey(),
25332543
);
25342544

2535-
log_trace!(logger, "Validating {} commitment in channel {}", self.received_msg(), context.channel_id());
2536-
if context.holder_signer.as_ref().validate_holder_commitment(
2537-
&self.funding().channel_transaction_parameters,
2538-
&holder_commitment_tx,
2539-
Vec::new(),
2540-
&context.secp_ctx,
2541-
).is_err() {
2545+
log_trace!(
2546+
logger,
2547+
"Validating {} commitment in channel {}",
2548+
self.received_msg(),
2549+
context.channel_id()
2550+
);
2551+
if context
2552+
.holder_signer
2553+
.as_ref()
2554+
.validate_holder_commitment(
2555+
&self.funding().channel_transaction_parameters,
2556+
&holder_commitment_tx,
2557+
Vec::new(),
2558+
&context.secp_ctx,
2559+
)
2560+
.is_err()
2561+
{
25422562
if !self.funding().is_outbound() {
25432563
self.funding_mut().channel_transaction_parameters.funding_outpoint = None;
25442564
}
25452565
return Err(ChannelError::close("Failed to validate our commitment".to_owned()));
25462566
}
25472567

2548-
let commitment_data = context.build_commitment_transaction(self.funding(),
2568+
let commitment_data = context.build_commitment_transaction(
2569+
self.funding(),
25492570
context.cur_counterparty_commitment_transaction_number,
2550-
&context.counterparty_cur_commitment_point.unwrap(), false, false, logger);
2571+
&context.counterparty_cur_commitment_point.unwrap(),
2572+
false,
2573+
false,
2574+
logger,
2575+
);
25512576
let counterparty_initial_commitment_tx = commitment_data.tx;
25522577
let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
25532578
let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
25542579

2555-
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2556-
&context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2580+
log_trace!(
2581+
logger,
2582+
"Initial counterparty tx for channel {} is: txid {} tx {}",
2583+
&context.channel_id(),
2584+
counterparty_initial_bitcoin_tx.txid,
2585+
encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction)
2586+
);
25572587

25582588
// Now that we're past error-generating stuff, update our local state:
25592589

@@ -2564,35 +2594,61 @@ where
25642594
assert!(!context.channel_state.is_monitor_update_in_progress()); // We have not had any monitor(s) yet to fail update!
25652595
if !is_v2_established {
25662596
if context.is_batch_funding() {
2567-
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::WAITING_FOR_BATCH);
2597+
context.channel_state = ChannelState::AwaitingChannelReady(
2598+
AwaitingChannelReadyFlags::WAITING_FOR_BATCH,
2599+
);
25682600
} else {
2569-
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2601+
context.channel_state =
2602+
ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
25702603
}
25712604
}
2572-
if holder_commitment_point.advance(&context.holder_signer, &context.secp_ctx, logger).is_err() {
2605+
if holder_commitment_point
2606+
.advance(&context.holder_signer, &context.secp_ctx, logger)
2607+
.is_err()
2608+
{
25732609
// We only fail to advance our commitment point/number if we're currently
25742610
// waiting for our signer to unblock and provide a commitment point.
25752611
// We cannot send accept_channel/open_channel before this has occurred, so if we
25762612
// err here by the time we receive funding_created/funding_signed, something has gone wrong.
2577-
debug_assert!(false, "We should be ready to advance our commitment point by the time we receive {}", self.received_msg());
2578-
return Err(ChannelError::close("Failed to advance holder commitment point".to_owned()));
2613+
debug_assert!(
2614+
false,
2615+
"We should be ready to advance our commitment point by the time we receive {}",
2616+
self.received_msg()
2617+
);
2618+
return Err(ChannelError::close(
2619+
"Failed to advance holder commitment point".to_owned(),
2620+
));
25792621
}
25802622

25812623
let context = self.context();
25822624
let funding = self.funding();
2583-
let obscure_factor = get_commitment_transaction_number_obscure_factor(&funding.get_holder_pubkeys().payment_point, &funding.get_counterparty_pubkeys().payment_point, funding.is_outbound());
2584-
let shutdown_script = context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
2625+
let obscure_factor = get_commitment_transaction_number_obscure_factor(
2626+
&funding.get_holder_pubkeys().payment_point,
2627+
&funding.get_counterparty_pubkeys().payment_point,
2628+
funding.is_outbound(),
2629+
);
2630+
let shutdown_script =
2631+
context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
25852632
let monitor_signer = signer_provider.derive_channel_signer(context.channel_keys_id);
25862633
// TODO(RBF): When implementing RBF, the funding_txo passed here must only update
25872634
// ChannelMonitorImp::first_confirmed_funding_txo during channel establishment, not splicing
25882635
let channel_monitor = ChannelMonitor::new(
2589-
context.secp_ctx.clone(), monitor_signer, shutdown_script,
2590-
funding.get_holder_selected_contest_delay(), &context.destination_script,
2591-
&funding.channel_transaction_parameters, funding.is_outbound(), obscure_factor,
2592-
holder_commitment_tx, best_block, context.counterparty_node_id, context.channel_id(),
2636+
context.secp_ctx.clone(),
2637+
monitor_signer,
2638+
shutdown_script,
2639+
funding.get_holder_selected_contest_delay(),
2640+
&context.destination_script,
2641+
&funding.channel_transaction_parameters,
2642+
funding.is_outbound(),
2643+
obscure_factor,
2644+
holder_commitment_tx,
2645+
best_block,
2646+
context.counterparty_node_id,
2647+
context.channel_id(),
25932648
);
25942649
channel_monitor.provide_initial_counterparty_commitment_tx(
2595-
counterparty_initial_commitment_tx.clone(), logger,
2650+
counterparty_initial_commitment_tx.clone(),
2651+
logger,
25962652
);
25972653

25982654
self.context_mut().cur_counterparty_commitment_transaction_number -= 1;
@@ -4173,71 +4229,100 @@ where
41734229
Ok(())
41744230
}
41754231

4176-
#[rustfmt::skip]
41774232
fn validate_commitment_signed<L: Deref>(
41784233
&self, funding: &FundingScope, holder_commitment_point: &HolderCommitmentPoint,
41794234
msg: &msgs::CommitmentSigned, logger: &L,
41804235
) -> Result<LatestHolderCommitmentTXInfo, ChannelError>
41814236
where
41824237
L::Target: Logger,
41834238
{
4184-
let commitment_data = self.build_commitment_transaction(funding,
4185-
holder_commitment_point.transaction_number(), &holder_commitment_point.current_point(),
4186-
true, false, logger);
4239+
let commitment_data = self.build_commitment_transaction(
4240+
funding,
4241+
holder_commitment_point.transaction_number(),
4242+
&holder_commitment_point.current_point(),
4243+
true,
4244+
false,
4245+
logger,
4246+
);
41874247

41884248
if msg.htlc_signatures.len() != commitment_data.tx.nondust_htlcs().len() {
4189-
return Err(ChannelError::close(format!("Got wrong number of HTLC signatures ({}) from remote. It must be {}", msg.htlc_signatures.len(), commitment_data.tx.nondust_htlcs().len())));
4249+
return Err(ChannelError::close(format!(
4250+
"Got wrong number of HTLC signatures ({}) from remote. It must be {}",
4251+
msg.htlc_signatures.len(),
4252+
commitment_data.tx.nondust_htlcs().len()
4253+
)));
41904254
}
41914255

41924256
let holder_commitment_tx = HolderCommitmentTransaction::new(
41934257
commitment_data.tx,
41944258
msg.signature,
41954259
msg.htlc_signatures.clone(),
41964260
&funding.get_holder_pubkeys().funding_pubkey,
4197-
funding.counterparty_funding_pubkey()
4261+
funding.counterparty_funding_pubkey(),
41984262
);
41994263

4200-
log_trace!(logger, "Validating commitment_signed commitment in channel {}", self.channel_id());
4201-
self.holder_signer.as_ref().validate_holder_commitment(
4202-
&funding.channel_transaction_parameters,
4203-
&holder_commitment_tx,
4204-
commitment_data.outbound_htlc_preimages,
4205-
&self.secp_ctx,
4206-
)
4264+
log_trace!(
4265+
logger,
4266+
"Validating commitment_signed commitment in channel {}",
4267+
self.channel_id()
4268+
);
4269+
self.holder_signer
4270+
.as_ref()
4271+
.validate_holder_commitment(
4272+
&funding.channel_transaction_parameters,
4273+
&holder_commitment_tx,
4274+
commitment_data.outbound_htlc_preimages,
4275+
&self.secp_ctx,
4276+
)
42074277
.map_err(|_| ChannelError::close("Failed to validate our commitment".to_owned()))?;
42084278

42094279
// If our counterparty updated the channel fee in this commitment transaction, check that
42104280
// they can actually afford the new fee now.
42114281
let update_fee = if let Some((_, update_state)) = self.pending_update_fee {
42124282
update_state == FeeUpdateState::RemoteAnnounced
4213-
} else { false };
4283+
} else {
4284+
false
4285+
};
42144286
if update_fee {
42154287
debug_assert!(!funding.is_outbound());
4216-
let counterparty_reserve_we_require_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
4217-
if commitment_data.stats.remote_balance_before_fee_anchors_msat < commitment_data.stats.total_fee_sat * 1000 + commitment_data.stats.total_anchors_sat * 1000 + counterparty_reserve_we_require_msat {
4218-
return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
4288+
let counterparty_reserve_we_require_msat =
4289+
funding.holder_selected_channel_reserve_satoshis * 1000;
4290+
if commitment_data.stats.remote_balance_before_fee_anchors_msat
4291+
< commitment_data.stats.total_fee_sat * 1000
4292+
+ commitment_data.stats.total_anchors_sat * 1000
4293+
+ counterparty_reserve_we_require_msat
4294+
{
4295+
return Err(ChannelError::close(
4296+
"Funding remote cannot afford proposed new fee".to_owned(),
4297+
));
42194298
}
42204299
}
42214300
#[cfg(any(test, fuzzing))]
42224301
{
42234302
if funding.is_outbound() {
4224-
let projected_commit_tx_info = funding.next_local_commitment_tx_fee_info_cached.lock().unwrap().take();
4303+
let projected_commit_tx_info =
4304+
funding.next_local_commitment_tx_fee_info_cached.lock().unwrap().take();
42254305
*funding.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
42264306
if let Some(info) = projected_commit_tx_info {
4227-
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
4307+
let total_pending_htlcs = self.pending_inbound_htlcs.len()
4308+
+ self.pending_outbound_htlcs.len()
42284309
+ self.holding_cell_htlc_updates.len();
42294310
if info.total_pending_htlcs == total_pending_htlcs
42304311
&& info.next_holder_htlc_id == self.next_holder_htlc_id
42314312
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
4232-
&& info.feerate == self.feerate_per_kw {
4233-
assert_eq!(commitment_data.stats.total_fee_sat, info.fee / 1000);
4234-
}
4313+
&& info.feerate == self.feerate_per_kw
4314+
{
4315+
assert_eq!(commitment_data.stats.total_fee_sat, info.fee / 1000);
4316+
}
42354317
}
42364318
}
42374319
}
42384320

4239-
let mut nondust_htlc_sources = Vec::with_capacity(holder_commitment_tx.nondust_htlcs().len());
4240-
let mut dust_htlcs = Vec::with_capacity(commitment_data.htlcs_included.len() - holder_commitment_tx.nondust_htlcs().len());
4321+
let mut nondust_htlc_sources =
4322+
Vec::with_capacity(holder_commitment_tx.nondust_htlcs().len());
4323+
let mut dust_htlcs = Vec::with_capacity(
4324+
commitment_data.htlcs_included.len() - holder_commitment_tx.nondust_htlcs().len(),
4325+
);
42414326
for (htlc, mut source_opt) in commitment_data.htlcs_included.into_iter() {
42424327
if let Some(_) = htlc.transaction_output_index {
42434328
if htlc.offered {

0 commit comments

Comments
 (0)