Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual-funding over multifundchannel fixups, etc #4211

Merged
merged 6 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,13 @@ size_t bitcoin_tx_input_sig_weight(void)
return 1 + 71;
}

/* We only do segwit inputs, and we assume witness is sig + key */
size_t bitcoin_tx_simple_input_weight(bool p2sh)
/* Input weight */
size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight)
{
size_t weight;
size_t weight = witness_weight;

/* Input weight: txid + index + sequence */
weight = (32 + 4 + 4) * 4;
weight += (32 + 4 + 4) * 4;

/* We always encode the length of the script, even if empty */
weight += 1 * 4;
Expand All @@ -856,16 +856,26 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh)
if (p2sh)
weight += 23 * 4;

/* Account for witness (1 byte count + sig + key) */
weight += 1 + (bitcoin_tx_input_sig_weight() + 1 + 33);

/* Elements inputs have 6 bytes of blank proofs attached. */
if (chainparams->is_elements)
weight += 6;

return weight;
}

size_t bitcoin_tx_simple_input_witness_weight(void)
{
/* Account for witness (1 byte count + sig + key) */
return 1 + (bitcoin_tx_input_sig_weight() + 1 + 33);
}

/* We only do segwit inputs, and we assume witness is sig + key */
size_t bitcoin_tx_simple_input_weight(bool p2sh)
{
return bitcoin_tx_input_weight(p2sh,
bitcoin_tx_simple_input_witness_weight());
}

struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw)
{
size_t outweight;
Expand Down
6 changes: 6 additions & 0 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ size_t bitcoin_tx_output_weight(size_t outscript_len);
/* Weight to push sig on stack. */
size_t bitcoin_tx_input_sig_weight(void);

/* Segwit input, but with parameter for witness weight (size) */
size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight);

/* The witness weight for a simple (sig + key) input */
size_t bitcoin_tx_simple_input_witness_weight(void);

/* We only do segwit inputs, and we assume witness is sig + key */
size_t bitcoin_tx_simple_input_weight(bool p2sh);

Expand Down
11 changes: 9 additions & 2 deletions common/utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
return utxo;
}

size_t utxo_spend_weight(const struct utxo *utxo)
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight)
{
return bitcoin_tx_simple_input_weight(utxo->is_p2sh);
size_t wit_weight = bitcoin_tx_simple_input_witness_weight();
/* If the min is less than what we'd use for a 'normal' tx,
* we return the value with the greater added/calculated */
if (wit_weight < min_witness_weight)
return bitcoin_tx_input_weight(utxo->is_p2sh,
min_witness_weight);

return bitcoin_tx_input_weight(utxo->is_p2sh, wit_weight);
}
2 changes: 1 addition & 1 deletion common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo);
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);

/* Estimate of (signed) UTXO weight in transaction */
size_t utxo_spend_weight(const struct utxo *utxo);
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight);
#endif /* LIGHTNING_COMMON_UTXO_H */
9 changes: 7 additions & 2 deletions doc/lightning-fundpsbt.7

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

6 changes: 5 additions & 1 deletion doc/lightning-fundpsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-fundpsbt -- Command to populate PSBT inputs from the wallet
SYNOPSIS
--------

**fundpsbt** *satoshi* *feerate* *startweight* \[*minconf*\] \[*reserve*\] \[*locktime*\]
**fundpsbt** *satoshi* *feerate* *startweight* \[*minconf*\] \[*reserve*\] \[*locktime*\] \[*min_witness_weight*\]

DESCRIPTION
-----------
Expand Down Expand Up @@ -39,6 +39,10 @@ called (successfully, with *exclusive* true) on the returned PSBT.
*locktime* is an optional locktime: if not set, it is set to a recent
block height.

*min_witness_weight* is an optional minimum weight to use for a UTXO's
witness. If the actual witness weight is greater than the provided minimum,
the actual witness weight will be used.

EXAMPLE USAGE
-------------

Expand Down
9 changes: 7 additions & 2 deletions doc/lightning-utxopsbt.7

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

6 changes: 5 additions & 1 deletion doc/lightning-utxopsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-utxopsbt -- Command to populate PSBT inputs from given UTXOs
SYNOPSIS
--------

**utxopsbt** *satoshi* *feerate* *startweight* *utxos* \[*reserve*\] \[*reservedok*\] \[*locktime*\]
**utxopsbt** *satoshi* *feerate* *startweight* *utxos* \[*reserve*\] \[*reservedok*\] \[*locktime*\] \[*min_witness_weight*\]

DESCRIPTION
-----------
Expand All @@ -29,6 +29,10 @@ if any of the *utxos* are already reserved.
*locktime* is an optional locktime: if not set, it is set to a recent
block height.

*min_witness_weight* is an optional minimum weight to use for a UTXO's
witness. If the actual witness weight is greater than the provided minimum,
the actual witness weight will be used.

RETURN VALUE
------------

Expand Down
58 changes: 29 additions & 29 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static const u8 *hook_extract_shutdown_script(struct subd* dualopend,
if (!json_tok_streq(buffer, t, "continue")) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dual_open_fail(NULL, errmsg)));
take(towire_dualopend_fail(NULL, errmsg)));
return NULL;
}

Expand Down Expand Up @@ -234,7 +234,7 @@ hook_extract_psbt(const tal_t *ctx, struct subd *dualopend, const char *buffer,
if (dualopend) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dual_open_fail(NULL, errmsg)));
take(towire_dualopend_fail(NULL, errmsg)));
}
return false;
}
Expand Down Expand Up @@ -285,7 +285,7 @@ hook_extract_amount(struct subd *dualopend,
if (!json_tok_streq(buffer, t, "continue")) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dual_open_fail(NULL, errmsg)));
take(towire_dualopend_fail(NULL, errmsg)));
return false;
}

Expand Down Expand Up @@ -440,7 +440,7 @@ openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)

/* If there's no plugin, the psbt will be NULL. We should pass an empty
* PSBT over, in this case */
msg = towire_dual_open_got_offer_reply(NULL, payload->accepter_funding,
msg = towire_dualopend_got_offer_reply(NULL, payload->accepter_funding,
payload->funding_feerate_per_kw,
payload->psbt,
payload->our_shutdown_scriptpubkey);
Expand Down Expand Up @@ -498,7 +498,7 @@ openchannel2_changed_hook_cb(struct openchannel2_psbt_payload *payload STEALS)
payload);

subd_send_msg(dualopend,
take(towire_dual_open_psbt_updated(NULL,
take(towire_dualopend_psbt_updated(NULL,
payload->psbt)));
}

Expand Down Expand Up @@ -685,7 +685,7 @@ static void opener_psbt_changed(struct subd *dualopend,
struct json_stream *response;
struct command *cmd = uc->fc->cmd;

if (!fromwire_dual_open_psbt_changed(cmd, msg,
if (!fromwire_dualopend_psbt_changed(cmd, msg,
&cid, &funding_serial,
&psbt)) {
log_broken(dualopend->log,
Expand Down Expand Up @@ -736,7 +736,7 @@ static void accepter_commit_received(struct subd *dualopend,
/* This is a new channel_info.their_config so set its ID to 0 */
channel_info.their_config.id = 0;

if (!fromwire_dual_open_commit_rcvd(tmpctx, msg,
if (!fromwire_dualopend_commit_rcvd(tmpctx, msg,
&channel_info.their_config,
&remote_commit,
&pbase,
Expand All @@ -760,9 +760,9 @@ static void accepter_commit_received(struct subd *dualopend,
&uc->our_config.channel_reserve,
&local_upfront_shutdown_script,
&remote_upfront_shutdown_script)) {
log_broken(uc->log, "bad WIRE_DUAL_OPEN_COMMIT_RCVD %s",
log_broken(uc->log, "bad WIRE_DUALOPEND_COMMIT_RCVD %s",
tal_hex(msg, msg));
uncommitted_channel_disconnect(uc, LOG_BROKEN, "bad WIRE_DUAL_OPEN_COMMIT_RCVD");
uncommitted_channel_disconnect(uc, LOG_BROKEN, "bad WIRE_DUALOPEND_COMMIT_RCVD");
close(fds[0]);
close(fds[1]);
close(fds[3]);
Expand Down Expand Up @@ -853,7 +853,7 @@ static void opener_commit_received(struct subd *dualopend,
/* This is a new channel_info.their_config so set its ID to 0 */
channel_info.their_config.id = 0;

if (!fromwire_dual_open_commit_rcvd(tmpctx, msg,
if (!fromwire_dualopend_commit_rcvd(tmpctx, msg,
&channel_info.their_config,
&remote_commit,
&pbase,
Expand All @@ -877,9 +877,9 @@ static void opener_commit_received(struct subd *dualopend,
&uc->our_config.channel_reserve,
&local_upfront_shutdown_script,
&remote_upfront_shutdown_script)) {
log_broken(uc->log, "bad WIRE_DUAL_OPEN_COMMIT_RCVD %s",
log_broken(uc->log, "bad WIRE_DUALOPEND_COMMIT_RCVD %s",
tal_hex(msg, msg));
err_reason = "bad WIRE_DUAL_OPEN_COMMIT_RCVD";
err_reason = "bad WIRE_DUALOPEND_COMMIT_RCVD";
uncommitted_channel_disconnect(uc, LOG_BROKEN, err_reason);
close(fds[0]);
close(fds[1]);
Expand Down Expand Up @@ -965,7 +965,7 @@ static void accepter_psbt_changed(struct subd *dualopend,
payload->psbt = NULL;
payload->rcvd = tal(payload, struct commit_rcvd);

if (!fromwire_dual_open_psbt_changed(payload, msg,
if (!fromwire_dualopend_psbt_changed(payload, msg,
&payload->rcvd->cid,
&unused,
&payload->psbt)) {
Expand All @@ -987,7 +987,7 @@ static void accepter_got_offer(struct subd *dualopend,

if (peer_active_channel(uc->peer)) {
subd_send_msg(dualopend,
take(towire_dual_open_fail(NULL, "Already have active channel")));
take(towire_dualopend_fail(NULL, "Already have active channel")));
return;
}

Expand All @@ -998,7 +998,7 @@ static void accepter_got_offer(struct subd *dualopend,
payload->our_shutdown_scriptpubkey = NULL;
payload->peer_id = uc->peer->id;

if (!fromwire_dual_open_got_offer(payload, msg,
if (!fromwire_dualopend_got_offer(payload, msg,
&payload->their_funding,
&payload->dust_limit_satoshis,
&payload->max_htlc_value_in_flight_msat,
Expand Down Expand Up @@ -1081,7 +1081,7 @@ static struct command_result *json_open_channel_update(struct command *cmd,

uc->fc->cmd = cmd;

msg = towire_dual_open_psbt_updated(NULL, psbt);
msg = towire_dualopend_psbt_updated(NULL, psbt);
subd_send_msg(uc->open_daemon, take(msg));
return command_still_pending(cmd);
}
Expand Down Expand Up @@ -1224,7 +1224,7 @@ static struct command_result *json_open_channel_init(struct command *cmd,
fc->our_upfront_shutdown_script
= tal_steal(fc, fc->our_upfront_shutdown_script);

msg = towire_dual_open_opener_init(NULL,
msg = towire_dualopend_opener_init(NULL,
psbt, *amount,
fc->our_upfront_shutdown_script,
*feerate_per_kw,
Expand All @@ -1242,10 +1242,10 @@ static unsigned int dual_opend_msg(struct subd *dualopend,
struct uncommitted_channel *uc = dualopend->channel;

switch (t) {
case WIRE_DUAL_OPEN_GOT_OFFER:
case WIRE_DUALOPEND_GOT_OFFER:
accepter_got_offer(dualopend, uc, msg);
return 0;
case WIRE_DUAL_OPEN_PSBT_CHANGED:
case WIRE_DUALOPEND_PSBT_CHANGED:
if (uc->fc) {
if (!uc->fc->cmd) {
log_unusual(dualopend->log,
Expand All @@ -1258,7 +1258,7 @@ static unsigned int dual_opend_msg(struct subd *dualopend,
} else
accepter_psbt_changed(dualopend, msg);
return 0;
case WIRE_DUAL_OPEN_COMMIT_RCVD:
case WIRE_DUALOPEND_COMMIT_RCVD:
if (tal_count(fds) != 3)
return 3;
if (uc->fc) {
Expand All @@ -1275,16 +1275,16 @@ static unsigned int dual_opend_msg(struct subd *dualopend,
accepter_commit_received(dualopend,
uc, fds, msg);
return 0;
case WIRE_DUAL_OPEN_FAILED:
case WIRE_DUAL_OPEN_DEV_MEMLEAK_REPLY:
case WIRE_DUALOPEND_FAILED:
case WIRE_DUALOPEND_DEV_MEMLEAK_REPLY:

/* Messages we send */
case WIRE_DUAL_OPEN_INIT:
case WIRE_DUAL_OPEN_OPENER_INIT:
case WIRE_DUAL_OPEN_GOT_OFFER_REPLY:
case WIRE_DUAL_OPEN_FAIL:
case WIRE_DUAL_OPEN_PSBT_UPDATED:
case WIRE_DUAL_OPEN_DEV_MEMLEAK:
case WIRE_DUALOPEND_INIT:
case WIRE_DUALOPEND_OPENER_INIT:
case WIRE_DUALOPEND_GOT_OFFER_REPLY:
case WIRE_DUALOPEND_FAIL:
case WIRE_DUALOPEND_PSBT_UPDATED:
case WIRE_DUALOPEND_DEV_MEMLEAK:
break;
}

Expand Down Expand Up @@ -1380,7 +1380,7 @@ void peer_start_dualopend(struct peer *peer,
*/
uc->minimum_depth = peer->ld->config.anchor_confirms;

msg = towire_dual_open_init(NULL,
msg = towire_dualopend_init(NULL,
chainparams,
peer->ld->our_features,
peer->their_features,
Expand Down
4 changes: 2 additions & 2 deletions lightningd/opening_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void opening_memleak_req_done(struct subd *open_daemon,

tal_del_destructor2(open_daemon, opening_died_forget_memleak, cmd);
if (!fromwire_openingd_dev_memleak_reply(msg, &found_leak) &&
!fromwire_dual_open_dev_memleak_reply(msg,
!fromwire_dualopend_dev_memleak_reply(msg,
&found_leak)) {
was_pending(command_fail(cmd, LIGHTNINGD,
"Bad opening_dev_memleak"));
Expand Down Expand Up @@ -210,7 +210,7 @@ static void opening_memleak_req_next(struct command *cmd, struct peer *prev)
continue;

if (streq(open_daemon->name, "dualopend"))
msg = towire_dual_open_dev_memleak(NULL);
msg = towire_dualopend_dev_memleak(NULL);
else
msg = towire_openingd_dev_memleak(NULL);

Expand Down
Loading