From 4270031d75797495640e7e1e1e9fcd142ded1695 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 13 Dec 2019 03:48:23 +1030 Subject: [PATCH] wallet: add per-channel feerate_state to the database. The upgrade here is a bit tricky: we map the two values into the feerate_state. This is trivial if they're both the same, but if they're different we don't know exactly what state they're in (this being the source of the bug!). So, we assume that the have received the update and not acked it, as that would be the normal case. Signed-off-by: Rusty Russell --- wallet/db.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index 8652224b5..c6ed48b3f 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -561,6 +561,34 @@ static struct migration dbmigrations[] = { {SQL("DROP TABLE temp_payments;"), NULL}, {SQL("ALTER TABLE channel_htlcs ADD partid BIGINT;"), NULL}, {SQL("UPDATE channel_htlcs SET partid = 0;"), NULL}, + {SQL("CREATE TABLE channel_feerates (" + " channel_id BIGINT REFERENCES channels(id) ON DELETE CASCADE," + " hstate INTEGER," + " feerate_per_kw INTEGER," + " UNIQUE (channel_id, hstate)" + ");"), + NULL}, + /* Cast old-style per-side feerates into most likely layout for statewise + * feerates. */ + /* If we're funder (LOCAL=0): + * Then our feerate is set last (SENT_ADD_ACK_REVOCATION = 4) */ + {SQL("INSERT INTO channel_feerates(channel_id, hstate, feerate_per_kw)" + " SELECT id, 4, local_feerate_per_kw FROM channels WHERE funder = 0;"), + NULL}, + /* If different, assume their feerate is in state SENT_ADD_COMMIT = 1 */ + {SQL("INSERT INTO channel_feerates(channel_id, hstate, feerate_per_kw)" + " SELECT id, 1, remote_feerate_per_kw FROM channels WHERE funder = 0 and local_feerate_per_kw != remote_feerate_per_kw;"), + NULL}, + /* If they're funder (REMOTE=1): + * Then their feerate is set last (RCVD_ADD_ACK_REVOCATION = 14) */ + {SQL("INSERT INTO channel_feerates(channel_id, hstate, feerate_per_kw)" + " SELECT id, 14, remote_feerate_per_kw FROM channels WHERE funder = 1;"), + NULL}, + /* If different, assume their feerate is in state RCVD_ADD_COMMIT = 11 */ + {SQL("INSERT INTO channel_feerates(channel_id, hstate, feerate_per_kw)" + " SELECT id, 11, local_feerate_per_kw FROM channels WHERE funder = 1 and local_feerate_per_kw != remote_feerate_per_kw;"), + NULL}, + /* FIXME: Remove now-unused local_feerate_per_kw and remote_feerate_per_kw from channels */ }; /* Leak tracking. */