forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onchain_control.c
555 lines (465 loc) · 15.8 KB
/
onchain_control.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <bitcoin/feerate.h>
#include <bitcoin/script.h>
#include <common/key_derive.h>
#include <errno.h>
#include <hsmd/gen_hsm_wire.h>
#include <inttypes.h>
#include <lightningd/chaintopology.h>
#include <lightningd/hsm_control.h>
#include <lightningd/log.h>
#include <lightningd/onchain_control.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
#include <lightningd/watch.h>
#include <onchaind/onchain_wire.h>
#include <wire/wire_sync.h>
/* We dump all the known preimages when onchaind starts up. */
static void onchaind_tell_fulfill(struct channel *channel)
{
struct htlc_in_map_iter ini;
struct htlc_in *hin;
u8 *msg;
struct lightningd *ld = channel->peer->ld;
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
hin;
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (hin->key.channel != channel)
continue;
/* BOLT #5:
*
* A local node:
* - if it receives (or already possesses) a payment preimage
* for an unresolved HTLC output that it has been offered AND
* for which it has committed to an outgoing HTLC:
* - MUST *resolve* the output by spending it, using the
* HTLC-success transaction.
* - MUST resolve the output of that HTLC-success transaction.
* - otherwise:
* - if the *remote node* is NOT irrevocably committed to
* the HTLC:
* - MUST NOT *resolve* the output by spending it.
*/
/* We only set preimage once it's irrevocably committed, and
* we spend even if we don't have an outgoing HTLC (eg. local
* payment complete) */
if (!hin->preimage)
continue;
msg = towire_onchain_known_preimage(channel, hin->preimage);
subd_send_msg(channel->owner, take(msg));
}
}
static void handle_onchain_init_reply(struct channel *channel, const u8 *msg UNUSED)
{
/* FIXME: We may already be ONCHAIN state when we implement restart! */
channel_set_state(channel, FUNDING_SPEND_SEEN, ONCHAIN);
}
/**
* Notify onchaind about the depth change of the watched tx.
*/
static void onchain_tx_depth(struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth)
{
u8 *msg;
msg = towire_onchain_depth(channel, txid, depth);
subd_send_msg(channel->owner, take(msg));
}
/**
* Entrypoint for the txwatch callback, calls onchain_tx_depth.
*/
static enum watch_result onchain_tx_watched(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth)
{
u32 blockheight = get_block_height(ld->topology);
if (depth == 0) {
log_unusual(channel->log, "Chain reorganization!");
channel_set_owner(channel, NULL, false);
/* FIXME!
topology_rescan(peer->ld->topology, peer->funding_txid);
*/
/* We will most likely be freed, so this is a noop */
return KEEP_WATCHING;
}
/* Store the channeltx so we can replay later */
wallet_channeltxs_add(ld->wallet, channel,
WIRE_ONCHAIN_DEPTH, txid, 0, blockheight);
onchain_tx_depth(channel, txid, depth);
return KEEP_WATCHING;
}
static void watch_tx_and_outputs(struct channel *channel,
const struct bitcoin_tx *tx);
/**
* Notify onchaind that an output was spent and register new watches.
*/
static void onchain_txo_spent(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, u32 blockheight)
{
u8 *msg;
watch_tx_and_outputs(channel, tx);
msg = towire_onchain_spent(channel, tx, input_num, blockheight);
subd_send_msg(channel->owner, take(msg));
}
/**
* Entrypoint for the txowatch callback, stores tx and calls onchain_txo_spent.
*/
static enum watch_result onchain_txo_watched(struct channel *channel,
const struct bitcoin_tx *tx,
size_t input_num,
const struct block *block)
{
struct bitcoin_txid txid;
bitcoin_txid(tx, &txid);
/* Store the channeltx so we can replay later */
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
WIRE_ONCHAIN_SPENT, &txid, input_num,
block->height);
onchain_txo_spent(channel, tx, input_num, block->height);
/* We don't need to keep watching: If this output is double-spent
* (reorg), we'll get a zero depth cb to onchain_tx_watched, and
* restart onchaind. */
return DELETE_WATCH;
}
/* To avoid races, we watch the tx and all outputs. */
static void watch_tx_and_outputs(struct channel *channel,
const struct bitcoin_tx *tx)
{
struct bitcoin_txid txid;
struct txwatch *txw;
struct lightningd *ld = channel->peer->ld;
bitcoin_txid(tx, &txid);
/* Make txwatch a parent of txo watches, so we can unwatch together. */
txw = watch_tx(channel->owner, ld->topology, channel, tx,
onchain_tx_watched);
for (size_t i = 0; i < tal_count(tx->output); i++)
watch_txo(txw, ld->topology, channel, &txid, i,
onchain_txo_watched);
}
static void handle_onchain_broadcast_tx(struct channel *channel, const u8 *msg)
{
struct bitcoin_tx *tx;
if (!fromwire_onchain_broadcast_tx(msg, msg, &tx)) {
channel_internal_error(channel, "Invalid onchain_broadcast_tx");
return;
}
/* We don't really care if it fails, we'll respond via watch. */
broadcast_tx(channel->peer->ld->topology, channel, tx, NULL);
}
static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg)
{
struct bitcoin_txid txid;
struct txwatch *txw;
if (!fromwire_onchain_unwatch_tx(msg, &txid)) {
channel_internal_error(channel, "Invalid onchain_unwatch_tx");
return;
}
/* Frees the txo watches, too: see watch_tx_and_outputs() */
txw = find_txwatch(channel->peer->ld->topology, &txid, channel);
if (!txw)
log_unusual(channel->log, "Can't unwatch txid %s",
type_to_string(tmpctx, struct bitcoin_txid, &txid));
tal_free(txw);
}
static void handle_extracted_preimage(struct channel *channel, const u8 *msg)
{
struct preimage preimage;
if (!fromwire_onchain_extracted_preimage(msg, &preimage)) {
channel_internal_error(channel, "Invalid extracted_preimage");
return;
}
onchain_fulfilled_htlc(channel, &preimage);
}
static void handle_missing_htlc_output(struct channel *channel, const u8 *msg)
{
struct htlc_stub htlc;
if (!fromwire_onchain_missing_htlc_output(msg, &htlc)) {
channel_internal_error(channel, "Invalid missing_htlc_output");
return;
}
/* BOLT #5:
*
* - for any committed HTLC that does NOT have an output in this
* commitment transaction:
* - once the commitment transaction has reached reasonable depth:
* - MUST fail the corresponding incoming HTLC (if any).
* - if no *valid* commitment transaction contains an output
* corresponding to the HTLC.
* - MAY fail the corresponding incoming HTLC sooner.
*/
onchain_failed_our_htlc(channel, &htlc, "missing in commitment tx");
}
static void handle_onchain_htlc_timeout(struct channel *channel, const u8 *msg)
{
struct htlc_stub htlc;
if (!fromwire_onchain_htlc_timeout(msg, &htlc)) {
channel_internal_error(channel, "Invalid onchain_htlc_timeout");
return;
}
/* BOLT #5:
*
* - if the commitment transaction HTLC output has *timed out* and
* hasn't been *resolved*:
* - MUST *resolve* the output by spending it using the HTLC-timeout
* transaction.
*/
onchain_failed_our_htlc(channel, &htlc, "timed out");
}
static void handle_irrevocably_resolved(struct channel *channel, const u8 *msg UNUSED)
{
/* FIXME: Implement check_htlcs to ensure no dangling hout->in ptrs! */
free_htlcs(channel->peer->ld, channel);
log_info(channel->log, "onchaind complete, forgetting peer");
/* This will also free onchaind. */
delete_channel(channel);
}
/**
* onchain_add_utxo -- onchaind is telling us about an UTXO we own
*/
static void onchain_add_utxo(struct channel *channel, const u8 *msg)
{
struct utxo *u = tal(msg, struct utxo);
u32 blockheight;
u->close_info = tal(u, struct unilateral_close_info);
u->is_p2sh = true;
u->keyindex = 0;
u->status = output_state_available;
u->close_info->channel_id = channel->dbid;
u->close_info->peer_id = channel->peer->id;
u->spendheight = NULL;
if (!fromwire_onchain_add_utxo(msg, &u->txid, &u->outnum,
&u->close_info->commitment_point,
&u->amount, &blockheight)) {
fatal("onchaind gave invalid add_utxo message: %s", tal_hex(msg, msg));
}
u->blockheight = blockheight>0?&blockheight:NULL;
outpointfilter_add(channel->peer->ld->wallet->owned_outpoints, &u->txid, u->outnum);
wallet_add_utxo(channel->peer->ld->wallet, u, p2wpkh);
}
static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds UNUSED)
{
enum onchain_wire_type t = fromwire_peektype(msg);
switch (t) {
case WIRE_ONCHAIN_INIT_REPLY:
handle_onchain_init_reply(sd->channel, msg);
break;
case WIRE_ONCHAIN_BROADCAST_TX:
handle_onchain_broadcast_tx(sd->channel, msg);
break;
case WIRE_ONCHAIN_UNWATCH_TX:
handle_onchain_unwatch_tx(sd->channel, msg);
break;
case WIRE_ONCHAIN_EXTRACTED_PREIMAGE:
handle_extracted_preimage(sd->channel, msg);
break;
case WIRE_ONCHAIN_MISSING_HTLC_OUTPUT:
handle_missing_htlc_output(sd->channel, msg);
break;
case WIRE_ONCHAIN_HTLC_TIMEOUT:
handle_onchain_htlc_timeout(sd->channel, msg);
break;
case WIRE_ONCHAIN_ALL_IRREVOCABLY_RESOLVED:
handle_irrevocably_resolved(sd->channel, msg);
break;
case WIRE_ONCHAIN_ADD_UTXO:
onchain_add_utxo(sd->channel, msg);
break;
/* We send these, not receive them */
case WIRE_ONCHAIN_INIT:
case WIRE_ONCHAIN_SPENT:
case WIRE_ONCHAIN_DEPTH:
case WIRE_ONCHAIN_HTLC:
case WIRE_ONCHAIN_KNOWN_PREIMAGE:
case WIRE_ONCHAIN_DEV_MEMLEAK:
case WIRE_ONCHAIN_DEV_MEMLEAK_REPLY:
break;
}
return 0;
}
/* If we want to know if this HTLC is missing, return depth. */
static bool tell_if_missing(const struct channel *channel,
struct htlc_stub *stub,
bool *tell_immediate)
{
struct htlc_out *hout;
/* Keep valgrind happy. */
*tell_immediate = false;
/* Don't care about incoming HTLCs, just ones we offered. */
if (stub->owner == REMOTE)
return false;
/* Might not be a current HTLC. */
hout = find_htlc_out(&channel->peer->ld->htlcs_out, channel, stub->id);
if (!hout)
return false;
/* BOLT #5:
*
* - for any committed HTLC that does NOT have an output in this
* commitment transaction:
* - once the commitment transaction has reached reasonable depth:
* - MUST fail the corresponding incoming HTLC (if any).
* - if no *valid* commitment transaction contains an output
* corresponding to the HTLC.
* - MAY fail the corresponding incoming HTLC sooner.
*/
if (hout->hstate >= RCVD_ADD_REVOCATION
&& hout->hstate < SENT_REMOVE_REVOCATION)
*tell_immediate = true;
log_debug(channel->log,
"We want to know if htlc %"PRIu64" is missing (%s)",
hout->key.id, *tell_immediate ? "immediate" : "later");
return true;
}
/* Only error onchaind can get is if it dies. */
static void onchain_error(struct channel *channel,
int peer_fd UNUSED, int gossip_fd UNUSED,
const struct crypto_state *cs UNUSED,
const struct channel_id *channel_id UNUSED,
const char *desc,
const u8 *err_for_them UNUSED)
{
/* FIXME: re-launch? */
log_broken(channel->log, "%s", desc);
channel_set_billboard(channel, true, desc);
channel_set_owner(channel, NULL, false);
}
/* With a reorg, this can get called multiple times; each time we'll kill
* onchaind (like any other owner), and restart */
enum watch_result onchaind_funding_spent(struct channel *channel,
const struct bitcoin_tx *tx,
u32 blockheight)
{
u8 *msg;
struct bitcoin_txid our_last_txid;
struct htlc_stub *stubs;
struct lightningd *ld = channel->peer->ld;
struct pubkey final_key;
int hsmfd;
u32 feerate;
channel_fail_permanent(channel, "Funding transaction spent");
/* We could come from almost any state. */
channel_set_state(channel, channel->state, FUNDING_SPEND_SEEN);
hsmfd = hsm_get_client_fd(ld, &channel->peer->id,
channel->dbid,
HSM_CAP_SIGN_ONCHAIN_TX
| HSM_CAP_COMMITMENT_POINT);
channel_set_owner(channel, new_channel_subd(ld,
"lightning_onchaind",
channel,
channel->log, false,
onchain_wire_type_name,
onchain_msg,
onchain_error,
channel_set_billboard,
take(&hsmfd),
NULL),
false);
if (!channel->owner) {
log_broken(channel->log, "Could not subdaemon onchain: %s",
strerror(errno));
return KEEP_WATCHING;
}
stubs = wallet_htlc_stubs(tmpctx, ld->wallet, channel);
if (!stubs) {
log_broken(channel->log, "Could not load htlc_stubs");
return KEEP_WATCHING;
}
if (!bip32_pubkey(ld->wallet->bip32_base, &final_key,
channel->final_key_idx)) {
log_broken(channel->log, "Could not derive onchain key %"PRIu64,
channel->final_key_idx);
return KEEP_WATCHING;
}
/* This could be a mutual close, but it doesn't matter. */
bitcoin_txid(channel->last_tx, &our_last_txid);
/* We try to use normal feerate for onchaind spends. */
feerate = try_get_feerate(ld->topology, FEERATE_NORMAL);
if (!feerate) {
/* We have at least one data point: the last tx's feerate. */
u64 fee = channel->funding_satoshi;
for (size_t i = 0; i < tal_count(channel->last_tx->output); i++)
fee -= channel->last_tx->output[i].amount;
feerate = fee / measure_tx_weight(tx);
if (feerate < feerate_floor())
feerate = feerate_floor();
}
msg = towire_onchain_init(channel,
&channel->their_shachain.chain,
channel->funding_satoshi,
&channel->channel_info.old_remote_per_commit,
&channel->channel_info.remote_per_commit,
/* BOLT #2:
* `to_self_delay` is the number of blocks
* that the other node's to-self outputs
* must be delayed */
/* So, these are reversed: they specify ours,
* we specify theirs. */
channel->channel_info.their_config.to_self_delay,
channel->our_config.to_self_delay,
feerate,
channel->our_config.dust_limit_satoshis,
&our_last_txid,
p2wpkh_for_keyidx(tmpctx, ld,
channel->final_key_idx),
channel->remote_shutdown_scriptpubkey,
&final_key,
channel->funder,
&channel->local_basepoints,
&channel->channel_info.theirbase,
tx,
blockheight,
/* FIXME: config for 'reasonable depth' */
3,
channel->last_htlc_sigs,
tal_count(stubs),
channel->min_possible_feerate,
channel->max_possible_feerate,
channel->future_per_commitment_point);
subd_send_msg(channel->owner, take(msg));
/* FIXME: Don't queue all at once, use an empty cb... */
for (size_t i = 0; i < tal_count(stubs); i++) {
bool tell_immediate;
bool tell = tell_if_missing(channel, &stubs[i], &tell_immediate);
msg = towire_onchain_htlc(channel, &stubs[i],
tell, tell_immediate);
subd_send_msg(channel->owner, take(msg));
}
/* Tell it about any preimages we know. */
onchaind_tell_fulfill(channel);
watch_tx_and_outputs(channel, tx);
/* We keep watching until peer finally deleted, for reorgs. */
return KEEP_WATCHING;
}
void onchaind_replay_channels(struct lightningd *ld)
{
u32 *onchaind_ids;
struct channeltx *txs;
struct channel *chan;
db_begin_transaction(ld->wallet->db);
onchaind_ids = wallet_onchaind_channels(ld->wallet, ld);
for (size_t i = 0; i < tal_count(onchaind_ids); i++) {
log_info(ld->log, "Restarting onchaind for channel %d",
onchaind_ids[i]);
txs = wallet_channeltxs_get(ld->wallet, onchaind_ids,
onchaind_ids[i]);
chan = channel_by_dbid(ld, onchaind_ids[i]);
for (size_t j = 0; j < tal_count(txs); j++) {
if (txs[j].type == WIRE_ONCHAIN_INIT) {
onchaind_funding_spent(chan, txs[j].tx,
txs[j].blockheight);
} else if (txs[j].type == WIRE_ONCHAIN_SPENT) {
onchain_txo_spent(chan, txs[j].tx,
txs[j].input_num,
txs[j].blockheight);
} else if (txs[j].type == WIRE_ONCHAIN_DEPTH) {
onchain_tx_depth(chan, &txs[j].txid,
txs[j].depth);
} else {
fatal("unknown message of type %d during "
"onchaind replay",
txs[j].type);
}
}
tal_free(txs);
}
tal_free(onchaind_ids);
db_commit_transaction(ld->wallet->db);
}