Skip to content

Commit 3df0fbb

Browse files
committed
lightningd: instantiate watchman at startup
Create watchman after setup_topology so it can queue bwatch RPC requests and replay pending ops once the plugin is ready. No subsystems register watches yet — that lands in the wallet migration PR.
1 parent f3d1c5d commit 3df0fbb

5 files changed

Lines changed: 22 additions & 6 deletions

File tree

lightningd/lightningd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include <lightningd/plugin_hook.h>
7676
#include <lightningd/runes.h>
7777
#include <lightningd/subd.h>
78+
#include <lightningd/watchman.h>
7879
#include <sys/resource.h>
7980
#include <wallet/invoices.h>
8081
#include <wally_bip32.h>
@@ -1347,6 +1348,10 @@ int main(int argc, char *argv[])
13471348
setup_topology(ld->topology);
13481349
trace_span_end(ld->topology);
13491350

1351+
/*~ Stand up the watchman: it queues bwatch RPC requests until the
1352+
* bwatch plugin reports ready, then replays them. */
1353+
ld->watchman = watchman_new(ld, ld);
1354+
13501355
db_begin_transaction(ld->wallet->db);
13511356
trace_span_start("delete_old_htlcs", ld->wallet);
13521357
wallet_delete_old_htlcs(ld->wallet);

lightningd/plugin.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <lightningd/plugin_control.h>
3030
#include <lightningd/plugin_hook.h>
3131
#include <lightningd/subd.h>
32+
#include <lightningd/watchman.h>
3233

3334
/* Only this file can include this generated header! */
3435
# include <plugins/list_of_builtin_plugins_gen.h>
@@ -2087,6 +2088,7 @@ static void plugin_config_cb(const char *buffer,
20872088
}
20882089
if (tal_count(plugin->custom_msgs))
20892090
tell_connectd_custommsgs(plugin->plugins);
2091+
watchman_notify_plugin_ready(plugin->plugins->ld, plugin);
20902092
notify_plugin_started(plugin->plugins->ld, plugin);
20912093
check_plugins_initted(plugin->plugins);
20922094
}

lightningd/test/run-find_my_abspath.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers
222222
/* Generated stub for wallet_sanity_check */
223223
bool wallet_sanity_check(struct wallet *w UNNEEDED)
224224
{ fprintf(stderr, "wallet_sanity_check called!\n"); abort(); }
225+
/* Generated stub for watchman_new */
226+
struct watchman *watchman_new(const tal_t *ctx UNNEEDED, struct lightningd *ld UNNEEDED)
227+
{ fprintf(stderr, "watchman_new called!\n"); abort(); }
225228
/* AUTOGENERATED MOCKS END */
226229

227230
struct logger *crashlog;

lightningd/watchman.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ static void load_pending_ops(struct watchman *wm)
135135
}
136136
}
137137

138-
static void watchman_on_plugin_ready(struct lightningd *ld, struct plugin *plugin);
139-
140138
/* Apply --rescan: negative means absolute height (only go back),
141139
* positive means relative (go back N blocks from stored tip). */
142140
static void apply_rescan(struct watchman *wm, struct lightningd *ld)
@@ -167,16 +165,15 @@ struct watchman *watchman_new(const tal_t *ctx, struct lightningd *ld)
167165
wm->ld = ld;
168166
wm->pending_ops = tal_arr(wm, struct pending_op *, 0);
169167

168+
db_begin_transaction(ld->wallet->db);
170169
load_pending_ops(wm);
171170
load_tip(wm);
171+
db_commit_transaction(ld->wallet->db);
172172
apply_rescan(wm, ld);
173173

174174
log_info(ld->log, "Watchman: height=%u, %zu pending ops",
175175
wm->last_processed_height, tal_count(wm->pending_ops));
176176

177-
/* Replay pending ops exactly when bwatch transitions to INIT_COMPLETE. */
178-
ld->plugins->on_plugin_ready = watchman_on_plugin_ready;
179-
180177
return wm;
181178
}
182179

@@ -359,7 +356,7 @@ void watchman_replay_pending(struct lightningd *ld)
359356

360357
/* Replay pending ops when bwatch is ready. On a fresh node current_height
361358
* is still 0, so we defer to json_block_processed where it's guaranteed > 0. */
362-
static void watchman_on_plugin_ready(struct lightningd *ld, struct plugin *plugin)
359+
void watchman_notify_plugin_ready(struct lightningd *ld, struct plugin *plugin)
363360
{
364361
struct watchman *wm = ld->watchman;
365362

lightningd/watchman.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
struct lightningd;
99
struct pending_op;
10+
struct plugin;
1011
struct short_channel_id;
1112

1213
/* lightningd's view of bwatch. bwatch lives in a separate process and tells
@@ -58,6 +59,14 @@ typedef void (*depth_found_fn)(struct lightningd *ld,
5859
u32 depth,
5960
u32 blockheight);
6061

62+
/**
63+
* watchman_notify_plugin_ready - Called by plugin.c when any plugin reaches INIT_COMPLETE
64+
*
65+
* Checks whether the newly-ready plugin is bwatch and, if so, replays any
66+
* pending watch operations that were queued before bwatch was available.
67+
*/
68+
void watchman_notify_plugin_ready(struct lightningd *ld, struct plugin *plugin);
69+
6170
/**
6271
* watchman_new - Create and initialize a new watchman instance
6372
* @ctx: tal context to allocate from

0 commit comments

Comments
 (0)