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

lightningnetwork / lnd / 13566028875

27 Feb 2025 12:09PM UTC coverage: 49.396% (-9.4%) from 58.748%
13566028875

Pull #9555

github

ellemouton
graph/db: populate the graph cache in Start instead of during construction

In this commit, we move the graph cache population logic out of the
ChannelGraph constructor and into its Start method instead.
Pull Request #9555: graph: extract cache from CRUD [6]

34 of 54 new or added lines in 4 files covered. (62.96%)

27464 existing lines in 436 files now uncovered.

101095 of 204664 relevant lines covered (49.4%)

1.54 hits per line

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

36.76
/graph/db/models/channel_edge_info.go
1
package models
2

3
import (
4
        "bytes"
5
        "fmt"
6

7
        "github.com/btcsuite/btcd/btcec/v2"
8
        "github.com/btcsuite/btcd/btcutil"
9
        "github.com/btcsuite/btcd/chaincfg/chainhash"
10
        "github.com/btcsuite/btcd/wire"
11
        "github.com/lightningnetwork/lnd/fn/v2"
12
)
13

14
// ChannelEdgeInfo represents a fully authenticated channel along with all its
15
// unique attributes. Once an authenticated channel announcement has been
16
// processed on the network, then an instance of ChannelEdgeInfo encapsulating
17
// the channels attributes is stored. The other portions relevant to routing
18
// policy of a channel are stored within a ChannelEdgePolicy for each direction
19
// of the channel.
20
type ChannelEdgeInfo struct {
21
        // ChannelID is the unique channel ID for the channel. The first 3
22
        // bytes are the block height, the next 3 the index within the block,
23
        // and the last 2 bytes are the output index for the channel.
24
        ChannelID uint64
25

26
        // ChainHash is the hash that uniquely identifies the chain that this
27
        // channel was opened within.
28
        //
29
        // TODO(roasbeef): need to modify db keying for multi-chain
30
        //  * must add chain hash to prefix as well
31
        ChainHash chainhash.Hash
32

33
        // NodeKey1Bytes is the raw public key of the first node.
34
        NodeKey1Bytes [33]byte
35
        nodeKey1      *btcec.PublicKey
36

37
        // NodeKey2Bytes is the raw public key of the first node.
38
        NodeKey2Bytes [33]byte
39
        nodeKey2      *btcec.PublicKey
40

41
        // BitcoinKey1Bytes is the raw public key of the first node.
42
        BitcoinKey1Bytes [33]byte
43
        bitcoinKey1      *btcec.PublicKey
44

45
        // BitcoinKey2Bytes is the raw public key of the first node.
46
        BitcoinKey2Bytes [33]byte
47
        bitcoinKey2      *btcec.PublicKey
48

49
        // Features is an opaque byte slice that encodes the set of channel
50
        // specific features that this channel edge supports.
51
        Features []byte
52

53
        // AuthProof is the authentication proof for this channel. This proof
54
        // contains a set of signatures binding four identities, which attests
55
        // to the legitimacy of the advertised channel.
56
        AuthProof *ChannelAuthProof
57

58
        // ChannelPoint is the funding outpoint of the channel. This can be
59
        // used to uniquely identify the channel within the channel graph.
60
        ChannelPoint wire.OutPoint
61

62
        // Capacity is the total capacity of the channel, this is determined by
63
        // the value output in the outpoint that created this channel.
64
        Capacity btcutil.Amount
65

66
        // FundingScript holds the script of the channel's funding transaction.
67
        //
68
        // NOTE: this is not currently persisted and so will not be present if
69
        // the edge object is loaded from the database.
70
        FundingScript fn.Option[[]byte]
71

72
        // ExtraOpaqueData is the set of data that was appended to this
73
        // message, some of which we may not actually know how to iterate or
74
        // parse. By holding onto this data, we ensure that we're able to
75
        // properly validate the set of signatures that cover these new fields,
76
        // and ensure we're able to make upgrades to the network in a forwards
77
        // compatible manner.
78
        ExtraOpaqueData []byte
79
}
80

81
// AddNodeKeys is a setter-like method that can be used to replace the set of
82
// keys for the target ChannelEdgeInfo.
83
func (c *ChannelEdgeInfo) AddNodeKeys(nodeKey1, nodeKey2, bitcoinKey1,
UNCOV
84
        bitcoinKey2 *btcec.PublicKey) {
×
UNCOV
85

×
UNCOV
86
        c.nodeKey1 = nodeKey1
×
UNCOV
87
        copy(c.NodeKey1Bytes[:], c.nodeKey1.SerializeCompressed())
×
UNCOV
88

×
UNCOV
89
        c.nodeKey2 = nodeKey2
×
UNCOV
90
        copy(c.NodeKey2Bytes[:], nodeKey2.SerializeCompressed())
×
UNCOV
91

×
UNCOV
92
        c.bitcoinKey1 = bitcoinKey1
×
UNCOV
93
        copy(c.BitcoinKey1Bytes[:], c.bitcoinKey1.SerializeCompressed())
×
UNCOV
94

×
UNCOV
95
        c.bitcoinKey2 = bitcoinKey2
×
UNCOV
96
        copy(c.BitcoinKey2Bytes[:], bitcoinKey2.SerializeCompressed())
×
UNCOV
97
}
×
98

99
// NodeKey1 is the identity public key of the "first" node that was involved in
100
// the creation of this channel. A node is considered "first" if the
101
// lexicographical ordering the its serialized public key is "smaller" than
102
// that of the other node involved in channel creation.
103
//
104
// NOTE: By having this method to access an attribute, we ensure we only need
105
// to fully deserialize the pubkey if absolutely necessary.
106
func (c *ChannelEdgeInfo) NodeKey1() (*btcec.PublicKey, error) {
3✔
107
        if c.nodeKey1 != nil {
6✔
108
                return c.nodeKey1, nil
3✔
109
        }
3✔
110

111
        key, err := btcec.ParsePubKey(c.NodeKey1Bytes[:])
3✔
112
        if err != nil {
3✔
113
                return nil, err
×
114
        }
×
115
        c.nodeKey1 = key
3✔
116

3✔
117
        return key, nil
3✔
118
}
119

120
// NodeKey2 is the identity public key of the "second" node that was involved in
121
// the creation of this channel. A node is considered "second" if the
122
// lexicographical ordering the its serialized public key is "larger" than that
123
// of the other node involved in channel creation.
124
//
125
// NOTE: By having this method to access an attribute, we ensure we only need
126
// to fully deserialize the pubkey if absolutely necessary.
127
func (c *ChannelEdgeInfo) NodeKey2() (*btcec.PublicKey, error) {
3✔
128
        if c.nodeKey2 != nil {
6✔
129
                return c.nodeKey2, nil
3✔
130
        }
3✔
131

132
        key, err := btcec.ParsePubKey(c.NodeKey2Bytes[:])
3✔
133
        if err != nil {
3✔
134
                return nil, err
×
135
        }
×
136
        c.nodeKey2 = key
3✔
137

3✔
138
        return key, nil
3✔
139
}
140

141
// BitcoinKey1 is the Bitcoin multi-sig key belonging to the first node, that
142
// was involved in the funding transaction that originally created the channel
143
// that this struct represents.
144
//
145
// NOTE: By having this method to access an attribute, we ensure we only need
146
// to fully deserialize the pubkey if absolutely necessary.
147
func (c *ChannelEdgeInfo) BitcoinKey1() (*btcec.PublicKey, error) {
×
148
        if c.bitcoinKey1 != nil {
×
149
                return c.bitcoinKey1, nil
×
150
        }
×
151

152
        key, err := btcec.ParsePubKey(c.BitcoinKey1Bytes[:])
×
153
        if err != nil {
×
154
                return nil, err
×
155
        }
×
156
        c.bitcoinKey1 = key
×
157

×
158
        return key, nil
×
159
}
160

161
// BitcoinKey2 is the Bitcoin multi-sig key belonging to the second node, that
162
// was involved in the funding transaction that originally created the channel
163
// that this struct represents.
164
//
165
// NOTE: By having this method to access an attribute, we ensure we only need
166
// to fully deserialize the pubkey if absolutely necessary.
167
func (c *ChannelEdgeInfo) BitcoinKey2() (*btcec.PublicKey, error) {
×
168
        if c.bitcoinKey2 != nil {
×
169
                return c.bitcoinKey2, nil
×
170
        }
×
171

172
        key, err := btcec.ParsePubKey(c.BitcoinKey2Bytes[:])
×
173
        if err != nil {
×
174
                return nil, err
×
175
        }
×
176
        c.bitcoinKey2 = key
×
177

×
178
        return key, nil
×
179
}
180

181
// OtherNodeKeyBytes returns the node key bytes of the other end of the channel.
182
func (c *ChannelEdgeInfo) OtherNodeKeyBytes(thisNodeKey []byte) (
183
        [33]byte, error) {
3✔
184

3✔
185
        switch {
3✔
186
        case bytes.Equal(c.NodeKey1Bytes[:], thisNodeKey):
3✔
187
                return c.NodeKey2Bytes, nil
3✔
188
        case bytes.Equal(c.NodeKey2Bytes[:], thisNodeKey):
3✔
189
                return c.NodeKey1Bytes, nil
3✔
190
        default:
×
191
                return [33]byte{}, fmt.Errorf("node not participating in " +
×
192
                        "this channel")
×
193
        }
194
}
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