Skip to content

Commit

Permalink
onchaind: Adjust the sweep target deadline for fee estimation
Browse files Browse the repository at this point in the history
We used to always target `now() + 300`, which ends up never really
confirming, as the fee estimate bumps into the min-relay-fee
limit. With this commit we set an absolute target of 2 weeks, and a
linear fee rampup, until we are at T-2h, at which point we just stick
with the estimate, and try with this increased feerate to try and get
the sweep confirmed.

This ought to make RBF transactions much more efficient for closing
channels.

Changelog-Changed onchaind: When sweeping funds from channel closes we now use an absolute deadline of 2 weeks, rather than a relative deadline which causes sweeps not confirm for prolonged times.
  • Loading branch information
cdecker committed Aug 7, 2024
1 parent e6d5334 commit 5259236
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,44 @@ static u32 infinite_block_deadline(const struct chain_topology *topo)
return get_block_height(topo) + 300;
}

/**
* slow_sweep_dealine -- A smart(-ish) way of estimating sweep fees.
*
* We use the `close_blockheight` as the basis for the target. We then
* target to have the funds back at most 2 weeks after the close,
* linearly increasing the confirmation target until we get
* close. However, we also don't want to panic and set a target that
* is too close, in order not to waste too many funds on the sweep
* fees.
*/
static u32 slow_sweep_deadline(const struct chain_topology *topo, const struct channel *channel)
{
u32 deadline, height = get_block_height(topo);
u32 closeheight =
channel->close_blockheight ? *channel->close_blockheight : 0;
if (closeheight == 0)
return get_block_height(topo) + 300;

/* A two week deadline seems like a not too bad default. We
* should likely tweak this to hit the desired cost vs. idle
* time tradeoff. */
deadline = closeheight + 2016;

/* If we missed the deadline, don't panic, let's continue with
* a somewhat reasonable target, and hope for the best. No
* need to ramp up to confirmation target 1, we're supposed to
* be slow. */
if (deadline < height + 12)
return height + 12;

/* At the same time don't be too lenient, we want this to
* confirm and not just idle around forever. */
if (deadline - height > 300)
return height + 300;

return deadline;
}

static struct onchain_signing_info *new_signing_info(const tal_t *ctx,
struct channel *channel,
enum onchaind_wire msgtype)
Expand Down Expand Up @@ -1104,7 +1142,8 @@ static void handle_onchaind_spend_to_us(struct channel *channel,
}

/* No real deadline on this, it's just returning to our wallet. */
info->deadline_block = infinite_block_deadline(channel->peer->ld->topology);
info->deadline_block =
slow_sweep_deadline(channel->peer->ld->topology, channel);
create_onchain_tx(channel, &out, out_sats,
channel->channel_info.their_config.to_self_delay, 0,
sign_tx_to_us, info,
Expand Down

0 comments on commit 5259236

Please sign in to comment.