From ecb19ba6f2cfe06efe4934a2b26722959838db9c Mon Sep 17 00:00:00 2001 From: niftynei Date: Wed, 16 Feb 2022 15:16:07 -0600 Subject: [PATCH] coin_mvt: report mutual close outputs also It's better to report every single utxo on close so we know when to mark a channel account as definitively closed. --- onchaind/onchaind.c | 31 +++++++++++++++++++++++++++++-- tests/test_closing.py | 4 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 1a7ee7499..d210590ad 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -247,6 +247,31 @@ static void record_external_deposit(const struct tracked_output *out, record_external_output(&out->outpoint, out->sat, blockheight, tag); } +static void record_mutual_close(const struct tx_parts *tx, + const u8 *remote_scriptpubkey, + u32 blockheight) +{ + /* FIXME: if we ever change how closes happen, this will + * need to be updated as there's no longer 1 output + * per peer */ + for (size_t i = 0; i < tal_count(tx->outputs); i++) { + struct bitcoin_outpoint out; + + if (!wally_tx_output_scripteq(tx->outputs[i], + remote_scriptpubkey)) + continue; + + out.n = i; + out.txid = tx->txid; + record_external_output(&out, + amount_sat(tx->outputs[i]->satoshi), + blockheight, + TO_THEM); + break; + } +} + + static void record_channel_deposit(struct tracked_output *out, u32 blockheight, enum mvt_tag tag) { @@ -3882,9 +3907,11 @@ int main(int argc, char *argv[]) * without any pending payments) and publish it on the blockchain (see * [BOLT #2: Channel Close](02-peer-protocol.md#channel-close)). */ - if (is_mutual_close(tx, scriptpubkey[LOCAL], scriptpubkey[REMOTE])) + if (is_mutual_close(tx, scriptpubkey[LOCAL], scriptpubkey[REMOTE])) { + record_mutual_close(tx, scriptpubkey[REMOTE], + tx_blockheight); handle_mutual_close(outs, tx); - else { + } else { /* BOLT #5: * * 2. The bad way (*unilateral close*): something goes wrong, diff --git a/tests/test_closing.py b/tests/test_closing.py index 2975701ac..705cac755 100644 --- a/tests/test_closing.py +++ b/tests/test_closing.py @@ -108,12 +108,12 @@ def test_closing_simple(node_factory, bitcoind, chainparams): expected_1 = { '0': [('wallet', ['deposit'], ['withdrawal'], 'A')], 'A': [('wallet', ['deposit'], None, None), ('cid1', ['channel_open', 'opener'], ['channel_close'], 'B')], - 'B': [('wallet', ['deposit'], None, None)], + 'B': [('wallet', ['deposit'], None, None), ('external', ['to_them'], None, None)], } expected_2 = { 'A': [('cid1', ['channel_open'], ['channel_close'], 'B')], - 'B': [('wallet', ['deposit'], None, None)], + 'B': [('wallet', ['deposit'], None, None), ('external', ['to_them'], None, None)], } tags = check_utxos_channel(l1, [channel_id], expected_1) check_utxos_channel(l2, [channel_id], expected_2, tags)