2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H
|
2016-04-24 12:07:13 +02:00
|
|
|
#include "config.h"
|
2017-03-02 13:21:49 +01:00
|
|
|
#include <bitcoin/block.h>
|
2017-12-18 07:41:52 +01:00
|
|
|
#include <bitcoin/tx.h>
|
2017-03-02 13:21:49 +01:00
|
|
|
#include <ccan/list/list.h>
|
2016-06-30 01:38:11 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2017-03-02 13:21:49 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-03-02 13:21:49 +01:00
|
|
|
#include <ccan/time/time.h>
|
2017-08-28 18:11:01 +02:00
|
|
|
#include <jsmn.h>
|
2018-08-27 07:11:39 +02:00
|
|
|
#include <lightningd/json.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/watch.h>
|
2018-07-15 18:30:43 +02:00
|
|
|
#include <math.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2016-06-30 01:38:11 +02:00
|
|
|
struct bitcoin_tx;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct bitcoind;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct command;
|
2017-08-28 18:09:01 +02:00
|
|
|
struct lightningd;
|
2016-06-30 01:38:11 +02:00
|
|
|
struct peer;
|
2016-04-24 12:07:13 +02:00
|
|
|
struct txwatch;
|
|
|
|
|
2018-08-27 07:11:39 +02:00
|
|
|
/* FIXME: move all feerate stuff out to new lightningd/feerate.[ch] files */
|
2017-11-21 04:30:29 +01:00
|
|
|
enum feerate {
|
chaintopology: better feerate targets differentiation
We kept track of an URGENT, a NORMAL, and a SLOW feerate. They were used
for opening (NORMAL), mutual (NORMAL), UNILATERAL (URGENT) transactions
as well as minimum and maximum estimations, and onchain resolution.
We now keep track of more fine-grained feerates:
- `opening` used for funding and also misc transactions
- `mutual_close` used for the mutual close transaction
- `unilateral_close` used for unilateral close (commitment transactions)
- `delayed_to_us` used for resolving our output from our unilateral close
- `htlc_resolution` used for resolving onchain HTLCs
- `penalty` used for resolving revoked transactions
We don't modify our requests to our Bitcoin backend, as the next commit
will batch them !
Changelog-deprecated: The "urgent", "slow", and "normal" field of the `feerates` command are now deprecated.
Changelog-added: The fields "opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution" and "penalty" have been added to the `feerates` command.
2020-03-10 17:52:13 +01:00
|
|
|
FEERATE_OPENING,
|
|
|
|
FEERATE_MUTUAL_CLOSE,
|
|
|
|
FEERATE_UNILATERAL_CLOSE,
|
|
|
|
FEERATE_DELAYED_TO_US,
|
|
|
|
FEERATE_HTLC_RESOLUTION,
|
|
|
|
FEERATE_PENALTY,
|
|
|
|
FEERATE_MIN,
|
|
|
|
FEERATE_MAX,
|
2017-11-21 04:30:29 +01:00
|
|
|
};
|
chaintopology: better feerate targets differentiation
We kept track of an URGENT, a NORMAL, and a SLOW feerate. They were used
for opening (NORMAL), mutual (NORMAL), UNILATERAL (URGENT) transactions
as well as minimum and maximum estimations, and onchain resolution.
We now keep track of more fine-grained feerates:
- `opening` used for funding and also misc transactions
- `mutual_close` used for the mutual close transaction
- `unilateral_close` used for unilateral close (commitment transactions)
- `delayed_to_us` used for resolving our output from our unilateral close
- `htlc_resolution` used for resolving onchain HTLCs
- `penalty` used for resolving revoked transactions
We don't modify our requests to our Bitcoin backend, as the next commit
will batch them !
Changelog-deprecated: The "urgent", "slow", and "normal" field of the `feerates` command are now deprecated.
Changelog-added: The fields "opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution" and "penalty" have been added to the `feerates` command.
2020-03-10 17:52:13 +01:00
|
|
|
#define NUM_FEERATES (FEERATE_MAX+1)
|
2017-11-21 04:30:29 +01:00
|
|
|
|
2018-08-24 04:22:05 +02:00
|
|
|
/* We keep the last three in case there are outliers (for min/max) */
|
|
|
|
#define FEE_HISTORY_NUM 3
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Off topology->outgoing_txs */
|
|
|
|
struct outgoing_tx {
|
|
|
|
struct list_node list;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *channel;
|
2017-03-02 13:21:49 +01:00
|
|
|
const char *hextx;
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid txid;
|
2020-01-09 12:25:45 +01:00
|
|
|
void (*failed_or_success)(struct channel *channel, bool success, const char *err);
|
2017-03-02 13:21:49 +01:00
|
|
|
};
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block {
|
2018-03-22 00:13:16 +01:00
|
|
|
u32 height;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* Actual header. */
|
|
|
|
struct bitcoin_block_hdr hdr;
|
|
|
|
|
|
|
|
/* Previous block (if any). */
|
|
|
|
struct block *prev;
|
|
|
|
|
|
|
|
/* Next block (if any). */
|
|
|
|
struct block *next;
|
|
|
|
|
|
|
|
/* Key for hash table */
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid blkid;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2020-07-24 09:15:41 +02:00
|
|
|
/* Full copy of txs (freed in filter_block_txs) */
|
2017-03-02 13:21:49 +01:00
|
|
|
struct bitcoin_tx **full_txs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Hash blocks by sha */
|
2017-12-18 07:44:10 +01:00
|
|
|
static inline const struct bitcoin_blkid *keyof_block_map(const struct block *b)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
return &b->blkid;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
static inline size_t hash_sha(const struct bitcoin_blkid *key)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
memcpy(&ret, key, sizeof(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
static inline bool block_eq(const struct block *b, const struct bitcoin_blkid *key)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
2018-07-04 07:30:02 +02:00
|
|
|
return bitcoin_blkid_eq(&b->blkid, key);
|
2017-03-02 13:21:49 +01:00
|
|
|
}
|
|
|
|
HTABLE_DEFINE_TYPE(struct block, keyof_block_map, hash_sha, block_eq, block_map);
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct chain_topology {
|
2018-09-03 02:44:27 +02:00
|
|
|
struct lightningd *ld;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block *root;
|
2020-01-29 05:50:11 +01:00
|
|
|
struct block *tip;
|
|
|
|
struct bitcoin_blkid prev_tip;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block_map block_map;
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 feerate[NUM_FEERATES];
|
2018-08-22 04:33:32 +02:00
|
|
|
bool feerate_uninitialized;
|
2018-08-24 04:22:05 +02:00
|
|
|
u32 feehistory[NUM_FEERATES][FEE_HISTORY_NUM];
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Where to log things. */
|
|
|
|
struct log *log;
|
|
|
|
|
2018-06-04 14:46:05 +02:00
|
|
|
/* What range of blocks do we have in our database? */
|
|
|
|
u32 min_blockheight, max_blockheight;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* How often to poll. */
|
2018-05-17 06:08:24 +02:00
|
|
|
u32 poll_seconds;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2019-08-10 12:47:32 +02:00
|
|
|
/* struct sync_waiters waiting for us to catch up with bitcoind (and
|
|
|
|
* once that has caught up with the network). NULL if we're already
|
|
|
|
* caught up. */
|
|
|
|
struct list_head *sync_waiters;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* The bitcoind. */
|
|
|
|
struct bitcoind *bitcoind;
|
|
|
|
|
|
|
|
/* Our timer list. */
|
|
|
|
struct timers *timers;
|
|
|
|
|
2017-12-07 23:59:39 +01:00
|
|
|
/* Bitcoin transactions we're broadcasting */
|
2017-03-02 13:21:49 +01:00
|
|
|
struct list_head outgoing_txs;
|
|
|
|
|
|
|
|
/* Transactions/txos we are watching. */
|
|
|
|
struct txwatch_hash txwatches;
|
|
|
|
struct txowatch_hash txowatches;
|
2020-08-03 13:26:44 +02:00
|
|
|
|
|
|
|
/* The number of headers known to the bitcoin backend at startup. Not
|
|
|
|
* updated after the initial check. */
|
|
|
|
u32 headercount;
|
2017-03-02 13:21:49 +01:00
|
|
|
};
|
|
|
|
|
2016-09-07 23:34:36 +02:00
|
|
|
/* Information relevant to locating a TX in a blockchain. */
|
|
|
|
struct txlocator {
|
|
|
|
|
|
|
|
/* The height of the block that includes this transaction */
|
|
|
|
u32 blkheight;
|
|
|
|
|
|
|
|
/* Position of the transaction in the transactions list */
|
|
|
|
u32 index;
|
|
|
|
};
|
|
|
|
|
2016-04-24 12:16:32 +02:00
|
|
|
/* This is the number of blocks which would have to be mined to invalidate
|
2018-04-09 17:49:03 +02:00
|
|
|
* the tx */
|
2017-03-02 13:21:49 +01:00
|
|
|
size_t get_tx_depth(const struct chain_topology *topo,
|
2018-04-09 17:49:03 +02:00
|
|
|
const struct bitcoin_txid *txid);
|
2016-04-24 12:16:32 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
/* Get highest block number. */
|
2017-03-02 13:21:49 +01:00
|
|
|
u32 get_block_height(const struct chain_topology *topo);
|
2016-06-28 23:19:21 +02:00
|
|
|
|
2020-08-03 13:26:44 +02:00
|
|
|
/* Get the highest block number in the network that we are aware of. Unlike
|
|
|
|
* `get_block_height` this takes into consideration the block header counter
|
|
|
|
* in the bitcoin backend as well. If an absolute time is required, rather
|
|
|
|
* than our current scan position this is preferable since it is far less
|
|
|
|
* likely to lag behind the rest of the network.*/
|
|
|
|
u32 get_network_blockheight(const struct chain_topology *topo);
|
|
|
|
|
2018-08-23 01:27:25 +02:00
|
|
|
/* Get fee rate in satoshi per kiloweight, or 0 if unavailable! */
|
|
|
|
u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
2018-08-24 04:22:04 +02:00
|
|
|
/* Get range of feerates to insist other side abide by for normal channels.
|
|
|
|
* If we have to guess, sets *unknown to true, otherwise false. */
|
|
|
|
u32 feerate_min(struct lightningd *ld, bool *unknown);
|
|
|
|
u32 feerate_max(struct lightningd *ld, bool *unknown);
|
|
|
|
|
2018-08-24 04:22:48 +02:00
|
|
|
u32 opening_feerate(struct chain_topology *topo);
|
2020-03-10 17:17:10 +01:00
|
|
|
u32 mutual_close_feerate(struct chain_topology *topo);
|
2018-08-24 04:22:48 +02:00
|
|
|
u32 unilateral_feerate(struct chain_topology *topo);
|
2020-03-10 17:17:10 +01:00
|
|
|
/* For onchain resolution. */
|
|
|
|
u32 delayed_to_us_feerate(struct chain_topology *topo);
|
|
|
|
u32 htlc_resolution_feerate(struct chain_topology *topo);
|
|
|
|
u32 penalty_feerate(struct chain_topology *topo);
|
2018-08-24 04:22:48 +02:00
|
|
|
|
2018-08-28 22:46:34 +02:00
|
|
|
const char *feerate_name(enum feerate feerate);
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
/* Set feerate_per_kw to this estimate & return NULL, or fail cmd */
|
|
|
|
struct command_result *param_feerate_estimate(struct command *cmd,
|
|
|
|
u32 **feerate_per_kw,
|
|
|
|
enum feerate feerate);
|
2018-08-28 22:46:34 +02:00
|
|
|
|
2016-11-07 13:31:02 +01:00
|
|
|
/* Broadcast a single tx, and rebroadcast as reqd (copies tx).
|
|
|
|
* If failed is non-NULL, call that and don't rebroadcast. */
|
2017-03-02 13:21:49 +01:00
|
|
|
void broadcast_tx(struct chain_topology *topo,
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *channel, const struct bitcoin_tx *tx,
|
|
|
|
void (*failed)(struct channel *channel,
|
2020-01-09 12:25:45 +01:00
|
|
|
bool success,
|
2016-11-07 13:31:02 +01:00
|
|
|
const char *err));
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2017-11-01 11:20:40 +01:00
|
|
|
struct chain_topology *new_topology(struct lightningd *ld, struct log *log);
|
2018-06-04 15:00:05 +02:00
|
|
|
void setup_topology(struct chain_topology *topology, struct timers *timers,
|
|
|
|
u32 min_blockheight, u32 max_blockheight);
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2018-01-03 06:26:44 +01:00
|
|
|
void begin_topology(struct chain_topology *topo);
|
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, const struct bitcoin_txid *txid);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2019-08-10 12:47:32 +02:00
|
|
|
static inline bool topology_synced(const struct chain_topology *topo)
|
|
|
|
{
|
|
|
|
return topo->sync_waiters == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* topology_add_sync_waiter: wait for lightningd to sync with bitcoin network
|
|
|
|
* @ctx: context to allocate the waiter from.
|
|
|
|
* @topo: chain topology
|
|
|
|
* @cb: callback to call when we're synced.
|
|
|
|
* @arg: arg for @cb
|
|
|
|
*
|
|
|
|
* topology_synced() must be false when this is called. It will be true
|
|
|
|
* when @cb is called. @cb will not be called if @ctx is freed first.
|
|
|
|
*/
|
|
|
|
void topology_add_sync_waiter_(const tal_t *ctx,
|
|
|
|
struct chain_topology *topo,
|
|
|
|
void (*cb)(struct chain_topology *topo,
|
|
|
|
void *arg),
|
|
|
|
void *arg);
|
|
|
|
#define topology_add_sync_waiter(ctx, topo, cb, arg) \
|
|
|
|
topology_add_sync_waiter_((ctx), (topo), \
|
|
|
|
typesafe_cb_preargs(void, void *, \
|
|
|
|
(cb), (arg), \
|
|
|
|
struct chain_topology *), \
|
|
|
|
(arg))
|
|
|
|
|
|
|
|
|
2018-08-23 01:27:22 +02:00
|
|
|
/* In channel_control.c */
|
2017-11-21 04:34:36 +01:00
|
|
|
void notify_feerate_change(struct lightningd *ld);
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H */
|