Skip to content

Commit f8597dc

Browse files
NishantBansal2003rustyrussell
authored andcommitted
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 725c417 commit f8597dc

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

plugins/bcli.c

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,59 +1044,45 @@ static void parse_getnetworkinfo_result(struct plugin *p, const char *buf)
10441044

10451045
static void wait_and_check_bitcoind(struct plugin *p)
10461046
{
1047-
int in, from, status, ret;
1047+
int in, from, status;
10481048
pid_t child;
1049-
const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL);
1050-
bool printed = false;
1049+
const char **cmd = gather_args(
1050+
bitcoind, "-rpcwait", "-rpcwaittimeout=30", "getnetworkinfo", NULL);
10511051
char *output = NULL;
10521052

1053-
for (;;) {
1054-
tal_free(output);
1053+
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
10551054

1056-
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
1055+
if (bitcoind->rpcpass)
1056+
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
10571057

1058-
if (bitcoind->rpcpass)
1059-
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
1058+
close(in);
10601059

1061-
close(in);
1060+
if (child < 0) {
1061+
if (errno == ENOENT)
1062+
bitcoind_failure(
1063+
p,
1064+
"bitcoin-cli not found. Is bitcoin-cli "
1065+
"(part of Bitcoin Core) available in your PATH?");
1066+
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1067+
}
10621068

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

1070-
output = grab_fd(cmd, from);
1071-
1072-
while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR);
1073-
if (ret != child)
1074-
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
1075-
cmd[0], strerror(errno)));
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-
break;
1082-
1083-
/* bitcoin/src/rpc/protocol.h:
1084-
* RPC_IN_WARMUP = -28, //!< Client still warming up
1085-
*/
1086-
if (WEXITSTATUS(status) != 28) {
1087-
if (WEXITSTATUS(status) == 1)
1088-
bitcoind_failure(p, "Could not connect to bitcoind using"
1089-
" bitcoin-cli. Is bitcoind running?");
1090-
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
1091-
cmd[0], WEXITSTATUS(status), output));
1092-
}
1071+
waitpid(child, &status, 0);
10931072

1094-
if (!printed) {
1095-
plugin_log(p, LOG_UNUSUAL,
1096-
"Waiting for bitcoind to warm up...");
1097-
printed = true;
1098-
}
1099-
sleep(1);
1073+
if (!WIFEXITED(status))
1074+
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1075+
cmd[0], WTERMSIG(status)));
1076+
1077+
if (WEXITSTATUS(status) != 0) {
1078+
if (WEXITSTATUS(status) == 1)
1079+
bitcoind_failure(p,
1080+
"RPC connection timed out. Could "
1081+
"not connect to bitcoind using "
1082+
"bitcoin-cli. Is bitcoind running?");
1083+
bitcoind_failure(p,
1084+
tal_fmt(bitcoind, "%s exited with code %i: %s",
1085+
cmd[0], WEXITSTATUS(status), output));
11001086
}
11011087

11021088
parse_getnetworkinfo_result(p, output);

0 commit comments

Comments
 (0)