| 434 | } |
| 435 | |
| 436 | static const char *init(struct command *cmd, |
| 437 | const char *buf UNUSED, |
| 438 | const jsmntok_t *config UNUSED) |
| 439 | { |
| 440 | struct bwatch *bwatch = bwatch_of(cmd->plugin); |
| 441 | |
| 442 | bwatch->plugin = cmd->plugin; |
| 443 | |
| 444 | bwatch->scriptpubkey_watches = new_htable(bwatch, scriptpubkey_watches); |
| 445 | bwatch->outpoint_watches = new_htable(bwatch, outpoint_watches); |
| 446 | bwatch->scid_watches = new_htable(bwatch, scid_watches); |
| 447 | bwatch->blockdepth_watches = new_htable(bwatch, blockdepth_watches); |
| 448 | |
| 449 | bwatch->block_history = tal_arr(bwatch, struct block_record_wire, 0); |
| 450 | |
| 451 | /* Default to "no chain seen yet"; bwatch_load_block_history will |
| 452 | * overwrite these from the datastore when we're enabled. */ |
| 453 | bwatch->current_height = 0; |
| 454 | memset(&bwatch->current_blockhash, 0, sizeof(bwatch->current_blockhash)); |
| 455 | |
| 456 | /* bwatch is opt-in: leave the plugin loaded but skip chain polling until the |
| 457 | * user passes --experimental-bwatch. */ |
| 458 | if (!bwatch->experimental) |
| 459 | return NULL; |
| 460 | |
| 461 | /* Replay persisted block history. load_block_history sets |
| 462 | * current_height / current_blockhash from the most recent record; |
| 463 | * if there are no records, fall back to zero so the first poll |
| 464 | * initialises us at the chain tip. */ |
| 465 | bwatch_load_block_history(cmd, bwatch); |
| 466 | bwatch_load_watches_from_datastore(cmd, bwatch); |
| 467 | |
| 468 | /* Send chaininfo to watchman first; the ack/err callbacks then |
| 469 | * kick off the chain-poll loop. */ |
| 470 | global_timer(cmd->plugin, time_from_sec(0), |
| 471 | bwatch_send_chaininfo, NULL); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | static const struct plugin_command commands[] = { |
| 476 | { "addscriptpubkeywatch", json_bwatch_add_scriptpubkey }, |
no test coverage detected