• 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

73.33
/labels/labels.go
1
// Package labels contains labels used to label transactions broadcast by lnd.
2
// These labels are used across packages, so they are declared in a separate
3
// package to avoid dependency issues.
4
//
5
// Labels for transactions broadcast by lnd have two set fields followed by an
6
// optional set labelled data values, all separated by colons.
7
//   - Label version: an integer that indicates the version lnd used
8
//   - Label type: the type of transaction we are labelling
9
//   - {field name}-{value}: a named field followed by its value, these items are
10
//     optional, and there may be more than field present.
11
//
12
// For version 0 we have the following optional data fields defined:
13
//   - shortchanid: the short channel ID that a transaction is associated with,
14
//     with its value set to the uint64 short channel id.
15
package labels
16

17
import (
18
        "fmt"
19

20
        "github.com/btcsuite/btcwallet/wtxmgr"
21
        "github.com/lightningnetwork/lnd/lnwire"
22
)
23

24
// External labels a transaction as user initiated via the api. This
25
// label is only used when a custom user provided label is not given.
26
const External = "external"
27

28
// ValidateAPI returns the generic api label if the label provided is empty.
29
// This allows us to label all transactions published by the api, even if
30
// no label is provided. If a label is provided, it is validated against
31
// the known restrictions.
32
func ValidateAPI(label string) (string, error) {
4✔
33
        if len(label) > wtxmgr.TxLabelLimit {
4✔
34
                return "", fmt.Errorf("label length: %v exceeds "+
×
35
                        "limit of %v", len(label), wtxmgr.TxLabelLimit)
×
36
        }
×
37

38
        // If no label was provided by the user, add the generic user
39
        // send label.
40
        if len(label) == 0 {
8✔
41
                return External, nil
4✔
42
        }
4✔
43

44
        return label, nil
×
45
}
46

47
// LabelVersion versions our labels so they can be easily update to contain
48
// new data while still easily string matched.
49
type LabelVersion uint8
50

51
// LabelVersionZero is the label version for labels that contain label type and
52
// channel ID (where available).
53
const LabelVersionZero LabelVersion = iota
54

55
// LabelType indicates the type of label we are creating. It is a string rather
56
// than an int for easy string matching and human-readability.
57
type LabelType string
58

59
const (
60
        // LabelTypeChannelOpen is used to label channel opens.
61
        LabelTypeChannelOpen LabelType = "openchannel"
62

63
        // LabelTypeChannelClose is used to label channel closes.
64
        LabelTypeChannelClose LabelType = "closechannel"
65

66
        // LabelTypeJusticeTransaction is used to label justice transactions.
67
        LabelTypeJusticeTransaction LabelType = "justicetx"
68

69
        // LabelTypeSweepTransaction is used to label sweeps.
70
        LabelTypeSweepTransaction LabelType = "sweep"
71
)
72

73
// LabelField is used to tag a value within a label.
74
type LabelField string
75

76
const (
77
        // ShortChanID is used to tag short channel id values in our labels.
78
        ShortChanID LabelField = "shortchanid"
79
)
80

81
// MakeLabel creates a label with the provided type and short channel id. If
82
// our short channel ID is not known, we simply return version:label_type. If
83
// we do have a short channel ID set, the label will also contain its value:
84
// shortchanid-{int64 chan ID}.
85
func MakeLabel(labelType LabelType, channelID *lnwire.ShortChannelID) string {
128✔
86
        if channelID == nil {
199✔
87
                return fmt.Sprintf("%v:%v", LabelVersionZero, labelType)
71✔
88
        }
71✔
89

90
        return fmt.Sprintf("%v:%v:%v-%v", LabelVersionZero, labelType,
57✔
91
                ShortChanID, channelID.ToUint64())
57✔
92
}
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