wallet: Store local channel basepoints when a new channel is created

This commit is contained in:
Christian Decker 2021-03-01 19:04:47 +01:00 committed by Rusty Russell
parent 4887a5a18f
commit ae7d722ec5
5 changed files with 106 additions and 84 deletions

View File

@ -345,3 +345,10 @@ def test_local_basepoints_cache(bitcoind, node_factory):
for f in fields:
assert(f in present)
assert(present[f] is not None)
# New channels should automatically have the basepoints cached.
l2, l3 = node_factory.line_graph(2)
present = l2.db.query(q)[0]
for f in fields:
assert(f in present)
assert(present[f] is not None)

View File

@ -1401,9 +1401,9 @@ struct db_query db_postgres_queries[] = {
.readonly = false,
},
{
.name = "INSERT INTO channels (peer_id, first_blocknum, id) VALUES (?, ?, ?);",
.query = "INSERT INTO channels (peer_id, first_blocknum, id) VALUES ($1, $2, $3);",
.placeholders = 3,
.name = "INSERT INTO channels ( peer_id, first_blocknum, id, revocation_basepoint_local, payment_basepoint_local, htlc_basepoint_local, delayed_payment_basepoint_local, funding_pubkey_local) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
.query = "INSERT INTO channels ( peer_id, first_blocknum, id, revocation_basepoint_local, payment_basepoint_local, htlc_basepoint_local, delayed_payment_basepoint_local, funding_pubkey_local) VALUES ($1, $2, $3, $4, $5, $6, $7, $8);",
.placeholders = 8,
.readonly = false,
},
{
@ -1876,4 +1876,4 @@ struct db_query db_postgres_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */
// SHA256STAMP:31b3cec58b89d8183275a5f240715bde33901fd0e839ea4d267eed79575e2d2d
// SHA256STAMP:e565991a4998ffbb3289af68158b6a2d6f4d6eac6476f237daec1789382ee7f2

View File

@ -1401,9 +1401,9 @@ struct db_query db_sqlite3_queries[] = {
.readonly = false,
},
{
.name = "INSERT INTO channels (peer_id, first_blocknum, id) VALUES (?, ?, ?);",
.query = "INSERT INTO channels (peer_id, first_blocknum, id) VALUES (?, ?, ?);",
.placeholders = 3,
.name = "INSERT INTO channels ( peer_id, first_blocknum, id, revocation_basepoint_local, payment_basepoint_local, htlc_basepoint_local, delayed_payment_basepoint_local, funding_pubkey_local) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
.query = "INSERT INTO channels ( peer_id, first_blocknum, id, revocation_basepoint_local, payment_basepoint_local, htlc_basepoint_local, delayed_payment_basepoint_local, funding_pubkey_local) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
.placeholders = 8,
.readonly = false,
},
{
@ -1876,4 +1876,4 @@ struct db_query db_sqlite3_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */
// SHA256STAMP:9b64275f5e8b221e0e7ae99a386d0fb5c5112b65eb893dc6c8f68c2e95d920d4
// SHA256STAMP:a7e5a416ba406c10a88ee92a1ff39ced4373e3ecb6f67a228dd08c506b62b23b

View File

@ -927,298 +927,298 @@ msgid "INSERT INTO peers (node_id, address) VALUES (?, ?);"
msgstr ""
#: wallet/wallet.c:1812
msgid "INSERT INTO channels (peer_id, first_blocknum, id) VALUES (?, ?, ?);"
msgid "INSERT INTO channels ( peer_id, first_blocknum, id, revocation_basepoint_local, payment_basepoint_local, htlc_basepoint_local, delayed_payment_basepoint_local, funding_pubkey_local) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:1838
#: wallet/wallet.c:1853
msgid "DELETE FROM channel_htlcs WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1844
#: wallet/wallet.c:1859
msgid "DELETE FROM htlc_sigs WHERE channelid=?"
msgstr ""
#: wallet/wallet.c:1850
#: wallet/wallet.c:1865
msgid "DELETE FROM channeltxs WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1857
#: wallet/wallet.c:1872
msgid "DELETE FROM channel_funding_inflights WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1863
#: wallet/wallet.c:1878
msgid "DELETE FROM shachains WHERE id IN ( SELECT shachain_remote_id FROM channels WHERE channels.id=?)"
msgstr ""
#: wallet/wallet.c:1873
#: wallet/wallet.c:1888
msgid "UPDATE channels SET state=?, peer_id=? WHERE channels.id=?"
msgstr ""
#: wallet/wallet.c:1887
#: wallet/wallet.c:1902
msgid "SELECT * FROM channels WHERE peer_id = ?;"
msgstr ""
#: wallet/wallet.c:1895
#: wallet/wallet.c:1910
msgid "DELETE FROM peers WHERE id=?"
msgstr ""
#: wallet/wallet.c:1906
#: wallet/wallet.c:1921
msgid "UPDATE outputs SET confirmation_height = ? WHERE prev_out_tx = ?"
msgstr ""
#: wallet/wallet.c:2009
#: wallet/wallet.c:2024
msgid "INSERT INTO channel_htlcs ( channel_id, channel_htlc_id, direction, msatoshi, cltv_expiry, payment_hash, payment_key, hstate, shared_secret, routing_onion, received_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:2062
#: wallet/wallet.c:2077
msgid "INSERT INTO channel_htlcs ( channel_id, channel_htlc_id, direction, origin_htlc, msatoshi, cltv_expiry, payment_hash, payment_key, hstate, routing_onion, malformed_onion, partid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?);"
msgstr ""
#: wallet/wallet.c:2123
#: wallet/wallet.c:2138
msgid "UPDATE channel_htlcs SET hstate=?, payment_key=?, malformed_onion=?, failuremsg=?, localfailmsg=?, we_filled=? WHERE id=?"
msgstr ""
#: wallet/wallet.c:2339
#: wallet/wallet.c:2354
msgid "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, payment_hash, payment_key, routing_onion, failuremsg, malformed_onion, origin_htlc, shared_secret, received_time, we_filled FROM channel_htlcs WHERE direction= ? AND channel_id= ? AND hstate != ?"
msgstr ""
#: wallet/wallet.c:2386
#: wallet/wallet.c:2401
msgid "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, payment_hash, payment_key, routing_onion, failuremsg, malformed_onion, origin_htlc, shared_secret, received_time, partid, localfailmsg FROM channel_htlcs WHERE direction = ? AND channel_id = ? AND hstate != ?"
msgstr ""
#: wallet/wallet.c:2517
#: wallet/wallet.c:2532
msgid "SELECT channel_id, direction, cltv_expiry, channel_htlc_id, payment_hash FROM channel_htlcs WHERE channel_id = ?;"
msgstr ""
#: wallet/wallet.c:2551
#: wallet/wallet.c:2566
msgid "DELETE FROM channel_htlcs WHERE direction = ? AND origin_htlc = ? AND payment_hash = ? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2604
#: wallet/wallet.c:2619
msgid "SELECT status FROM payments WHERE payment_hash=? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2622
#: wallet/wallet.c:2637
msgid "INSERT INTO payments ( status, payment_hash, destination, msatoshi, timestamp, path_secrets, route_nodes, route_channels, msatoshi_sent, description, bolt11, total_msat, partid, local_offer_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:2711
#: wallet/wallet.c:2726
msgid "DELETE FROM payments WHERE payment_hash = ? AND partid = ?"
msgstr ""
#: wallet/wallet.c:2725
#: wallet/wallet.c:2740
msgid "DELETE FROM payments WHERE payment_hash = ?"
msgstr ""
#: wallet/wallet.c:2826
#: wallet/wallet.c:2841
msgid "SELECT id, status, destination, msatoshi, payment_hash, timestamp, payment_preimage, path_secrets, route_nodes, route_channels, msatoshi_sent, description, bolt11, failonionreply, total_msat, partid, local_offer_id FROM payments WHERE payment_hash = ? AND partid = ?"
msgstr ""
#: wallet/wallet.c:2876
#: wallet/wallet.c:2891
msgid "UPDATE payments SET status=? WHERE payment_hash=? AND partid=?"
msgstr ""
#: wallet/wallet.c:2886
#: wallet/wallet.c:2901
msgid "UPDATE payments SET payment_preimage=? WHERE payment_hash=? AND partid=?"
msgstr ""
#: wallet/wallet.c:2896
#: wallet/wallet.c:2911
msgid "UPDATE payments SET path_secrets = NULL , route_nodes = NULL , route_channels = NULL WHERE payment_hash = ? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2928
#: wallet/wallet.c:2943
msgid "SELECT failonionreply, faildestperm, failindex, failcode, failnode, failchannel, failupdate, faildetail, faildirection FROM payments WHERE payment_hash=? AND partid=?;"
msgstr ""
#: wallet/wallet.c:2995
#: wallet/wallet.c:3010
msgid "UPDATE payments SET failonionreply=? , faildestperm=? , failindex=? , failcode=? , failnode=? , failchannel=? , failupdate=? , faildetail=? , faildirection=? WHERE payment_hash=? AND partid=?;"
msgstr ""
#: wallet/wallet.c:3054
#: wallet/wallet.c:3069
msgid "SELECT id, status, destination, msatoshi, payment_hash, timestamp, payment_preimage, path_secrets, route_nodes, route_channels, msatoshi_sent, description, bolt11, failonionreply, total_msat, partid, local_offer_id FROM payments WHERE payment_hash = ?;"
msgstr ""
#: wallet/wallet.c:3076
#: wallet/wallet.c:3091
msgid "SELECT id, status, destination, msatoshi, payment_hash, timestamp, payment_preimage, path_secrets, route_nodes, route_channels, msatoshi_sent, description, bolt11, failonionreply, total_msat, partid, local_offer_id FROM payments ORDER BY id;"
msgstr ""
#: wallet/wallet.c:3127
#: wallet/wallet.c:3142
msgid "SELECT id, status, destination, msatoshi, payment_hash, timestamp, payment_preimage, path_secrets, route_nodes, route_channels, msatoshi_sent, description, bolt11, failonionreply, total_msat, partid, local_offer_id FROM payments WHERE local_offer_id = ?;"
msgstr ""
#: wallet/wallet.c:3172
#: wallet/wallet.c:3187
msgid "DELETE FROM htlc_sigs WHERE channelid = ?"
msgstr ""
#: wallet/wallet.c:3179
#: wallet/wallet.c:3194
msgid "INSERT INTO htlc_sigs (channelid, signature) VALUES (?, ?)"
msgstr ""
#: wallet/wallet.c:3191
#: wallet/wallet.c:3206
msgid "SELECT blobval FROM vars WHERE name='genesis_hash'"
msgstr ""
#: wallet/wallet.c:3215
#: wallet/wallet.c:3230
msgid "INSERT INTO vars (name, blobval) VALUES ('genesis_hash', ?);"
msgstr ""
#: wallet/wallet.c:3233
#: wallet/wallet.c:3248
msgid "SELECT txid, outnum FROM utxoset WHERE spendheight < ?"
msgstr ""
#: wallet/wallet.c:3245
#: wallet/wallet.c:3260
msgid "DELETE FROM utxoset WHERE spendheight < ?"
msgstr ""
#: wallet/wallet.c:3253 wallet/wallet.c:3367
#: wallet/wallet.c:3268 wallet/wallet.c:3382
msgid "INSERT INTO blocks (height, hash, prev_hash) VALUES (?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3272
#: wallet/wallet.c:3287
msgid "DELETE FROM blocks WHERE hash = ?"
msgstr ""
#: wallet/wallet.c:3278
#: wallet/wallet.c:3293
msgid "SELECT * FROM blocks WHERE height >= ?;"
msgstr ""
#: wallet/wallet.c:3287
#: wallet/wallet.c:3302
msgid "DELETE FROM blocks WHERE height > ?"
msgstr ""
#: wallet/wallet.c:3299
#: wallet/wallet.c:3314
msgid "UPDATE outputs SET spend_height = ?, status = ? WHERE prev_out_tx = ? AND prev_out_index = ?"
msgstr ""
#: wallet/wallet.c:3317
#: wallet/wallet.c:3332
msgid "UPDATE utxoset SET spendheight = ? WHERE txid = ? AND outnum = ?"
msgstr ""
#: wallet/wallet.c:3340 wallet/wallet.c:3378
#: wallet/wallet.c:3355 wallet/wallet.c:3393
msgid "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3404
#: wallet/wallet.c:3419
msgid "SELECT height FROM blocks WHERE height = ?"
msgstr ""
#: wallet/wallet.c:3417
#: wallet/wallet.c:3432
msgid "SELECT txid, spendheight, scriptpubkey, satoshis FROM utxoset WHERE blockheight = ? AND txindex = ? AND outnum = ? AND spendheight IS NULL"
msgstr ""
#: wallet/wallet.c:3459
#: wallet/wallet.c:3474
msgid "SELECT blockheight, txindex, outnum FROM utxoset WHERE spendheight = ?"
msgstr ""
#: wallet/wallet.c:3490 wallet/wallet.c:3650
#: wallet/wallet.c:3505 wallet/wallet.c:3665
msgid "SELECT blockheight FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3500
#: wallet/wallet.c:3515
msgid "INSERT INTO transactions ( id, blockheight, txindex, rawtx) VALUES (?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3521
#: wallet/wallet.c:3536
msgid "UPDATE transactions SET blockheight = ?, txindex = ? WHERE id = ?"
msgstr ""
#: wallet/wallet.c:3538
#: wallet/wallet.c:3553
msgid "INSERT INTO transaction_annotations (txid, idx, location, type, channel) VALUES (?, ?, ?, ?, ?) ON CONFLICT(txid,idx) DO NOTHING;"
msgstr ""
#: wallet/wallet.c:3570
#: wallet/wallet.c:3585
msgid "SELECT type, channel_id FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3586
#: wallet/wallet.c:3601
msgid "UPDATE transactions SET type = ?, channel_id = ? WHERE id = ?"
msgstr ""
#: wallet/wallet.c:3605
#: wallet/wallet.c:3620
msgid "SELECT type FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3628
#: wallet/wallet.c:3643
msgid "SELECT rawtx FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3674
#: wallet/wallet.c:3689
msgid "SELECT blockheight, txindex FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3702
#: wallet/wallet.c:3717
msgid "SELECT id FROM transactions WHERE blockheight=?"
msgstr ""
#: wallet/wallet.c:3721
#: wallet/wallet.c:3736
msgid "INSERT INTO channeltxs ( channel_id, type, transaction_id, input_num, blockheight) VALUES (?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3745
#: wallet/wallet.c:3760
msgid "SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"
msgstr ""
#: wallet/wallet.c:3766
#: wallet/wallet.c:3781
msgid "SELECT c.type, c.blockheight, t.rawtx, c.input_num, c.blockheight - t.blockheight + 1 AS depth, t.id as txid FROM channeltxs c JOIN transactions t ON t.id = c.transaction_id WHERE c.channel_id = ? ORDER BY c.id ASC;"
msgstr ""
#: wallet/wallet.c:3811
#: wallet/wallet.c:3826
msgid "UPDATE forwarded_payments SET in_msatoshi=?, out_msatoshi=?, state=?, resolved_time=?, failcode=? WHERE in_htlc_id=?"
msgstr ""
#: wallet/wallet.c:3869
#: wallet/wallet.c:3884
msgid "INSERT INTO forwarded_payments ( in_htlc_id, out_htlc_id, in_channel_scid, out_channel_scid, in_msatoshi, out_msatoshi, state, received_time, resolved_time, failcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3928
#: wallet/wallet.c:3943
msgid "SELECT CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)FROM forwarded_payments WHERE state = ?;"
msgstr ""
#: wallet/wallet.c:3977
#: wallet/wallet.c:3992
msgid "SELECT f.state, in_msatoshi, out_msatoshi, hin.payment_hash as payment_hash, in_channel_scid, out_channel_scid, f.received_time, f.resolved_time, f.failcode FROM forwarded_payments f LEFT JOIN channel_htlcs hin ON (f.in_htlc_id = hin.id) WHERE (1 = ? OR f.state = ?) AND (1 = ? OR f.in_channel_scid = ?) AND (1 = ? OR f.out_channel_scid = ?)"
msgstr ""
#: wallet/wallet.c:4099
#: wallet/wallet.c:4114
msgid "SELECT t.id, t.rawtx, t.blockheight, t.txindex, t.type as txtype, c2.short_channel_id as txchan, a.location, a.idx as ann_idx, a.type as annotation_type, c.short_channel_id FROM transactions t LEFT JOIN transaction_annotations a ON (a.txid = t.id) LEFT JOIN channels c ON (a.channel = c.id) LEFT JOIN channels c2 ON (t.channel_id = c2.id) ORDER BY t.blockheight, t.txindex ASC"
msgstr ""
#: wallet/wallet.c:4193
#: wallet/wallet.c:4208
msgid "INSERT INTO penalty_bases ( channel_id, commitnum, txid, outnum, amount) VALUES (?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:4218
#: wallet/wallet.c:4233
msgid "SELECT commitnum, txid, outnum, amount FROM penalty_bases WHERE channel_id = ?"
msgstr ""
#: wallet/wallet.c:4242
#: wallet/wallet.c:4257
msgid "DELETE FROM penalty_bases WHERE channel_id = ? AND commitnum = ?"
msgstr ""
#: wallet/wallet.c:4260
#: wallet/wallet.c:4275
msgid "SELECT 1 FROM offers WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4273
#: wallet/wallet.c:4288
msgid "INSERT INTO offers ( offer_id, bolt12, label, status) VALUES (?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:4300
#: wallet/wallet.c:4315
msgid "SELECT bolt12, label, status FROM offers WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4328
#: wallet/wallet.c:4343
msgid "SELECT offer_id FROM offers;"
msgstr ""
#: wallet/wallet.c:4354
#: wallet/wallet.c:4369
msgid "UPDATE offers SET status=? WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4365
#: wallet/wallet.c:4380
msgid "UPDATE invoices SET state=? WHERE state=? AND local_offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4393
#: wallet/wallet.c:4408
msgid "SELECT status FROM offers WHERE offer_id = ?;"
msgstr ""
@ -1237,4 +1237,4 @@ msgstr ""
#: wallet/test/run-wallet.c:802
msgid "INSERT INTO channels (id) VALUES (1);"
msgstr ""
# SHA256STAMP:66ddf4612050d39dda1ae651a34cf5fe89390ef39c7648a3dca2f3a932a15eb4
# SHA256STAMP:681af9d5ce7629449d850f3a51a12a262a14f788f3704b3c8062e9e04afe8576

View File

@ -1810,10 +1810,25 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
/* Insert a stub, that we update, unifies INSERT and UPDATE paths */
stmt = db_prepare_v2(
w->db, SQL("INSERT INTO channels ("
"peer_id, first_blocknum, id) VALUES (?, ?, ?);"));
" peer_id"
", first_blocknum"
", id"
", revocation_basepoint_local"
", payment_basepoint_local"
", htlc_basepoint_local"
", delayed_payment_basepoint_local"
", funding_pubkey_local"
") VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
db_bind_u64(stmt, 0, chan->peer->dbid);
db_bind_int(stmt, 1, chan->first_blocknum);
db_bind_int(stmt, 2, chan->dbid);
db_bind_pubkey(stmt, 3, &chan->local_basepoints.revocation);
db_bind_pubkey(stmt, 4, &chan->local_basepoints.payment);
db_bind_pubkey(stmt, 5, &chan->local_basepoints.htlc);
db_bind_pubkey(stmt, 6, &chan->local_basepoints.delayed_payment);
db_bind_pubkey(stmt, 7, &chan->local_funding_pubkey);
db_exec_prepared_v2(take(stmt));
wallet_channel_config_insert(w, &chan->our_config);