From 455d5df7590dcac966423f12dbd3ce87c13e1bdf Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 17 Jul 2020 14:58:48 +0930 Subject: [PATCH] sendpsbt: annotate transactions in wallet. This is what txsend does, only we have a psbt so we have to change the db interface to take a wally_tx. Signed-off-by: Rusty Russell --- lightningd/chaintopology.c | 10 +++++----- lightningd/onchain_control.c | 2 +- lightningd/peer_control.c | 2 +- lightningd/test/run-invoice-select-inchan.c | 2 +- wallet/db.c | 4 ++-- wallet/db.h | 2 +- wallet/wallet.c | 4 ++-- wallet/wallet.h | 2 +- wallet/walletrpc.c | 11 +++++++++-- 9 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 5eb72591734b..fd058d3c367a 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -83,7 +83,7 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) txo = txowatch_hash_get(&topo->txowatches, &out); if (txo) { wallet_transaction_add(topo->ld->wallet, - tx, b->height, i); + tx->wtx, b->height, i); txowatch_fire(txo, tx, j, b); } } @@ -93,14 +93,14 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) if (txfilter_match(topo->bitcoind->ld->owned_txfilter, tx)) { wallet_extract_owned_outputs(topo->bitcoind->ld->wallet, tx->wtx, &b->height, &owned); - wallet_transaction_add(topo->ld->wallet, tx, b->height, - i); + wallet_transaction_add(topo->ld->wallet, tx->wtx, + b->height, i); } /* We did spends first, in case that tells us to watch tx. */ if (watching_txid(topo, &txid) || we_broadcast(topo, &txid)) { wallet_transaction_add(topo->ld->wallet, - tx, b->height, i); + tx->wtx, b->height, i); } txwatch_inform(topo, &txid, tx); @@ -235,7 +235,7 @@ void broadcast_tx(struct chain_topology *topo, log_debug(topo->log, "Broadcasting txid %s", type_to_string(tmpctx, struct bitcoin_txid, &otx->txid)); - wallet_transaction_add(topo->ld->wallet, tx, 0, 0); + wallet_transaction_add(topo->ld->wallet, tx->wtx, 0, 0); bitcoind_sendrawtx(topo->bitcoind, otx->hextx, broadcast_done, otx); } diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 83ac6468ff48..8632667dca4c 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -229,7 +229,7 @@ static void handle_onchain_broadcast_tx(struct channel *channel, const u8 *msg) tx->chainparams = chainparams; bitcoin_txid(tx, &txid); - wallet_transaction_add(w, tx, 0, 0); + wallet_transaction_add(w, tx->wtx, 0, 0); wallet_transaction_annotate(w, &txid, type, channel->dbid); /* We don't really care if it fails, we'll respond via watch. */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 6cf21234acaf..f491efbb3845 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -356,7 +356,7 @@ void drop_to_chain(struct lightningd *ld, struct channel *channel, } else { sign_last_tx(channel); bitcoin_txid(channel->last_tx, &txid); - wallet_transaction_add(ld->wallet, channel->last_tx, 0, 0); + wallet_transaction_add(ld->wallet, channel->last_tx->wtx, 0, 0); wallet_transaction_annotate(ld->wallet, &txid, channel->last_tx_type, channel->dbid); /* Keep broadcasting until we say stop (can fail due to dup, diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index e7d426b0c0d5..e51f2ebb5b3f 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -619,7 +619,7 @@ void wallet_peer_delete(struct wallet *w UNNEEDED, u64 peer_dbid UNNEEDED) struct amount_msat wallet_total_forward_fees(struct wallet *w UNNEEDED) { fprintf(stderr, "wallet_total_forward_fees called!\n"); abort(); } /* Generated stub for wallet_transaction_add */ -void wallet_transaction_add(struct wallet *w UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, +void wallet_transaction_add(struct wallet *w UNNEEDED, const struct wally_tx *tx UNNEEDED, const u32 blockheight UNNEEDED, const u32 txindex UNNEEDED) { fprintf(stderr, "wallet_transaction_add called!\n"); abort(); } /* Generated stub for wallet_transaction_annotate */ diff --git a/wallet/db.c b/wallet/db.c index b3cca1b9ea30..56faffb13618 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1339,9 +1339,9 @@ void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t) db_bind_u64(stmt, col, timestamp); } -void db_bind_tx(struct db_stmt *stmt, int col, const struct bitcoin_tx *tx) +void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx) { - u8 *ser = linearize_tx(stmt, tx); + u8 *ser = linearize_wtx(stmt, tx); assert(ser); db_bind_blob(stmt, col, ser, tal_count(ser)); } diff --git a/wallet/db.h b/wallet/db.h index f8b6c112e999..1104109cb0c4 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -116,7 +116,7 @@ void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col, void db_bind_signature(struct db_stmt *stmt, int col, const secp256k1_ecdsa_signature *sig); void db_bind_timeabs(struct db_stmt *stmt, int col, struct timeabs t); -void db_bind_tx(struct db_stmt *stmt, int col, const struct bitcoin_tx *tx); +void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx); void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt); void db_bind_amount_msat(struct db_stmt *stmt, int pos, const struct amount_msat *msat); diff --git a/wallet/wallet.c b/wallet/wallet.c index 11e3c1597f9b..1f0c30299c00 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -3376,14 +3376,14 @@ struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx, return op; } -void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx, +void wallet_transaction_add(struct wallet *w, const struct wally_tx *tx, const u32 blockheight, const u32 txindex) { struct bitcoin_txid txid; struct db_stmt *stmt = db_prepare_v2( w->db, SQL("SELECT blockheight FROM transactions WHERE id=?")); - bitcoin_txid(tx, &txid); + wally_txid(tx, &txid); db_bind_txid(stmt, 0, &txid); db_query_prepared(stmt); diff --git a/wallet/wallet.h b/wallet/wallet.h index 690bd7272606..4d83b70060de 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1164,7 +1164,7 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, const u32 txindex, const u8 *scriptpubkey, struct amount_sat sat); -void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx, +void wallet_transaction_add(struct wallet *w, const struct wally_tx *tx, const u32 blockheight, const u32 txindex); void wallet_annotate_txout(struct wallet *w, const struct bitcoin_txid *txid, diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 5b9e63b05ab4..afaeb4980120 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -543,7 +543,7 @@ static struct command_result *json_txsend(struct command *cmd, /* We're the owning cmd now. */ utx->wtx->cmd = cmd; - wallet_transaction_add(cmd->ld->wallet, utx->tx, 0, 0); + wallet_transaction_add(cmd->ld->wallet, utx->tx->wtx, 0, 0); wallet_transaction_annotate(cmd->ld->wallet, &utx->txid, TX_UNKNOWN, 0); @@ -610,7 +610,7 @@ static struct command_result *json_withdraw(struct command *cmd, return res; /* Store the transaction in the DB and annotate it as a withdrawal */ - wallet_transaction_add(cmd->ld->wallet, utx->tx, 0, 0); + wallet_transaction_add(cmd->ld->wallet, utx->tx->wtx, 0, 0); wallet_transaction_annotate(cmd->ld->wallet, &utx->txid, TX_WALLET_WITHDRAWAL, 0); @@ -1329,6 +1329,7 @@ static struct command_result *json_sendpsbt(struct command *cmd, struct wally_tx *w_tx; struct tx_broadcast *txb; struct utxo **utxos; + struct bitcoin_txid txid; if (!param(cmd, buffer, params, p_req("psbt", param_psbt, &psbt), @@ -1356,6 +1357,12 @@ static struct command_result *json_sendpsbt(struct command *cmd, txb->cmd = cmd; txb->expected_change = NULL; + /* FIXME: Do this *after* successful broadcast! */ + wallet_transaction_add(cmd->ld->wallet, txb->wtx, 0, 0); + wally_txid(txb->wtx, &txid); + wallet_transaction_annotate(cmd->ld->wallet, &txid, + TX_UNKNOWN, 0); + /* Now broadcast the transaction */ bitcoind_sendrawtx(cmd->ld->topology->bitcoind, tal_hex(tmpctx, linearize_wtx(tmpctx, w_tx)),