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

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

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

56.76
/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
        if h.hasCLTV() {
4✔
61
                log.Infof("%T(%x): CSV and CLTV locks expired, offering "+
×
62
                        "second-layer output to sweeper: %v", h, payHash, op)
×
63

×
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