Skip to content

Commit 387043e

Browse files
Claudio RaimondiRaimo33
authored andcommitted
Add rpcwait flag to bitcoin-cli command instead of manual loop
Implements: #3505 Changelog-Changed: changed bitcoind wait loop to use standard -rpcwait flag
1 parent 87d8629 commit 387043e

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

plugins/bcli.c

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,61 +1046,40 @@ static void wait_and_check_bitcoind(struct plugin *p)
10461046
{
10471047
int in, from, status, ret;
10481048
pid_t child;
1049-
const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL);
1050-
bool printed = false;
1049+
const char **cmd = gather_args(bitcoind, "-rpcwait", "getnetworkinfo", NULL);
10511050
char *output = NULL;
10521051

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

1056-
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
1057-
1058-
if (bitcoind->rpcpass)
1059-
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
1060-
1061-
close(in);
1054+
if (bitcoind->rpcpass)
1055+
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
1056+
close(in);
10621057

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-
}
1058+
if (child < 0) {
1059+
if (errno == ENOENT)
1060+
bitcoind_failure(p, "bitcoin-cli not found. Is bitcoin-cli "
1061+
"(part of Bitcoin Core) available in your PATH?");
1062+
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1063+
}
10691064

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-
}
1065+
output = grab_fd(cmd, from);
10931066

1094-
if (!printed) {
1095-
plugin_log(p, LOG_UNUSUAL,
1096-
"Waiting for bitcoind to warm up...");
1097-
printed = true;
1098-
}
1099-
sleep(1);
1067+
while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR);
1068+
if (ret != child)
1069+
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
1070+
cmd[0], strerror(errno)));
1071+
if (!WIFEXITED(status))
1072+
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1073+
cmd[0], WTERMSIG(status)));
1074+
1075+
if (WEXITSTATUS(status) != 0) {
1076+
if (WEXITSTATUS(status) == 1)
1077+
bitcoind_failure(p, "Could not connect to bitcoind using bitcoin-cli. Is bitcoind running?");
1078+
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
1079+
cmd[0], WEXITSTATUS(status), output));
11001080
}
11011081

11021082
parse_getnetworkinfo_result(p, output);
1103-
11041083
tal_free(cmd);
11051084
}
11061085

0 commit comments

Comments
 (0)