1 #ifndef LIGHTNING_PLUGINS_SPENDER_MULTIFUNDCHANNEL_H 2 #define LIGHTNING_PLUGINS_SPENDER_MULTIFUNDCHANNEL_H 3 #include "config.h" 4 5 #include <ccan/ccan/list/list.h> 6 #include <common/channel_id.h> 7 #include <plugins/libplugin.h> 8 9 extern const struct plugin_command multifundchannel_commands[]; 10 extern const size_t num_multifundchannel_commands; 11 12 /* Which protocol this channel open is using. 13 * OPEN_CHANNEL implies opt_dual_fund */ 14 enum channel_protocol { 15 FUND_CHANNEL, 16 OPEN_CHANNEL, 17 }; 18 19 /* Current state of the funding process. */ 20 enum multifundchannel_state { 21 /* We have not yet performed `fundchannel_start`. */ 22 MULTIFUNDCHANNEL_START_NOT_YET = 0, 23 /* The `connect` command succeeded. `*/ 24 MULTIFUNDCHANNEL_CONNECTED, 25 26 /* The `fundchannel_start` or `openchannel_init` command 27 * succeeded. */ 28 MULTIFUNDCHANNEL_STARTED, 29 30 /* V1 states */ 31 /* The `fundchannel_complete` command succeeded. */ 32 MULTIFUNDCHANNEL_COMPLETED, 33 34 /* V2 states */ 35 /* The `openchannel_update` command succeeded. */ 36 MULTIFUNDCHANNEL_UPDATED, 37 /* The commitments for this destinations have been secured */ 38 MULTIFUNDCHANNEL_SECURED, 39 /* We've recieved the peer sigs for this destination */ 40 MULTIFUNDCHANNEL_SIGNED, 41 /* We've gotten their sigs, but still waiting for their commit sigs */ 42 MULTIFUNDCHANNEL_SIGNED_NOT_SECURED, 43 44 /* The transaction might now be broadcasted. */ 45 MULTIFUNDCHANNEL_DONE, 46 /* Global fail state. Oops */ 47 MULTIFUNDCHANNEL_FAILED, 48 }; 49 50 /* Stores a destination that was removed due to some failure. */ 51 struct multifundchannel_removed { 52 /* The destination we removed. */ 53 struct node_id id; 54 /* The method that failed: 55 connect, fundchannel_start, fundchannel_complete. 56 */ 57 const char *method; 58 const char *error_message; 59 errcode_t error_code; 60 /* Optional JSON object containing extra data */ 61 const char *error_data; 62 }; 63 64 /* the object for a single destination. */ 65 struct multifundchannel_destination { 66 /* the overall multifundchannel command object. */ 67 struct multifundchannel_command *mfc; 68 69 /* the overall multifundchannel_command contains an 70 array of multifundchannel_destinations. 71 this provides the index within the array. 72 73 this is used in debug printing. 74 */ 75 unsigned int index; 76 77 /* id for this destination. */ 78 struct node_id id; 79 /* address hint for this destination, null if not 80 specified. 81 */ 82 const char *addrhint; 83 /* the features this destination has. */ 84 const u8 *their_features; 85 86 /* whether we have `fundchannel_start`, failed `connect` or 87 `fundchannel_complete`, etc. 88 */ 89 enum multifundchannel_state state; 90 91 /* Last known state before failure */ 92 enum multifundchannel_state fail_state; 93 94 /* the actual target script and address. */ 95 const u8 *funding_script; 96 const char *funding_addr; 97 98 /* the upfront shutdown script for this channel */ 99 const char *close_to_str; 100 101 /* The scriptpubkey we will close to. Only set if 102 * peer supports opt_upfront_shutdownscript and 103 * we passsed in a valid close_to_str */ 104 const u8 *close_to_script; 105 106 /* the amount to be funded for this destination. 107 if the specified amount is "all" then the `all` 108 flag is set, and the amount is initially 0 until 109 we have figured out how much exactly "all" is, 110 after the dryrun stage. 111 */ 112 bool all; 113 struct amount_sat amount; 114 115 /* the output index for this destination. */ 116 unsigned int outnum; 117 118 /* whether the channel to this destination will 119 be announced. 120 */ 121 bool announce; 122 /* how much of the initial funding to push to 123 the destination. 124 */ 125 struct amount_msat push_msat; 126 127 /* the actual channel_id. */ 128 struct channel_id channel_id; 129 130 const char *error_message; 131 errcode_t error_code; 132 /* Optional JSON object containing extra data */ 133 const char *error_data; 134 135 /* what channel protocol this destination is using */ 136 enum channel_protocol protocol; 137 138 /* PSBT for the inflight channel open (OPEN_CHANNEL) */ 139 struct wally_psbt *psbt; 140 141 /* PSBT for the inflight channel open, updated (OPEN_CHANNEL) */ 142 struct wally_psbt *updated_psbt; 143 144 /* serial of the funding output for this channel (OPEN_CHANNEL) */ 145 u64 funding_serial; 146 147 /* amount to request peer to lease (OPEN_CHANNEL) */ 148 struct amount_sat request_amt; 149 150 /* Channel lease rates that we expect the peer to respond with */ 151 struct lease_rates *rates; 152 }; 153 154 155 /* The object for a single multifundchannel command. */ 156 struct multifundchannel_command { 157 /* A unique numeric identifier for this particular 158 multifundchannel execution. 159 160 This is used for debug logs; we want to be able to 161 identify *which* multifundchannel is being described 162 in the debug logs, especially if the user runs 163 multiple `multifundchannel` commands in parallel, or 164 in very close sequence, which might confuse us with 165 *which* debug message belongs with *which* command. 166 167 We actually just reuse the id from the cmd. 168 Store it here for easier access. 169 */ 170 u64 id; 171 172 /* The plugin-level command. */ 173 struct command *cmd; 174 /* An array of destinations. */ 175 struct multifundchannel_destination *destinations; 176 /* Number of pending parallel fundchannel_start or 177 fundchannel_complete. 178 */ 179 size_t pending; 180 181 /* The feerate desired by the user. */ 182 const char *feerate_str; 183 184 /* If specified, the feerate to be used for channel commitment 185 * transactions. Defaults to the `feerate_str` if not provided. */ 186 const char *cmtmt_feerate_str; 187 188 /* The minimum number of confirmations for owned 189 UTXOs to be selected. 190 */ 191 u32 minconf; 192 /* The set of utxos to be used. */ 193 const char *utxos_str; 194 /* How long should we keep going if things fail. */ 195 size_t minchannels; 196 /* Array of destinations that were removed in a best-effort 197 attempt to fund as many channels as possible. 198 */ 199 struct multifundchannel_removed *removeds; 200 201 /* The PSBT of the funding transaction we are building. 202 Prior to `fundchannel_start` completing for all destinations, 203 this contains an unsigned incomplete transaction that is just a 204 reservation of the inputs. 205 After `fundchannel_start`, this contains an unsigned transaction 206 with complete outputs. 207 After `fundchannel_complete`, this contains a signed, finalized 208 transaction. 209 */ 210 struct wally_psbt *psbt; 211 /* The actual feerate of the PSBT. */ 212 u32 feerate_per_kw; 213 /* The expected weight of the PSBT after adding in all the outputs. 214 * In weight units (sipa). */ 215 u32 estimated_final_weight; 216 /* Excess satoshi from the PSBT. 217 * If "all" this is the entire amount; if not "all" this is the 218 * proposed change amount, which if dusty should be donated to 219 * the miners. 220 */ 221 struct amount_sat excess_sat; 222 223 /* A convenient change address. NULL at the start, filled in 224 * if we detect we need it. */ 225 const u8 *change_scriptpubkey; 226 /* Whether we need a change output. */ 227 bool change_needed; 228 /* The change amount. */ 229 struct amount_sat change_amount; 230 231 /* The txid of the final funding transaction. */ 232 struct bitcoin_txid *txid; 233 234 /* The actual tx of the actual final funding transaction 235 that was broadcast. 236 */ 237 const char *final_tx; 238 const char *final_txid; 239 240 /* V2 things */ 241 struct list_node list; 242 243 /* V2 channel opens use this flag to gate PSBT signing */ 244 bool sigs_collected; 245 }; 246 247 /* Use this instead of forward_error. */ 248 struct command_result * 249 mfc_forward_error(struct command *cmd, 250 const char *buf, const jsmntok_t *error, 251 struct multifundchannel_command *); 252 253 /* When a destination fails, record the furthest state reached, and the 254 * error message for the failure */ 255 void fail_destination_tok(struct multifundchannel_destination *dest, 256 const char *buf, 257 const jsmntok_t *error); 258 void fail_destination_msg(struct multifundchannel_destination *dest, 259 int error_code, 260 const char *err_str TAKES); 261 262 /* dest_count - Returns count of destinations using given protocol version */ 263 size_t dest_count(const struct multifundchannel_command *mfc, 264 enum channel_protocol); 265 266 /* Is this destination using the v2/OPEN_CHANNEL protocol? */ 267 bool is_v2(const struct multifundchannel_destination *dest); 268 269 /* Use this instead of command_finished. */ 270 struct command_result * 271 mfc_finished(struct multifundchannel_command *, struct json_stream *response); 272 273 struct command_result * 274 after_channel_start(struct multifundchannel_command *mfc); 275 276 struct command_result * 277 perform_fundchannel_complete(struct multifundchannel_command *mfc); 278 279 struct command_result * 280 perform_signpsbt(struct multifundchannel_command *mfc); 281 282 struct command_result * 283 multifundchannel_finished(struct multifundchannel_command *mfc); 284 285 struct command_result * 286 redo_multifundchannel(struct multifundchannel_command *mfc, 287 const char *failing_method, 288 const char *why); 289 #endif /* LIGHTNING_PLUGINS_SPENDER_MULTIFUNDCHANNEL_H */ 290