Browse Source

onchaind: Replay stored channeltxs to restore onchaind state

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
4b22760cf9
  1. 4
      lightningd/lightningd.c
  2. 45
      lightningd/onchain_control.c
  3. 4
      lightningd/onchain_control.h
  4. 3
      lightningd/test/run-find_my_path.c

4
lightningd/lightningd.c

@ -30,6 +30,7 @@
#include <lightningd/invoice.h> #include <lightningd/invoice.h>
#include <lightningd/jsonrpc.h> #include <lightningd/jsonrpc.h>
#include <lightningd/log.h> #include <lightningd/log.h>
#include <lightningd/onchain_control.h>
#include <lightningd/options.h> #include <lightningd/options.h>
#include <onchaind/onchain_wire.h> #include <onchaind/onchain_wire.h>
#include <signal.h> #include <signal.h>
@ -369,6 +370,9 @@ int main(int argc, char *argv[])
ld->config.poll_time, ld->config.poll_time,
first_blocknum); first_blocknum);
/* Replay transactions for all running onchainds */
onchaind_replay_channels(ld);
/* Create RPC socket (if any) */ /* Create RPC socket (if any) */
setup_jsonrpc(ld, ld->rpc_filename); setup_jsonrpc(ld, ld->rpc_filename);

45
lightningd/onchain_control.c

@ -3,7 +3,6 @@
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <lightningd/chaintopology.h> #include <lightningd/chaintopology.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h> #include <lightningd/log.h>
#include <lightningd/onchain_control.h> #include <lightningd/onchain_control.h>
#include <lightningd/peer_control.h> #include <lightningd/peer_control.h>
@ -469,3 +468,47 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
/* We keep watching until peer finally deleted, for reorgs. */ /* We keep watching until peer finally deleted, for reorgs. */
return KEEP_WATCHING; return KEEP_WATCHING;
} }
void onchaind_replay_channels(struct lightningd *ld)
{
u32 *onchaind_ids;
struct channeltx *txs;
struct channel *chan;
db_begin_transaction(ld->wallet->db);
onchaind_ids = wallet_onchaind_channels(ld->wallet, ld);
for (size_t i = 0; i < tal_count(onchaind_ids); i++) {
log_info(ld->log, "Restarting onchaind for channel %d",
onchaind_ids[i]);
txs = wallet_channeltxs_get(ld->wallet, onchaind_ids,
onchaind_ids[i]);
chan = channel_by_dbid(ld, onchaind_ids[i]);
for (size_t j = 0; j < tal_count(txs); j++) {
if (txs[j].type == WIRE_ONCHAIN_INIT) {
onchaind_funding_spent(chan, txs[j].tx,
txs[j].blockheight);
} else if (txs[j].type == WIRE_ONCHAIN_SPENT) {
onchain_txo_spent(chan, txs[j].tx,
txs[j].input_num,
txs[j].blockheight);
} else if (txs[j].type == WIRE_ONCHAIN_DEPTH) {
onchain_tx_depth(chan, &txs[j].txid,
txs[j].depth);
} else {
fatal("unknown message of type %d during "
"onchaind replay",
txs[j].type);
}
}
tal_free(txs);
}
tal_free(onchaind_ids);
db_commit_transaction(ld->wallet->db);
}

4
lightningd/onchain_control.h

@ -2,6 +2,8 @@
#define LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H #define LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <lightningd/lightningd.h>
#include <onchaind/gen_onchain_wire.h>
struct channel; struct channel;
struct bitcoin_tx; struct bitcoin_tx;
@ -11,4 +13,6 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
const struct bitcoin_tx *tx, const struct bitcoin_tx *tx,
u32 blockheight); u32 blockheight);
void onchaind_replay_channels(struct lightningd *ld);
#endif /* LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H */ #endif /* LIGHTNING_LIGHTNINGD_ONCHAIN_CONTROL_H */

3
lightningd/test/run-find_my_path.c

@ -74,6 +74,9 @@ struct log_book *new_log_book(size_t max_mem UNNEEDED,
/* Generated stub for new_topology */ /* Generated stub for new_topology */
struct chain_topology *new_topology(struct lightningd *ld UNNEEDED, struct log *log UNNEEDED) struct chain_topology *new_topology(struct lightningd *ld UNNEEDED, struct log *log UNNEEDED)
{ fprintf(stderr, "new_topology called!\n"); abort(); } { fprintf(stderr, "new_topology called!\n"); abort(); }
/* Generated stub for onchaind_replay_channels */
void onchaind_replay_channels(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "onchaind_replay_channels called!\n"); abort(); }
/* Generated stub for register_opts */ /* Generated stub for register_opts */
void register_opts(struct lightningd *ld UNNEEDED) void register_opts(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "register_opts called!\n"); abort(); } { fprintf(stderr, "register_opts called!\n"); abort(); }

Loading…
Cancel
Save