Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins/bcli: use -rpcwait to simplify waiting for bitcoind to warm up #7967

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 30 additions & 44 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1035,59 +1035,45 @@ static void parse_getnetworkinfo_result(struct plugin *p, const char *buf)

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

for (;;) {
tal_free(output);
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));

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

if (bitcoind->rpcpass)
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
close(in);

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

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

output = grab_fd(cmd, from);

while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR);
if (ret != child)
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
cmd[0], strerror(errno)));
if (!WIFEXITED(status))
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
cmd[0], WTERMSIG(status)));

if (WEXITSTATUS(status) == 0)
break;

/* bitcoin/src/rpc/protocol.h:
* RPC_IN_WARMUP = -28, //!< Client still warming up
*/
if (WEXITSTATUS(status) != 28) {
if (WEXITSTATUS(status) == 1)
bitcoind_failure(p, "Could not connect to bitcoind using"
" bitcoin-cli. Is bitcoind running?");
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
cmd[0], WEXITSTATUS(status), output));
}
waitpid(child, &status, 0);

if (!printed) {
plugin_log(p, LOG_UNUSUAL,
"Waiting for bitcoind to warm up...");
printed = true;
}
sleep(1);
if (!WIFEXITED(status))
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
cmd[0], WTERMSIG(status)));

if (WEXITSTATUS(status) != 0) {
if (WEXITSTATUS(status) == 1)
bitcoind_failure(p,
"RPC connection timed out. Could "
"not connect to bitcoind using "
"bitcoin-cli. Is bitcoind running?");
bitcoind_failure(p,
tal_fmt(bitcoind, "%s exited with code %i: %s",
cmd[0], WEXITSTATUS(status), output));
}

parse_getnetworkinfo_result(p, output);
Expand Down