Skip to content

Commit

Permalink
df: use estimated witness weight, not fixed 110 value
Browse files Browse the repository at this point in the history
Persist the estimated-witness-weight thru via the PSBT object.
Use this to calculate the estimated fee burden of a peer.

For now we just re-use the 110 "default" witness weight.

FIXME: use BIP322 and send proofs/use that as estimate
  • Loading branch information
niftynei committed Feb 3, 2023
1 parent 3eeec50 commit f0f32b4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
33 changes: 32 additions & 1 deletion common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ bool psbt_get_serial_id(const struct wally_map *map, u64 *serial_id)
return true;
}

static bool psbt_map_get_u32(const struct wally_map *map, u32 type, u32 *val)
{
size_t value_len;
beint32_t bev;
void *result = psbt_get_lightning(map, type, &value_len);
if (!result)
return false;

if (value_len != sizeof(bev))
return false;

memcpy(&bev, result, value_len);
*val = be32_to_cpu(bev);
return true;
}

static int compare_serials(const struct wally_map *map_a,
const struct wally_map *map_b)
{
Expand Down Expand Up @@ -72,7 +88,6 @@ static const u8 *linearize_input(const tal_t *ctx,
psbt->inputs[0] = *in;
psbt->num_inputs++;


/* Sort the inputs, so serializing them is ok */
wally_map_sort(&psbt->inputs[0].unknowns, 0);

Expand Down Expand Up @@ -320,6 +335,22 @@ struct psbt_changeset *psbt_get_changeset(const tal_t *ctx,
return set;
}

void psbt_set_est_witness_weight(const tal_t *ctx,
struct wally_psbt_input *input,
u32 est_witness_weight)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_EST_WITNESS_WEIGHT, NULL);
beint32_t bev = cpu_to_be32(est_witness_weight);

psbt_input_set_unknown(ctx, input, key, &bev, sizeof(bev));
}

bool psbt_get_est_witness_weight(const struct wally_psbt_input *input,
u32 *est_witness_weight)
{
return psbt_map_get_u32(&input->unknowns, PSBT_TYPE_EST_WITNESS_WEIGHT,
est_witness_weight);
}

void psbt_input_set_serial_id(const tal_t *ctx,
struct wally_psbt_input *input,
Expand Down
24 changes: 22 additions & 2 deletions common/psbt_open.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct psbt_changeset {
#define PSBT_TYPE_SERIAL_ID 0x01
#define PSBT_TYPE_INPUT_MARKER 0x02
#define PSBT_TYPE_OUTPUT_EXTERNAL 0x04
#define PSBT_TYPE_EST_WITNESS_WEIGHT 0x08

/* psbt_get_serial_id - Returns the serial_id from an unknowns map
*
Expand All @@ -46,8 +47,27 @@ struct psbt_changeset {
*
* Returns false if serial_id is not present
*/
WARN_UNUSED_RESULT bool psbt_get_serial_id(const struct wally_map *map,
u64 *serial_id);
bool psbt_get_serial_id(const struct wally_map *map, u64 *serial_id);

/* psbt_set_est_witness_weight - Set the estimated witness weight for an input
*
* @ctx - the allocation context
* @input - the psbt input to annotate with it's estimated witness weight
* @est_witness_weight - the estimated witness weight
* */
void psbt_set_est_witness_weight(const tal_t *ctx,
struct wally_psbt_input *input,
u32 est_witness_weight);

/* psbt_get_est_witness_weight - Get estimated witness weight from input
*
* @input - psbt input to retreive the estimated wintess weight annotation of
* @est_witness_weight - witness weight, if present
*
* Returns false if est_witness_weight not present
*/
WARN_UNUSED_RESULT bool psbt_get_est_witness_weight(const struct wally_psbt_input *input,
u32 *est_witness_weight);

/* psbt_sort_by_serial_id - Sort PSBT by serial_ids
*
Expand Down
10 changes: 5 additions & 5 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ openchannel2_hook_deserialize(struct openchannel2_payload *payload,
!lease_rates_set_chan_fee_base_msat(payload->rates, fee_max_base))
fatal("Plugin sent overflowing `channel_fee_max_base_msat`");

/* Add a serial_id to everything that doesn't have one yet */
/* Add a serial_id + est_weight to everything that doesn't have one yet */
if (payload->psbt)
psbt_add_serials(payload->psbt, TX_ACCEPTER);

Expand Down Expand Up @@ -867,7 +867,7 @@ openchannel2_changed_deserialize(struct openchannel2_psbt_payload *payload,
false, &psbt))
return false;

/* Add serials to PSBT, before checking for required fields */
/* Add serials + est_weights to PSBT, before checking for required fields */
psbt_add_serials(psbt, TX_ACCEPTER);

/* We require the PSBT to meet certain criteria such as
Expand Down Expand Up @@ -2446,7 +2446,7 @@ json_openchannel_bump(struct command *cmd,
oa->our_upfront_shutdown_script
= channel->shutdown_scriptpubkey[LOCAL];

/* Add serials to any input that's missing them */
/* Add serials+est-weight to any input that's missing them */
psbt_add_serials(psbt, TX_INITIATOR);

/* We require the PSBT to meet certain criteria such as
Expand Down Expand Up @@ -2703,7 +2703,7 @@ static struct command_result *json_openchannel_update(struct command *cmd,
"Another openchannel command"
" is in progress");

/* Add serials to PSBT */
/* Add serials+est-weight to PSBT */
psbt_add_serials(psbt, TX_INITIATOR);

/* We require the PSBT to meet certain criteria such as
Expand Down Expand Up @@ -2881,7 +2881,7 @@ static struct command_result *json_openchannel_init(struct command *cmd,
type_to_string(tmpctx, struct amount_sat,
&chainparams->max_funding));

/* Add serials to any input that's missing them */
/* Add serials+weights to any input that's missing them */
psbt_add_serials(psbt, TX_INITIATOR);

/* We require the PSBT to meet certain criteria such as
Expand Down
19 changes: 12 additions & 7 deletions openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,12 @@ static u8 *psbt_changeset_get_next(const tal_t *ctx,
struct psbt_changeset *set)
{
u64 serial_id;
/* FIXME: actually estimate this? */
u32 est_witness_weight = 110;
u8 *msg;

if (tal_count(set->added_ins) != 0) {
const struct input_set *in = &set->added_ins[0];
u32 est_witness_weight = 0;

if (!psbt_get_serial_id(&in->input.unknowns, &serial_id))
abort();
Expand Down Expand Up @@ -601,18 +602,22 @@ static size_t psbt_input_weight(struct wally_psbt *psbt,
size_t in)
{
size_t weight;
u32 est_witness_weight;

/* txid + txout + sequence */
weight = (32 + 4 + 4) * 4;
/* This should be just one 00 byte,
* pre-Segwit inputs aren't allowed */
weight +=
(psbt->inputs[in].redeem_script_len +
(varint_t) varint_size(psbt->inputs[in].redeem_script_len)) * 4;

/* BOLT-f53ca2301232db780843e894f55d95d512f297f9 #3:
*
* The minimum witness weight for an input is 110.
*/
weight += 110;
/* Add input's estimated witness weight */
if (!psbt_get_est_witness_weight(&psbt->inputs[in], &est_witness_weight))
/* Default to 110 */
est_witness_weight = 110;

weight += est_witness_weight;
return weight;
}

Expand Down Expand Up @@ -1498,7 +1503,6 @@ static bool run_tx_interactive(struct state *state,
cast_const2(u8 **,
&tx_bytes),
&outpoint.n, &sequence,
/* FIXME: use this */
&est_witness_weight))
open_err_fatal(state,
"Parsing tx_add_input %s",
Expand Down Expand Up @@ -1630,6 +1634,7 @@ static bool run_tx_interactive(struct state *state,
&asset);
}
psbt_input_set_serial_id(psbt, in, serial_id);
psbt_set_est_witness_weight(psbt, in, est_witness_weight);

break;
}
Expand Down

0 comments on commit f0f32b4

Please sign in to comment.