2018-08-23 01:27:25 +02:00
|
|
|
#include <bitcoin/feerate.h>
|
2018-03-07 01:06:07 +01:00
|
|
|
#include <bitcoin/script.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <common/key_derive.h>
|
2019-10-15 12:58:30 +02:00
|
|
|
#include <common/utils.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/chaintopology.h>
|
2020-04-02 04:01:05 +02:00
|
|
|
#include <lightningd/coin_mvts.h>
|
2018-07-23 04:23:02 +02:00
|
|
|
#include <lightningd/hsm_control.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/log.h>
|
|
|
|
#include <lightningd/onchain_control.h>
|
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/subd.h>
|
|
|
|
#include <lightningd/watch.h>
|
2018-07-23 04:23:02 +02:00
|
|
|
#include <wire/wire_sync.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* 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:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* 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.
|
2021-02-22 02:58:14 +01:00
|
|
|
* - MUST NOT reveal its own preimage when it's not the final recipient...
|
2018-06-17 12:13:44 +02:00
|
|
|
* - 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.
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2020-04-03 03:46:18 +02:00
|
|
|
/* Sooo these are *probably* replays since they're coming
|
|
|
|
* from the database but it's hard to be sure since we update
|
|
|
|
* the database before notifying onchaind about them.
|
|
|
|
* There's a *very* rare chance that we'll not log them,
|
|
|
|
* only in that we only make ledger records as a result of this call
|
|
|
|
* iff the output isn't deemed 'trackable'. So if we do miss a
|
|
|
|
* ledger record as a result of this decision, it's guaranteed to be
|
|
|
|
* impreceptibly tiny *and* not show up anywhere else in the node's
|
|
|
|
* utxo set.
|
|
|
|
*
|
|
|
|
* Aka a reconciliator's nightmare.
|
|
|
|
* The alternative is to double-count *every* ignored htlc output
|
|
|
|
* It's easier to delete than find a missing, but I'm banking on
|
|
|
|
* the rarity of failure here. (hahaha) */
|
2020-08-25 04:15:48 +02:00
|
|
|
msg = towire_onchaind_known_preimage(channel, hin->preimage, false);
|
2018-02-20 21:59:09 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:48:39 +02:00
|
|
|
static void handle_onchain_init_reply(struct channel *channel, const u8 *msg UNUSED)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2018-02-23 07:23:51 +01:00
|
|
|
/* FIXME: We may already be ONCHAIN state when we implement restart! */
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
channel_set_state(channel,
|
|
|
|
FUNDING_SPEND_SEEN,
|
|
|
|
ONCHAIN,
|
|
|
|
REASON_UNKNOWN,
|
|
|
|
"Onchain init reply");
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 13:20:45 +02:00
|
|
|
/**
|
|
|
|
* Notify onchaind about the depth change of the watched tx.
|
|
|
|
*/
|
|
|
|
static void onchain_tx_depth(struct channel *channel,
|
|
|
|
const struct bitcoin_txid *txid,
|
2020-04-03 03:46:18 +02:00
|
|
|
unsigned int depth,
|
|
|
|
bool is_replay)
|
2018-04-16 13:20:45 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
2020-08-25 04:15:48 +02:00
|
|
|
msg = towire_onchaind_depth(channel, txid, depth, is_replay);
|
2018-04-16 13:20:45 +02:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entrypoint for the txwatch callback, calls onchain_tx_depth.
|
|
|
|
*/
|
2018-08-13 05:05:33 +02:00
|
|
|
static enum watch_result onchain_tx_watched(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
2018-04-09 15:20:54 +02:00
|
|
|
const struct bitcoin_txid *txid,
|
2019-06-28 03:58:31 +02:00
|
|
|
const struct bitcoin_tx *tx,
|
2018-02-20 21:59:09 +01:00
|
|
|
unsigned int depth)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2018-08-13 05:05:33 +02:00
|
|
|
u32 blockheight = get_block_height(ld->topology);
|
2019-06-28 03:58:31 +02:00
|
|
|
|
|
|
|
if (tx != NULL) {
|
|
|
|
struct bitcoin_txid txid2;
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &txid2);
|
|
|
|
if (!bitcoin_txid_eq(txid, &txid2)) {
|
|
|
|
channel_internal_error(channel, "Txid for %s is not %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct bitcoin_tx,
|
|
|
|
tx),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct bitcoin_txid,
|
|
|
|
txid));
|
|
|
|
return DELETE_WATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (depth == 0) {
|
|
|
|
log_unusual(channel->log, "Chain reorganization!");
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* We will most likely be freed, so this is a noop */
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
}
|
|
|
|
|
2018-04-17 15:20:27 +02:00
|
|
|
/* Store the channeltx so we can replay later */
|
2018-08-13 05:05:33 +02:00
|
|
|
wallet_channeltxs_add(ld->wallet, channel,
|
2020-08-25 04:15:48 +02:00
|
|
|
WIRE_ONCHAIND_DEPTH, txid, 0, blockheight);
|
2018-04-17 15:20:27 +02:00
|
|
|
|
2020-04-03 03:46:18 +02:00
|
|
|
onchain_tx_depth(channel, txid, depth, false);
|
2018-02-20 21:59:09 +01:00
|
|
|
return KEEP_WATCHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void watch_tx_and_outputs(struct channel *channel,
|
|
|
|
const struct bitcoin_tx *tx);
|
|
|
|
|
2018-04-16 13:20:45 +02:00
|
|
|
/**
|
|
|
|
* Notify onchaind that an output was spent and register new watches.
|
|
|
|
*/
|
2020-04-03 03:46:18 +02:00
|
|
|
static void onchain_txo_spent(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, u32 blockheight, bool is_replay)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
|
|
|
u8 *msg;
|
2020-05-26 05:39:40 +02:00
|
|
|
/* Onchaind needs all inputs, since it uses those to compare
|
|
|
|
* with existing spends (which can vary, with feerate changes). */
|
|
|
|
struct tx_parts *parts = tx_parts_from_wally_tx(tmpctx, tx->wtx,
|
|
|
|
-1, -1);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
watch_tx_and_outputs(channel, tx);
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
msg = towire_onchaind_spent(channel, parts, input_num, blockheight, is_replay);
|
2018-02-20 21:59:09 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
|
2018-04-16 13:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2018-04-17 15:20:27 +02:00
|
|
|
struct bitcoin_txid txid;
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
|
|
|
|
/* Store the channeltx so we can replay later */
|
|
|
|
wallet_channeltxs_add(channel->peer->ld->wallet, channel,
|
2020-08-25 04:15:48 +02:00
|
|
|
WIRE_ONCHAIND_SPENT, &txid, input_num,
|
2018-04-17 15:20:27 +02:00
|
|
|
block->height);
|
|
|
|
|
2020-04-03 03:46:18 +02:00
|
|
|
onchain_txo_spent(channel, tx, input_num, block->height, false);
|
2018-04-16 13:20:45 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* 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,
|
2018-02-20 21:59:09 +01:00
|
|
|
onchain_tx_watched);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++)
|
2018-02-20 21:59:09 +01:00
|
|
|
watch_txo(txw, ld->topology, channel, &txid, i,
|
2018-02-20 21:59:09 +01:00
|
|
|
onchain_txo_watched);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 04:16:32 +02:00
|
|
|
static void handle_onchain_log_coin_move(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct chain_coin_mvt *mvt = tal(NULL, struct chain_coin_mvt);
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_notify_coin_mvt(msg, mvt)) {
|
2020-04-02 04:16:32 +02:00
|
|
|
channel_internal_error(channel, "Invalid onchain notify_coin_mvt");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvt->account_name =
|
2020-09-09 09:20:53 +02:00
|
|
|
type_to_string(mvt, struct channel_id, &channel->cid);
|
2020-04-02 04:16:32 +02:00
|
|
|
notify_chain_mvt(channel->peer->ld, mvt);
|
|
|
|
tal_free(mvt);
|
|
|
|
}
|
|
|
|
|
2020-09-08 05:21:02 +02:00
|
|
|
/** handle_onchain_broadcast_rbf_tx_cb
|
|
|
|
*
|
|
|
|
* @brief suppresses the rebroadcast of a
|
|
|
|
* transaction.
|
|
|
|
*
|
|
|
|
* @desc when using the `bitcoin_tx` function,
|
|
|
|
* if a callback is not given, the transaction
|
|
|
|
* will be rebroadcast automatically by
|
|
|
|
* chaintopology.
|
|
|
|
* However, in the case of an RBF transaction
|
|
|
|
* from `onchaind`, `onchaind` will periodically
|
|
|
|
* create a new, higher-fee replacement, thus
|
|
|
|
* `onchaind` will trigger rebroadcast (with a
|
|
|
|
* higher fee) by itself, which the `lightningd`
|
|
|
|
* chaintopology should not repeat.
|
|
|
|
* This callback exists to suppress the
|
|
|
|
* rebroadcast behavior of chaintopology.
|
|
|
|
*
|
|
|
|
* @param channel - the channel for which the
|
|
|
|
* transaction was broadcast.
|
|
|
|
* @param success - whether the tx was broadcast.
|
|
|
|
* @param err - the error received from the
|
|
|
|
* underlying sendrawtx.
|
|
|
|
*/
|
|
|
|
static void handle_onchain_broadcast_rbf_tx_cb(struct channel *channel,
|
|
|
|
bool success,
|
|
|
|
const char *err)
|
|
|
|
{
|
|
|
|
/* Victory is boring. */
|
|
|
|
if (success)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Failure is unusual but not broken: it is possible that just
|
|
|
|
* as we were about to broadcast, a new block came in which
|
|
|
|
* contains a previous version of the transaction, thus
|
|
|
|
* causing the higher-fee replacement to fail broadcast.
|
|
|
|
*
|
|
|
|
* ...or it could be a bug in onchaind which prevents it from
|
|
|
|
* successfully RBFing out the transaction, in which case we
|
|
|
|
* should log it for devs to check.
|
|
|
|
*/
|
|
|
|
log_unusual(channel->log,
|
|
|
|
"Broadcast of RBF tx failed, "
|
|
|
|
"did a new block just come in? "
|
|
|
|
"error: %s",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_onchain_broadcast_tx(struct channel *channel,
|
|
|
|
const u8 *msg)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx;
|
2019-05-27 13:04:10 +02:00
|
|
|
struct wallet *w = channel->peer->ld->wallet;
|
|
|
|
struct bitcoin_txid txid;
|
2019-06-07 11:38:20 +02:00
|
|
|
enum wallet_tx_type type;
|
2020-09-08 05:21:02 +02:00
|
|
|
bool is_rbf;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2020-09-08 05:21:02 +02:00
|
|
|
if (!fromwire_onchaind_broadcast_tx(msg, msg, &tx, &type, &is_rbf)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "Invalid onchain_broadcast_tx");
|
|
|
|
return;
|
|
|
|
}
|
2020-09-08 05:21:02 +02:00
|
|
|
|
2019-10-15 12:58:30 +02:00
|
|
|
tx->chainparams = chainparams;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2019-05-27 13:04:10 +02:00
|
|
|
bitcoin_txid(tx, &txid);
|
2020-08-07 03:30:47 +02:00
|
|
|
wallet_transaction_add(w, tx->wtx, 0, 0);
|
2019-05-27 13:04:10 +02:00
|
|
|
wallet_transaction_annotate(w, &txid, type, channel->dbid);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* We don't really care if it fails, we'll respond via watch. */
|
2020-09-08 05:22:41 +02:00
|
|
|
/* If the onchaind signals this as RBF-able, then we also
|
|
|
|
* set allowhighfees, as the transaction may be RBFed into
|
|
|
|
* high feerates as protection against the MAD-HTLC attack. */
|
|
|
|
broadcast_tx_ahf(channel->peer->ld->topology, channel,
|
|
|
|
tx, is_rbf,
|
|
|
|
is_rbf ? &handle_onchain_broadcast_rbf_tx_cb : NULL);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_txid txid;
|
|
|
|
struct txwatch *txw;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_unwatch_tx(msg, &txid)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
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",
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &txid));
|
2018-02-20 21:59:09 +01:00
|
|
|
tal_free(txw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_extracted_preimage(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct preimage preimage;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_extracted_preimage(msg, &preimage)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
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;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_missing_htlc_output(msg, &htlc)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "Invalid missing_htlc_output");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #5:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - 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.
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_htlc_timeout(msg, &htlc)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "Invalid onchain_htlc_timeout");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #5:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - 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.
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
|
|
|
onchain_failed_our_htlc(channel, &htlc, "timed out");
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:48:39 +02:00
|
|
|
static void handle_irrevocably_resolved(struct channel *channel, const u8 *msg UNUSED)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
|
|
|
/* 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. */
|
2018-02-21 16:50:49 +01:00
|
|
|
delete_channel(channel);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onchain_add_utxo -- onchaind is telling us about an UTXO we own
|
|
|
|
*/
|
|
|
|
static void onchain_add_utxo(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
2020-04-02 04:01:05 +02:00
|
|
|
struct chain_coin_mvt *mvt;
|
2018-03-22 00:18:53 +01:00
|
|
|
u32 blockheight;
|
2020-07-06 07:28:43 +02:00
|
|
|
struct bitcoin_txid txid;
|
2021-07-02 21:54:40 +02:00
|
|
|
u32 outnum, csv_lock;
|
2020-07-06 07:28:43 +02:00
|
|
|
struct amount_sat amount;
|
|
|
|
struct pubkey *commitment_point;
|
|
|
|
u8 *scriptPubkey;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_add_utxo(
|
2020-07-06 07:28:43 +02:00
|
|
|
tmpctx, msg, &txid, &outnum, &commitment_point,
|
2021-07-02 21:54:40 +02:00
|
|
|
&amount, &blockheight, &scriptPubkey,
|
|
|
|
&csv_lock)) {
|
2020-07-06 07:28:43 +02:00
|
|
|
log_broken(channel->log,
|
|
|
|
"onchaind gave invalid add_utxo message: %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 07:28:43 +02:00
|
|
|
assert(blockheight);
|
|
|
|
outpointfilter_add(channel->peer->ld->wallet->owned_outpoints,
|
|
|
|
&txid, outnum);
|
2021-07-02 21:54:40 +02:00
|
|
|
log_debug(channel->log, "adding utxo to watch %s:%u, csv %u",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &txid),
|
|
|
|
outnum, csv_lock);
|
|
|
|
|
2020-07-06 07:28:43 +02:00
|
|
|
wallet_add_onchaind_utxo(channel->peer->ld->wallet,
|
|
|
|
&txid, outnum, scriptPubkey,
|
|
|
|
blockheight, amount, channel,
|
2021-07-02 21:54:40 +02:00
|
|
|
commitment_point,
|
|
|
|
csv_lock);
|
2020-04-02 04:01:05 +02:00
|
|
|
|
2020-07-06 07:28:43 +02:00
|
|
|
mvt = new_coin_deposit_sat(msg, "wallet", &txid,
|
|
|
|
outnum, blockheight, amount);
|
2020-04-02 04:01:05 +02:00
|
|
|
notify_chain_mvt(channel->peer->ld, mvt);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 12:27:48 +02:00
|
|
|
static void onchain_annotate_txout(struct channel *channel, const u8 *msg)
|
2019-05-27 13:06:02 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_txid txid;
|
2019-06-07 11:38:20 +02:00
|
|
|
enum wallet_tx_type type;
|
2019-10-04 12:27:48 +02:00
|
|
|
u32 outnum;
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_annotate_txout(msg, &txid, &outnum, &type))
|
2019-10-04 12:27:48 +02:00
|
|
|
fatal("onchaind gave invalid onchain_annotate_txout "
|
2019-05-27 13:06:02 +02:00
|
|
|
"message: %s",
|
|
|
|
tal_hex(msg, msg));
|
2019-10-04 12:27:48 +02:00
|
|
|
wallet_annotate_txout(channel->peer->ld->wallet, &txid, outnum, type,
|
|
|
|
channel->dbid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onchain_annotate_txin(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_txid txid;
|
|
|
|
enum wallet_tx_type type;
|
|
|
|
u32 innum;
|
2020-08-25 04:15:48 +02:00
|
|
|
if (!fromwire_onchaind_annotate_txin(msg, &txid, &innum, &type))
|
2019-10-04 12:27:48 +02:00
|
|
|
fatal("onchaind gave invalid onchain_annotate_txin "
|
|
|
|
"message: %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
wallet_annotate_txin(channel->peer->ld->wallet, &txid, innum, type,
|
2019-05-27 13:06:02 +02:00
|
|
|
channel->dbid);
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:40:50 +01:00
|
|
|
static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds UNUSED)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2020-08-25 04:15:48 +02:00
|
|
|
enum onchaind_wire t = fromwire_peektype(msg);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
switch (t) {
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_INIT_REPLY:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_onchain_init_reply(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_BROADCAST_TX:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_onchain_broadcast_tx(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_UNWATCH_TX:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_onchain_unwatch_tx(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_EXTRACTED_PREIMAGE:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_extracted_preimage(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_MISSING_HTLC_OUTPUT:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_missing_htlc_output(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_HTLC_TIMEOUT:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_onchain_htlc_timeout(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_ALL_IRREVOCABLY_RESOLVED:
|
2018-02-20 21:59:09 +01:00
|
|
|
handle_irrevocably_resolved(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_ADD_UTXO:
|
2018-02-20 21:59:09 +01:00
|
|
|
onchain_add_utxo(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_ANNOTATE_TXIN:
|
2019-10-04 12:27:48 +02:00
|
|
|
onchain_annotate_txin(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_ANNOTATE_TXOUT:
|
2019-10-04 12:27:48 +02:00
|
|
|
onchain_annotate_txout(sd->channel, msg);
|
2019-05-27 13:06:02 +02:00
|
|
|
break;
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_NOTIFY_COIN_MVT:
|
2020-04-02 04:16:32 +02:00
|
|
|
handle_onchain_log_coin_move(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* We send these, not receive them */
|
2020-08-25 04:15:48 +02:00
|
|
|
case WIRE_ONCHAIND_INIT:
|
|
|
|
case WIRE_ONCHAIND_SPENT:
|
|
|
|
case WIRE_ONCHAIND_DEPTH:
|
|
|
|
case WIRE_ONCHAIND_HTLC:
|
|
|
|
case WIRE_ONCHAIND_KNOWN_PREIMAGE:
|
|
|
|
case WIRE_ONCHAIND_DEV_MEMLEAK:
|
|
|
|
case WIRE_ONCHAIND_DEV_MEMLEAK_REPLY:
|
2018-02-20 21:59:09 +01:00
|
|
|
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;
|
|
|
|
|
2018-10-23 11:40:22 +02:00
|
|
|
/* 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);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!hout)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* BOLT #5:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - 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.
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
|
|
|
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,
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps UNUSED,
|
2018-03-05 17:40:50 +01:00
|
|
|
const struct channel_id *channel_id UNUSED,
|
2018-02-20 21:59:09 +01:00
|
|
|
const char *desc,
|
2021-02-02 13:47:01 +01:00
|
|
|
bool warning UNUSED,
|
2018-03-05 17:40:50 +01:00
|
|
|
const u8 *err_for_them UNUSED)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
|
|
|
/* FIXME: re-launch? */
|
|
|
|
log_broken(channel->log, "%s", desc);
|
2018-03-03 19:59:07 +01:00
|
|
|
channel_set_billboard(channel, true, desc);
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* With a reorg, this can get called multiple times; each time we'll kill
|
|
|
|
* onchaind (like any other owner), and restart */
|
2018-04-16 13:20:45 +02:00
|
|
|
enum watch_result onchaind_funding_spent(struct channel *channel,
|
|
|
|
const struct bitcoin_tx *tx,
|
2020-04-03 03:46:18 +02:00
|
|
|
u32 blockheight,
|
|
|
|
bool is_replay)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2018-03-07 01:06:07 +01:00
|
|
|
u8 *msg;
|
2020-05-26 05:39:40 +02:00
|
|
|
struct bitcoin_txid our_last_txid;
|
2018-02-20 21:59:09 +01:00
|
|
|
struct htlc_stub *stubs;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2018-03-07 01:06:07 +01:00
|
|
|
struct pubkey final_key;
|
2018-07-23 04:23:02 +02:00
|
|
|
int hsmfd;
|
2020-03-10 17:17:10 +01:00
|
|
|
u32 feerates[3];
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
enum state_change reason;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
/* use REASON_ONCHAIN or closer's reason, if known */
|
|
|
|
reason = REASON_ONCHAIN;
|
|
|
|
if (channel->closer != NUM_SIDES)
|
|
|
|
reason = REASON_UNKNOWN; /* will use last cause as reason */
|
|
|
|
|
|
|
|
channel_fail_permanent(channel, reason, "Funding transaction spent");
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* We could come from almost any state. */
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
/* NOTE(mschmoock) above comment is wrong, since we failed above! */
|
|
|
|
channel_set_state(channel,
|
|
|
|
channel->state,
|
|
|
|
FUNDING_SPEND_SEEN,
|
|
|
|
reason,
|
|
|
|
"Onchain funding spend");
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
hsmfd = hsm_get_client_fd(ld, &channel->peer->id,
|
|
|
|
channel->dbid,
|
2018-07-23 04:23:03 +02:00
|
|
|
HSM_CAP_SIGN_ONCHAIN_TX
|
|
|
|
| HSM_CAP_COMMITMENT_POINT);
|
2018-07-23 04:23:02 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_set_owner(channel, new_channel_subd(ld,
|
|
|
|
"lightning_onchaind",
|
2021-01-20 02:51:15 +01:00
|
|
|
channel,
|
2020-12-01 21:49:35 +01:00
|
|
|
&channel->peer->id,
|
2018-04-26 06:51:01 +02:00
|
|
|
channel->log, false,
|
2020-08-25 04:15:48 +02:00
|
|
|
onchaind_wire_name,
|
2018-02-20 21:59:09 +01:00
|
|
|
onchain_msg,
|
|
|
|
onchain_error,
|
2018-02-23 06:53:47 +01:00
|
|
|
channel_set_billboard,
|
2018-07-23 04:23:02 +02:00
|
|
|
take(&hsmfd),
|
2019-07-25 04:47:34 +02:00
|
|
|
NULL));
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
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;
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
/* This could be a mutual close, but it doesn't matter. */
|
|
|
|
bitcoin_txid(channel->last_tx, &our_last_txid);
|
|
|
|
|
2020-03-10 17:17:10 +01:00
|
|
|
/* We try to get the feerate for each transaction type, 0 if estimation
|
|
|
|
* failed. */
|
|
|
|
feerates[0] = delayed_to_us_feerate(ld->topology);
|
|
|
|
feerates[1] = htlc_resolution_feerate(ld->topology);
|
|
|
|
feerates[2] = penalty_feerate(ld->topology);
|
|
|
|
/* We check them separately but there is a high chance that if estimation
|
|
|
|
* failed for one, it failed for all.. */
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
if (!feerates[i]) {
|
|
|
|
/* We have at least one data point: the last tx's feerate. */
|
|
|
|
struct amount_sat fee = channel->funding;
|
|
|
|
for (size_t i = 0;
|
|
|
|
i < channel->last_tx->wtx->num_outputs; i++) {
|
|
|
|
struct amount_asset asset =
|
|
|
|
bitcoin_tx_output_get_amount(channel->last_tx, i);
|
|
|
|
struct amount_sat amt;
|
|
|
|
assert(amount_asset_is_main(&asset));
|
|
|
|
amt = amount_asset_to_sat(&asset);
|
|
|
|
if (!amount_sat_sub(&fee, fee, amt)) {
|
|
|
|
log_broken(channel->log, "Could not get fee"
|
|
|
|
" funding %s tx %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_sat,
|
|
|
|
&channel->funding),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct bitcoin_tx,
|
|
|
|
channel->last_tx));
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:17:10 +01:00
|
|
|
feerates[i] = fee.satoshis / bitcoin_tx_weight(tx); /* Raw: reverse feerate extraction */
|
|
|
|
if (feerates[i] < feerate_floor())
|
|
|
|
feerates[i] = feerate_floor();
|
|
|
|
}
|
2018-08-23 01:27:25 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 07:13:47 +02:00
|
|
|
log_debug(channel->log, "channel->static_remotekey_start[LOCAL] %"PRIu64,
|
|
|
|
channel->static_remotekey_start[LOCAL]);
|
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
msg = towire_onchaind_init(channel,
|
2018-07-23 04:23:03 +02:00
|
|
|
&channel->their_shachain.chain,
|
2019-09-25 22:38:45 +02:00
|
|
|
chainparams,
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->funding,
|
2020-04-02 04:21:22 +02:00
|
|
|
channel->our_msat,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->channel_info.old_remote_per_commit,
|
|
|
|
&channel->channel_info.remote_per_commit,
|
|
|
|
/* BOLT #2:
|
|
|
|
* `to_self_delay` is the number of blocks
|
2018-06-17 12:13:44 +02:00
|
|
|
* that the other node's to-self outputs
|
2018-02-20 21:59:09 +01:00
|
|
|
* 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,
|
2020-03-10 17:17:10 +01:00
|
|
|
/* delayed_to_us, htlc, and penalty. */
|
|
|
|
feerates[0], feerates[1], feerates[2],
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->our_config.dust_limit,
|
2018-02-20 21:59:09 +01:00
|
|
|
&our_last_txid,
|
2019-09-29 10:53:26 +02:00
|
|
|
channel->shutdown_scriptpubkey[LOCAL],
|
2019-09-29 09:35:45 +02:00
|
|
|
channel->shutdown_scriptpubkey[REMOTE],
|
2018-03-07 01:06:07 +01:00
|
|
|
&final_key,
|
2019-09-09 18:11:24 +02:00
|
|
|
channel->opener,
|
2018-07-23 04:23:03 +02:00
|
|
|
&channel->local_basepoints,
|
2018-07-09 13:17:59 +02:00
|
|
|
&channel->channel_info.theirbase,
|
2020-05-26 05:39:40 +02:00
|
|
|
tx_parts_from_wally_tx(tmpctx, tx->wtx, -1, -1),
|
|
|
|
tx->wtx->locktime,
|
2018-04-16 13:20:45 +02:00
|
|
|
blockheight,
|
2018-02-20 21:59:09 +01:00
|
|
|
/* FIXME: config for 'reasonable depth' */
|
|
|
|
3,
|
|
|
|
channel->last_htlc_sigs,
|
2018-04-03 06:31:48 +02:00
|
|
|
tal_count(stubs),
|
2018-04-03 09:19:39 +02:00
|
|
|
channel->min_possible_feerate,
|
2018-08-17 07:06:36 +02:00
|
|
|
channel->max_possible_feerate,
|
2019-09-10 04:23:27 +02:00
|
|
|
channel->future_per_commitment_point,
|
2020-08-14 03:30:42 +02:00
|
|
|
&channel->local_funding_pubkey,
|
|
|
|
&channel->channel_info.remote_fundingkey,
|
2021-06-04 07:13:47 +02:00
|
|
|
channel->static_remotekey_start[LOCAL],
|
|
|
|
channel->static_remotekey_start[REMOTE],
|
2021-09-09 07:29:35 +02:00
|
|
|
channel_has(channel, OPT_ANCHOR_OUTPUTS),
|
2020-09-08 05:20:02 +02:00
|
|
|
is_replay,
|
|
|
|
feerate_min(ld, NULL));
|
2018-02-20 21:59:09 +01:00
|
|
|
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);
|
2020-08-25 04:15:48 +02:00
|
|
|
msg = towire_onchaind_htlc(channel, &stubs[i],
|
2018-02-20 21:59:09 +01:00
|
|
|
tell, tell_immediate);
|
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:48:39 +02:00
|
|
|
/* Tell it about any preimages we know. */
|
|
|
|
onchaind_tell_fulfill(channel);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
watch_tx_and_outputs(channel, tx);
|
|
|
|
|
|
|
|
/* We keep watching until peer finally deleted, for reorgs. */
|
|
|
|
return KEEP_WATCHING;
|
|
|
|
}
|
2018-04-17 15:31:30 +02:00
|
|
|
|
|
|
|
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++) {
|
2020-08-25 04:15:48 +02:00
|
|
|
if (txs[j].type == WIRE_ONCHAIND_INIT) {
|
2018-04-17 15:31:30 +02:00
|
|
|
onchaind_funding_spent(chan, txs[j].tx,
|
2020-04-03 03:46:18 +02:00
|
|
|
txs[j].blockheight,
|
|
|
|
true);
|
2018-04-17 15:31:30 +02:00
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
} else if (txs[j].type == WIRE_ONCHAIND_SPENT) {
|
2018-04-17 15:31:30 +02:00
|
|
|
onchain_txo_spent(chan, txs[j].tx,
|
|
|
|
txs[j].input_num,
|
2020-04-03 03:46:18 +02:00
|
|
|
txs[j].blockheight,
|
|
|
|
true);
|
2018-04-17 15:31:30 +02:00
|
|
|
|
2020-08-25 04:15:48 +02:00
|
|
|
} else if (txs[j].type == WIRE_ONCHAIND_DEPTH) {
|
2018-04-17 15:31:30 +02:00
|
|
|
onchain_tx_depth(chan, &txs[j].txid,
|
2020-04-03 03:46:18 +02:00
|
|
|
txs[j].depth, true);
|
2018-04-17 15:31:30 +02:00
|
|
|
|
|
|
|
} 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);
|
|
|
|
}
|