Skip to content

Commit

Permalink
Merge pull request #8751 from yyforyongyu/fix-sweeper-18
Browse files Browse the repository at this point in the history
contractcourt+sweep: fix fee function and deadline issue
  • Loading branch information
Roasbeef committed May 21, 2024
2 parents 2a8ca87 + 3475377 commit e1c5fe2
Show file tree
Hide file tree
Showing 16 changed files with 963 additions and 377 deletions.
16 changes: 8 additions & 8 deletions chainntnfs/txnotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,8 @@ func (n *TxNotifier) CancelConf(confRequest ConfRequest, confID uint64) {
return
}

Log.Infof("Canceling confirmation notification: conf_id=%d, %v", confID,
confRequest)
Log.Debugf("Canceling confirmation notification: conf_id=%d, %v",
confID, confRequest)

// We'll close all the notification channels to let the client know
// their cancel request has been fulfilled.
Expand Down Expand Up @@ -925,7 +925,7 @@ func (n *TxNotifier) dispatchConfDetails(
// we'll dispatch a confirmation notification to the caller.
confHeight := details.BlockHeight + ntfn.NumConfirmations - 1
if confHeight <= n.currentHeight {
Log.Infof("Dispatching %v confirmation notification for %v",
Log.Debugf("Dispatching %v confirmation notification for %v",
ntfn.NumConfirmations, ntfn.ConfRequest)

// We'll send a 0 value to the Updates channel,
Expand Down Expand Up @@ -1058,7 +1058,7 @@ func (n *TxNotifier) RegisterSpend(outpoint *wire.OutPoint, pkScript []byte,
n.Lock()
defer n.Unlock()

Log.Infof("New spend subscription: spend_id=%d, %v, height_hint=%d",
Log.Debugf("New spend subscription: spend_id=%d, %v, height_hint=%d",
ntfn.SpendID, ntfn.SpendRequest, startHeight)

// Keep track of the notification request so that we can properly
Expand Down Expand Up @@ -1136,7 +1136,7 @@ func (n *TxNotifier) RegisterSpend(outpoint *wire.OutPoint, pkScript []byte,
// notifications don't also attempt a historical dispatch.
spendSet.rescanStatus = rescanPending

Log.Infof("Dispatching historical spend rescan for %v, start=%d, "+
Log.Debugf("Dispatching historical spend rescan for %v, start=%d, "+
"end=%d", ntfn.SpendRequest, startHeight, n.currentHeight)

return &SpendRegistration{
Expand Down Expand Up @@ -1171,7 +1171,7 @@ func (n *TxNotifier) CancelSpend(spendRequest SpendRequest, spendID uint64) {
return
}

Log.Infof("Canceling spend notification: spend_id=%d, %v", spendID,
Log.Debugf("Canceling spend notification: spend_id=%d, %v", spendID,
spendRequest)

// We'll close all the notification channels to let the client know
Expand Down Expand Up @@ -1364,7 +1364,7 @@ func (n *TxNotifier) dispatchSpendDetails(ntfn *SpendNtfn, details *SpendDetail)
return nil
}

Log.Infof("Dispatching confirmed spend notification for %v at "+
Log.Debugf("Dispatching confirmed spend notification for %v at "+
"current height=%d: %v", ntfn.SpendRequest, n.currentHeight,
details)

Expand Down Expand Up @@ -1743,7 +1743,7 @@ func (n *TxNotifier) NotifyHeight(height uint32) error {
for ntfn := range n.ntfnsByConfirmHeight[height] {
confSet := n.confNotifications[ntfn.ConfRequest]

Log.Infof("Dispatching %v confirmation notification for %v",
Log.Debugf("Dispatching %v confirmation notification for %v",
ntfn.NumConfirmations, ntfn.ConfRequest)

// The default notification we assigned above includes the
Expand Down
18 changes: 10 additions & 8 deletions cmd/lncli/walletrpc_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,11 @@ func bumpFee(ctx *cli.Context) error {
}

resp, err := client.BumpFee(ctxc, &walletrpc.BumpFeeRequest{
Outpoint: protoOutPoint,
TargetConf: uint32(ctx.Uint64("conf_target")),
Immediate: immediate,
Budget: ctx.Uint64("budget"),
Outpoint: protoOutPoint,
TargetConf: uint32(ctx.Uint64("conf_target")),
Immediate: immediate,
Budget: ctx.Uint64("budget"),
SatPerVbyte: ctx.Uint64("sat_per_vbyte"),
})
if err != nil {
return err
Expand Down Expand Up @@ -487,10 +488,11 @@ func bumpForceCloseFee(ctx *cli.Context) error {

resp, err := walletClient.BumpFee(
ctxc, &walletrpc.BumpFeeRequest{
Outpoint: sweep.Outpoint,
TargetConf: uint32(ctx.Uint64("conf_target")),
Budget: ctx.Uint64("budget"),
Immediate: ctx.Bool("immediate"),
Outpoint: sweep.Outpoint,
TargetConf: uint32(ctx.Uint64("conf_target")),
Budget: ctx.Uint64("budget"),
Immediate: ctx.Bool("immediate"),
SatPerVbyte: ctx.Uint64("sat_per_vbyte"),
})
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions contractcourt/chain_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,10 @@ func (c *ChainArbitrator) FindOutgoingHTLCDeadline(scid lnwire.ShortChannelID,
continue
}

// Make sure the channel arbitrator has the latest view of its
// active HTLCs.
channelArb.updateActiveHTLCs()

// Iterate all the known HTLCs to find the targeted incoming
// HTLC.
for _, htlcs := range channelArb.activeHTLCs {
Expand Down
42 changes: 34 additions & 8 deletions contractcourt/channel_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,9 +1436,13 @@ func (c *ChannelArbitrator) sweepAnchors(anchors *lnwallet.AnchorResolutions,

// findCommitmentDeadlineAndValue finds the deadline (relative block height)
// for a commitment transaction by extracting the minimum CLTV from its HTLCs.
// From our PoV, the deadline is defined to be the smaller of,
// - the least CLTV from outgoing HTLCs, or,
// - the least CLTV from incoming HTLCs if the preimage is available.
// From our PoV, the deadline delta is defined to be the smaller of,
// - half of the least CLTV from outgoing HTLCs' corresponding incoming
// HTLCs, or,
// - half of the least CLTV from incoming HTLCs if the preimage is available.
//
// We use half of the CTLV value to ensure that we have enough time to sweep
// the second-level HTLCs.
//
// It also finds the total value that are time-sensitive, which is the sum of
// all the outgoing HTLCs plus incoming HTLCs whose preimages are known. It
Expand Down Expand Up @@ -1468,10 +1472,24 @@ func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
}

value := htlc.Amt.ToSatoshis()
totalValue += value

if htlc.RefundTimeout < deadlineMinHeight {
deadlineMinHeight = htlc.RefundTimeout
// Find the expiry height for this outgoing HTLC's incoming
// HTLC.
deadlineOpt := c.cfg.FindOutgoingHTLCDeadline(htlc)

// The deadline is default to the current deadlineMinHeight,
// and it's overwritten when it's not none.
deadline := deadlineMinHeight
deadlineOpt.WhenSome(func(d int32) {
deadline = uint32(d)

// We only consider the value is under protection when
// it's time-sensitive.
totalValue += value
})

if deadline < deadlineMinHeight {
deadlineMinHeight = deadline

log.Tracef("ChannelArbitrator(%v): outgoing HTLC has "+
"deadline=%v, value=%v", c.cfg.ChanPoint,
Expand Down Expand Up @@ -1521,7 +1539,7 @@ func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
// * none of the HTLCs are preimageAvailable.
// - when our deadlineMinHeight is no greater than the heightHint,
// which means we are behind our schedule.
deadline := deadlineMinHeight - heightHint
var deadline uint32
switch {
// When we couldn't find a deadline height from our HTLCs, we will fall
// back to the default value as there's no time pressure here.
Expand All @@ -1535,6 +1553,11 @@ func (c *ChannelArbitrator) findCommitmentDeadlineAndValue(heightHint uint32,
"deadlineMinHeight=%d, heightHint=%d",
c.cfg.ChanPoint, deadlineMinHeight, heightHint)
deadline = 1

// Use half of the deadline delta, and leave the other half to be used
// to sweep the HTLCs.
default:
deadline = (deadlineMinHeight - heightHint) / 2
}

// Calculate the value left after subtracting the budget used for
Expand Down Expand Up @@ -2800,7 +2823,7 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
// state, so we'll get the most up to date signals to we can
// properly do our job.
case signalUpdate := <-c.signalUpdates:
log.Tracef("ChannelArbitrator(%v) got new signal "+
log.Tracef("ChannelArbitrator(%v): got new signal "+
"update!", c.cfg.ChanPoint)

// We'll update the ShortChannelID.
Expand Down Expand Up @@ -3066,6 +3089,9 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
// We've just received a request to forcibly close out the
// channel. We'll
case closeReq := <-c.forceCloseReqs:
log.Infof("ChannelArbitrator(%v): received force "+
"close request", c.cfg.ChanPoint)

if c.state != StateDefault {
select {
case closeReq.closeTx <- nil:
Expand Down

0 comments on commit e1c5fe2

Please sign in to comment.