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 <bitcoin/shadouble.h>
|
|
|
|
#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;
|
|
|
|
struct sha256_double;
|
2016-04-24 12:07:13 +02:00
|
|
|
struct txwatch;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Off topology->outgoing_txs */
|
|
|
|
struct outgoing_tx {
|
|
|
|
struct list_node list;
|
|
|
|
struct peer *peer;
|
|
|
|
const char *hextx;
|
|
|
|
struct sha256_double txid;
|
|
|
|
void (*failed)(struct peer *peer, int exitstatus, const char *err);
|
2017-03-02 13:21:49 +01:00
|
|
|
/* FIXME: Remove this. */
|
2017-03-02 13:21:49 +01:00
|
|
|
struct chain_topology *topo;
|
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 */
|
|
|
|
struct sha256_double blkid;
|
|
|
|
|
|
|
|
/* 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;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* FIXME: Remove this. */
|
2017-03-02 13:21:49 +01:00
|
|
|
struct chain_topology *topo;
|
2017-03-02 13:21:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Hash blocks by sha */
|
|
|
|
static inline const struct sha256_double *keyof_block_map(const struct block *b)
|
|
|
|
{
|
|
|
|
return &b->blkid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t hash_sha(const struct sha256_double *key)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
memcpy(&ret, key, sizeof(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool block_eq(const struct block *b, const struct sha256_double *key)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
struct block *tip;
|
|
|
|
struct block_map block_map;
|
|
|
|
u64 feerate;
|
|
|
|
bool startup;
|
|
|
|
|
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-03-02 13:21:49 +01:00
|
|
|
/* Bitcoin transctions we're broadcasting */
|
|
|
|
struct list_head outgoing_txs;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Force a partiular fee rate regardless of estimatefee (satoshis/kb) */
|
|
|
|
u64 override_fee_rate;
|
|
|
|
|
|
|
|
/* What fee we use if estimatefee fails (satoshis/kb) */
|
|
|
|
u64 default_fee_rate;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Transactions/txos we are watching. */
|
|
|
|
struct txwatch_hash txwatches;
|
|
|
|
struct txowatch_hash txowatches;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-03-02 13:21:49 +01:00
|
|
|
/* Suppress broadcast (for testing) */
|
|
|
|
bool dev_no_broadcast;
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
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
|
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-08-18 06:43:53 +02:00
|
|
|
const struct sha256_double *txid,
|
|
|
|
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
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Get fee rate. */
|
2017-03-02 13:21:49 +01:00
|
|
|
u64 get_feerate(const struct chain_topology *topo);
|
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,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct peer *peer, const struct bitcoin_tx *tx,
|
2016-11-07 13:31:02 +01:00
|
|
|
void (*failed)(struct peer *peer,
|
|
|
|
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,
|
|
|
|
struct timerel poll_time, u32 first_peer_block);
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, const struct sha256_double *txid);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void notify_new_block(struct chain_topology *topo, unsigned int height);
|
2016-09-07 23:34:36 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-03-02 13:21:49 +01:00
|
|
|
void json_dev_broadcast(struct command *cmd,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct chain_topology *topo,
|
2017-03-02 13:21:49 +01:00
|
|
|
const char *buffer, const jsmntok_t *params);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H */
|