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

Bitcoin backend generalization #3488

Merged
merged 21 commits into from
Feb 12, 2020
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fd6172a
libplugin: add a 'still_pending' helper
darosior Feb 2, 2020
22780f8
libplugin: generalize the plugin_timer callback type
darosior Feb 3, 2020
f5999fd
plugins/bcli: a new plugin for gathering Bitcoin data
darosior Jan 2, 2020
44d408c
plugins/bcli: wait for bitcoind to be warmed up at init
darosior Jan 4, 2020
8261ff1
plugins/bcli: register Bitcoin-related options
darosior Jan 4, 2020
983d2e3
plugins/libplugin: don't crash if 'lightning-rpc' doesnt exist (yet)
darosior Jan 7, 2020
fbc92b0
chaintopology: check bitcoin plugin commands at startup
darosior Jan 7, 2020
a3eb36b
bitcoind: allow commands to be registered by different plugins
darosior Jan 7, 2020
c21d902
bitcoind: initialize Bitcoin-backend plugin early
darosior Jan 7, 2020
ed2a714
chaintopology: dont check bitcoin-core version at startup
darosior Jan 8, 2020
3515cff
lightningd/chaintopology: use plugin backend to setup topology
darosior Jan 8, 2020
0ff12ce
lightningd/chaintopology: use plugin for tip polling
darosior Jan 8, 2020
6aa9233
lightningd/bitcoind: use the Bitcoin plugin to send transactions
darosior Jan 9, 2020
900af43
lightningd/bitcoind: use getrawblockatheight for getfilteredblock
darosior Jan 9, 2020
fa7de2c
lightningd/bitcoind: use the Bitcoin plugin for getutxout
darosior Jan 9, 2020
a9239df
lightningd/bitcoind: use the Bitcoin plugin for fee estimates
darosior Jan 9, 2020
6ee5fe4
lightningd/bitcoind: remove all bitcoin-cli specific code
darosior Jan 10, 2020
c3b175d
lightningd: clean utxos after the topology is setup
darosior Feb 5, 2020
08c0e08
pytest: adjust mocks and logs for bitcoind
darosior Feb 5, 2020
765033d
pytest: fixup flaky test_closing_specified_destination
darosior Feb 5, 2020
f7b48ed
pytest: test Bitcoin plugin registration and the bcli plugin
darosior Feb 6, 2020
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
70 changes: 69 additions & 1 deletion plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,72 @@ static struct command_result *getutxout(struct command *cmd,
return command_still_pending(cmd);
}

static void bitcoind_failure(struct plugin *p, const char *error_message)
{
const char **cmd = gather_args(bitcoind, "echo", NULL);
const char *err =
tal_fmt(bitcoind, "\n%s\n\n"
"Make sure you have bitcoind running and that bitcoin-cli"
" is able to connect to bitcoind.\n\n"
"You can verify that your Bitcoin Core installation is"
" ready for use by running:\n\n"
" $ %s 'hello world'\n", error_message,
args_string(cmd, cmd));
plugin_err(p, err);
}

static void wait_for_bitcoind(struct plugin *p)
{
int from, status, ret;
pid_t child;
const char **cmd = gather_args(bitcoind, "echo", NULL);
bool printed = false;

for (;;) {
darosior marked this conversation as resolved.
Show resolved Hide resolved
child = pipecmdarr(NULL, &from, &from, cast_const2(char **,cmd));
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));
}

char *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));
}

if (!printed) {
plugin_log(p, LOG_UNUSUAL,
"Waiting for bitcoind to warm up...");
printed = true;
}
sleep(1);
}
tal_free(cmd);
}

/* Initialize the global context when handshake is done. */
static void init(struct plugin *p UNUSED, const char *buffer UNUSED,
static void init(struct plugin *p, const char *buffer UNUSED,
const jsmntok_t *config UNUSED)
{
bitcoind = tal(NULL, struct bitcoind);
Expand All @@ -704,6 +768,10 @@ static void init(struct plugin *p UNUSED, const char *buffer UNUSED,
bitcoind->rpcpass = NULL;
bitcoind->rpcconnect = NULL;
bitcoind->rpcport = NULL;

wait_for_bitcoind(p);
plugin_log(p, LOG_INFORM,
"bitcoin-cli initialized and connected to bitcoind.");
}

static const struct plugin_command commands[] = {
Expand Down