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-03-02 13:21:49 +01:00
|
|
|
#include <ccan/list/list.h>
|
2022-07-04 05:52:34 +02:00
|
|
|
#include <lightningd/feerate.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/watch.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
|
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-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 {
|
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;
|
2022-09-12 23:19:11 +02:00
|
|
|
const char *cmd_id;
|
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;
|
2020-08-26 13:31:41 +02:00
|
|
|
struct bitcoin_txid *txids;
|
2017-03-02 13:21:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2022-11-15 14:52:04 +01:00
|
|
|
/* Hash blocks by sha */
|
|
|
|
static inline const struct bitcoin_txid *keyof_outgoing_tx_map(const struct outgoing_tx *t)
|
|
|
|
{
|
|
|
|
return &t->txid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t outgoing_tx_hash_sha(const struct bitcoin_txid *key)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
memcpy(&ret, key, sizeof(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool outgoing_tx_eq(const struct outgoing_tx *b, const struct bitcoin_txid *key)
|
|
|
|
{
|
|
|
|
return bitcoin_txid_eq(&b->txid, key);
|
|
|
|
}
|
|
|
|
HTABLE_DEFINE_TYPE(struct outgoing_tx, keyof_outgoing_tx_map,
|
|
|
|
outgoing_tx_hash_sha, outgoing_tx_eq, outgoing_tx_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;
|
|
|
|
|
2021-09-01 06:35:15 +02:00
|
|
|
/* Timers we're running. */
|
|
|
|
struct oneshot *extend_timer, *updatefee_timer;
|
|
|
|
|
2017-12-07 23:59:39 +01:00
|
|
|
/* Bitcoin transactions we're broadcasting */
|
2022-11-15 15:23:21 +01:00
|
|
|
struct outgoing_tx_map outgoing_txs;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* 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;
|
2021-09-08 03:37:01 +02:00
|
|
|
|
|
|
|
/* Are we stopped? */
|
|
|
|
bool stopping;
|
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
|
|
|
|
2022-09-12 23:19:11 +02:00
|
|
|
/**
|
|
|
|
* broadcast_tx - Broadcast a single tx, and rebroadcast as reqd (copies tx).
|
|
|
|
* @topo: topology
|
|
|
|
* @channel: the channel responsible for this (stop broadcasting if freed).
|
|
|
|
* @tx: the transaction
|
|
|
|
* @cmd_id: the JSON command id which triggered this (or NULL).
|
|
|
|
* @allowhighfees: set to true to override the high-fee checks in the backend.
|
|
|
|
* @failed: if 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,
|
2022-09-12 23:19:11 +02:00
|
|
|
const char *cmd_id, bool allowhighfees,
|
2022-07-25 09:01:09 +02:00
|
|
|
void (*failed)(struct 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);
|
2021-09-01 06:34:24 +02:00
|
|
|
void setup_topology(struct chain_topology *topology,
|
2018-06-04 15:00:05 +02:00
|
|
|
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);
|
|
|
|
|
2021-09-01 06:35:15 +02:00
|
|
|
void stop_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,
|
2022-07-25 09:01:09 +02:00
|
|
|
void (*cb)(struct chain_topology *,
|
|
|
|
void *),
|
2019-08-10 12:47:32 +02:00
|
|
|
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 */
|