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>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/watch.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;
|
|
|
|
|
2017-11-21 04:30:29 +01:00
|
|
|
enum feerate {
|
|
|
|
FEERATE_IMMEDIATE, /* Aka: aim for next block. */
|
|
|
|
FEERATE_NORMAL, /* Aka: next 4 blocks or so. */
|
|
|
|
FEERATE_SLOW, /* Aka: next 100 blocks or so. */
|
|
|
|
};
|
|
|
|
#define NUM_FEERATES (FEERATE_SLOW+1)
|
|
|
|
|
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;
|
2018-02-12 11:13:04 +01:00
|
|
|
void (*failed)(struct channel *channel, int exitstatus, const char *err);
|
2017-03-02 13:21:49 +01:00
|
|
|
};
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block {
|
|
|
|
int height;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* Transactions in this block we care about */
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct bitcoin_tx **txs;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* And their associated index in the block */
|
|
|
|
u32 *txnums;
|
|
|
|
|
|
|
|
/* Full copy of txs (trimmed to txs list in connect_block) */
|
|
|
|
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
|
|
|
{
|
|
|
|
return structeq(&b->blkid, key);
|
|
|
|
}
|
|
|
|
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 {
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block *root;
|
2017-12-21 12:10:26 +01:00
|
|
|
struct block *prev_tip, *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];
|
2017-03-02 13:21:49 +01:00
|
|
|
bool startup;
|
|
|
|
|
2018-02-19 15:20:51 +01:00
|
|
|
/* Where to store blockchain info. */
|
|
|
|
struct wallet *wallet;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Where to log things. */
|
|
|
|
struct log *log;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* How far back (in blocks) to go. */
|
|
|
|
unsigned int first_blocknum;
|
|
|
|
|
|
|
|
/* How often to poll. */
|
|
|
|
struct timerel poll_time;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2018-01-30 07:55:44 +01:00
|
|
|
/* Force a particular fee rate regardless of estimatefee (satoshis/kw) */
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 *override_fee_rate;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2018-01-30 07:55:44 +01:00
|
|
|
/* What fee we use if estimatefee fails (satoshis/kw) */
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 default_fee_rate;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Transactions/txos we are watching. */
|
|
|
|
struct txwatch_hash txwatches;
|
|
|
|
struct txowatch_hash txowatches;
|
|
|
|
};
|
|
|
|
|
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
|
2017-08-18 06:43:53 +02:00
|
|
|
* the tx (optional tx is filled in if return is non-zero). */
|
2017-03-02 13:21:49 +01:00
|
|
|
size_t get_tx_depth(const struct chain_topology *topo,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *txid,
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct bitcoin_tx **tx);
|
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
|
|
|
|
2017-11-21 04:27:47 +01:00
|
|
|
/* Get fee rate in satoshi per kiloweight. */
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 get_feerate(const struct chain_topology *topo, enum feerate feerate);
|
2016-08-18 06:53:46 +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,
|
2016-11-07 13:31:02 +01:00
|
|
|
int exitstatus,
|
|
|
|
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);
|
2017-08-28 18:09:01 +02:00
|
|
|
void setup_topology(struct chain_topology *topology,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct timers *timers,
|
2018-02-12 11:13:04 +01:00
|
|
|
struct timerel poll_time, u32 first_channel_block);
|
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
|
|
|
|
2017-11-02 00:53:19 +01:00
|
|
|
void notify_new_block(struct lightningd *ld, unsigned int height);
|
2017-11-21 04:34:36 +01:00
|
|
|
void notify_feerate_change(struct lightningd *ld);
|
2016-09-07 23:34:36 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-12-15 11:17:54 +01:00
|
|
|
void chaintopology_mark_pointers_used(struct htable *memtable,
|
|
|
|
const struct chain_topology *topo);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H */
|