• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12583319996

02 Jan 2025 01:38PM UTC coverage: 57.522% (-1.1%) from 58.598%
12583319996

Pull #9361

github

starius
fn/ContextGuard: use context.AfterFunc to wait

Simplifies context cancellation handling by using context.AfterFunc instead of a
goroutine to wait for context cancellation. This approach avoids the overhead of
a goroutine during the waiting period.

For ctxQuitUnsafe, since g.quit is closed only in the Quit method (which also
cancels all associated contexts), waiting on context cancellation ensures the
same behavior without unnecessary dependency on g.quit.

Added a test to ensure that the Create method does not launch any goroutines.
Pull Request #9361: fn: optimize context guard

102587 of 178344 relevant lines covered (57.52%)

24734.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.86
/contractcourt/htlc_lease_resolver.go
1
package contractcourt
2

3
import (
4
        "math"
5

6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/lightningnetwork/lnd/chainntnfs"
8
        "github.com/lightningnetwork/lnd/channeldb"
9
        "github.com/lightningnetwork/lnd/fn/v2"
10
        "github.com/lightningnetwork/lnd/input"
11
        "github.com/lightningnetwork/lnd/tlv"
12
)
13

14
// htlcLeaseResolver is a struct that houses the lease specific HTLC resolution
15
// logic. This includes deriving the _true_ waiting height, as well as the
16
// input to offer to the sweeper.
17
type htlcLeaseResolver struct {
18
        // channelInitiator denotes whether the party responsible for resolving
19
        // the contract initiated the channel.
20
        channelInitiator bool
21

22
        // leaseExpiry denotes the additional waiting period the contract must
23
        // hold until it can be resolved. This waiting period is known as the
24
        // expiration of a script-enforced leased channel and only applies to
25
        // the channel initiator.
26
        //
27
        // NOTE: This value should only be set when the contract belongs to a
28
        // leased channel.
29
        leaseExpiry uint32
30
}
31

32
// hasCLTV denotes whether the resolver must wait for an additional CLTV to
33
// expire before resolving the contract.
34
func (h *htlcLeaseResolver) hasCLTV() bool {
12✔
35
        return h.channelInitiator && h.leaseExpiry > 0
12✔
36
}
12✔
37

38
// deriveWaitHeight computes the height the resolver needs to wait until it can
39
// sweep the input.
40
func (h *htlcLeaseResolver) deriveWaitHeight(csvDelay uint32,
41
        commitSpend *chainntnfs.SpendDetail) uint32 {
4✔
42

4✔
43
        waitHeight := uint32(commitSpend.SpendingHeight) + csvDelay - 1
4✔
44
        if h.hasCLTV() {
4✔
45
                waitHeight = uint32(math.Max(
×
46
                        float64(waitHeight), float64(h.leaseExpiry),
×
47
                ))
×
48
        }
×
49

50
        return waitHeight
4✔
51
}
52

53
// makeSweepInput constructs the type of input (either just csv or csv+ctlv) to
54
// send to the sweeper so the output can ultimately be swept.
55
func (h *htlcLeaseResolver) makeSweepInput(op *wire.OutPoint,
56
        wType, cltvWtype input.StandardWitnessType,
57
        signDesc *input.SignDescriptor, csvDelay, broadcastHeight uint32,
58
        payHash [32]byte, resBlob fn.Option[tlv.Blob]) *input.BaseInput {
4✔
59

4✔
60
        log.Infof("%T(%x): offering second-layer output to sweeper: %v", h,
4✔
61
                payHash, op)
4✔
62

4✔
63
        if h.hasCLTV() {
4✔
64
                return input.NewCsvInputWithCltv(
×
65
                        op, cltvWtype, signDesc,
×
66
                        broadcastHeight, csvDelay,
×
67
                        h.leaseExpiry,
×
68
                        input.WithResolutionBlob(resBlob),
×
69
                )
×
70
        }
×
71

72
        log.Infof("%T(%x): CSV lock expired, offering second-layer output to "+
4✔
73
                "sweeper: %v", h, payHash, op)
4✔
74

4✔
75
        return input.NewCsvInput(
4✔
76
                op, wType, signDesc, broadcastHeight, csvDelay,
4✔
77
                input.WithResolutionBlob(resBlob),
4✔
78
        )
4✔
79
}
80

81
// SupplementState allows the user of a ContractResolver to supplement it with
82
// state required for the proper resolution of a contract.
83
//
84
// NOTE: Part of the ContractResolver interface.
85
func (h *htlcLeaseResolver) SupplementState(state *channeldb.OpenChannel) {
2✔
86
        if state.ChanType.HasLeaseExpiration() {
2✔
87
                h.leaseExpiry = state.ThawHeight
×
88
        }
×
89
        h.channelInitiator = state.IsInitiator
2✔
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc