mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
wallet: Added loading of HTLCs from the database
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
53763ba6a4
commit
eb5ed961ed
4 changed files with 40 additions and 19 deletions
|
@ -15,10 +15,12 @@ WALLET_LIB_OBJS := $(WALLET_LIB_SRC:.c=.o)
|
||||||
WALLET_LIB_HEADERS := $(WALLET_LIB_SRC:.c=.h)
|
WALLET_LIB_HEADERS := $(WALLET_LIB_SRC:.c=.h)
|
||||||
|
|
||||||
WALLET_TEST_COMMON_OBJS := \
|
WALLET_TEST_COMMON_OBJS := \
|
||||||
lightningd/log.o \
|
common/htlc_state.o \
|
||||||
common/type_to_string.o \
|
common/type_to_string.o \
|
||||||
common/pseudorand.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_SRC := $(wildcard wallet/*_tests.c)
|
||||||
WALLET_TEST_OBJS := $(WALLET_TEST_SRC:.c=.o)
|
WALLET_TEST_OBJS := $(WALLET_TEST_SRC:.c=.o)
|
||||||
|
|
|
@ -1020,7 +1020,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
|
||||||
sqlite3_stmt *stmt = db_query(
|
sqlite3_stmt *stmt = db_query(
|
||||||
__func__, wallet->db,
|
__func__, wallet->db,
|
||||||
"SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, "
|
"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=%d AND channel_id=%" PRIu64 " AND hstate != %d",
|
||||||
DIRECTION_INCOMING, chan->id, SENT_REMOVE_ACK_REVOCATION);
|
DIRECTION_INCOMING, chan->id, SENT_REMOVE_ACK_REVOCATION);
|
||||||
|
|
||||||
|
@ -1041,7 +1041,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
|
||||||
stmt = db_query(
|
stmt = db_query(
|
||||||
__func__, wallet->db,
|
__func__, wallet->db,
|
||||||
"SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, "
|
"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=%d AND channel_id=%" PRIu64 " AND hstate != %d",
|
||||||
DIRECTION_OUTGOING, chan->id, RCVD_REMOVE_ACK_REVOCATION);
|
DIRECTION_OUTGOING, chan->id, RCVD_REMOVE_ACK_REVOCATION);
|
||||||
|
|
||||||
|
@ -1063,18 +1063,3 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet,
|
||||||
|
|
||||||
return ok;
|
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);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
@ -264,4 +264,29 @@ bool wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
|
||||||
const enum htlc_state new_state,
|
const enum htlc_state new_state,
|
||||||
const struct preimage *payment_key);
|
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 */
|
#endif /* WALLET_WALLET_H */
|
||||||
|
|
|
@ -301,6 +301,8 @@ static bool test_htlc_crud(const tal_t *ctx)
|
||||||
struct preimage payment_key;
|
struct preimage payment_key;
|
||||||
struct wallet_channel chan;
|
struct wallet_channel chan;
|
||||||
struct wallet *w = create_test_wallet(ctx);
|
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 */
|
/* Make sure we have our references correct */
|
||||||
db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);");
|
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(out.dbid != 0, "HTLC DB ID was not set.");
|
||||||
CHECK_MSG(!wallet_htlc_save_out(w, &chan, &out),
|
CHECK_MSG(!wallet_htlc_save_out(w, &chan, &out),
|
||||||
"Saving two HTLCs with the same data must not succeed.");
|
"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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue