Skip to content

Commit

Permalink
df: persist our setting to disk, read back to dualopend at reinit
Browse files Browse the repository at this point in the history
It's not likely but possible that the node's settings will shift btw a
start and an RBF; we persist the setting to the database so we don't
lose it.

Right now holding onto it forever is kind of extra but maybe we'll
reuse the setting for splices? idk.

Should this be a channel type??
  • Loading branch information
niftynei authored and endothermicdev committed Feb 8, 2023
1 parent fa80f15 commit beec517
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 21 deletions.
4 changes: 3 additions & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
struct log *log,
const char *transient_billboard TAKES,
u8 channel_flags,
bool req_confirmed_ins_local,
bool req_confirmed_ins_remote,
const struct channel_config *our_config,
u32 minimum_depth,
Expand Down Expand Up @@ -431,7 +432,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
dbid);
} else
channel->log = tal_steal(channel, log);
channel->req_confirmed_ins = req_confirmed_ins_remote;
channel->req_confirmed_ins[LOCAL] = req_confirmed_ins_local;
channel->req_confirmed_ins[REMOTE] = req_confirmed_ins_remote;
channel->channel_flags = channel_flags;
channel->our_config = *our_config;
channel->minimum_depth = minimum_depth;
Expand Down
3 changes: 2 additions & 1 deletion lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct channel {
struct channel_config our_config;

/* Require confirmed inputs for interactive tx */
bool req_confirmed_ins;
bool req_confirmed_ins[NUM_SIDES];

/* Minimum funding depth (specified by us if they fund). */
u32 minimum_depth;
Expand Down Expand Up @@ -286,6 +286,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
struct log *log STEALS,
const char *transient_billboard TAKES,
u8 channel_flags,
bool req_confirmed_ins_local,
bool req_confirmed_ins_remote,
const struct channel_config *our_config,
u32 minimum_depth,
Expand Down
29 changes: 17 additions & 12 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct rbf_channel_payload {
struct amount_sat our_last_funding;
u32 funding_feerate_per_kw;
u32 locktime;
bool req_confirmed_ins;
bool req_confirmed_ins_remote;

/* General info */
u32 feerate_our_max;
Expand Down Expand Up @@ -230,7 +230,7 @@ static void rbf_channel_hook_serialize(struct rbf_channel_payload *payload,
json_add_amount_sat_msat(stream, "requested_lease_msat",
*payload->requested_lease_amt);
json_add_bool(stream, "require_confirmed_inputs",
payload->req_confirmed_ins);
payload->req_confirmed_ins_remote);
json_object_end(stream);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ struct openchannel2_payload {
struct amount_sat *requested_lease_amt;
u32 lease_blockheight_start;
u32 node_blockheight;
bool req_confirmed_ins;
bool req_confirmed_ins_remote;

struct amount_sat accepter_funding;
struct wally_psbt *psbt;
Expand Down Expand Up @@ -324,7 +324,7 @@ static void openchannel2_hook_serialize(struct openchannel2_payload *payload,
payload->node_blockheight);
}
json_add_bool(stream, "require_confirmed_inputs",
payload->req_confirmed_ins);
payload->req_confirmed_ins_remote);
json_object_end(stream);
}

Expand All @@ -346,7 +346,7 @@ openchannel2_changed_hook_serialize(struct openchannel2_psbt_payload *payload,
type_to_string(tmpctx, struct channel_id,
&payload->channel->cid));
json_add_bool(stream, "require_confirmed_inputs",
payload->channel->req_confirmed_ins);
payload->channel->req_confirmed_ins[REMOTE]);
json_object_end(stream);
}

Expand Down Expand Up @@ -700,7 +700,8 @@ openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)
channel->cid = payload->channel_id;
channel->opener = REMOTE;
channel->open_attempt = new_channel_open_attempt(channel);
channel->req_confirmed_ins = payload->req_confirmed_ins;
channel->req_confirmed_ins[REMOTE] =
payload->req_confirmed_ins_remote;
msg = towire_dualopend_got_offer_reply(NULL,
payload->accepter_funding,
payload->psbt,
Expand Down Expand Up @@ -1252,6 +1253,8 @@ wallet_commit_channel(struct lightningd *ld,
channel->push = lease_fee_msat;
channel->msat_to_us_min = our_msat;
channel->msat_to_us_max = our_msat;
channel->req_confirmed_ins[LOCAL] =
ld->config.require_confirmed_inputs;

channel->last_tx = tal_steal(channel, remote_commit);
channel->last_sig = *remote_commit_sig;
Expand Down Expand Up @@ -1888,7 +1891,8 @@ static void rbf_got_offer(struct subd *dualopend, const u8 *msg)
payload->peer_id = channel->peer->id;
payload->feerate_our_max = feerate_max(dualopend->ld, NULL);
payload->feerate_our_min = feerate_min(dualopend->ld, NULL);
payload->req_confirmed_ins = channel->req_confirmed_ins;
payload->req_confirmed_ins_remote =
channel->req_confirmed_ins[REMOTE];

payload->psbt = NULL;

Expand Down Expand Up @@ -1944,7 +1948,7 @@ static void accepter_got_offer(struct subd *dualopend,
&payload->shutdown_scriptpubkey,
&payload->requested_lease_amt,
&payload->lease_blockheight_start,
&payload->req_confirmed_ins)) {
&payload->req_confirmed_ins_remote)) {
channel_internal_error(channel, "Bad DUALOPEND_GOT_OFFER: %s",
tal_hex(tmpctx, msg));
return;
Expand Down Expand Up @@ -2726,7 +2730,7 @@ static struct command_result *json_openchannel_update(struct command *cmd,
pv->success = openchannel_update_valid_psbt;
pv->invalid_input = openchannel_invalid_psbt;

if (channel->req_confirmed_ins) {
if (channel->req_confirmed_ins[REMOTE]) {
/* We might fail/terminate in validate's first call,
* which expects us to be at "command still pending" */
ret = command_still_pending(cmd);
Expand Down Expand Up @@ -3054,7 +3058,7 @@ static void handle_psbt_changed(struct subd *dualopend,

if (!fromwire_dualopend_psbt_changed(tmpctx, msg,
&cid,
&channel->req_confirmed_ins,
&channel->req_confirmed_ins[REMOTE],
&funding_serial,
&psbt)) {
channel_internal_error(channel,
Expand Down Expand Up @@ -3083,7 +3087,7 @@ static void handle_psbt_changed(struct subd *dualopend,
json_add_bool(response, "commitments_secured", false);
json_add_u64(response, "funding_serial", funding_serial);
json_add_bool(response, "requires_confirmed_inputs",
channel->req_confirmed_ins);
channel->req_confirmed_ins[REMOTE]);

oa->cmd = NULL;
was_pending(command_success(cmd, response));
Expand Down Expand Up @@ -3746,7 +3750,8 @@ bool peer_restart_dualopend(struct peer *peer,
amount_sat_zero(inflight->lease_amt) ?
NULL : &inflight->lease_amt,
channel->type,
false); /* FIXME: use persisted state? */
channel->req_confirmed_ins[LOCAL],
channel->req_confirmed_ins[REMOTE]);

subd_send_msg(channel->owner, take(msg));
return true;
Expand Down
4 changes: 2 additions & 2 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ wallet_commit_channel(struct lightningd *ld,
uc->log,
take(uc->transient_billboard),
channel_flags,
false,
false, false,
&uc->our_config,
uc->minimum_depth,
1, 1, 0,
Expand Down Expand Up @@ -1398,7 +1398,7 @@ static struct channel *stub_chan(struct command *cmd,
LOCAL,
NULL,
"restored from static channel backup",
0, false,
0, false, false,
our_config,
0,
1, 1, 1,
Expand Down
3 changes: 2 additions & 1 deletion openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -4289,7 +4289,8 @@ int main(int argc, char *argv[])
&state->tx_state->lease_chan_max_ppt,
&requested_lease,
&state->channel_type,
&state->require_confirmed_inputs[LOCAL])) {
&state->require_confirmed_inputs[LOCAL],
&state->require_confirmed_inputs[REMOTE])) {

bool ok;

Expand Down
1 change: 1 addition & 0 deletions openingd/dualopend_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ msgdata,dualopend_reinit,lease_chan_max_ppt,u16,
msgdata,dualopend_reinit,requested_lease,?amount_sat,
msgdata,dualopend_reinit,channel_type,channel_type,
msgdata,dualopend_reinit,we_require_confirmed_inputs,bool,
msgdata,dualopend_reinit,they_require_confirmed_inputs,bool,

# dualopend->master: they offered channel, should we continue?
msgtype,dualopend_got_offer,7005
Expand Down
1 change: 1 addition & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ static struct migration dbmigrations[] = {
/* FIXME: Remove payments local_offer_id column! */
{SQL("ALTER TABLE channel_funding_inflights ADD COLUMN lease_satoshi BIGINT;"), NULL},
{SQL("ALTER TABLE channels ADD require_confirm_inputs_remote INTEGER DEFAULT 0;"), NULL},
{SQL("ALTER TABLE channels ADD require_confirm_inputs_local INTEGER DEFAULT 0;"), NULL},
};

/**
Expand Down
2 changes: 1 addition & 1 deletion wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
NULL,
DUALOPEND_AWAITING_LOCKIN,
LOCAL, NULL, "billboard",
8, false, &our_config,
8, false, false, &our_config,
101, 1, 1, 1,
&outpoint,
funding_sats, AMOUNT_MSAT(0),
Expand Down
10 changes: 7 additions & 3 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm
NULL, /* Set up fresh log */
"Loaded from database",
db_col_int(stmt, "channel_flags"),
db_col_int(stmt, "require_confirm_inputs_local") != 0,
db_col_int(stmt, "require_confirm_inputs_remote") != 0,
&our_config,
db_col_int(stmt, "minimum_depth"),
Expand Down Expand Up @@ -1583,7 +1584,8 @@ static bool wallet_channels_load_active(struct wallet *w)
", state"
", funder"
", channel_flags"
", require_confirm_inputs"
", require_confirm_inputs_local"
", require_confirm_inputs_remote"
", minimum_depth"
", next_index_local"
", next_index_remote"
Expand Down Expand Up @@ -2213,7 +2215,8 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
", delayed_payment_basepoint_local"
", funding_pubkey_local"
", require_confirm_inputs_remote"
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
", require_confirm_inputs_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);
Expand All @@ -2223,7 +2226,8 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
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_bind_int(stmt, 8, chan->req_confirmed_ins);
db_bind_int(stmt, 8, chan->req_confirmed_ins[REMOTE]);
db_bind_int(stmt, 9, chan->req_confirmed_ins[LOCAL]);

db_exec_prepared_v2(take(stmt));

Expand Down

0 comments on commit beec517

Please sign in to comment.