df: add helper for creating an "unsaved" channel

Channel that's not saved to database yet
This commit is contained in:
niftynei 2021-01-19 19:09:38 -06:00 committed by neil saitug
parent 1786fcbbdc
commit 538fd670ce
7 changed files with 171 additions and 79 deletions

View File

@ -198,6 +198,85 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
return oa;
}
struct channel *new_unsaved_channel(struct peer *peer,
u32 feerate_base,
u32 feerate_ppm)
{
struct lightningd *ld = peer->ld;
struct channel *channel = tal(ld, struct channel);
channel->peer = peer;
/* Not saved to the database yet! */
channel->unsaved_dbid = wallet_get_channel_dbid(ld->wallet);
/* A zero value database id means it's not saved in the database yet */
channel->dbid = 0;
channel->error = NULL;
channel->htlc_timeout = NULL;
channel->openchannel_signed_cmd = NULL;
channel->state = DUALOPEND_OPEN_INIT;
channel->owner = NULL;
memset(&channel->billboard, 0, sizeof(channel->billboard));
channel->billboard.transient = tal_fmt(channel, "%s",
"Empty channel init'd");
channel->log = new_log(channel, ld->log_book,
&peer->id,
"chan#%"PRIu64,
channel->unsaved_dbid);
memset(&channel->cid, 0xFF, sizeof(channel->cid));
channel->our_config.id = 0;
channel->open_attempt = NULL;
channel->last_htlc_sigs = NULL;
channel->remote_funding_locked = false;
channel->scid = NULL;
channel->next_index[LOCAL] = 1;
channel->next_index[REMOTE] = 1;
channel->next_htlc_id = 0;
/* FIXME: remove push when v1 deprecated */
channel->push = AMOUNT_MSAT(0);
channel->closing_fee_negotiation_step = 50;
channel->closing_fee_negotiation_step_unit
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
/* Channel is connected! */
channel->connected = true;
channel->shutdown_scriptpubkey[REMOTE] = NULL;
channel->last_was_revoke = false;
channel->last_sent_commit = NULL;
channel->last_tx_type = TX_UNKNOWN;
channel->feerate_base = feerate_base;
channel->feerate_ppm = feerate_ppm;
/* closer not yet known */
channel->closer = NUM_SIDES;
/* BOLT-7b04b1461739c5036add61782d58ac490842d98b #9
* | 222/223 | `option_dual_fund`
* | Use v2 of channel open, enables dual funding
* | IN9
* | `option_anchor_outputs` */
channel->option_static_remotekey = true;
channel->option_anchor_outputs = true;
channel->future_per_commitment_point = NULL;
/* No shachain yet */
channel->their_shachain.id = 0;
shachain_init(&channel->their_shachain.chain);
get_channel_basepoints(ld, &peer->id, channel->unsaved_dbid,
&channel->local_basepoints,
&channel->local_funding_pubkey);
channel->forgets = tal_arr(channel, struct command *, 0);
list_add_tail(&peer->channels, &channel->list);
channel->rr_number = peer->ld->rr_counter++;
tal_add_destructor(channel, destroy_channel);
list_head_init(&channel->inflights);
return channel;
}
struct channel *new_channel(struct peer *peer, u64 dbid,
/* NULL or stolen */
struct wallet_shachain *their_shachain,
@ -258,6 +337,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
assert(dbid != 0);
channel->peer = peer;
channel->dbid = dbid;
channel->unsaved_dbid = 0;
channel->error = NULL;
channel->htlc_timeout = NULL;
channel->open_attempt = NULL;

View File

@ -74,6 +74,9 @@ struct channel {
/* Database ID: 0 == not in db yet */
u64 dbid;
/* Populated by new_unsaved_channel */
u64 unsaved_dbid;
/* Error message (iff in error state) */
u8 *error;
@ -205,6 +208,11 @@ struct channel {
struct command *openchannel_signed_cmd;
};
/* For v2 opens, a channel that has not yet been committed/saved to disk */
struct channel *new_unsaved_channel(struct peer *peer,
u32 feerate_base,
u32 feerate_ppm);
struct open_attempt *new_channel_open_attempt(struct channel *channel);
struct channel *new_channel(struct peer *peer, u64 dbid,

View File

@ -1834,4 +1834,4 @@ struct db_query db_postgres_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */
// SHA256STAMP:bd2601eacac93b31e97a7bacfd71545dc748a47e40babb05e7e8dd77b009417d
// SHA256STAMP:9db19d2c8ea068737d26c097552cb01a25c884ee904fd8901e12445bacff45db

View File

@ -1834,4 +1834,4 @@ struct db_query db_sqlite3_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */
// SHA256STAMP:bd2601eacac93b31e97a7bacfd71545dc748a47e40babb05e7e8dd77b009417d
// SHA256STAMP:9db19d2c8ea068737d26c097552cb01a25c884ee904fd8901e12445bacff45db

View File

@ -898,299 +898,299 @@ msgstr ""
msgid "INSERT INTO peers (node_id, address) VALUES (?, ?);"
msgstr ""
#: wallet/wallet.c:1809
#: wallet/wallet.c:1812
msgid "INSERT INTO channels (peer_id, first_blocknum, id) VALUES (?, ?, ?);"
msgstr ""
#: wallet/wallet.c:1835
#: wallet/wallet.c:1838
msgid "DELETE FROM channel_htlcs WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1841
#: wallet/wallet.c:1844
msgid "DELETE FROM htlc_sigs WHERE channelid=?"
msgstr ""
#: wallet/wallet.c:1847
#: wallet/wallet.c:1850
msgid "DELETE FROM channeltxs WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1854
#: wallet/wallet.c:1857
msgid "DELETE FROM channel_funding_inflights WHERE channel_id=?"
msgstr ""
#: wallet/wallet.c:1860
#: wallet/wallet.c:1863
msgid "DELETE FROM shachains WHERE id IN ( SELECT shachain_remote_id FROM channels WHERE channels.id=?)"
msgstr ""
#: wallet/wallet.c:1870
#: wallet/wallet.c:1873
msgid "UPDATE channels SET state=?, peer_id=? WHERE channels.id=?"
msgstr ""
#: wallet/wallet.c:1884
#: wallet/wallet.c:1887
msgid "SELECT * FROM channels WHERE peer_id = ?;"
msgstr ""
#: wallet/wallet.c:1892
#: wallet/wallet.c:1895
msgid "DELETE FROM peers WHERE id=?"
msgstr ""
#: wallet/wallet.c:1903
#: wallet/wallet.c:1906
msgid "UPDATE outputs SET confirmation_height = ? WHERE prev_out_tx = ?"
msgstr ""
#: wallet/wallet.c:2006
#: wallet/wallet.c:2009
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:2059
#: wallet/wallet.c:2062
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:2120
#: wallet/wallet.c:2123
msgid "UPDATE channel_htlcs SET hstate=?, payment_key=?, malformed_onion=?, failuremsg=?, localfailmsg=?, we_filled=? WHERE id=?"
msgstr ""
#: wallet/wallet.c:2336
#: wallet/wallet.c:2339
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:2383
#: wallet/wallet.c:2386
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:2514
#: wallet/wallet.c:2517
msgid "SELECT channel_id, direction, cltv_expiry, channel_htlc_id, payment_hash FROM channel_htlcs WHERE channel_id = ?;"
msgstr ""
#: wallet/wallet.c:2548
#: wallet/wallet.c:2551
msgid "DELETE FROM channel_htlcs WHERE direction = ? AND origin_htlc = ? AND payment_hash = ? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2601
#: wallet/wallet.c:2604
msgid "SELECT status FROM payments WHERE payment_hash=? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2619
#: wallet/wallet.c:2622
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:2708
#: wallet/wallet.c:2711
msgid "DELETE FROM payments WHERE payment_hash = ? AND partid = ?"
msgstr ""
#: wallet/wallet.c:2722
#: wallet/wallet.c:2725
msgid "DELETE FROM payments WHERE payment_hash = ?"
msgstr ""
#: wallet/wallet.c:2823
#: wallet/wallet.c:2826
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:2873
#: wallet/wallet.c:2876
msgid "UPDATE payments SET status=? WHERE payment_hash=? AND partid=?"
msgstr ""
#: wallet/wallet.c:2883
#: wallet/wallet.c:2886
msgid "UPDATE payments SET payment_preimage=? WHERE payment_hash=? AND partid=?"
msgstr ""
#: wallet/wallet.c:2893
#: wallet/wallet.c:2896
msgid "UPDATE payments SET path_secrets = NULL , route_nodes = NULL , route_channels = NULL WHERE payment_hash = ? AND partid = ?;"
msgstr ""
#: wallet/wallet.c:2925
#: wallet/wallet.c:2928
msgid "SELECT failonionreply, faildestperm, failindex, failcode, failnode, failchannel, failupdate, faildetail, faildirection FROM payments WHERE payment_hash=? AND partid=?;"
msgstr ""
#: wallet/wallet.c:2992
#: wallet/wallet.c:2995
msgid "UPDATE payments SET failonionreply=? , faildestperm=? , failindex=? , failcode=? , failnode=? , failchannel=? , failupdate=? , faildetail=? , faildirection=? WHERE payment_hash=? AND partid=?;"
msgstr ""
#: wallet/wallet.c:3051
#: wallet/wallet.c:3054
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:3073
#: wallet/wallet.c:3076
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:3124
#: wallet/wallet.c:3127
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:3169
#: wallet/wallet.c:3172
msgid "DELETE FROM htlc_sigs WHERE channelid = ?"
msgstr ""
#: wallet/wallet.c:3176
#: wallet/wallet.c:3179
msgid "INSERT INTO htlc_sigs (channelid, signature) VALUES (?, ?)"
msgstr ""
#: wallet/wallet.c:3188
#: wallet/wallet.c:3191
msgid "SELECT blobval FROM vars WHERE name='genesis_hash'"
msgstr ""
#: wallet/wallet.c:3212
#: wallet/wallet.c:3215
msgid "INSERT INTO vars (name, blobval) VALUES ('genesis_hash', ?);"
msgstr ""
#: wallet/wallet.c:3230
#: wallet/wallet.c:3233
msgid "SELECT txid, outnum FROM utxoset WHERE spendheight < ?"
msgstr ""
#: wallet/wallet.c:3242
#: wallet/wallet.c:3245
msgid "DELETE FROM utxoset WHERE spendheight < ?"
msgstr ""
#: wallet/wallet.c:3250 wallet/wallet.c:3364
#: wallet/wallet.c:3253 wallet/wallet.c:3367
msgid "INSERT INTO blocks (height, hash, prev_hash) VALUES (?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3269
#: wallet/wallet.c:3272
msgid "DELETE FROM blocks WHERE hash = ?"
msgstr ""
#: wallet/wallet.c:3275
#: wallet/wallet.c:3278
msgid "SELECT * FROM blocks WHERE height >= ?;"
msgstr ""
#: wallet/wallet.c:3284
#: wallet/wallet.c:3287
msgid "DELETE FROM blocks WHERE height > ?"
msgstr ""
#: wallet/wallet.c:3296
#: wallet/wallet.c:3299
msgid "UPDATE outputs SET spend_height = ?, status = ? WHERE prev_out_tx = ? AND prev_out_index = ?"
msgstr ""
#: wallet/wallet.c:3314
#: wallet/wallet.c:3317
msgid "UPDATE utxoset SET spendheight = ? WHERE txid = ? AND outnum = ?"
msgstr ""
#: wallet/wallet.c:3337 wallet/wallet.c:3375
#: wallet/wallet.c:3340 wallet/wallet.c:3378
msgid "INSERT INTO utxoset ( txid, outnum, blockheight, spendheight, txindex, scriptpubkey, satoshis) VALUES(?, ?, ?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3401
#: wallet/wallet.c:3404
msgid "SELECT height FROM blocks WHERE height = ?"
msgstr ""
#: wallet/wallet.c:3414
#: wallet/wallet.c:3417
msgid "SELECT txid, spendheight, scriptpubkey, satoshis FROM utxoset WHERE blockheight = ? AND txindex = ? AND outnum = ? AND spendheight IS NULL"
msgstr ""
#: wallet/wallet.c:3456
#: wallet/wallet.c:3459
msgid "SELECT blockheight, txindex, outnum FROM utxoset WHERE spendheight = ?"
msgstr ""
#: wallet/wallet.c:3487 wallet/wallet.c:3647
#: wallet/wallet.c:3490 wallet/wallet.c:3650
msgid "SELECT blockheight FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3497
#: wallet/wallet.c:3500
msgid "INSERT INTO transactions ( id, blockheight, txindex, rawtx) VALUES (?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3518
#: wallet/wallet.c:3521
msgid "UPDATE transactions SET blockheight = ?, txindex = ? WHERE id = ?"
msgstr ""
#: wallet/wallet.c:3535
#: wallet/wallet.c:3538
msgid "INSERT INTO transaction_annotations (txid, idx, location, type, channel) VALUES (?, ?, ?, ?, ?) ON CONFLICT(txid,idx) DO NOTHING;"
msgstr ""
#: wallet/wallet.c:3567
#: wallet/wallet.c:3570
msgid "SELECT type, channel_id FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3583
#: wallet/wallet.c:3586
msgid "UPDATE transactions SET type = ?, channel_id = ? WHERE id = ?"
msgstr ""
#: wallet/wallet.c:3602
#: wallet/wallet.c:3605
msgid "SELECT type FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3625
#: wallet/wallet.c:3628
msgid "SELECT rawtx FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3671
#: wallet/wallet.c:3674
msgid "SELECT blockheight, txindex FROM transactions WHERE id=?"
msgstr ""
#: wallet/wallet.c:3699
#: wallet/wallet.c:3702
msgid "SELECT id FROM transactions WHERE blockheight=?"
msgstr ""
#: wallet/wallet.c:3718
#: wallet/wallet.c:3721
msgid "INSERT INTO channeltxs ( channel_id, type, transaction_id, input_num, blockheight) VALUES (?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:3742
#: wallet/wallet.c:3745
msgid "SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"
msgstr ""
#: wallet/wallet.c:3763
#: wallet/wallet.c:3766
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:3808
#: wallet/wallet.c:3811
msgid "UPDATE forwarded_payments SET in_msatoshi=?, out_msatoshi=?, state=?, resolved_time=?, failcode=? WHERE in_htlc_id=?"
msgstr ""
#: wallet/wallet.c:3866
#: wallet/wallet.c:3869
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:3925
#: wallet/wallet.c:3928
msgid "SELECT CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)FROM forwarded_payments WHERE state = ?;"
msgstr ""
#: wallet/wallet.c:3974
#: wallet/wallet.c:3977
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:4096
#: wallet/wallet.c:4099
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:4190
#: wallet/wallet.c:4193
msgid "INSERT INTO penalty_bases ( channel_id, commitnum, txid, outnum, amount) VALUES (?, ?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:4215
#: wallet/wallet.c:4218
msgid "SELECT commitnum, txid, outnum, amount FROM penalty_bases WHERE channel_id = ?"
msgstr ""
#: wallet/wallet.c:4239
#: wallet/wallet.c:4242
msgid "DELETE FROM penalty_bases WHERE channel_id = ? AND commitnum = ?"
msgstr ""
#: wallet/wallet.c:4257
#: wallet/wallet.c:4260
msgid "SELECT 1 FROM offers WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4270
#: wallet/wallet.c:4273
msgid "INSERT INTO offers ( offer_id, bolt12, label, status) VALUES (?, ?, ?, ?);"
msgstr ""
#: wallet/wallet.c:4297
#: wallet/wallet.c:4300
msgid "SELECT bolt12, label, status FROM offers WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4325
#: wallet/wallet.c:4328
msgid "SELECT offer_id FROM offers;"
msgstr ""
#: wallet/wallet.c:4351
#: wallet/wallet.c:4354
msgid "UPDATE offers SET status=? WHERE offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4362
#: wallet/wallet.c:4365
msgid "UPDATE invoices SET state=? WHERE state=? AND local_offer_id = ?;"
msgstr ""
#: wallet/wallet.c:4390
#: wallet/wallet.c:4393
msgid "SELECT status FROM offers WHERE offer_id = ?;"
msgstr ""
@ -1202,11 +1202,11 @@ msgstr ""
msgid "not a valid SQL statement"
msgstr ""
#: wallet/test/run-wallet.c:1399
#: wallet/test/run-wallet.c:1400
msgid "SELECT COUNT(1) FROM channel_funding_inflights WHERE channel_id = ?;"
msgstr ""
#: wallet/test/run-wallet.c:1597
#: wallet/test/run-wallet.c:1598
msgid "INSERT INTO channels (id) VALUES (1);"
msgstr ""
# SHA256STAMP:8eabbc819a99d6803565907ab1e4e9988a3aa2212149235cb43ffc2319ac903a
# SHA256STAMP:0bc40d2193358aa8f18c22aa844a9fae0d35341127b4d35cf15ea925a2f3fab9

View File

@ -1298,6 +1298,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
c1.last_sig.s = *sig;
c1.last_sig.sighash_type = SIGHASH_ALL;
c1.last_tx->chainparams = chainparams_for_network("bitcoin");
c1.unsaved_dbid = 0;
/* Init channel inflights */
list_head_init(&c1.inflights);

View File

@ -1801,6 +1801,9 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
{
struct db_stmt *stmt;
assert(chan->dbid != 0);
assert(chan->unsaved_dbid == 0);
if (chan->peer->dbid == 0)
wallet_peer_save(w, chan->peer);