2018-01-14 15:15:30 +01:00
|
|
|
#include "invoices.h"
|
2017-05-23 22:07:20 +02:00
|
|
|
#include "wallet.h"
|
|
|
|
|
2017-06-21 17:40:42 +02:00
|
|
|
#include <bitcoin/script.h>
|
2019-01-08 01:19:50 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-08-05 12:26:18 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2018-03-07 01:06:07 +01:00
|
|
|
#include <common/key_derive.h>
|
2018-03-07 01:06:07 +01:00
|
|
|
#include <common/wireaddr.h>
|
2017-07-18 19:01:26 +02:00
|
|
|
#include <inttypes.h>
|
2017-06-25 04:33:13 +02:00
|
|
|
#include <lightningd/lightningd.h>
|
2017-07-20 16:42:55 +02:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/peer_htlcs.h>
|
2018-04-03 16:57:28 +02:00
|
|
|
#include <onchaind/gen_onchain_wire.h>
|
2018-04-16 15:29:40 +02:00
|
|
|
#include <string.h>
|
2017-06-05 13:59:51 +02:00
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF
|
2017-09-14 21:27:41 +02:00
|
|
|
#define DIRECTION_INCOMING 0
|
|
|
|
#define DIRECTION_OUTGOING 1
|
2018-03-04 03:42:19 +01:00
|
|
|
/* How many blocks must a UTXO entry be buried under to be considered old enough
|
|
|
|
* to prune? */
|
|
|
|
#define UTXO_PRUNE_DEPTH 144
|
2017-07-18 19:01:26 +02:00
|
|
|
|
2018-02-26 14:57:13 +01:00
|
|
|
static void outpointfilters_init(struct wallet *w)
|
|
|
|
{
|
2018-03-04 01:37:09 +01:00
|
|
|
sqlite3_stmt *stmt;
|
2018-02-26 14:57:13 +01:00
|
|
|
struct utxo **utxos = wallet_get_utxos(NULL, w, output_state_any);
|
2018-03-04 01:37:09 +01:00
|
|
|
struct bitcoin_txid txid;
|
|
|
|
u32 outnum;
|
2018-02-26 14:57:13 +01:00
|
|
|
|
|
|
|
w->owned_outpoints = outpointfilter_new(w);
|
|
|
|
for (size_t i = 0; i < tal_count(utxos); i++)
|
|
|
|
outpointfilter_add(w->owned_outpoints, &utxos[i]->txid, utxos[i]->outnum);
|
|
|
|
|
|
|
|
tal_free(utxos);
|
2018-03-04 01:37:09 +01:00
|
|
|
|
|
|
|
w->utxoset_outpoints = outpointfilter_new(w);
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, "txid, outnum FROM utxoset WHERE spendheight is NULL");
|
2018-03-04 01:37:09 +01:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2018-03-04 01:37:09 +01:00
|
|
|
sqlite3_column_sha256_double(stmt, 0, &txid.shad);
|
|
|
|
outnum = sqlite3_column_int(stmt, 1);
|
|
|
|
outpointfilter_add(w->utxoset_outpoints, &txid, outnum);
|
|
|
|
}
|
2018-02-26 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:10:17 +01:00
|
|
|
struct wallet *wallet_new(struct lightningd *ld,
|
2018-01-23 00:53:21 +01:00
|
|
|
struct log *log, struct timers *timers)
|
2017-05-23 22:07:20 +02:00
|
|
|
{
|
2018-02-12 11:10:17 +01:00
|
|
|
struct wallet *wallet = tal(ld, struct wallet);
|
|
|
|
wallet->ld = ld;
|
2019-02-24 06:12:09 +01:00
|
|
|
wallet->db = db_setup(wallet, ld, log);
|
2017-05-23 22:07:20 +02:00
|
|
|
wallet->log = log;
|
2017-06-21 17:26:38 +02:00
|
|
|
wallet->bip32_base = NULL;
|
2018-01-17 21:29:49 +01:00
|
|
|
list_head_init(&wallet->unstored_payments);
|
2019-06-05 09:00:02 +02:00
|
|
|
list_head_init(&wallet->unreleased_txs);
|
2018-02-26 14:57:13 +01:00
|
|
|
|
|
|
|
db_begin_transaction(wallet->db);
|
2018-09-03 02:45:27 +02:00
|
|
|
wallet->invoices = invoices_new(wallet, wallet->db, log, timers);
|
2018-02-26 14:57:13 +01:00
|
|
|
outpointfilters_init(wallet);
|
|
|
|
db_commit_transaction(wallet->db);
|
2017-05-23 22:07:20 +02:00
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
2018-08-13 05:02:18 +02:00
|
|
|
#define UTXO_FIELDS \
|
|
|
|
"prev_out_tx, prev_out_index, value, type, status, keyindex, " \
|
|
|
|
"channel_id, peer_id, commitment_point, confirmation_height, " \
|
2019-02-22 15:47:30 +01:00
|
|
|
"spend_height, scriptpubkey"
|
2018-08-13 05:02:18 +02:00
|
|
|
|
2019-03-15 03:49:18 +01:00
|
|
|
/* This can fail if we've already seen UTXO. */
|
2017-05-23 22:07:20 +02:00
|
|
|
bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
|
|
|
|
enum wallet_output_type type)
|
|
|
|
{
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
|
|
|
"* from outputs WHERE prev_out_tx=? AND prev_out_index=?");
|
2019-03-15 03:49:18 +01:00
|
|
|
sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT);
|
|
|
|
sqlite3_bind_int(stmt, 2, utxo->outnum);
|
|
|
|
|
|
|
|
/* If we get a result, that means a clash. */
|
2019-03-15 03:50:18 +01:00
|
|
|
if (db_select_step(w->db, stmt)) {
|
2019-03-15 03:49:18 +01:00
|
|
|
db_stmt_done(stmt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 11:36:48 +01:00
|
|
|
stmt = db_prepare(w->db, "INSERT INTO outputs ("
|
2018-08-13 05:02:18 +02:00
|
|
|
UTXO_FIELDS
|
2019-02-22 15:47:30 +01:00
|
|
|
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT);
|
|
|
|
sqlite3_bind_int(stmt, 2, utxo->outnum);
|
2019-02-21 03:39:51 +01:00
|
|
|
sqlite3_bind_amount_sat(stmt, 3, utxo->amount);
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_bind_int(stmt, 4, wallet_output_type_in_db(type));
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_bind_int(stmt, 5, output_state_available);
|
|
|
|
sqlite3_bind_int(stmt, 6, utxo->keyindex);
|
2017-12-20 12:44:00 +01:00
|
|
|
if (utxo->close_info) {
|
|
|
|
sqlite3_bind_int64(stmt, 7, utxo->close_info->channel_id);
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_bind_node_id(stmt, 8, &utxo->close_info->peer_id);
|
2017-12-20 12:44:00 +01:00
|
|
|
sqlite3_bind_pubkey(stmt, 9, &utxo->close_info->commitment_point);
|
|
|
|
} else {
|
|
|
|
sqlite3_bind_null(stmt, 7);
|
|
|
|
sqlite3_bind_null(stmt, 8);
|
|
|
|
sqlite3_bind_null(stmt, 9);
|
|
|
|
}
|
2018-02-26 11:36:48 +01:00
|
|
|
|
|
|
|
if (utxo->blockheight) {
|
|
|
|
sqlite3_bind_int(stmt, 10, *utxo->blockheight);
|
|
|
|
} else
|
|
|
|
sqlite3_bind_null(stmt, 10);
|
|
|
|
|
|
|
|
if (utxo->spendheight)
|
|
|
|
sqlite3_bind_int(stmt, 11, *utxo->spendheight);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 11);
|
|
|
|
|
2019-02-22 15:47:30 +01:00
|
|
|
if (utxo->scriptPubkey)
|
|
|
|
sqlite3_bind_blob(stmt, 12, utxo->scriptPubkey,
|
|
|
|
tal_bytelen(utxo->scriptPubkey),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 12);
|
|
|
|
|
2019-03-15 03:49:18 +01:00
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
return true;
|
2017-05-23 22:07:20 +02:00
|
|
|
}
|
2017-05-31 16:16:59 +02:00
|
|
|
|
2017-06-05 13:59:51 +02:00
|
|
|
/**
|
2018-02-08 22:43:01 +01:00
|
|
|
* wallet_stmt2output - Extract data from stmt and fill an UTXO
|
2017-06-05 13:59:51 +02:00
|
|
|
*/
|
2018-09-27 02:19:24 +02:00
|
|
|
static struct utxo *wallet_stmt2output(const tal_t *ctx, sqlite3_stmt *stmt)
|
2017-06-05 13:59:51 +02:00
|
|
|
{
|
2018-09-27 02:19:24 +02:00
|
|
|
struct utxo *utxo = tal(ctx, struct utxo);
|
2018-03-22 00:13:16 +01:00
|
|
|
u32 *blockheight, *spendheight;
|
2017-12-20 12:44:00 +01:00
|
|
|
sqlite3_column_sha256_double(stmt, 0, &utxo->txid.shad);
|
2017-06-05 13:59:51 +02:00
|
|
|
utxo->outnum = sqlite3_column_int(stmt, 1);
|
2019-02-21 03:39:51 +01:00
|
|
|
utxo->amount = sqlite3_column_amount_sat(stmt, 2);
|
2017-06-05 13:59:51 +02:00
|
|
|
utxo->is_p2sh = sqlite3_column_int(stmt, 3) == p2sh_wpkh;
|
|
|
|
utxo->status = sqlite3_column_int(stmt, 4);
|
|
|
|
utxo->keyindex = sqlite3_column_int(stmt, 5);
|
2017-12-20 12:44:00 +01:00
|
|
|
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
|
|
|
|
utxo->close_info = tal(utxo, struct unilateral_close_info);
|
|
|
|
utxo->close_info->channel_id = sqlite3_column_int64(stmt, 6);
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_column_node_id(stmt, 7, &utxo->close_info->peer_id);
|
2017-12-20 12:44:00 +01:00
|
|
|
sqlite3_column_pubkey(stmt, 8, &utxo->close_info->commitment_point);
|
|
|
|
} else {
|
|
|
|
utxo->close_info = NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-26 11:36:48 +01:00
|
|
|
utxo->blockheight = NULL;
|
|
|
|
utxo->spendheight = NULL;
|
2019-02-22 15:47:30 +01:00
|
|
|
utxo->scriptPubkey = NULL;
|
2018-02-26 11:36:48 +01:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 9) != SQLITE_NULL) {
|
2018-03-22 00:13:16 +01:00
|
|
|
blockheight = tal(utxo, u32);
|
2018-02-26 11:36:48 +01:00
|
|
|
*blockheight = sqlite3_column_int(stmt, 9);
|
|
|
|
utxo->blockheight = blockheight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 10) != SQLITE_NULL) {
|
2018-03-22 00:13:16 +01:00
|
|
|
spendheight = tal(utxo, u32);
|
2018-02-26 11:36:48 +01:00
|
|
|
*spendheight = sqlite3_column_int(stmt, 10);
|
|
|
|
utxo->spendheight = spendheight;
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:47:30 +01:00
|
|
|
if (sqlite3_column_type(stmt, 11) != SQLITE_NULL) {
|
|
|
|
utxo->scriptPubkey =
|
|
|
|
tal_dup_arr(utxo, u8, sqlite3_column_blob(stmt, 11),
|
|
|
|
sqlite3_column_bytes(stmt, 11), 0);
|
|
|
|
}
|
|
|
|
|
2018-09-27 02:19:24 +02:00
|
|
|
return utxo;
|
2017-06-05 13:59:51 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 16:16:59 +02:00
|
|
|
bool wallet_update_output_status(struct wallet *w,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *txid,
|
2017-05-31 16:16:59 +02:00
|
|
|
const u32 outnum, enum output_status oldstatus,
|
|
|
|
enum output_status newstatus)
|
|
|
|
{
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2017-05-31 16:16:59 +02:00
|
|
|
if (oldstatus != output_state_any) {
|
2017-10-18 14:54:33 +02:00
|
|
|
stmt = db_prepare(
|
|
|
|
w->db, "UPDATE outputs SET status=? WHERE status=? AND prev_out_tx=? AND prev_out_index=?");
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, output_status_in_db(newstatus));
|
|
|
|
sqlite3_bind_int(stmt, 2, output_status_in_db(oldstatus));
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_bind_blob(stmt, 3, txid, sizeof(*txid), SQLITE_TRANSIENT);
|
|
|
|
sqlite3_bind_int(stmt, 4, outnum);
|
2017-05-31 16:16:59 +02:00
|
|
|
} else {
|
2017-10-18 14:54:33 +02:00
|
|
|
stmt = db_prepare(
|
|
|
|
w->db, "UPDATE outputs SET status=? WHERE prev_out_tx=? AND prev_out_index=?");
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, output_status_in_db(newstatus));
|
2017-10-18 14:54:33 +02:00
|
|
|
sqlite3_bind_blob(stmt, 2, txid, sizeof(*txid), SQLITE_TRANSIENT);
|
|
|
|
sqlite3_bind_int(stmt, 3, outnum);
|
2017-05-31 16:16:59 +02:00
|
|
|
}
|
2017-10-18 14:54:33 +02:00
|
|
|
db_exec_prepared(w->db, stmt);
|
2017-05-31 16:16:59 +02:00
|
|
|
return sqlite3_changes(w->db->sql) > 0;
|
|
|
|
}
|
2017-06-05 13:59:51 +02:00
|
|
|
|
|
|
|
struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum output_status state)
|
|
|
|
{
|
|
|
|
struct utxo **results;
|
|
|
|
int i;
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2017-06-05 13:59:51 +02:00
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
if (state == output_state_any)
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, UTXO_FIELDS " FROM outputs");
|
2018-08-09 04:27:25 +02:00
|
|
|
else {
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, UTXO_FIELDS
|
|
|
|
" FROM outputs WHERE status=?1");
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, output_status_in_db(state));
|
|
|
|
}
|
2017-06-05 13:59:51 +02:00
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
results = tal_arr(ctx, struct utxo*, 0);
|
2019-03-15 03:50:18 +01:00
|
|
|
for (i=0; db_select_step(w->db, stmt); i++) {
|
2018-09-27 02:19:24 +02:00
|
|
|
struct utxo *u = wallet_stmt2output(results, stmt);
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&results, u);
|
2017-06-05 13:59:51 +02:00
|
|
|
}
|
2018-09-27 02:19:24 +02:00
|
|
|
|
2018-08-13 05:02:18 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct utxo **wallet_get_unconfirmed_closeinfo_utxos(const tal_t *ctx, struct wallet *w)
|
|
|
|
{
|
|
|
|
struct utxo **results;
|
|
|
|
int i;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(
|
|
|
|
w->db, UTXO_FIELDS
|
2018-08-13 05:02:18 +02:00
|
|
|
" FROM outputs WHERE channel_id IS NOT NULL and confirmation_height IS NULL");
|
|
|
|
|
|
|
|
results = tal_arr(ctx, struct utxo*, 0);
|
2019-03-15 03:50:18 +01:00
|
|
|
for (i=0; db_select_step(w->db, stmt); i++) {
|
2018-09-27 02:19:24 +02:00
|
|
|
struct utxo *u = wallet_stmt2output(results, stmt);
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&results, u);
|
2018-08-13 05:02:18 +02:00
|
|
|
}
|
2017-06-05 13:59:51 +02:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2017-06-13 15:42:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* unreserve_utxo - Mark a reserved UTXO as available again
|
|
|
|
*/
|
|
|
|
static void unreserve_utxo(struct wallet *w, const struct utxo *unres)
|
|
|
|
{
|
|
|
|
if (!wallet_update_output_status(w, &unres->txid, unres->outnum,
|
|
|
|
output_state_reserved,
|
|
|
|
output_state_available)) {
|
2017-11-01 02:21:56 +01:00
|
|
|
fatal("Unable to unreserve output");
|
2017-06-13 15:42:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* destroy_utxos - Destructor for an array of pointers to utxo
|
|
|
|
*/
|
|
|
|
static void destroy_utxos(const struct utxo **utxos, struct wallet *w)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(utxos); i++)
|
|
|
|
unreserve_utxo(w, utxos[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_confirm_utxos(struct wallet *w, const struct utxo **utxos)
|
|
|
|
{
|
|
|
|
tal_del_destructor2(utxos, destroy_utxos, w);
|
|
|
|
for (size_t i = 0; i < tal_count(utxos); i++) {
|
|
|
|
if (!wallet_update_output_status(
|
|
|
|
w, &utxos[i]->txid, utxos[i]->outnum,
|
|
|
|
output_state_reserved, output_state_spent)) {
|
2017-11-01 02:21:56 +01:00
|
|
|
fatal("Unable to mark output as spent");
|
2017-06-13 15:42:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:52:45 +01:00
|
|
|
static const struct utxo **wallet_select(const tal_t *ctx, struct wallet *w,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat sat,
|
2017-12-19 20:52:45 +01:00
|
|
|
const u32 feerate_per_kw,
|
2017-12-20 07:05:01 +01:00
|
|
|
size_t outscriptlen,
|
|
|
|
bool may_have_change,
|
2019-02-21 20:13:20 +01:00
|
|
|
u32 maxheight,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat *satoshi_in,
|
|
|
|
struct amount_sat *fee_estimate)
|
2017-06-13 15:42:31 +02:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
struct utxo **available;
|
2017-12-20 07:05:01 +01:00
|
|
|
u64 weight;
|
2017-06-13 15:42:31 +02:00
|
|
|
const struct utxo **utxos = tal_arr(ctx, const struct utxo *, 0);
|
|
|
|
tal_add_destructor2(utxos, destroy_utxos, w);
|
|
|
|
|
2017-12-20 07:05:01 +01:00
|
|
|
/* version, input count, output count, locktime */
|
|
|
|
weight = (4 + 1 + 1 + 4) * 4;
|
|
|
|
|
|
|
|
/* The main output: amount, len, scriptpubkey */
|
|
|
|
weight += (8 + 1 + outscriptlen) * 4;
|
|
|
|
|
|
|
|
/* Change output will be P2WPKH */
|
|
|
|
if (may_have_change)
|
|
|
|
weight += (8 + 1 + BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN) * 4;
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
*fee_estimate = AMOUNT_SAT(0);
|
|
|
|
*satoshi_in = AMOUNT_SAT(0);
|
2017-12-19 20:52:45 +01:00
|
|
|
|
2017-06-13 15:42:31 +02:00
|
|
|
available = wallet_get_utxos(ctx, w, output_state_available);
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(available); i++) {
|
2017-12-20 07:05:01 +01:00
|
|
|
size_t input_weight;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat needed;
|
2018-09-27 02:19:24 +02:00
|
|
|
struct utxo *u = tal_steal(utxos, available[i]);
|
2017-12-20 07:05:01 +01:00
|
|
|
|
2019-02-21 20:13:20 +01:00
|
|
|
/* If we require confirmations check that we have a
|
|
|
|
* confirmation height and that it is below the required
|
|
|
|
* maxheight (current_height - minconf */
|
|
|
|
if (maxheight != 0 &&
|
|
|
|
(!u->blockheight || *u->blockheight > maxheight))
|
|
|
|
continue;
|
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&utxos, u);
|
2017-06-13 15:42:31 +02:00
|
|
|
|
|
|
|
if (!wallet_update_output_status(
|
|
|
|
w, &available[i]->txid, available[i]->outnum,
|
|
|
|
output_state_available, output_state_reserved))
|
2017-11-01 02:21:56 +01:00
|
|
|
fatal("Unable to reserve output");
|
2017-12-21 12:10:24 +01:00
|
|
|
|
2017-12-20 07:05:01 +01:00
|
|
|
/* Input weight: txid + index + sequence */
|
|
|
|
input_weight = (32 + 4 + 4) * 4;
|
2017-06-13 15:42:31 +02:00
|
|
|
|
2017-12-20 07:05:01 +01:00
|
|
|
/* We always encode the length of the script, even if empty */
|
|
|
|
input_weight += 1 * 4;
|
|
|
|
|
|
|
|
/* P2SH variants include push of <0 <20-byte-key-hash>> */
|
2018-09-27 02:19:24 +02:00
|
|
|
if (u->is_p2sh)
|
2017-12-20 07:05:01 +01:00
|
|
|
input_weight += 23 * 4;
|
|
|
|
|
|
|
|
/* Account for witness (1 byte count + sig + key) */
|
|
|
|
input_weight += 1 + (1 + 73 + 1 + 33);
|
|
|
|
|
|
|
|
weight += input_weight;
|
2017-06-13 15:42:31 +02:00
|
|
|
|
2019-04-18 04:10:34 +02:00
|
|
|
if (!amount_sat_add(satoshi_in, *satoshi_in, u->amount))
|
2019-02-21 04:45:55 +01:00
|
|
|
fatal("Overflow in available satoshis %zu/%zu %s + %s",
|
|
|
|
i, tal_count(available),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
satoshi_in),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-04-18 04:10:34 +02:00
|
|
|
&u->amount));
|
2019-02-21 04:45:55 +01:00
|
|
|
|
|
|
|
*fee_estimate = amount_tx_fee(feerate_per_kw, weight);
|
|
|
|
if (!amount_sat_add(&needed, sat, *fee_estimate))
|
|
|
|
fatal("Overflow in fee estimate %zu/%zu %s + %s",
|
|
|
|
i, tal_count(available),
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &sat),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
fee_estimate));
|
|
|
|
if (amount_sat_greater_eq(*satoshi_in, needed))
|
2017-06-13 15:42:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tal_free(available);
|
|
|
|
|
|
|
|
return utxos;
|
|
|
|
}
|
2017-06-21 17:40:42 +02:00
|
|
|
|
2017-12-19 20:52:45 +01:00
|
|
|
const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat sat,
|
2017-12-19 20:52:45 +01:00
|
|
|
const u32 feerate_per_kw,
|
2017-12-20 07:05:01 +01:00
|
|
|
size_t outscriptlen,
|
2019-02-21 20:13:20 +01:00
|
|
|
u32 maxheight,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat *fee_estimate,
|
|
|
|
struct amount_sat *change)
|
2017-12-19 20:52:45 +01:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat satoshi_in;
|
2017-12-19 20:52:45 +01:00
|
|
|
const struct utxo **utxo;
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
utxo = wallet_select(ctx, w, sat, feerate_per_kw,
|
2019-02-21 20:13:20 +01:00
|
|
|
outscriptlen, true, maxheight,
|
2017-12-19 20:52:45 +01:00
|
|
|
&satoshi_in, fee_estimate);
|
|
|
|
|
|
|
|
/* Couldn't afford it? */
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_sub(change, satoshi_in, sat)
|
|
|
|
|| !amount_sat_sub(change, *change, *fee_estimate))
|
2017-12-19 20:52:45 +01:00
|
|
|
return tal_free(utxo);
|
|
|
|
|
|
|
|
return utxo;
|
|
|
|
}
|
2017-12-21 12:10:24 +01:00
|
|
|
|
2017-12-19 20:52:45 +01:00
|
|
|
const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w,
|
2017-12-20 07:05:01 +01:00
|
|
|
const u32 feerate_per_kw,
|
|
|
|
size_t outscriptlen,
|
2019-02-21 20:13:20 +01:00
|
|
|
u32 maxheight,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat *value,
|
|
|
|
struct amount_sat *fee_estimate)
|
2017-12-19 20:52:45 +01:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat satoshi_in;
|
2017-12-19 20:52:45 +01:00
|
|
|
const struct utxo **utxo;
|
|
|
|
|
|
|
|
/* Huge value, but won't overflow on addition */
|
2019-02-21 04:45:55 +01:00
|
|
|
utxo = wallet_select(ctx, w, AMOUNT_SAT(1ULL << 56), feerate_per_kw,
|
2019-02-21 20:13:20 +01:00
|
|
|
outscriptlen, false, maxheight,
|
2017-12-19 20:52:45 +01:00
|
|
|
&satoshi_in, fee_estimate);
|
|
|
|
|
|
|
|
/* Can't afford fees? */
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_sub(value, satoshi_in, *fee_estimate))
|
2017-12-19 20:52:45 +01:00
|
|
|
return tal_free(utxo);
|
|
|
|
|
|
|
|
return utxo;
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:40:42 +02:00
|
|
|
bool wallet_can_spend(struct wallet *w, const u8 *script,
|
|
|
|
u32 *index, bool *output_is_p2sh)
|
|
|
|
{
|
|
|
|
struct ext_key ext;
|
|
|
|
u64 bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0);
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
/* If not one of these, can't be for us. */
|
2017-10-26 04:58:19 +02:00
|
|
|
if (is_p2sh(script, NULL))
|
2017-06-21 17:40:42 +02:00
|
|
|
*output_is_p2sh = true;
|
2017-10-26 04:58:19 +02:00
|
|
|
else if (is_p2wpkh(script, NULL))
|
2017-06-21 17:40:42 +02:00
|
|
|
*output_is_p2sh = false;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2017-06-22 14:20:41 +02:00
|
|
|
for (i = 0; i <= bip32_max_index; i++) {
|
2017-06-21 17:40:42 +02:00
|
|
|
u8 *s;
|
|
|
|
|
|
|
|
if (bip32_key_from_parent(w->bip32_base, i,
|
|
|
|
BIP32_FLAG_KEY_PUBLIC, &ext)
|
|
|
|
!= WALLY_OK) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
s = scriptpubkey_p2wpkh_derkey(w, ext.pub_key);
|
|
|
|
if (*output_is_p2sh) {
|
|
|
|
u8 *p2sh = scriptpubkey_p2sh(w, s);
|
|
|
|
tal_free(s);
|
|
|
|
s = p2sh;
|
|
|
|
}
|
|
|
|
if (scripteq(s, script)) {
|
|
|
|
tal_free(s);
|
|
|
|
*index = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tal_free(s);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-25 04:33:13 +02:00
|
|
|
s64 wallet_get_newindex(struct lightningd *ld)
|
|
|
|
{
|
|
|
|
u64 newidx = db_get_intvar(ld->wallet->db, "bip32_max_index", 0) + 1;
|
|
|
|
|
|
|
|
if (newidx == BIP32_INITIAL_HARDENED_CHILD)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
db_set_intvar(ld->wallet->db, "bip32_max_index", newidx);
|
|
|
|
return newidx;
|
|
|
|
}
|
2017-07-18 19:01:26 +02:00
|
|
|
|
2018-02-18 13:54:46 +01:00
|
|
|
static void wallet_shachain_init(struct wallet *wallet,
|
|
|
|
struct wallet_shachain *chain)
|
2017-07-18 19:01:26 +02:00
|
|
|
{
|
2017-10-18 15:34:51 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2018-02-18 13:54:46 +01:00
|
|
|
|
|
|
|
assert(chain->id == 0);
|
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
/* Create shachain */
|
|
|
|
shachain_init(&chain->chain);
|
2017-10-18 15:34:51 +02:00
|
|
|
stmt = db_prepare(wallet->db, "INSERT INTO shachains (min_index, num_valid) VALUES (?, 0);");
|
|
|
|
sqlite3_bind_int64(stmt, 1, chain->chain.min_index);
|
2017-11-01 02:21:56 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2017-10-18 15:34:51 +02:00
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
chain->id = sqlite3_last_insert_rowid(wallet->db->sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO(cdecker) Stolen from shachain, move to some appropriate location */
|
2017-08-18 06:43:53 +02:00
|
|
|
static unsigned int count_trailing_zeroes(uint64_t index)
|
2017-07-18 19:01:26 +02:00
|
|
|
{
|
|
|
|
#if HAVE_BUILTIN_CTZLL
|
|
|
|
return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
|
|
|
|
#else
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SHACHAIN_BITS; i++) {
|
|
|
|
if (index & (1ULL << i))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wallet_shachain_add_hash(struct wallet *wallet,
|
|
|
|
struct wallet_shachain *chain,
|
2017-08-18 06:43:53 +02:00
|
|
|
uint64_t index,
|
2018-07-09 13:17:58 +02:00
|
|
|
const struct secret *hash)
|
2017-07-18 19:01:26 +02:00
|
|
|
{
|
2017-10-18 15:34:51 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2017-07-18 19:01:26 +02:00
|
|
|
u32 pos = count_trailing_zeroes(index);
|
2018-07-09 13:17:58 +02:00
|
|
|
struct sha256 s;
|
|
|
|
|
|
|
|
BUILD_ASSERT(sizeof(s) == sizeof(*hash));
|
|
|
|
memcpy(&s, hash, sizeof(s));
|
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
assert(index < SQLITE_MAX_UINT);
|
2018-07-09 13:17:58 +02:00
|
|
|
if (!shachain_add_hash(&chain->chain, index, &s)) {
|
2017-07-18 19:01:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-18 15:34:51 +02:00
|
|
|
stmt = db_prepare(wallet->db, "UPDATE shachains SET num_valid=?, min_index=? WHERE id=?");
|
|
|
|
sqlite3_bind_int(stmt, 1, chain->chain.num_valid);
|
|
|
|
sqlite3_bind_int64(stmt, 2, index);
|
|
|
|
sqlite3_bind_int64(stmt, 3, chain->id);
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2017-10-18 15:34:51 +02:00
|
|
|
|
|
|
|
stmt = db_prepare(
|
|
|
|
wallet->db,
|
|
|
|
"REPLACE INTO shachain_known (shachain_id, pos, idx, hash) VALUES (?, ?, ?, ?);");
|
|
|
|
sqlite3_bind_int64(stmt, 1, chain->id);
|
|
|
|
sqlite3_bind_int(stmt, 2, pos);
|
|
|
|
sqlite3_bind_int64(stmt, 3, index);
|
|
|
|
sqlite3_bind_blob(stmt, 4, hash, sizeof(*hash), SQLITE_TRANSIENT);
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2017-07-18 19:01:26 +02:00
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
return true;
|
2017-07-18 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wallet_shachain_load(struct wallet *wallet, u64 id,
|
|
|
|
struct wallet_shachain *chain)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
chain->id = id;
|
|
|
|
shachain_init(&chain->chain);
|
|
|
|
|
|
|
|
/* Load shachain metadata */
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db, "min_index, num_valid FROM shachains WHERE id=?");
|
2017-10-18 15:34:51 +02:00
|
|
|
sqlite3_bind_int64(stmt, 1, id);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(wallet->db, stmt))
|
2017-07-18 19:01:26 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
chain->chain.min_index = sqlite3_column_int64(stmt, 0);
|
|
|
|
chain->chain.num_valid = sqlite3_column_int64(stmt, 1);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2017-07-18 19:01:26 +02:00
|
|
|
|
|
|
|
/* Load shachain known entries */
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db, "idx, hash, pos FROM shachain_known WHERE shachain_id=?");
|
2017-10-18 15:34:51 +02:00
|
|
|
sqlite3_bind_int64(stmt, 1, id);
|
2017-07-18 19:01:26 +02:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(wallet->db, stmt)) {
|
2017-07-18 19:01:26 +02:00
|
|
|
int pos = sqlite3_column_int(stmt, 2);
|
|
|
|
chain->chain.known[pos].index = sqlite3_column_int64(stmt, 0);
|
2017-10-18 15:34:51 +02:00
|
|
|
memcpy(&chain->chain.known[pos].hash, sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
|
2017-07-18 19:01:26 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:37:08 +01:00
|
|
|
static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
|
2017-07-20 16:42:55 +02:00
|
|
|
{
|
2017-12-18 11:17:15 +01:00
|
|
|
const unsigned char *addrstr;
|
2018-02-12 11:12:55 +01:00
|
|
|
struct peer *peer;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-10-26 08:01:26 +02:00
|
|
|
struct wireaddr_internal addr;
|
2017-12-18 11:17:15 +01:00
|
|
|
|
|
|
|
sqlite3_stmt *stmt =
|
2019-03-15 03:50:18 +01:00
|
|
|
db_select(w->db,
|
|
|
|
"id, node_id, address FROM peers WHERE id=%"PRIu64";", dbid);
|
2017-12-18 11:17:15 +01:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(w->db, stmt))
|
2018-02-12 11:12:55 +01:00
|
|
|
return NULL;
|
2019-03-15 03:50:18 +01:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!sqlite3_column_node_id(stmt, 1, &id)) {
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-02-12 11:12:55 +01:00
|
|
|
return NULL;
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
2017-12-18 11:17:15 +01:00
|
|
|
addrstr = sqlite3_column_text(stmt, 2);
|
2018-10-26 08:01:26 +02:00
|
|
|
if (!parse_wireaddr_internal((const char*)addrstr, &addr, DEFAULT_PORT, false, false, true, NULL)) {
|
|
|
|
db_stmt_done(stmt);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-18 11:17:15 +01:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
peer = new_peer(w->ld, sqlite3_column_int64(stmt, 0),
|
2018-10-26 08:01:26 +02:00
|
|
|
&id, &addr);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2017-12-18 11:17:15 +01:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
return peer;
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 16:40:59 +01:00
|
|
|
static secp256k1_ecdsa_signature *
|
|
|
|
wallet_htlc_sigs_load(const tal_t *ctx, struct wallet *w, u64 channelid)
|
|
|
|
{
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(w->db, "signature FROM htlc_sigs WHERE channelid = ?");
|
2018-02-09 16:40:59 +01:00
|
|
|
secp256k1_ecdsa_signature *htlc_sigs = tal_arr(ctx, secp256k1_ecdsa_signature, 0);
|
|
|
|
sqlite3_bind_int64(stmt, 1, channelid);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2019-01-15 04:51:27 +01:00
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
sqlite3_column_signature(stmt, 0, &sig);
|
|
|
|
tal_arr_expand(&htlc_sigs, sig);
|
|
|
|
}
|
2018-09-27 02:19:24 +02:00
|
|
|
|
|
|
|
log_debug(w->log, "Loaded %zu HTLC signatures from DB",
|
|
|
|
tal_count(htlc_sigs));
|
2018-02-09 16:40:59 +01:00
|
|
|
return htlc_sigs;
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:21:26 +02:00
|
|
|
bool wallet_remote_ann_sigs_load(const tal_t *ctx, struct wallet *w, u64 id,
|
|
|
|
secp256k1_ecdsa_signature **remote_ann_node_sig,
|
|
|
|
secp256k1_ecdsa_signature **remote_ann_bitcoin_sig)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int res;
|
|
|
|
stmt = db_select_prepare(w->db,
|
|
|
|
"remote_ann_node_sig, remote_ann_bitcoin_sig"
|
|
|
|
" FROM channels WHERE id = ?");
|
|
|
|
sqlite3_bind_int64(stmt, 1, id);
|
|
|
|
|
|
|
|
res = sqlite3_step(stmt);
|
|
|
|
|
|
|
|
/* This must succeed, since we know the channel exists */
|
|
|
|
assert(res == SQLITE_ROW);
|
|
|
|
|
|
|
|
/* if only one sig exists, forget the sig and hope peer send new ones*/
|
|
|
|
if(sqlite3_column_type(stmt, 0) == SQLITE_NULL ||
|
|
|
|
sqlite3_column_type(stmt, 1) == SQLITE_NULL) {
|
|
|
|
*remote_ann_node_sig = *remote_ann_bitcoin_sig = NULL;
|
|
|
|
db_stmt_done(stmt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the case left over is both sigs exist */
|
|
|
|
*remote_ann_node_sig = tal(ctx, secp256k1_ecdsa_signature);
|
|
|
|
*remote_ann_bitcoin_sig = tal(ctx, secp256k1_ecdsa_signature);
|
|
|
|
|
|
|
|
if (!sqlite3_column_signature(stmt, 0, *remote_ann_node_sig))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!sqlite3_column_signature(stmt, 1, *remote_ann_bitcoin_sig))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
db_stmt_done(stmt);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*remote_ann_node_sig = tal_free(*remote_ann_node_sig);
|
|
|
|
*remote_ann_bitcoin_sig = tal_free(*remote_ann_bitcoin_sig);
|
|
|
|
db_stmt_done(stmt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:42:55 +02:00
|
|
|
/**
|
|
|
|
* wallet_stmt2channel - Helper to populate a wallet_channel from a sqlite3_stmt
|
|
|
|
*/
|
2019-05-16 10:54:38 +02:00
|
|
|
static struct channel *wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt)
|
2017-07-20 16:42:55 +02:00
|
|
|
{
|
|
|
|
bool ok = true;
|
2018-02-19 02:06:14 +01:00
|
|
|
struct channel_info channel_info;
|
|
|
|
struct short_channel_id *scid;
|
2018-02-12 11:12:55 +01:00
|
|
|
struct channel *chan;
|
|
|
|
u64 peer_dbid;
|
|
|
|
struct peer *peer;
|
2018-02-19 02:06:14 +01:00
|
|
|
struct wallet_shachain wshachain;
|
|
|
|
struct channel_config our_config;
|
|
|
|
struct bitcoin_txid funding_txid;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature last_sig;
|
2018-02-19 02:06:14 +01:00
|
|
|
u8 *remote_shutdown_scriptpubkey;
|
|
|
|
struct changed_htlc *last_sent_commit;
|
2018-03-07 01:06:07 +01:00
|
|
|
s64 final_key_idx;
|
2018-07-23 04:23:02 +02:00
|
|
|
struct basepoints local_basepoints;
|
|
|
|
struct pubkey local_funding_pubkey;
|
2018-08-17 07:06:36 +02:00
|
|
|
struct pubkey *future_per_commitment_point;
|
2018-02-12 11:12:55 +01:00
|
|
|
|
|
|
|
peer_dbid = sqlite3_column_int64(stmt, 1);
|
|
|
|
peer = find_peer_by_dbid(w->ld, peer_dbid);
|
|
|
|
if (!peer) {
|
2018-02-21 16:37:08 +01:00
|
|
|
peer = wallet_peer_load(w, peer_dbid);
|
2018-02-19 02:06:14 +01:00
|
|
|
if (!peer) {
|
2018-02-12 11:12:55 +01:00
|
|
|
return NULL;
|
2018-02-19 02:06:14 +01:00
|
|
|
}
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-26 00:04:02 +01:00
|
|
|
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL) {
|
2018-02-19 02:06:14 +01:00
|
|
|
scid = tal(tmpctx, struct short_channel_id);
|
2019-01-20 06:43:35 +01:00
|
|
|
if (!sqlite3_column_short_channel_id(stmt, 2, scid))
|
|
|
|
return NULL;
|
2017-07-20 16:42:55 +02:00
|
|
|
} else {
|
2018-02-19 02:06:14 +01:00
|
|
|
scid = NULL;
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
ok &= wallet_shachain_load(w, sqlite3_column_int64(stmt, 27),
|
|
|
|
&wshachain);
|
2017-07-20 16:42:55 +02:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
remote_shutdown_scriptpubkey = sqlite3_column_arr(tmpctx, stmt, 28, u8);
|
2017-07-20 16:42:55 +02:00
|
|
|
|
|
|
|
/* Do we have a last_sent_commit, if yes, populate */
|
2018-09-04 09:02:35 +02:00
|
|
|
if (sqlite3_column_type(stmt, 41) != SQLITE_NULL) {
|
|
|
|
const u8 *cursor = sqlite3_column_blob(stmt, 41);
|
|
|
|
size_t len = sqlite3_column_bytes(stmt, 41);
|
|
|
|
size_t n = 0;
|
|
|
|
last_sent_commit = tal_arr(tmpctx, struct changed_htlc, n);
|
|
|
|
while (len) {
|
|
|
|
tal_resize(&last_sent_commit, n+1);
|
|
|
|
fromwire_changed_htlc(&cursor, &len,
|
|
|
|
&last_sent_commit[n++]);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
last_sent_commit = NULL;
|
|
|
|
|
|
|
|
#ifdef COMPAT_V060
|
|
|
|
if (!last_sent_commit && sqlite3_column_type(stmt, 30) != SQLITE_NULL) {
|
2018-02-19 02:06:14 +01:00
|
|
|
last_sent_commit = tal(tmpctx, struct changed_htlc);
|
|
|
|
last_sent_commit->newstate = sqlite3_column_int64(stmt, 30);
|
|
|
|
last_sent_commit->id = sqlite3_column_int64(stmt, 31);
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
2018-09-04 09:02:35 +02:00
|
|
|
#endif
|
2017-08-08 19:09:08 +02:00
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
if (sqlite3_column_type(stmt, 40) != SQLITE_NULL) {
|
|
|
|
future_per_commitment_point = tal(tmpctx, struct pubkey);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 40,
|
|
|
|
future_per_commitment_point);
|
|
|
|
} else
|
|
|
|
future_per_commitment_point = NULL;
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
ok &= wallet_channel_config_load(w, sqlite3_column_int64(stmt, 3),
|
|
|
|
&our_config);
|
|
|
|
ok &= sqlite3_column_sha256_double(stmt, 12, &funding_txid.shad);
|
2017-08-18 06:43:52 +02:00
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
ok &= sqlite3_column_signature(stmt, 33, &last_sig.s);
|
|
|
|
last_sig.sighash_type = SIGHASH_ALL;
|
2017-08-21 11:25:15 +02:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
/* Populate channel_info */
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 18, &channel_info.remote_fundingkey);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 19, &channel_info.theirbase.revocation);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 20, &channel_info.theirbase.payment);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 21, &channel_info.theirbase.htlc);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 22, &channel_info.theirbase.delayed_payment);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 23, &channel_info.remote_per_commit);
|
|
|
|
ok &= sqlite3_column_pubkey(stmt, 24, &channel_info.old_remote_per_commit);
|
|
|
|
channel_info.feerate_per_kw[LOCAL] = sqlite3_column_int(stmt, 25);
|
|
|
|
channel_info.feerate_per_kw[REMOTE] = sqlite3_column_int(stmt, 26);
|
2019-05-01 00:57:09 +02:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
wallet_channel_config_load(w, sqlite3_column_int64(stmt, 4),
|
|
|
|
&channel_info.their_config);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-09 16:40:59 +01:00
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
final_key_idx = sqlite3_column_int64(stmt, 29);
|
|
|
|
if (final_key_idx < 0) {
|
|
|
|
log_broken(w->log, "%s: Final key < 0", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-23 04:23:02 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
get_channel_basepoints(w->ld, &peer->id, sqlite3_column_int64(stmt, 0),
|
|
|
|
&local_basepoints, &local_funding_pubkey);
|
2018-02-19 02:06:14 +01:00
|
|
|
chan = new_channel(peer, sqlite3_column_int64(stmt, 0),
|
|
|
|
&wshachain,
|
|
|
|
sqlite3_column_int(stmt, 5),
|
|
|
|
sqlite3_column_int(stmt, 6),
|
|
|
|
NULL, /* Set up fresh log */
|
2018-02-23 06:53:44 +01:00
|
|
|
"Loaded from database",
|
2018-02-19 02:06:14 +01:00
|
|
|
sqlite3_column_int(stmt, 7),
|
|
|
|
&our_config,
|
|
|
|
sqlite3_column_int(stmt, 8),
|
|
|
|
sqlite3_column_int64(stmt, 9),
|
|
|
|
sqlite3_column_int64(stmt, 10),
|
|
|
|
sqlite3_column_int64(stmt, 11),
|
|
|
|
&funding_txid,
|
|
|
|
sqlite3_column_int(stmt, 13),
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_column_amount_sat(stmt, 14),
|
|
|
|
sqlite3_column_amount_msat(stmt, 16),
|
2018-02-19 02:06:14 +01:00
|
|
|
sqlite3_column_int(stmt, 15) != 0,
|
|
|
|
scid,
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_column_amount_msat(stmt, 17),
|
|
|
|
sqlite3_column_amount_msat(stmt, 38), /* msatoshi_to_us_min */
|
|
|
|
sqlite3_column_amount_msat(stmt, 39), /* msatoshi_to_us_max */
|
2018-02-19 02:06:14 +01:00
|
|
|
sqlite3_column_tx(tmpctx, stmt, 32),
|
|
|
|
&last_sig,
|
|
|
|
wallet_htlc_sigs_load(tmpctx, w,
|
|
|
|
sqlite3_column_int64(stmt, 0)),
|
|
|
|
&channel_info,
|
|
|
|
remote_shutdown_scriptpubkey,
|
2018-03-07 01:06:07 +01:00
|
|
|
final_key_idx,
|
2018-02-19 02:06:14 +01:00
|
|
|
sqlite3_column_int(stmt, 34) != 0,
|
|
|
|
last_sent_commit,
|
2018-04-03 09:19:39 +02:00
|
|
|
sqlite3_column_int64(stmt, 35),
|
|
|
|
sqlite3_column_int(stmt, 36),
|
2018-04-26 06:51:01 +02:00
|
|
|
sqlite3_column_int(stmt, 37),
|
|
|
|
/* Not connected */
|
2018-07-23 04:23:02 +02:00
|
|
|
false,
|
2018-08-17 07:06:36 +02:00
|
|
|
&local_basepoints, &local_funding_pubkey,
|
2019-02-21 01:01:33 +01:00
|
|
|
future_per_commitment_point,
|
|
|
|
sqlite3_column_int(stmt, 42),
|
2019-05-01 00:57:09 +02:00
|
|
|
sqlite3_column_int(stmt, 43),
|
|
|
|
sqlite3_column_arr(tmpctx, stmt, 44, u8));
|
2018-02-12 11:12:55 +01:00
|
|
|
return chan;
|
2017-07-20 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 13:52:47 +02:00
|
|
|
/* List of fields to retrieve from the channels DB table, in the order
|
|
|
|
* that wallet_stmt2channel understands and will parse correctly */
|
2018-03-31 02:21:13 +02:00
|
|
|
/* Numbers below are sqlite3_column indices for the first field
|
|
|
|
* of that line. */
|
2018-01-03 06:25:30 +01:00
|
|
|
static const char *channel_fields =
|
2018-03-31 02:21:13 +02:00
|
|
|
/*0*/ "id, peer_id, short_channel_id, channel_config_local, "
|
|
|
|
/*4*/ "channel_config_remote, state, funder, channel_flags, "
|
|
|
|
/*8*/ "minimum_depth, "
|
|
|
|
/*9*/ "next_index_local, next_index_remote, "
|
|
|
|
/*11*/ "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, "
|
|
|
|
/*15*/ "funding_locked_remote, push_msatoshi, msatoshi_local, "
|
|
|
|
/*18*/ "fundingkey_remote, revocation_basepoint_remote, "
|
|
|
|
/*20*/ "payment_basepoint_remote, htlc_basepoint_remote, "
|
|
|
|
/*22*/ "delayed_payment_basepoint_remote, per_commit_remote, "
|
|
|
|
/*24*/ "old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, "
|
|
|
|
/*28*/ "shutdown_scriptpubkey_remote, shutdown_keyidx_local, "
|
|
|
|
/*30*/ "last_sent_commit_state, last_sent_commit_id, "
|
|
|
|
/*32*/ "last_tx, last_sig, last_was_revoke, first_blocknum, "
|
|
|
|
/*36*/ "min_possible_feerate, max_possible_feerate, "
|
2018-09-04 09:02:35 +02:00
|
|
|
/*38*/ "msatoshi_to_us_min, msatoshi_to_us_max, future_per_commitment_point, "
|
2019-02-21 01:01:33 +01:00
|
|
|
/*41*/ "last_sent_commit, "
|
2019-05-01 00:57:09 +02:00
|
|
|
/*42*/ "feerate_base, feerate_ppm, remote_upfront_shutdown_script";
|
2017-08-17 13:52:47 +02:00
|
|
|
|
2019-05-16 10:54:38 +02:00
|
|
|
bool wallet_channels_load_active(struct wallet *w)
|
2017-08-17 15:30:24 +02:00
|
|
|
{
|
|
|
|
bool ok = true;
|
2018-02-12 11:10:37 +01:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
/* We load all channels */
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select(w->db, "%s FROM channels;", channel_fields);
|
2018-02-18 13:53:46 +01:00
|
|
|
|
|
|
|
w->max_channel_dbid = 0;
|
2017-08-17 15:30:24 +02:00
|
|
|
|
|
|
|
int count = 0;
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2019-05-16 10:54:38 +02:00
|
|
|
struct channel *c = wallet_stmt2channel(w, stmt);
|
2018-02-12 11:12:55 +01:00
|
|
|
if (!c) {
|
|
|
|
ok = false;
|
2019-03-15 03:50:18 +01:00
|
|
|
db_stmt_done(stmt);
|
2018-02-12 11:12:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-02-18 13:53:46 +01:00
|
|
|
if (c->dbid > w->max_channel_dbid)
|
|
|
|
w->max_channel_dbid = c->dbid;
|
2017-08-17 15:30:24 +02:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
log_debug(w->log, "Loaded %d channels from DB", count);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
static
|
|
|
|
void wallet_channel_stats_incr_x(struct wallet *w,
|
|
|
|
char const *dir,
|
|
|
|
char const *typ,
|
|
|
|
u64 cdbid,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat msat)
|
2018-03-24 09:26:08 +01:00
|
|
|
{
|
|
|
|
char const *payments_stat = tal_fmt(tmpctx, "%s_payments_%s",
|
|
|
|
dir, typ);
|
|
|
|
char const *msatoshi_stat = tal_fmt(tmpctx, "%s_msatoshi_%s",
|
|
|
|
dir, typ);
|
|
|
|
char const *qry = tal_fmt(tmpctx,
|
|
|
|
"UPDATE channels"
|
|
|
|
" SET %s = COALESCE(%s, 0) + 1"
|
|
|
|
" , %s = COALESCE(%s, 0) + %"PRIu64""
|
|
|
|
" WHERE id = %"PRIu64";",
|
|
|
|
payments_stat, payments_stat,
|
2019-02-21 04:45:56 +01:00
|
|
|
msatoshi_stat, msatoshi_stat, msat.millisatoshis, /* Raw: db access */
|
2018-03-24 09:26:08 +01:00
|
|
|
cdbid);
|
|
|
|
sqlite3_stmt *stmt = db_prepare(w->db, qry);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 id,
|
|
|
|
struct amount_msat m)
|
2018-03-24 09:26:08 +01:00
|
|
|
{
|
|
|
|
wallet_channel_stats_incr_x(w, "in", "offered", id, m);
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 id,
|
|
|
|
struct amount_msat m)
|
2018-03-24 09:26:08 +01:00
|
|
|
{
|
|
|
|
wallet_channel_stats_incr_x(w, "in", "fulfilled", id, m);
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 id,
|
|
|
|
struct amount_msat m)
|
2018-03-24 09:26:08 +01:00
|
|
|
{
|
|
|
|
wallet_channel_stats_incr_x(w, "out", "offered", id, m);
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 id,
|
|
|
|
struct amount_msat m)
|
2018-03-24 09:26:08 +01:00
|
|
|
{
|
|
|
|
wallet_channel_stats_incr_x(w, "out", "fulfilled", id, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_channel_stats_load(struct wallet *w,
|
|
|
|
u64 id,
|
|
|
|
struct channel_stats *stats)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-10-12 14:58:36 +02:00
|
|
|
int res;
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
|
|
|
" in_payments_offered, in_payments_fulfilled"
|
|
|
|
", in_msatoshi_offered, in_msatoshi_fulfilled"
|
|
|
|
", out_payments_offered, out_payments_fulfilled"
|
|
|
|
", out_msatoshi_offered, out_msatoshi_fulfilled"
|
2018-03-24 09:26:08 +01:00
|
|
|
" FROM channels"
|
|
|
|
" WHERE id = ?");
|
|
|
|
sqlite3_bind_int64(stmt, 1, id);
|
2018-10-12 14:58:36 +02:00
|
|
|
|
|
|
|
res = sqlite3_step(stmt);
|
|
|
|
|
|
|
|
/* This must succeed, since we know the channel exists */
|
|
|
|
assert(res == SQLITE_ROW);
|
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
stats->in_payments_offered = sqlite3_column_int64(stmt, 0);
|
|
|
|
stats->in_payments_fulfilled = sqlite3_column_int64(stmt, 1);
|
2019-02-21 04:45:55 +01:00
|
|
|
stats->in_msatoshi_offered = sqlite3_column_amount_msat(stmt, 2);
|
|
|
|
stats->in_msatoshi_fulfilled = sqlite3_column_amount_msat(stmt, 3);
|
2018-03-24 09:26:08 +01:00
|
|
|
stats->out_payments_offered = sqlite3_column_int64(stmt, 4);
|
|
|
|
stats->out_payments_fulfilled = sqlite3_column_int64(stmt, 5);
|
2019-02-21 04:45:55 +01:00
|
|
|
stats->out_msatoshi_offered = sqlite3_column_amount_msat(stmt, 6);
|
|
|
|
stats->out_msatoshi_fulfilled = sqlite3_column_amount_msat(stmt, 7);
|
2018-04-27 03:40:38 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-03-24 09:26:08 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 14:47:32 +02:00
|
|
|
void wallet_blocks_heights(struct wallet *w, u32 def, u32 *min, u32 *max)
|
2018-01-03 06:26:30 +01:00
|
|
|
{
|
2018-06-04 14:47:32 +02:00
|
|
|
assert(min != NULL && max != NULL);
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(w->db, "MIN(height), MAX(height) FROM blocks;");
|
|
|
|
|
|
|
|
*min = def;
|
|
|
|
*max = def;
|
2018-01-03 06:26:30 +01:00
|
|
|
|
2018-04-17 23:31:30 +02:00
|
|
|
/* If we ever processed a block we'll get the latest block in the chain */
|
2019-03-15 03:50:18 +01:00
|
|
|
if (db_select_step(w->db, stmt)) {
|
|
|
|
if (sqlite3_column_type(stmt, 0) != SQLITE_NULL) {
|
|
|
|
*min = sqlite3_column_int(stmt, 0);
|
|
|
|
*max = sqlite3_column_int(stmt, 1);
|
|
|
|
}
|
|
|
|
db_stmt_done(stmt);
|
2018-04-27 03:40:38 +02:00
|
|
|
}
|
2018-01-03 06:26:30 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:54:46 +01:00
|
|
|
static void wallet_channel_config_insert(struct wallet *w,
|
|
|
|
struct channel_config *cc)
|
2017-08-05 14:45:42 +02:00
|
|
|
{
|
2017-10-20 19:04:43 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2017-08-05 14:45:42 +02:00
|
|
|
|
2018-02-18 13:54:46 +01:00
|
|
|
assert(cc->id == 0);
|
|
|
|
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO channel_configs DEFAULT VALUES;");
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
cc->id = sqlite3_last_insert_rowid(w->db->sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wallet_channel_config_save(struct wallet *w,
|
|
|
|
const struct channel_config *cc)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
assert(cc->id != 0);
|
2017-10-20 19:04:43 +02:00
|
|
|
stmt = db_prepare(w->db, "UPDATE channel_configs SET"
|
|
|
|
" dust_limit_satoshis=?,"
|
|
|
|
" max_htlc_value_in_flight_msat=?,"
|
|
|
|
" channel_reserve_satoshis=?,"
|
|
|
|
" htlc_minimum_msat=?,"
|
|
|
|
" to_self_delay=?,"
|
|
|
|
" max_accepted_htlcs=?"
|
|
|
|
" WHERE id=?;");
|
2019-02-21 04:45:54 +01:00
|
|
|
sqlite3_bind_amount_sat(stmt, 1, cc->dust_limit);
|
|
|
|
sqlite3_bind_amount_msat(stmt, 2, cc->max_htlc_value_in_flight);
|
|
|
|
sqlite3_bind_amount_sat(stmt, 3, cc->channel_reserve);
|
|
|
|
sqlite3_bind_amount_msat(stmt, 4, cc->htlc_minimum);
|
2017-10-20 19:04:43 +02:00
|
|
|
sqlite3_bind_int(stmt, 5, cc->to_self_delay);
|
|
|
|
sqlite3_bind_int(stmt, 6, cc->max_accepted_htlcs);
|
|
|
|
sqlite3_bind_int64(stmt, 7, cc->id);
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(w->db, stmt);
|
2017-08-05 14:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wallet_channel_config_load(struct wallet *w, const u64 id,
|
|
|
|
struct channel_config *cc)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
int col = 1;
|
|
|
|
const char *query =
|
2019-03-15 03:50:18 +01:00
|
|
|
"id, dust_limit_satoshis, max_htlc_value_in_flight_msat, "
|
2017-08-05 14:45:42 +02:00
|
|
|
"channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, "
|
|
|
|
"max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";";
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select(w->db, query, id);
|
|
|
|
if (!db_select_step(w->db, stmt))
|
2017-08-05 14:45:42 +02:00
|
|
|
return false;
|
2019-03-15 03:50:18 +01:00
|
|
|
|
2017-08-05 14:45:42 +02:00
|
|
|
cc->id = id;
|
2019-02-21 04:45:54 +01:00
|
|
|
cc->dust_limit = sqlite3_column_amount_sat(stmt, col++);
|
|
|
|
cc->max_htlc_value_in_flight = sqlite3_column_amount_msat(stmt, col++);
|
|
|
|
cc->channel_reserve = sqlite3_column_amount_sat(stmt, col++);
|
|
|
|
cc->htlc_minimum = sqlite3_column_amount_msat(stmt, col++);
|
2017-08-05 14:45:42 +02:00
|
|
|
cc->to_self_delay = sqlite3_column_int(stmt, col++);
|
|
|
|
cc->max_accepted_htlcs = sqlite3_column_int(stmt, col++);
|
|
|
|
assert(col == 7);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2017-08-05 14:45:42 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
u64 wallet_get_channel_dbid(struct wallet *wallet)
|
|
|
|
{
|
|
|
|
return ++wallet->max_channel_dbid;
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:21:26 +02:00
|
|
|
/* When we receive the remote announcement message, we will also call this function */
|
|
|
|
void wallet_announcement_save(struct wallet *w, u64 id,
|
|
|
|
secp256k1_ecdsa_signature *remote_ann_node_sig,
|
|
|
|
secp256k1_ecdsa_signature *remote_ann_bitcoin_sig)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
stmt = db_prepare(w->db, "UPDATE channels SET"
|
|
|
|
" remote_ann_node_sig=?,"
|
|
|
|
" remote_ann_bitcoin_sig=?"
|
|
|
|
" WHERE id=?");
|
|
|
|
|
|
|
|
sqlite3_bind_signature(stmt, 1, remote_ann_node_sig);
|
|
|
|
sqlite3_bind_signature(stmt, 2, remote_ann_bitcoin_sig);
|
|
|
|
sqlite3_bind_int64(stmt, 3, id);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
void wallet_channel_save(struct wallet *w, struct channel *chan)
|
2018-01-03 06:26:43 +01:00
|
|
|
{
|
2017-10-20 19:04:43 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2018-09-04 09:02:35 +02:00
|
|
|
u8 *last_sent_commit;
|
2018-02-06 12:15:26 +01:00
|
|
|
assert(chan->first_blocknum);
|
2017-10-20 19:04:43 +02:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
wallet_channel_config_save(w, &chan->our_config);
|
2017-08-05 15:49:36 +02:00
|
|
|
|
2017-12-13 21:04:09 +01:00
|
|
|
stmt = db_prepare(w->db, "UPDATE channels SET"
|
|
|
|
" shachain_remote_id=?,"
|
|
|
|
" short_channel_id=?,"
|
|
|
|
" state=?,"
|
|
|
|
" funder=?,"
|
|
|
|
" channel_flags=?,"
|
|
|
|
" minimum_depth=?,"
|
|
|
|
" next_index_local=?,"
|
|
|
|
" next_index_remote=?,"
|
|
|
|
" next_htlc_id=?,"
|
|
|
|
" funding_tx_id=?,"
|
|
|
|
" funding_tx_outnum=?,"
|
|
|
|
" funding_satoshi=?,"
|
|
|
|
" funding_locked_remote=?,"
|
|
|
|
" push_msatoshi=?,"
|
|
|
|
" msatoshi_local=?,"
|
|
|
|
" shutdown_scriptpubkey_remote=?,"
|
|
|
|
" shutdown_keyidx_local=?,"
|
|
|
|
" channel_config_local=?,"
|
2018-01-13 14:25:49 +01:00
|
|
|
" last_tx=?, last_sig=?,"
|
2018-04-03 09:19:39 +02:00
|
|
|
" last_was_revoke=?,"
|
|
|
|
" min_possible_feerate=?,"
|
2018-03-31 02:21:13 +02:00
|
|
|
" max_possible_feerate=?,"
|
|
|
|
" msatoshi_to_us_min=?,"
|
2019-02-21 01:01:33 +01:00
|
|
|
" msatoshi_to_us_max=?,"
|
|
|
|
" feerate_base=?,"
|
2019-05-01 00:57:09 +02:00
|
|
|
" feerate_ppm=?,"
|
|
|
|
" remote_upfront_shutdown_script=?"
|
2017-12-13 21:04:09 +01:00
|
|
|
" WHERE id=?");
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, chan->their_shachain.id);
|
|
|
|
if (chan->scid)
|
|
|
|
sqlite3_bind_short_channel_id(stmt, 2, chan->scid);
|
2018-06-15 12:42:40 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 2);
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int(stmt, 3, chan->state);
|
|
|
|
sqlite3_bind_int(stmt, 4, chan->funder);
|
|
|
|
sqlite3_bind_int(stmt, 5, chan->channel_flags);
|
|
|
|
sqlite3_bind_int(stmt, 6, chan->minimum_depth);
|
|
|
|
|
|
|
|
sqlite3_bind_int64(stmt, 7, chan->next_index[LOCAL]);
|
|
|
|
sqlite3_bind_int64(stmt, 8, chan->next_index[REMOTE]);
|
|
|
|
sqlite3_bind_int64(stmt, 9, chan->next_htlc_id);
|
|
|
|
|
2018-02-19 02:06:12 +01:00
|
|
|
sqlite3_bind_sha256_double(stmt, 10, &chan->funding_txid.shad);
|
2018-02-12 11:12:55 +01:00
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 11, chan->funding_outnum);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_sat(stmt, 12, chan->funding);
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int(stmt, 13, chan->remote_funding_locked);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 14, chan->push);
|
|
|
|
sqlite3_bind_amount_msat(stmt, 15, chan->our_msat);
|
2018-02-12 11:12:55 +01:00
|
|
|
|
|
|
|
if (chan->remote_shutdown_scriptpubkey)
|
|
|
|
sqlite3_bind_blob(stmt, 16, chan->remote_shutdown_scriptpubkey,
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_count(chan->remote_shutdown_scriptpubkey),
|
2017-10-20 19:04:43 +02:00
|
|
|
SQLITE_TRANSIENT);
|
2018-06-15 12:49:00 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 16);
|
2017-10-20 19:04:43 +02:00
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
sqlite3_bind_int64(stmt, 17, chan->final_key_idx);
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int64(stmt, 18, chan->our_config.id);
|
2018-02-19 02:06:12 +01:00
|
|
|
sqlite3_bind_tx(stmt, 19, chan->last_tx);
|
2018-12-03 00:15:06 +01:00
|
|
|
sqlite3_bind_signature(stmt, 20, &chan->last_sig.s);
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int(stmt, 21, chan->last_was_revoke);
|
2018-04-03 09:19:39 +02:00
|
|
|
sqlite3_bind_int(stmt, 22, chan->min_possible_feerate);
|
|
|
|
sqlite3_bind_int(stmt, 23, chan->max_possible_feerate);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 24, chan->msat_to_us_min);
|
|
|
|
sqlite3_bind_amount_msat(stmt, 25, chan->msat_to_us_max);
|
2019-02-21 01:01:33 +01:00
|
|
|
sqlite3_bind_int(stmt, 26, chan->feerate_base);
|
|
|
|
sqlite3_bind_int(stmt, 27, chan->feerate_ppm);
|
2019-05-01 00:57:09 +02:00
|
|
|
if (chan->remote_upfront_shutdown_script)
|
|
|
|
sqlite3_bind_blob(stmt, 28, chan->remote_upfront_shutdown_script,
|
|
|
|
tal_count(chan->remote_upfront_shutdown_script),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 28);
|
|
|
|
sqlite3_bind_int64(stmt, 29, chan->dbid);
|
2017-10-20 19:04:43 +02:00
|
|
|
db_exec_prepared(w->db, stmt);
|
2017-08-05 12:26:18 +02:00
|
|
|
|
2018-02-19 02:06:12 +01:00
|
|
|
wallet_channel_config_save(w, &chan->channel_info.their_config);
|
|
|
|
stmt = db_prepare(w->db, "UPDATE channels SET"
|
|
|
|
" fundingkey_remote=?,"
|
|
|
|
" revocation_basepoint_remote=?,"
|
|
|
|
" payment_basepoint_remote=?,"
|
|
|
|
" htlc_basepoint_remote=?,"
|
|
|
|
" delayed_payment_basepoint_remote=?,"
|
|
|
|
" per_commit_remote=?,"
|
|
|
|
" old_per_commit_remote=?,"
|
|
|
|
" local_feerate_per_kw=?,"
|
|
|
|
" remote_feerate_per_kw=?,"
|
2018-08-17 07:06:36 +02:00
|
|
|
" channel_config_remote=?,"
|
|
|
|
" future_per_commitment_point=?"
|
2018-02-19 02:06:12 +01:00
|
|
|
" WHERE id=?");
|
|
|
|
sqlite3_bind_pubkey(stmt, 1, &chan->channel_info.remote_fundingkey);
|
|
|
|
sqlite3_bind_pubkey(stmt, 2, &chan->channel_info.theirbase.revocation);
|
|
|
|
sqlite3_bind_pubkey(stmt, 3, &chan->channel_info.theirbase.payment);
|
|
|
|
sqlite3_bind_pubkey(stmt, 4, &chan->channel_info.theirbase.htlc);
|
|
|
|
sqlite3_bind_pubkey(stmt, 5, &chan->channel_info.theirbase.delayed_payment);
|
|
|
|
sqlite3_bind_pubkey(stmt, 6, &chan->channel_info.remote_per_commit);
|
|
|
|
sqlite3_bind_pubkey(stmt, 7, &chan->channel_info.old_remote_per_commit);
|
|
|
|
sqlite3_bind_int(stmt, 8, chan->channel_info.feerate_per_kw[LOCAL]);
|
|
|
|
sqlite3_bind_int(stmt, 9, chan->channel_info.feerate_per_kw[REMOTE]);
|
|
|
|
sqlite3_bind_int64(stmt, 10, chan->channel_info.their_config.id);
|
2018-08-17 07:06:36 +02:00
|
|
|
if (chan->future_per_commitment_point)
|
|
|
|
sqlite3_bind_pubkey(stmt, 11, chan->future_per_commitment_point);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 11);
|
|
|
|
sqlite3_bind_int64(stmt, 12, chan->dbid);
|
2018-02-19 02:06:12 +01:00
|
|
|
db_exec_prepared(w->db, stmt);
|
2017-08-05 12:26:18 +02:00
|
|
|
|
|
|
|
/* If we have a last_sent_commit, store it */
|
2018-09-04 09:02:35 +02:00
|
|
|
last_sent_commit = tal_arr(tmpctx, u8, 0);
|
|
|
|
for (size_t i = 0; i < tal_count(chan->last_sent_commit); i++)
|
|
|
|
towire_changed_htlc(&last_sent_commit,
|
|
|
|
&chan->last_sent_commit[i]);
|
|
|
|
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"UPDATE channels SET"
|
|
|
|
" last_sent_commit=?"
|
|
|
|
" WHERE id=?");
|
|
|
|
if (tal_count(last_sent_commit))
|
|
|
|
sqlite3_bind_blob(stmt, 1,
|
|
|
|
last_sent_commit, tal_count(last_sent_commit),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 1);
|
|
|
|
sqlite3_bind_int64(stmt, 2, chan->dbid);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2017-08-05 12:26:18 +02:00
|
|
|
}
|
2017-09-01 14:33:21 +02:00
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
void wallet_channel_insert(struct wallet *w, struct channel *chan)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
if (chan->peer->dbid == 0) {
|
|
|
|
/* Need to create the peer first */
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO peers (node_id, address) VALUES (?, ?);");
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_bind_node_id(stmt, 1, &chan->peer->id);
|
2018-10-26 08:01:30 +02:00
|
|
|
sqlite3_bind_text(stmt, 2,
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal, &chan->peer->addr),
|
|
|
|
-1, SQLITE_TRANSIENT);
|
2018-02-18 13:53:46 +01:00
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
chan->peer->dbid = sqlite3_last_insert_rowid(w->db->sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert a stub, that we update, unifies INSERT and UPDATE paths */
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO channels ("
|
|
|
|
"peer_id, first_blocknum, id) VALUES (?, ?, ?);");
|
|
|
|
sqlite3_bind_int64(stmt, 1, chan->peer->dbid);
|
|
|
|
sqlite3_bind_int(stmt, 2, chan->first_blocknum);
|
|
|
|
sqlite3_bind_int(stmt, 3, chan->dbid);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
|
2018-02-18 13:54:46 +01:00
|
|
|
wallet_channel_config_insert(w, &chan->our_config);
|
2018-02-19 02:06:12 +01:00
|
|
|
wallet_channel_config_insert(w, &chan->channel_info.their_config);
|
2018-02-18 13:54:46 +01:00
|
|
|
wallet_shachain_init(w, &chan->their_shachain);
|
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
/* Now save path as normal */
|
|
|
|
wallet_channel_save(w, chan);
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:53:04 +01:00
|
|
|
void wallet_channel_delete(struct wallet *w, u64 wallet_id)
|
2018-01-23 14:48:09 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-02-12 11:10:40 +01:00
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"DELETE FROM channels WHERE id=?");
|
2018-01-23 14:48:09 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, wallet_id);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2018-02-14 02:53:04 +01:00
|
|
|
}
|
2018-02-12 11:10:40 +01:00
|
|
|
|
2018-02-14 02:53:04 +01:00
|
|
|
void wallet_peer_delete(struct wallet *w, u64 peer_dbid)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
/* Must not have any channels still using this peer */
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select(w->db,
|
|
|
|
"* FROM channels WHERE peer_id = %"PRIu64,
|
|
|
|
peer_dbid);
|
|
|
|
if (db_select_step(w->db, stmt))
|
|
|
|
fatal("We have channels using peer %"PRIu64, peer_dbid);
|
2018-02-14 02:53:04 +01:00
|
|
|
|
|
|
|
stmt = db_prepare(w->db, "DELETE FROM peers WHERE id=?");
|
2018-02-12 11:10:40 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, peer_dbid);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2018-01-23 14:48:09 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 05:03:08 +02:00
|
|
|
void wallet_confirm_tx(struct wallet *w,
|
|
|
|
const struct bitcoin_txid *txid,
|
|
|
|
const u32 confirmation_height)
|
2018-03-02 14:33:01 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
assert(confirmation_height > 0);
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"UPDATE outputs "
|
|
|
|
"SET confirmation_height = ? "
|
2018-08-13 05:03:08 +02:00
|
|
|
"WHERE prev_out_tx = ?");
|
2018-03-02 14:33:01 +01:00
|
|
|
sqlite3_bind_int(stmt, 1, confirmation_height);
|
|
|
|
sqlite3_bind_sha256_double(stmt, 2, &txid->shad);
|
|
|
|
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:33:21 +02:00
|
|
|
int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
|
2019-02-21 04:45:55 +01:00
|
|
|
const u32 *blockheight,
|
|
|
|
struct amount_sat *total)
|
2017-09-01 14:33:21 +02:00
|
|
|
{
|
|
|
|
int num_utxos = 0;
|
2019-02-21 04:45:55 +01:00
|
|
|
|
|
|
|
*total = AMOUNT_SAT(0);
|
2019-03-25 11:35:56 +01:00
|
|
|
for (size_t output = 0; output < tx->wtx->num_outputs; output++) {
|
2017-09-01 14:33:21 +02:00
|
|
|
struct utxo *utxo;
|
|
|
|
u32 index;
|
|
|
|
bool is_p2sh;
|
2019-03-25 11:35:56 +01:00
|
|
|
const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, output);
|
2017-09-01 14:33:21 +02:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
|
|
|
|
if (!wallet_can_spend(w, script, &index,
|
2017-09-01 14:33:21 +02:00
|
|
|
&is_p2sh))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
utxo = tal(w, struct utxo);
|
|
|
|
utxo->keyindex = index;
|
|
|
|
utxo->is_p2sh = is_p2sh;
|
2019-03-25 11:35:56 +01:00
|
|
|
utxo->amount = bitcoin_tx_output_get_amount(tx, output);
|
2017-09-01 14:33:21 +02:00
|
|
|
utxo->status = output_state_available;
|
|
|
|
bitcoin_txid(tx, &utxo->txid);
|
|
|
|
utxo->outnum = output;
|
2017-12-20 12:44:00 +01:00
|
|
|
utxo->close_info = NULL;
|
2018-02-26 11:36:48 +01:00
|
|
|
|
2019-02-15 17:18:39 +01:00
|
|
|
utxo->blockheight = blockheight ? blockheight : NULL;
|
2018-02-26 11:36:48 +01:00
|
|
|
utxo->spendheight = NULL;
|
2019-03-25 11:35:56 +01:00
|
|
|
utxo->scriptPubkey = tal_dup_arr(utxo, u8, script, tal_bytelen(script), 0);
|
2018-02-26 11:36:48 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
log_debug(w->log, "Owning output %zu %s (%s) txid %s%s",
|
|
|
|
output,
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-03-25 11:35:56 +01:00
|
|
|
&utxo->amount),
|
2017-09-12 13:50:47 +02:00
|
|
|
is_p2sh ? "P2SH" : "SEGWIT",
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid,
|
2019-02-15 17:18:39 +01:00
|
|
|
&utxo->txid), blockheight ? " CONFIRMED" : "");
|
2017-09-12 13:50:47 +02:00
|
|
|
|
|
|
|
if (!wallet_add_utxo(w, utxo, is_p2sh ? p2sh_wpkh : our_change)) {
|
2018-03-02 14:33:01 +01:00
|
|
|
/* In case we already know the output, make
|
|
|
|
* sure we actually track its
|
|
|
|
* blockheight. This can happen when we grab
|
|
|
|
* the output from a transaction we created
|
2018-03-23 11:01:43 +01:00
|
|
|
* ourselves. */
|
2018-03-22 00:13:16 +01:00
|
|
|
if (blockheight)
|
2018-08-13 05:03:08 +02:00
|
|
|
wallet_confirm_tx(w, &utxo->txid, *blockheight);
|
2017-09-01 14:33:21 +02:00
|
|
|
tal_free(utxo);
|
2018-03-02 15:18:20 +01:00
|
|
|
continue;
|
2017-09-01 14:33:21 +02:00
|
|
|
}
|
2018-02-26 14:57:13 +01:00
|
|
|
outpointfilter_add(w->owned_outpoints, &utxo->txid, utxo->outnum);
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_add(total, *total, utxo->amount))
|
|
|
|
fatal("Cannot add utxo output %zu/%zu %s + %s",
|
2019-03-25 11:35:56 +01:00
|
|
|
output, tx->wtx->num_outputs,
|
2019-02-21 04:45:55 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat, total),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&utxo->amount));
|
2017-12-13 21:03:24 +01:00
|
|
|
tal_free(utxo);
|
2017-09-01 14:33:21 +02:00
|
|
|
num_utxos++;
|
|
|
|
}
|
|
|
|
return num_utxos;
|
|
|
|
}
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
void wallet_htlc_save_in(struct wallet *wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
const struct channel *chan, struct htlc_in *in)
|
2017-09-14 21:27:41 +02:00
|
|
|
{
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_stmt *stmt;
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2017-10-30 16:49:25 +01:00
|
|
|
stmt = db_prepare(
|
|
|
|
wallet->db,
|
|
|
|
"INSERT INTO channel_htlcs ("
|
|
|
|
" channel_id,"
|
|
|
|
" channel_htlc_id, "
|
|
|
|
" direction,"
|
|
|
|
" msatoshi,"
|
|
|
|
" cltv_expiry,"
|
|
|
|
" payment_hash, "
|
|
|
|
" payment_key,"
|
|
|
|
" hstate,"
|
|
|
|
" shared_secret,"
|
2019-04-03 19:01:47 +02:00
|
|
|
" routing_onion,"
|
|
|
|
" received_time) VALUES "
|
|
|
|
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2017-10-30 16:49:25 +01:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, chan->dbid);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int64(stmt, 2, in->key.id);
|
|
|
|
sqlite3_bind_int(stmt, 3, DIRECTION_INCOMING);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 4, in->msat);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 5, in->cltv_expiry);
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 6, &in->payment_hash);
|
|
|
|
|
2017-10-30 16:49:25 +01:00
|
|
|
if (in->preimage)
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_bind_preimage(stmt, 7, in->preimage);
|
2018-06-15 12:54:22 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 7);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 8, in->hstate);
|
|
|
|
|
2019-01-08 01:19:50 +01:00
|
|
|
if (!in->shared_secret)
|
|
|
|
sqlite3_bind_null(stmt, 9);
|
|
|
|
else
|
|
|
|
sqlite3_bind_blob(stmt, 9, in->shared_secret,
|
|
|
|
sizeof(*in->shared_secret), SQLITE_TRANSIENT);
|
2017-10-30 22:44:00 +01:00
|
|
|
|
|
|
|
sqlite3_bind_blob(stmt, 10, &in->onion_routing_packet,
|
|
|
|
sizeof(in->onion_routing_packet), SQLITE_TRANSIENT);
|
2017-10-30 16:49:25 +01:00
|
|
|
|
2019-04-03 19:01:47 +02:00
|
|
|
sqlite3_bind_timeabs(stmt, 11, in->received_time);
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
in->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
|
2017-09-14 21:27:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
void wallet_htlc_save_out(struct wallet *wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
const struct channel *chan,
|
2017-09-14 21:27:41 +02:00
|
|
|
struct htlc_out *out)
|
|
|
|
{
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_stmt *stmt;
|
2017-09-14 21:27:41 +02:00
|
|
|
|
|
|
|
/* We absolutely need the incoming HTLC to be persisted before
|
|
|
|
* we can persist it's dependent */
|
|
|
|
assert(out->in == NULL || out->in->dbid != 0);
|
2017-09-25 21:50:20 +02:00
|
|
|
out->origin_htlc_id = out->in?out->in->dbid:0;
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2017-10-30 16:49:25 +01:00
|
|
|
stmt = db_prepare(
|
|
|
|
wallet->db,
|
|
|
|
"INSERT INTO channel_htlcs ("
|
|
|
|
" channel_id,"
|
|
|
|
" channel_htlc_id,"
|
|
|
|
" direction,"
|
|
|
|
" origin_htlc,"
|
|
|
|
" msatoshi,"
|
|
|
|
" cltv_expiry,"
|
|
|
|
" payment_hash,"
|
|
|
|
" payment_key,"
|
|
|
|
" hstate,"
|
|
|
|
" routing_onion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, chan->dbid);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int64(stmt, 2, out->key.id);
|
|
|
|
sqlite3_bind_int(stmt, 3, DIRECTION_OUTGOING);
|
|
|
|
if (out->in)
|
|
|
|
sqlite3_bind_int64(stmt, 4, out->in->dbid);
|
2018-06-14 15:19:02 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 4);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 5, out->msat);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 6, out->cltv_expiry);
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 7, &out->payment_hash);
|
|
|
|
|
2017-10-30 16:49:25 +01:00
|
|
|
if (out->preimage)
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_bind_preimage(stmt, 8,out->preimage);
|
2018-06-15 12:56:44 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 8);
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 9, out->hstate);
|
2017-10-30 22:44:00 +01:00
|
|
|
|
|
|
|
sqlite3_bind_blob(stmt, 10, &out->onion_routing_packet,
|
|
|
|
sizeof(out->onion_routing_packet), SQLITE_TRANSIENT);
|
2017-10-30 16:49:25 +01:00
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
out->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
|
2017-09-14 21:27:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
|
2017-09-14 21:27:41 +02:00
|
|
|
const enum htlc_state new_state,
|
2018-10-09 10:56:52 +02:00
|
|
|
const struct preimage *payment_key,
|
|
|
|
enum onion_type failcode, const u8 *failuremsg)
|
2017-09-14 21:27:41 +02:00
|
|
|
{
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_stmt *stmt;
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2017-09-18 20:18:29 +02:00
|
|
|
/* The database ID must be set by a previous call to
|
|
|
|
* `wallet_htlc_save_*` */
|
|
|
|
assert(htlc_dbid);
|
2017-10-30 16:49:25 +01:00
|
|
|
stmt = db_prepare(
|
2017-10-30 22:44:00 +01:00
|
|
|
wallet->db,
|
2018-10-09 10:56:52 +02:00
|
|
|
"UPDATE channel_htlcs SET hstate=?, payment_key=?, malformed_onion=?, failuremsg=? WHERE id=?");
|
2017-10-30 22:44:00 +01:00
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
/* FIXME: htlc_state_in_db */
|
2017-10-30 16:49:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 1, new_state);
|
2018-10-09 10:56:52 +02:00
|
|
|
sqlite3_bind_int64(stmt, 5, htlc_dbid);
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2017-10-30 22:44:00 +01:00
|
|
|
if (payment_key)
|
|
|
|
sqlite3_bind_preimage(stmt, 2, payment_key);
|
2018-06-15 12:57:56 +02:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 2);
|
2017-10-30 22:44:00 +01:00
|
|
|
|
2018-10-09 10:56:52 +02:00
|
|
|
sqlite3_bind_int(stmt, 3, failcode);
|
|
|
|
if (failuremsg)
|
|
|
|
sqlite3_bind_blob(stmt, 4,
|
|
|
|
failuremsg, tal_bytelen(failuremsg),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 4);
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2017-09-14 21:27:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
/* origin_htlc is htlc_out only, shared_secret is htlc_in only */
|
|
|
|
#define HTLC_FIELDS \
|
|
|
|
"id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " \
|
|
|
|
"payment_hash, payment_key, routing_onion, " \
|
|
|
|
"failuremsg, malformed_onion," \
|
2019-04-03 19:01:47 +02:00
|
|
|
"origin_htlc, shared_secret, received_time"
|
2018-10-09 10:55:52 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool wallet_stmt2htlc_in(struct channel *channel,
|
2017-09-14 21:27:41 +02:00
|
|
|
sqlite3_stmt *stmt, struct htlc_in *in)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
in->dbid = sqlite3_column_int64(stmt, 0);
|
|
|
|
in->key.id = sqlite3_column_int64(stmt, 1);
|
2018-02-12 11:13:04 +01:00
|
|
|
in->key.channel = channel;
|
2019-02-21 04:45:55 +01:00
|
|
|
in->msat = sqlite3_column_amount_msat(stmt, 2);
|
2017-09-14 21:27:41 +02:00
|
|
|
in->cltv_expiry = sqlite3_column_int(stmt, 3);
|
|
|
|
in->hstate = sqlite3_column_int(stmt, 4);
|
|
|
|
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_column_sha256(stmt, 5, &in->payment_hash);
|
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
|
2017-09-14 21:27:41 +02:00
|
|
|
in->preimage = tal(in, struct preimage);
|
2018-10-09 10:55:52 +02:00
|
|
|
sqlite3_column_preimage(stmt, 6, in->preimage);
|
2017-09-14 21:27:41 +02:00
|
|
|
} else {
|
|
|
|
in->preimage = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
assert(sqlite3_column_bytes(stmt, 7) == sizeof(in->onion_routing_packet));
|
|
|
|
memcpy(&in->onion_routing_packet, sqlite3_column_blob(stmt, 7),
|
2017-10-30 22:44:00 +01:00
|
|
|
sizeof(in->onion_routing_packet));
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
in->failuremsg = sqlite3_column_arr(in, stmt, 8, u8);
|
|
|
|
in->failcode = sqlite3_column_int(stmt, 9);
|
|
|
|
|
2019-01-08 01:19:50 +01:00
|
|
|
if (sqlite3_column_type(stmt, 11) == SQLITE_NULL) {
|
|
|
|
in->shared_secret = NULL;
|
|
|
|
} else {
|
|
|
|
assert(sqlite3_column_bytes(stmt, 11) == sizeof(struct secret));
|
|
|
|
in->shared_secret = tal(in, struct secret);
|
|
|
|
memcpy(in->shared_secret, sqlite3_column_blob(stmt, 11),
|
|
|
|
sizeof(struct secret));
|
|
|
|
#ifdef COMPAT_V062
|
|
|
|
if (memeqzero(in->shared_secret, sizeof(*in->shared_secret)))
|
|
|
|
in->shared_secret = tal_free(in->shared_secret);
|
|
|
|
#endif
|
|
|
|
}
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2019-04-03 19:01:47 +02:00
|
|
|
in->received_time = sqlite3_column_timeabs(stmt, 12);
|
|
|
|
|
2017-09-14 21:27:41 +02:00
|
|
|
return ok;
|
|
|
|
}
|
2018-10-09 10:55:52 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool wallet_stmt2htlc_out(struct channel *channel,
|
2017-09-14 21:27:41 +02:00
|
|
|
sqlite3_stmt *stmt, struct htlc_out *out)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
out->dbid = sqlite3_column_int64(stmt, 0);
|
|
|
|
out->key.id = sqlite3_column_int64(stmt, 1);
|
2018-02-12 11:13:04 +01:00
|
|
|
out->key.channel = channel;
|
2019-02-21 04:45:55 +01:00
|
|
|
out->msat = sqlite3_column_amount_msat(stmt, 2);
|
2017-09-14 21:27:41 +02:00
|
|
|
out->cltv_expiry = sqlite3_column_int(stmt, 3);
|
|
|
|
out->hstate = sqlite3_column_int(stmt, 4);
|
2017-10-30 22:44:00 +01:00
|
|
|
sqlite3_column_sha256(stmt, 5, &out->payment_hash);
|
2017-09-14 21:27:41 +02:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
|
|
|
|
out->preimage = tal(out, struct preimage);
|
2018-10-09 10:55:52 +02:00
|
|
|
sqlite3_column_preimage(stmt, 6, out->preimage);
|
2017-09-14 21:27:41 +02:00
|
|
|
} else {
|
|
|
|
out->preimage = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
assert(sqlite3_column_bytes(stmt, 7) == sizeof(out->onion_routing_packet));
|
|
|
|
memcpy(&out->onion_routing_packet, sqlite3_column_blob(stmt, 7),
|
2017-10-30 22:44:00 +01:00
|
|
|
sizeof(out->onion_routing_packet));
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2018-10-09 10:55:52 +02:00
|
|
|
out->failuremsg = sqlite3_column_arr(out, stmt, 8, u8);
|
|
|
|
out->failcode = sqlite3_column_int(stmt, 9);
|
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 10) != SQLITE_NULL) {
|
|
|
|
out->origin_htlc_id = sqlite3_column_int64(stmt, 10);
|
2018-10-09 22:21:31 +02:00
|
|
|
out->am_origin = false;
|
2018-10-09 10:55:52 +02:00
|
|
|
} else {
|
|
|
|
out->origin_htlc_id = 0;
|
2018-10-09 22:21:31 +02:00
|
|
|
out->am_origin = true;
|
2018-10-09 10:55:52 +02:00
|
|
|
}
|
2017-09-14 21:27:41 +02:00
|
|
|
|
|
|
|
/* Need to defer wiring until we can look up all incoming
|
|
|
|
* htlcs, will wire using origin_htlc_id */
|
|
|
|
out->in = NULL;
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:54:52 +02:00
|
|
|
static void fixup_hin(struct wallet *wallet, struct htlc_in *hin)
|
|
|
|
{
|
2018-10-09 10:57:52 +02:00
|
|
|
/* We don't save the outgoing channel which failed; probably not worth
|
|
|
|
* it for this corner case. So we can't set hin->failoutchannel to
|
|
|
|
* tell channeld what update to send, thus we turn those into a
|
|
|
|
* WIRE_TEMPORARY_NODE_FAILURE. */
|
|
|
|
if (hin->failcode & UPDATE)
|
|
|
|
hin->failcode = WIRE_TEMPORARY_NODE_FAILURE;
|
|
|
|
|
|
|
|
/* We didn't used to save failcore, failuremsg... */
|
|
|
|
#ifdef COMPAT_V061
|
2018-10-09 10:54:52 +02:00
|
|
|
/* We care about HTLCs being removed only, not those being added. */
|
|
|
|
if (hin->hstate < SENT_REMOVE_HTLC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Successful ones are fine. */
|
|
|
|
if (hin->preimage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Failed ones (only happens after db fixed!) OK. */
|
|
|
|
if (hin->failcode || hin->failuremsg)
|
|
|
|
return;
|
|
|
|
|
2018-10-09 10:57:52 +02:00
|
|
|
hin->failcode = WIRE_TEMPORARY_NODE_FAILURE;
|
2018-10-09 10:54:52 +02:00
|
|
|
|
|
|
|
log_broken(wallet->log, "HTLC #%"PRIu64" (%s) "
|
2019-02-21 04:45:55 +01:00
|
|
|
" for amount %s"
|
2018-10-09 10:54:52 +02:00
|
|
|
" from %s"
|
|
|
|
" is missing a resolution:"
|
2018-10-09 10:57:52 +02:00
|
|
|
" subsituting temporary node failure",
|
2018-10-09 10:54:52 +02:00
|
|
|
hin->key.id, htlc_state_name(hin->hstate),
|
2019-02-21 04:45:55 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, &hin->msat),
|
2019-04-08 11:58:32 +02:00
|
|
|
type_to_string(tmpctx, struct node_id,
|
2018-10-09 10:54:52 +02:00
|
|
|
&hin->key.channel->peer->id));
|
2018-10-09 10:56:52 +02:00
|
|
|
#endif
|
2018-10-09 10:57:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 21:27:41 +02:00
|
|
|
bool wallet_htlcs_load_for_channel(struct wallet *wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
struct channel *chan,
|
2017-09-14 21:27:41 +02:00
|
|
|
struct htlc_in_map *htlcs_in,
|
|
|
|
struct htlc_out_map *htlcs_out)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
int incount = 0, outcount = 0;
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
log_debug(wallet->log, "Loading HTLCs for channel %"PRIu64, chan->dbid);
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select(
|
2018-04-27 03:39:56 +02:00
|
|
|
wallet->db,
|
2019-03-15 03:50:18 +01:00
|
|
|
HTLC_FIELDS " FROM channel_htlcs WHERE "
|
2017-09-14 21:27:41 +02:00
|
|
|
"direction=%d AND channel_id=%" PRIu64 " AND hstate != %d",
|
2018-02-12 11:12:55 +01:00
|
|
|
DIRECTION_INCOMING, chan->dbid, SENT_REMOVE_ACK_REVOCATION);
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(wallet->db, stmt)) {
|
2017-09-14 21:27:41 +02:00
|
|
|
struct htlc_in *in = tal(chan, struct htlc_in);
|
|
|
|
ok &= wallet_stmt2htlc_in(chan, stmt, in);
|
|
|
|
connect_htlc_in(htlcs_in, in);
|
2018-10-09 10:54:52 +02:00
|
|
|
fixup_hin(wallet, in);
|
|
|
|
ok &= htlc_in_check(in, NULL) != NULL;
|
2017-09-14 21:27:41 +02:00
|
|
|
incount++;
|
|
|
|
}
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select(
|
2018-04-27 03:39:56 +02:00
|
|
|
wallet->db,
|
2019-03-15 03:50:18 +01:00
|
|
|
HTLC_FIELDS " FROM channel_htlcs WHERE "
|
2017-09-14 21:27:41 +02:00
|
|
|
"direction=%d AND channel_id=%" PRIu64 " AND hstate != %d",
|
2018-02-12 11:12:55 +01:00
|
|
|
DIRECTION_OUTGOING, chan->dbid, RCVD_REMOVE_ACK_REVOCATION);
|
2017-09-14 21:27:41 +02:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(wallet->db, stmt)) {
|
2017-09-14 21:27:41 +02:00
|
|
|
struct htlc_out *out = tal(chan, struct htlc_out);
|
|
|
|
ok &= wallet_stmt2htlc_out(chan, stmt, out);
|
|
|
|
connect_htlc_out(htlcs_out, out);
|
|
|
|
/* Cannot htlc_out_check because we haven't wired the
|
|
|
|
* dependencies in yet */
|
|
|
|
outcount++;
|
|
|
|
}
|
2019-03-15 03:50:18 +01:00
|
|
|
|
2017-09-14 21:27:41 +02:00
|
|
|
log_debug(wallet->log, "Restored %d incoming and %d outgoing HTLCS", incount, outcount);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2017-09-26 14:40:41 +02:00
|
|
|
|
2018-02-23 02:04:47 +01:00
|
|
|
bool wallet_invoice_create(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
2019-02-21 03:38:35 +01:00
|
|
|
const struct amount_msat *msat TAKES,
|
2018-03-26 02:08:16 +02:00
|
|
|
const struct json_escaped *label TAKES,
|
2018-02-28 17:26:58 +01:00
|
|
|
u64 expiry,
|
|
|
|
const char *b11enc,
|
2018-07-24 20:24:18 +02:00
|
|
|
const char *description,
|
2018-02-28 17:26:58 +01:00
|
|
|
const struct preimage *r,
|
|
|
|
const struct sha256 *rhash)
|
|
|
|
{
|
2019-02-21 03:38:35 +01:00
|
|
|
return invoices_create(wallet->invoices, pinvoice, msat, label, expiry, b11enc, description, r, rhash);
|
2018-01-14 15:15:30 +01:00
|
|
|
}
|
2018-02-23 02:04:47 +01:00
|
|
|
bool wallet_invoice_find_by_label(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
2018-03-26 02:08:16 +02:00
|
|
|
const struct json_escaped *label)
|
2017-12-26 11:50:30 +01:00
|
|
|
{
|
2018-02-23 02:04:47 +01:00
|
|
|
return invoices_find_by_label(wallet->invoices, pinvoice, label);
|
2017-12-26 11:50:30 +01:00
|
|
|
}
|
2018-04-25 01:04:33 +02:00
|
|
|
bool wallet_invoice_find_by_rhash(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
|
|
|
const struct sha256 *rhash)
|
|
|
|
{
|
|
|
|
return invoices_find_by_rhash(wallet->invoices, pinvoice, rhash);
|
|
|
|
}
|
2018-02-23 02:04:47 +01:00
|
|
|
bool wallet_invoice_find_unpaid(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
|
|
|
const struct sha256 *rhash)
|
2017-12-19 12:52:03 +01:00
|
|
|
{
|
2018-02-23 02:04:47 +01:00
|
|
|
return invoices_find_unpaid(wallet->invoices, pinvoice, rhash);
|
2017-12-19 12:52:03 +01:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
bool wallet_invoice_delete(struct wallet *wallet,
|
2018-02-23 02:04:47 +01:00
|
|
|
struct invoice invoice)
|
2017-12-19 12:52:03 +01:00
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
return invoices_delete(wallet->invoices, invoice);
|
2017-12-19 12:52:03 +01:00
|
|
|
}
|
2018-02-26 13:37:53 +01:00
|
|
|
void wallet_invoice_delete_expired(struct wallet *wallet, u64 e)
|
|
|
|
{
|
|
|
|
invoices_delete_expired(wallet->invoices, e);
|
|
|
|
}
|
2018-02-24 13:11:14 +01:00
|
|
|
bool wallet_invoice_iterate(struct wallet *wallet,
|
|
|
|
struct invoice_iterator *it)
|
2017-10-04 14:43:55 +02:00
|
|
|
{
|
2018-02-24 13:11:14 +01:00
|
|
|
return invoices_iterate(wallet->invoices, it);
|
|
|
|
}
|
2018-07-27 12:57:02 +02:00
|
|
|
const struct invoice_details *
|
|
|
|
wallet_invoice_iterator_deref(const tal_t *ctx, struct wallet *wallet,
|
|
|
|
const struct invoice_iterator *it)
|
2018-02-24 13:11:14 +01:00
|
|
|
{
|
2018-07-27 12:57:02 +02:00
|
|
|
return invoices_iterator_deref(ctx, wallet->invoices, it);
|
2017-10-04 14:43:55 +02:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
void wallet_invoice_resolve(struct wallet *wallet,
|
2018-02-23 02:04:47 +01:00
|
|
|
struct invoice invoice,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat msatoshi_received)
|
2017-10-04 14:43:55 +02:00
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
invoices_resolve(wallet->invoices, invoice, msatoshi_received);
|
2017-09-26 14:40:41 +02:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
void wallet_invoice_waitany(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
|
|
|
u64 lastpay_index,
|
|
|
|
void (*cb)(const struct invoice *, void*),
|
|
|
|
void *cbarg)
|
2017-10-05 23:29:56 +02:00
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
invoices_waitany(ctx, wallet->invoices, lastpay_index, cb, cbarg);
|
2017-10-05 23:29:56 +02:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
void wallet_invoice_waitone(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
2018-02-23 02:04:47 +01:00
|
|
|
struct invoice invoice,
|
2018-01-14 15:15:30 +01:00
|
|
|
void (*cb)(const struct invoice *, void*),
|
|
|
|
void *cbarg)
|
|
|
|
{
|
|
|
|
invoices_waitone(ctx, wallet->invoices, invoice, cb, cbarg);
|
|
|
|
}
|
2018-07-27 12:57:02 +02:00
|
|
|
|
|
|
|
const struct invoice_details *wallet_invoice_details(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
|
|
|
struct invoice invoice)
|
2018-02-20 02:07:30 +01:00
|
|
|
{
|
2018-07-27 12:57:02 +02:00
|
|
|
return invoices_get_details(ctx, wallet->invoices, invoice);
|
2018-02-20 02:07:30 +01:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
|
2017-12-15 11:29:32 +01:00
|
|
|
struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
struct channel *chan)
|
2017-10-10 13:04:32 +02:00
|
|
|
{
|
|
|
|
struct htlc_stub *stubs;
|
|
|
|
struct sha256 payment_hash;
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(wallet->db,
|
|
|
|
"channel_id, direction, cltv_expiry, channel_htlc_id, payment_hash "
|
2017-10-10 13:04:32 +02:00
|
|
|
"FROM channel_htlcs WHERE channel_id = ?;");
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
sqlite3_bind_int64(stmt, 1, chan->dbid);
|
2017-10-10 13:04:32 +02:00
|
|
|
|
|
|
|
stubs = tal_arr(ctx, struct htlc_stub, 0);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(wallet->db, stmt)) {
|
2019-01-15 04:51:27 +01:00
|
|
|
struct htlc_stub stub;
|
2017-10-10 13:04:32 +02:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
assert(sqlite3_column_int64(stmt, 0) == chan->dbid);
|
2017-10-10 13:04:32 +02:00
|
|
|
|
|
|
|
/* FIXME: merge these two enums */
|
2019-01-15 04:51:27 +01:00
|
|
|
stub.owner = sqlite3_column_int(stmt, 1)==DIRECTION_INCOMING?REMOTE:LOCAL;
|
|
|
|
stub.cltv_expiry = sqlite3_column_int(stmt, 2);
|
|
|
|
stub.id = sqlite3_column_int(stmt, 3);
|
2017-10-10 13:04:32 +02:00
|
|
|
|
2018-10-23 11:40:22 +02:00
|
|
|
sqlite3_column_sha256(stmt, 4, &payment_hash);
|
2019-01-15 04:51:27 +01:00
|
|
|
ripemd160(&stub.ripemd, payment_hash.u.u8, sizeof(payment_hash.u));
|
|
|
|
tal_arr_expand(&stubs, stub);
|
2017-10-10 13:04:32 +02:00
|
|
|
}
|
|
|
|
return stubs;
|
|
|
|
}
|
2017-11-02 21:09:57 +01:00
|
|
|
|
2018-03-16 01:09:37 +01:00
|
|
|
void wallet_local_htlc_out_delete(struct wallet *wallet,
|
|
|
|
struct channel *chan,
|
|
|
|
const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
stmt = db_prepare(wallet->db,
|
|
|
|
"DELETE FROM channel_htlcs"
|
|
|
|
" WHERE direction = ?"
|
|
|
|
" AND origin_htlc = ?"
|
|
|
|
" AND payment_hash = ?");
|
|
|
|
sqlite3_bind_int(stmt, 1, DIRECTION_OUTGOING);
|
|
|
|
sqlite3_bind_int(stmt, 2, 0);
|
|
|
|
sqlite3_bind_sha256(stmt, 3, payment_hash);
|
|
|
|
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
static struct wallet_payment *
|
|
|
|
find_unstored_payment(struct wallet *wallet, const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
struct wallet_payment *i;
|
|
|
|
|
|
|
|
list_for_each(&wallet->unstored_payments, i, list) {
|
2018-07-04 07:30:02 +02:00
|
|
|
if (sha256_eq(payment_hash, &i->payment_hash))
|
2018-01-17 21:29:49 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:53:13 +01:00
|
|
|
static void destroy_unstored_payment(struct wallet_payment *payment)
|
2018-01-17 21:29:49 +01:00
|
|
|
{
|
|
|
|
list_del(&payment->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_payment_setup(struct wallet *wallet, struct wallet_payment *payment)
|
|
|
|
{
|
|
|
|
assert(!find_unstored_payment(wallet, &payment->payment_hash));
|
|
|
|
|
|
|
|
list_add_tail(&wallet->unstored_payments, &payment->list);
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_add_destructor(payment, destroy_unstored_payment);
|
2018-01-17 21:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_payment_store(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash)
|
2017-11-02 21:09:57 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct wallet_payment *payment;
|
|
|
|
|
|
|
|
payment = find_unstored_payment(wallet, payment_hash);
|
2018-03-09 03:17:51 +01:00
|
|
|
if (!payment) {
|
|
|
|
/* Already stored on-disk */
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Double-check that it is indeed stored to disk
|
|
|
|
* (catch bug, where we call this on a payment_hash
|
|
|
|
* we never paid to) */
|
2019-03-15 03:50:18 +01:00
|
|
|
bool res;
|
|
|
|
stmt = db_select_prepare(wallet->db,
|
|
|
|
"status FROM payments"
|
|
|
|
" WHERE payment_hash=?;");
|
2018-03-09 03:17:51 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
2019-03-15 03:50:18 +01:00
|
|
|
res = db_select_step(wallet->db, stmt);
|
|
|
|
assert(res);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-03-09 03:17:51 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2017-11-02 21:09:57 +01:00
|
|
|
|
|
|
|
/* Don't attempt to add the same payment twice */
|
|
|
|
assert(!payment->id);
|
|
|
|
|
|
|
|
stmt = db_prepare(
|
|
|
|
wallet->db,
|
|
|
|
"INSERT INTO payments ("
|
|
|
|
" status,"
|
|
|
|
" payment_hash,"
|
|
|
|
" destination,"
|
|
|
|
" msatoshi,"
|
2018-01-19 13:31:08 +01:00
|
|
|
" timestamp,"
|
2018-01-20 15:00:35 +01:00
|
|
|
" path_secrets,"
|
|
|
|
" route_nodes,"
|
2018-03-22 14:21:18 +01:00
|
|
|
" route_channels,"
|
2018-07-20 15:57:00 +02:00
|
|
|
" msatoshi_sent,"
|
2019-02-23 04:11:12 +01:00
|
|
|
" description,"
|
|
|
|
" bolt11"
|
|
|
|
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2017-11-02 21:09:57 +01:00
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 1, payment->status);
|
|
|
|
sqlite3_bind_sha256(stmt, 2, &payment->payment_hash);
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_bind_node_id(stmt, 3, &payment->destination);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 4, payment->msatoshi);
|
2018-01-17 21:22:25 +01:00
|
|
|
sqlite3_bind_int(stmt, 5, payment->timestamp);
|
2018-01-19 13:31:08 +01:00
|
|
|
sqlite3_bind_blob(stmt, 6, payment->path_secrets,
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_bytelen(payment->path_secrets),
|
2018-01-19 13:31:08 +01:00
|
|
|
SQLITE_TRANSIENT);
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_bind_node_id_array(stmt, 7, payment->route_nodes);
|
2018-01-20 15:00:35 +01:00
|
|
|
sqlite3_bind_short_channel_id_array(stmt, 8,
|
|
|
|
payment->route_channels);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 9, payment->msatoshi_sent);
|
2017-11-02 21:09:57 +01:00
|
|
|
|
2019-02-23 04:11:14 +01:00
|
|
|
if (payment->label != NULL)
|
|
|
|
sqlite3_bind_text(stmt, 10, payment->label,
|
|
|
|
strlen(payment->label),
|
2018-07-20 15:57:00 +02:00
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 10);
|
|
|
|
|
2019-02-23 04:11:12 +01:00
|
|
|
if (payment->bolt11 != NULL)
|
|
|
|
sqlite3_bind_text(stmt, 11, payment->bolt11,
|
|
|
|
strlen(payment->bolt11),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 11);
|
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2018-01-17 21:29:49 +01:00
|
|
|
|
|
|
|
tal_free(payment);
|
2017-11-02 21:09:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
void wallet_payment_delete(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct wallet_payment *payment;
|
|
|
|
|
|
|
|
payment = find_unstored_payment(wallet, payment_hash);
|
|
|
|
if (payment) {
|
|
|
|
tal_free(payment);
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
|
|
|
|
stmt = db_prepare(
|
|
|
|
wallet->db,
|
|
|
|
"DELETE FROM payments WHERE payment_hash = ?");
|
|
|
|
|
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
|
|
|
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
}
|
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
static struct wallet_payment *wallet_stmt2payment(const tal_t *ctx,
|
|
|
|
sqlite3_stmt *stmt)
|
|
|
|
{
|
|
|
|
struct wallet_payment *payment = tal(ctx, struct wallet_payment);
|
|
|
|
payment->id = sqlite3_column_int64(stmt, 0);
|
|
|
|
payment->status = sqlite3_column_int(stmt, 1);
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_column_node_id(stmt, 2, &payment->destination);
|
2019-02-21 04:45:55 +01:00
|
|
|
payment->msatoshi = sqlite3_column_amount_msat(stmt, 3);
|
2018-01-17 21:22:25 +01:00
|
|
|
sqlite3_column_sha256(stmt, 4, &payment->payment_hash);
|
2017-11-02 21:09:57 +01:00
|
|
|
|
2018-01-17 21:22:25 +01:00
|
|
|
payment->timestamp = sqlite3_column_int(stmt, 5);
|
2018-01-17 21:29:49 +01:00
|
|
|
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
|
|
|
|
payment->payment_preimage = tal(payment, struct preimage);
|
|
|
|
sqlite3_column_preimage(stmt, 6, payment->payment_preimage);
|
|
|
|
} else
|
|
|
|
payment->payment_preimage = NULL;
|
2018-01-17 21:29:49 +01:00
|
|
|
|
|
|
|
/* Can be NULL for old db! */
|
|
|
|
payment->path_secrets = sqlite3_column_secrets(payment, stmt, 7);
|
2018-01-20 15:00:35 +01:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
payment->route_nodes = sqlite3_column_node_id_array(payment, stmt, 8);
|
2018-01-20 15:00:35 +01:00
|
|
|
payment->route_channels
|
|
|
|
= sqlite3_column_short_channel_id_array(payment, stmt, 9);
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
payment->msatoshi_sent = sqlite3_column_amount_msat(stmt, 10);
|
2018-03-22 14:21:18 +01:00
|
|
|
|
2018-07-20 15:57:00 +02:00
|
|
|
if (sqlite3_column_type(stmt, 11) != SQLITE_NULL)
|
2019-02-23 04:11:14 +01:00
|
|
|
payment->label = tal_strdup(
|
2018-07-20 15:57:00 +02:00
|
|
|
payment, (const char *)sqlite3_column_text(stmt, 11));
|
|
|
|
else
|
2019-02-23 04:11:14 +01:00
|
|
|
payment->label = NULL;
|
2018-07-20 15:57:00 +02:00
|
|
|
|
2019-02-23 04:11:12 +01:00
|
|
|
if (sqlite3_column_type(stmt, 12) != SQLITE_NULL)
|
|
|
|
payment->bolt11 = tal_strdup(payment,
|
|
|
|
(const char *)sqlite3_column_text(stmt, 12));
|
|
|
|
else
|
|
|
|
payment->bolt11 = NULL;
|
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
return payment;
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:27:58 +02:00
|
|
|
/* List of the fields that stmt2payment expects to correctly convert */
|
|
|
|
#define PAYMENT_FIELDS \
|
|
|
|
"id, status, destination, msatoshi, payment_hash, timestamp, " \
|
|
|
|
"payment_preimage, path_secrets, route_nodes, route_channels, " \
|
2019-02-23 04:11:12 +01:00
|
|
|
"msatoshi_sent, description, bolt11 "
|
2018-07-23 15:27:58 +02:00
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
struct wallet_payment *
|
|
|
|
wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct wallet_payment *payment;
|
|
|
|
|
|
|
|
/* Present the illusion that it's in the db... */
|
|
|
|
payment = find_unstored_payment(wallet, payment_hash);
|
|
|
|
if (payment)
|
|
|
|
return payment;
|
2017-11-02 21:09:57 +01:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db, PAYMENT_FIELDS " FROM payments "
|
2018-07-23 15:27:58 +02:00
|
|
|
"WHERE payment_hash = ?");
|
2017-11-02 21:09:57 +01:00
|
|
|
|
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
2019-03-15 03:50:18 +01:00
|
|
|
if (db_select_step(wallet->db, stmt)) {
|
2017-11-02 21:09:57 +01:00
|
|
|
payment = wallet_stmt2payment(ctx, stmt);
|
2019-03-15 03:50:18 +01:00
|
|
|
db_stmt_done(stmt);
|
2017-11-02 21:09:57 +01:00
|
|
|
}
|
|
|
|
return payment;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_payment_set_status(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash,
|
2018-01-17 21:29:49 +01:00
|
|
|
const enum wallet_payment_status newstatus,
|
|
|
|
const struct preimage *preimage)
|
2017-11-02 21:09:57 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-01-17 21:29:50 +01:00
|
|
|
struct wallet_payment *payment;
|
2017-11-02 21:09:57 +01:00
|
|
|
|
2018-01-17 21:29:50 +01:00
|
|
|
/* We can only fail an unstored payment! */
|
|
|
|
payment = find_unstored_payment(wallet, payment_hash);
|
|
|
|
if (payment) {
|
|
|
|
assert(newstatus == PAYMENT_FAILED);
|
|
|
|
tal_free(payment);
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
stmt = db_prepare(wallet->db,
|
|
|
|
"UPDATE payments SET status=? "
|
|
|
|
"WHERE payment_hash=?");
|
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, wallet_payment_status_in_db(newstatus));
|
2017-11-02 21:09:57 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 2, payment_hash);
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
2018-01-17 21:29:49 +01:00
|
|
|
|
|
|
|
if (preimage) {
|
|
|
|
stmt = db_prepare(wallet->db,
|
|
|
|
"UPDATE payments SET payment_preimage=? "
|
|
|
|
"WHERE payment_hash=?");
|
|
|
|
|
|
|
|
sqlite3_bind_preimage(stmt, 1, preimage);
|
|
|
|
sqlite3_bind_sha256(stmt, 2, payment_hash);
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
}
|
2018-03-21 13:14:34 +01:00
|
|
|
if (newstatus != PAYMENT_PENDING) {
|
|
|
|
stmt = db_prepare(wallet->db,
|
|
|
|
"UPDATE payments"
|
|
|
|
" SET path_secrets = NULL"
|
|
|
|
" , route_nodes = NULL"
|
|
|
|
" , route_channels = NULL"
|
|
|
|
" WHERE payment_hash = ?;");
|
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
}
|
2017-11-02 21:09:57 +01:00
|
|
|
}
|
2017-11-16 19:11:59 +01:00
|
|
|
|
2018-03-07 16:41:23 +01:00
|
|
|
void wallet_payment_get_failinfo(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
/* outputs */
|
|
|
|
u8 **failonionreply,
|
|
|
|
bool *faildestperm,
|
|
|
|
int *failindex,
|
|
|
|
enum onion_type *failcode,
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id **failnode,
|
2018-03-07 16:41:23 +01:00
|
|
|
struct short_channel_id **failchannel,
|
2018-04-16 15:29:40 +02:00
|
|
|
u8 **failupdate,
|
2019-01-15 05:02:27 +01:00
|
|
|
char **faildetail,
|
|
|
|
int *faildirection)
|
2018-03-07 16:41:23 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
bool resb;
|
|
|
|
size_t len;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db,
|
|
|
|
"failonionreply, faildestperm"
|
|
|
|
", failindex, failcode"
|
|
|
|
", failnode, failchannel"
|
|
|
|
", failupdate, faildetail, faildirection"
|
|
|
|
" FROM payments"
|
|
|
|
" WHERE payment_hash=?;");
|
2018-03-07 16:41:23 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
2019-03-15 03:50:18 +01:00
|
|
|
resb = db_select_step(wallet->db, stmt);
|
|
|
|
assert(resb);
|
2018-03-07 16:41:23 +01:00
|
|
|
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
|
|
|
|
*failonionreply = NULL;
|
|
|
|
else {
|
|
|
|
len = sqlite3_column_bytes(stmt, 0);
|
|
|
|
*failonionreply = tal_arr(ctx, u8, len);
|
|
|
|
memcpy(*failonionreply, sqlite3_column_blob(stmt, 0), len);
|
|
|
|
}
|
|
|
|
*faildestperm = sqlite3_column_int(stmt, 1) != 0;
|
|
|
|
*failindex = sqlite3_column_int(stmt, 2);
|
|
|
|
*failcode = (enum onion_type) sqlite3_column_int(stmt, 3);
|
|
|
|
if (sqlite3_column_type(stmt, 4) == SQLITE_NULL)
|
|
|
|
*failnode = NULL;
|
|
|
|
else {
|
2019-04-08 11:58:32 +02:00
|
|
|
*failnode = tal(ctx, struct node_id);
|
|
|
|
resb = sqlite3_column_node_id(stmt, 4, *failnode);
|
2018-03-07 16:41:23 +01:00
|
|
|
assert(resb);
|
|
|
|
}
|
|
|
|
if (sqlite3_column_type(stmt, 5) == SQLITE_NULL)
|
|
|
|
*failchannel = NULL;
|
|
|
|
else {
|
|
|
|
*failchannel = tal(ctx, struct short_channel_id);
|
|
|
|
resb = sqlite3_column_short_channel_id(stmt, 5, *failchannel);
|
|
|
|
assert(resb);
|
2019-01-15 05:02:27 +01:00
|
|
|
|
|
|
|
/* For pre-0.6.2 dbs, direction will be 0 */
|
|
|
|
*faildirection = sqlite3_column_int(stmt, 8);
|
2018-03-07 16:41:23 +01:00
|
|
|
}
|
|
|
|
if (sqlite3_column_type(stmt, 6) == SQLITE_NULL)
|
|
|
|
*failupdate = NULL;
|
|
|
|
else {
|
|
|
|
len = sqlite3_column_bytes(stmt, 6);
|
|
|
|
*failupdate = tal_arr(ctx, u8, len);
|
|
|
|
memcpy(*failupdate, sqlite3_column_blob(stmt, 6), len);
|
|
|
|
}
|
2018-04-16 15:29:40 +02:00
|
|
|
*faildetail = tal_strndup(ctx, sqlite3_column_blob(stmt, 7),
|
|
|
|
sqlite3_column_bytes(stmt, 7));
|
2018-03-07 16:41:23 +01:00
|
|
|
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-03-07 16:41:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_payment_set_failinfo(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const u8 *failonionreply /*tal_arr*/,
|
|
|
|
bool faildestperm,
|
|
|
|
int failindex,
|
|
|
|
enum onion_type failcode,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *failnode,
|
2018-03-07 16:41:23 +01:00
|
|
|
const struct short_channel_id *failchannel,
|
2018-04-16 15:29:40 +02:00
|
|
|
const u8 *failupdate /*tal_arr*/,
|
2019-01-15 05:02:27 +01:00
|
|
|
const char *faildetail,
|
|
|
|
int faildirection)
|
2018-03-07 16:41:23 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
stmt = db_prepare(wallet->db,
|
|
|
|
"UPDATE payments"
|
|
|
|
" SET failonionreply=?"
|
|
|
|
" , faildestperm=?"
|
|
|
|
" , failindex=?"
|
|
|
|
" , failcode=?"
|
|
|
|
" , failnode=?"
|
|
|
|
" , failchannel=?"
|
|
|
|
" , failupdate=?"
|
2018-04-16 15:29:40 +02:00
|
|
|
" , faildetail=?"
|
2019-01-15 05:02:27 +01:00
|
|
|
" , faildirection=?"
|
2018-03-07 16:41:23 +01:00
|
|
|
" WHERE payment_hash=?;");
|
|
|
|
if (failonionreply)
|
|
|
|
sqlite3_bind_blob(stmt, 1,
|
|
|
|
failonionreply, tal_count(failonionreply),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 1);
|
|
|
|
sqlite3_bind_int(stmt, 2, faildestperm ? 1 : 0);
|
|
|
|
sqlite3_bind_int(stmt, 3, failindex);
|
|
|
|
sqlite3_bind_int(stmt, 4, (int) failcode);
|
|
|
|
if (failnode)
|
2019-04-08 11:58:32 +02:00
|
|
|
sqlite3_bind_node_id(stmt, 5, failnode);
|
2018-03-07 16:41:23 +01:00
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 5);
|
|
|
|
if (failchannel) {
|
|
|
|
/* sqlite3_bind_short_channel_id requires the input
|
|
|
|
* channel to be tal-allocated... */
|
2018-03-25 21:51:11 +02:00
|
|
|
struct short_channel_id *scid = tal(tmpctx, struct short_channel_id);
|
2018-03-07 16:41:23 +01:00
|
|
|
*scid = *failchannel;
|
|
|
|
sqlite3_bind_short_channel_id(stmt, 6, scid);
|
2019-01-15 05:02:27 +01:00
|
|
|
sqlite3_bind_int(stmt, 9, faildirection);
|
|
|
|
} else {
|
2018-03-07 16:41:23 +01:00
|
|
|
sqlite3_bind_null(stmt, 6);
|
2019-01-15 05:02:27 +01:00
|
|
|
sqlite3_bind_null(stmt, 9);
|
|
|
|
}
|
2018-03-07 16:41:23 +01:00
|
|
|
if (failupdate)
|
|
|
|
sqlite3_bind_blob(stmt, 7,
|
|
|
|
failupdate, tal_count(failupdate),
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 7);
|
2018-04-16 15:29:40 +02:00
|
|
|
sqlite3_bind_blob(stmt, 8,
|
|
|
|
faildetail, strlen(faildetail),
|
|
|
|
SQLITE_TRANSIENT);
|
2018-03-07 16:41:23 +01:00
|
|
|
|
2019-01-15 05:02:27 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 10, payment_hash);
|
2018-03-07 16:41:23 +01:00
|
|
|
|
|
|
|
db_exec_prepared(wallet->db, stmt);
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:44:32 +01:00
|
|
|
const struct wallet_payment **
|
|
|
|
wallet_payment_list(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash)
|
2017-11-16 19:11:59 +01:00
|
|
|
{
|
|
|
|
const struct wallet_payment **payments;
|
|
|
|
sqlite3_stmt *stmt;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct wallet_payment *p;
|
|
|
|
size_t i;
|
2017-11-16 19:11:59 +01:00
|
|
|
|
|
|
|
payments = tal_arr(ctx, const struct wallet_payment *, 0);
|
2018-01-16 20:44:32 +01:00
|
|
|
if (payment_hash) {
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db,
|
|
|
|
PAYMENT_FIELDS " FROM payments "
|
|
|
|
"WHERE payment_hash = ?;");
|
2018-01-16 20:44:32 +01:00
|
|
|
sqlite3_bind_sha256(stmt, 1, payment_hash);
|
|
|
|
} else {
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(wallet->db,
|
|
|
|
PAYMENT_FIELDS " FROM payments;");
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
2017-11-16 19:11:59 +01:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
for (i = 0; db_select_step(wallet->db, stmt); i++) {
|
2017-11-16 19:11:59 +01:00
|
|
|
tal_resize(&payments, i+1);
|
|
|
|
payments[i] = wallet_stmt2payment(payments, stmt);
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* Now attach payments not yet in db. */
|
|
|
|
list_for_each(&wallet->unstored_payments, p, list) {
|
2018-07-04 07:30:02 +02:00
|
|
|
if (payment_hash && !sha256_eq(&p->payment_hash, payment_hash))
|
2018-01-16 20:44:32 +01:00
|
|
|
continue;
|
2018-01-17 21:29:49 +01:00
|
|
|
tal_resize(&payments, i+1);
|
|
|
|
payments[i++] = p;
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:11:59 +01:00
|
|
|
return payments;
|
|
|
|
}
|
2018-02-09 15:44:39 +01:00
|
|
|
|
|
|
|
void wallet_htlc_sigs_save(struct wallet *w, u64 channel_id,
|
|
|
|
secp256k1_ecdsa_signature *htlc_sigs)
|
|
|
|
{
|
|
|
|
/* Clear any existing HTLC sigs for this channel */
|
|
|
|
sqlite3_stmt *stmt =
|
|
|
|
db_prepare(w->db, "DELETE FROM htlc_sigs WHERE channelid = ?");
|
|
|
|
sqlite3_bind_int64(stmt, 1, channel_id);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
|
|
|
|
/* Now insert the new ones */
|
|
|
|
for (size_t i=0; i<tal_count(htlc_sigs); i++) {
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO htlc_sigs (channelid, signature) VALUES (?, ?)");
|
|
|
|
sqlite3_bind_int64(stmt, 1, channel_id);
|
|
|
|
sqlite3_bind_signature(stmt, 2, &htlc_sigs[i]);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 17:55:33 +01:00
|
|
|
|
|
|
|
bool wallet_network_check(struct wallet *w,
|
|
|
|
const struct chainparams *chainparams)
|
|
|
|
{
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select(w->db,
|
|
|
|
"val FROM vars WHERE name='genesis_hash'");
|
2018-02-16 17:55:33 +01:00
|
|
|
struct bitcoin_blkid chainhash;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (db_select_step(w->db, stmt)) {
|
2018-02-16 17:55:33 +01:00
|
|
|
sqlite3_column_sha256_double(stmt, 0, &chainhash.shad);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&chainhash,
|
|
|
|
&chainparams->genesis_blockhash)) {
|
2018-02-16 17:55:33 +01:00
|
|
|
log_broken(w->log, "Wallet blockchain hash does not "
|
|
|
|
"match network blockchain hash: %s "
|
2018-07-03 16:37:01 +02:00
|
|
|
"!= %s. "
|
2018-10-19 06:21:00 +02:00
|
|
|
"Are you on the right network? "
|
|
|
|
"(--network={bitcoin, testnet, regtest, "
|
|
|
|
"litecoin or litecoin-testnet})",
|
2018-02-16 17:55:33 +01:00
|
|
|
type_to_string(w, struct bitcoin_blkid,
|
|
|
|
&chainhash),
|
|
|
|
type_to_string(w, struct bitcoin_blkid,
|
|
|
|
&chainparams->genesis_blockhash));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Still a pristine wallet, claim it for the chain
|
|
|
|
* that we are running */
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO vars (name, val) VALUES ('genesis_hash', ?);");
|
|
|
|
sqlite3_bind_sha256_double(stmt, 1, &chainparams->genesis_blockhash.shad);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-17 16:45:34 +01:00
|
|
|
|
2018-03-04 03:42:19 +01:00
|
|
|
/**
|
|
|
|
* wallet_utxoset_prune -- Remove spent UTXO entries that are old
|
|
|
|
*/
|
|
|
|
static void wallet_utxoset_prune(struct wallet *w, const u32 blockheight)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2018-03-26 20:30:11 +02:00
|
|
|
struct bitcoin_txid txid;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, "txid, outnum FROM utxoset WHERE spendheight < ?");
|
2018-03-26 20:30:11 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, blockheight - UTXO_PRUNE_DEPTH);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2018-03-26 20:30:11 +02:00
|
|
|
sqlite3_column_sha256_double(stmt, 0, &txid.shad);
|
|
|
|
outpointfilter_remove(w->utxoset_outpoints, &txid, sqlite3_column_int(stmt, 1));
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:42:19 +01:00
|
|
|
stmt = db_prepare(w->db, "DELETE FROM utxoset WHERE spendheight < ?");
|
|
|
|
sqlite3_bind_int(stmt, 1, blockheight - UTXO_PRUNE_DEPTH);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:45:34 +01:00
|
|
|
void wallet_block_add(struct wallet *w, struct block *b)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt = db_prepare(w->db,
|
|
|
|
"INSERT INTO blocks "
|
|
|
|
"(height, hash, prev_hash) "
|
|
|
|
"VALUES (?, ?, ?);");
|
|
|
|
sqlite3_bind_int(stmt, 1, b->height);
|
|
|
|
sqlite3_bind_sha256_double(stmt, 2, &b->blkid.shad);
|
|
|
|
if (b->prev) {
|
|
|
|
sqlite3_bind_sha256_double(stmt, 3, &b->prev->blkid.shad);
|
|
|
|
}else {
|
|
|
|
sqlite3_bind_null(stmt, 3);
|
|
|
|
}
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2018-03-04 03:42:19 +01:00
|
|
|
|
|
|
|
/* Now cleanup UTXOs that we don't care about anymore */
|
|
|
|
wallet_utxoset_prune(w, b->height);
|
2018-02-17 16:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void wallet_block_remove(struct wallet *w, struct block *b)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt = db_prepare(w->db,
|
|
|
|
"DELETE FROM blocks WHERE hash = ?");
|
|
|
|
sqlite3_bind_sha256_double(stmt, 1, &b->blkid.shad);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, "* FROM blocks WHERE height >= ?;");
|
2018-02-17 16:45:34 +01:00
|
|
|
sqlite3_bind_int(stmt, 1, b->height);
|
2019-03-15 03:50:18 +01:00
|
|
|
assert(!db_select_step(w->db, stmt));
|
2018-02-17 16:45:34 +01:00
|
|
|
}
|
2018-02-19 14:01:42 +01:00
|
|
|
|
|
|
|
void wallet_blocks_rollback(struct wallet *w, u32 height)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt = db_prepare(w->db, "DELETE FROM blocks "
|
2018-04-05 18:09:46 +02:00
|
|
|
"WHERE height > ?");
|
2018-02-19 14:01:42 +01:00
|
|
|
sqlite3_bind_int(stmt, 1, height);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
2018-03-02 15:07:13 +01:00
|
|
|
|
2018-03-28 11:13:43 +02:00
|
|
|
const struct short_channel_id *
|
|
|
|
wallet_outpoint_spend(struct wallet *w, const tal_t *ctx, const u32 blockheight,
|
|
|
|
const struct bitcoin_txid *txid, const u32 outnum)
|
2018-03-02 15:07:13 +01:00
|
|
|
{
|
2018-03-28 11:13:43 +02:00
|
|
|
struct short_channel_id *scid;
|
2018-03-02 15:07:13 +01:00
|
|
|
sqlite3_stmt *stmt;
|
2019-03-15 03:50:18 +01:00
|
|
|
bool res;
|
2018-03-02 15:07:13 +01:00
|
|
|
if (outpointfilter_matches(w->owned_outpoints, txid, outnum)) {
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"UPDATE outputs "
|
|
|
|
"SET spend_height = ? "
|
|
|
|
"WHERE prev_out_tx = ?"
|
|
|
|
" AND prev_out_index = ?");
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 1, blockheight);
|
2018-03-04 02:27:17 +01:00
|
|
|
sqlite3_bind_sha256_double(stmt, 2, &txid->shad);
|
|
|
|
sqlite3_bind_int(stmt, 3, outnum);
|
|
|
|
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outpointfilter_matches(w->utxoset_outpoints, txid, outnum)) {
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"UPDATE utxoset "
|
|
|
|
"SET spendheight = ? "
|
|
|
|
"WHERE txid = ?"
|
|
|
|
" AND outnum = ?");
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 1, blockheight);
|
2018-03-02 15:07:13 +01:00
|
|
|
sqlite3_bind_sha256_double(stmt, 2, &txid->shad);
|
|
|
|
sqlite3_bind_int(stmt, 3, outnum);
|
|
|
|
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2018-03-28 11:13:43 +02:00
|
|
|
|
|
|
|
if (sqlite3_changes(w->db->sql) == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now look for the outpoint's short_channel_id */
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
|
|
|
"blockheight, txindex "
|
|
|
|
"FROM utxoset "
|
|
|
|
"WHERE txid = ? AND outnum = ?");
|
2018-03-28 11:13:43 +02:00
|
|
|
sqlite3_bind_sha256_double(stmt, 1, &txid->shad);
|
|
|
|
sqlite3_bind_int(stmt, 2, outnum);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
res = db_select_step(w->db, stmt);
|
|
|
|
assert(res);
|
2018-03-28 11:13:43 +02:00
|
|
|
|
|
|
|
scid = tal(ctx, struct short_channel_id);
|
2019-01-21 01:57:43 +01:00
|
|
|
if (!mk_short_channel_id(scid, sqlite3_column_int(stmt, 0),
|
|
|
|
sqlite3_column_int(stmt, 1), outnum))
|
|
|
|
fatal("wallet_outpoint_spend: invalid scid %u:%u:%u",
|
|
|
|
sqlite3_column_int(stmt, 0),
|
|
|
|
sqlite3_column_int(stmt, 1), outnum);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-03-28 11:13:43 +02:00
|
|
|
return scid;
|
2018-03-02 15:07:13 +01:00
|
|
|
}
|
2018-03-28 11:13:43 +02:00
|
|
|
return NULL;
|
2018-03-02 15:07:13 +01:00
|
|
|
}
|
2018-03-04 01:37:56 +01:00
|
|
|
|
|
|
|
void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx,
|
|
|
|
const u32 outnum, const u32 blockheight,
|
|
|
|
const u32 txindex, const u8 *scriptpubkey,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat sat)
|
2018-03-04 01:37:56 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
struct bitcoin_txid txid;
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO utxoset ("
|
|
|
|
" txid,"
|
|
|
|
" outnum,"
|
|
|
|
" blockheight,"
|
|
|
|
" spendheight,"
|
|
|
|
" txindex,"
|
|
|
|
" scriptpubkey,"
|
|
|
|
" satoshis"
|
|
|
|
") VALUES(?, ?, ?, ?, ?, ?, ?);");
|
|
|
|
sqlite3_bind_sha256_double(stmt, 1, &txid.shad);
|
|
|
|
sqlite3_bind_int(stmt, 2, outnum);
|
|
|
|
sqlite3_bind_int(stmt, 3, blockheight);
|
|
|
|
sqlite3_bind_null(stmt, 4);
|
|
|
|
sqlite3_bind_int(stmt, 5, txindex);
|
2018-07-28 08:00:16 +02:00
|
|
|
sqlite3_bind_blob(stmt, 6, scriptpubkey, tal_count(scriptpubkey), SQLITE_TRANSIENT);
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_sat(stmt, 7, sat);
|
2018-03-04 01:37:56 +01:00
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
|
|
|
|
outpointfilter_add(w->utxoset_outpoints, &txid, outnum);
|
|
|
|
}
|
2018-03-05 23:15:54 +01:00
|
|
|
|
|
|
|
struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
struct outpoint *op;
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
2018-03-05 23:15:54 +01:00
|
|
|
" txid,"
|
|
|
|
" spendheight,"
|
|
|
|
" scriptpubkey,"
|
|
|
|
" satoshis "
|
|
|
|
"FROM utxoset "
|
|
|
|
"WHERE blockheight = ?"
|
|
|
|
" AND txindex = ?"
|
2018-09-21 02:31:30 +02:00
|
|
|
" AND outnum = ?"
|
|
|
|
" AND spendheight IS NULL");
|
2018-03-05 23:15:54 +01:00
|
|
|
sqlite3_bind_int(stmt, 1, short_channel_id_blocknum(scid));
|
|
|
|
sqlite3_bind_int(stmt, 2, short_channel_id_txnum(scid));
|
|
|
|
sqlite3_bind_int(stmt, 3, short_channel_id_outnum(scid));
|
|
|
|
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(w->db, stmt))
|
2018-03-05 23:15:54 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
op = tal(ctx, struct outpoint);
|
|
|
|
op->blockheight = short_channel_id_blocknum(scid);
|
|
|
|
op->txindex = short_channel_id_txnum(scid);
|
|
|
|
op->outnum = short_channel_id_outnum(scid);
|
|
|
|
sqlite3_column_sha256_double(stmt, 0, &op->txid.shad);
|
|
|
|
op->spendheight = sqlite3_column_int(stmt, 1);
|
|
|
|
op->scriptpubkey = tal_arr(op, u8, sqlite3_column_bytes(stmt, 2));
|
|
|
|
memcpy(op->scriptpubkey, sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2));
|
2019-02-21 04:45:55 +01:00
|
|
|
op->sat = sqlite3_column_amount_sat(stmt, 3);
|
2018-04-27 03:40:38 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-03-05 23:15:54 +01:00
|
|
|
|
|
|
|
return op;
|
|
|
|
}
|
2018-04-07 14:39:11 +02:00
|
|
|
|
|
|
|
void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx,
|
|
|
|
const u32 blockheight, const u32 txindex)
|
|
|
|
{
|
|
|
|
struct bitcoin_txid txid;
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(w->db, "blockheight FROM transactions WHERE id=?");
|
2018-04-07 14:39:11 +02:00
|
|
|
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
sqlite3_bind_sha256(stmt, 1, &txid.shad.sha);
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(w->db, stmt)) {
|
2018-04-07 14:39:11 +02:00
|
|
|
/* This transaction is still unknown, insert */
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"INSERT INTO transactions ("
|
|
|
|
" id"
|
|
|
|
", blockheight"
|
|
|
|
", txindex"
|
|
|
|
", rawtx) VALUES (?, ?, ?, ?);");
|
|
|
|
sqlite3_bind_sha256(stmt, 1, &txid.shad.sha);
|
|
|
|
if (blockheight) {
|
|
|
|
sqlite3_bind_int(stmt, 2, blockheight);
|
|
|
|
sqlite3_bind_int(stmt, 3, txindex);
|
|
|
|
} else {
|
|
|
|
sqlite3_bind_null(stmt, 2);
|
|
|
|
sqlite3_bind_null(stmt, 3);
|
|
|
|
}
|
|
|
|
sqlite3_bind_tx(stmt, 4, tx);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
2019-03-15 03:50:18 +01:00
|
|
|
} else {
|
|
|
|
db_stmt_done(stmt);
|
|
|
|
|
|
|
|
if (blockheight) {
|
|
|
|
/* We know about the transaction, update */
|
|
|
|
stmt = db_prepare(w->db,
|
|
|
|
"UPDATE transactions "
|
|
|
|
"SET blockheight = ?, txindex = ? "
|
|
|
|
"WHERE id = ?");
|
|
|
|
sqlite3_bind_int(stmt, 1, blockheight);
|
|
|
|
sqlite3_bind_int(stmt, 2, txindex);
|
|
|
|
sqlite3_bind_sha256(stmt, 3, &txid.shad.sha);
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
2018-04-07 14:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-09 15:18:09 +02:00
|
|
|
|
|
|
|
u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
u32 blockheight;
|
2019-03-15 03:50:18 +01:00
|
|
|
sqlite3_stmt *stmt = db_select_prepare(
|
|
|
|
w->db, "blockheight FROM transactions WHERE id=?");
|
2018-04-09 15:18:09 +02:00
|
|
|
sqlite3_bind_sha256(stmt, 1, &txid->shad.sha);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(w->db, stmt))
|
2018-04-09 15:18:09 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
blockheight = sqlite3_column_int(stmt, 0);
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-04-09 15:18:09 +02:00
|
|
|
return blockheight;
|
|
|
|
}
|
2018-04-09 18:48:39 +02:00
|
|
|
|
|
|
|
struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w,
|
|
|
|
const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
struct txlocator *loc;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(
|
|
|
|
w->db, "blockheight, txindex FROM transactions WHERE id=?");
|
2018-04-09 18:48:39 +02:00
|
|
|
sqlite3_bind_sha256(stmt, 1, &txid->shad.sha);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
if (!db_select_step(w->db, stmt))
|
|
|
|
return NULL;
|
2018-04-09 18:48:39 +02:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
|
2019-03-15 03:50:18 +01:00
|
|
|
loc = NULL;
|
|
|
|
else {
|
|
|
|
loc = tal(ctx, struct txlocator);
|
|
|
|
loc->blkheight = sqlite3_column_int(stmt, 0);
|
|
|
|
loc->index = sqlite3_column_int(stmt, 1);
|
|
|
|
}
|
2018-04-27 03:39:33 +02:00
|
|
|
db_stmt_done(stmt);
|
2018-04-09 18:48:39 +02:00
|
|
|
return loc;
|
|
|
|
}
|
2018-04-10 11:14:50 +02:00
|
|
|
|
|
|
|
struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
|
|
|
|
struct wallet *w,
|
|
|
|
const u32 blockheight)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
struct bitcoin_txid *txids = tal_arr(ctx, struct bitcoin_txid, 0);
|
|
|
|
int count = 0;
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(
|
|
|
|
w->db, "id FROM transactions WHERE blockheight=?");
|
2018-04-10 11:14:50 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, blockheight);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2018-04-10 11:14:50 +02:00
|
|
|
count++;
|
|
|
|
tal_resize(&txids, count);
|
|
|
|
sqlite3_column_sha256(stmt, 0, &txids[count-1].shad.sha);
|
|
|
|
}
|
|
|
|
|
|
|
|
return txids;
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:57:28 +02:00
|
|
|
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
|
|
|
|
const int type, const struct bitcoin_txid *txid,
|
|
|
|
const u32 input_num, const u32 blockheight)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
stmt = db_prepare(w->db, "INSERT INTO channeltxs ("
|
|
|
|
" channel_id"
|
|
|
|
", type"
|
|
|
|
", transaction_id"
|
|
|
|
", input_num"
|
|
|
|
", blockheight"
|
|
|
|
") VALUES (?, ?, ?, ?, ?);");
|
|
|
|
sqlite3_bind_int(stmt, 1, chan->dbid);
|
|
|
|
sqlite3_bind_int(stmt, 2, type);
|
|
|
|
sqlite3_bind_sha256(stmt, 3, &txid->shad.sha);
|
|
|
|
sqlite3_bind_int(stmt, 4, input_num);
|
|
|
|
sqlite3_bind_int(stmt, 5, blockheight);
|
|
|
|
|
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 *wallet_onchaind_channels(struct wallet *w,
|
|
|
|
const tal_t *ctx)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
size_t count = 0;
|
|
|
|
u32 *channel_ids = tal_arr(ctx, u32, 0);
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db, "DISTINCT(channel_id) FROM channeltxs WHERE type = ?;");
|
2018-04-03 16:57:28 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, WIRE_ONCHAIN_INIT);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2018-04-03 16:57:28 +02:00
|
|
|
count++;
|
|
|
|
tal_resize(&channel_ids, count);
|
|
|
|
channel_ids[count-1] = sqlite3_column_int64(stmt, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx,
|
|
|
|
u32 channel_id)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
size_t count = 0;
|
|
|
|
struct channeltx *res = tal_arr(ctx, struct channeltx, 0);
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
2018-04-03 16:57:28 +02:00
|
|
|
" c.type"
|
|
|
|
", c.blockheight"
|
|
|
|
", t.rawtx"
|
|
|
|
", c.input_num"
|
|
|
|
", c.blockheight - t.blockheight + 1 AS depth"
|
|
|
|
", t.id as txid "
|
|
|
|
"FROM channeltxs c "
|
|
|
|
"JOIN transactions t ON t.id == c.transaction_id "
|
|
|
|
"WHERE channel_id = ? "
|
|
|
|
"ORDER BY c.id ASC;");
|
|
|
|
sqlite3_bind_int(stmt, 1, channel_id);
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
while (db_select_step(w->db, stmt)) {
|
2018-04-03 16:57:28 +02:00
|
|
|
count++;
|
|
|
|
tal_resize(&res, count);
|
|
|
|
|
|
|
|
res[count-1].channel_id = channel_id;
|
|
|
|
res[count-1].type = sqlite3_column_int(stmt, 0);
|
|
|
|
res[count-1].blockheight = sqlite3_column_int(stmt, 1);
|
|
|
|
res[count-1].tx = sqlite3_column_tx(ctx, stmt, 2);
|
|
|
|
res[count-1].input_num = sqlite3_column_int(stmt, 3);
|
|
|
|
res[count-1].depth = sqlite3_column_int(stmt, 4);
|
|
|
|
sqlite3_column_sha256(stmt, 5, &res[count-1].txid.shad.sha);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-10-16 21:28:35 +02:00
|
|
|
|
|
|
|
void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
|
|
|
|
const struct htlc_out *out,
|
2019-04-15 15:47:37 +02:00
|
|
|
enum forward_status state,
|
|
|
|
enum onion_type failcode)
|
2018-10-16 21:28:35 +02:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
stmt = db_prepare(
|
|
|
|
w->db,
|
|
|
|
"INSERT OR REPLACE INTO forwarded_payments ("
|
|
|
|
" in_htlc_id"
|
|
|
|
", out_htlc_id"
|
|
|
|
", in_channel_scid"
|
|
|
|
", out_channel_scid"
|
|
|
|
", in_msatoshi"
|
|
|
|
", out_msatoshi"
|
2019-04-03 20:50:14 +02:00
|
|
|
", state"
|
|
|
|
", received_time"
|
|
|
|
", resolved_time"
|
2019-04-15 15:47:37 +02:00
|
|
|
", failcode"
|
|
|
|
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2018-10-16 21:28:35 +02:00
|
|
|
sqlite3_bind_int64(stmt, 1, in->dbid);
|
2019-04-15 15:47:37 +02:00
|
|
|
|
|
|
|
if(out) {
|
|
|
|
sqlite3_bind_int64(stmt, 2, out->dbid);
|
|
|
|
sqlite3_bind_int64(stmt, 4, out->key.channel->scid->u64);
|
|
|
|
sqlite3_bind_amount_msat(stmt, 6, out->msat);
|
|
|
|
} else {
|
|
|
|
/* FORWARD_LOCAL_FAILED may occur before we get htlc_out */
|
|
|
|
assert(failcode != 0);
|
|
|
|
assert(state == FORWARD_LOCAL_FAILED);
|
|
|
|
sqlite3_bind_null(stmt, 2);
|
|
|
|
sqlite3_bind_null(stmt, 4);
|
|
|
|
sqlite3_bind_null(stmt, 6);
|
|
|
|
}
|
|
|
|
|
2018-10-16 21:28:35 +02:00
|
|
|
sqlite3_bind_int64(stmt, 3, in->key.channel->scid->u64);
|
2019-04-15 15:47:37 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
sqlite3_bind_amount_msat(stmt, 5, in->msat);
|
2019-04-15 15:47:37 +02:00
|
|
|
|
2018-10-19 06:19:52 +02:00
|
|
|
sqlite3_bind_int(stmt, 7, wallet_forward_status_in_db(state));
|
2019-04-03 20:50:14 +02:00
|
|
|
sqlite3_bind_timeabs(stmt, 8, in->received_time);
|
|
|
|
|
|
|
|
if (state == FORWARD_SETTLED || state == FORWARD_FAILED)
|
|
|
|
sqlite3_bind_timeabs(stmt, 9, time_now());
|
|
|
|
else
|
|
|
|
sqlite3_bind_null(stmt, 9);
|
|
|
|
|
2019-04-15 15:47:37 +02:00
|
|
|
if(failcode != 0) {
|
|
|
|
assert(state == FORWARD_FAILED || state == FORWARD_LOCAL_FAILED);
|
|
|
|
sqlite3_bind_int(stmt, 10, (int)failcode);
|
|
|
|
} else {
|
|
|
|
sqlite3_bind_null(stmt, 10);
|
|
|
|
}
|
|
|
|
|
2018-10-16 21:28:35 +02:00
|
|
|
db_exec_prepared(w->db, stmt);
|
|
|
|
}
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat wallet_total_forward_fees(struct wallet *w)
|
2018-10-17 20:11:04 +02:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat total;
|
2019-03-15 03:50:18 +01:00
|
|
|
bool res;
|
2018-10-19 06:19:52 +02:00
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
2018-10-19 06:19:52 +02:00
|
|
|
" SUM(in_msatoshi - out_msatoshi) "
|
2018-10-17 20:11:04 +02:00
|
|
|
"FROM forwarded_payments "
|
2018-10-19 06:19:52 +02:00
|
|
|
"WHERE state = ?;");
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2018-10-19 06:19:52 +02:00
|
|
|
sqlite3_bind_int(stmt, 1, wallet_forward_status_in_db(FORWARD_SETTLED));
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
res = db_select_step(w->db, stmt);
|
|
|
|
assert(res);
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
total = sqlite3_column_amount_msat(stmt, 0);
|
2018-10-17 20:11:04 +02:00
|
|
|
db_stmt_done(stmt);
|
|
|
|
|
2018-10-19 06:19:52 +02:00
|
|
|
return total;
|
2018-10-17 20:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct forwarding *wallet_forwarded_payments_get(struct wallet *w,
|
|
|
|
const tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct forwarding *results = tal_arr(ctx, struct forwarding, 0);
|
|
|
|
size_t count = 0;
|
|
|
|
sqlite3_stmt *stmt;
|
2019-03-15 03:50:18 +01:00
|
|
|
stmt = db_select_prepare(w->db,
|
2018-10-17 20:11:04 +02:00
|
|
|
" f.state"
|
|
|
|
", in_msatoshi"
|
|
|
|
", out_msatoshi"
|
|
|
|
", hin.payment_hash as payment_hash"
|
|
|
|
", in_channel_scid"
|
2019-04-03 21:28:33 +02:00
|
|
|
", out_channel_scid"
|
|
|
|
", f.received_time"
|
2019-04-15 16:04:12 +02:00
|
|
|
", f.resolved_time"
|
|
|
|
", f.failcode "
|
2018-10-17 20:11:04 +02:00
|
|
|
"FROM forwarded_payments f "
|
|
|
|
"LEFT JOIN channel_htlcs hin ON (f.in_htlc_id == hin.id)");
|
|
|
|
|
2019-03-15 03:50:18 +01:00
|
|
|
for (count=0; db_select_step(w->db, stmt); count++) {
|
2018-10-17 20:11:04 +02:00
|
|
|
tal_resize(&results, count+1);
|
|
|
|
struct forwarding *cur = &results[count];
|
|
|
|
cur->status = sqlite3_column_int(stmt, 0);
|
2019-02-21 04:45:55 +01:00
|
|
|
cur->msat_in = sqlite3_column_amount_msat(stmt, 1);
|
2019-04-15 16:04:12 +02:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 2) != SQLITE_NULL)
|
|
|
|
cur->msat_out = sqlite3_column_amount_msat(stmt, 2);
|
|
|
|
else {
|
|
|
|
assert(cur->status == FORWARD_LOCAL_FAILED);
|
|
|
|
cur->msat_out = AMOUNT_MSAT(0);
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_sub(&cur->fee, cur->msat_in, cur->msat_out)) {
|
|
|
|
log_broken(w->log, "Forwarded in %s less than out %s!",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&cur->msat_in),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&cur->msat_out));
|
|
|
|
cur->fee = AMOUNT_MSAT(0);
|
|
|
|
}
|
2018-10-17 23:28:43 +02:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL) {
|
|
|
|
cur->payment_hash = tal(ctx, struct sha256_double);
|
|
|
|
sqlite3_column_sha256_double(stmt, 3, cur->payment_hash);
|
|
|
|
} else {
|
|
|
|
cur->payment_hash = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->channel_in.u64 = sqlite3_column_int64(stmt, 4);
|
2019-04-15 16:04:12 +02:00
|
|
|
|
|
|
|
if (sqlite3_column_type(stmt, 5) != SQLITE_NULL) {
|
|
|
|
cur->channel_out.u64 = sqlite3_column_int64(stmt, 5);
|
|
|
|
} else {
|
|
|
|
assert(cur->status == FORWARD_LOCAL_FAILED);
|
|
|
|
cur->channel_out.u64 = 0;
|
|
|
|
}
|
2019-04-03 21:28:33 +02:00
|
|
|
|
|
|
|
cur->received_time = sqlite3_column_timeabs(stmt, 6);
|
2019-04-15 16:04:12 +02:00
|
|
|
|
2019-04-03 21:28:33 +02:00
|
|
|
if (sqlite3_column_type(stmt, 7) != SQLITE_NULL) {
|
|
|
|
cur->resolved_time = tal(ctx, struct timeabs);
|
|
|
|
*cur->resolved_time = sqlite3_column_timeabs(stmt, 7);
|
|
|
|
} else {
|
|
|
|
cur->resolved_time = NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:04:12 +02:00
|
|
|
if (sqlite3_column_type(stmt, 8) != SQLITE_NULL) {
|
|
|
|
assert(cur->status == FORWARD_FAILED || cur->status == FORWARD_LOCAL_FAILED);
|
|
|
|
cur->failcode = sqlite3_column_int(stmt, 8);
|
|
|
|
} else {
|
|
|
|
cur->failcode = 0;
|
|
|
|
}
|
2018-10-17 20:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2019-06-05 09:00:02 +02:00
|
|
|
|
|
|
|
struct unreleased_tx *find_unreleased_tx(struct wallet *w,
|
|
|
|
const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
struct unreleased_tx *utx;
|
|
|
|
|
|
|
|
list_for_each(&w->unreleased_txs, utx, list) {
|
|
|
|
if (bitcoin_txid_eq(txid, &utx->txid))
|
|
|
|
return utx;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_unreleased_tx(struct unreleased_tx *utx)
|
|
|
|
{
|
|
|
|
list_del(&utx->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_unreleased_tx(struct unreleased_tx *utx)
|
|
|
|
{
|
|
|
|
tal_del_destructor(utx, destroy_unreleased_tx);
|
|
|
|
list_del(&utx->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_unreleased_tx(struct wallet *w, struct unreleased_tx *utx)
|
|
|
|
{
|
|
|
|
list_add_tail(&w->unreleased_txs, &utx->list);
|
|
|
|
tal_add_destructor(utx, destroy_unreleased_tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These will touch the db, so need to be explicitly freed. */
|
|
|
|
void free_unreleased_txs(struct wallet *w)
|
|
|
|
{
|
|
|
|
struct unreleased_tx *utx;
|
|
|
|
|
|
|
|
while ((utx = list_top(&w->unreleased_txs, struct unreleased_tx, list)))
|
|
|
|
tal_free(utx);
|
|
|
|
}
|
|
|
|
|