Skip to content

Commit 4efc1b5

Browse files
committed
f correctly figure out the to-self value
1 parent ddf777b commit 4efc1b5

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
@@ -1363,17 +1363,15 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13631363
let us = self.inner.lock().unwrap();
13641364

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

13791377
macro_rules! walk_htlcs {
@@ -1412,19 +1410,65 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14121410
}
14131411

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

0 commit comments

Comments
 (0)