From ff4a2bf38fa6fa25687bda3983c02ba32a023f0b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 4 Oct 2019 12:27:48 +0200 Subject: [PATCH] onchaind: Annotate inputs and outputs not the transactions --- bitcoin/tx.c | 2 +- lightningd/onchain_control.c | 30 ++++++++++++++++---- onchaind/onchain_wire.csv | 13 +++++++-- onchaind/onchaind.c | 40 +++++++++++++++++---------- onchaind/test/run-grind_feerate-bug.c | 9 ++++-- onchaind/test/run-grind_feerate.c | 9 ++++-- 6 files changed, 73 insertions(+), 30 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 2ea1b16f9..3e11a2b55 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -428,7 +428,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, /* We don't know the input amounts yet, so set them all to NULL */ tx->input_amounts = tal_arrz(tx, struct amount_sat *, tx->wtx->inputs_allocation_len); - tx->chainparams = NULL; + tx->chainparams = chainparams; *cursor += wsize; *max -= wsize; diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 7673cc64b..00776fc4d 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -306,15 +306,29 @@ static void onchain_add_utxo(struct channel *channel, const u8 *msg) wallet_add_utxo(channel->peer->ld->wallet, u, p2wpkh); } -static void onchain_transaction_annotate(struct channel *channel, const u8 *msg) +static void onchain_annotate_txout(struct channel *channel, const u8 *msg) { struct bitcoin_txid txid; enum wallet_tx_type type; - if (!fromwire_onchain_transaction_annotate(msg, &txid, &type)) - fatal("onchaind gave invalid onchain_transaction_annotate " + u32 outnum; + if (!fromwire_onchain_annotate_txout(msg, &txid, &outnum, &type)) + fatal("onchaind gave invalid onchain_annotate_txout " "message: %s", tal_hex(msg, msg)); - wallet_transaction_annotate(channel->peer->ld->wallet, &txid, type, + wallet_annotate_txout(channel->peer->ld->wallet, &txid, outnum, type, + channel->dbid); +} + +static void onchain_annotate_txin(struct channel *channel, const u8 *msg) +{ + struct bitcoin_txid txid; + enum wallet_tx_type type; + u32 innum; + if (!fromwire_onchain_annotate_txin(msg, &txid, &innum, &type)) + fatal("onchaind gave invalid onchain_annotate_txin " + "message: %s", + tal_hex(msg, msg)); + wallet_annotate_txin(channel->peer->ld->wallet, &txid, innum, type, channel->dbid); } @@ -355,8 +369,12 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds U onchain_add_utxo(sd->channel, msg); break; - case WIRE_ONCHAIN_TRANSACTION_ANNOTATE: - onchain_transaction_annotate(sd->channel, msg); + case WIRE_ONCHAIN_ANNOTATE_TXIN: + onchain_annotate_txin(sd->channel, msg); + break; + + case WIRE_ONCHAIN_ANNOTATE_TXOUT: + onchain_annotate_txout(sd->channel, msg); break; /* We send these, not receive them */ diff --git a/onchaind/onchain_wire.csv b/onchaind/onchain_wire.csv index 56bafa8bf..c4d9edf7c 100644 --- a/onchaind/onchain_wire.csv +++ b/onchaind/onchain_wire.csv @@ -108,6 +108,13 @@ msgdata,onchain_dev_memleak_reply,leak,bool, # Tell the main daemon what we've been watching, mainly used for transactions # that we tracked automatically but only onchaind knows how to classify their # transactions. -msgtype,onchain_transaction_annotate,5034 -msgdata,onchain_transaction_annotate,txid,bitcoin_txid, -msgdata,onchain_transaction_annotate,type,enum wallet_tx_type, +msgtype,onchain_annotate_txout,5035 +msgdata,onchain_annotate_txout,txid,bitcoin_txid, +msgdata,onchain_annotate_txout,outnum,u32, +msgdata,onchain_annotate_txout,type,enum wallet_tx_type, + +msgtype,onchain_annotate_txin,5036 +msgdata,onchain_annotate_txin,txid,bitcoin_txid, +msgdata,onchain_annotate_txin,innum,u32, +msgdata,onchain_annotate_txin,type,enum wallet_tx_type, + diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index b391751f8..54b396e09 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -1010,12 +1010,20 @@ static void steal_htlc_tx(struct tracked_output *out) propose_resolution(out, tx, 0, tx_type); } -static void onchain_transaction_annotate(const struct bitcoin_txid *txid, - enum wallet_tx_type type) +static void onchain_annotate_txout(const struct bitcoin_txid *txid, u32 outnum, + enum wallet_tx_type type) { - u8 *msg = towire_onchain_transaction_annotate(tmpctx, txid, type); - wire_sync_write(REQ_FD, take(msg)); + wire_sync_write(REQ_FD, take(towire_onchain_annotate_txout( + tmpctx, txid, outnum, type))); } + +static void onchain_annotate_txin(const struct bitcoin_txid *txid, u32 innum, + enum wallet_tx_type type) +{ + wire_sync_write(REQ_FD, take(towire_onchain_annotate_txin( + tmpctx, txid, innum, type))); +} + /* An output has been spent: see if it resolves something we care about. */ static void output_spent(const struct chainparams *chainparams, struct tracked_output ***outs, @@ -1062,8 +1070,8 @@ static void output_spent(const struct chainparams *chainparams, } else { /* We ignore this timeout tx, since we should * resolve by ignoring once we reach depth. */ - onchain_transaction_annotate( - &spendertxid, + onchain_annotate_txout( + &spendertxid, out->outnum, TX_CHANNEL_HTLC_TIMEOUT | TX_THEIRS); } break; @@ -1097,8 +1105,8 @@ static void output_spent(const struct chainparams *chainparams, * output is considered *irrevocably resolved* */ ignore_output(out); - onchain_transaction_annotate( - &spendertxid, + onchain_annotate_txout( + &spendertxid, out->outnum, TX_CHANNEL_HTLC_SUCCESS | TX_THEIRS); } break; @@ -1434,7 +1442,10 @@ static void handle_mutual_close(const struct chainparams *chainparams, struct tracked_output **outs) { init_reply("Tracking mutual close transaction"); - onchain_transaction_annotate(txid, TX_CHANNEL_CLOSE); + + /* Annotate the first input as close. We can currently only have a + * single input for these. */ + onchain_annotate_txin(txid, 0, TX_CHANNEL_CLOSE); /* BOLT #5: * @@ -1732,7 +1743,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, size_t i; init_reply("Tracking our own unilateral close"); - onchain_transaction_annotate(txid, TX_CHANNEL_UNILATERAL); + onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL); /* BOLT #5: * @@ -1898,7 +1909,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, matches = match_htlc_output(tmpctx, tx, i, htlc_scripts); /* FIXME: limp along when this happens! */ if (tal_count(matches) == 0) { - onchain_transaction_annotate(txid, TX_CHANNEL_PENALTY | TX_THEIRS); + onchain_annotate_txout(txid, i, TX_CHANNEL_PENALTY | TX_THEIRS); status_failed(STATUS_FAIL_INTERNAL_ERROR, "Could not find resolution for output %zu", i); @@ -2065,7 +2076,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, size_t i; init_reply("Tracking their illegal close: taking all funds"); - onchain_transaction_annotate(txid, TX_CHANNEL_UNILATERAL | TX_CHANNEL_CHEAT | TX_THEIRS); + onchain_annotate_txin( + txid, 0, TX_CHANNEL_UNILATERAL | TX_CHANNEL_CHEAT | TX_THEIRS); /* BOLT #5: * @@ -2308,7 +2320,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, size_t i; init_reply("Tracking their unilateral close"); - onchain_transaction_annotate(txid, TX_CHANNEL_UNILATERAL | TX_THEIRS); + onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL | TX_THEIRS); /* HSM can't derive this. */ remote_per_commitment_point = this_remote_per_commitment_point; @@ -2540,7 +2552,7 @@ static void handle_unknown_commitment(const struct bitcoin_tx *tx, int to_us_output = -1; u8 *local_script; - onchain_transaction_annotate(txid, TX_CHANNEL_UNILATERAL | TX_THEIRS); + onchain_annotate_txin(txid, 0, TX_CHANNEL_UNILATERAL | TX_THEIRS); resolved_by_other(outs[0], txid, UNKNOWN_UNILATERAL); diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index aeb0e95cd..ae8102bdf 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -137,6 +137,12 @@ u8 *towire_onchain_add_utxo(const tal_t *ctx UNNEEDED, const struct bitcoin_txid /* Generated stub for towire_onchain_all_irrevocably_resolved */ u8 *towire_onchain_all_irrevocably_resolved(const tal_t *ctx UNNEEDED) { fprintf(stderr, "towire_onchain_all_irrevocably_resolved called!\n"); abort(); } +/* Generated stub for towire_onchain_annotate_txin */ +u8 *towire_onchain_annotate_txin(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, u32 innum UNNEEDED, enum wallet_tx_type type UNNEEDED) +{ fprintf(stderr, "towire_onchain_annotate_txin called!\n"); abort(); } +/* Generated stub for towire_onchain_annotate_txout */ +u8 *towire_onchain_annotate_txout(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, u32 outnum UNNEEDED, enum wallet_tx_type type UNNEEDED) +{ fprintf(stderr, "towire_onchain_annotate_txout called!\n"); abort(); } /* Generated stub for towire_onchain_broadcast_tx */ u8 *towire_onchain_broadcast_tx(const tal_t *ctx UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, enum wallet_tx_type type UNNEEDED) { fprintf(stderr, "towire_onchain_broadcast_tx called!\n"); abort(); } @@ -155,9 +161,6 @@ u8 *towire_onchain_init_reply(const tal_t *ctx UNNEEDED) /* Generated stub for towire_onchain_missing_htlc_output */ u8 *towire_onchain_missing_htlc_output(const tal_t *ctx UNNEEDED, const struct htlc_stub *htlc UNNEEDED) { fprintf(stderr, "towire_onchain_missing_htlc_output called!\n"); abort(); } -/* Generated stub for towire_onchain_transaction_annotate */ -u8 *towire_onchain_transaction_annotate(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, enum wallet_tx_type type UNNEEDED) -{ fprintf(stderr, "towire_onchain_transaction_annotate called!\n"); abort(); } /* Generated stub for towire_onchain_unwatch_tx */ u8 *towire_onchain_unwatch_tx(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED) { fprintf(stderr, "towire_onchain_unwatch_tx called!\n"); abort(); } diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 486199594..a4c32238f 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -153,6 +153,12 @@ u8 *towire_onchain_add_utxo(const tal_t *ctx UNNEEDED, const struct bitcoin_txid /* Generated stub for towire_onchain_all_irrevocably_resolved */ u8 *towire_onchain_all_irrevocably_resolved(const tal_t *ctx UNNEEDED) { fprintf(stderr, "towire_onchain_all_irrevocably_resolved called!\n"); abort(); } +/* Generated stub for towire_onchain_annotate_txin */ +u8 *towire_onchain_annotate_txin(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, u32 innum UNNEEDED, enum wallet_tx_type type UNNEEDED) +{ fprintf(stderr, "towire_onchain_annotate_txin called!\n"); abort(); } +/* Generated stub for towire_onchain_annotate_txout */ +u8 *towire_onchain_annotate_txout(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, u32 outnum UNNEEDED, enum wallet_tx_type type UNNEEDED) +{ fprintf(stderr, "towire_onchain_annotate_txout called!\n"); abort(); } /* Generated stub for towire_onchain_broadcast_tx */ u8 *towire_onchain_broadcast_tx(const tal_t *ctx UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, enum wallet_tx_type type UNNEEDED) { fprintf(stderr, "towire_onchain_broadcast_tx called!\n"); abort(); } @@ -171,9 +177,6 @@ u8 *towire_onchain_init_reply(const tal_t *ctx UNNEEDED) /* Generated stub for towire_onchain_missing_htlc_output */ u8 *towire_onchain_missing_htlc_output(const tal_t *ctx UNNEEDED, const struct htlc_stub *htlc UNNEEDED) { fprintf(stderr, "towire_onchain_missing_htlc_output called!\n"); abort(); } -/* Generated stub for towire_onchain_transaction_annotate */ -u8 *towire_onchain_transaction_annotate(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, enum wallet_tx_type type UNNEEDED) -{ fprintf(stderr, "towire_onchain_transaction_annotate called!\n"); abort(); } /* Generated stub for towire_onchain_unwatch_tx */ u8 *towire_onchain_unwatch_tx(const tal_t *ctx UNNEEDED, const struct bitcoin_txid *txid UNNEEDED) { fprintf(stderr, "towire_onchain_unwatch_tx called!\n"); abort(); }