diff --git a/wallet/db.c b/wallet/db.c index 758108cb4..a7523161f 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -339,6 +339,21 @@ char *dbmigrations[] = { "ALTER TABLE channels ADD future_per_commitment_point BLOB;", /* last_sent_commit array fix */ "ALTER TABLE channels ADD last_sent_commit BLOB;", + + /* Stats table to track forwarded HTLCs. The values in the HTLCs + * and their states are replicated here and the entries are not + * deleted when the HTLC entries or the channel entries are + * deleted to avoid unexpected drops in statistics. */ + "CREATE TABLE forwarded_payments (" + " in_htlc_id INTEGER REFERENCES channel_htlcs(id) ON DELETE SET NULL" + ", out_htlc_id INTEGER REFERENCES channel_htlcs(id) ON DELETE SET NULL" + ", in_channel_scid INTEGER" + ", out_channel_scid INTEGER" + ", in_msatoshi INTEGER" + ", out_msatoshi INTEGER" + ", state INTEGER" + ", UNIQUE(in_htlc_id, out_htlc_id)" + ");", NULL, }; diff --git a/wallet/wallet.h b/wallet/wallet.h index edd8431a2..cb225b3e9 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -118,6 +118,34 @@ static inline enum wallet_output_type wallet_output_type_in_db(enum wallet_outpu fatal("%s: %u is invalid", __func__, w); } +/** + * Possible states for forwards + * + */ +/* /!\ This is a DB ENUM, please do not change the numbering of any + * already defined elements (adding is ok) /!\ */ +enum forward_status { + FORWARD_OFFERED = 0, + FORWARD_SETTLED = 1, + FORWARD_FAILED = 2 +}; + +static inline enum forward_status wallet_forward_status_in_db(enum forward_status s) +{ + switch (s) { + case FORWARD_OFFERED: + BUILD_ASSERT(FORWARD_OFFERED == 0); + return s; + case FORWARD_SETTLED: + BUILD_ASSERT(FORWARD_SETTLED == 1); + return s; + case FORWARD_FAILED: + BUILD_ASSERT(FORWARD_FAILED == 2); + return s; + } + fatal("%s: %u is invalid", __func__, s); +} + /* A database backed shachain struct. The datastructure is * writethrough, reads are performed from an in-memory version, all * writes are passed through to the DB. */