Fix matching of second-stage HTLC claim in get_htlc_balance
authorMatt Corallo <git@bluematt.me>
Sun, 24 Sep 2023 02:32:08 +0000 (02:32 +0000)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Fri, 29 Sep 2023 23:24:39 +0000 (16:24 -0700)
commit0930be3304efab510e242581d8d1968ab0b0652a
treee10fc56daedc6c01aea56e595ee5fa2204af53fb
parent620244dc2ec3153a61e009b80a8c59cf41514482
Fix matching of second-stage HTLC claim in get_htlc_balance

We incorrectly assumed that the descriptor's output index from
second-stage HTLC transaction would always match the HTLC's output index
in the commitment transaction. This doesn't make any sense though, we
need to make sure we map the descriptor to it's corresponding HTLC in
the commitment. Instead, we check that the transaction from which the
descriptor originated from spends the HTLC in question.

Note that pre-anchors, second-stage HTLC transactions are always 1
input-1 output, so previously we would only match if the HTLC was the
first output in the commitment transaction. Post-anchors, they are
malleable, so  we can aggregate multiple HTLC claims into a single
transaction making this even more likely to happen. Unfortunately, we
lacked proper coverage in this area so the bug went unnoticed. To
address this, we aim to extend our existing coverage of
`get_claimable_balances` to anchor outputs channels in the following
commits.
lightning/src/chain/channelmonitor.rs