Got chain state from bcli: optionally roll back, then forward to watchman. */
| 238 | |
| 239 | /* Got chain state from bcli: optionally roll back, then forward to watchman. */ |
| 240 | static 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. */ |
| 288 | static struct command_result *chaininfo_getchaininfo_failed(struct command *cmd, |
nothing calls this directly
no test coverage detected