2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include <bitcoin/feerate.h>
|
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <bitcoin/tx.h>
|
2016-04-24 12:12:18 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2016-08-09 05:41:22 +02:00
|
|
|
#include <ccan/io/io.h>
|
2016-11-07 13:29:02 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
#include <common/configdir.h>
|
2020-08-13 19:44:02 +02:00
|
|
|
#include <common/htlc_tx.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
2022-07-04 05:49:38 +02:00
|
|
|
#include <common/json_param.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/timeout.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/type_to_string.h>
|
2022-01-03 19:45:35 +01:00
|
|
|
#include <db/exec.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <lightningd/bitcoind.h>
|
|
|
|
#include <lightningd/chaintopology.h>
|
|
|
|
#include <lightningd/channel.h>
|
2020-04-03 23:56:57 +02:00
|
|
|
#include <lightningd/coin_mvts.h>
|
2018-03-28 12:03:40 +02:00
|
|
|
#include <lightningd/gossip_control.h>
|
2019-05-30 04:30:10 +02:00
|
|
|
#include <lightningd/io_loop_with_timers.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <lightningd/jsonrpc.h>
|
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/log.h>
|
2022-09-09 14:48:31 +02:00
|
|
|
#include <lightningd/notification.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <wallet/txfilter.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
/* Mutual recursion via timer. */
|
|
|
|
static void try_extend_tip(struct chain_topology *topo);
|
2016-05-09 22:56:09 +02:00
|
|
|
|
2021-12-10 16:46:42 +01:00
|
|
|
static bool first_update_complete = false;
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
/* init_topo sets topo->root, start_fee_estimate clears
|
2018-08-22 04:33:32 +02:00
|
|
|
* feerate_uninitialized (even if unsuccessful) */
|
|
|
|
static void maybe_completed_init(struct chain_topology *topo)
|
|
|
|
{
|
|
|
|
if (topo->feerate_uninitialized)
|
|
|
|
return;
|
|
|
|
if (!topo->root)
|
|
|
|
return;
|
2022-06-26 06:51:01 +02:00
|
|
|
log_debug(topo->ld->log, "io_break: %s", __func__);
|
2018-08-22 04:33:32 +02:00
|
|
|
io_break(topo);
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void next_topology_timer(struct chain_topology *topo)
|
2016-05-09 22:56:09 +02:00
|
|
|
{
|
2021-09-01 06:35:15 +02:00
|
|
|
assert(!topo->extend_timer);
|
|
|
|
topo->extend_timer = new_reltimer(topo->ld->timers, topo,
|
|
|
|
time_from_sec(topo->poll_seconds),
|
|
|
|
try_extend_tip, topo);
|
2016-05-09 22:56:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static bool we_broadcast(const struct chain_topology *topo,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *txid)
|
2016-05-04 08:41:16 +02:00
|
|
|
{
|
2023-01-03 05:46:42 +01:00
|
|
|
return outgoing_tx_map_get(topo->outgoing_txs, txid) != NULL;
|
2016-05-04 08:41:16 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
static void filter_block_txs(struct chain_topology *topo, struct block *b)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
size_t i;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat owned;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
/* Now we see if any of those txs are interesting. */
|
|
|
|
for (i = 0; i < tal_count(b->full_txs); i++) {
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct bitcoin_tx *tx = b->full_txs[i];
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid txid;
|
2016-04-24 12:18:35 +02:00
|
|
|
size_t j;
|
2022-11-08 15:42:38 +01:00
|
|
|
bool is_coinbase = i == 0;
|
2016-04-24 12:18:35 +02:00
|
|
|
|
|
|
|
/* Tell them if it spends a txo we care about. */
|
2019-03-25 11:35:56 +01:00
|
|
|
for (j = 0; j < tx->wtx->num_inputs; j++) {
|
2021-10-13 05:45:36 +02:00
|
|
|
struct bitcoin_outpoint out;
|
2016-04-24 12:18:35 +02:00
|
|
|
struct txowatch *txo;
|
2019-03-25 11:35:56 +01:00
|
|
|
bitcoin_tx_input_get_txid(tx, j, &out.txid);
|
2021-10-13 05:45:36 +02:00
|
|
|
out.n = tx->wtx->inputs[j].index;
|
2016-04-24 12:18:35 +02:00
|
|
|
|
2023-01-03 05:46:42 +01:00
|
|
|
txo = txowatch_hash_get(topo->txowatches, &out);
|
2018-04-17 15:20:27 +02:00
|
|
|
if (txo) {
|
2018-09-03 02:44:27 +02:00
|
|
|
wallet_transaction_add(topo->ld->wallet,
|
2020-08-07 03:30:47 +02:00
|
|
|
tx->wtx, b->height, i);
|
2018-02-21 16:57:45 +01:00
|
|
|
txowatch_fire(txo, tx, j, b);
|
2018-04-17 15:20:27 +02:00
|
|
|
}
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
owned = AMOUNT_SAT(0);
|
2020-08-26 13:31:41 +02:00
|
|
|
txid = b->txids[i];
|
2017-11-27 16:20:10 +01:00
|
|
|
if (txfilter_match(topo->bitcoind->ld->owned_txfilter, tx)) {
|
|
|
|
wallet_extract_owned_outputs(topo->bitcoind->ld->wallet,
|
2022-11-08 15:42:38 +01:00
|
|
|
tx->wtx, is_coinbase, &b->height, &owned);
|
2020-08-07 03:30:47 +02:00
|
|
|
wallet_transaction_add(topo->ld->wallet, tx->wtx,
|
|
|
|
b->height, i);
|
2017-11-27 16:20:10 +01:00
|
|
|
}
|
2017-11-25 13:31:20 +01:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
/* We did spends first, in case that tells us to watch tx. */
|
2019-05-23 23:01:00 +02:00
|
|
|
if (watching_txid(topo, &txid) || we_broadcast(topo, &txid)) {
|
2018-09-03 02:44:27 +02:00
|
|
|
wallet_transaction_add(topo->ld->wallet,
|
2020-08-07 03:30:47 +02:00
|
|
|
tx->wtx, b->height, i);
|
2018-04-09 14:37:21 +02:00
|
|
|
}
|
2019-06-28 03:58:31 +02:00
|
|
|
|
|
|
|
txwatch_inform(topo, &txid, tx);
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
b->full_txs = tal_free(b->full_txs);
|
2020-09-23 03:37:04 +02:00
|
|
|
b->txids = tal_free(b->txids);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
size_t get_tx_depth(const struct chain_topology *topo,
|
2018-04-09 17:49:03 +02:00
|
|
|
const struct bitcoin_txid *txid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2018-09-03 02:44:27 +02:00
|
|
|
u32 blockheight = wallet_transaction_height(topo->ld->wallet, txid);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2018-04-09 17:49:03 +02:00
|
|
|
if (blockheight == 0)
|
2016-05-04 08:36:19 +02:00
|
|
|
return 0;
|
2018-04-09 17:49:03 +02:00
|
|
|
return topo->tip->height - blockheight + 1;
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
struct txs_to_broadcast {
|
|
|
|
/* We just sent txs[cursor] */
|
|
|
|
size_t cursor;
|
|
|
|
/* These are hex encoded already, for bitcoind_sendrawtx */
|
|
|
|
const char **txs;
|
2016-11-09 07:44:21 +01:00
|
|
|
|
2022-09-12 23:19:11 +02:00
|
|
|
/* IDs to attach to each tx (could be NULL!) */
|
|
|
|
const char **cmd_id;
|
2016-11-09 07:44:10 +01:00
|
|
|
};
|
|
|
|
|
2016-11-07 13:34:02 +01:00
|
|
|
/* We just sent the last entry in txs[]. Shrink and send the next last. */
|
2017-03-02 13:21:49 +01:00
|
|
|
static void broadcast_remainder(struct bitcoind *bitcoind,
|
2020-01-09 12:25:45 +01:00
|
|
|
bool success, const char *msg,
|
2016-11-09 07:44:10 +01:00
|
|
|
struct txs_to_broadcast *txs)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
2021-01-06 07:02:04 +01:00
|
|
|
if (!success)
|
2017-03-02 13:21:49 +01:00
|
|
|
log_debug(bitcoind->log,
|
2016-05-04 08:33:10 +02:00
|
|
|
"Expected error broadcasting tx %s: %s",
|
2016-11-09 07:44:10 +01:00
|
|
|
txs->txs[txs->cursor], msg);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
txs->cursor++;
|
|
|
|
if (txs->cursor == tal_count(txs->txs)) {
|
2016-05-04 08:33:10 +02:00
|
|
|
tal_free(txs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:34:02 +01:00
|
|
|
/* Broadcast next one. */
|
2022-09-12 23:19:11 +02:00
|
|
|
bitcoind_sendrawtx(bitcoind,
|
|
|
|
txs->cmd_id[txs->cursor], txs->txs[txs->cursor],
|
|
|
|
false,
|
2016-11-09 07:44:10 +01:00
|
|
|
broadcast_remainder, txs);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: This is dumb. We can group txs and avoid bothering bitcoind
|
|
|
|
* if any one tx is in the main chain. */
|
2022-09-12 23:19:11 +02:00
|
|
|
static void rebroadcast_txs(struct chain_topology *topo)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
|
|
|
/* Copy txs now (peers may go away, and they own txs). */
|
2016-11-09 07:44:21 +01:00
|
|
|
struct txs_to_broadcast *txs;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct outgoing_tx *otx;
|
2022-11-15 15:23:21 +01:00
|
|
|
struct outgoing_tx_map_iter it;
|
2023-03-23 06:45:52 +01:00
|
|
|
tal_t *cleanup_ctx = tal(NULL, char);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
txs = tal(topo, struct txs_to_broadcast);
|
2022-09-12 23:19:11 +02:00
|
|
|
txs->cmd_id = tal_arr(txs, const char *, 0);
|
2016-11-09 07:44:21 +01:00
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
/* Put any txs we want to broadcast in ->txs. */
|
|
|
|
txs->txs = tal_arr(txs, const char *, 0);
|
2022-11-15 15:23:21 +01:00
|
|
|
|
2023-01-03 05:46:42 +01:00
|
|
|
for (otx = outgoing_tx_map_first(topo->outgoing_txs, &it); otx;
|
|
|
|
otx = outgoing_tx_map_next(topo->outgoing_txs, &it)) {
|
2018-09-03 02:44:27 +02:00
|
|
|
if (wallet_transaction_height(topo->ld->wallet, &otx->txid))
|
2017-03-02 13:21:49 +01:00
|
|
|
continue;
|
2016-05-04 08:33:10 +02:00
|
|
|
|
chaintopology: allow minblock for broadcast_tx.
Fun story. We're changing onchaind to hand txs to us, and we will
construct them and do the broadcast for it. lightningd tells onchaind
the witness it used (with flags to indicate which fields were
signatures so should be ignored) so onchaind can recognize the tx
when/if it is mined.
And when onchaind was waiting for a CLTV delay, it wouldn't tell
lightningd yet, but wait until the parent was sufficiently deep
But this caused bugs!
In particular, on replay, onchaind would see transactions which it
hasn't sent yet. This was not a problem before, as onchaind had
created the tx, even if it hadn't told lightningd to broadcast it, so
recognized the variant when it came in. When we're relying on
lightningd to tell us what the tx will look like, this doesn't work
any more.
The cause of this is that we fire off txowatches ("this output was
spent!") while we process blocks, and only fire off txwatches ("this
tx increased depth") once all the current blocks are processed. Often
this didn't matter, since we replay messages to onchaind from the
database, *but* we trim the last few blocks on restart (or, if there's
a small reorg while we're stopped), and we can hit this misordering.
Changing our topology code to only ever process one block at a time
would be a solution, but slows down catchup (and tests, where we often
mine a run of blocks).
So, this seems like a premature optimization, but it's really
required! And in future, lightningd can use this knowledge of pending
transactions to combine them in more clever ways.
Note that if a tx is valid at block N, we broadcast it once we see
block N-1, to get it in the mempool for block N.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-03-23 06:47:52 +01:00
|
|
|
/* Don't send ones which aren't ready yet. Note that if the
|
|
|
|
* minimum block is N, we broadcast it when we have block N-1! */
|
|
|
|
if (get_block_height(topo) + 1 < otx->minblock)
|
|
|
|
continue;
|
|
|
|
|
2023-03-23 06:45:52 +01:00
|
|
|
/* Don't free from txmap inside loop! */
|
|
|
|
if (otx->refresh
|
|
|
|
&& !otx->refresh(otx->channel, &otx->tx, otx->refresh_arg)) {
|
|
|
|
tal_steal(cleanup_ctx, otx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:44:52 +01:00
|
|
|
tal_arr_expand(&txs->txs, fmt_bitcoin_tx(txs->txs, otx->tx));
|
2022-09-12 23:19:11 +02:00
|
|
|
tal_arr_expand(&txs->cmd_id,
|
|
|
|
otx->cmd_id ? tal_strdup(txs, otx->cmd_id) : NULL);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
2023-03-23 06:45:52 +01:00
|
|
|
tal_free(cleanup_ctx);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
/* Let this do the dirty work. */
|
|
|
|
txs->cursor = (size_t)-1;
|
2020-01-09 12:25:45 +01:00
|
|
|
broadcast_remainder(topo->bitcoind, true, "", txs);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 15:23:21 +01:00
|
|
|
static void destroy_outgoing_tx(struct outgoing_tx *otx, struct chain_topology *topo)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
2023-01-03 05:46:42 +01:00
|
|
|
outgoing_tx_map_del(topo->outgoing_txs, otx);
|
2016-11-07 13:29:02 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static void clear_otx_channel(struct channel *channel, struct outgoing_tx *otx)
|
2017-03-07 01:55:48 +01:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
if (otx->channel != channel)
|
|
|
|
fatal("channel %p, otx %p has channel %p", channel, otx, otx->channel);
|
|
|
|
otx->channel = NULL;
|
2017-03-07 01:55:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void broadcast_done(struct bitcoind *bitcoind,
|
2020-01-09 12:25:45 +01:00
|
|
|
bool success, const char *msg,
|
2016-11-07 13:30:02 +01:00
|
|
|
struct outgoing_tx *otx)
|
2016-11-07 13:29:02 +01:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
/* Channel gone? Stop. */
|
|
|
|
if (!otx->channel) {
|
2017-03-07 01:55:48 +01:00
|
|
|
tal_free(otx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
/* No longer needs to be disconnected if channel dies. */
|
|
|
|
tal_del_destructor2(otx->channel, clear_otx_channel, otx);
|
2018-01-09 11:16:21 +01:00
|
|
|
|
2023-03-23 06:42:52 +01:00
|
|
|
if (otx->finished) {
|
|
|
|
otx->finished(otx->channel, success, msg);
|
2016-11-07 13:31:02 +01:00
|
|
|
tal_free(otx);
|
2022-11-15 14:19:48 +01:00
|
|
|
} else if (we_broadcast(bitcoind->ld->topology, &otx->txid)) {
|
|
|
|
log_debug(
|
|
|
|
bitcoind->ld->topology->log,
|
|
|
|
"Not adding %s to list of outgoing transactions, already "
|
|
|
|
"present",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &otx->txid));
|
|
|
|
tal_free(otx);
|
2016-11-07 13:31:02 +01:00
|
|
|
} else {
|
2018-02-12 11:13:04 +01:00
|
|
|
/* For continual rebroadcasting, until channel freed. */
|
|
|
|
tal_steal(otx->channel, otx);
|
2023-03-23 06:46:52 +01:00
|
|
|
outgoing_tx_map_add(bitcoind->ld->topology->outgoing_txs, otx);
|
2022-11-15 15:23:21 +01:00
|
|
|
tal_add_destructor2(otx, destroy_outgoing_tx, bitcoind->ld->topology);
|
2016-11-07 13:31:02 +01:00
|
|
|
}
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 06:45:52 +01:00
|
|
|
void broadcast_tx_(struct chain_topology *topo,
|
|
|
|
struct channel *channel, const struct bitcoin_tx *tx,
|
chaintopology: allow minblock for broadcast_tx.
Fun story. We're changing onchaind to hand txs to us, and we will
construct them and do the broadcast for it. lightningd tells onchaind
the witness it used (with flags to indicate which fields were
signatures so should be ignored) so onchaind can recognize the tx
when/if it is mined.
And when onchaind was waiting for a CLTV delay, it wouldn't tell
lightningd yet, but wait until the parent was sufficiently deep
But this caused bugs!
In particular, on replay, onchaind would see transactions which it
hasn't sent yet. This was not a problem before, as onchaind had
created the tx, even if it hadn't told lightningd to broadcast it, so
recognized the variant when it came in. When we're relying on
lightningd to tell us what the tx will look like, this doesn't work
any more.
The cause of this is that we fire off txowatches ("this output was
spent!") while we process blocks, and only fire off txwatches ("this
tx increased depth") once all the current blocks are processed. Often
this didn't matter, since we replay messages to onchaind from the
database, *but* we trim the last few blocks on restart (or, if there's
a small reorg while we're stopped), and we can hit this misordering.
Changing our topology code to only ever process one block at a time
would be a solution, but slows down catchup (and tests, where we often
mine a run of blocks).
So, this seems like a premature optimization, but it's really
required! And in future, lightningd can use this knowledge of pending
transactions to combine them in more clever ways.
Note that if a tx is valid at block N, we broadcast it once we see
block N-1, to get it in the mempool for block N.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-03-23 06:47:52 +01:00
|
|
|
const char *cmd_id, bool allowhighfees, u32 minblock,
|
2023-03-23 06:45:52 +01:00
|
|
|
void (*finished)(struct channel *channel,
|
|
|
|
bool success,
|
|
|
|
const char *err),
|
|
|
|
bool (*refresh)(struct channel *channel,
|
|
|
|
const struct bitcoin_tx **tx,
|
|
|
|
void *arg),
|
|
|
|
void *refresh_arg)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
/* Channel might vanish: topo owns it to start with. */
|
2017-03-07 01:55:48 +01:00
|
|
|
struct outgoing_tx *otx = tal(topo, struct outgoing_tx);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
otx->channel = channel;
|
2016-11-07 13:29:02 +01:00
|
|
|
bitcoin_txid(tx, &otx->txid);
|
2023-03-23 06:44:52 +01:00
|
|
|
otx->tx = clone_bitcoin_tx(otx, tx);
|
chaintopology: allow minblock for broadcast_tx.
Fun story. We're changing onchaind to hand txs to us, and we will
construct them and do the broadcast for it. lightningd tells onchaind
the witness it used (with flags to indicate which fields were
signatures so should be ignored) so onchaind can recognize the tx
when/if it is mined.
And when onchaind was waiting for a CLTV delay, it wouldn't tell
lightningd yet, but wait until the parent was sufficiently deep
But this caused bugs!
In particular, on replay, onchaind would see transactions which it
hasn't sent yet. This was not a problem before, as onchaind had
created the tx, even if it hadn't told lightningd to broadcast it, so
recognized the variant when it came in. When we're relying on
lightningd to tell us what the tx will look like, this doesn't work
any more.
The cause of this is that we fire off txowatches ("this output was
spent!") while we process blocks, and only fire off txwatches ("this
tx increased depth") once all the current blocks are processed. Often
this didn't matter, since we replay messages to onchaind from the
database, *but* we trim the last few blocks on restart (or, if there's
a small reorg while we're stopped), and we can hit this misordering.
Changing our topology code to only ever process one block at a time
would be a solution, but slows down catchup (and tests, where we often
mine a run of blocks).
So, this seems like a premature optimization, but it's really
required! And in future, lightningd can use this knowledge of pending
transactions to combine them in more clever ways.
Note that if a tx is valid at block N, we broadcast it once we see
block N-1, to get it in the mempool for block N.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-03-23 06:47:52 +01:00
|
|
|
otx->minblock = minblock;
|
2023-03-23 06:42:52 +01:00
|
|
|
otx->finished = finished;
|
2023-03-23 06:45:52 +01:00
|
|
|
otx->refresh = refresh;
|
|
|
|
otx->refresh_arg = refresh_arg;
|
|
|
|
if (taken(otx->refresh_arg))
|
|
|
|
tal_steal(otx, otx->refresh_arg);
|
2022-09-12 23:19:11 +02:00
|
|
|
if (cmd_id)
|
|
|
|
otx->cmd_id = tal_strdup(otx, cmd_id);
|
|
|
|
else
|
|
|
|
otx->cmd_id = NULL;
|
2016-05-04 08:33:10 +02:00
|
|
|
|
chaintopology: allow minblock for broadcast_tx.
Fun story. We're changing onchaind to hand txs to us, and we will
construct them and do the broadcast for it. lightningd tells onchaind
the witness it used (with flags to indicate which fields were
signatures so should be ignored) so onchaind can recognize the tx
when/if it is mined.
And when onchaind was waiting for a CLTV delay, it wouldn't tell
lightningd yet, but wait until the parent was sufficiently deep
But this caused bugs!
In particular, on replay, onchaind would see transactions which it
hasn't sent yet. This was not a problem before, as onchaind had
created the tx, even if it hadn't told lightningd to broadcast it, so
recognized the variant when it came in. When we're relying on
lightningd to tell us what the tx will look like, this doesn't work
any more.
The cause of this is that we fire off txowatches ("this output was
spent!") while we process blocks, and only fire off txwatches ("this
tx increased depth") once all the current blocks are processed. Often
this didn't matter, since we replay messages to onchaind from the
database, *but* we trim the last few blocks on restart (or, if there's
a small reorg while we're stopped), and we can hit this misordering.
Changing our topology code to only ever process one block at a time
would be a solution, but slows down catchup (and tests, where we often
mine a run of blocks).
So, this seems like a premature optimization, but it's really
required! And in future, lightningd can use this knowledge of pending
transactions to combine them in more clever ways.
Note that if a tx is valid at block N, we broadcast it once we see
block N-1, to get it in the mempool for block N.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-03-23 06:47:52 +01:00
|
|
|
/* Note that if the minimum block is N, we broadcast it when
|
|
|
|
* we have block N-1! */
|
|
|
|
if (get_block_height(topo) + 1 < otx->minblock) {
|
|
|
|
log_debug(topo->log, "Deferring broadcast of txid %s until block %u",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &otx->txid),
|
|
|
|
otx->minblock - 1);
|
|
|
|
|
|
|
|
/* For continual rebroadcasting, until channel freed. */
|
|
|
|
tal_steal(otx->channel, otx);
|
|
|
|
outgoing_tx_map_add(topo->outgoing_txs, otx);
|
|
|
|
tal_add_destructor2(otx, destroy_outgoing_tx, topo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_add_destructor2(channel, clear_otx_channel, otx);
|
2022-09-12 23:19:11 +02:00
|
|
|
log_debug(topo->log, "Broadcasting txid %s%s%s",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &otx->txid),
|
|
|
|
cmd_id ? " for " : "", cmd_id ? cmd_id : "");
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2020-08-07 03:30:47 +02:00
|
|
|
wallet_transaction_add(topo->ld->wallet, tx->wtx, 0, 0);
|
2023-03-23 06:44:52 +01:00
|
|
|
bitcoind_sendrawtx(topo->bitcoind, otx->cmd_id,
|
|
|
|
fmt_bitcoin_tx(tmpctx, otx->tx),
|
2022-09-12 23:19:11 +02:00
|
|
|
allowhighfees,
|
|
|
|
broadcast_done, otx);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 05:07:46 +02:00
|
|
|
static enum watch_result closeinfo_txid_confirmed(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
|
|
|
const struct bitcoin_txid *txid,
|
2019-06-28 03:58:31 +02:00
|
|
|
const struct bitcoin_tx *tx,
|
2018-08-13 05:07:46 +02:00
|
|
|
unsigned int depth)
|
|
|
|
{
|
2019-06-28 03:58:31 +02:00
|
|
|
/* Sanity check. */
|
|
|
|
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-08-13 05:07:46 +02:00
|
|
|
/* We delete ourselves first time, so should not be reorged out!! */
|
|
|
|
assert(depth > 0);
|
|
|
|
/* Subtle: depth 1 == current block. */
|
|
|
|
wallet_confirm_tx(ld->wallet, txid,
|
|
|
|
get_block_height(ld->topology) + 1 - depth);
|
|
|
|
return DELETE_WATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need to know if close_info UTXOs (which the wallet doesn't natively know
|
|
|
|
* how to spend, so is not in the normal path) get reconfirmed.
|
|
|
|
*
|
|
|
|
* This can happen on startup (where we manually unwind 100 blocks) or on a
|
|
|
|
* reorg. The db NULLs out the confirmation_height, so we can't easily figure
|
|
|
|
* out just the new ones (and removing the ON DELETE SET NULL clause is
|
|
|
|
* non-trivial).
|
|
|
|
*
|
|
|
|
* So every time, we just set a notification for every tx in this class we're
|
|
|
|
* not already watching: there are not usually many, nor many reorgs, so the
|
|
|
|
* redundancy is OK.
|
|
|
|
*/
|
|
|
|
static void watch_for_utxo_reconfirmation(struct chain_topology *topo,
|
|
|
|
struct wallet *wallet)
|
|
|
|
{
|
|
|
|
struct utxo **unconfirmed;
|
|
|
|
|
|
|
|
unconfirmed = wallet_get_unconfirmed_closeinfo_utxos(tmpctx, wallet);
|
|
|
|
for (size_t i = 0; i < tal_count(unconfirmed); i++) {
|
|
|
|
assert(unconfirmed[i]->close_info != NULL);
|
|
|
|
assert(unconfirmed[i]->blockheight == NULL);
|
|
|
|
|
2021-10-13 05:45:36 +02:00
|
|
|
if (find_txwatch(topo, &unconfirmed[i]->outpoint.txid, NULL))
|
2018-08-13 05:07:46 +02:00
|
|
|
continue;
|
|
|
|
|
2023-03-23 06:46:52 +01:00
|
|
|
watch_txid(topo, topo, NULL,
|
|
|
|
&unconfirmed[i]->outpoint.txid,
|
|
|
|
closeinfo_txid_confirmed);
|
2018-08-13 05:07:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:07:55 +01:00
|
|
|
/* Mutual recursion via timer. */
|
|
|
|
static void next_updatefee_timer(struct chain_topology *topo);
|
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
static u32 interp_feerate(const struct feerate_est *rates, u32 blockcount)
|
2018-08-24 04:22:05 +02:00
|
|
|
{
|
2023-04-07 06:39:53 +02:00
|
|
|
const struct feerate_est *before = NULL, *after = NULL;
|
|
|
|
|
|
|
|
/* Find before and after. */
|
|
|
|
for (size_t i = 0; i < tal_count(rates); i++) {
|
|
|
|
if (rates[i].blockcount <= blockcount) {
|
|
|
|
before = &rates[i];
|
|
|
|
} else if (rates[i].blockcount > blockcount && !after) {
|
|
|
|
after = &rates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No estimates at all? */
|
|
|
|
if (!before && !after)
|
|
|
|
return 0;
|
|
|
|
/* We don't extrapolate. */
|
|
|
|
if (!before && after)
|
|
|
|
return after->rate;
|
|
|
|
if (before && !after)
|
|
|
|
return before->rate;
|
|
|
|
|
|
|
|
/* Interpolate, eg. blockcount 10, rate 15000, blockcount 20, rate 5000.
|
|
|
|
* At 15, rate should be 10000.
|
|
|
|
* 15000 + (15 - 10) / (20 - 10) * (15000 - 5000)
|
|
|
|
* 15000 + 5 / 10 * 10000
|
|
|
|
* => 10000
|
|
|
|
*/
|
|
|
|
/* Don't go backwards though! */
|
|
|
|
if (before->rate < after->rate)
|
|
|
|
return before->rate;
|
|
|
|
|
|
|
|
return before->rate
|
|
|
|
- ((u64)(blockcount - before->blockcount)
|
|
|
|
* (before->rate - after->rate)
|
|
|
|
/ (after->blockcount - before->blockcount));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 feerate_for_deadline(const struct chain_topology *topo, u32 blockcount)
|
|
|
|
{
|
|
|
|
u32 rate = interp_feerate(topo->feerates[0], blockcount);
|
|
|
|
|
|
|
|
/* 0 is a special value, meaning "don't know" */
|
|
|
|
if (rate && rate < topo->feerate_floor)
|
|
|
|
rate = topo->feerate_floor;
|
|
|
|
return rate;
|
2018-08-24 04:22:05 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
u32 smoothed_feerate_for_deadline(const struct chain_topology *topo,
|
|
|
|
u32 blockcount)
|
2018-08-24 04:22:05 +02:00
|
|
|
{
|
2023-04-07 06:39:53 +02:00
|
|
|
/* Note: we cap it at feerate_floor when we smooth */
|
|
|
|
return interp_feerate(topo->smoothed_feerates, blockcount);
|
2018-08-24 04:22:05 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
/* Mixes in fresh feerate rate into old smoothed values, modifies rate */
|
|
|
|
static void smooth_one_feerate(const struct chain_topology *topo,
|
|
|
|
struct feerate_est *rate)
|
2017-11-21 04:30:29 +01:00
|
|
|
{
|
2018-07-15 18:30:43 +02:00
|
|
|
/* Smoothing factor alpha for simple exponential smoothing. The goal is to
|
2018-07-25 12:06:46 +02:00
|
|
|
* have the feerate account for 90 percent of the values polled in the last
|
|
|
|
* 2 minutes. The following will do that in a polling interval
|
|
|
|
* independent manner. */
|
2018-07-15 18:30:43 +02:00
|
|
|
double alpha = 1 - pow(0.1,(double)topo->poll_seconds / 120);
|
2023-04-07 06:39:53 +02:00
|
|
|
u32 old_feerate, feerate_smooth;
|
2017-11-21 04:34:36 +01:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
/* We don't call this unless we had a previous feerate */
|
|
|
|
old_feerate = smoothed_feerate_for_deadline(topo, rate->blockcount);
|
|
|
|
assert(old_feerate);
|
2018-03-20 04:30:21 +01:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
feerate_smooth = rate->rate * alpha + old_feerate * (1 - alpha);
|
2018-05-17 06:46:19 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
/* But to avoid updating forever, only apply smoothing when its
|
|
|
|
* effect is more then 10 percent */
|
|
|
|
if (abs((int)rate->rate - (int)feerate_smooth) > (0.1 * rate->rate)) {
|
|
|
|
rate->rate = feerate_smooth;
|
|
|
|
log_debug(topo->log,
|
|
|
|
"... polled feerate estimate for %u blocks smoothed to %u (alpha=%.2f)",
|
|
|
|
rate->blockcount, rate->rate, alpha);
|
|
|
|
}
|
2018-05-17 06:46:19 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
if (rate->rate < get_feerate_floor(topo)) {
|
|
|
|
rate->rate = get_feerate_floor(topo);
|
|
|
|
log_debug(topo->log,
|
|
|
|
"... feerate estimate for %u blocks hit floor %u",
|
|
|
|
rate->blockcount, rate->rate);
|
|
|
|
}
|
2018-07-25 12:06:46 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
if (rate->rate != feerate_smooth)
|
|
|
|
log_debug(topo->log,
|
|
|
|
"Feerate estimate for %u blocks set to %u (was %u)",
|
|
|
|
rate->blockcount, rate->rate, feerate_smooth);
|
|
|
|
}
|
2018-07-15 18:30:43 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
static bool feerates_differ(const struct feerate_est *a,
|
|
|
|
const struct feerate_est *b)
|
|
|
|
{
|
|
|
|
if (tal_count(a) != tal_count(b))
|
|
|
|
return true;
|
|
|
|
for (size_t i = 0; i < tal_count(a); i++) {
|
|
|
|
if (a[i].blockcount != b[i].blockcount)
|
|
|
|
return true;
|
|
|
|
if (a[i].rate != b[i].rate)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-20 04:30:21 +01:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
/* In case the plugin does weird stuff! */
|
|
|
|
static bool different_blockcounts(struct chain_topology *topo,
|
|
|
|
const struct feerate_est *old,
|
|
|
|
const struct feerate_est *new)
|
|
|
|
{
|
|
|
|
if (tal_count(old) != tal_count(new)) {
|
|
|
|
log_unusual(topo->log, "Presented with %zu feerates this time (was %zu!)",
|
|
|
|
tal_count(new), tal_count(old));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < tal_count(old); i++) {
|
|
|
|
if (old[i].blockcount != new[i].blockcount) {
|
|
|
|
log_unusual(topo->log, "Presented with feerates"
|
|
|
|
" for blockcount %u, previously %u",
|
|
|
|
new[i].blockcount, old[i].blockcount);
|
|
|
|
return true;
|
2018-03-20 05:07:30 +01:00
|
|
|
}
|
2023-04-07 06:39:53 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_feerates(struct lightningd *ld,
|
|
|
|
u32 feerate_floor,
|
|
|
|
const struct feerate_est *rates TAKES)
|
|
|
|
{
|
|
|
|
struct feerate_est *new_smoothed;
|
|
|
|
bool changed;
|
|
|
|
struct chain_topology *topo = ld->topology;
|
|
|
|
|
|
|
|
topo->feerate_floor = feerate_floor;
|
|
|
|
|
|
|
|
/* Don't bother updating if we got no feerates; we'd rather have
|
|
|
|
* historical ones, if any. */
|
|
|
|
if (tal_count(rates) == 0)
|
|
|
|
goto rearm;
|
|
|
|
|
|
|
|
/* If the feerate blockcounts differ, don't average, just override */
|
|
|
|
if (topo->feerates[0] && different_blockcounts(topo, topo->feerates[0], rates)) {
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(topo->feerates); i++)
|
|
|
|
topo->feerates[i] = tal_free(topo->feerates[i]);
|
|
|
|
topo->smoothed_feerates = tal_free(topo->smoothed_feerates);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move down historical rates, insert these */
|
|
|
|
tal_free(topo->feerates[FEE_HISTORY_NUM-1]);
|
|
|
|
memmove(topo->feerates + 1, topo->feerates,
|
|
|
|
sizeof(topo->feerates[0]) * (FEE_HISTORY_NUM-1));
|
|
|
|
topo->feerates[0] = tal_dup_talarr(topo, struct feerate_est, rates);
|
|
|
|
changed = feerates_differ(topo->feerates[0], topo->feerates[1]);
|
2022-09-09 22:24:20 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
/* Use this as basis of new smoothed ones. */
|
|
|
|
new_smoothed = tal_dup_talarr(topo, struct feerate_est, topo->feerates[0]);
|
|
|
|
|
|
|
|
/* If there were old smoothed feerates, incorporate those */
|
|
|
|
if (tal_count(topo->smoothed_feerates) != 0) {
|
|
|
|
for (size_t i = 0; i < tal_count(new_smoothed); i++)
|
|
|
|
smooth_one_feerate(topo, &new_smoothed[i]);
|
2017-11-21 04:30:29 +01:00
|
|
|
}
|
2023-04-07 06:39:53 +02:00
|
|
|
changed |= feerates_differ(topo->smoothed_feerates, new_smoothed);
|
|
|
|
tal_free(topo->smoothed_feerates);
|
|
|
|
topo->smoothed_feerates = new_smoothed;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
notify_feerate_change(topo->ld);
|
2018-08-22 04:33:32 +02:00
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
rearm:
|
2018-08-22 04:33:32 +02:00
|
|
|
if (topo->feerate_uninitialized) {
|
2018-08-23 13:48:53 +02:00
|
|
|
/* This doesn't mean we *have* a fee estimate, but it does
|
|
|
|
* mean we tried. */
|
2018-08-22 04:33:32 +02:00
|
|
|
topo->feerate_uninitialized = false;
|
|
|
|
maybe_completed_init(topo);
|
|
|
|
}
|
2017-11-21 04:30:29 +01:00
|
|
|
|
2017-12-21 12:07:55 +01:00
|
|
|
next_updatefee_timer(topo);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 12:07:55 +01:00
|
|
|
static void start_fee_estimate(struct chain_topology *topo)
|
2016-05-04 08:36:19 +02:00
|
|
|
{
|
2021-09-01 06:35:15 +02:00
|
|
|
topo->updatefee_timer = NULL;
|
2021-09-08 03:37:01 +02:00
|
|
|
if (topo->stopping)
|
|
|
|
return;
|
2017-12-21 12:07:55 +01:00
|
|
|
/* Once per new block head, update fee estimates. */
|
2023-04-07 06:39:53 +02:00
|
|
|
bitcoind_estimate_fees(topo->bitcoind, update_feerates);
|
2017-12-21 12:07:55 +01:00
|
|
|
}
|
|
|
|
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
struct rate_conversion {
|
|
|
|
u32 blockcount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rate_conversion conversions[] = {
|
|
|
|
[FEERATE_OPENING] = { 12 },
|
|
|
|
[FEERATE_MUTUAL_CLOSE] = { 100 },
|
|
|
|
[FEERATE_UNILATERAL_CLOSE] = { 6 },
|
|
|
|
[FEERATE_DELAYED_TO_US] = { 12 },
|
|
|
|
[FEERATE_HTLC_RESOLUTION] = { 6 },
|
|
|
|
[FEERATE_PENALTY] = { 12 },
|
|
|
|
};
|
|
|
|
|
chaintopology: better feerate targets differentiation
We kept track of an URGENT, a NORMAL, and a SLOW feerate. They were used
for opening (NORMAL), mutual (NORMAL), UNILATERAL (URGENT) transactions
as well as minimum and maximum estimations, and onchain resolution.
We now keep track of more fine-grained feerates:
- `opening` used for funding and also misc transactions
- `mutual_close` used for the mutual close transaction
- `unilateral_close` used for unilateral close (commitment transactions)
- `delayed_to_us` used for resolving our output from our unilateral close
- `htlc_resolution` used for resolving onchain HTLCs
- `penalty` used for resolving revoked transactions
We don't modify our requests to our Bitcoin backend, as the next commit
will batch them !
Changelog-deprecated: The "urgent", "slow", and "normal" field of the `feerates` command are now deprecated.
Changelog-added: The fields "opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution" and "penalty" have been added to the `feerates` command.
2020-03-10 17:52:13 +01:00
|
|
|
u32 opening_feerate(struct chain_topology *topo)
|
2018-08-24 04:22:48 +02:00
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_OPENING];
|
|
|
|
return feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_OPENING].blockcount);
|
2018-08-24 04:22:48 +02:00
|
|
|
}
|
|
|
|
|
chaintopology: better feerate targets differentiation
We kept track of an URGENT, a NORMAL, and a SLOW feerate. They were used
for opening (NORMAL), mutual (NORMAL), UNILATERAL (URGENT) transactions
as well as minimum and maximum estimations, and onchain resolution.
We now keep track of more fine-grained feerates:
- `opening` used for funding and also misc transactions
- `mutual_close` used for the mutual close transaction
- `unilateral_close` used for unilateral close (commitment transactions)
- `delayed_to_us` used for resolving our output from our unilateral close
- `htlc_resolution` used for resolving onchain HTLCs
- `penalty` used for resolving revoked transactions
We don't modify our requests to our Bitcoin backend, as the next commit
will batch them !
Changelog-deprecated: The "urgent", "slow", and "normal" field of the `feerates` command are now deprecated.
Changelog-added: The fields "opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution" and "penalty" have been added to the `feerates` command.
2020-03-10 17:52:13 +01:00
|
|
|
u32 mutual_close_feerate(struct chain_topology *topo)
|
2018-08-24 04:22:48 +02:00
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_MUTUAL_CLOSE];
|
|
|
|
return smoothed_feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_MUTUAL_CLOSE].blockcount);
|
2018-08-24 04:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 unilateral_feerate(struct chain_topology *topo)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_UNILATERAL_CLOSE];
|
|
|
|
return smoothed_feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_UNILATERAL_CLOSE].blockcount)
|
|
|
|
* topo->ld->config.commit_fee_percent / 100;
|
2018-08-24 04:22:48 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:17:10 +01:00
|
|
|
u32 delayed_to_us_feerate(struct chain_topology *topo)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_DELAYED_TO_US];
|
|
|
|
return smoothed_feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_DELAYED_TO_US].blockcount);
|
2020-03-10 17:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 htlc_resolution_feerate(struct chain_topology *topo)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_HTLC_RESOLUTION];
|
|
|
|
return smoothed_feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_HTLC_RESOLUTION].blockcount);
|
2020-03-10 17:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 penalty_feerate(struct chain_topology *topo)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (topo->ld->force_feerates)
|
|
|
|
return topo->ld->force_feerates[FEERATE_PENALTY];
|
|
|
|
return smoothed_feerate_for_deadline(topo,
|
|
|
|
conversions[FEERATE_PENALTY].blockcount);
|
2020-03-10 17:17:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-07 06:39:53 +02:00
|
|
|
u32 get_feerate_floor(const struct chain_topology *topo)
|
|
|
|
{
|
2023-04-07 06:44:12 +02:00
|
|
|
return topo->feerate_floor;
|
2023-04-07 06:39:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_feerates(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-08-24 04:22:02 +02:00
|
|
|
{
|
|
|
|
struct chain_topology *topo = cmd->ld->topology;
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response;
|
2018-08-24 04:22:02 +02:00
|
|
|
bool missing;
|
2018-08-27 07:11:39 +02:00
|
|
|
enum feerate_style *style;
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
u32 rate;
|
2018-08-24 04:22:02 +02:00
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("style", param_feerate_style, &style),
|
2018-08-24 04:22:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-08-24 04:22:02 +02:00
|
|
|
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
missing = (tal_count(topo->feerates[0]) == 0);
|
2018-08-24 04:22:02 +02:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
response = json_stream_success(cmd);
|
2020-07-14 08:27:50 +02:00
|
|
|
if (missing)
|
|
|
|
json_add_string(response, "warning_missing_feerates",
|
|
|
|
"Some fee estimates unavailable: bitcoind startup?");
|
|
|
|
|
2020-07-07 22:50:21 +02:00
|
|
|
json_object_start(response, feerate_style_name(*style));
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
rate = opening_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "opening", feerate_to_style(rate, *style));
|
|
|
|
rate = mutual_close_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "mutual_close",
|
|
|
|
feerate_to_style(rate, *style));
|
|
|
|
rate = unilateral_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "unilateral_close",
|
|
|
|
feerate_to_style(rate, *style));
|
|
|
|
rate = penalty_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "penalty",
|
|
|
|
feerate_to_style(rate, *style));
|
|
|
|
if (deprecated_apis) {
|
|
|
|
rate = delayed_to_us_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "delayed_to_us",
|
|
|
|
feerate_to_style(rate, *style));
|
|
|
|
rate = htlc_resolution_feerate(topo);
|
|
|
|
if (rate)
|
|
|
|
json_add_num(response, "htlc_resolution",
|
|
|
|
feerate_to_style(rate, *style));
|
2018-08-24 04:22:02 +02:00
|
|
|
}
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
|
2018-08-24 04:22:05 +02:00
|
|
|
json_add_u64(response, "min_acceptable",
|
2018-08-27 07:11:39 +02:00
|
|
|
feerate_to_style(feerate_min(cmd->ld, NULL), *style));
|
2018-08-24 04:22:05 +02:00
|
|
|
json_add_u64(response, "max_acceptable",
|
2018-08-27 07:11:39 +02:00
|
|
|
feerate_to_style(feerate_max(cmd->ld, NULL), *style));
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
|
|
|
|
json_array_start(response, "estimates");
|
|
|
|
assert(tal_count(topo->smoothed_feerates) == tal_count(topo->feerates[0]));
|
|
|
|
for (size_t i = 0; i < tal_count(topo->feerates[0]); i++) {
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_num(response, "blockcount",
|
|
|
|
topo->feerates[0][i].blockcount);
|
|
|
|
json_add_u64(response, "feerate",
|
|
|
|
feerate_to_style(topo->feerates[0][i].rate, *style));
|
|
|
|
json_add_u64(response, "smoothed_feerate",
|
|
|
|
feerate_to_style(topo->smoothed_feerates[i].rate,
|
|
|
|
*style));
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
2018-08-24 04:22:02 +02:00
|
|
|
json_object_end(response);
|
|
|
|
|
2020-07-14 08:27:50 +02:00
|
|
|
if (!missing) {
|
2020-08-13 19:44:02 +02:00
|
|
|
/* It actually is negotiated per-channel... */
|
|
|
|
bool anchor_outputs
|
|
|
|
= feature_offered(cmd->ld->our_features->bits[INIT_FEATURE],
|
|
|
|
OPT_ANCHOR_OUTPUTS);
|
|
|
|
|
2018-08-24 04:22:48 +02:00
|
|
|
json_object_start(response, "onchain_fee_estimates");
|
|
|
|
/* eg 020000000001016f51de645a47baa49a636b8ec974c28bdff0ac9151c0f4eda2dbe3b41dbe711d000000001716001401fad90abcd66697e2592164722de4a95ebee165ffffffff0240420f00000000002200205b8cd3b914cf67cdd8fa6273c930353dd36476734fbd962102c2df53b90880cdb73f890000000000160014c2ccab171c2a5be9dab52ec41b825863024c54660248304502210088f65e054dbc2d8f679de3e40150069854863efa4a45103b2bb63d060322f94702200d3ae8923924a458cffb0b7360179790830027bb6b29715ba03e12fc22365de1012103d745445c9362665f22e0d96e9e766f273f3260dea39c8a76bfa05dd2684ddccf00000000 == weight 702 */
|
|
|
|
json_add_num(response, "opening_channel_satoshis",
|
|
|
|
opening_feerate(cmd->ld->topology) * 702 / 1000);
|
|
|
|
/* eg. 02000000000101afcfac637d44d4e0df52031dba55b18d3f1bd79ad4b7ebbee964f124c5163dc30100000000ffffffff02400d03000000000016001427213e2217b4f56bd19b6c8393dc9f61be691233ca1f0c0000000000160014071c49cad2f420f3c805f9f6b98a57269cb1415004004830450221009a12b4d5ae1d41781f79bedecfa3e65542b1799a46c272287ba41f009d2e27ff0220382630c899207487eba28062f3989c4b656c697c23a8c89c1d115c98d82ff261014730440220191ddf13834aa08ea06dca8191422e85d217b065462d1b405b665eefa0684ed70220252409bf033eeab3aae89ae27596d7e0491bcc7ae759c5644bced71ef3cccef30147522102324266de8403b3ab157a09f1f784d587af61831c998c151bcc21bb74c2b2314b2102e3bd38009866c9da8ec4aa99cc4ea9c6c0dd46df15c61ef0ce1f271291714e5752ae00000000 == weight 673 */
|
|
|
|
json_add_u64(response, "mutual_close_satoshis",
|
|
|
|
mutual_close_feerate(cmd->ld->topology) * 673 / 1000);
|
|
|
|
/* eg. 02000000000101c4fecaae1ea940c15ec502de732c4c386d51f981317605bbe5ad2c59165690ab00000000009db0e280010a2d0f00000000002200208d290003cedb0dd00cd5004c2d565d55fc70227bf5711186f4fa9392f8f32b4a0400483045022100952fcf8c730c91cf66bcb742cd52f046c0db3694dc461e7599be330a22466d790220740738a6f9d9e1ae5c86452fa07b0d8dddc90f8bee4ded24a88fe4b7400089eb01483045022100db3002a93390fc15c193da57d6ce1020e82705e760a3aa935ebe864bd66dd8e8022062ee9c6aa7b88ff4580e2671900a339754116371d8f40eba15b798136a76cd150147522102324266de8403b3ab157a09f1f784d587af61831c998c151bcc21bb74c2b2314b2102e3bd38009866c9da8ec4aa99cc4ea9c6c0dd46df15c61ef0ce1f271291714e5752ae9a3ed620 == weight 598 */
|
|
|
|
json_add_u64(response, "unilateral_close_satoshis",
|
|
|
|
unilateral_feerate(cmd->ld->topology) * 598 / 1000);
|
2020-08-13 19:44:02 +02:00
|
|
|
|
|
|
|
/* This really depends on whether we *negotiated*
|
|
|
|
* option_anchor_outputs for a particular channel! */
|
2020-03-10 19:37:56 +01:00
|
|
|
json_add_u64(response, "htlc_timeout_satoshis",
|
2020-08-13 19:44:02 +02:00
|
|
|
htlc_timeout_fee(htlc_resolution_feerate(cmd->ld->topology),
|
|
|
|
anchor_outputs).satoshis /* Raw: estimate */);
|
2020-03-10 19:37:56 +01:00
|
|
|
json_add_u64(response, "htlc_success_satoshis",
|
2020-08-13 19:44:02 +02:00
|
|
|
htlc_success_fee(htlc_resolution_feerate(cmd->ld->topology),
|
|
|
|
anchor_outputs).satoshis /* Raw: estimate */);
|
2018-08-24 04:22:48 +02:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_success(cmd, response);
|
2018-08-24 04:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command feerates_command = {
|
|
|
|
"feerates",
|
2019-05-22 16:08:16 +02:00
|
|
|
"bitcoin",
|
2018-08-24 04:22:02 +02:00
|
|
|
json_feerates,
|
2018-08-28 22:46:36 +02:00
|
|
|
"Return feerate estimates, either satoshi-per-kw ({style} perkw) or satoshi-per-kb ({style} perkb)."
|
2018-08-24 04:22:02 +02:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &feerates_command);
|
|
|
|
|
2021-06-23 21:31:47 +02:00
|
|
|
static struct command_result *json_parse_feerate(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct json_stream *response;
|
|
|
|
u32 *feerate;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("feerate", param_feerate, &feerate),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
|
|
|
json_add_num(response, feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
feerate_to_style(*feerate, FEERATE_PER_KSIPA));
|
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command parse_feerate_command = {
|
|
|
|
"parsefeerate",
|
|
|
|
"bitcoin",
|
|
|
|
json_parse_feerate,
|
|
|
|
"Return current feerate in perkw + perkb for given feerate string."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &parse_feerate_command);
|
|
|
|
|
2017-12-21 12:07:55 +01:00
|
|
|
static void next_updatefee_timer(struct chain_topology *topo)
|
|
|
|
{
|
2021-09-01 06:35:15 +02:00
|
|
|
assert(!topo->updatefee_timer);
|
|
|
|
topo->updatefee_timer = new_reltimer(topo->ld->timers, topo,
|
|
|
|
time_from_sec(topo->poll_seconds),
|
|
|
|
start_fee_estimate, topo);
|
2017-12-21 12:07:55 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 12:47:32 +02:00
|
|
|
struct sync_waiter {
|
|
|
|
/* Linked from chain_topology->sync_waiters */
|
|
|
|
struct list_node list;
|
|
|
|
void (*cb)(struct chain_topology *topo, void *arg);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void destroy_sync_waiter(struct sync_waiter *waiter)
|
|
|
|
{
|
|
|
|
list_del(&waiter->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void topology_add_sync_waiter_(const tal_t *ctx,
|
|
|
|
struct chain_topology *topo,
|
|
|
|
void (*cb)(struct chain_topology *topo,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct sync_waiter *w = tal(ctx, struct sync_waiter);
|
|
|
|
w->cb = cb;
|
|
|
|
w->arg = arg;
|
|
|
|
list_add_tail(topo->sync_waiters, &w->list);
|
|
|
|
tal_add_destructor(w, destroy_sync_waiter);
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:10:26 +01:00
|
|
|
/* Once we're run out of new blocks to add, call this. */
|
|
|
|
static void updates_complete(struct chain_topology *topo)
|
|
|
|
{
|
2020-01-29 05:50:11 +01:00
|
|
|
if (!bitcoin_blkid_eq(&topo->tip->blkid, &topo->prev_tip)) {
|
2017-12-21 12:10:26 +01:00
|
|
|
/* Tell watch code to re-evaluate all txs. */
|
|
|
|
watch_topology_changed(topo);
|
|
|
|
|
2022-07-07 04:26:51 +02:00
|
|
|
/* Tell lightningd about new block. */
|
|
|
|
notify_new_block(topo->bitcoind->ld, topo->tip->height);
|
|
|
|
|
2017-12-21 12:10:26 +01:00
|
|
|
/* Maybe need to rebroadcast. */
|
2022-09-12 23:19:11 +02:00
|
|
|
rebroadcast_txs(topo);
|
2017-12-21 12:10:26 +01:00
|
|
|
|
2018-02-15 23:09:53 +01:00
|
|
|
/* We've processed these UTXOs */
|
|
|
|
db_set_intvar(topo->bitcoind->ld->wallet->db,
|
|
|
|
"last_processed_block", topo->tip->height);
|
|
|
|
|
2020-01-29 05:50:11 +01:00
|
|
|
topo->prev_tip = topo->tip->blkid;
|
2021-12-13 23:44:32 +01:00
|
|
|
|
|
|
|
/* Send out an account balance snapshot */
|
2021-12-10 16:46:42 +01:00
|
|
|
if (!first_update_complete) {
|
|
|
|
send_account_balance_snapshot(topo->ld, topo->tip->height);
|
|
|
|
first_update_complete = true;
|
|
|
|
}
|
2017-12-21 12:10:26 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 12:47:32 +02:00
|
|
|
/* If bitcoind is synced, we're now synced. */
|
|
|
|
if (topo->bitcoind->synced && !topology_synced(topo)) {
|
|
|
|
struct sync_waiter *w;
|
|
|
|
struct list_head *list = topo->sync_waiters;
|
|
|
|
|
|
|
|
/* Mark topology_synced() before callbacks. */
|
|
|
|
topo->sync_waiters = NULL;
|
|
|
|
|
|
|
|
while ((w = list_pop(list, struct sync_waiter, list))) {
|
|
|
|
/* In case it doesn't free itself. */
|
|
|
|
tal_del_destructor(w, destroy_sync_waiter);
|
|
|
|
tal_steal(list, w);
|
|
|
|
w->cb(topo, w->arg);
|
|
|
|
}
|
|
|
|
tal_free(list);
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:10:26 +01:00
|
|
|
/* Try again soon. */
|
|
|
|
next_topology_timer(topo);
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:54:56 +01:00
|
|
|
static void record_wallet_spend(struct lightningd *ld,
|
|
|
|
struct bitcoin_outpoint *outpoint,
|
|
|
|
struct bitcoin_txid *txid,
|
|
|
|
u32 tx_blockheight)
|
2020-04-03 23:56:57 +02:00
|
|
|
{
|
|
|
|
struct utxo *utxo;
|
|
|
|
|
2021-11-10 22:54:56 +01:00
|
|
|
/* Find the amount this was for */
|
|
|
|
utxo = wallet_utxo_get(tmpctx, ld->wallet, outpoint);
|
2020-06-17 00:09:21 +02:00
|
|
|
if (!utxo) {
|
2021-10-13 05:45:36 +02:00
|
|
|
log_broken(ld->log, "No record of utxo %s",
|
2021-11-10 22:54:56 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_outpoint,
|
|
|
|
outpoint));
|
2020-06-17 00:09:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-03 23:56:57 +02:00
|
|
|
|
2021-11-10 22:54:56 +01:00
|
|
|
notify_chain_mvt(ld, new_coin_wallet_withdraw(tmpctx, txid, outpoint,
|
|
|
|
tx_blockheight,
|
|
|
|
utxo->amount, WITHDRAWAL));
|
2020-04-03 23:56:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:07:13 +01:00
|
|
|
/**
|
|
|
|
* topo_update_spends -- Tell the wallet about all spent outpoints
|
|
|
|
*/
|
|
|
|
static void topo_update_spends(struct chain_topology *topo, struct block *b)
|
|
|
|
{
|
2020-09-04 17:50:17 +02:00
|
|
|
const struct short_channel_id *spent_scids;
|
2018-03-02 15:07:13 +01:00
|
|
|
for (size_t i = 0; i < tal_count(b->full_txs); i++) {
|
|
|
|
const struct bitcoin_tx *tx = b->full_txs[i];
|
2020-04-03 23:56:57 +02:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
for (size_t j = 0; j < tx->wtx->num_inputs; j++) {
|
2021-10-13 05:45:36 +02:00
|
|
|
struct bitcoin_outpoint outpoint;
|
2020-04-03 23:56:57 +02:00
|
|
|
|
2021-10-13 05:45:36 +02:00
|
|
|
bitcoin_tx_input_get_outpoint(tx, j, &outpoint);
|
2019-03-25 11:35:56 +01:00
|
|
|
|
2021-11-10 22:54:56 +01:00
|
|
|
if (wallet_outpoint_spend(topo->ld->wallet, tmpctx,
|
|
|
|
b->height, &outpoint))
|
|
|
|
record_wallet_spend(topo->ld, &outpoint,
|
|
|
|
&b->txids[i], b->height);
|
2020-04-03 23:56:57 +02:00
|
|
|
|
2021-11-10 22:54:56 +01:00
|
|
|
}
|
2018-03-02 15:07:13 +01:00
|
|
|
}
|
2021-11-10 22:54:56 +01:00
|
|
|
|
2020-09-04 17:50:17 +02:00
|
|
|
/* Retrieve all potential channel closes from the UTXO set and
|
|
|
|
* tell gossipd about them. */
|
|
|
|
spent_scids =
|
|
|
|
wallet_utxoset_get_spent(tmpctx, topo->ld->wallet, b->height);
|
2022-09-14 05:50:32 +02:00
|
|
|
gossipd_notify_spends(topo->bitcoind->ld, b->height, spent_scids);
|
2018-03-02 15:07:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 01:38:49 +01:00
|
|
|
static void topo_add_utxos(struct chain_topology *topo, struct block *b)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(b->full_txs); i++) {
|
|
|
|
const struct bitcoin_tx *tx = b->full_txs[i];
|
2021-10-13 05:45:36 +02:00
|
|
|
struct bitcoin_outpoint outpoint;
|
|
|
|
|
|
|
|
bitcoin_txid(tx, &outpoint.txid);
|
|
|
|
for (outpoint.n = 0;
|
|
|
|
outpoint.n < tx->wtx->num_outputs;
|
|
|
|
outpoint.n++) {
|
|
|
|
if (tx->wtx->outputs[outpoint.n].features
|
|
|
|
& WALLY_TX_IS_COINBASE)
|
2019-04-13 19:14:07 +02:00
|
|
|
continue;
|
|
|
|
|
2021-10-13 05:45:36 +02:00
|
|
|
const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, outpoint.n);
|
|
|
|
struct amount_asset amt = bitcoin_tx_output_get_amount(tx, outpoint.n);
|
2019-03-25 11:35:56 +01:00
|
|
|
|
2019-09-26 21:07:20 +02:00
|
|
|
if (amount_asset_is_main(&amt) && is_p2wsh(script, NULL)) {
|
2021-10-13 05:45:36 +02:00
|
|
|
wallet_utxoset_add(topo->ld->wallet, &outpoint,
|
2019-03-25 11:35:56 +01:00
|
|
|
b->height, i, script,
|
2019-09-26 21:07:20 +02:00
|
|
|
amount_asset_to_sat(&amt));
|
2018-03-04 01:38:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
static void add_tip(struct chain_topology *topo, struct block *b)
|
2017-12-21 12:07:55 +01:00
|
|
|
{
|
2017-12-21 12:09:25 +01:00
|
|
|
/* Attach to tip; b is now the tip. */
|
|
|
|
assert(b->height == topo->tip->height + 1);
|
|
|
|
b->prev = topo->tip;
|
2019-03-24 08:19:50 +01:00
|
|
|
topo->tip->next = b; /* FIXME this doesn't seem to be used anywhere */
|
2017-12-21 12:09:25 +01:00
|
|
|
topo->tip = b;
|
2018-09-03 02:44:27 +02:00
|
|
|
wallet_block_add(topo->ld->wallet, b);
|
2018-02-26 11:36:48 +01:00
|
|
|
|
2018-03-04 01:38:49 +01:00
|
|
|
topo_add_utxos(topo, b);
|
2018-03-02 15:07:13 +01:00
|
|
|
topo_update_spends(topo, b);
|
|
|
|
|
2018-02-26 11:36:48 +01:00
|
|
|
/* Only keep the transactions we care about. */
|
|
|
|
filter_block_txs(topo, b);
|
|
|
|
|
2023-01-03 05:46:42 +01:00
|
|
|
block_map_add(topo->block_map, b);
|
2018-06-04 14:46:05 +02:00
|
|
|
topo->max_blockheight = b->height;
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static struct block *new_block(struct chain_topology *topo,
|
2016-05-04 08:36:19 +02:00
|
|
|
struct bitcoin_block *blk,
|
2017-12-21 12:09:25 +01:00
|
|
|
unsigned int height)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct block *b = tal(topo, struct block);
|
|
|
|
|
2019-04-13 17:55:06 +02:00
|
|
|
bitcoin_block_blkid(blk, &b->blkid);
|
2018-01-25 12:03:50 +01:00
|
|
|
log_debug(topo->log, "Adding block %u: %s",
|
|
|
|
height,
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));
|
2023-01-03 05:46:42 +01:00
|
|
|
assert(!block_map_get(topo->block_map, &b->blkid));
|
2017-12-21 12:09:25 +01:00
|
|
|
b->next = NULL;
|
|
|
|
b->prev = NULL;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
b->height = height;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
b->hdr = blk->hdr;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
b->full_txs = tal_steal(b, blk->tx);
|
2020-08-26 13:31:41 +02:00
|
|
|
b->txids = tal_steal(b, blk->txids);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
static void remove_tip(struct chain_topology *topo)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
2017-12-21 12:09:25 +01:00
|
|
|
struct block *b = topo->tip;
|
2018-04-10 11:14:50 +02:00
|
|
|
struct bitcoin_txid *txs;
|
2022-07-25 09:00:09 +02:00
|
|
|
size_t n;
|
2021-08-12 05:05:55 +02:00
|
|
|
const struct short_channel_id *removed_scids;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2019-03-24 08:19:50 +01:00
|
|
|
log_debug(topo->log, "Removing stale block %u: %s",
|
|
|
|
topo->tip->height,
|
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
/* Move tip back one. */
|
|
|
|
topo->tip = b->prev;
|
2019-03-24 08:19:50 +01:00
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
if (!topo->tip)
|
|
|
|
fatal("Initial block %u (%s) reorganized out!",
|
|
|
|
b->height,
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2018-09-03 02:44:27 +02:00
|
|
|
txs = wallet_transactions_by_height(b, topo->ld->wallet, b->height);
|
2018-04-10 11:14:50 +02:00
|
|
|
n = tal_count(txs);
|
|
|
|
|
2019-03-24 08:19:50 +01:00
|
|
|
/* Notify that txs are kicked out (their height will be set NULL in db) */
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t i = 0; i < n; i++)
|
2018-04-10 11:14:50 +02:00
|
|
|
txwatch_fire(topo, &txs[i], 0);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2021-08-12 05:05:55 +02:00
|
|
|
/* Grab these before we delete block from db */
|
|
|
|
removed_scids = wallet_utxoset_get_created(tmpctx, topo->ld->wallet,
|
|
|
|
b->height);
|
2018-09-03 02:44:27 +02:00
|
|
|
wallet_block_remove(topo->ld->wallet, b);
|
2021-08-12 05:05:55 +02:00
|
|
|
|
2018-08-13 05:07:46 +02:00
|
|
|
/* This may have unconfirmed txs: reconfirm as we add blocks. */
|
2018-09-03 02:44:27 +02:00
|
|
|
watch_for_utxo_reconfirmation(topo, topo->ld->wallet);
|
2023-01-03 05:46:42 +01:00
|
|
|
block_map_del(topo->block_map, b);
|
2021-08-12 05:05:55 +02:00
|
|
|
|
|
|
|
/* These no longer exist, so gossipd drops any reference to them just
|
|
|
|
* as if they were spent. */
|
2022-09-14 05:50:32 +02:00
|
|
|
gossipd_notify_spends(topo->bitcoind->ld, b->height, removed_scids);
|
|
|
|
tal_free(b);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 18:47:35 +01:00
|
|
|
static void get_new_block(struct bitcoind *bitcoind,
|
|
|
|
struct bitcoin_blkid *blkid,
|
|
|
|
struct bitcoin_block *blk,
|
|
|
|
struct chain_topology *topo)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
2020-01-08 18:47:35 +01:00
|
|
|
if (!blkid && !blk) {
|
|
|
|
/* No such block, we're done. */
|
|
|
|
updates_complete(topo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(blkid && blk);
|
|
|
|
|
2019-07-30 22:04:55 +02:00
|
|
|
/* Annotate all transactions with the chainparams */
|
2020-01-08 18:47:35 +01:00
|
|
|
for (size_t i = 0; i < tal_count(blk->tx); i++)
|
2019-07-30 22:04:55 +02:00
|
|
|
blk->tx[i]->chainparams = chainparams;
|
|
|
|
|
2018-01-02 10:38:49 +01:00
|
|
|
/* Unexpected predecessor? Free predecessor, refetch it. */
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&topo->tip->blkid, &blk->hdr.prev_hash))
|
2017-12-21 12:09:25 +01:00
|
|
|
remove_tip(topo);
|
2022-09-09 14:48:31 +02:00
|
|
|
else {
|
2017-12-21 12:09:25 +01:00
|
|
|
add_tip(topo, new_block(topo, blk, topo->tip->height + 1));
|
|
|
|
|
2022-09-09 14:48:31 +02:00
|
|
|
/* tell plugins a new block was processed */
|
2022-09-12 22:43:54 +02:00
|
|
|
notify_block_added(topo->ld, topo->tip);
|
2022-09-09 14:48:31 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
/* Try for next one. */
|
|
|
|
try_extend_tip(topo);
|
2017-03-02 13:21:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 12:09:25 +01:00
|
|
|
static void try_extend_tip(struct chain_topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2021-09-01 06:35:15 +02:00
|
|
|
topo->extend_timer = NULL;
|
2021-09-08 03:37:01 +02:00
|
|
|
if (topo->stopping)
|
|
|
|
return;
|
2020-01-08 18:47:35 +01:00
|
|
|
bitcoind_getrawblockbyheight(topo->bitcoind, topo->tip->height + 1,
|
|
|
|
get_new_block, topo);
|
2016-08-09 05:41:22 +02:00
|
|
|
}
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void init_topo(struct bitcoind *bitcoind UNUSED,
|
2020-01-08 17:53:31 +01:00
|
|
|
struct bitcoin_blkid *blkid UNUSED,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct bitcoin_block *blk,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct chain_topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2018-06-04 14:46:05 +02:00
|
|
|
topo->root = new_block(topo, blk, topo->max_blockheight);
|
2023-01-03 05:46:42 +01:00
|
|
|
block_map_add(topo->block_map, topo->root);
|
2020-01-29 05:50:11 +01:00
|
|
|
topo->tip = topo->root;
|
|
|
|
topo->prev_tip = topo->tip->blkid;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2018-03-06 19:30:21 +01:00
|
|
|
/* In case we don't get all the way to updates_complete */
|
|
|
|
db_set_intvar(topo->bitcoind->ld->wallet->db,
|
|
|
|
"last_processed_block", topo->tip->height);
|
|
|
|
|
2018-08-22 04:33:32 +02:00
|
|
|
maybe_completed_init(topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
u32 get_block_height(const struct chain_topology *topo)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
return topo->tip->height;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 13:26:44 +02:00
|
|
|
u32 get_network_blockheight(const struct chain_topology *topo)
|
|
|
|
{
|
|
|
|
if (topo->tip->height > topo->headercount)
|
|
|
|
return topo->tip->height;
|
|
|
|
else
|
|
|
|
return topo->headercount;
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:22:04 +02:00
|
|
|
u32 feerate_min(struct lightningd *ld, bool *unknown)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
const struct chain_topology *topo = ld->topology;
|
2018-08-24 04:22:04 +02:00
|
|
|
u32 min;
|
|
|
|
|
|
|
|
if (unknown)
|
|
|
|
*unknown = false;
|
|
|
|
|
|
|
|
/* We can't allow less than feerate_floor, since that won't relay */
|
|
|
|
if (ld->config.ignore_fee_limits)
|
|
|
|
min = 1;
|
|
|
|
else {
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
min = 0xFFFFFFFF;
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(topo->feerates); i++) {
|
|
|
|
for (size_t j = 0; j < tal_count(topo->feerates[i]); j++) {
|
|
|
|
if (topo->feerates[i][j].rate < min)
|
|
|
|
min = topo->feerates[i][j].rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (min == 0xFFFFFFFF) {
|
2018-08-24 04:22:05 +02:00
|
|
|
if (unknown)
|
|
|
|
*unknown = true;
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
min = 0;
|
2018-08-24 04:22:05 +02:00
|
|
|
}
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
|
|
|
|
/* FIXME: This is what bcli used to do: halve the slow feerate! */
|
|
|
|
min /= 2;
|
2018-08-24 04:22:04 +02:00
|
|
|
}
|
|
|
|
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
if (min < get_feerate_floor(topo))
|
|
|
|
return get_feerate_floor(topo);
|
2018-08-24 04:22:04 +02:00
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 feerate_max(struct lightningd *ld, bool *unknown)
|
|
|
|
{
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
const struct chain_topology *topo = ld->topology;
|
|
|
|
u32 max = 0;
|
2018-08-24 04:22:04 +02:00
|
|
|
|
|
|
|
if (unknown)
|
|
|
|
*unknown = false;
|
|
|
|
|
|
|
|
if (ld->config.ignore_fee_limits)
|
|
|
|
return UINT_MAX;
|
|
|
|
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(topo->feerates); i++) {
|
|
|
|
for (size_t j = 0; j < tal_count(topo->feerates[i]); j++) {
|
|
|
|
if (topo->feerates[i][j].rate > max)
|
|
|
|
max = topo->feerates[i][j].rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!max) {
|
2018-08-24 04:22:04 +02:00
|
|
|
if (unknown)
|
|
|
|
*unknown = true;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
lightningd: clean up feerate handling, deprecate old terms.
Drop try_get_feerate() in favor of explicit feerate_for_deadline() and
smoothed_feerate_for_deadline().
This shows us everywhere we deal with old-style feerates by names.
`delayed_to_us` and `htlc_resolution` will be moving to dynamic fees,
so deprecate those.
Note that "penalty" is still used for generating penalty txs for
watchtowers, and "unilateral_close" still used until we get zero-fee
anchors.
Changelog-Added: JSON-RPC: `feerates` `estimates` array shows fee estimates by blockcount from underlying plugin (usually *bcli*).
Changelog-Changed: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) value *slow* is now 100 block-estimate, not half of 100-block estimate.
Changelog-Deprecated: JSON-RPC: `close`, `fundchannel`, `fundpsbt`, `multifundchannel`, `multiwithdraw`, `txprepare`, `upgradewallet`, `withdraw` `feerate` (`feerange` for `close`) expressed as, "delayed_to_us", "htlc_resolution", "max_acceptable" or "min_acceptable". Use explicit block counts or *slow*/*normal*/*urgent*/*minimum*.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 06:43:39 +02:00
|
|
|
return max * topo->ld->config.max_fee_multiplier;
|
2018-08-24 04:22:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
/* On shutdown, channels get deleted last. That frees from our list, so
|
2017-03-02 13:21:49 +01:00
|
|
|
* do it now instead. */
|
2018-02-14 02:53:13 +01:00
|
|
|
static void destroy_chain_topology(struct chain_topology *topo)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
struct outgoing_tx *otx;
|
2022-11-15 15:23:21 +01:00
|
|
|
struct outgoing_tx_map_iter it;
|
2023-01-03 05:46:42 +01:00
|
|
|
for (otx = outgoing_tx_map_first(topo->outgoing_txs, &it); otx;
|
|
|
|
otx = outgoing_tx_map_next(topo->outgoing_txs, &it)) {
|
2022-11-15 15:23:21 +01:00
|
|
|
tal_del_destructor2(otx, destroy_outgoing_tx, topo);
|
2017-03-02 13:21:49 +01:00
|
|
|
tal_free(otx);
|
2022-11-15 15:23:21 +01:00
|
|
|
}
|
2017-03-02 13:21:49 +01:00
|
|
|
}
|
|
|
|
|
2017-11-01 11:20:40 +01:00
|
|
|
struct chain_topology *new_topology(struct lightningd *ld, struct log *log)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2017-11-01 11:20:40 +01:00
|
|
|
struct chain_topology *topo = tal(ld, struct chain_topology);
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2018-09-03 02:44:27 +02:00
|
|
|
topo->ld = ld;
|
2023-01-03 05:46:42 +01:00
|
|
|
topo->block_map = tal(topo, struct block_map);
|
|
|
|
block_map_init(topo->block_map);
|
|
|
|
topo->outgoing_txs = tal(topo, struct outgoing_tx_map);
|
|
|
|
outgoing_tx_map_init(topo->outgoing_txs);
|
|
|
|
topo->txwatches = tal(topo, struct txwatch_hash);
|
|
|
|
txwatch_hash_init(topo->txwatches);
|
|
|
|
topo->txowatches = tal(topo, struct txowatch_hash);
|
|
|
|
txowatch_hash_init(topo->txowatches);
|
2017-03-02 13:21:49 +01:00
|
|
|
topo->log = log;
|
2017-11-01 11:20:40 +01:00
|
|
|
topo->bitcoind = new_bitcoind(topo, ld, log);
|
2018-05-17 06:08:24 +02:00
|
|
|
topo->poll_seconds = 30;
|
2018-08-22 04:33:32 +02:00
|
|
|
topo->feerate_uninitialized = true;
|
2023-04-07 06:39:53 +02:00
|
|
|
memset(topo->feerates, 0, sizeof(topo->feerates));
|
|
|
|
topo->smoothed_feerates = NULL;
|
2018-08-22 04:33:32 +02:00
|
|
|
topo->root = NULL;
|
2019-09-22 04:05:43 +02:00
|
|
|
topo->sync_waiters = tal(topo, struct list_head);
|
2022-11-30 03:37:46 +01:00
|
|
|
topo->extend_timer = NULL;
|
2021-09-08 03:37:01 +02:00
|
|
|
topo->stopping = false;
|
2019-09-22 04:05:43 +02:00
|
|
|
list_head_init(topo->sync_waiters);
|
2020-01-08 17:53:31 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
return topo;
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
static void check_blockcount(struct chain_topology *topo, u32 blockcount)
|
|
|
|
{
|
|
|
|
/* If bitcoind's current blockheight is below the requested
|
|
|
|
* height, refuse. You can always explicitly request a reindex from
|
|
|
|
* that block number using --rescan=. */
|
|
|
|
if (blockcount < topo->max_blockheight) {
|
|
|
|
/* UINT32_MAX == no blocks in database */
|
|
|
|
if (topo->max_blockheight == UINT32_MAX) {
|
|
|
|
/* Relative rescan, but we didn't know the blockheight */
|
|
|
|
/* Protect against underflow in subtraction.
|
|
|
|
* Possible in regtest mode. */
|
|
|
|
if (blockcount < topo->bitcoind->ld->config.rescan)
|
|
|
|
topo->max_blockheight = 0;
|
|
|
|
else
|
|
|
|
topo->max_blockheight = blockcount - topo->bitcoind->ld->config.rescan;
|
|
|
|
} else
|
|
|
|
fatal("bitcoind has gone backwards from %u to %u blocks!",
|
|
|
|
topo->max_blockheight, blockcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rollback to the given blockheight, so we start track
|
|
|
|
* correctly again */
|
|
|
|
wallet_blocks_rollback(topo->ld->wallet, topo->max_blockheight);
|
|
|
|
/* This may have unconfirmed txs: reconfirm as we add blocks. */
|
|
|
|
watch_for_utxo_reconfirmation(topo, topo->ld->wallet);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void retry_check_chain(struct chain_topology *topo);
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_chain(struct bitcoind *bitcoind, const char *chain,
|
|
|
|
const u32 headercount, const u32 blockcount, const bool ibd,
|
|
|
|
const bool first_call, struct chain_topology *topo)
|
|
|
|
{
|
|
|
|
if (!streq(chain, chainparams->bip70_name))
|
|
|
|
fatal("Wrong network! Our Bitcoin backend is running on '%s',"
|
|
|
|
" but we expect '%s'.", chain, chainparams->bip70_name);
|
|
|
|
|
2020-08-03 13:26:44 +02:00
|
|
|
topo->headercount = headercount;
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
if (first_call) {
|
|
|
|
/* Has the Bitcoin backend gone backward ? */
|
|
|
|
check_blockcount(topo, blockcount);
|
|
|
|
/* Get up to speed with topology. */
|
|
|
|
bitcoind_getrawblockbyheight(topo->bitcoind, topo->max_blockheight,
|
|
|
|
init_topo, topo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibd) {
|
|
|
|
if (first_call)
|
|
|
|
log_unusual(bitcoind->log,
|
|
|
|
"Waiting for initial block download (this can take"
|
|
|
|
" a while!)");
|
|
|
|
else
|
|
|
|
log_debug(bitcoind->log,
|
|
|
|
"Still waiting for initial block download");
|
|
|
|
} else if (headercount != blockcount) {
|
|
|
|
if (first_call)
|
|
|
|
log_unusual(bitcoind->log,
|
|
|
|
"Waiting for bitcoind to catch up"
|
|
|
|
" (%u blocks of %u)",
|
|
|
|
blockcount, headercount);
|
|
|
|
else
|
|
|
|
log_debug(bitcoind->log,
|
|
|
|
"Waiting for bitcoind to catch up"
|
|
|
|
" (%u blocks of %u)",
|
|
|
|
blockcount, headercount);
|
|
|
|
} else {
|
|
|
|
if (!first_call)
|
|
|
|
log_unusual(bitcoind->log,
|
|
|
|
"Bitcoin backend now synced.");
|
|
|
|
bitcoind->synced = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:35:15 +02:00
|
|
|
assert(!bitcoind->checkchain_timer);
|
|
|
|
bitcoind->checkchain_timer
|
|
|
|
= new_reltimer(bitcoind->ld->timers, bitcoind,
|
|
|
|
/* Be 4x more aggressive in this case. */
|
|
|
|
time_divide(time_from_sec(bitcoind->ld->topology
|
|
|
|
->poll_seconds), 4),
|
|
|
|
retry_check_chain, bitcoind->ld->topology);
|
|
|
|
}
|
2020-01-08 17:53:31 +01:00
|
|
|
|
|
|
|
static void retry_check_chain(struct chain_topology *topo)
|
|
|
|
{
|
2021-09-01 06:35:15 +02:00
|
|
|
topo->bitcoind->checkchain_timer = NULL;
|
2021-09-08 03:37:01 +02:00
|
|
|
if (topo->stopping)
|
|
|
|
return;
|
2020-01-09 12:25:45 +01:00
|
|
|
bitcoind_getchaininfo(topo->bitcoind, false, check_chain, topo);
|
2020-01-08 17:53:31 +01:00
|
|
|
}
|
|
|
|
|
2017-08-28 18:09:01 +02:00
|
|
|
void setup_topology(struct chain_topology *topo,
|
2018-06-04 15:00:05 +02:00
|
|
|
u32 min_blockheight, u32 max_blockheight)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
2022-06-26 06:51:01 +02:00
|
|
|
void *ret;
|
2018-02-16 00:07:04 +01:00
|
|
|
|
2018-06-04 15:00:05 +02:00
|
|
|
topo->min_blockheight = min_blockheight;
|
|
|
|
topo->max_blockheight = max_blockheight;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2020-01-07 11:59:18 +01:00
|
|
|
/* This waits for bitcoind. */
|
|
|
|
bitcoind_check_commands(topo->bitcoind);
|
|
|
|
|
2020-02-06 19:00:14 +01:00
|
|
|
/* For testing.. */
|
|
|
|
log_debug(topo->ld->log, "All Bitcoin plugin commands registered");
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
/* Sanity checks, then topology initialization. */
|
2021-09-01 06:35:15 +02:00
|
|
|
topo->bitcoind->checkchain_timer = NULL;
|
2020-01-08 17:53:31 +01:00
|
|
|
bitcoind_getchaininfo(topo->bitcoind, true, check_chain, topo);
|
2016-08-09 05:41:22 +02:00
|
|
|
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_add_destructor(topo, destroy_chain_topology);
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2018-08-22 04:33:32 +02:00
|
|
|
start_fee_estimate(topo);
|
|
|
|
|
2018-01-03 06:26:44 +01:00
|
|
|
/* Once it gets initial block, it calls io_break() and we return. */
|
2022-06-26 06:51:01 +02:00
|
|
|
ret = io_loop_with_timers(topo->ld);
|
|
|
|
assert(ret == topo);
|
|
|
|
log_debug(topo->ld->log, "io_loop_with_timers: %s", __func__);
|
2018-01-03 06:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void begin_topology(struct chain_topology *topo)
|
|
|
|
{
|
|
|
|
try_extend_tip(topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
2021-09-01 06:35:15 +02:00
|
|
|
|
|
|
|
void stop_topology(struct chain_topology *topo)
|
|
|
|
{
|
2021-09-08 03:37:01 +02:00
|
|
|
/* Stop timers from re-arming. */
|
|
|
|
topo->stopping = true;
|
|
|
|
|
2021-09-01 06:35:15 +02:00
|
|
|
/* Remove timers while we're cleaning up plugins. */
|
|
|
|
tal_free(topo->bitcoind->checkchain_timer);
|
|
|
|
tal_free(topo->extend_timer);
|
|
|
|
tal_free(topo->updatefee_timer);
|
|
|
|
}
|