Skip to content

Commit

Permalink
feat(bitcoind): pass the current known block height
Browse files Browse the repository at this point in the history
When core lightning is asking the information about
the blockchain with `getchaininfo` command lightningd
know already the information about the min and max block height.

the problem is when we have a smarter Bitcoin backend that is able
to switch between different clients in some cases is helpful
give the information about current known height by lightningd and
pass it down to the plugin.

In this way, the plugin knows what is the correct known height from lightnind, and can
try to fix some problems if any exit.

This is particularly useful when you are syncing a new backend from scratch
like https://github.com/cloudhead/nakamoto and we avoid returning the
lower height from the known, and avoid the crash of core lightning.

With this information, the plugin can start to sync the chain and return
the answer back only when the chain is in sync with the current status of
lightningd.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 16, 2023
1 parent 2e5ad0f commit 56c0691
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ static void getchaininfo_callback(const char *buf, const jsmntok_t *toks,

void bitcoind_getchaininfo_(struct bitcoind *bitcoind,
const bool first_call,
const u32 height,
void (*cb)(struct bitcoind *bitcoind,
const char *chain,
u32 headercount,
Expand All @@ -598,6 +599,7 @@ void bitcoind_getchaininfo_(struct bitcoind *bitcoind,
req = jsonrpc_request_start(bitcoind, "getchaininfo", NULL, true,
bitcoind->log,
NULL, getchaininfo_callback, call);
json_add_u32(req->stream, "height", height);
jsonrpc_request_end(req);
bitcoin_plugin_send(bitcoind, req);
}
Expand Down
5 changes: 3 additions & 2 deletions lightningd/bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ void bitcoind_getfilteredblock_(struct bitcoind *bitcoind, u32 height,

void bitcoind_getchaininfo_(struct bitcoind *bitcoind,
const bool first_call,
const u32 height,
void (*cb)(struct bitcoind *bitcoind,
const char *chain,
u32 headercount,
u32 blockcount,
const bool ibd,
const bool first_call, void *),
void *cb_arg);
#define bitcoind_getchaininfo(bitcoind_, first_call_, cb, arg) \
bitcoind_getchaininfo_((bitcoind_), (first_call_), \
#define bitcoind_getchaininfo(bitcoind_, first_call_, height_, cb, arg) \
bitcoind_getchaininfo_((bitcoind_), (first_call_), (height_), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
Expand Down
4 changes: 2 additions & 2 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ static void retry_check_chain(struct chain_topology *topo)
topo->bitcoind->checkchain_timer = NULL;
if (topo->stopping)
return;
bitcoind_getchaininfo(topo->bitcoind, false, check_chain, topo);
bitcoind_getchaininfo(topo->bitcoind, false, topo->max_blockheight, check_chain, topo);
}

void setup_topology(struct chain_topology *topo,
Expand All @@ -1306,7 +1306,7 @@ void setup_topology(struct chain_topology *topo,

/* Sanity checks, then topology initialization. */
topo->bitcoind->checkchain_timer = NULL;
bitcoind_getchaininfo(topo->bitcoind, true, check_chain, topo);
bitcoind_getchaininfo(topo->bitcoind, true, topo->max_blockheight, check_chain, topo);

tal_add_destructor(topo, destroy_chain_topology);

Expand Down
9 changes: 8 additions & 1 deletion plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,14 @@ static struct command_result *getchaininfo(struct command *cmd,
const char *buf UNUSED,
const jsmntok_t *toks UNUSED)
{
if (!param(cmd, buf, toks, NULL))
// FIXME(vincenzopalazzo): inside the json request
// we had the current height known from core lightning, so
// we could try to do not crash if the getchaininfo return
// the a lower height from the known one, by waiting a little bit
// but currently I had no a better idea on how to do it here!
if (!param(cmd, buf, toks,
p_opt_any(),
NULL))
return command_param_failed();

start_bitcoin_cli(NULL, cmd, process_getblockchaininfo, false,
Expand Down

0 comments on commit 56c0691

Please sign in to comment.