MCPcopy Create free account
hub / github.com/ElementsProject/lightning / chaininfo_getchaininfo_done

Function chaininfo_getchaininfo_done

plugins/bwatch/bwatch_interface.c:240–285  ·  view source on GitHub ↗

Got chain state from bcli: optionally roll back, then forward to watchman. */

Source from the content-addressed store, hash-verified

238
239/* Got chain state from bcli: optionally roll back, then forward to watchman. */
240static struct command_result *chaininfo_getchaininfo_done(struct command *cmd,
241 const char *method UNUSED,
242 const char *buf,
243 const jsmntok_t *result,
244 void *unused UNUSED)
245{
246 struct bwatch *bwatch = bwatch_of(cmd->plugin);
247 struct out_req *req;
248 const char *chain;
249 u32 headercount, blockcount;
250 bool ibd;
251 const char *err;
252
253 err = json_scan(tmpctx, buf, result,
254 "{chain:%,headercount:%,blockcount:%,ibd:%}",
255 JSON_SCAN_TAL(tmpctx, json_strdup, &chain),
256 JSON_SCAN(json_to_number, &headercount),
257 JSON_SCAN(json_to_number, &blockcount),
258 JSON_SCAN(json_to_bool, &ibd));
259 if (err) {
260 plugin_log(cmd->plugin, LOG_BROKEN,
261 "getchaininfo parse failed: %s", err);
262 return timer_complete(cmd);
263 }
264
265 /* Startup-only rollback: if bitcoind's chain is shorter than our
266 * stored tip, peel off stale blocks now. During normal polling the
267 * shorter-chain case is handled by hash-mismatch reorg detection
268 * inside handle_block. */
269 if (blockcount < bwatch->current_height) {
270 plugin_log(cmd->plugin, LOG_INFORM,
271 "Startup: chain at %u but bwatch at %u; rolling back",
272 blockcount, bwatch->current_height);
273 while (bwatch->current_height > blockcount
274 && bwatch_last_block(bwatch))
275 bwatch_remove_tip(cmd, bwatch);
276 }
277
278 req = jsonrpc_request_start(cmd, "chaininfo",
279 chaininfo_ack, chaininfo_err, NULL);
280 json_add_string(req->js, "chain", chain);
281 json_add_u32(req->js, "headercount", headercount);
282 json_add_u32(req->js, "blockcount", blockcount);
283 json_add_bool(req->js, "ibd", ibd);
284 return send_outreq(req);
285}
286
287/* bcli unreachable: log and fall back to polling so we don't stall init. */
288static struct command_result *chaininfo_getchaininfo_failed(struct command *cmd,

Callers

nothing calls this directly

Calls 10

bwatch_ofFunction · 0.85
timer_completeFunction · 0.85
bwatch_last_blockFunction · 0.85
bwatch_remove_tipFunction · 0.85
json_add_u32Function · 0.85
json_add_boolFunction · 0.85
json_scanFunction · 0.50
plugin_logFunction · 0.50
json_add_stringFunction · 0.50
send_outreqFunction · 0.50

Tested by

no test coverage detected