1 #ifndef LIGHTNING_LIGHTNINGD_BITCOIND_H
2 #define LIGHTNING_LIGHTNINGD_BITCOIND_H
3 #include "config.h"
4 #include <bitcoin/chainparams.h>
5 #include <bitcoin/tx.h>
6 #include <ccan/list/list.h>
7 #include <ccan/strmap/strmap.h>
8 
9 struct bitcoin_blkid;
10 struct bitcoin_tx_output;
11 struct block;
12 struct lightningd;
13 struct ripemd160;
14 struct bitcoin_tx;
15 struct bitcoin_block;
16 
17 struct bitcoind {
18 	/* Where to do logging. */
19 	struct log *log;
20 
21 	/* Main lightningd structure */
22 	struct lightningd *ld;
23 
24 	/* Is our Bitcoin backend synced?  If not, we retry. */
25 	bool synced;
26 
27 	/* Ignore results, we're shutting down. */
28 	bool shutdown;
29 
30 	/* Timer if we're waiting for it to warm up. */
31 	struct oneshot *checkchain_timer;
32 
33 	struct list_head pending_getfilteredblock;
34 
35 	/* Map each method to a plugin, so we can have multiple plugins
36 	 * handling different functionalities. */
37 	STRMAP(struct plugin *) pluginsmap;
38 };
39 
40 /* A single outpoint in a filtered block */
41 struct filteredblock_outpoint {
42 	struct bitcoin_outpoint outpoint;
43 	u32 txindex;
44 	const u8 *scriptPubKey;
45 	struct amount_sat amount;
46 };
47 
48 /* A struct representing a block with most of the parts filtered out. */
49 struct filteredblock {
50 	struct bitcoin_blkid id;
51 	u32 height;
52 	struct bitcoin_blkid prev_hash;
53 	struct filteredblock_outpoint **outpoints;
54 };
55 
56 struct bitcoind *new_bitcoind(const tal_t *ctx,
57 			      struct lightningd *ld,
58 			      struct log *log);
59 
60 void bitcoind_estimate_fees_(struct bitcoind *bitcoind,
61 			     size_t num_estimates,
62 			     void (*cb)(struct bitcoind *bitcoind,
63 					const u32 satoshi_per_kw[], void *),
64 			     void *arg);
65 
66 #define bitcoind_estimate_fees(bitcoind_, num, cb, arg) \
67 	bitcoind_estimate_fees_((bitcoind_), (num), \
68 				typesafe_cb_preargs(void, void *,	\
69 						    (cb), (arg),	\
70 						    struct bitcoind *,	\
71 						    const u32 *),	\
72 				(arg))
73 
74 void bitcoind_sendrawtx_ahf_(struct bitcoind *bitcoind,
75 			     const char *hextx,
76 			     bool allowhighfees,
77 			     void (*cb)(struct bitcoind *bitcoind,
78 					bool success, const char *msg, void *),
79 			     void *arg);
80 #define bitcoind_sendrawtx_ahf(bitcoind_, hextx, allowhighfees, cb, arg)\
81 	bitcoind_sendrawtx_ahf_((bitcoind_), (hextx),			\
82 				(allowhighfees),			\
83 				typesafe_cb_preargs(void, void *,	\
84 						    (cb), (arg),	\
85 						    struct bitcoind *,	\
86 						    bool, const char *),\
87 				(arg))
88 
89 void bitcoind_sendrawtx_(struct bitcoind *bitcoind,
90 			 const char *hextx,
91 			 void (*cb)(struct bitcoind *bitcoind,
92 				    bool success, const char *msg, void *),
93 			 void *arg);
94 
95 #define bitcoind_sendrawtx(bitcoind_, hextx, cb, arg)			\
96 	bitcoind_sendrawtx_((bitcoind_), (hextx),			\
97 			    typesafe_cb_preargs(void, void *,		\
98 						(cb), (arg),		\
99 						struct bitcoind *,	\
100 						bool, const char *),	\
101 			    (arg))
102 
103 void bitcoind_getfilteredblock_(struct bitcoind *bitcoind, u32 height,
104 				void (*cb)(struct bitcoind *bitcoind,
105 					   const struct filteredblock *fb,
106 					   void *arg),
107 				void *arg);
108 #define bitcoind_getfilteredblock(bitcoind_, height, cb, arg)		\
109 	bitcoind_getfilteredblock_((bitcoind_),				\
110 				   (height),				\
111 				   typesafe_cb_preargs(void, void *,	\
112 						       (cb), (arg),	\
113 						       struct bitcoind *, \
114 						       const struct filteredblock *), \
115 				   (arg))
116 
117 void bitcoind_getchaininfo_(struct bitcoind *bitcoind,
118 			    const bool first_call,
119 			    void (*cb)(struct bitcoind *bitcoind,
120 				       const char *chain,
121 				       u32 headercount,
122 				       u32 blockcount,
123 				       const bool ibd,
124 				       const bool first_call, void *),
125 			    void *cb_arg);
126 #define bitcoind_getchaininfo(bitcoind_, first_call_, cb, arg)		   \
127 	bitcoind_getchaininfo_((bitcoind_), (first_call_),		   \
128 			      typesafe_cb_preargs(void, void *,		   \
129 						  (cb), (arg),		   \
130 						  struct bitcoind *,	   \
131 						  const char *, u32, u32,  \
132 						  const bool, const bool), \
133 			      (arg))
134 
135 void bitcoind_getrawblockbyheight_(struct bitcoind *bitcoind,
136 				   u32 height,
137 				   void (*cb)(struct bitcoind *bitcoind,
138 					      struct bitcoin_blkid *blkid,
139 					      struct bitcoin_block *blk,
140 					      void *arg),
141 				   void *arg);
142 #define bitcoind_getrawblockbyheight(bitcoind_, height_, cb, arg)		\
143 	bitcoind_getrawblockbyheight_((bitcoind_), (height_),			\
144 				      typesafe_cb_preargs(void, void *,		\
145 							  (cb), (arg),		\
146 							  struct bitcoind *,	\
147 							  struct bitcoin_blkid *, \
148 							  struct bitcoin_block *),\
149 				      (arg))
150 
151 void bitcoind_getutxout_(struct bitcoind *bitcoind,
152 			 const struct bitcoin_outpoint *outpoint,
153 			 void (*cb)(struct bitcoind *bitcoind,
154 				    const struct bitcoin_tx_output *txout,
155 				    void *arg),
156 			 void *arg);
157 #define bitcoind_getutxout(bitcoind_, outpoint_, cb, arg)		\
158 	bitcoind_getutxout_((bitcoind_), (outpoint_),			\
159 			    typesafe_cb_preargs(void, void *,		\
160 					        (cb), (arg),		\
161 					        struct bitcoind *,	\
162 					        struct bitcoin_tx_output *),\
163 			    (arg))
164 
165 void bitcoind_getclientversion(struct bitcoind *bitcoind);
166 
167 void bitcoind_check_commands(struct bitcoind *bitcoind);
168 
169 #endif /* LIGHTNING_LIGHTNINGD_BITCOIND_H */
170