Skip to content

Commit 93b0e02

Browse files
plugins/bcli: use -rpcwait to simplify waiting for bitcoind to warm up
Replaced custom wait logic with the -rpcwait flag in bitcoin-cli to handle waiting for bitcoind to warm up. This simplifies the code and ensures that errors unrelated to warmup are passed up directly without additional checks. Changelog-None Signed-off-by: Nishant Bansal <[email protected]>
1 parent 98679aa commit 93b0e02

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

plugins/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ PLUGIN_COMMON_OBJS := \
192192
common/splice_script.o \
193193
common/setup.o \
194194
common/status_levels.o \
195+
common/timeout.o \
195196
common/utils.o \
196197
common/version.o \
197198
common/wireaddr.o \

plugins/bcli.c

+37-44
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <common/json_param.h>
1111
#include <common/json_stream.h>
1212
#include <common/memleak.h>
13+
#include <common/timeout.h>
1314
#include <errno.h>
1415
#include <plugins/libplugin.h>
1516

@@ -20,6 +21,7 @@
2021
*/
2122
#define BITCOIND_MAX_PARALLEL 4
2223
#define RPC_TRANSACTION_ALREADY_IN_CHAIN -27
24+
#define BITCOIND_WARMUP_TIMER 30
2325

2426
enum bitcoind_prio {
2527
BITCOIND_LOW_PRIO,
@@ -1033,61 +1035,52 @@ static void parse_getnetworkinfo_result(struct plugin *p, const char *buf)
10331035
tal_free(result);
10341036
}
10351037

1038+
static void log_warm_up(struct plugin *p)
1039+
{
1040+
plugin_log(p, LOG_UNUSUAL, "Waiting for bitcoind to warm up...");
1041+
}
1042+
10361043
static void wait_and_check_bitcoind(struct plugin *p)
10371044
{
1038-
int in, from, status, ret;
1045+
int in, from, status;
10391046
pid_t child;
1040-
const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL);
1041-
bool printed = false;
1047+
const char **cmd =
1048+
gather_args(bitcoind, "-rpcwait", "getnetworkinfo", NULL);
10421049
char *output = NULL;
10431050

1044-
for (;;) {
1045-
tal_free(output);
1051+
struct timers *timer = tal(cmd, struct timers);
1052+
timers_init(timer, time_mono());
1053+
new_reltimer(timer, timer, time_from_sec(BITCOIND_WARMUP_TIMER),
1054+
log_warm_up, p);
10461055

1047-
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
1056+
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
10481057

1049-
if (bitcoind->rpcpass)
1050-
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
1058+
if (bitcoind->rpcpass)
1059+
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
10511060

1052-
close(in);
1061+
close(in);
10531062

1054-
if (child < 0) {
1055-
if (errno == ENOENT)
1056-
bitcoind_failure(p, "bitcoin-cli not found. Is bitcoin-cli "
1057-
"(part of Bitcoin Core) available in your PATH?");
1058-
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1059-
}
1063+
if (child < 0) {
1064+
if (errno == ENOENT)
1065+
bitcoind_failure(
1066+
p,
1067+
"bitcoin-cli not found. Is bitcoin-cli "
1068+
"(part of Bitcoin Core) available in your PATH?");
1069+
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1070+
}
10601071

1061-
output = grab_fd(cmd, from);
1062-
1063-
while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR);
1064-
if (ret != child)
1065-
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
1066-
cmd[0], strerror(errno)));
1067-
if (!WIFEXITED(status))
1068-
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1069-
cmd[0], WTERMSIG(status)));
1070-
1071-
if (WEXITSTATUS(status) == 0)
1072-
break;
1073-
1074-
/* bitcoin/src/rpc/protocol.h:
1075-
* RPC_IN_WARMUP = -28, //!< Client still warming up
1076-
*/
1077-
if (WEXITSTATUS(status) != 28) {
1078-
if (WEXITSTATUS(status) == 1)
1079-
bitcoind_failure(p, "Could not connect to bitcoind using"
1080-
" bitcoin-cli. Is bitcoind running?");
1081-
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
1082-
cmd[0], WEXITSTATUS(status), output));
1083-
}
1072+
output = grab_fd(cmd, from);
10841073

1085-
if (!printed) {
1086-
plugin_log(p, LOG_UNUSUAL,
1087-
"Waiting for bitcoind to warm up...");
1088-
printed = true;
1089-
}
1090-
sleep(1);
1074+
waitpid(child, &status, 0);
1075+
1076+
if (!WIFEXITED(status))
1077+
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1078+
cmd[0], WTERMSIG(status)));
1079+
1080+
if (WEXITSTATUS(status) != 0) {
1081+
bitcoind_failure(p,
1082+
tal_fmt(bitcoind, "%s exited with code %i: %s",
1083+
cmd[0], WEXITSTATUS(status), output));
10911084
}
10921085

10931086
parse_getnetworkinfo_result(p, output);

0 commit comments

Comments
 (0)