Skip to content

Commit

Permalink
sendpsbt: annotate transactions in wallet.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell committed Jul 17, 2020
1 parent ec8db08 commit 455d5df
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 455d5df

Please sign in to comment.