From d8c9e70c0c6878053819fa8a8904d9659d2c92ce Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Mon, 30 Sep 2019 16:47:16 -0500 Subject: [PATCH] wallet-df: save our_funds amount to channel record We'll need it to represent to user in `listpeers` --- lightningd/channel.c | 2 + lightningd/channel.h | 5 ++ lightningd/opening_control.c | 7 ++- wallet/db.c | 25 ++++++++ wallet/wallet.c | 114 ++++++++++++++++++----------------- 5 files changed, 98 insertions(+), 55 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 33dcb13d0..4e00bbc17 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -159,6 +159,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u16 funding_outnum, struct amount_sat funding, struct amount_msat push, + struct amount_sat our_funds, bool remote_funding_locked, /* NULL or stolen */ struct short_channel_id *scid, @@ -227,6 +228,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->funding_outnum = funding_outnum; channel->funding = funding; channel->push = push; + channel->our_funds = our_funds; channel->remote_funding_locked = remote_funding_locked; channel->scid = tal_steal(channel, scid); channel->our_msat = our_msat; diff --git a/lightningd/channel.h b/lightningd/channel.h index 9fc5c432f..fbd30e010 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -60,6 +60,10 @@ struct channel { struct bitcoin_txid funding_txid; u16 funding_outnum; struct amount_sat funding; + + /* Our original funds, in funding amount */ + struct amount_sat our_funds; + struct amount_msat push; bool remote_funding_locked; /* Channel if locked locally. */ @@ -149,6 +153,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u16 funding_outnum, struct amount_sat funding, struct amount_msat push, + struct amount_sat our_funds, bool remote_funding_locked, /* NULL or stolen */ struct short_channel_id *scid STEALS, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index a19b43751..60730c86f 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -174,6 +174,7 @@ wallet_commit_channel(struct lightningd *ld, { struct channel *channel; struct amount_msat our_msat; + struct amount_sat local_funding; s64 final_key_idx; bool option_static_remotekey; @@ -193,8 +194,11 @@ wallet_commit_channel(struct lightningd *ld, &funding)); return NULL; } - } else + local_funding = funding; + } else { our_msat = push; + local_funding = AMOUNT_SAT(0); + } channel_info->fee_states = new_fee_states(uc, uc->fc ? LOCAL : REMOTE, &feerate); @@ -238,6 +242,7 @@ wallet_commit_channel(struct lightningd *ld, funding_outnum, funding, push, + local_funding, false, /* !remote_funding_locked */ NULL, /* no scid yet */ /* The three arguments below are msatoshi_to_us, diff --git a/wallet/db.c b/wallet/db.c index 3e06ce436..673d4d88a 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -20,6 +20,8 @@ struct migration { static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db); +static void migrate_our_funding(struct lightningd *ld, struct db *db); + /* Do not reorder or remove elements from this array, it is used to * migrate existing databases from a previous state, based on the * string indices */ @@ -596,6 +598,7 @@ static struct migration dbmigrations[] = { * Turn anything in transition into a WIRE_TEMPORARY_NODE_FAILURE. */ {SQL("ALTER TABLE channel_htlcs ADD localfailmsg BLOB;"), NULL}, {SQL("UPDATE channel_htlcs SET localfailmsg=decode('2002', 'hex') WHERE malformed_onion != 0 AND direction = 1;"), NULL}, + {SQL("ALTER TABLE channels ADD our_funding_satoshi BIGINT DEFAULT 0;"), migrate_our_funding}, }; /* Leak tracking. */ @@ -1072,6 +1075,28 @@ static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db tal_free(stmt); } +/* We've added a column `our_funding_satoshis`, since channels can now + * have funding for either channel participant. We need to 'backfill' this + * data, however. We can do this using the fact that our_funding_satoshi + * is the same as the funding_satoshi for every channel where we are + * the `funder` + */ +static void migrate_our_funding(struct lightningd *ld, struct db *db) +{ + struct db_stmt *stmt; + + /* Statement to update record */ + stmt = db_prepare_v2(db, SQL("UPDATE channels" + " SET our_funding_satoshi = funding_satoshi" + " WHERE funder = 0;")); /* 0 == LOCAL */ + db_exec_prepared_v2(stmt); + if (stmt->error) + db_fatal("Error migrating funding satoshis to our_funding (%s)", + stmt->error); + + tal_free(stmt); +} + void db_bind_null(struct db_stmt *stmt, int pos) { assert(pos < tal_count(stmt->bindings)); diff --git a/wallet/wallet.c b/wallet/wallet.c index 071bfa795..452ccdfe1 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -873,7 +873,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm struct basepoints local_basepoints; struct pubkey local_funding_pubkey; struct pubkey *future_per_commitment_point; - struct amount_sat funding_sat; + struct amount_sat funding_sat, our_funding_sat; struct amount_msat push_msat, our_msat, msat_to_us_min, msat_to_us_max; peer_dbid = db_column_u64(stmt, 1); @@ -893,15 +893,15 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm scid = NULL; } - ok &= wallet_shachain_load(w, db_column_u64(stmt, 27), &wshachain); + ok &= wallet_shachain_load(w, db_column_u64(stmt, 28), &wshachain); - remote_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 28, u8); - local_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 46, u8); + remote_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 29, u8); + local_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 47, u8); /* Do we have a last_sent_commit, if yes, populate */ - if (!db_column_is_null(stmt, 41)) { - const u8 *cursor = db_column_blob(stmt, 41); - size_t len = db_column_bytes(stmt, 41); + if (!db_column_is_null(stmt, 42)) { + const u8 *cursor = db_column_blob(stmt, 42); + size_t len = db_column_bytes(stmt, 42); size_t n = 0; last_sent_commit = tal_arr(tmpctx, struct changed_htlc, n); while (len) { @@ -913,33 +913,33 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm last_sent_commit = NULL; #ifdef COMPAT_V060 - if (!last_sent_commit && !db_column_is_null(stmt, 30)) { + if (!last_sent_commit && !db_column_is_null(stmt, 31)) { last_sent_commit = tal(tmpctx, struct changed_htlc); - last_sent_commit->newstate = db_column_u64(stmt, 30); - last_sent_commit->id = db_column_u64(stmt, 31); + last_sent_commit->newstate = db_column_u64(stmt, 31); + last_sent_commit->id = db_column_u64(stmt, 32); } #endif - if (!db_column_is_null(stmt, 40)) { + if (!db_column_is_null(stmt, 41)) { future_per_commitment_point = tal(tmpctx, struct pubkey); - db_column_pubkey(stmt, 40, future_per_commitment_point); + db_column_pubkey(stmt, 41, future_per_commitment_point); } else future_per_commitment_point = NULL; channel_config_id = db_column_u64(stmt, 3); ok &= wallet_channel_config_load(w, channel_config_id, &our_config); db_column_sha256d(stmt, 12, &funding_txid.shad); - ok &= db_column_signature(stmt, 33, &last_sig.s); + ok &= db_column_signature(stmt, 34, &last_sig.s); last_sig.sighash_type = SIGHASH_ALL; /* Populate channel_info */ - db_column_pubkey(stmt, 18, &channel_info.remote_fundingkey); - db_column_pubkey(stmt, 19, &channel_info.theirbase.revocation); - db_column_pubkey(stmt, 20, &channel_info.theirbase.payment); - db_column_pubkey(stmt, 21, &channel_info.theirbase.htlc); - db_column_pubkey(stmt, 22, &channel_info.theirbase.delayed_payment); - db_column_pubkey(stmt, 23, &channel_info.remote_per_commit); - db_column_pubkey(stmt, 24, &channel_info.old_remote_per_commit); + db_column_pubkey(stmt, 19, &channel_info.remote_fundingkey); + db_column_pubkey(stmt, 20, &channel_info.theirbase.revocation); + db_column_pubkey(stmt, 21, &channel_info.theirbase.payment); + db_column_pubkey(stmt, 22, &channel_info.theirbase.htlc); + db_column_pubkey(stmt, 23, &channel_info.theirbase.delayed_payment); + db_column_pubkey(stmt, 24, &channel_info.remote_per_commit); + db_column_pubkey(stmt, 25, &channel_info.old_remote_per_commit); wallet_channel_config_load(w, db_column_u64(stmt, 4), &channel_info.their_config); @@ -956,7 +956,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm return NULL; } - final_key_idx = db_column_u64(stmt, 29); + final_key_idx = db_column_u64(stmt, 30); if (final_key_idx < 0) { log_broken(w->log, "%s: Final key < 0", __func__); return NULL; @@ -966,10 +966,11 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm &local_basepoints, &local_funding_pubkey); db_column_amount_sat(stmt, 14, &funding_sat); - db_column_amount_msat(stmt, 16, &push_msat); - db_column_amount_msat(stmt, 17, &our_msat); - db_column_amount_msat(stmt, 38, &msat_to_us_min); - db_column_amount_msat(stmt, 39, &msat_to_us_max); + db_column_amount_sat(stmt, 15, &our_funding_sat); + db_column_amount_msat(stmt, 17, &push_msat); + db_column_amount_msat(stmt, 18, &our_msat); + db_column_amount_msat(stmt, 39, &msat_to_us_min); + db_column_amount_msat(stmt, 40, &msat_to_us_max); /* We want it to take this, rather than copy. */ take(channel_info.fee_states); @@ -989,12 +990,13 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm db_column_int(stmt, 13), funding_sat, push_msat, - db_column_int(stmt, 15) != 0, + our_funding_sat, + db_column_int(stmt, 16) != 0, scid, our_msat, msat_to_us_min, /* msatoshi_to_us_min */ msat_to_us_max, /* msatoshi_to_us_max */ - db_column_tx(tmpctx, stmt, 32), + db_column_tx(tmpctx, stmt, 33), &last_sig, wallet_htlc_sigs_load(tmpctx, w, db_column_u64(stmt, 0)), @@ -1002,19 +1004,19 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm remote_shutdown_scriptpubkey, local_shutdown_scriptpubkey, final_key_idx, - db_column_int(stmt, 34) != 0, + db_column_int(stmt, 35) != 0, last_sent_commit, - db_column_u64(stmt, 35), - db_column_int(stmt, 36), + db_column_u64(stmt, 36), db_column_int(stmt, 37), + db_column_int(stmt, 38), /* Not connected */ false, &local_basepoints, &local_funding_pubkey, future_per_commitment_point, - db_column_int(stmt, 42), db_column_int(stmt, 43), - db_column_arr(tmpctx, stmt, 44, u8), - db_column_int(stmt, 45)); + db_column_int(stmt, 44), + db_column_arr(tmpctx, stmt, 45, u8), + db_column_int(stmt, 46)); return chan; } @@ -1055,6 +1057,7 @@ static bool wallet_channels_load_active(struct wallet *w) ", funding_tx_id" ", funding_tx_outnum" ", funding_satoshi" + ", our_funding_satoshi" ", funding_locked_remote" ", push_msatoshi" ", msatoshi_local" @@ -1335,6 +1338,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " funding_tx_id=?," " funding_tx_outnum=?," " funding_satoshi=?," + " our_funding_satoshi=?," " funding_locked_remote=?," " push_msatoshi=?," " msatoshi_local=?," @@ -1371,37 +1375,39 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) db_bind_int(stmt, 10, chan->funding_outnum); db_bind_amount_sat(stmt, 11, &chan->funding); - db_bind_int(stmt, 12, chan->remote_funding_locked); - db_bind_amount_msat(stmt, 13, &chan->push); - db_bind_amount_msat(stmt, 14, &chan->our_msat); + db_bind_amount_sat(stmt, 12, &chan->our_funds); + db_bind_int(stmt, 13, chan->remote_funding_locked); + db_bind_amount_msat(stmt, 14, &chan->push); + db_bind_amount_msat(stmt, 15, &chan->our_msat); if (chan->shutdown_scriptpubkey[REMOTE]) - db_bind_blob(stmt, 15, chan->shutdown_scriptpubkey[REMOTE], + db_bind_blob(stmt, 16, chan->shutdown_scriptpubkey[REMOTE], tal_count(chan->shutdown_scriptpubkey[REMOTE])); else - db_bind_null(stmt, 15); + db_bind_null(stmt, 16); - db_bind_u64(stmt, 16, chan->final_key_idx); - db_bind_u64(stmt, 17, chan->our_config.id); - db_bind_tx(stmt, 18, chan->last_tx); - db_bind_signature(stmt, 19, &chan->last_sig.s); - db_bind_int(stmt, 20, chan->last_was_revoke); - db_bind_int(stmt, 21, chan->min_possible_feerate); - db_bind_int(stmt, 22, chan->max_possible_feerate); - db_bind_amount_msat(stmt, 23, &chan->msat_to_us_min); - db_bind_amount_msat(stmt, 24, &chan->msat_to_us_max); - db_bind_int(stmt, 25, chan->feerate_base); - db_bind_int(stmt, 26, chan->feerate_ppm); + db_bind_u64(stmt, 17, chan->final_key_idx); + db_bind_u64(stmt, 18, chan->our_config.id); + db_bind_tx(stmt, 19, chan->last_tx); + db_bind_signature(stmt, 20, &chan->last_sig.s); + db_bind_int(stmt, 21, chan->last_was_revoke); + db_bind_int(stmt, 22, chan->min_possible_feerate); + db_bind_int(stmt, 23, chan->max_possible_feerate); + db_bind_amount_msat(stmt, 24, &chan->msat_to_us_min); + db_bind_amount_msat(stmt, 25, &chan->msat_to_us_max); + db_bind_int(stmt, 26, chan->feerate_base); + db_bind_int(stmt, 27, chan->feerate_ppm); if (chan->remote_upfront_shutdown_script) db_bind_blob( - stmt, 27, chan->remote_upfront_shutdown_script, + stmt, 28, chan->remote_upfront_shutdown_script, tal_count(chan->remote_upfront_shutdown_script)); else - db_bind_null(stmt, 27); - db_bind_int(stmt, 28, chan->option_static_remotekey); - db_bind_blob(stmt, 29, chan->shutdown_scriptpubkey[LOCAL], + db_bind_null(stmt, 28); + + db_bind_int(stmt, 29, chan->option_static_remotekey); + db_bind_blob(stmt, 30, chan->shutdown_scriptpubkey[LOCAL], tal_count(chan->shutdown_scriptpubkey[LOCAL])); - db_bind_u64(stmt, 30, chan->dbid); + db_bind_u64(stmt, 31, chan->dbid); db_exec_prepared_v2(take(stmt)); wallet_channel_config_save(w, &chan->channel_info.their_config);