• 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

72.34
/graph/db/models/node.go
1
package models
2

3
import (
4
        "fmt"
5
        "image/color"
6
        "net"
7
        "time"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
11
        "github.com/lightningnetwork/lnd/lnwire"
12
)
13

14
// LightningNode represents an individual vertex/node within the channel graph.
15
// A node is connected to other nodes by one or more channel edges emanating
16
// from it. As the graph is directed, a node will also have an incoming edge
17
// attached to it for each outgoing edge.
18
type LightningNode struct {
19
        // PubKeyBytes is the raw bytes of the public key of the target node.
20
        PubKeyBytes [33]byte
21
        pubKey      *btcec.PublicKey
22

23
        // HaveNodeAnnouncement indicates whether we received a node
24
        // announcement for this particular node. If true, the remaining fields
25
        // will be set, if false only the PubKey is known for this node.
26
        HaveNodeAnnouncement bool
27

28
        // LastUpdate is the last time the vertex information for this node has
29
        // been updated.
30
        LastUpdate time.Time
31

32
        // Address is the TCP address this node is reachable over.
33
        Addresses []net.Addr
34

35
        // Color is the selected color for the node.
36
        Color color.RGBA
37

38
        // Alias is a nick-name for the node. The alias can be used to confirm
39
        // a node's identity or to serve as a short ID for an address book.
40
        Alias string
41

42
        // AuthSigBytes is the raw signature under the advertised public key
43
        // which serves to authenticate the attributes announced by this node.
44
        AuthSigBytes []byte
45

46
        // Features is the list of protocol features supported by this node.
47
        Features *lnwire.FeatureVector
48

49
        // ExtraOpaqueData is the set of data that was appended to this
50
        // message, some of which we may not actually know how to iterate or
51
        // parse. By holding onto this data, we ensure that we're able to
52
        // properly validate the set of signatures that cover these new fields,
53
        // and ensure we're able to make upgrades to the network in a forwards
54
        // compatible manner.
55
        ExtraOpaqueData []byte
56

57
        // TODO(roasbeef): discovery will need storage to keep it's last IP
58
        // address and re-announce if interface changes?
59

60
        // TODO(roasbeef): add update method and fetch?
61
}
62

63
// PubKey is the node's long-term identity public key. This key will be used to
64
// authenticated any advertisements/updates sent by the node.
65
//
66
// NOTE: By having this method to access an attribute, we ensure we only need
67
// to fully deserialize the pubkey if absolutely necessary.
68
func (l *LightningNode) PubKey() (*btcec.PublicKey, error) {
1,484✔
69
        if l.pubKey != nil {
1,975✔
70
                return l.pubKey, nil
491✔
71
        }
491✔
72

73
        key, err := btcec.ParsePubKey(l.PubKeyBytes[:])
993✔
74
        if err != nil {
993✔
75
                return nil, err
×
76
        }
×
77
        l.pubKey = key
993✔
78

993✔
79
        return key, nil
993✔
80
}
81

82
// AuthSig is a signature under the advertised public key which serves to
83
// authenticate the attributes announced by this node.
84
//
85
// NOTE: By having this method to access an attribute, we ensure we only need
86
// to fully deserialize the signature if absolutely necessary.
87
func (l *LightningNode) AuthSig() (*ecdsa.Signature, error) {
×
88
        return ecdsa.ParseSignature(l.AuthSigBytes)
×
89
}
×
90

91
// AddPubKey is a setter-link method that can be used to swap out the public
92
// key for a node.
93
func (l *LightningNode) AddPubKey(key *btcec.PublicKey) {
60✔
94
        l.pubKey = key
60✔
95
        copy(l.PubKeyBytes[:], key.SerializeCompressed())
60✔
96
}
60✔
97

98
// NodeAnnouncement retrieves the latest node announcement of the node.
99
func (l *LightningNode) NodeAnnouncement(signed bool) (*lnwire.NodeAnnouncement,
100
        error) {
14✔
101

14✔
102
        if !l.HaveNodeAnnouncement {
14✔
103
                return nil, fmt.Errorf("node does not have node announcement")
×
104
        }
×
105

106
        alias, err := lnwire.NewNodeAlias(l.Alias)
14✔
107
        if err != nil {
14✔
108
                return nil, err
×
109
        }
×
110

111
        nodeAnn := &lnwire.NodeAnnouncement{
14✔
112
                Features:        l.Features.RawFeatureVector,
14✔
113
                NodeID:          l.PubKeyBytes,
14✔
114
                RGBColor:        l.Color,
14✔
115
                Alias:           alias,
14✔
116
                Addresses:       l.Addresses,
14✔
117
                Timestamp:       uint32(l.LastUpdate.Unix()),
14✔
118
                ExtraOpaqueData: l.ExtraOpaqueData,
14✔
119
        }
14✔
120

14✔
121
        if !signed {
14✔
122
                return nodeAnn, nil
×
123
        }
×
124

125
        sig, err := lnwire.NewSigFromECDSARawSignature(l.AuthSigBytes)
14✔
126
        if err != nil {
14✔
127
                return nil, err
×
128
        }
×
129

130
        nodeAnn.Signature = sig
14✔
131

14✔
132
        return nodeAnn, nil
14✔
133
}
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