2016-01-21 21:11:49 +01:00
|
|
|
/* Code for talking to bitcoind. We use bitcoin-cli. */
|
|
|
|
#include "bitcoin/base58.h"
|
2016-04-24 12:10:29 +02:00
|
|
|
#include "bitcoin/block.h"
|
2018-10-22 21:36:08 +02:00
|
|
|
#include "bitcoin/feerate.h"
|
2019-08-05 18:33:08 +02:00
|
|
|
#include "bitcoin/script.h"
|
2016-01-21 21:11:49 +01:00
|
|
|
#include "bitcoin/shadouble.h"
|
|
|
|
#include "bitcoind.h"
|
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
2020-01-07 11:59:18 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/pipecmd/pipecmd.h>
|
|
|
|
#include <ccan/str/hex/hex.h>
|
2020-01-07 11:59:18 +01:00
|
|
|
#include <ccan/str/str.h>
|
2016-04-12 06:47:46 +02:00
|
|
|
#include <ccan/take/take.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <ccan/tal/grab_file/grab_file.h>
|
|
|
|
#include <ccan/tal/path/path.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-01-15 04:54:27 +01:00
|
|
|
#include <common/json_helpers.h>
|
2017-12-15 11:22:57 +01:00
|
|
|
#include <common/memleak.h>
|
2018-02-24 06:11:18 +01:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <errno.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
#include <inttypes.h>
|
2018-01-04 12:40:58 +01:00
|
|
|
#include <lightningd/chaintopology.h>
|
2020-01-07 11:59:18 +01:00
|
|
|
#include <lightningd/plugin.h>
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
/* Bitcoind's web server has a default of 4 threads, with queue depth 16.
|
|
|
|
* It will *fail* rather than queue beyond that, so we must not stress it!
|
|
|
|
*
|
|
|
|
* This is how many request for each priority level we have.
|
|
|
|
*/
|
|
|
|
#define BITCOIND_MAX_PARALLEL 4
|
|
|
|
|
2020-01-07 11:59:18 +01:00
|
|
|
/* The names of the request we can make to our Bitcoin backend. */
|
|
|
|
static const char *methods[] = {"getchaininfo", "getrawblockbyheight",
|
|
|
|
"sendrawtransaction", "getutxout",
|
|
|
|
"getfeerate"};
|
|
|
|
|
2020-01-07 18:08:03 +01:00
|
|
|
static void plugin_config_cb(const char *buffer,
|
|
|
|
const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
plugin->plugin_state = CONFIGURED;
|
|
|
|
io_break(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_plugin(struct plugin *plugin)
|
|
|
|
{
|
|
|
|
struct jsonrpc_request *req;
|
|
|
|
|
|
|
|
req = jsonrpc_request_start(plugin, "init", plugin->log,
|
|
|
|
plugin_config_cb, plugin);
|
|
|
|
plugin_populate_init_request(plugin, req);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
plugin_request_send(plugin, req);
|
|
|
|
io_loop_with_timers(plugin->plugins->ld);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_plugin(struct bitcoind *bitcoind, const char *method,
|
|
|
|
struct plugin *p)
|
|
|
|
{
|
|
|
|
/* We need our Bitcoin backend to be initialized, but the plugins have
|
|
|
|
* not yet been started at this point.
|
|
|
|
* So send `init` to each plugin which registered for a Bitcoin method
|
|
|
|
* and wait for its response, which we take as an ACK that it is
|
|
|
|
* operational (i.e. bcli will wait for `bitcoind` to be warmed up
|
|
|
|
* before responding to `init`).
|
|
|
|
* Note that lightningd/plugin will not send `init` to an already
|
|
|
|
* configured plugin. */
|
|
|
|
if (p->plugin_state != CONFIGURED)
|
|
|
|
config_plugin(p);
|
|
|
|
strmap_add(&bitcoind->pluginsmap, method, p);
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:59:18 +01:00
|
|
|
void bitcoind_check_commands(struct bitcoind *bitcoind)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
struct plugin *p;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(methods); i++) {
|
|
|
|
p = find_plugin_for_command(bitcoind->ld, methods[i]);
|
|
|
|
if (p == NULL) {
|
|
|
|
fatal("Could not access the plugin for %s, is a "
|
|
|
|
"Bitcoin plugin (by default plugins/bcli) "
|
|
|
|
"registered ?", methods[i]);
|
|
|
|
}
|
2020-01-07 18:08:03 +01:00
|
|
|
wait_plugin(bitcoind, methods[i], p);
|
2020-01-07 11:59:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
/* Add the n'th arg to *args, incrementing n and keeping args of size n+1 */
|
|
|
|
static void add_arg(const char ***args, const char *arg)
|
2016-01-21 21:11:49 +01:00
|
|
|
{
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(args, arg);
|
2018-01-31 04:11:36 +01:00
|
|
|
}
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
static const char **gather_args(const struct bitcoind *bitcoind,
|
|
|
|
const tal_t *ctx, const char *cmd, va_list ap)
|
|
|
|
{
|
|
|
|
const char **args = tal_arr(ctx, const char *, 1);
|
|
|
|
const char *arg;
|
2017-07-10 10:59:15 +02:00
|
|
|
|
2019-10-15 12:58:30 +02:00
|
|
|
args[0] = bitcoind->cli ? bitcoind->cli : chainparams->cli;
|
|
|
|
if (chainparams->cli_args)
|
|
|
|
add_arg(&args, chainparams->cli_args);
|
2018-01-30 15:17:58 +01:00
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
if (bitcoind->datadir)
|
|
|
|
add_arg(&args, tal_fmt(args, "-datadir=%s", bitcoind->datadir));
|
2018-01-30 15:17:58 +01:00
|
|
|
|
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
if (bitcoind->rpcconnect)
|
|
|
|
add_arg(&args,
|
|
|
|
tal_fmt(args, "-rpcconnect=%s", bitcoind->rpcconnect));
|
2018-01-30 15:17:58 +01:00
|
|
|
|
2018-03-25 13:57:52 +02:00
|
|
|
if (bitcoind->rpcport)
|
|
|
|
add_arg(&args,
|
|
|
|
tal_fmt(args, "-rpcport=%s", bitcoind->rpcport));
|
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
if (bitcoind->rpcuser)
|
|
|
|
add_arg(&args, tal_fmt(args, "-rpcuser=%s", bitcoind->rpcuser));
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2018-01-31 04:11:36 +01:00
|
|
|
if (bitcoind->rpcpass)
|
|
|
|
add_arg(&args,
|
|
|
|
tal_fmt(args, "-rpcpassword=%s", bitcoind->rpcpass));
|
|
|
|
|
|
|
|
add_arg(&args, cmd);
|
|
|
|
|
|
|
|
while ((arg = va_arg(ap, const char *)) != NULL)
|
|
|
|
add_arg(&args, tal_strdup(args, arg));
|
|
|
|
|
|
|
|
add_arg(&args, NULL);
|
2016-01-21 21:11:49 +01:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bitcoin_cli {
|
2016-01-21 21:11:49 +01:00
|
|
|
struct list_node list;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct bitcoind *bitcoind;
|
2016-01-21 21:11:49 +01:00
|
|
|
int fd;
|
2016-05-04 08:33:10 +02:00
|
|
|
int *exitstatus;
|
2016-01-21 21:11:49 +01:00
|
|
|
pid_t pid;
|
2018-01-31 04:11:36 +01:00
|
|
|
const char **args;
|
2018-09-19 04:32:36 +02:00
|
|
|
struct timeabs start;
|
2018-09-19 04:45:25 +02:00
|
|
|
enum bitcoind_prio prio;
|
2016-01-21 21:11:49 +01:00
|
|
|
char *output;
|
|
|
|
size_t output_bytes;
|
|
|
|
size_t new_output;
|
2018-02-24 06:11:18 +01:00
|
|
|
bool (*process)(struct bitcoin_cli *);
|
2016-01-21 21:11:49 +01:00
|
|
|
void *cb;
|
|
|
|
void *cb_arg;
|
2016-11-07 13:27:02 +01:00
|
|
|
struct bitcoin_cli **stopper;
|
2016-01-21 21:11:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct io_plan *read_more(struct io_conn *conn, struct bitcoin_cli *bcli)
|
|
|
|
{
|
|
|
|
bcli->output_bytes += bcli->new_output;
|
|
|
|
if (bcli->output_bytes == tal_count(bcli->output))
|
|
|
|
tal_resize(&bcli->output, bcli->output_bytes * 2);
|
|
|
|
return io_read_partial(conn, bcli->output + bcli->output_bytes,
|
|
|
|
tal_count(bcli->output) - bcli->output_bytes,
|
|
|
|
&bcli->new_output, read_more, bcli);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *output_init(struct io_conn *conn, struct bitcoin_cli *bcli)
|
|
|
|
{
|
|
|
|
bcli->output_bytes = bcli->new_output = 0;
|
|
|
|
bcli->output = tal_arr(bcli, char, 100);
|
|
|
|
return read_more(conn, bcli);
|
|
|
|
}
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
static void next_bcli(struct bitcoind *bitcoind, enum bitcoind_prio prio);
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2019-11-05 07:21:12 +01:00
|
|
|
/* For printing: simple string of args (no secrets!) */
|
|
|
|
static char *args_string(const tal_t *ctx, const char **args)
|
2016-04-12 06:47:46 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
2019-06-30 11:00:48 +02:00
|
|
|
char *ret = tal_strdup(ctx, args[0]);
|
2016-04-12 06:47:46 +02:00
|
|
|
|
2019-06-30 11:00:48 +02:00
|
|
|
for (i = 1; args[i]; i++) {
|
2019-04-03 05:06:06 +02:00
|
|
|
ret = tal_strcat(ctx, take(ret), " ");
|
2019-06-30 11:00:48 +02:00
|
|
|
if (strstarts(args[i], "-rpcpassword")) {
|
2019-04-03 05:06:06 +02:00
|
|
|
ret = tal_strcat(ctx, take(ret), "-rpcpassword=...");
|
2019-06-30 11:00:48 +02:00
|
|
|
} else if (strstarts(args[i], "-rpcuser")) {
|
2019-04-03 05:06:06 +02:00
|
|
|
ret = tal_strcat(ctx, take(ret), "-rpcuser=...");
|
|
|
|
} else {
|
2019-06-30 11:00:48 +02:00
|
|
|
ret = tal_strcat(ctx, take(ret), args[i]);
|
2019-04-03 05:06:06 +02:00
|
|
|
}
|
2016-04-12 06:47:46 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2019-06-30 11:00:48 +02:00
|
|
|
static char *bcli_args(const tal_t *ctx, struct bitcoin_cli *bcli)
|
|
|
|
{
|
2019-11-05 07:21:12 +01:00
|
|
|
return args_string(ctx, bcli->args);
|
2019-06-30 11:00:48 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:22 +01:00
|
|
|
static void retry_bcli(struct bitcoin_cli *bcli)
|
|
|
|
{
|
2018-09-19 04:45:25 +02:00
|
|
|
list_add_tail(&bcli->bitcoind->pending[bcli->prio], &bcli->list);
|
|
|
|
next_bcli(bcli->bitcoind, bcli->prio);
|
2018-02-24 06:11:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
/* We allow 60 seconds of spurious errors, eg. reorg. */
|
|
|
|
static void bcli_failure(struct bitcoind *bitcoind,
|
|
|
|
struct bitcoin_cli *bcli,
|
|
|
|
int exitstatus)
|
|
|
|
{
|
|
|
|
struct timerel t;
|
|
|
|
|
|
|
|
if (!bitcoind->error_count)
|
|
|
|
bitcoind->first_error_time = time_mono();
|
|
|
|
|
|
|
|
t = timemono_between(time_mono(), bitcoind->first_error_time);
|
2019-06-29 21:12:15 +02:00
|
|
|
if (time_greater(t, time_from_sec(bitcoind->retry_timeout)))
|
|
|
|
fatal("%s exited %u (after %u other errors) '%.*s'; "
|
|
|
|
"we have been retrying command for "
|
|
|
|
"--bitcoin-retry-timeout=%"PRIu64" seconds; "
|
|
|
|
"bitcoind setup or our --bitcoin-* configs broken?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
2018-02-24 06:11:18 +01:00
|
|
|
exitstatus,
|
|
|
|
bitcoind->error_count,
|
|
|
|
(int)bcli->output_bytes,
|
2019-06-29 21:12:15 +02:00
|
|
|
bcli->output,
|
|
|
|
bitcoind->retry_timeout);
|
2018-02-24 06:11:18 +01:00
|
|
|
|
|
|
|
log_unusual(bitcoind->log,
|
2018-09-19 04:31:36 +02:00
|
|
|
"%s exited with status %u",
|
|
|
|
bcli_args(tmpctx, bcli), exitstatus);
|
2018-02-24 06:11:18 +01:00
|
|
|
|
|
|
|
bitcoind->error_count++;
|
2018-02-24 06:11:22 +01:00
|
|
|
|
2018-06-15 03:12:14 +02:00
|
|
|
/* Retry in 1 second (not a leak!) */
|
2019-11-21 05:13:37 +01:00
|
|
|
notleak(new_reltimer(bitcoind->ld->timers, notleak(bcli),
|
|
|
|
time_from_sec(1),
|
|
|
|
retry_bcli, bcli));
|
2018-02-24 06:11:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void bcli_finished(struct io_conn *conn UNUSED, struct bitcoin_cli *bcli)
|
2016-01-21 21:11:49 +01:00
|
|
|
{
|
|
|
|
int ret, status;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct bitcoind *bitcoind = bcli->bitcoind;
|
2018-09-19 04:45:25 +02:00
|
|
|
enum bitcoind_prio prio = bcli->prio;
|
2018-02-24 06:11:18 +01:00
|
|
|
bool ok;
|
2018-10-10 01:06:33 +02:00
|
|
|
u64 msec = time_to_msec(time_between(time_now(), bcli->start));
|
|
|
|
|
|
|
|
/* If it took over 10 seconds, that's rather strange. */
|
|
|
|
if (msec > 10000)
|
|
|
|
log_unusual(bitcoind->log,
|
|
|
|
"bitcoin-cli: finished %s (%"PRIu64" ms)",
|
|
|
|
bcli_args(tmpctx, bcli), msec);
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
assert(bitcoind->num_requests[prio] > 0);
|
2018-09-19 04:32:36 +02:00
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
/* FIXME: If we waited for SIGCHILD, this could never hang! */
|
2018-04-05 09:53:12 +02:00
|
|
|
while ((ret = waitpid(bcli->pid, &status, 0)) < 0 && errno == EINTR);
|
2016-01-21 21:11:49 +01:00
|
|
|
if (ret != bcli->pid)
|
2018-09-19 04:31:36 +02:00
|
|
|
fatal("%s %s", bcli_args(tmpctx, bcli),
|
2016-01-21 21:11:49 +01:00
|
|
|
ret == 0 ? "not exited?" : strerror(errno));
|
|
|
|
|
|
|
|
if (!WIFEXITED(status))
|
2016-04-12 06:47:46 +02:00
|
|
|
fatal("%s died with signal %i",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
2016-01-21 21:11:49 +01:00
|
|
|
WTERMSIG(status));
|
|
|
|
|
2016-05-04 08:33:10 +02:00
|
|
|
if (!bcli->exitstatus) {
|
|
|
|
if (WEXITSTATUS(status) != 0) {
|
2018-02-24 06:11:18 +01:00
|
|
|
bcli_failure(bitcoind, bcli, WEXITSTATUS(status));
|
2018-09-19 04:45:25 +02:00
|
|
|
bitcoind->num_requests[prio]--;
|
2018-01-17 21:29:50 +01:00
|
|
|
goto done;
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
*bcli->exitstatus = WEXITSTATUS(status);
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2017-09-28 05:39:05 +02:00
|
|
|
if (WEXITSTATUS(status) == 0)
|
|
|
|
bitcoind->error_count = 0;
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
bitcoind->num_requests[bcli->prio]--;
|
2017-09-12 06:55:54 +02:00
|
|
|
|
|
|
|
/* Don't continue if were only here because we were freed for shutdown */
|
|
|
|
if (bitcoind->shutdown)
|
|
|
|
return;
|
|
|
|
|
2017-11-01 02:10:48 +01:00
|
|
|
db_begin_transaction(bitcoind->ld->wallet->db);
|
2018-02-24 06:11:18 +01:00
|
|
|
ok = bcli->process(bcli);
|
2017-11-01 02:10:48 +01:00
|
|
|
db_commit_transaction(bitcoind->ld->wallet->db);
|
2018-01-17 21:29:50 +01:00
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
if (!ok)
|
|
|
|
bcli_failure(bitcoind, bcli, WEXITSTATUS(status));
|
2018-02-24 06:11:22 +01:00
|
|
|
else
|
|
|
|
tal_free(bcli);
|
2018-02-24 06:11:18 +01:00
|
|
|
|
2018-01-17 21:29:50 +01:00
|
|
|
done:
|
2018-09-19 04:45:25 +02:00
|
|
|
next_bcli(bitcoind, prio);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
static void next_bcli(struct bitcoind *bitcoind, enum bitcoind_prio prio)
|
2016-01-21 21:11:49 +01:00
|
|
|
{
|
|
|
|
struct bitcoin_cli *bcli;
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
if (bitcoind->num_requests[prio] >= BITCOIND_MAX_PARALLEL)
|
2016-01-21 21:11:49 +01:00
|
|
|
return;
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
bcli = list_pop(&bitcoind->pending[prio], struct bitcoin_cli, list);
|
2016-01-21 21:11:49 +01:00
|
|
|
if (!bcli)
|
|
|
|
return;
|
|
|
|
|
2018-12-08 01:31:56 +01:00
|
|
|
bcli->pid = pipecmdarr(NULL, &bcli->fd, &bcli->fd,
|
2018-01-31 04:11:36 +01:00
|
|
|
cast_const2(char **, bcli->args));
|
2016-01-21 21:11:49 +01:00
|
|
|
if (bcli->pid < 0)
|
|
|
|
fatal("%s exec failed: %s", bcli->args[0], strerror(errno));
|
|
|
|
|
2018-09-19 04:32:36 +02:00
|
|
|
bcli->start = time_now();
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
bitcoind->num_requests[prio]++;
|
|
|
|
|
2017-12-15 11:22:57 +01:00
|
|
|
/* This lifetime is attached to bitcoind command fd */
|
|
|
|
conn = notleak(io_new_conn(bitcoind, bcli->fd, output_init, bcli));
|
2016-01-21 21:11:49 +01:00
|
|
|
io_set_finish(conn, bcli_finished, bcli);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_donothing(struct bitcoin_cli *bcli UNUSED)
|
2016-11-07 13:27:02 +01:00
|
|
|
{
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2016-11-07 13:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If stopper gets freed first, set process() to a noop. */
|
|
|
|
static void stop_process_bcli(struct bitcoin_cli **stopper)
|
|
|
|
{
|
|
|
|
(*stopper)->process = process_donothing;
|
|
|
|
(*stopper)->stopper = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It command finishes first, free stopper. */
|
|
|
|
static void remove_stopper(struct bitcoin_cli *bcli)
|
|
|
|
{
|
|
|
|
/* Calls stop_process_bcli, but we don't care. */
|
|
|
|
tal_free(bcli->stopper);
|
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
/* If ctx is non-NULL, and is freed before we return, we don't call process().
|
|
|
|
* process returns false() if it's a spurious error, and we should retry. */
|
2016-01-21 21:11:49 +01:00
|
|
|
static void
|
2017-03-02 13:21:49 +01:00
|
|
|
start_bitcoin_cli(struct bitcoind *bitcoind,
|
2016-11-07 13:27:02 +01:00
|
|
|
const tal_t *ctx,
|
2018-02-24 06:11:18 +01:00
|
|
|
bool (*process)(struct bitcoin_cli *),
|
2016-05-04 08:33:10 +02:00
|
|
|
bool nonzero_exit_ok,
|
2018-09-19 04:45:25 +02:00
|
|
|
enum bitcoind_prio prio,
|
2016-01-21 21:11:49 +01:00
|
|
|
void *cb, void *cb_arg,
|
|
|
|
char *cmd, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct bitcoin_cli *bcli = tal(bitcoind, struct bitcoin_cli);
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
bcli->bitcoind = bitcoind;
|
2016-01-21 21:11:49 +01:00
|
|
|
bcli->process = process;
|
2018-09-19 04:45:25 +02:00
|
|
|
bcli->prio = prio;
|
2016-01-21 21:11:49 +01:00
|
|
|
bcli->cb = cb;
|
|
|
|
bcli->cb_arg = cb_arg;
|
2016-11-07 13:27:02 +01:00
|
|
|
if (ctx) {
|
|
|
|
/* Create child whose destructor will stop us calling */
|
|
|
|
bcli->stopper = tal(ctx, struct bitcoin_cli *);
|
|
|
|
*bcli->stopper = bcli;
|
|
|
|
tal_add_destructor(bcli->stopper, stop_process_bcli);
|
|
|
|
tal_add_destructor(bcli, remove_stopper);
|
|
|
|
} else
|
|
|
|
bcli->stopper = NULL;
|
|
|
|
|
2016-05-04 08:33:10 +02:00
|
|
|
if (nonzero_exit_ok)
|
|
|
|
bcli->exitstatus = tal(bcli, int);
|
|
|
|
else
|
|
|
|
bcli->exitstatus = NULL;
|
2016-01-21 21:11:49 +01:00
|
|
|
va_start(ap, cmd);
|
2017-03-02 13:21:49 +01:00
|
|
|
bcli->args = gather_args(bitcoind, bcli, cmd, ap);
|
2016-01-21 21:11:49 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
list_add_tail(&bitcoind->pending[bcli->prio], &bcli->list);
|
|
|
|
next_bcli(bitcoind, bcli->prio);
|
2016-01-21 21:11:49 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 04:26:39 +01:00
|
|
|
static bool extract_feerate(struct bitcoin_cli *bcli,
|
|
|
|
const char *output, size_t output_bytes,
|
2018-08-22 03:28:49 +02:00
|
|
|
u64 *feerate)
|
2016-04-12 05:37:03 +02:00
|
|
|
{
|
2017-11-21 04:26:39 +01:00
|
|
|
const jsmntok_t *tokens, *feeratetok;
|
|
|
|
bool valid;
|
2016-04-12 05:37:03 +02:00
|
|
|
|
2018-12-08 01:37:56 +01:00
|
|
|
tokens = json_parse_input(output, output, output_bytes, &valid);
|
2017-11-21 04:26:39 +01:00
|
|
|
if (!tokens)
|
|
|
|
fatal("%s: %s response",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
2017-11-21 04:26:39 +01:00
|
|
|
valid ? "partial" : "invalid");
|
|
|
|
|
2018-01-23 16:08:51 +01:00
|
|
|
if (tokens[0].type != JSMN_OBJECT) {
|
|
|
|
log_unusual(bcli->bitcoind->log,
|
|
|
|
"%s: gave non-object (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
2018-01-23 16:08:51 +01:00
|
|
|
(int)output_bytes, output);
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-12 05:37:03 +02:00
|
|
|
|
2017-11-21 04:26:39 +01:00
|
|
|
feeratetok = json_get_member(output, tokens, "feerate");
|
|
|
|
if (!feeratetok)
|
|
|
|
return false;
|
|
|
|
|
2018-12-08 01:38:56 +01:00
|
|
|
return json_to_bitcoin_amount(output, feeratetok, feerate);
|
2016-04-12 05:37:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 04:30:29 +01:00
|
|
|
struct estimatefee {
|
|
|
|
size_t i;
|
|
|
|
const u32 *blocks;
|
|
|
|
const char **estmode;
|
|
|
|
|
2017-11-21 04:33:22 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind, const u32 satoshi_per_kw[],
|
2017-11-21 04:30:29 +01:00
|
|
|
void *);
|
|
|
|
void *arg;
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 *satoshi_per_kw;
|
2017-11-21 04:30:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void do_one_estimatefee(struct bitcoind *bitcoind,
|
|
|
|
struct estimatefee *efee);
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_estimatefee(struct bitcoin_cli *bcli)
|
2016-07-19 05:22:18 +02:00
|
|
|
{
|
2018-08-22 03:28:49 +02:00
|
|
|
u64 feerate;
|
2017-11-21 04:30:29 +01:00
|
|
|
struct estimatefee *efee = bcli->cb_arg;
|
2016-07-19 05:22:18 +02:00
|
|
|
|
2017-11-21 04:26:39 +01:00
|
|
|
/* FIXME: We could trawl recent blocks for median fee... */
|
|
|
|
if (!extract_feerate(bcli, bcli->output, bcli->output_bytes, &feerate)) {
|
2017-11-21 04:30:29 +01:00
|
|
|
log_unusual(bcli->bitcoind->log, "Unable to estimate %s/%u fee",
|
|
|
|
efee->estmode[efee->i], efee->blocks[efee->i]);
|
2018-10-22 21:36:08 +02:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* This is needed to test for failed feerate estimates
|
|
|
|
* in DEVELOPER mode */
|
2017-11-21 04:30:29 +01:00
|
|
|
efee->satoshi_per_kw[efee->i] = 0;
|
2018-10-22 21:36:08 +02:00
|
|
|
#else
|
|
|
|
/* If we are in testnet mode we want to allow payments
|
|
|
|
* with the minimal fee even if the estimate didn't
|
|
|
|
* work out. This is less disruptive than erring out
|
|
|
|
* all the time. */
|
2019-10-15 12:58:30 +02:00
|
|
|
if (chainparams->testnet)
|
2018-10-22 21:36:08 +02:00
|
|
|
efee->satoshi_per_kw[efee->i] = FEERATE_FLOOR;
|
|
|
|
else
|
|
|
|
efee->satoshi_per_kw[efee->i] = 0;
|
|
|
|
#endif
|
2017-11-21 04:26:39 +01:00
|
|
|
} else
|
2017-11-21 04:27:47 +01:00
|
|
|
/* Rate in satoshi per kw. */
|
2018-08-27 07:11:39 +02:00
|
|
|
efee->satoshi_per_kw[efee->i]
|
|
|
|
= feerate_from_style(feerate, FEERATE_PER_KBYTE);
|
2017-11-21 04:30:29 +01:00
|
|
|
|
|
|
|
efee->i++;
|
|
|
|
if (efee->i == tal_count(efee->satoshi_per_kw)) {
|
|
|
|
efee->cb(bcli->bitcoind, efee->satoshi_per_kw, efee->arg);
|
|
|
|
tal_free(efee);
|
|
|
|
} else {
|
|
|
|
/* Next */
|
|
|
|
do_one_estimatefee(bcli->bitcoind, efee);
|
|
|
|
}
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2017-11-21 04:30:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_one_estimatefee(struct bitcoind *bitcoind,
|
|
|
|
struct estimatefee *efee)
|
|
|
|
{
|
|
|
|
char blockstr[STR_MAX_CHARS(u32)];
|
2017-11-21 04:26:39 +01:00
|
|
|
|
2018-07-31 14:56:04 +02:00
|
|
|
snprintf(blockstr, sizeof(blockstr), "%u", efee->blocks[efee->i]);
|
2018-09-19 04:45:25 +02:00
|
|
|
start_bitcoin_cli(bitcoind, NULL, process_estimatefee, false,
|
|
|
|
BITCOIND_LOW_PRIO,
|
2018-09-19 04:30:36 +02:00
|
|
|
NULL, efee,
|
2017-11-21 04:30:29 +01:00
|
|
|
"estimatesmartfee", blockstr, efee->estmode[efee->i],
|
|
|
|
NULL);
|
2016-07-19 05:22:18 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 04:30:29 +01:00
|
|
|
void bitcoind_estimate_fees_(struct bitcoind *bitcoind,
|
|
|
|
const u32 blocks[], const char *estmode[],
|
|
|
|
size_t num_estimates,
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2017-11-21 04:33:22 +01:00
|
|
|
const u32 satoshi_per_kw[], void *),
|
2017-11-21 04:30:29 +01:00
|
|
|
void *arg)
|
2016-04-12 05:37:03 +02:00
|
|
|
{
|
2017-11-21 04:30:29 +01:00
|
|
|
struct estimatefee *efee = tal(bitcoind, struct estimatefee);
|
|
|
|
|
|
|
|
efee->i = 0;
|
|
|
|
efee->blocks = tal_dup_arr(efee, u32, blocks, num_estimates, 0);
|
|
|
|
efee->estmode = tal_dup_arr(efee, const char *, estmode, num_estimates,
|
|
|
|
0);
|
|
|
|
efee->cb = cb;
|
|
|
|
efee->arg = arg;
|
2017-11-21 04:33:22 +01:00
|
|
|
efee->satoshi_per_kw = tal_arr(efee, u32, num_estimates);
|
2017-11-21 04:30:29 +01:00
|
|
|
|
|
|
|
do_one_estimatefee(bitcoind, efee);
|
2016-04-12 05:37:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_sendrawtx(struct bitcoin_cli *bcli)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2016-11-07 13:30:02 +01:00
|
|
|
int, const char *msg, void *) = bcli->cb;
|
2018-02-21 11:29:57 +01:00
|
|
|
const char *msg = tal_strndup(bcli, bcli->output,
|
2016-05-04 08:33:10 +02:00
|
|
|
bcli->output_bytes);
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
log_debug(bcli->bitcoind->log, "sendrawtx exit %u, gave %s",
|
2016-05-04 08:33:10 +02:00
|
|
|
*bcli->exitstatus, msg);
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
cb(bcli->bitcoind, *bcli->exitstatus, msg, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 15:34:43 +02:00
|
|
|
void bitcoind_sendrawtx_(struct bitcoind *bitcoind,
|
2016-05-04 08:33:10 +02:00
|
|
|
const char *hextx,
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2016-11-07 13:30:02 +01:00
|
|
|
int exitstatus, const char *msg, void *),
|
2016-05-04 08:33:10 +02:00
|
|
|
void *arg)
|
|
|
|
{
|
2017-05-18 13:31:34 +02:00
|
|
|
log_debug(bitcoind->log, "sendrawtransaction: %s", hextx);
|
2018-09-19 04:45:25 +02:00
|
|
|
start_bitcoin_cli(bitcoind, NULL, process_sendrawtx, true,
|
|
|
|
BITCOIND_HIGH_PRIO,
|
2018-09-19 04:30:36 +02:00
|
|
|
cb, arg,
|
2016-05-04 08:33:10 +02:00
|
|
|
"sendrawtransaction", hextx, NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_rawblock(struct bitcoin_cli *bcli)
|
2016-04-24 12:10:29 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_block *blk;
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2016-04-24 12:10:29 +02:00
|
|
|
struct bitcoin_block *blk,
|
|
|
|
void *arg) = bcli->cb;
|
|
|
|
|
2019-10-15 12:58:30 +02:00
|
|
|
blk = bitcoin_block_from_hex(bcli, chainparams,
|
2019-07-30 19:51:53 +02:00
|
|
|
bcli->output, bcli->output_bytes);
|
2016-04-24 12:10:29 +02:00
|
|
|
if (!blk)
|
|
|
|
fatal("%s: bad block '%.*s'?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
2018-02-21 11:29:57 +01:00
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2016-04-24 12:10:29 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
cb(bcli->bitcoind, blk, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2016-04-24 12:10:29 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void bitcoind_getrawblock_(struct bitcoind *bitcoind,
|
2017-12-18 07:44:10 +01:00
|
|
|
const struct bitcoin_blkid *blockid,
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2016-04-24 12:10:29 +02:00
|
|
|
struct bitcoin_block *blk,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
char hex[hex_str_size(sizeof(*blockid))];
|
|
|
|
|
2016-04-24 12:11:20 +02:00
|
|
|
bitcoin_blkid_to_hex(blockid, hex, sizeof(hex));
|
2018-09-19 04:45:25 +02:00
|
|
|
start_bitcoin_cli(bitcoind, NULL, process_rawblock, false,
|
|
|
|
BITCOIND_HIGH_PRIO,
|
2018-09-19 04:30:36 +02:00
|
|
|
cb, arg,
|
2016-04-24 12:10:29 +02:00
|
|
|
"getblock", hex, "false", NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
/* Our Bitcoin backend plugin gave us a bad response. We can't recover. */
|
|
|
|
static void bitcoin_plugin_error(struct bitcoind *bitcoind, const char *buf,
|
|
|
|
const jsmntok_t *toks, const char *method,
|
|
|
|
const char *reason)
|
2016-04-24 12:06:13 +02:00
|
|
|
{
|
2020-01-08 17:53:31 +01:00
|
|
|
struct plugin *p = strmap_get(&bitcoind->pluginsmap, method);
|
|
|
|
fatal("%s error: bad response to %s (%s), response was %.*s",
|
|
|
|
p->cmd, method, reason,
|
|
|
|
toks->end - toks->start, buf + toks->start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* `getrawblockbyheight`
|
|
|
|
*
|
|
|
|
* If no block were found at that height, will set each field to `null`.
|
|
|
|
* Plugin response:
|
|
|
|
* {
|
|
|
|
* "blockhash": "<blkid>",
|
|
|
|
* "block": "rawblock"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct getrawblockbyheight_call {
|
|
|
|
struct bitcoind *bitcoind;
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2020-01-08 17:53:31 +01:00
|
|
|
struct bitcoin_blkid *blkid,
|
|
|
|
struct bitcoin_block *block,
|
|
|
|
void *);
|
|
|
|
void *cb_arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
getrawblockbyheight_callback(const char *buf, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct getrawblockbyheight_call *call)
|
|
|
|
{
|
|
|
|
const jsmntok_t *resulttok, *blockhashtok, *blocktok;
|
|
|
|
const char *block_str, *blockhash_str;
|
|
|
|
struct bitcoin_blkid blkid;
|
|
|
|
struct bitcoin_block *blk;
|
2016-04-24 12:06:13 +02:00
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
resulttok = json_get_member(buf, toks, "result");
|
|
|
|
if (!resulttok)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks,
|
|
|
|
"getrawblockbyheight",
|
|
|
|
"bad 'result' field");
|
|
|
|
|
|
|
|
blockhashtok = json_get_member(buf, resulttok, "blockhash");
|
|
|
|
if (!blockhashtok)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks,
|
|
|
|
"getrawblockbyheight",
|
|
|
|
"bad 'blockhash' field");
|
|
|
|
/* If block hash is `null`, this means not found! Call the callback
|
|
|
|
* with NULL values. */
|
|
|
|
if (json_tok_is_null(buf, blockhashtok)) {
|
|
|
|
db_begin_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
call->cb(call->bitcoind, NULL, NULL, call->cb_arg);
|
|
|
|
db_commit_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
blockhash_str = json_strdup(tmpctx, buf, blockhashtok);
|
|
|
|
if (!bitcoin_blkid_from_hex(blockhash_str, strlen(blockhash_str), &blkid))
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks,
|
|
|
|
"getrawblockbyheight",
|
|
|
|
"bad block hash");
|
|
|
|
|
|
|
|
blocktok = json_get_member(buf, resulttok, "block");
|
|
|
|
if (!blocktok)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks,
|
|
|
|
"getrawblockbyheight",
|
|
|
|
"bad 'block' field");
|
|
|
|
block_str = json_strdup(tmpctx, buf, blocktok);
|
|
|
|
blk = bitcoin_block_from_hex(tmpctx, chainparams, block_str,
|
|
|
|
strlen(block_str));
|
|
|
|
if (!blk)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks,
|
|
|
|
"getrawblockbyheight",
|
|
|
|
"bad block");
|
2016-04-24 12:06:13 +02:00
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
db_begin_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
call->cb(call->bitcoind, &blkid, blk, call->cb_arg);
|
|
|
|
db_commit_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
|
|
|
|
clean:
|
|
|
|
tal_free(call);
|
2016-04-24 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 17:53:31 +01:00
|
|
|
void bitcoind_getrawblockbyheight_(struct bitcoind *bitcoind,
|
|
|
|
u32 height,
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
|
|
|
struct bitcoin_blkid *blkid,
|
|
|
|
struct bitcoin_block *blk,
|
|
|
|
void *arg),
|
|
|
|
void *cb_arg)
|
2016-04-24 12:06:13 +02:00
|
|
|
{
|
2020-01-08 17:53:31 +01:00
|
|
|
struct jsonrpc_request *req;
|
|
|
|
struct getrawblockbyheight_call *call = tal(NULL,
|
|
|
|
struct getrawblockbyheight_call);
|
|
|
|
|
|
|
|
call->bitcoind = bitcoind;
|
|
|
|
call->cb = cb;
|
|
|
|
call->cb_arg = cb_arg;
|
|
|
|
|
|
|
|
req = jsonrpc_request_start(bitcoind, "getrawblockbyheight",
|
|
|
|
bitcoind->log, getrawblockbyheight_callback,
|
|
|
|
/* Freed in cb. */
|
|
|
|
notleak(call));
|
|
|
|
json_add_num(req->stream, "height", height);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
plugin_request_send(strmap_get(&bitcoind->pluginsmap,
|
|
|
|
"getrawblockbyheight"), req);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* `getchaininfo`
|
|
|
|
*
|
|
|
|
* Called at startup to check the network we are operating on, and to check
|
|
|
|
* if the Bitcoin backend is synced to the network tip. This also allows to
|
|
|
|
* get the current block count.
|
|
|
|
* {
|
|
|
|
* "chain": "<bip70_chainid>",
|
|
|
|
* "headercount": <number of fetched headers>,
|
|
|
|
* "blockcount": <number of fetched block>,
|
|
|
|
* "ibd": <synced?>
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct getchaininfo_call {
|
|
|
|
struct bitcoind *bitcoind;
|
|
|
|
/* Should we log verbosely? */
|
|
|
|
bool first_call;
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
|
|
|
const char *chain,
|
|
|
|
u32 headercount,
|
|
|
|
u32 blockcount,
|
|
|
|
const bool ibd,
|
|
|
|
const bool first_call,
|
|
|
|
void *);
|
|
|
|
void *cb_arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void getchaininfo_callback(const char *buf, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct getchaininfo_call *call)
|
|
|
|
{
|
|
|
|
const jsmntok_t *resulttok, *chaintok, *headerstok, *blktok, *ibdtok;
|
|
|
|
u32 headers = 0;
|
|
|
|
u32 blocks = 0;
|
|
|
|
bool ibd = false;
|
|
|
|
|
|
|
|
resulttok = json_get_member(buf, toks, "result");
|
|
|
|
if (!resulttok)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks, "getchaininfo",
|
|
|
|
"bad 'result' field");
|
|
|
|
|
|
|
|
chaintok = json_get_member(buf, resulttok, "chain");
|
|
|
|
if (!chaintok)
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks, "getchaininfo",
|
|
|
|
"bad 'chain' field");
|
|
|
|
|
|
|
|
headerstok = json_get_member(buf, resulttok, "headercount");
|
|
|
|
if (!headerstok || !json_to_number(buf, headerstok, &headers))
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks, "getchaininfo",
|
|
|
|
"bad 'headercount' field");
|
|
|
|
|
|
|
|
blktok = json_get_member(buf, resulttok, "blockcount");
|
|
|
|
if (!blktok || !json_to_number(buf, blktok, &blocks))
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks, "getchaininfo",
|
|
|
|
"bad 'blockcount' field");
|
|
|
|
|
|
|
|
ibdtok = json_get_member(buf, resulttok, "ibd");
|
|
|
|
if (!ibdtok || !json_to_bool(buf, ibdtok, &ibd))
|
|
|
|
bitcoin_plugin_error(call->bitcoind, buf, toks, "getchaininfo",
|
|
|
|
"bad 'ibd' field");
|
|
|
|
|
|
|
|
db_begin_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
call->cb(call->bitcoind, json_strdup(tmpctx, buf, chaintok), headers,
|
|
|
|
blocks, ibd, call->first_call, call->cb_arg);
|
|
|
|
db_commit_transaction(call->bitcoind->ld->wallet->db);
|
|
|
|
|
|
|
|
tal_free(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitcoind_getchaininfo_(struct bitcoind *bitcoind,
|
|
|
|
const bool first_call,
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
|
|
|
const char *chain,
|
|
|
|
u32 headercount,
|
|
|
|
u32 blockcount,
|
|
|
|
const bool ibd,
|
|
|
|
const bool first_call,
|
|
|
|
void *),
|
|
|
|
void *cb_arg)
|
|
|
|
{
|
|
|
|
struct jsonrpc_request *req;
|
|
|
|
struct getchaininfo_call *call = tal(bitcoind, struct getchaininfo_call);
|
|
|
|
|
|
|
|
call->bitcoind = bitcoind;
|
|
|
|
call->cb = cb;
|
|
|
|
call->cb_arg = cb_arg;
|
|
|
|
call->first_call = first_call;
|
|
|
|
|
|
|
|
req = jsonrpc_request_start(bitcoind, "getchaininfo", bitcoind->log,
|
|
|
|
getchaininfo_callback, call);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
plugin_request_send(strmap_get(&bitcoind->pluginsmap, "getchaininfo"),
|
|
|
|
req);
|
2016-04-24 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
struct get_output {
|
|
|
|
unsigned int blocknum, txnum, outnum;
|
|
|
|
|
2018-01-31 22:26:25 +01:00
|
|
|
/* The real callback */
|
|
|
|
void (*cb)(struct bitcoind *bitcoind, const struct bitcoin_tx_output *txout, void *arg);
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
/* The real callback arg */
|
|
|
|
void *cbarg;
|
|
|
|
};
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_gettxout(struct bitcoin_cli *bcli)
|
2018-01-04 12:40:58 +01:00
|
|
|
{
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
|
|
|
const struct bitcoin_tx_output *output,
|
|
|
|
void *arg) = bcli->cb;
|
|
|
|
const jsmntok_t *tokens, *valuetok, *scriptpubkeytok, *hextok;
|
|
|
|
struct bitcoin_tx_output out;
|
|
|
|
bool valid;
|
|
|
|
|
2018-01-13 22:56:38 +01:00
|
|
|
/* As of at least v0.15.1.0, bitcoind returns "success" but an empty
|
|
|
|
string on a spent gettxout */
|
|
|
|
if (*bcli->exitstatus != 0 || bcli->output_bytes == 0) {
|
2018-01-31 22:26:25 +01:00
|
|
|
cb(bcli->bitcoind, NULL, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 01:37:56 +01:00
|
|
|
tokens = json_parse_input(bcli->output, bcli->output, bcli->output_bytes,
|
|
|
|
&valid);
|
2018-01-04 12:40:58 +01:00
|
|
|
if (!tokens)
|
|
|
|
fatal("%s: %s response",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli), valid ? "partial" : "invalid");
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
if (tokens[0].type != JSMN_OBJECT)
|
|
|
|
fatal("%s: gave non-object (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
valuetok = json_get_member(bcli->output, tokens, "value");
|
|
|
|
if (!valuetok)
|
|
|
|
fatal("%s: had no value member (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2019-02-21 04:45:56 +01:00
|
|
|
if (!json_to_bitcoin_amount(bcli->output, valuetok, &out.amount.satoshis)) /* Raw: talking to bitcoind */
|
2018-01-04 12:40:58 +01:00
|
|
|
fatal("%s: had bad value (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
scriptpubkeytok = json_get_member(bcli->output, tokens, "scriptPubKey");
|
|
|
|
if (!scriptpubkeytok)
|
|
|
|
fatal("%s: had no scriptPubKey member (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
hextok = json_get_member(bcli->output, scriptpubkeytok, "hex");
|
|
|
|
if (!hextok)
|
|
|
|
fatal("%s: had no scriptPubKey->hex member (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
out.script = tal_hexdata(bcli, bcli->output + hextok->start,
|
|
|
|
hextok->end - hextok->start);
|
|
|
|
if (!out.script)
|
|
|
|
fatal("%s: scriptPubKey->hex invalid hex (%.*s)?",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-01-31 22:26:25 +01:00
|
|
|
cb(bcli->bitcoind, &out, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
static bool process_getblockhash(struct bitcoin_cli *bcli)
|
2016-04-24 12:06:13 +02:00
|
|
|
{
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid blkid;
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2017-12-18 07:44:10 +01:00
|
|
|
const struct bitcoin_blkid *blkid,
|
2016-04-24 12:06:13 +02:00
|
|
|
void *arg) = bcli->cb;
|
|
|
|
|
2018-02-24 06:11:18 +01:00
|
|
|
/* If it failed with error 8, call with NULL block. */
|
2017-12-21 12:07:55 +01:00
|
|
|
if (*bcli->exitstatus != 0) {
|
2018-02-24 06:11:18 +01:00
|
|
|
/* Other error means we have to retry. */
|
|
|
|
if (*bcli->exitstatus != 8)
|
|
|
|
return false;
|
2017-12-21 12:07:55 +01:00
|
|
|
cb(bcli->bitcoind, NULL, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2017-12-21 12:07:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:06:13 +02:00
|
|
|
if (bcli->output_bytes == 0
|
2016-04-24 12:11:20 +02:00
|
|
|
|| !bitcoin_blkid_from_hex(bcli->output, bcli->output_bytes-1,
|
|
|
|
&blkid)) {
|
2016-04-24 12:06:13 +02:00
|
|
|
fatal("%s: bad blockid '%.*s'",
|
2018-09-19 04:31:36 +02:00
|
|
|
bcli_args(tmpctx, bcli),
|
|
|
|
(int)bcli->output_bytes, bcli->output);
|
2016-04-24 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
cb(bcli->bitcoind, &blkid, bcli->cb_arg);
|
2018-02-24 06:11:18 +01:00
|
|
|
return true;
|
2016-04-24 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void bitcoind_getblockhash_(struct bitcoind *bitcoind,
|
2016-04-24 12:06:13 +02:00
|
|
|
u32 height,
|
2017-03-02 13:21:49 +01:00
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2017-12-18 07:44:10 +01:00
|
|
|
const struct bitcoin_blkid *blkid,
|
2016-04-24 12:06:13 +02:00
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
char str[STR_MAX_CHARS(height)];
|
2018-07-31 14:56:04 +02:00
|
|
|
snprintf(str, sizeof(str), "%u", height);
|
2016-04-24 12:06:13 +02:00
|
|
|
|
2018-09-19 04:45:25 +02:00
|
|
|
start_bitcoin_cli(bitcoind, NULL, process_getblockhash, true,
|
|
|
|
BITCOIND_HIGH_PRIO,
|
2018-09-19 04:30:36 +02:00
|
|
|
cb, arg,
|
2016-04-24 12:06:13 +02:00
|
|
|
"getblockhash", str, NULL);
|
|
|
|
}
|
2017-03-02 13:21:49 +01:00
|
|
|
|
2018-01-31 22:26:25 +01:00
|
|
|
void bitcoind_gettxout(struct bitcoind *bitcoind,
|
|
|
|
const struct bitcoin_txid *txid, const u32 outnum,
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
|
|
|
const struct bitcoin_tx_output *txout,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
start_bitcoin_cli(bitcoind, NULL,
|
2018-09-19 04:45:25 +02:00
|
|
|
process_gettxout, true, BITCOIND_LOW_PRIO, cb, arg,
|
2018-01-31 22:26:25 +01:00
|
|
|
"gettxout",
|
2018-02-01 01:22:22 +01:00
|
|
|
take(type_to_string(NULL, struct bitcoin_txid, txid)),
|
|
|
|
take(tal_fmt(NULL, "%u", outnum)),
|
2018-01-31 22:26:25 +01:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:33:08 +02:00
|
|
|
/* Context for the getfilteredblock call. Wraps the actual arguments while we
|
|
|
|
* process the various steps. */
|
|
|
|
struct filteredblock_call {
|
2019-08-06 16:26:42 +02:00
|
|
|
struct list_node list;
|
2019-08-08 06:24:33 +02:00
|
|
|
void (*cb)(struct bitcoind *bitcoind, const struct filteredblock *fb,
|
2019-08-05 18:33:08 +02:00
|
|
|
void *arg);
|
|
|
|
void *arg;
|
|
|
|
|
|
|
|
struct filteredblock *result;
|
|
|
|
struct filteredblock_outpoint **outpoints;
|
|
|
|
size_t current_outpoint;
|
|
|
|
struct timeabs start_time;
|
2019-08-19 19:49:09 +02:00
|
|
|
u32 height;
|
2019-08-05 18:33:08 +02:00
|
|
|
};
|
|
|
|
|
2019-08-06 16:26:42 +02:00
|
|
|
/* Declaration for recursion in process_getfilteredblock_step1 */
|
|
|
|
static void
|
|
|
|
process_getfiltered_block_final(struct bitcoind *bitcoind,
|
|
|
|
const struct filteredblock_call *call);
|
|
|
|
|
2019-08-05 18:33:08 +02:00
|
|
|
static void
|
|
|
|
process_getfilteredblock_step3(struct bitcoind *bitcoind,
|
|
|
|
const struct bitcoin_tx_output *output,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct filteredblock_call *call = (struct filteredblock_call *)arg;
|
|
|
|
struct filteredblock_outpoint *o = call->outpoints[call->current_outpoint];
|
|
|
|
|
|
|
|
/* If this output is unspent, add it to the filteredblock result. */
|
|
|
|
if (output)
|
|
|
|
tal_arr_expand(&call->result->outpoints, tal_steal(call->result, o));
|
|
|
|
|
|
|
|
call->current_outpoint++;
|
|
|
|
if (call->current_outpoint < tal_count(call->outpoints)) {
|
|
|
|
o = call->outpoints[call->current_outpoint];
|
|
|
|
bitcoind_gettxout(bitcoind, &o->txid, o->outnum,
|
|
|
|
process_getfilteredblock_step3, call);
|
|
|
|
} else {
|
|
|
|
/* If there were no more outpoints to check, we call the callback. */
|
2019-08-06 16:26:42 +02:00
|
|
|
process_getfiltered_block_final(bitcoind, call);
|
2019-08-05 18:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_getfilteredblock_step2(struct bitcoind *bitcoind,
|
|
|
|
struct bitcoin_block *block,
|
|
|
|
struct filteredblock_call *call)
|
|
|
|
{
|
|
|
|
struct filteredblock_outpoint *o;
|
|
|
|
struct bitcoin_tx *tx;
|
2019-08-19 19:49:09 +02:00
|
|
|
|
|
|
|
/* If for some reason we couldn't get the block, just report a
|
|
|
|
* failure. */
|
|
|
|
if (block == NULL)
|
|
|
|
return process_getfiltered_block_final(bitcoind, call);
|
|
|
|
|
2019-08-05 18:33:08 +02:00
|
|
|
call->result->prev_hash = block->hdr.prev_hash;
|
|
|
|
|
|
|
|
/* Allocate an array containing all the potentially interesting
|
|
|
|
* outpoints. We will later copy the ones we're interested in into the
|
|
|
|
* call->result if they are unspent. */
|
|
|
|
|
|
|
|
call->outpoints = tal_arr(call, struct filteredblock_outpoint *, 0);
|
|
|
|
for (size_t i = 0; i < tal_count(block->tx); i++) {
|
|
|
|
tx = block->tx[i];
|
|
|
|
for (size_t j = 0; j < tx->wtx->num_outputs; j++) {
|
|
|
|
const u8 *script = bitcoin_tx_output_get_script(NULL, tx, j);
|
2019-09-26 21:07:20 +02:00
|
|
|
struct amount_asset amount = bitcoin_tx_output_get_amount(tx, j);
|
|
|
|
if (amount_asset_is_main(&amount) && is_p2wsh(script, NULL)) {
|
2019-08-05 18:33:08 +02:00
|
|
|
/* This is an interesting output, remember it. */
|
|
|
|
o = tal(call->outpoints, struct filteredblock_outpoint);
|
|
|
|
bitcoin_txid(tx, &o->txid);
|
2019-09-26 21:07:20 +02:00
|
|
|
o->amount = amount_asset_to_sat(&amount);
|
2019-08-05 18:33:08 +02:00
|
|
|
o->txindex = i;
|
|
|
|
o->outnum = j;
|
|
|
|
o->scriptPubKey = tal_steal(o, script);
|
|
|
|
tal_arr_expand(&call->outpoints, o);
|
|
|
|
} else {
|
|
|
|
tal_free(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tal_count(call->outpoints) == 0) {
|
|
|
|
/* If there were no outpoints to check, we can short-circuit
|
|
|
|
* and just call the callback. */
|
2019-08-06 16:26:42 +02:00
|
|
|
process_getfiltered_block_final(bitcoind, call);
|
2019-08-05 18:33:08 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Otherwise we start iterating through call->outpoints and
|
|
|
|
* store the one's that are unspent in
|
|
|
|
* call->result->outpoints. */
|
|
|
|
o = call->outpoints[call->current_outpoint];
|
|
|
|
bitcoind_gettxout(bitcoind, &o->txid, o->outnum,
|
|
|
|
process_getfilteredblock_step3, call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_getfilteredblock_step1(struct bitcoind *bitcoind,
|
|
|
|
const struct bitcoin_blkid *blkid,
|
|
|
|
struct filteredblock_call *call)
|
|
|
|
{
|
2019-08-19 19:49:09 +02:00
|
|
|
/* If we were unable to fetch the block hash (bitcoind doesn't know
|
|
|
|
* about a block at that height), we can short-circuit and just call
|
|
|
|
* the callback. */
|
|
|
|
if (!blkid)
|
|
|
|
return process_getfiltered_block_final(bitcoind, call);
|
|
|
|
|
2019-08-05 18:33:08 +02:00
|
|
|
/* So we have the first piece of the puzzle, the block hash */
|
2019-08-19 19:49:09 +02:00
|
|
|
call->result = tal(call, struct filteredblock);
|
|
|
|
call->result->height = call->height;
|
|
|
|
call->result->outpoints = tal_arr(call->result, struct filteredblock_outpoint *, 0);
|
2019-08-05 18:33:08 +02:00
|
|
|
call->result->id = *blkid;
|
|
|
|
|
|
|
|
/* Now get the raw block to get all outpoints that were created in
|
|
|
|
* this block. */
|
|
|
|
bitcoind_getrawblock(bitcoind, blkid, process_getfilteredblock_step2, call);
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:26:42 +02:00
|
|
|
/* Takes a call, dispatches it to all queued requests that match the same
|
|
|
|
* height, and then kicks off the next call. */
|
|
|
|
static void
|
|
|
|
process_getfiltered_block_final(struct bitcoind *bitcoind,
|
|
|
|
const struct filteredblock_call *call)
|
|
|
|
{
|
|
|
|
struct filteredblock_call *c, *next;
|
2019-08-19 19:49:09 +02:00
|
|
|
u32 height = call->height;
|
|
|
|
|
|
|
|
if (call->result == NULL)
|
|
|
|
goto next;
|
|
|
|
|
2019-08-06 16:26:42 +02:00
|
|
|
/* Need to steal so we don't accidentally free it while iterating through the list below. */
|
|
|
|
struct filteredblock *fb = tal_steal(NULL, call->result);
|
|
|
|
list_for_each_safe(&bitcoind->pending_getfilteredblock, c, next, list) {
|
2019-08-19 19:49:09 +02:00
|
|
|
if (c->height == height) {
|
2019-08-06 16:26:42 +02:00
|
|
|
c->cb(bitcoind, fb, c->arg);
|
|
|
|
list_del(&c->list);
|
|
|
|
tal_free(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tal_free(fb);
|
|
|
|
|
2019-08-19 19:49:09 +02:00
|
|
|
next:
|
2019-08-06 16:26:42 +02:00
|
|
|
/* Nothing to free here, since `*call` was already deleted during the
|
|
|
|
* iteration above. It was also removed from the list, so no need to
|
|
|
|
* pop here. */
|
|
|
|
if (!list_empty(&bitcoind->pending_getfilteredblock)) {
|
|
|
|
c = list_top(&bitcoind->pending_getfilteredblock, struct filteredblock_call, list);
|
2019-08-19 19:49:09 +02:00
|
|
|
bitcoind_getblockhash(bitcoind, c->height, process_getfilteredblock_step1, c);
|
2019-08-06 16:26:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:33:08 +02:00
|
|
|
void bitcoind_getfilteredblock_(struct bitcoind *bitcoind, u32 height,
|
|
|
|
void (*cb)(struct bitcoind *bitcoind,
|
2019-08-08 06:24:33 +02:00
|
|
|
const struct filteredblock *fb,
|
2019-08-05 18:33:08 +02:00
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
/* Stash the call context for when we need to call the callback after
|
|
|
|
* all the bitcoind calls we need to perform. */
|
|
|
|
struct filteredblock_call *call = tal(bitcoind, struct filteredblock_call);
|
2019-08-06 16:26:42 +02:00
|
|
|
/* If this is the first request, we should start processing it. */
|
|
|
|
bool start = list_empty(&bitcoind->pending_getfilteredblock);
|
2019-08-05 18:33:08 +02:00
|
|
|
call->cb = cb;
|
|
|
|
call->arg = arg;
|
2019-08-19 19:49:09 +02:00
|
|
|
call->height = height;
|
2019-08-05 18:33:08 +02:00
|
|
|
assert(call->cb != NULL);
|
|
|
|
call->start_time = time_now();
|
2019-08-19 19:49:09 +02:00
|
|
|
call->result = NULL;
|
2019-08-06 12:38:20 +02:00
|
|
|
call->current_outpoint = 0;
|
2019-08-05 18:33:08 +02:00
|
|
|
|
2019-08-06 16:26:42 +02:00
|
|
|
list_add_tail(&bitcoind->pending_getfilteredblock, &call->list);
|
|
|
|
if (start)
|
|
|
|
bitcoind_getblockhash(bitcoind, height, process_getfilteredblock_step1, call);
|
2019-08-05 18:33:08 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 06:55:54 +02:00
|
|
|
static void destroy_bitcoind(struct bitcoind *bitcoind)
|
|
|
|
{
|
2020-01-07 16:09:59 +01:00
|
|
|
strmap_clear(&bitcoind->pluginsmap);
|
2017-09-12 06:55:54 +02:00
|
|
|
/* Suppresses the callbacks from bcli_finished as we free conns. */
|
|
|
|
bitcoind->shutdown = true;
|
|
|
|
}
|
|
|
|
|
2017-11-01 11:20:40 +01:00
|
|
|
struct bitcoind *new_bitcoind(const tal_t *ctx,
|
|
|
|
struct lightningd *ld,
|
|
|
|
struct log *log)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
struct bitcoind *bitcoind = tal(ctx, struct bitcoind);
|
|
|
|
|
2020-01-07 16:09:59 +01:00
|
|
|
strmap_init(&bitcoind->pluginsmap);
|
2018-02-24 06:13:34 +01:00
|
|
|
bitcoind->cli = NULL;
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind->datadir = NULL;
|
2017-11-01 11:20:40 +01:00
|
|
|
bitcoind->ld = ld;
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind->log = log;
|
2018-09-19 04:45:25 +02:00
|
|
|
for (size_t i = 0; i < BITCOIND_NUM_PRIO; i++) {
|
|
|
|
bitcoind->num_requests[i] = 0;
|
|
|
|
list_head_init(&bitcoind->pending[i]);
|
|
|
|
}
|
2019-08-06 16:26:42 +02:00
|
|
|
list_head_init(&bitcoind->pending_getfilteredblock);
|
2017-09-12 06:55:54 +02:00
|
|
|
bitcoind->shutdown = false;
|
2017-09-28 05:39:05 +02:00
|
|
|
bitcoind->error_count = 0;
|
2019-06-29 21:12:15 +02:00
|
|
|
bitcoind->retry_timeout = 60;
|
2018-01-30 15:17:58 +01:00
|
|
|
bitcoind->rpcuser = NULL;
|
|
|
|
bitcoind->rpcpass = NULL;
|
|
|
|
bitcoind->rpcconnect = NULL;
|
2018-03-25 13:57:52 +02:00
|
|
|
bitcoind->rpcport = NULL;
|
2017-09-12 06:55:54 +02:00
|
|
|
tal_add_destructor(bitcoind, destroy_bitcoind);
|
2020-01-08 17:53:31 +01:00
|
|
|
bitcoind->synced = false;
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
return bitcoind;
|
|
|
|
}
|