Browse Source

bitcoind: Do not crash when getblock fails

This is a common occurence on pruned nodes. By calling the callback
upon failures, we communicate that we couldn't verify the txoutput. We
fail safe rejecting any channel we can't verify.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
5319ff1bc0
  1. 23
      lightningd/bitcoind.c

23
lightningd/bitcoind.c

@ -479,20 +479,34 @@ static void process_gettxout(struct bitcoin_cli *bcli)
cb(bcli->bitcoind, &out, cbarg); cb(bcli->bitcoind, &out, cbarg);
} }
/**
* process_getblock -- Retrieve a block from bitcoind
*
* Used to resolve a `txoutput` after identifying the blockhash, and
* before extracting the outpoint from the UTXO.
*/
static void process_getblock(struct bitcoin_cli *bcli) static void process_getblock(struct bitcoin_cli *bcli)
{ {
void (*cb)(struct bitcoind *bitcoind, void (*cb)(struct bitcoind *bitcoind,
const struct bitcoin_tx_output *output, const struct bitcoin_tx_output *output,
void *arg) = bcli->cb; void *arg) = bcli->cb;
struct get_output *go = bcli->cb_arg; struct get_output *go = bcli->cb_arg;
void *cbarg = go->cbarg;
const jsmntok_t *tokens, *txstok, *txidtok; const jsmntok_t *tokens, *txstok, *txidtok;
struct bitcoin_txid txid; struct bitcoin_txid txid;
bool valid; bool valid;
tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid); tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid);
if (!tokens) if (!tokens) {
fatal("%s: %s response", /* Most likely we are running on a pruned node, call
bcli_args(bcli), valid ? "partial" : "invalid"); * the callback with NULL to indicate failure */
log_debug(bcli->bitcoind->log,
"%s: returned invalid block, is this a pruned node?",
bcli_args(bcli));
cb(bcli->bitcoind, NULL, cbarg);
tal_free(go);
return;
}
if (tokens[0].type != JSMN_OBJECT) if (tokens[0].type != JSMN_OBJECT)
fatal("%s: gave non-object (%.*s)?", fatal("%s: gave non-object (%.*s)?",
@ -511,11 +525,10 @@ static void process_getblock(struct bitcoin_cli *bcli)
/* Now, this can certainly happen, if txnum too large. */ /* Now, this can certainly happen, if txnum too large. */
txidtok = json_get_arr(txstok, go->txnum); txidtok = json_get_arr(txstok, go->txnum);
if (!txidtok) { if (!txidtok) {
void *cbarg = go->cbarg;
log_debug(bcli->bitcoind->log, "%s: no txnum %u", log_debug(bcli->bitcoind->log, "%s: no txnum %u",
bcli_args(bcli), go->txnum); bcli_args(bcli), go->txnum);
tal_free(go);
cb(bcli->bitcoind, NULL, cbarg); cb(bcli->bitcoind, NULL, cbarg);
tal_free(go);
return; return;
} }

Loading…
Cancel
Save