psbt: move channels.last_tx field to be a psbt

note: missing migration at the moment lol
This commit is contained in:
niftynei 2020-05-21 22:18:33 -05:00 committed by Christian Decker
parent 052d40ae98
commit 8fa04a710a
5 changed files with 60 additions and 15 deletions

View File

@ -494,6 +494,38 @@ char *bitcoin_tx_to_psbt_base64(const tal_t *ctx, struct bitcoin_tx *tx)
return ret_val;
}
struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psbt STEALS)
{
struct wally_psbt *tmppsbt;
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams,
psbt->tx->num_inputs,
psbt->tx->num_outputs,
psbt->tx->locktime);
wally_tx_free(tx->wtx);
/* We want the 'finalized' tx since that includes any signature
* data, not the global tx. But 'finalizing' a tx destroys some fields
* so we 'clone' it first and then finalize it */
if (wally_psbt_clone(psbt, &tmppsbt) != WALLY_OK)
abort();
if (wally_finalize_psbt(tmppsbt) != WALLY_OK)
abort();
if (psbt_is_finalized(tmppsbt)) {
if (wally_extract_psbt(tmppsbt, &tx->wtx) != WALLY_OK)
abort();
} else if (wally_tx_clone(psbt->tx, &tx->wtx) != WALLY_OK)
abort();
wally_psbt_free(tmppsbt);
tal_free(tx->psbt);
tx->psbt = tal_steal(tx, psbt);
return tx;
}
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor,
size_t *max)
{

View File

@ -65,6 +65,9 @@ bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len);
/* Create a bitcoin_tx from a psbt */
struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psbt);
/* Internal de-linearization functions. */
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
const u8 **cursor, size_t *max);

View File

@ -1,5 +1,6 @@
#include "db.h"
#include <bitcoin/psbt.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/node_id.h>
@ -1253,6 +1254,14 @@ void db_bind_tx(struct db_stmt *stmt, int col, const struct bitcoin_tx *tx)
db_bind_blob(stmt, col, ser, tal_count(ser));
}
void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt)
{
size_t bytes_written;
const u8 *ser = psbt_get_bytes(stmt, psbt, &bytes_written);
assert(ser);
db_bind_blob(stmt, col, ser, bytes_written);
}
void db_bind_amount_msat(struct db_stmt *stmt, int pos,
const struct amount_msat *msat)
{
@ -1372,6 +1381,17 @@ struct bitcoin_tx *db_column_tx(const tal_t *ctx, struct db_stmt *stmt, int col)
return pull_bitcoin_tx(ctx, &src, &len);
}
struct bitcoin_tx *db_column_psbt_to_tx(const tal_t *ctx, struct db_stmt *stmt, int col)
{
struct wally_psbt *psbt;
const u8 *src = db_column_blob(stmt, col);
size_t len = db_column_bytes(stmt, col);
psbt = psbt_from_bytes(ctx, src, len);
if (!psbt)
return NULL;
return bitcoin_tx_with_psbt(ctx, psbt);
}
void *db_column_arr_(const tal_t *ctx, struct db_stmt *stmt, int col,
size_t bytes, const char *label, const char *caller)
{

View File

@ -21,6 +21,7 @@ struct node_id;
struct onionreply;
struct db_stmt;
struct db;
struct wally_psbt;
/**
* Macro to annotate a named SQL query.
@ -115,6 +116,7 @@ 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_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);
void db_bind_amount_sat(struct db_stmt *stmt, int pos,
@ -153,6 +155,7 @@ bool db_column_signature(struct db_stmt *stmt, int col,
secp256k1_ecdsa_signature *sig);
struct timeabs db_column_timeabs(struct db_stmt *stmt, int col);
struct bitcoin_tx *db_column_tx(const tal_t *ctx, struct db_stmt *stmt, int col);
struct bitcoin_tx *db_column_psbt_to_tx(const tal_t *ctx, struct db_stmt *stmt, int col);
struct onionreply *db_column_onionreply(const tal_t *ctx,
struct db_stmt *stmt, int col);

View File

@ -1055,7 +1055,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
our_msat,
msat_to_us_min, /* msatoshi_to_us_min */
msat_to_us_max, /* msatoshi_to_us_max */
db_column_tx(tmpctx, stmt, 33),
db_column_psbt_to_tx(tmpctx, stmt, 33),
&last_sig,
wallet_htlc_sigs_load(tmpctx, w,
db_column_u64(stmt, 0)),
@ -1077,19 +1077,6 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
db_column_arr(tmpctx, stmt, 45, u8),
db_column_int(stmt, 46));
/* as a final step, we go ahead and populate the utxo
* for the last_tx with the funding amount */
/* FIXME: input index for funding will not always be zero! */
if (chan->last_tx) {
const u8 * funding_wscript =
bitcoin_redeem_2of2(tmpctx,
&chan->local_funding_pubkey,
&chan->channel_info.remote_fundingkey);
psbt_input_set_prev_utxo_wscript(chan->last_tx->psbt,
0, funding_wscript,
chan->funding);
}
return chan;
}
@ -1461,7 +1448,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
db_bind_u64(stmt, 17, chan->final_key_idx);
db_bind_u64(stmt, 18, chan->our_config.id);
db_bind_tx(stmt, 19, chan->last_tx);
db_bind_psbt(stmt, 19, chan->last_tx->psbt);
db_bind_signature(stmt, 20, &chan->last_sig.s);
db_bind_int(stmt, 21, chan->last_was_revoke);
db_bind_int(stmt, 22, chan->min_possible_feerate);