Skip to content

Commit

Permalink
wallet: save thresholds for option_static_remotekey.
Browse files Browse the repository at this point in the history
Since we will soon be able to activate it on existing channels,
we need to mark the threshold.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jun 3, 2021
1 parent 8416af9 commit 2e347b1
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 198 deletions.
9 changes: 6 additions & 3 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ struct channel *new_unsaved_channel(struct peer *peer,
* | Use v2 of channel open, enables dual funding
* | IN9
* | `option_anchor_outputs` */
channel->option_static_remotekey = true;
channel->static_remotekey_start[LOCAL]
= channel->static_remotekey_start[REMOTE] = 0;
channel->option_anchor_outputs = true;
channel->future_per_commitment_point = NULL;

Expand Down Expand Up @@ -334,7 +335,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
u32 feerate_base,
u32 feerate_ppm,
const u8 *remote_upfront_shutdown_script,
bool option_static_remotekey,
u64 local_static_remotekey_start,
u64 remote_static_remotekey_start,
bool option_anchor_outputs,
enum side closer,
enum state_change reason,
Expand Down Expand Up @@ -423,7 +425,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->feerate_ppm = feerate_ppm;
channel->remote_upfront_shutdown_script
= tal_steal(channel, remote_upfront_shutdown_script);
channel->option_static_remotekey = option_static_remotekey;
channel->static_remotekey_start[LOCAL] = local_static_remotekey_start;
channel->static_remotekey_start[REMOTE] = remote_static_remotekey_start;
channel->option_anchor_outputs = option_anchor_outputs;
channel->forgets = tal_arr(channel, struct command *, 0);

Expand Down
7 changes: 4 additions & 3 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ struct channel {
/* If they used option_upfront_shutdown_script. */
const u8 *remote_upfront_shutdown_script;

/* Was this negotiated with `option_static_remotekey? */
bool option_static_remotekey;
/* At what commit numbers does `option_static_remotekey` apply? */
u64 static_remotekey_start[NUM_SIDES];

/* Was this negotiated with `option_anchor_outputs? */
bool option_anchor_outputs;
Expand Down Expand Up @@ -267,7 +267,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
u32 feerate_ppm,
/* NULL or stolen */
const u8 *remote_upfront_shutdown_script STEALS,
bool option_static_remotekey,
u64 local_static_remotekey_start,
u64 remote_static_remotekey_start,
bool option_anchor_outputs,
enum side closer,
enum state_change reason,
Expand Down
8 changes: 1 addition & 7 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,6 @@ void channel_fallen_behind(struct channel *channel, const u8 *msg)
* use its presence as a flag so set it any valid key in that case. */
if (!channel->future_per_commitment_point) {
struct pubkey *any = tal(channel, struct pubkey);
if (!channel->option_static_remotekey) {
channel_internal_error(channel,
"bad channel_fail_fallen_behind %s",
tal_hex(tmpctx, msg));
return;
}
if (!pubkey_from_node_id(any, &channel->peer->ld->id))
fatal("Our own id invalid?");
channel->future_per_commitment_point = any;
Expand Down Expand Up @@ -608,7 +602,7 @@ void peer_start_channeld(struct channel *channel,
remote_ann_bitcoin_sig,
/* Set at channel open, even if not
* negotiated now! */
channel->option_static_remotekey,
channel->next_index[LOCAL] >= channel->static_remotekey_start[LOCAL],
channel->option_anchor_outputs,
IFDEV(ld->dev_fast_gossip, false),
IFDEV(dev_fail_process_onionpacket, false),
Expand Down
3 changes: 2 additions & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
channel->future_per_commitment_point,
&channel->local_funding_pubkey,
&channel->channel_info.remote_fundingkey,
channel->option_static_remotekey,
/* FIXME! onchaind needs start numbers! */
channel->static_remotekey_start[LOCAL] == 0,
channel->option_anchor_outputs,
is_replay,
feerate_min(ld, NULL));
Expand Down
15 changes: 9 additions & 6 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ wallet_commit_channel(struct lightningd *ld,
struct amount_msat our_msat;
struct amount_sat local_funding;
s64 final_key_idx;
bool option_static_remotekey;
u64 static_remotekey_start;
bool option_anchor_outputs;

/* We cannot both be the fundee *and* have a `fundchannel_start`
Expand Down Expand Up @@ -153,10 +153,13 @@ wallet_commit_channel(struct lightningd *ld,
* transactions
*/
/* i.e. We set it now for the channel permanently. */
option_static_remotekey
= feature_negotiated(ld->our_features,
uc->peer->their_features,
OPT_STATIC_REMOTEKEY);
if (feature_negotiated(ld->our_features,
uc->peer->their_features,
OPT_STATIC_REMOTEKEY))
static_remotekey_start = 0;
else
static_remotekey_start = 0x7FFFFFFFFFFFFFFF;

option_anchor_outputs
= feature_negotiated(ld->our_features,
uc->peer->their_features,
Expand Down Expand Up @@ -209,7 +212,7 @@ wallet_commit_channel(struct lightningd *ld,
ld->config.fee_base,
ld->config.fee_per_satoshi,
remote_upfront_shutdown_script,
option_static_remotekey,
static_remotekey_start, static_remotekey_start,
option_anchor_outputs,
NUM_SIDES, /* closer not yet known */
uc->fc ? REASON_USER : REASON_REMOTE,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ static void json_add_channel(struct lightningd *ld,
json_add_null(response, "closer");

json_array_start(response, "features");
if (channel->option_static_remotekey)
if (channel->static_remotekey_start[LOCAL] != 0x7FFFFFFFFFFFFFFF)
json_add_string(response, NULL, "option_static_remotekey");
if (channel->option_anchor_outputs)
json_add_string(response, NULL, "option_anchor_outputs");
Expand Down
11 changes: 11 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,17 @@ static struct migration dbmigrations[] = {
{SQL("ALTER TABLE channels ADD shutdown_wrong_txid BLOB DEFAULT NULL"), NULL},
{SQL("ALTER TABLE channels ADD shutdown_wrong_outnum INTEGER DEFAULT NULL"), NULL},
{NULL, migrate_inflight_last_tx_to_psbt},
/* Channels can now change their type at specific commit indexes. */
{SQL("ALTER TABLE channels ADD local_static_remotekey_start BIGINT DEFAULT 0"),
NULL},
{SQL("ALTER TABLE channels ADD remote_static_remotekey_start BIGINT DEFAULT 0"),
NULL},
/* Set counter past 2^48 if they don't have option */
{SQL("UPDATE channels SET"
" remote_static_remotekey_start = 9223372036854775807,"
" local_static_remotekey_start = 9223372036854775807"
" WHERE option_static_remotekey = 0"),
NULL},
};

/* Leak tracking. */
Expand Down
32 changes: 25 additions & 7 deletions wallet/db_postgres_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e347b1

Please sign in to comment.