diff --git a/wallet/Makefile b/wallet/Makefile index e0a06c8e8..6c66fbb4d 100644 --- a/wallet/Makefile +++ b/wallet/Makefile @@ -15,10 +15,12 @@ WALLET_LIB_OBJS := $(WALLET_LIB_SRC:.c=.o) WALLET_LIB_HEADERS := $(WALLET_LIB_SRC:.c=.h) WALLET_TEST_COMMON_OBJS := \ - lightningd/log.o \ + common/htlc_state.o \ common/type_to_string.o \ common/pseudorand.o \ - common/utils.o + common/utils.o \ + lightningd/htlc_end.o \ + lightningd/log.o WALLET_TEST_SRC := $(wildcard wallet/*_tests.c) WALLET_TEST_OBJS := $(WALLET_TEST_SRC:.c=.o) diff --git a/wallet/wallet.c b/wallet/wallet.c index 71ff4c45b..91a379436 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1020,7 +1020,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, sqlite3_stmt *stmt = db_query( __func__, wallet->db, "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " - "payment_hash, shared_secret, payment_key FROM channel_htlcs WHERE " + "payment_hash, shared_secret, payment_key, routing_onion FROM channel_htlcs WHERE " "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", DIRECTION_INCOMING, chan->id, SENT_REMOVE_ACK_REVOCATION); @@ -1041,7 +1041,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, stmt = db_query( __func__, wallet->db, "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " - "payment_hash, origin_htlc, payment_key FROM channel_htlcs WHERE " + "payment_hash, origin_htlc, payment_key, routing_onion FROM channel_htlcs WHERE " "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", DIRECTION_OUTGOING, chan->id, RCVD_REMOVE_ACK_REVOCATION); @@ -1063,18 +1063,3 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, return ok; } - -/** - * wallet_shachain_delete - Drop the shachain from the database - * - * Deletes the shachain from the database, including dependent - * shachain_known items. - */ -/* TOOD(cdecker) Uncomment once we have implemented channel delete -static bool wallet_shachain_delete(struct wallet *w, - struct wallet_shachain *chain) -{ - return db_exec(__func__, w->db, - "DELETE FROM shachains WHERE id=%" PRIu64, chain->id); -} -*/ diff --git a/wallet/wallet.h b/wallet/wallet.h index f642671c0..f69a4aee5 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -264,4 +264,29 @@ bool wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, const enum htlc_state new_state, const struct preimage *payment_key); +/** + * wallet_htlcs_load_for_channel - Load HTLCs associated with chan from DB. + * + * @wallet: wallet to load from + * @chan: load HTLCs associated with this channel + * @htlcs_in: htlc_in_map to store loaded htlc_in in + * @htlcs_out: htlc_out_map to store loaded htlc_out in + * + * This function looks for HTLCs that are associated with the given + * channel and loads them into the provided maps. One caveat is that + * the `struct htlc_out` instances are not wired up with the + * corresponding `struct htlc_in` in the forwarding case nor are they + * associated with a `struct pay_command` in the case we originated + * the payment. In the former case the corresponding `struct htlc_in` + * may not have been loaded yet. In the latter case the pay_command + * does not exist anymore since we restarted. + * + * Use `wallet_htlcs_reconnect` to wire htlc_out instances to the + * corresponding htlc_in after loading all channels. + */ +bool wallet_htlcs_load_for_channel(struct wallet *wallet, + struct wallet_channel *chan, + struct htlc_in_map *htlcs_in, + struct htlc_out_map *htlcs_out); + #endif /* WALLET_WALLET_H */ diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index 7740e87ca..668fb9396 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -301,6 +301,8 @@ static bool test_htlc_crud(const tal_t *ctx) struct preimage payment_key; struct wallet_channel chan; struct wallet *w = create_test_wallet(ctx); + struct htlc_in_map htlcs_in; + struct htlc_out_map htlcs_out; /* Make sure we have our references correct */ db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);"); @@ -333,6 +335,13 @@ static bool test_htlc_crud(const tal_t *ctx) CHECK_MSG(out.dbid != 0, "HTLC DB ID was not set."); CHECK_MSG(!wallet_htlc_save_out(w, &chan, &out), "Saving two HTLCs with the same data must not succeed."); + + /* Attempt to load them from the DB again */ + htlc_in_map_init(&htlcs_in); + htlc_out_map_init(&htlcs_out); + + CHECK_MSG(wallet_htlcs_load_for_channel(w, &chan, &htlcs_in, &htlcs_out), + "Failed loading HTLCs"); return true; }