1 #ifndef LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H
2 #define LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H
3 #include "config.h"
4 #include <bitcoin/block.h>
5 #include <ccan/list/list.h>
6 #include <lightningd/watch.h>
7
8 struct bitcoin_tx;
9 struct bitcoind;
10 struct command;
11 struct lightningd;
12 struct peer;
13 struct txwatch;
14
15 /* FIXME: move all feerate stuff out to new lightningd/feerate.[ch] files */
16 enum feerate {
17 /* DO NOT REORDER: force-feerates uses this order! */
18 FEERATE_OPENING,
19 FEERATE_MUTUAL_CLOSE,
20 FEERATE_UNILATERAL_CLOSE,
21 FEERATE_DELAYED_TO_US,
22 FEERATE_HTLC_RESOLUTION,
23 FEERATE_PENALTY,
24 FEERATE_MIN,
25 FEERATE_MAX,
26 };
27 #define NUM_FEERATES (FEERATE_MAX+1)
28
29 /* We keep the last three in case there are outliers (for min/max) */
30 #define FEE_HISTORY_NUM 3
31
32 /* Off topology->outgoing_txs */
33 struct outgoing_tx {
34 struct list_node list;
35 struct channel *channel;
36 const char *hextx;
37 struct bitcoin_txid txid;
38 void (*failed_or_success)(struct channel *channel, bool success, const char *err);
39 };
40
41 struct block {
42 u32 height;
43
44 /* Actual header. */
45 struct bitcoin_block_hdr hdr;
46
47 /* Previous block (if any). */
48 struct block *prev;
49
50 /* Next block (if any). */
51 struct block *next;
52
53 /* Key for hash table */
54 struct bitcoin_blkid blkid;
55
56 /* Full copy of txs (freed in filter_block_txs) */
57 struct bitcoin_tx **full_txs;
58 struct bitcoin_txid *txids;
59 };
60
61 /* Hash blocks by sha */
keyof_block_map(const struct block * b)62 static inline const struct bitcoin_blkid *keyof_block_map(const struct block *b)
63 {
64 return &b->blkid;
65 }
66
hash_sha(const struct bitcoin_blkid * key)67 static inline size_t hash_sha(const struct bitcoin_blkid *key)
68 {
69 size_t ret;
70
71 memcpy(&ret, key, sizeof(ret));
72 return ret;
73 }
74
block_eq(const struct block * b,const struct bitcoin_blkid * key)75 static inline bool block_eq(const struct block *b, const struct bitcoin_blkid *key)
76 {
77 return bitcoin_blkid_eq(&b->blkid, key);
78 }
79 HTABLE_DEFINE_TYPE(struct block, keyof_block_map, hash_sha, block_eq, block_map);
80
81 struct chain_topology {
82 struct lightningd *ld;
83 struct block *root;
84 struct block *tip;
85 struct bitcoin_blkid prev_tip;
86 struct block_map block_map;
87 u32 feerate[NUM_FEERATES];
88 bool feerate_uninitialized;
89 u32 feehistory[NUM_FEERATES][FEE_HISTORY_NUM];
90
91 /* Where to log things. */
92 struct log *log;
93
94 /* What range of blocks do we have in our database? */
95 u32 min_blockheight, max_blockheight;
96
97 /* How often to poll. */
98 u32 poll_seconds;
99
100 /* struct sync_waiters waiting for us to catch up with bitcoind (and
101 * once that has caught up with the network). NULL if we're already
102 * caught up. */
103 struct list_head *sync_waiters;
104
105 /* The bitcoind. */
106 struct bitcoind *bitcoind;
107
108 /* Timers we're running. */
109 struct oneshot *extend_timer, *updatefee_timer;
110
111 /* Bitcoin transactions we're broadcasting */
112 struct list_head outgoing_txs;
113
114 /* Transactions/txos we are watching. */
115 struct txwatch_hash txwatches;
116 struct txowatch_hash txowatches;
117
118 /* The number of headers known to the bitcoin backend at startup. Not
119 * updated after the initial check. */
120 u32 headercount;
121
122 /* Are we stopped? */
123 bool stopping;
124 };
125
126 /* Information relevant to locating a TX in a blockchain. */
127 struct txlocator {
128
129 /* The height of the block that includes this transaction */
130 u32 blkheight;
131
132 /* Position of the transaction in the transactions list */
133 u32 index;
134 };
135
136 /* This is the number of blocks which would have to be mined to invalidate
137 * the tx */
138 size_t get_tx_depth(const struct chain_topology *topo,
139 const struct bitcoin_txid *txid);
140
141 /* Get highest block number. */
142 u32 get_block_height(const struct chain_topology *topo);
143
144 /* Get the highest block number in the network that we are aware of. Unlike
145 * `get_block_height` this takes into consideration the block header counter
146 * in the bitcoin backend as well. If an absolute time is required, rather
147 * than our current scan position this is preferable since it is far less
148 * likely to lag behind the rest of the network.*/
149 u32 get_network_blockheight(const struct chain_topology *topo);
150
151 /* Get fee rate in satoshi per kiloweight, or 0 if unavailable! */
152 u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate);
153
154 /* Get range of feerates to insist other side abide by for normal channels.
155 * If we have to guess, sets *unknown to true, otherwise false. */
156 u32 feerate_min(struct lightningd *ld, bool *unknown);
157 u32 feerate_max(struct lightningd *ld, bool *unknown);
158
159 u32 opening_feerate(struct chain_topology *topo);
160 u32 mutual_close_feerate(struct chain_topology *topo);
161 u32 unilateral_feerate(struct chain_topology *topo);
162 /* For onchain resolution. */
163 u32 delayed_to_us_feerate(struct chain_topology *topo);
164 u32 htlc_resolution_feerate(struct chain_topology *topo);
165 u32 penalty_feerate(struct chain_topology *topo);
166
167 const char *feerate_name(enum feerate feerate);
168
169 /* Set feerate_per_kw to this estimate & return NULL, or fail cmd */
170 struct command_result *param_feerate_estimate(struct command *cmd,
171 u32 **feerate_per_kw,
172 enum feerate feerate);
173
174 /* Broadcast a single tx, and rebroadcast as reqd (copies tx).
175 * If failed is non-NULL, call that and don't rebroadcast. */
176 void broadcast_tx(struct chain_topology *topo,
177 struct channel *channel, const struct bitcoin_tx *tx,
178 void (*failed)(struct channel *channel,
179 bool success,
180 const char *err));
181 /* Like the above, but with an additional `allowhighfees` parameter.
182 * If true, suppress any high-fee checks in the backend. */
183 void broadcast_tx_ahf(struct chain_topology *topo,
184 struct channel *channel, const struct bitcoin_tx *tx,
185 bool allowhighfees,
186 void (*failed)(struct channel *channel,
187 bool success,
188 const char *err));
189
190 struct chain_topology *new_topology(struct lightningd *ld, struct log *log);
191 void setup_topology(struct chain_topology *topology,
192 u32 min_blockheight, u32 max_blockheight);
193
194 void begin_topology(struct chain_topology *topo);
195
196 void stop_topology(struct chain_topology *topo);
197
198 struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, const struct bitcoin_txid *txid);
199
topology_synced(const struct chain_topology * topo)200 static inline bool topology_synced(const struct chain_topology *topo)
201 {
202 return topo->sync_waiters == NULL;
203 }
204
205 /**
206 * topology_add_sync_waiter: wait for lightningd to sync with bitcoin network
207 * @ctx: context to allocate the waiter from.
208 * @topo: chain topology
209 * @cb: callback to call when we're synced.
210 * @arg: arg for @cb
211 *
212 * topology_synced() must be false when this is called. It will be true
213 * when @cb is called. @cb will not be called if @ctx is freed first.
214 */
215 void topology_add_sync_waiter_(const tal_t *ctx,
216 struct chain_topology *topo,
217 void (*cb)(struct chain_topology *topo,
218 void *arg),
219 void *arg);
220 #define topology_add_sync_waiter(ctx, topo, cb, arg) \
221 topology_add_sync_waiter_((ctx), (topo), \
222 typesafe_cb_preargs(void, void *, \
223 (cb), (arg), \
224 struct chain_topology *), \
225 (arg))
226
227
228 /* In channel_control.c */
229 void notify_feerate_change(struct lightningd *ld);
230 #endif /* LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H */
231