Get infos about the block chain. * Calls `getblockchaininfo` and returns headers count, blocks count, * the chain id, and whether this is initialblockdownload. */
| 447 | * the chain id, and whether this is initialblockdownload. |
| 448 | */ |
| 449 | static struct command_result *getchaininfo(struct command *cmd, |
| 450 | const char *buf UNUSED, |
| 451 | const jsmntok_t *toks UNUSED) |
| 452 | { |
| 453 | /* FIXME(vincenzopalazzo): Inside the JSON request, |
| 454 | * we have the current height known from Core Lightning. Therefore, |
| 455 | * we can attempt to prevent a crash if the 'getchaininfo' function returns |
| 456 | * a lower height than the one we already know, by waiting for a short period. |
| 457 | * However, I currently don't have a better idea on how to handle this situation. */ |
| 458 | u32 *height UNUSED; |
| 459 | struct bcli_result *res; |
| 460 | const jsmntok_t *tokens; |
| 461 | struct json_stream *response; |
| 462 | bool ibd; |
| 463 | u32 headers, blocks; |
| 464 | const char *chain, *err; |
| 465 | |
| 466 | if (!param(cmd, buf, toks, |
| 467 | p_opt("last_height", param_number, &height), |
| 468 | NULL)) |
| 469 | return command_param_failed(); |
| 470 | |
| 471 | res = run_bitcoin_cli(cmd, cmd->plugin, "getblockchaininfo", NULL); |
| 472 | if (res->exitstatus != 0) |
| 473 | return command_err(cmd, res, "command failed"); |
| 474 | |
| 475 | tokens = json_parse_simple(res->output, res->output, res->output_len); |
| 476 | if (!tokens) |
| 477 | return command_err(cmd, res, "bad JSON: cannot parse"); |
| 478 | |
| 479 | err = json_scan(tmpctx, res->output, tokens, |
| 480 | "{chain:%,headers:%,blocks:%,initialblockdownload:%}", |
| 481 | JSON_SCAN_TAL(tmpctx, json_strdup, &chain), |
| 482 | JSON_SCAN(json_to_number, &headers), |
| 483 | JSON_SCAN(json_to_number, &blocks), |
| 484 | JSON_SCAN(json_to_bool, &ibd)); |
| 485 | if (err) |
| 486 | return command_err(cmd, res, tal_fmt(tmpctx, "bad JSON: %s", err)); |
| 487 | |
| 488 | if (bitcoind->dev_ignore_ibd) |
| 489 | ibd = false; |
| 490 | |
| 491 | response = jsonrpc_stream_success(cmd); |
| 492 | json_add_string(response, "chain", chain); |
| 493 | json_add_u32(response, "headercount", headers); |
| 494 | json_add_u32(response, "blockcount", blocks); |
| 495 | json_add_bool(response, "ibd", ibd); |
| 496 | |
| 497 | return command_finished(cmd, response); |
| 498 | } |
| 499 | |
| 500 | /* Add a feerate, but don't publish one that bitcoind won't accept. */ |
| 501 | static void json_add_feerate(struct json_stream *result, const char *fieldname, |
nothing calls this directly
no test coverage detected