diff --git a/lightningd/htlc_end.c b/lightningd/htlc_end.c index 23fa95fe4..c38068ea1 100644 --- a/lightningd/htlc_end.c +++ b/lightningd/htlc_end.c @@ -96,6 +96,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx, { struct htlc_in *hin = tal(ctx, struct htlc_in); + hin->dbid = 0; hin->key.peer = peer; hin->key.id = id; hin->msatoshi = msatoshi; @@ -141,6 +142,9 @@ struct htlc_out *new_htlc_out(const tal_t *ctx, { struct htlc_out *hout = tal(ctx, struct htlc_out); + /* Mark this as an as of now unsaved HTLC */ + hout->dbid = 0; + hout->key.peer = peer; hout->msatoshi = msatoshi; hout->cltv_expiry = cltv_expiry; diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 1a97a54ef..0b5f30c11 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -56,7 +56,9 @@ static bool htlc_in_update_state(struct peer *peer, if (!state_update_ok(peer, hin->hstate, newstate, hin->key.id, "in")) return false; - /* FIXME: db commit */ + if (!wallet_htlc_update(peer->ld->wallet, hin->dbid, newstate, hin->preimage)) + return false; + hin->hstate = newstate; htlc_in_check(hin, __func__); return true; @@ -69,7 +71,9 @@ static bool htlc_out_update_state(struct peer *peer, if (!state_update_ok(peer, hout->hstate, newstate, hout->key.id, "out")) return false; - /* FIXME: db commit */ + if (!wallet_htlc_update(peer->ld->wallet, hout->dbid, newstate, NULL)) + return false; + hout->hstate = newstate; htlc_out_check(hout, __func__); return true; @@ -713,6 +717,7 @@ static void fulfill_our_htlc_out(struct peer *peer, struct htlc_out *hout, htlc_out_check(hout, __func__); /* FIXME: Save to db */ + wallet_htlc_update(peer->ld->wallet, hout->dbid, hout->hstate, preimage); if (hout->in) fulfill_htlc(hout->in, preimage); @@ -913,6 +918,13 @@ static bool update_out_htlc(struct peer *peer, u64 id, enum htlc_state newstate) return false; } + if (!hout->dbid && !wallet_htlc_save_out(peer->ld->wallet, peer->channel, hout)) { + peer_internal_error( + peer, "Unable to save the htlc_out to the database: %s", + peer->ld->wallet->db->err); + return false; + } + if (!htlc_out_update_state(peer, hout, newstate)) return false; @@ -1053,7 +1065,8 @@ static void added_their_htlc(struct peer *peer, added->cltv_expiry, &added->payment_hash, shared_secret, added->onion_routing_packet); - /* FIXME: Save to db */ + /* Save an incoming htlc to the wallet */ + wallet_htlc_save_in(peer->ld->wallet, peer->channel, hin); log_debug(peer->log, "Adding their HTLC %"PRIu64, added->id); connect_htlc_in(&peer->ld->htlcs_in, hin); diff --git a/wallet/wallet.c b/wallet/wallet.c index b8122ca58..71ff4c45b 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -917,6 +917,9 @@ bool wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, bool ok = true; tal_t *tmpctx = tal_tmpctx(wallet); + /* The database ID must be set by a previous call to + * `wallet_htlc_save_*` */ + assert(htlc_dbid); if (payment_key) { ok &= db_exec( __func__, wallet->db, "UPDATE channel_htlcs SET hstate=%d, "