Skip to content

Commit db1c462

Browse files
committed
f correctly figure out the to-self value
1 parent c2b1a9d commit db1c462

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,17 +1361,15 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13611361
let us = self.inner.lock().unwrap();
13621362

13631363
let mut confirmed_txid = us.funding_spend_confirmed;
1364+
let mut pending_commitment_tx_conf_thresh = None;
13641365
if let Some((txid, conf_thresh)) = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
13651366
if let OnchainEvent::FundingSpendConfirmation { txid, .. } = event.event {
13661367
Some((txid, event.confirmation_threshold()))
13671368
} else { None }
13681369
}) {
13691370
debug_assert!(us.funding_spend_confirmed.is_none(), "We have a pending funding spend awaiting confirmation, we can't have confirmed it already!");
13701371
confirmed_txid = Some(txid);
1371-
res.push(ClaimableBalance::ClaimableAwaitingConfirmations {
1372-
claimable_amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
1373-
confirmation_height: conf_thresh,
1374-
});
1372+
pending_commitment_tx_conf_thresh = Some(conf_thresh);
13751373
}
13761374

13771375
macro_rules! walk_htlcs {
@@ -1410,19 +1408,65 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14101408
}
14111409

14121410
if let Some(txid) = confirmed_txid {
1411+
let mut found_commitment_tx = false;
14131412
if Some(txid) == us.current_counterparty_commitment_txid || Some(txid) == us.prev_counterparty_commitment_txid {
14141413
walk_htlcs!(false, us.counterparty_claimable_outpoints.get(&txid).unwrap().iter().map(|(a, _)| a));
1414+
if let Some(conf_thresh) = pending_commitment_tx_conf_thresh {
1415+
if let Some(value) = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1416+
if let OnchainEvent::MaturingOutput {
1417+
descriptor: SpendableOutputDescriptor::StaticPaymentOutput(descriptor)
1418+
} = &event.event {
1419+
Some(descriptor.output.value)
1420+
} else { None }
1421+
}) {
1422+
res.push(ClaimableBalance::ClaimableAwaitingConfirmations {
1423+
claimable_amount_satoshis: value,
1424+
confirmation_height: conf_thresh,
1425+
});
1426+
} else {
1427+
// If a counterparty commitment transaction is awaiting confirmation, we
1428+
// should also have a StaticPaymentOutput MaturingOutput event awaiting
1429+
// confirmation with the same height. Not having one implies something has
1430+
// gone terribly wrong with our commitment txid tracking.
1431+
debug_assert!(false);
1432+
}
1433+
}
1434+
found_commitment_tx = true;
14151435
} else if txid == us.current_holder_commitment_tx.txid {
14161436
walk_htlcs!(true, us.current_holder_commitment_tx.htlc_outputs.iter().map(|(a, _, _)| a));
1437+
if let Some(conf_thresh) = pending_commitment_tx_conf_thresh {
1438+
res.push(ClaimableBalance::ClaimableAwaitingConfirmations {
1439+
claimable_amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
1440+
confirmation_height: conf_thresh,
1441+
});
1442+
}
1443+
found_commitment_tx = true;
14171444
} else if let Some(prev_commitment) = &us.prev_holder_signed_commitment_tx {
14181445
if txid == prev_commitment.txid {
14191446
walk_htlcs!(true, prev_commitment.htlc_outputs.iter().map(|(a, _, _)| a));
1447+
if let Some(conf_thresh) = pending_commitment_tx_conf_thresh {
1448+
res.push(ClaimableBalance::ClaimableAwaitingConfirmations {
1449+
claimable_amount_satoshis: prev_commitment.to_self_value_sat,
1450+
confirmation_height: conf_thresh,
1451+
});
1452+
}
1453+
found_commitment_tx = true;
1454+
}
1455+
}
1456+
if !found_commitment_tx {
1457+
if let Some(conf_thresh) = pending_commitment_tx_conf_thresh {
1458+
// We blindly assume this is a cooperative close transaction here, and that the
1459+
// counterparty didn't misbehave. At worst we've over-estimated the amount we can
1460+
// claim as we'll punish a misbehaving counterparty (as long as we didn't
1461+
// misbehave).
1462+
res.push(ClaimableBalance::ClaimableAwaitingConfirmations {
1463+
claimable_amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
1464+
confirmation_height: conf_thresh,
1465+
});
14201466
}
14211467
}
14221468
// TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
14231469
// outputs.
1424-
// Otherwise assume we closed with a cooperative close which only needs the
1425-
// `ClaimableAwaitingConfirmations` balance pushed first.
14261470
} else {
14271471
let mut claimable_inbound_htlc_value_sat = 0;
14281472
for (htlc, _, _) in us.current_holder_commitment_tx.htlc_outputs.iter() {

0 commit comments

Comments
 (0)