2018-03-22 11:36:25 +01:00
|
|
|
#ifndef LIGHTNING_WALLET_WALLET_H
|
|
|
|
#define LIGHTNING_WALLET_WALLET_H
|
2017-05-23 22:07:20 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "db.h"
|
2018-02-16 17:55:33 +01:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-09-01 14:33:21 +02:00
|
|
|
#include <bitcoin/tx.h>
|
2018-08-09 04:27:25 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2017-07-18 19:01:26 +02:00
|
|
|
#include <ccan/crypto/shachain/shachain.h>
|
2017-08-17 15:30:24 +02:00
|
|
|
#include <ccan/list/list.h>
|
2017-05-23 22:07:20 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/channel_config.h>
|
|
|
|
#include <common/utxo.h>
|
2018-02-17 16:45:34 +01:00
|
|
|
#include <lightningd/chaintopology.h>
|
2017-09-14 21:27:41 +02:00
|
|
|
#include <lightningd/htlc_end.h>
|
2017-10-04 14:43:55 +02:00
|
|
|
#include <lightningd/invoice.h>
|
2018-08-09 04:27:25 +02:00
|
|
|
#include <lightningd/log.h>
|
2017-10-10 13:04:32 +02:00
|
|
|
#include <onchaind/onchain_wire.h>
|
2017-06-21 17:26:38 +02:00
|
|
|
#include <wally_bip32.h>
|
2017-05-23 22:07:20 +02:00
|
|
|
|
2018-03-07 16:41:23 +01:00
|
|
|
enum onion_type;
|
2018-01-14 15:15:30 +01:00
|
|
|
struct invoices;
|
2018-02-12 11:12:55 +01:00
|
|
|
struct channel;
|
2017-06-25 04:33:13 +02:00
|
|
|
struct lightningd;
|
2018-01-23 02:02:10 +01:00
|
|
|
struct oneshot;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct peer;
|
2017-08-28 18:04:01 +02:00
|
|
|
struct pubkey;
|
2018-01-23 00:53:21 +01:00
|
|
|
struct timers;
|
2017-06-25 04:33:13 +02:00
|
|
|
|
2017-05-23 22:07:20 +02:00
|
|
|
struct wallet {
|
2018-02-12 11:10:17 +01:00
|
|
|
struct lightningd *ld;
|
2017-05-23 22:07:20 +02:00
|
|
|
struct db *db;
|
|
|
|
struct log *log;
|
2017-06-21 17:26:38 +02:00
|
|
|
struct ext_key *bip32_base;
|
2018-01-14 15:15:30 +01:00
|
|
|
struct invoices *invoices;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct list_head unstored_payments;
|
2018-02-18 13:53:46 +01:00
|
|
|
u64 max_channel_dbid;
|
2018-02-26 14:57:13 +01:00
|
|
|
|
|
|
|
/* Filter matching all outpoints corresponding to our owned outputs,
|
|
|
|
* including all spent ones */
|
|
|
|
struct outpointfilter *owned_outpoints;
|
2018-03-04 01:37:09 +01:00
|
|
|
|
|
|
|
/* Filter matching all outpoints that might be a funding transaction on
|
|
|
|
* the blockchain. This is currently all P2WSH outputs */
|
|
|
|
struct outpointfilter *utxoset_outpoints;
|
2017-05-23 22:07:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Possible states for tracked outputs in the database. Not sure yet
|
|
|
|
* whether we really want to have reservations reflected in the
|
|
|
|
* database, it would simplify queries at the cost of some IO ops */
|
2018-08-09 04:27:25 +02:00
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
|
|
|
* already defined elements (adding is ok) /!\ */
|
2017-05-23 22:07:20 +02:00
|
|
|
enum output_status {
|
|
|
|
output_state_available= 0,
|
|
|
|
output_state_reserved = 1,
|
|
|
|
output_state_spent = 2,
|
|
|
|
/* Special status used to express that we don't care in
|
|
|
|
* queries */
|
|
|
|
output_state_any = 255
|
|
|
|
};
|
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
static inline enum output_status output_status_in_db(enum output_status s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case output_state_available:
|
|
|
|
BUILD_ASSERT(output_state_available == 0);
|
|
|
|
return s;
|
|
|
|
case output_state_reserved:
|
|
|
|
BUILD_ASSERT(output_state_reserved == 1);
|
|
|
|
return s;
|
|
|
|
case output_state_spent:
|
|
|
|
BUILD_ASSERT(output_state_spent == 2);
|
|
|
|
return s;
|
|
|
|
/* This one doesn't go into db */
|
|
|
|
case output_state_any:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fatal("%s: %u is invalid", __func__, s);
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:07:20 +02:00
|
|
|
/* Enumeration of all known output types. These include all types that
|
|
|
|
* could ever end up on-chain and we may need to react upon. Notice
|
|
|
|
* that `to_local`, `htlc_offer`, and `htlc_recv` may need immediate
|
|
|
|
* action since they are encumbered with a CSV. */
|
2018-08-09 04:27:25 +02:00
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
|
|
|
* already defined elements (adding is ok) /!\ */
|
2017-05-23 22:07:20 +02:00
|
|
|
enum wallet_output_type {
|
|
|
|
p2sh_wpkh = 0,
|
|
|
|
to_local = 1,
|
|
|
|
htlc_offer = 3,
|
2017-09-12 13:50:47 +02:00
|
|
|
htlc_recv = 4,
|
2017-12-20 23:05:32 +01:00
|
|
|
our_change = 5,
|
|
|
|
p2wpkh = 6
|
2017-05-23 22:07:20 +02:00
|
|
|
};
|
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
static inline enum wallet_output_type wallet_output_type_in_db(enum wallet_output_type w)
|
|
|
|
{
|
|
|
|
switch (w) {
|
|
|
|
case p2sh_wpkh:
|
|
|
|
BUILD_ASSERT(p2sh_wpkh == 0);
|
|
|
|
return w;
|
|
|
|
case to_local:
|
|
|
|
BUILD_ASSERT(to_local == 1);
|
|
|
|
return w;
|
|
|
|
case htlc_offer:
|
|
|
|
BUILD_ASSERT(htlc_offer == 3);
|
|
|
|
return w;
|
|
|
|
case htlc_recv:
|
|
|
|
BUILD_ASSERT(htlc_recv == 4);
|
|
|
|
return w;
|
|
|
|
case our_change:
|
|
|
|
BUILD_ASSERT(our_change == 5);
|
|
|
|
return w;
|
|
|
|
case p2wpkh:
|
|
|
|
BUILD_ASSERT(p2wpkh == 6);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
fatal("%s: %u is invalid", __func__, w);
|
|
|
|
}
|
|
|
|
|
2018-10-16 19:48:09 +02:00
|
|
|
/**
|
|
|
|
* Possible states for forwards
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
|
|
|
* already defined elements (adding is ok) /!\ */
|
|
|
|
enum forward_status {
|
|
|
|
FORWARD_OFFERED = 0,
|
|
|
|
FORWARD_SETTLED = 1,
|
|
|
|
FORWARD_FAILED = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline enum forward_status wallet_forward_status_in_db(enum forward_status s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case FORWARD_OFFERED:
|
|
|
|
BUILD_ASSERT(FORWARD_OFFERED == 0);
|
|
|
|
return s;
|
|
|
|
case FORWARD_SETTLED:
|
|
|
|
BUILD_ASSERT(FORWARD_SETTLED == 1);
|
|
|
|
return s;
|
|
|
|
case FORWARD_FAILED:
|
|
|
|
BUILD_ASSERT(FORWARD_FAILED == 2);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
fatal("%s: %u is invalid", __func__, s);
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:04 +02:00
|
|
|
static inline const char* forward_status_name(enum forward_status status)
|
|
|
|
{
|
|
|
|
switch(status) {
|
|
|
|
case FORWARD_OFFERED:
|
|
|
|
return "offered";
|
|
|
|
case FORWARD_SETTLED:
|
|
|
|
return "settled";
|
|
|
|
case FORWARD_FAILED:
|
|
|
|
return "failed";
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct forwarding {
|
|
|
|
struct short_channel_id channel_in, channel_out;
|
|
|
|
u64 msatoshi_in, msatoshi_out, fee;
|
|
|
|
struct sha256_double payment_hash;
|
|
|
|
enum forward_status status;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct forwarding_stats {
|
|
|
|
/* One entry for each of the forward_statuses */
|
|
|
|
u64 count[3], msatoshi[3], fee[3];
|
|
|
|
};
|
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
/* A database backed shachain struct. The datastructure is
|
|
|
|
* writethrough, reads are performed from an in-memory version, all
|
|
|
|
* writes are passed through to the DB. */
|
|
|
|
struct wallet_shachain {
|
|
|
|
u64 id;
|
|
|
|
struct shachain chain;
|
|
|
|
};
|
|
|
|
|
2017-11-01 13:27:50 +01:00
|
|
|
/* Possible states for a wallet_payment. Payments start in
|
|
|
|
* `PENDING`. Outgoing payments are set to `PAYMENT_COMPLETE` once we
|
|
|
|
* get the preimage matching the rhash, or to
|
2018-01-17 21:22:22 +01:00
|
|
|
* `PAYMENT_FAILED`. */
|
2017-11-01 13:27:50 +01:00
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
2018-07-27 12:55:37 +02:00
|
|
|
* already defined elements (adding is ok but you should append the
|
|
|
|
* test case test_wallet_payment_status_enum() ) /!\ */
|
2017-11-01 13:27:50 +01:00
|
|
|
enum wallet_payment_status {
|
|
|
|
PAYMENT_PENDING = 0,
|
|
|
|
PAYMENT_COMPLETE = 1,
|
|
|
|
PAYMENT_FAILED = 2
|
|
|
|
};
|
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
static inline enum wallet_payment_status wallet_payment_status_in_db(enum wallet_payment_status w)
|
|
|
|
{
|
|
|
|
switch (w) {
|
|
|
|
case PAYMENT_PENDING:
|
|
|
|
BUILD_ASSERT(PAYMENT_PENDING == 0);
|
|
|
|
return w;
|
|
|
|
case PAYMENT_COMPLETE:
|
|
|
|
BUILD_ASSERT(PAYMENT_COMPLETE == 1);
|
|
|
|
return w;
|
|
|
|
case PAYMENT_FAILED:
|
|
|
|
BUILD_ASSERT(PAYMENT_FAILED == 2);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
fatal("%s: %u is invalid", __func__, w);
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:22:22 +01:00
|
|
|
/* Outgoing payments. A simple persisted representation
|
|
|
|
* of a payment we initiated. This can be used by
|
|
|
|
* a UI (alongside invoices) to display the balance history.
|
2017-11-01 13:27:50 +01:00
|
|
|
*/
|
|
|
|
struct wallet_payment {
|
2018-01-17 21:29:49 +01:00
|
|
|
/* If it's in unstored_payments */
|
|
|
|
struct list_node list;
|
2017-11-01 13:27:50 +01:00
|
|
|
u64 id;
|
|
|
|
u32 timestamp;
|
|
|
|
struct sha256 payment_hash;
|
2017-11-02 21:09:57 +01:00
|
|
|
enum wallet_payment_status status;
|
2018-01-17 21:22:22 +01:00
|
|
|
struct pubkey destination;
|
|
|
|
u64 msatoshi;
|
2018-03-22 14:21:18 +01:00
|
|
|
u64 msatoshi_sent;
|
2018-01-29 05:46:54 +01:00
|
|
|
/* If and only if PAYMENT_COMPLETE */
|
2018-01-17 21:29:49 +01:00
|
|
|
struct preimage *payment_preimage;
|
2018-01-20 15:00:35 +01:00
|
|
|
/* Needed for recovering from routing failures. */
|
2018-01-17 21:29:49 +01:00
|
|
|
struct secret *path_secrets;
|
2018-01-20 15:00:35 +01:00
|
|
|
struct pubkey *route_nodes;
|
|
|
|
struct short_channel_id *route_channels;
|
2018-07-20 15:10:01 +02:00
|
|
|
|
|
|
|
/* The description of the payment. Must support `tal_len` */
|
|
|
|
const char *description;
|
2017-11-01 13:27:50 +01:00
|
|
|
};
|
|
|
|
|
2018-03-05 23:15:54 +01:00
|
|
|
struct outpoint {
|
|
|
|
struct bitcoin_txid txid;
|
|
|
|
u32 blockheight;
|
|
|
|
u32 txindex;
|
|
|
|
u32 outnum;
|
|
|
|
u64 satoshis;
|
|
|
|
u8 *scriptpubkey;
|
|
|
|
u32 spendheight;
|
|
|
|
};
|
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
/* Statistics for a channel */
|
|
|
|
struct channel_stats {
|
|
|
|
u64 in_payments_offered, in_payments_fulfilled;
|
|
|
|
u64 in_msatoshi_offered, in_msatoshi_fulfilled;
|
|
|
|
u64 out_payments_offered, out_payments_fulfilled;
|
|
|
|
u64 out_msatoshi_offered, out_msatoshi_fulfilled;
|
|
|
|
};
|
|
|
|
|
2018-04-03 16:57:28 +02:00
|
|
|
struct channeltx {
|
2018-04-03 16:45:06 +02:00
|
|
|
u32 channel_id;
|
|
|
|
int type;
|
|
|
|
u32 blockheight;
|
|
|
|
struct bitcoin_txid txid;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
u32 input_num;
|
|
|
|
u32 depth;
|
|
|
|
};
|
|
|
|
|
2017-05-23 22:07:20 +02:00
|
|
|
/**
|
|
|
|
* wallet_new - Constructor for a new sqlite3 based wallet
|
|
|
|
*
|
|
|
|
* This is guaranteed to either return a valid wallet, or abort with
|
|
|
|
* `fatal` if it cannot be initialized.
|
|
|
|
*/
|
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-08 22:43:01 +01:00
|
|
|
* wallet_add_utxo - Register an UTXO which we (partially) own
|
2017-05-23 22:07:20 +02:00
|
|
|
*
|
2018-02-08 22:43:01 +01:00
|
|
|
* Add an UTXO to the set of outputs we care about.
|
2017-05-23 22:07:20 +02:00
|
|
|
*/
|
|
|
|
bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
|
|
|
|
enum wallet_output_type type);
|
|
|
|
|
2018-08-13 05:03:08 +02:00
|
|
|
/**
|
|
|
|
* wallet_confirm_tx - Confirm a tx which contains a UTXO.
|
|
|
|
*/
|
|
|
|
void wallet_confirm_tx(struct wallet *w,
|
|
|
|
const struct bitcoin_txid *txid,
|
|
|
|
const u32 confirmation_height);
|
|
|
|
|
2017-05-31 16:16:59 +02:00
|
|
|
/**
|
|
|
|
* wallet_update_output_status - Perform an output state transition
|
|
|
|
*
|
|
|
|
* Change the current status of an output we are tracking in the
|
|
|
|
* database. Returns true if the output exists with the @oldstatus and
|
|
|
|
* was successfully updated to @newstatus. May fail if either the
|
|
|
|
* output does not exist, or it does not have the expected
|
|
|
|
* @oldstatus. In case we don't care about the previous state use
|
|
|
|
* `output_state_any` as @oldstatus.
|
|
|
|
*/
|
|
|
|
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-06-05 13:59:51 +02:00
|
|
|
/**
|
|
|
|
* wallet_get_utxos - Retrieve all utxos matching a given state
|
|
|
|
*
|
|
|
|
* Returns a `tal_arr` of `utxo` structs. Double indirection in order
|
|
|
|
* to be able to steal individual elements onto something else.
|
|
|
|
*/
|
|
|
|
struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w,
|
|
|
|
const enum output_status state);
|
|
|
|
|
2018-08-13 05:02:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_get_unconfirmed_closeinfo_utxos - Retrieve any unconfirmed utxos w/ closeinfo
|
|
|
|
*
|
|
|
|
* Returns a `tal_arr` of `utxo` structs. Double indirection in order
|
|
|
|
* to be able to steal individual elements onto something else.
|
|
|
|
*/
|
|
|
|
struct utxo **wallet_get_unconfirmed_closeinfo_utxos(const tal_t *ctx,
|
|
|
|
struct wallet *w);
|
|
|
|
|
2017-06-13 15:42:31 +02:00
|
|
|
const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w,
|
|
|
|
const u64 value,
|
|
|
|
const u32 feerate_per_kw,
|
2017-12-20 07:05:01 +01:00
|
|
|
size_t outscriptlen,
|
2017-06-20 16:54:53 +02:00
|
|
|
u64 *fee_estimate,
|
|
|
|
u64 *change_satoshi);
|
2017-06-13 15:42:31 +02:00
|
|
|
|
2017-12-19 20:52:45 +01:00
|
|
|
const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w,
|
|
|
|
const u32 feerate_per_kw,
|
2017-12-20 07:05:01 +01:00
|
|
|
size_t outscriptlen,
|
2017-12-19 20:52:45 +01:00
|
|
|
u64 *value,
|
|
|
|
u64 *fee_estimate);
|
|
|
|
|
2017-06-13 15:42:31 +02:00
|
|
|
/**
|
|
|
|
* wallet_confirm_utxos - Once we've spent a set of utxos, mark them confirmed.
|
|
|
|
*
|
|
|
|
* May be called once the transaction spending these UTXOs has been
|
|
|
|
* broadcast. If something fails use `tal_free(utxos)` instead to undo
|
|
|
|
* the reservation.
|
|
|
|
*/
|
|
|
|
void wallet_confirm_utxos(struct wallet *w, const struct utxo **utxos);
|
|
|
|
|
2017-06-21 17:40:42 +02:00
|
|
|
/**
|
|
|
|
* wallet_can_spend - Do we have the private key matching this scriptpubkey?
|
|
|
|
*
|
|
|
|
* FIXME: This is very slow with lots of inputs!
|
|
|
|
*
|
|
|
|
* @w: (in) allet holding the pubkeys to check against (privkeys are on HSM)
|
|
|
|
* @script: (in) the script to check
|
|
|
|
* @index: (out) the bip32 derivation index that matched the script
|
|
|
|
* @output_is_p2sh: (out) whether the script is a p2sh, or p2wpkh
|
|
|
|
*/
|
|
|
|
bool wallet_can_spend(struct wallet *w, const u8 *script,
|
|
|
|
u32 *index, bool *output_is_p2sh);
|
|
|
|
|
2017-06-25 04:33:13 +02:00
|
|
|
/**
|
|
|
|
* wallet_get_newindex - get a new index from the wallet.
|
|
|
|
* @ld: (in) lightning daemon
|
|
|
|
*
|
|
|
|
* Returns -1 on error (key exhaustion).
|
|
|
|
*/
|
|
|
|
s64 wallet_get_newindex(struct lightningd *ld);
|
|
|
|
|
2017-07-18 19:01:26 +02:00
|
|
|
/**
|
|
|
|
* wallet_shachain_add_hash -- wallet wrapper around shachain_add_hash
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_shachain_load -- Load an existing shachain from the wallet.
|
|
|
|
*
|
|
|
|
* @wallet: the wallet to load from
|
|
|
|
* @id: the shachain id to load
|
|
|
|
* @chain: where to load the shachain into
|
|
|
|
*/
|
|
|
|
bool wallet_shachain_load(struct wallet *wallet, u64 id,
|
|
|
|
struct wallet_shachain *chain);
|
|
|
|
|
2017-08-05 12:26:18 +02:00
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
/**
|
|
|
|
* wallet_get_uncommitted_channel_dbid -- get a unique channel dbid
|
|
|
|
*
|
|
|
|
* @wallet: the wallet
|
|
|
|
*/
|
|
|
|
u64 wallet_get_channel_dbid(struct wallet *wallet);
|
|
|
|
|
2017-08-05 12:26:18 +02:00
|
|
|
/**
|
|
|
|
* wallet_channel_save -- Upsert the channel into the database
|
|
|
|
*
|
|
|
|
* @wallet: the wallet to save into
|
|
|
|
* @chan: the instance to store (not const so we can update the unique_id upon
|
|
|
|
* insert)
|
|
|
|
*/
|
2018-02-12 11:12:55 +01:00
|
|
|
void wallet_channel_save(struct wallet *w, struct channel *chan);
|
2017-08-05 14:45:42 +02:00
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
/**
|
|
|
|
* wallet_channel_insert -- Insert the initial channel into the database
|
|
|
|
*
|
|
|
|
* @wallet: the wallet to save into
|
|
|
|
* @chan: the instance to store
|
|
|
|
*/
|
|
|
|
void wallet_channel_insert(struct wallet *w, struct channel *chan);
|
|
|
|
|
2018-01-23 14:48:09 +01:00
|
|
|
/**
|
|
|
|
* wallet_channel_delete -- After resolving a channel, forget about it
|
|
|
|
*/
|
2018-02-14 02:53:04 +01:00
|
|
|
void wallet_channel_delete(struct wallet *w, u64 wallet_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_peer_delete -- After no more channels in peer, forget about it
|
|
|
|
*/
|
|
|
|
void wallet_peer_delete(struct wallet *w, u64 peer_dbid);
|
2018-01-23 14:48:09 +01:00
|
|
|
|
2017-08-05 14:45:42 +02:00
|
|
|
/**
|
|
|
|
* wallet_channel_config_load -- Load channel_config from database into cc
|
|
|
|
*/
|
|
|
|
bool wallet_channel_config_load(struct wallet *w, const u64 id,
|
|
|
|
struct channel_config *cc);
|
2017-08-14 16:04:11 +02:00
|
|
|
|
2017-08-17 15:30:24 +02:00
|
|
|
/**
|
|
|
|
* wlalet_channels_load_active -- Load persisted active channels into the peers
|
|
|
|
*
|
2018-01-03 06:26:44 +01:00
|
|
|
* @ctx: context to allocate peers from
|
2017-08-17 15:30:24 +02:00
|
|
|
* @w: wallet to load from
|
|
|
|
*
|
|
|
|
* Be sure to call this only once on startup since it'll append peers
|
|
|
|
* loaded from the database to the list without checking.
|
|
|
|
*/
|
2018-02-12 11:12:55 +01:00
|
|
|
bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w);
|
2017-08-17 15:30:24 +02:00
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
/**
|
|
|
|
* wallet_channel_stats_incr_* - Increase channel statistics.
|
|
|
|
*
|
|
|
|
* @w: wallet containing the channel
|
|
|
|
* @cdbid: channel database id
|
|
|
|
* @msatoshi: amount in msatoshi being transferred
|
|
|
|
*/
|
|
|
|
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
|
|
|
|
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
|
|
|
|
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
|
|
|
|
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_channel_stats_load - Load channel statistics
|
|
|
|
*
|
|
|
|
* @w: wallet containing the channel
|
|
|
|
* @cdbid: channel database id
|
|
|
|
* @stats: location to load statistics to
|
|
|
|
*/
|
|
|
|
void wallet_channel_stats_load(struct wallet *w, u64 cdbid, struct channel_stats *stats);
|
|
|
|
|
2018-01-03 06:26:30 +01:00
|
|
|
/**
|
2018-04-20 14:44:34 +02:00
|
|
|
* Retrieve the blockheight of the last block processed by lightningd.
|
|
|
|
*
|
2018-06-04 14:47:32 +02:00
|
|
|
* Will set min/max either the minimal/maximal blockheight or the default value
|
|
|
|
* if the wallet was never used before.
|
2018-01-03 06:26:30 +01:00
|
|
|
*
|
|
|
|
* @w: wallet to load from.
|
2018-04-20 14:44:34 +02:00
|
|
|
* @def: the default value to return if we've never used the wallet before
|
2018-06-04 14:47:32 +02:00
|
|
|
* @min(out): height of the first block we track
|
|
|
|
* @max(out): height of the last block we added
|
2018-01-03 06:26:30 +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
|
|
|
|
2017-09-01 14:33:21 +02:00
|
|
|
/**
|
|
|
|
* wallet_extract_owned_outputs - given a tx, extract all of our outputs
|
|
|
|
*/
|
|
|
|
int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
|
2018-03-22 00:13:16 +01:00
|
|
|
const u32 *blockheight, u64 *total_satoshi);
|
2017-09-01 14:33:21 +02:00
|
|
|
|
2017-09-14 21:27:41 +02:00
|
|
|
/**
|
2018-02-08 22:43:01 +01:00
|
|
|
* wallet_htlc_save_in - store an htlc_in in the database
|
2017-09-14 21:27:41 +02:00
|
|
|
*
|
|
|
|
* @wallet: wallet to store the htlc into
|
2018-02-12 11:12:55 +01:00
|
|
|
* @chan: the channel this HTLC is associated with
|
2017-09-14 21:27:41 +02:00
|
|
|
* @in: the htlc_in to store
|
|
|
|
*
|
|
|
|
* This will store the contents of the `struct htlc_in` in the
|
|
|
|
* database. Since `struct htlc_in` commonly only change state after
|
|
|
|
* being created we do not support updating arbitrary fields and this
|
|
|
|
* function will fail when attempting to call it multiple times for
|
|
|
|
* the same `struct htlc_in`. Instead `wallet_htlc_update` may be used
|
|
|
|
* for state transitions or to set the `payment_key` for completed
|
|
|
|
* HTLCs.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2018-02-08 22:43:01 +01:00
|
|
|
* wallet_htlc_save_out - store an htlc_out in the database
|
2017-09-14 21:27:41 +02:00
|
|
|
*
|
|
|
|
* See comment for wallet_htlc_save_in.
|
|
|
|
*/
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_htlc_update - perform state transition or add payment_key
|
|
|
|
*
|
|
|
|
* @wallet: the wallet containing the HTLC to update
|
|
|
|
* @htlc_dbid: the database ID used to identify the HTLC
|
|
|
|
* @new_state: the state we should transition to
|
|
|
|
* @payment_key: the `payment_key` which hashes to the `payment_hash`,
|
|
|
|
* or NULL if unknown.
|
2018-10-09 10:56:52 +02:00
|
|
|
* @failcode: the current failure code, or 0.
|
|
|
|
* @failuremsg: the current failure message (from peer), or NULL.
|
2017-09-14 21:27:41 +02:00
|
|
|
*
|
|
|
|
* Used to update the state of an HTLC, either a `struct htlc_in` or a
|
|
|
|
* `struct htlc_out` and optionally set the `payment_key` should the
|
2018-10-09 10:56:52 +02:00
|
|
|
* HTLC have been settled, or `failcode`/`failuremsg` if failed.
|
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-09-25 20:44:23 +02:00
|
|
|
/**
|
|
|
|
* wallet_htlcs_load_for_channel - Load HTLCs associated with chan from DB.
|
|
|
|
*
|
|
|
|
* @wallet: wallet to load from
|
|
|
|
* @chan: load HTLCs associated with this channel
|
|
|
|
* @htlcs_in: htlc_in_map to store loaded htlc_in in
|
|
|
|
* @htlcs_out: htlc_out_map to store loaded htlc_out in
|
|
|
|
*
|
|
|
|
* This function looks for HTLCs that are associated with the given
|
|
|
|
* channel and loads them into the provided maps. One caveat is that
|
|
|
|
* the `struct htlc_out` instances are not wired up with the
|
|
|
|
* corresponding `struct htlc_in` in the forwarding case nor are they
|
|
|
|
* associated with a `struct pay_command` in the case we originated
|
|
|
|
* the payment. In the former case the corresponding `struct htlc_in`
|
|
|
|
* may not have been loaded yet. In the latter case the pay_command
|
|
|
|
* does not exist anymore since we restarted.
|
|
|
|
*
|
2018-09-03 03:08:53 +02:00
|
|
|
* Use `htlcs_reconnect` to wire htlc_out instances to the
|
2017-09-25 20:44:23 +02:00
|
|
|
* corresponding htlc_in after loading all channels.
|
|
|
|
*/
|
|
|
|
bool wallet_htlcs_load_for_channel(struct wallet *wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
struct channel *chan,
|
2017-09-25 20:44:23 +02:00
|
|
|
struct htlc_in_map *htlcs_in,
|
|
|
|
struct htlc_out_map *htlcs_out);
|
|
|
|
|
2017-09-26 14:40:41 +02:00
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
|
|
|
* already defined elements (adding is ok) /!\ */
|
|
|
|
enum invoice_status {
|
|
|
|
UNPAID,
|
|
|
|
PAID,
|
2018-01-23 02:02:10 +01:00
|
|
|
EXPIRED,
|
2018-01-14 15:15:30 +01:00
|
|
|
};
|
|
|
|
|
2018-08-09 04:27:25 +02:00
|
|
|
static inline enum invoice_status invoice_status_in_db(enum invoice_status s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case UNPAID:
|
|
|
|
BUILD_ASSERT(UNPAID == 0);
|
|
|
|
return s;
|
|
|
|
case PAID:
|
|
|
|
BUILD_ASSERT(PAID == 1);
|
|
|
|
return s;
|
|
|
|
case EXPIRED:
|
|
|
|
BUILD_ASSERT(EXPIRED == 2);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
fatal("%s: %u is invalid", __func__, s);
|
|
|
|
}
|
|
|
|
|
2018-02-20 02:07:30 +01:00
|
|
|
/* The information about an invoice */
|
|
|
|
struct invoice_details {
|
|
|
|
/* Current invoice state */
|
|
|
|
enum invoice_status state;
|
|
|
|
/* Preimage for this invoice */
|
|
|
|
struct preimage r;
|
|
|
|
/* Hash of preimage r */
|
|
|
|
struct sha256 rhash;
|
|
|
|
/* Label assigned by user */
|
2018-03-26 02:08:16 +02:00
|
|
|
const struct json_escaped *label;
|
2018-02-20 02:07:30 +01:00
|
|
|
/* NULL if they specified "any" */
|
|
|
|
u64 *msatoshi;
|
|
|
|
/* Absolute UNIX epoch time this will expire */
|
|
|
|
u64 expiry_time;
|
|
|
|
/* Set if state == PAID; order to be returned by waitanyinvoice */
|
|
|
|
u64 pay_index;
|
|
|
|
/* Set if state == PAID; amount received */
|
|
|
|
u64 msatoshi_received;
|
|
|
|
/* Set if state == PAID; time paid */
|
|
|
|
u64 paid_timestamp;
|
2018-02-28 17:26:19 +01:00
|
|
|
/* BOLT11 encoding for this invoice */
|
|
|
|
const char *bolt11;
|
2018-07-20 15:10:01 +02:00
|
|
|
|
|
|
|
/* The description of the payment. */
|
|
|
|
char *description;
|
2018-02-20 02:07:30 +01:00
|
|
|
};
|
|
|
|
|
2018-02-24 13:11:14 +01:00
|
|
|
/* An object that handles iteration over the set of invoices */
|
|
|
|
struct invoice_iterator {
|
|
|
|
/* The contents of this object is subject to change
|
|
|
|
* and should not be depended upon */
|
2018-02-23 02:04:47 +01:00
|
|
|
void *p;
|
2018-02-24 13:11:14 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
struct invoice {
|
2018-02-01 06:18:11 +01:00
|
|
|
/* Internal, rest of lightningd should not use */
|
2018-01-14 15:15:30 +01:00
|
|
|
/* Database ID */
|
|
|
|
u64 id;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define INVOICE_MAX_LABEL_LEN 128
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_invoice_create - Create a new invoice.
|
|
|
|
*
|
|
|
|
* @wallet - the wallet to create the invoice in.
|
2018-02-23 02:04:47 +01:00
|
|
|
* @pinvoice - pointer to location to load new invoice in.
|
2018-01-14 15:15:30 +01:00
|
|
|
* @msatoshi - the amount the invoice should have, or
|
|
|
|
* NULL for any-amount invoices.
|
|
|
|
* @label - the unique label for this invoice. Must be
|
2018-03-26 02:08:16 +02:00
|
|
|
* non-NULL.
|
2018-01-14 15:15:30 +01:00
|
|
|
* @expiry - the number of seconds before the invoice
|
|
|
|
* expires
|
|
|
|
*
|
2018-02-23 02:04:47 +01:00
|
|
|
* Returns false if label already exists or expiry is 0.
|
|
|
|
* Returns true if created invoice.
|
2018-01-14 15:15:30 +01:00
|
|
|
* FIXME: Fallback addresses
|
|
|
|
*/
|
2018-02-23 02:04:47 +01:00
|
|
|
bool wallet_invoice_create(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
|
|
|
u64 *msatoshi 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);
|
2018-01-14 15:15:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_invoice_find_by_label - Search for an invoice by label
|
|
|
|
*
|
|
|
|
* @wallet - the wallet to search.
|
2018-02-23 02:04:47 +01:00
|
|
|
* @pinvoice - pointer to location to load found invoice in.
|
2018-03-26 02:08:16 +02:00
|
|
|
* @label - the label to search for.
|
2018-01-14 15:15:30 +01:00
|
|
|
*
|
2018-02-23 02:04:47 +01:00
|
|
|
* Returns false if no invoice with that label exists.
|
|
|
|
* Returns true if found.
|
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);
|
2018-01-14 15:15:30 +01:00
|
|
|
|
2018-04-25 01:04:33 +02:00
|
|
|
/**
|
|
|
|
* wallet_invoice_find_by_rhash - Search for an invoice by payment_hash
|
|
|
|
*
|
|
|
|
* @wallet - the wallet to search.
|
|
|
|
* @pinvoice - pointer to location to load found invoice in.
|
|
|
|
* @rhash - the payment_hash to search for.
|
|
|
|
*
|
|
|
|
* Returns false if no invoice with that rhash exists.
|
|
|
|
* Returns true if found.
|
|
|
|
*/
|
|
|
|
bool wallet_invoice_find_by_rhash(struct wallet *wallet,
|
|
|
|
struct invoice *pinvoice,
|
|
|
|
const struct sha256 *rhash);
|
|
|
|
|
2017-12-26 11:50:30 +01:00
|
|
|
/**
|
2018-01-14 15:15:30 +01:00
|
|
|
* wallet_invoice_find_unpaid - Search for an unpaid, unexpired invoice by
|
|
|
|
* payment_hash
|
2017-12-26 11:50:30 +01:00
|
|
|
*
|
2018-01-14 15:15:30 +01:00
|
|
|
* @wallet - the wallet to search.
|
2018-02-23 02:04:47 +01:00
|
|
|
* @pinvoice - pointer to location to load found invoice in.
|
2018-01-14 15:15:30 +01:00
|
|
|
* @rhash - the payment_hash to search for.
|
2017-12-26 11:50:30 +01:00
|
|
|
*
|
2018-02-23 02:04:47 +01:00
|
|
|
* Returns false if no unpaid invoice with that rhash exists.
|
|
|
|
* Returns true if found.
|
2017-12-26 11:50:30 +01:00
|
|
|
*/
|
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-26 11:50:30 +01:00
|
|
|
|
2017-10-04 14:43:55 +02:00
|
|
|
/**
|
2018-01-14 15:15:30 +01:00
|
|
|
* wallet_invoice_delete - Delete an invoice
|
2017-10-04 14:43:55 +02:00
|
|
|
*
|
2018-01-14 15:15:30 +01:00
|
|
|
* @wallet - the wallet to delete the invoice from.
|
|
|
|
* @invoice - the invoice to delete.
|
2017-10-04 14:43:55 +02:00
|
|
|
*
|
2018-01-14 15:15:30 +01:00
|
|
|
* Return false on failure.
|
2017-10-04 14:43:55 +02: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-10-04 14:43:55 +02:00
|
|
|
|
2018-02-26 13:37:53 +01:00
|
|
|
/**
|
|
|
|
* wallet_invoice_delete_expired - Delete all expired invoices
|
|
|
|
* with expiration time less than or equal to the given.
|
|
|
|
*
|
|
|
|
* @wallet - the wallet to delete invoices from.
|
|
|
|
* @max_expiry_time - the maximum expiry time to delete.
|
|
|
|
*/
|
|
|
|
void wallet_invoice_delete_expired(struct wallet *wallet,
|
|
|
|
u64 max_expiry_time);
|
|
|
|
|
2018-03-17 16:12:37 +01:00
|
|
|
/**
|
|
|
|
* wallet_invoice_autoclean - Set up a repeating autoclean of
|
|
|
|
* expired invoices.
|
|
|
|
* Cleans (deletes) expired invoices every @cycle_seconds.
|
|
|
|
* Clean only those invoices that have been expired for at
|
|
|
|
* least @expired_by seconds or more.
|
|
|
|
*/
|
|
|
|
void wallet_invoice_autoclean(struct wallet * wallet,
|
|
|
|
u64 cycle_seconds,
|
|
|
|
u64 expired_by);
|
|
|
|
|
2017-10-04 14:43:55 +02:00
|
|
|
/**
|
2018-01-14 15:15:30 +01:00
|
|
|
* wallet_invoice_iterate - Iterate over all existing invoices
|
|
|
|
*
|
2018-01-18 17:50:35 +01:00
|
|
|
* @wallet - the wallet whose invoices are to be iterated over.
|
2018-02-24 13:11:14 +01:00
|
|
|
* @iterator - the iterator object to use.
|
2017-10-04 14:43:55 +02:00
|
|
|
*
|
2018-02-24 13:11:14 +01:00
|
|
|
* Return false at end-of-sequence, true if still iterating.
|
|
|
|
* Usage:
|
2017-10-04 14:43:55 +02:00
|
|
|
*
|
2018-02-24 13:11:14 +01:00
|
|
|
* struct invoice_iterator it;
|
|
|
|
* memset(&it, 0, sizeof(it))
|
|
|
|
* while (wallet_invoice_iterate(wallet, &it)) {
|
2018-01-14 15:15:30 +01:00
|
|
|
* ...
|
|
|
|
* }
|
2017-10-04 14:43:55 +02:00
|
|
|
*/
|
2018-02-24 13:11:14 +01:00
|
|
|
bool wallet_invoice_iterate(struct wallet *wallet,
|
|
|
|
struct invoice_iterator *it);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_invoice_iterator_deref - Read the details of the
|
|
|
|
* invoice currently pointed to by the given iterator.
|
|
|
|
*
|
|
|
|
* @ctx - the owner of the label and msatoshi fields returned.
|
|
|
|
* @wallet - the wallet whose invoices are to be iterated over.
|
|
|
|
* @iterator - the iterator object to use.
|
2018-07-27 12:57:02 +02:00
|
|
|
* @return pointer to the invoice details allocated off of `ctx`.
|
2018-02-24 13:11:14 +01:00
|
|
|
*/
|
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
|
|
|
const struct invoice_details *wallet_invoice_iterator_deref(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
2018-07-27 12:57:02 +02:00
|
|
|
const struct invoice_iterator *it);
|
2017-10-04 14:43:55 +02:00
|
|
|
|
2017-10-05 23:29:56 +02:00
|
|
|
/**
|
2018-01-14 15:15:30 +01:00
|
|
|
* wallet_invoice_resolve - Mark an invoice as paid
|
2017-10-05 23:29:56 +02:00
|
|
|
*
|
2018-01-14 15:15:30 +01:00
|
|
|
* @wallet - the wallet containing the invoice.
|
|
|
|
* @invoice - the invoice to mark as paid.
|
|
|
|
* @msatoshi_received - the actual amount received.
|
2017-10-05 23:29:56 +02:00
|
|
|
*
|
2018-01-14 15:15:30 +01:00
|
|
|
* Precondition: the invoice must not yet be expired (wallet
|
2018-02-23 02:04:47 +01:00
|
|
|
* does not check!).
|
2017-10-05 23:29:56 +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,
|
2018-01-14 15:15:30 +01:00
|
|
|
u64 msatoshi_received);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_invoice_waitany - Wait for any invoice to be paid.
|
|
|
|
*
|
|
|
|
* @ctx - the owner of the callback. If the owner is freed,
|
|
|
|
* the callback is cancelled.
|
|
|
|
* @wallet - the wallet to query.
|
|
|
|
* @lastpay_index - wait for invoices after the specified
|
|
|
|
* pay_index. Use 0 to wait for the first invoice.
|
|
|
|
* @cb - the callback to invoke. If an invoice is already
|
|
|
|
* paid with pay_index greater than lastpay_index, this
|
|
|
|
* is called immediately, otherwise it is called during
|
2018-02-23 02:04:47 +01:00
|
|
|
* an invoices_resolve call. Will never be given a NULL
|
|
|
|
* pointer-to-invoice.
|
2018-01-14 15:15:30 +01:00
|
|
|
* @cbarg - the callback data.
|
|
|
|
*/
|
|
|
|
void wallet_invoice_waitany(const tal_t *ctx,
|
|
|
|
struct wallet *wallet,
|
|
|
|
u64 lastpay_index,
|
|
|
|
void (*cb)(const struct invoice *, void*),
|
|
|
|
void *cbarg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_invoice_waitone - Wait for a specific invoice to be paid,
|
|
|
|
* deleted, or expired.
|
|
|
|
*
|
|
|
|
* @ctx - the owner of the callback. If the owner is freed,
|
|
|
|
* the callback is cancelled.
|
|
|
|
* @wallet - the wallet to query.
|
|
|
|
* @invoice - the invoice to wait on.
|
|
|
|
* @cb - the callback to invoice. If invoice is already paid
|
|
|
|
* or expired, this is called immediately, otherwise it is
|
|
|
|
* called during an invoices_resolve or invoices_delete call.
|
|
|
|
* If the invoice was deleted, the callback is given a NULL
|
|
|
|
* invoice.
|
|
|
|
* @cbarg - the callback data.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2018-02-20 02:07:30 +01:00
|
|
|
/**
|
|
|
|
* wallet_invoice_details - Get the invoice_details of an invoice.
|
|
|
|
*
|
|
|
|
* @ctx - the owner of the label and msatoshi fields returned.
|
|
|
|
* @wallet - the wallet to query.
|
|
|
|
* @invoice - the invoice to get details on.
|
2018-07-27 12:57:02 +02:00
|
|
|
* @return pointer to the invoice details allocated off of `ctx`.
|
2018-02-20 02:07:30 +01:00
|
|
|
*/
|
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);
|
2017-10-05 23:29:56 +02:00
|
|
|
|
2017-10-10 13:04:32 +02:00
|
|
|
/**
|
|
|
|
* wallet_htlc_stubs - Retrieve HTLC stubs for the given channel
|
|
|
|
*
|
|
|
|
* Load minimal necessary information about HTLCs for the on-chain
|
|
|
|
* settlement. This returns a `tal_arr` allocated off of @ctx with the
|
|
|
|
* necessary size to hold all HTLCs.
|
|
|
|
*
|
|
|
|
* @ctx: Allocation context for the return value
|
|
|
|
* @wallet: Wallet to load from
|
|
|
|
* @chan: Channel to fetch stubs for
|
|
|
|
*/
|
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
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
/**
|
2018-01-17 21:29:49 +01:00
|
|
|
* wallet_payment_setup - Remember this payment for later committing.
|
|
|
|
*
|
|
|
|
* Either wallet_payment_store() gets called to put in db once hout
|
|
|
|
* is ready to go (and frees @payment), or @payment is tal_free'd.
|
|
|
|
*
|
|
|
|
* @wallet: wallet we're going to store it in.
|
|
|
|
* @payment: the payment for later committing.
|
|
|
|
*/
|
|
|
|
void wallet_payment_setup(struct wallet *wallet, struct wallet_payment *payment);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_payment_store - Record a new incoming/outgoing payment
|
2017-11-02 21:09:57 +01:00
|
|
|
*
|
|
|
|
* Stores the payment in the database.
|
|
|
|
*/
|
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
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/**
|
|
|
|
* wallet_payment_delete - Remove a payment
|
|
|
|
*
|
|
|
|
* Removes the payment from the database.
|
|
|
|
*/
|
|
|
|
void wallet_payment_delete(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash);
|
|
|
|
|
2018-03-16 01:09:37 +01:00
|
|
|
/**
|
|
|
|
* wallet_local_htlc_out_delete - Remove a local outgoing failed HTLC
|
|
|
|
*
|
|
|
|
* This is not a generic HTLC cleanup! This is specifically for the
|
|
|
|
* narrow (and simple!) case of removing the HTLC associated with a
|
|
|
|
* local outgoing payment.
|
|
|
|
*/
|
|
|
|
void wallet_local_htlc_out_delete(struct wallet *wallet,
|
|
|
|
struct channel *chan,
|
|
|
|
const struct sha256 *payment_hash);
|
|
|
|
|
2017-11-02 21:09:57 +01:00
|
|
|
/**
|
|
|
|
* wallet_payment_by_hash - Retrieve a specific payment
|
|
|
|
*
|
|
|
|
* Given the `payment_hash` retrieve the matching payment.
|
|
|
|
*/
|
|
|
|
struct wallet_payment *
|
|
|
|
wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_payment_set_status - Update the status of the payment
|
|
|
|
*
|
|
|
|
* Search for the payment with the given `payment_hash` and update
|
|
|
|
* its state.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2018-03-07 16:41:23 +01:00
|
|
|
/**
|
|
|
|
* wallet_payment_get_failinfo - Get failure information for a given
|
|
|
|
* `payment_hash`.
|
|
|
|
*
|
|
|
|
* Data is allocated as children of the given context.
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
struct pubkey **failnode,
|
|
|
|
struct short_channel_id **failchannel,
|
2018-04-16 15:29:40 +02:00
|
|
|
u8 **failupdate,
|
|
|
|
char **faildetail);
|
2018-03-07 16:41:23 +01:00
|
|
|
/**
|
|
|
|
* wallet_payment_set_failinfo - Set failure information for a given
|
|
|
|
* `payment_hash`.
|
|
|
|
*/
|
|
|
|
void wallet_payment_set_failinfo(struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const u8 *failonionreply,
|
|
|
|
bool faildestperm,
|
|
|
|
int failindex,
|
|
|
|
enum onion_type failcode,
|
|
|
|
const struct pubkey *failnode,
|
|
|
|
const struct short_channel_id *failchannel,
|
2018-04-16 15:29:40 +02:00
|
|
|
const u8 *failupdate,
|
|
|
|
const char *faildetail);
|
2018-03-07 16:41:23 +01:00
|
|
|
|
2017-11-16 19:11:59 +01:00
|
|
|
/**
|
|
|
|
* wallet_payment_list - Retrieve a list of payments
|
2018-01-16 20:44:32 +01:00
|
|
|
*
|
|
|
|
* payment_hash: optional filter for only this payment hash.
|
2017-11-16 19:11:59 +01:00
|
|
|
*/
|
|
|
|
const struct wallet_payment **wallet_payment_list(const tal_t *ctx,
|
2018-01-16 20:44:32 +01:00
|
|
|
struct wallet *wallet,
|
|
|
|
const struct sha256 *payment_hash);
|
2017-11-16 19:11:59 +01:00
|
|
|
|
2018-02-09 15:44:39 +01:00
|
|
|
/**
|
|
|
|
* wallet_htlc_sigs_save - Store the latest HTLC sigs for the channel
|
|
|
|
*/
|
|
|
|
void wallet_htlc_sigs_save(struct wallet *w, u64 channel_id,
|
|
|
|
secp256k1_ecdsa_signature *htlc_sigs);
|
2018-02-16 17:55:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_network_check - Check that the wallet is setup for this chain
|
|
|
|
*
|
|
|
|
* Ensure that the genesis_hash from the chainparams matches the
|
|
|
|
* genesis_hash with which the DB was initialized. Returns false if
|
|
|
|
* the check failed, i.e., if the genesis hashes do not match.
|
|
|
|
*/
|
|
|
|
bool wallet_network_check(struct wallet *w,
|
|
|
|
const struct chainparams *chainparams);
|
|
|
|
|
2018-02-17 16:45:34 +01:00
|
|
|
/**
|
|
|
|
* wallet_block_add - Add a block to the blockchain tracked by this wallet
|
|
|
|
*/
|
|
|
|
void wallet_block_add(struct wallet *w, struct block *b);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wallet_block_remove - Remove a block (and all its descendants) from the tracked blockchain
|
|
|
|
*/
|
|
|
|
void wallet_block_remove(struct wallet *w, struct block *b);
|
|
|
|
|
2018-02-19 14:01:42 +01:00
|
|
|
/**
|
|
|
|
* wallet_blocks_rollback - Roll the blockchain back to the given height
|
|
|
|
*/
|
|
|
|
void wallet_blocks_rollback(struct wallet *w, u32 height);
|
2018-02-17 16:45:34 +01:00
|
|
|
|
2018-03-28 11:13:43 +02:00
|
|
|
/**
|
|
|
|
* Mark an outpoint as spent, both in the owned as well as the UTXO set
|
|
|
|
*
|
|
|
|
* Given the outpoint (txid, outnum), and the blockheight, mark the
|
|
|
|
* corresponding DB entries as spent at the blockheight.
|
|
|
|
*
|
|
|
|
* @return scid The short_channel_id corresponding to the spent outpoint, if
|
|
|
|
* any.
|
|
|
|
*/
|
|
|
|
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-05 23:15:54 +01:00
|
|
|
struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx,
|
|
|
|
const struct short_channel_id *scid);
|
|
|
|
|
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,
|
|
|
|
const u64 satoshis);
|
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);
|
|
|
|
|
2018-04-09 15:18:09 +02:00
|
|
|
/**
|
|
|
|
* Get the confirmation height of a transaction we are watching by its
|
|
|
|
* txid. Returns 0 if the transaction was not part of any block.
|
|
|
|
*/
|
|
|
|
u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid);
|
|
|
|
|
2018-04-09 18:48:39 +02:00
|
|
|
/**
|
|
|
|
* Locate a transaction in the blockchain, returns NULL if the transaction is
|
|
|
|
* not tracked or is not yet confirmed.
|
|
|
|
*/
|
|
|
|
struct txlocator *wallet_transaction_locate(const tal_t *ctx, struct wallet *w,
|
|
|
|
const struct bitcoin_txid *txid);
|
|
|
|
|
2018-04-10 11:14:50 +02:00
|
|
|
/**
|
|
|
|
* Get transaction IDs for transactions that we are tracking.
|
|
|
|
*/
|
|
|
|
struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
|
|
|
|
struct wallet *w,
|
|
|
|
const u32 blockheight);
|
|
|
|
|
2018-04-03 16:57:28 +02:00
|
|
|
/**
|
|
|
|
* Store transactions of interest in the database to replay on restart
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List channels for which we had an onchaind running
|
|
|
|
*/
|
|
|
|
u32 *wallet_onchaind_channels(struct wallet *w,
|
|
|
|
const tal_t *ctx);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get transactions that we'd like to replay for a channel.
|
|
|
|
*/
|
|
|
|
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx,
|
|
|
|
u32 channel_id);
|
|
|
|
|
2018-10-17 20:11:04 +02:00
|
|
|
/**
|
|
|
|
* Add of update a forwarded_payment
|
|
|
|
*/
|
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,
|
|
|
|
enum forward_status state);
|
|
|
|
|
2018-10-17 20:11:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieve global stats about all forwarded_payments
|
|
|
|
*/
|
|
|
|
const struct forwarding_stats *wallet_forwarded_payments_stats(struct wallet *w,
|
|
|
|
const tal_t *ctx);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a list of all forwarded_payments
|
|
|
|
*/
|
|
|
|
const struct forwarding *wallet_forwarded_payments_get(struct wallet *w,
|
|
|
|
const tal_t *ctx);
|
2018-03-22 11:36:25 +01:00
|
|
|
#endif /* LIGHTNING_WALLET_WALLET_H */
|