diff --git a/lightningd/htlc_end.c b/lightningd/htlc_end.c index 9bd594914..a1b70649e 100644 --- a/lightningd/htlc_end.c +++ b/lightningd/htlc_end.c @@ -134,6 +134,8 @@ struct htlc_in *new_htlc_in(const tal_t *ctx, hin->failuremsg = NULL; hin->preimage = NULL; + hin->received_time = time_now(); + return htlc_in_check(hin, "new_htlc_in"); } diff --git a/lightningd/htlc_end.h b/lightningd/htlc_end.h index 558924f28..a955487bc 100644 --- a/lightningd/htlc_end.h +++ b/lightningd/htlc_end.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -47,6 +48,9 @@ struct htlc_in { /* If they fulfilled, here's the preimage. */ struct preimage *preimage; + /* Remember the timestamp we received this HTLC so we can later record + * it, and the resolution time, in the forwards table. */ + struct timeabs received_time; }; struct htlc_out { diff --git a/wallet/db.c b/wallet/db.c index eb0c26599..ebf26796e 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -376,6 +376,7 @@ static struct migration dbmigrations[] = { { "ALTER TABLE channels ADD feerate_base INTEGER;", NULL }, { "ALTER TABLE channels ADD feerate_ppm INTEGER;", NULL }, { NULL, migrate_pr2342_feerate_per_channel }, + { "ALTER TABLE channel_htlcs ADD received_time INTEGER", NULL }, }; /* Leak tracking. */ diff --git a/wallet/wallet.c b/wallet/wallet.c index 60bf2d138..e6b895fdb 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1233,8 +1233,9 @@ void wallet_htlc_save_in(struct wallet *wallet, " payment_key," " hstate," " shared_secret," - " routing_onion) VALUES " - "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); + " routing_onion," + " received_time) VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); sqlite3_bind_int64(stmt, 1, chan->dbid); sqlite3_bind_int64(stmt, 2, in->key.id); @@ -1258,6 +1259,8 @@ void wallet_htlc_save_in(struct wallet *wallet, sqlite3_bind_blob(stmt, 10, &in->onion_routing_packet, sizeof(in->onion_routing_packet), SQLITE_TRANSIENT); + sqlite3_bind_timeabs(stmt, 11, in->received_time); + db_exec_prepared(wallet->db, stmt); in->dbid = sqlite3_last_insert_rowid(wallet->db->sql); } @@ -1351,7 +1354,7 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, "id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " \ "payment_hash, payment_key, routing_onion, " \ "failuremsg, malformed_onion," \ - "origin_htlc, shared_secret" + "origin_htlc, shared_secret, received_time" static bool wallet_stmt2htlc_in(struct channel *channel, sqlite3_stmt *stmt, struct htlc_in *in) @@ -1393,6 +1396,8 @@ static bool wallet_stmt2htlc_in(struct channel *channel, #endif } + in->received_time = sqlite3_column_timeabs(stmt, 12); + return ok; }