contractcourt: specify deadline and budget for anchor output

This commit is contained in:
yyforyongyu committed 2024-04-19 21:33:34 +08:00
1 parent f4035ade05
commit 6f0c2b5bab
4 files changed
+91 -51

No files matched your search

+46 -31
View File
@@ -37,12 +37,6 @@ var (
)
const (
// anchorSweepConfTarget is the conf target used when sweeping
// commitment anchors. This value is only used when the commitment
// transaction has no valid HTLCs for determining a confirmation
// deadline.
anchorSweepConfTarget = 144
// arbitratorBlockBufferSize is the size of the buffer we give to each
// channel arbitrator.
arbitratorBlockBufferSize = 20
@@ -1321,36 +1315,23 @@ func (c *ChannelArbitrator) sweepAnchors(anchors *lnwallet.AnchorResolutions,
htlcs htlcSet, anchorPath string) error {
// Find the deadline for this specific anchor.
deadlineOpt, _, err := c.findCommitmentDeadlineAndValue(
deadline, value, err := c.findCommitmentDeadlineAndValue(
heightHint, htlcs,
)
if err != nil {
return err
}
deadline := uint32(1)
deadlineOpt.WhenSome(func(d int32) {
deadline = uint32(d)
})
// If we cannot find a deadline, it means there's no HTLCs at
// stake, which means we can relax our anchor sweeping as we
// don't have any time sensitive outputs to sweep.
if deadline.IsNone() {
log.Infof("ChannelArbitrator(%v): no HTLCs at stake, "+
"skipped anchor CPFP", c.cfg.ChanPoint)
// Create a force flag that's used to indicate whether we
// should force sweeping this anchor.
var force bool
// Check the deadline against the default value. If it's less
// than the default value of 144, it means there is a deadline
// and we will perform a CPFP for this commitment tx.
if deadline < anchorSweepConfTarget {
// Signal that this is a force sweep, so that the
// anchor will be swept even if it isn't economical
// purely based on the anchor value.
force = true
return nil
}
log.Debugf("ChannelArbitrator(%v): pre-confirmation sweep of "+
"anchor of %s commit tx %v, force=%v", c.cfg.ChanPoint,
anchorPath, anchor.CommitAnchor, force)
witnessType := input.CommitmentAnchor
// For taproot channels, we need to use the proper witness
@@ -1374,6 +1355,28 @@ func (c *ChannelArbitrator) sweepAnchors(anchors *lnwallet.AnchorResolutions,
},
)
// If we have a deadline, we'll use it to calculate the
// deadline height, otherwise default to none.
deadlineDesc := "None"
deadlineHeight := fn.MapOption(func(d int32) int32 {
deadlineDesc = fmt.Sprintf("%d", d)
return d + int32(heightHint)
})(deadline)
// Calculate the budget based on the value under protection,
// which is the sum of all HTLCs on this commitment subtracted
// by their budgets.
budget := calculateBudget(
value, c.cfg.Budget.AnchorCPFPRatio,
c.cfg.Budget.AnchorCPFP,
)
log.Infof("ChannelArbitrator(%v): offering anchor from %s "+
"commitment %v to sweeper with deadline=%v, budget=%v",
c.cfg.ChanPoint, anchorPath, anchor.CommitAnchor,
deadlineDesc, budget)
// Sweep anchor output with a confirmation target fee
// preference. Because this is a cpfp-operation, the anchor
// will only be attempted to sweep when the current fee
@@ -1382,11 +1385,9 @@ func (c *ChannelArbitrator) sweepAnchors(anchors *lnwallet.AnchorResolutions,
_, err = c.cfg.Sweeper.SweepInput(
&anchorInput,
sweep.Params{
Fee: sweep.FeeEstimateInfo{
ConfTarget: deadline,
},
Force: force,
ExclusiveGroup: &exclusiveGroup,
Budget: budget,
DeadlineHeight: deadlineHeight,
},
)
if err != nil {
@@ -2138,6 +2139,20 @@ func (c *ChannelArbitrator) checkRemoteDiffActions(height uint32,
continue
}
preimageAvailable, err := c.isPreimageAvailable(htlc.RHash)
if err != nil {
log.Errorf("ChannelArbitrator(%v): failed to query "+
"preimage for dangling htlc=%x from remote "+
"commitments diff", c.cfg.ChanPoint,
htlc.RHash[:])
continue
}
if preimageAvailable {
continue
}
actionMap[HtlcFailNowAction] = append(
actionMap[HtlcFailNowAction], htlc,
)