|
10 | 10 | #include <common/json_param.h>
|
11 | 11 | #include <common/json_stream.h>
|
12 | 12 | #include <common/memleak.h>
|
| 13 | +#include <common/timeout.h> |
13 | 14 | #include <errno.h>
|
14 | 15 | #include <plugins/libplugin.h>
|
15 | 16 |
|
|
20 | 21 | */
|
21 | 22 | #define BITCOIND_MAX_PARALLEL 4
|
22 | 23 | #define RPC_TRANSACTION_ALREADY_IN_CHAIN -27
|
| 24 | +#define BITCOIND_WARMUP_TIMER 30 |
23 | 25 |
|
24 | 26 | enum bitcoind_prio {
|
25 | 27 | BITCOIND_LOW_PRIO,
|
@@ -1033,61 +1035,52 @@ static void parse_getnetworkinfo_result(struct plugin *p, const char *buf)
|
1033 | 1035 | tal_free(result);
|
1034 | 1036 | }
|
1035 | 1037 |
|
| 1038 | +static void log_warm_up(struct plugin *p) |
| 1039 | +{ |
| 1040 | + plugin_log(p, LOG_UNUSUAL, "Waiting for bitcoind to warm up..."); |
| 1041 | +} |
| 1042 | + |
1036 | 1043 | static void wait_and_check_bitcoind(struct plugin *p)
|
1037 | 1044 | {
|
1038 |
| - int in, from, status, ret; |
| 1045 | + int in, from, status; |
1039 | 1046 | 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); |
1042 | 1049 | char *output = NULL;
|
1043 | 1050 |
|
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); |
1046 | 1055 |
|
1047 |
| - child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd)); |
| 1056 | + child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd)); |
1048 | 1057 |
|
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)); |
1051 | 1060 |
|
1052 |
| - close(in); |
| 1061 | + close(in); |
1053 | 1062 |
|
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 | + } |
1060 | 1071 |
|
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); |
1084 | 1073 |
|
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)); |
1091 | 1084 | }
|
1092 | 1085 |
|
1093 | 1086 | parse_getnetworkinfo_result(p, output);
|
|
0 commit comments