• 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

0.0
/channeldb/migration_01_to_11/addr.go
1
package migration_01_to_11
2

3
import (
4
        "encoding/binary"
5
        "errors"
6
        "fmt"
7
        "io"
8
        "net"
9

10
        "github.com/lightningnetwork/lnd/tor"
11
)
12

13
// addressType specifies the network protocol and version that should be used
14
// when connecting to a node at a particular address.
15
type addressType uint8
16

17
const (
18
        // tcp4Addr denotes an IPv4 TCP address.
19
        tcp4Addr addressType = 0
20

21
        // tcp6Addr denotes an IPv6 TCP address.
22
        tcp6Addr addressType = 1
23

24
        // v2OnionAddr denotes a version 2 Tor onion service address.
25
        v2OnionAddr addressType = 2
26

27
        // v3OnionAddr denotes a version 3 Tor (prop224) onion service address.
28
        v3OnionAddr addressType = 3
29
)
30

31
// encodeTCPAddr serializes a TCP address into its compact raw bytes
32
// representation.
UNCOV
33
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
×
UNCOV
34
        var (
×
UNCOV
35
                addrType byte
×
UNCOV
36
                ip       []byte
×
UNCOV
37
        )
×
UNCOV
38

×
UNCOV
39
        if addr.IP.To4() != nil {
×
UNCOV
40
                addrType = byte(tcp4Addr)
×
UNCOV
41
                ip = addr.IP.To4()
×
UNCOV
42
        } else {
×
UNCOV
43
                addrType = byte(tcp6Addr)
×
UNCOV
44
                ip = addr.IP.To16()
×
UNCOV
45
        }
×
46

UNCOV
47
        if ip == nil {
×
48
                return fmt.Errorf("unable to encode IP %v", addr.IP)
×
49
        }
×
50

UNCOV
51
        if _, err := w.Write([]byte{addrType}); err != nil {
×
52
                return err
×
53
        }
×
54

UNCOV
55
        if _, err := w.Write(ip); err != nil {
×
56
                return err
×
57
        }
×
58

UNCOV
59
        var port [2]byte
×
UNCOV
60
        byteOrder.PutUint16(port[:], uint16(addr.Port))
×
UNCOV
61
        if _, err := w.Write(port[:]); err != nil {
×
62
                return err
×
63
        }
×
64

UNCOV
65
        return nil
×
66
}
67

68
// encodeOnionAddr serializes an onion address into its compact raw bytes
69
// representation.
70
func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
×
71
        var suffixIndex int
×
72
        hostLen := len(addr.OnionService)
×
73
        switch hostLen {
×
74
        case tor.V2Len:
×
75
                if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
×
76
                        return err
×
77
                }
×
78
                suffixIndex = tor.V2Len - tor.OnionSuffixLen
×
79
        case tor.V3Len:
×
80
                if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
×
81
                        return err
×
82
                }
×
83
                suffixIndex = tor.V3Len - tor.OnionSuffixLen
×
84
        default:
×
85
                return errors.New("unknown onion service length")
×
86
        }
87

88
        suffix := addr.OnionService[suffixIndex:]
×
89
        if suffix != tor.OnionSuffix {
×
90
                return fmt.Errorf("invalid suffix \"%v\"", suffix)
×
91
        }
×
92

93
        host, err := tor.Base32Encoding.DecodeString(
×
94
                addr.OnionService[:suffixIndex],
×
95
        )
×
96
        if err != nil {
×
97
                return err
×
98
        }
×
99

100
        // Sanity check the decoded length.
101
        switch {
×
102
        case hostLen == tor.V2Len && len(host) != tor.V2DecodedLen:
×
103
                return fmt.Errorf("onion service %v decoded to invalid host %x",
×
104
                        addr.OnionService, host)
×
105

106
        case hostLen == tor.V3Len && len(host) != tor.V3DecodedLen:
×
107
                return fmt.Errorf("onion service %v decoded to invalid host %x",
×
108
                        addr.OnionService, host)
×
109
        }
110

111
        if _, err := w.Write(host); err != nil {
×
112
                return err
×
113
        }
×
114

115
        var port [2]byte
×
116
        byteOrder.PutUint16(port[:], uint16(addr.Port))
×
117
        if _, err := w.Write(port[:]); err != nil {
×
118
                return err
×
119
        }
×
120

121
        return nil
×
122
}
123

124
// deserializeAddr reads the serialized raw representation of an address and
125
// deserializes it into the actual address. This allows us to avoid address
126
// resolution within the channeldb package.
UNCOV
127
func deserializeAddr(r io.Reader) (net.Addr, error) {
×
UNCOV
128
        var addrType [1]byte
×
UNCOV
129
        if _, err := r.Read(addrType[:]); err != nil {
×
130
                return nil, err
×
131
        }
×
132

UNCOV
133
        var address net.Addr
×
UNCOV
134
        switch addressType(addrType[0]) {
×
UNCOV
135
        case tcp4Addr:
×
UNCOV
136
                var ip [4]byte
×
UNCOV
137
                if _, err := r.Read(ip[:]); err != nil {
×
138
                        return nil, err
×
139
                }
×
140

UNCOV
141
                var port [2]byte
×
UNCOV
142
                if _, err := r.Read(port[:]); err != nil {
×
143
                        return nil, err
×
144
                }
×
145

UNCOV
146
                address = &net.TCPAddr{
×
UNCOV
147
                        IP:   net.IP(ip[:]),
×
UNCOV
148
                        Port: int(binary.BigEndian.Uint16(port[:])),
×
UNCOV
149
                }
×
UNCOV
150
        case tcp6Addr:
×
UNCOV
151
                var ip [16]byte
×
UNCOV
152
                if _, err := r.Read(ip[:]); err != nil {
×
153
                        return nil, err
×
154
                }
×
155

UNCOV
156
                var port [2]byte
×
UNCOV
157
                if _, err := r.Read(port[:]); err != nil {
×
158
                        return nil, err
×
159
                }
×
160

UNCOV
161
                address = &net.TCPAddr{
×
UNCOV
162
                        IP:   net.IP(ip[:]),
×
UNCOV
163
                        Port: int(binary.BigEndian.Uint16(port[:])),
×
UNCOV
164
                }
×
165
        case v2OnionAddr:
×
166
                var h [tor.V2DecodedLen]byte
×
167
                if _, err := r.Read(h[:]); err != nil {
×
168
                        return nil, err
×
169
                }
×
170

171
                var p [2]byte
×
172
                if _, err := r.Read(p[:]); err != nil {
×
173
                        return nil, err
×
174
                }
×
175

176
                onionService := tor.Base32Encoding.EncodeToString(h[:])
×
177
                onionService += tor.OnionSuffix
×
178
                port := int(binary.BigEndian.Uint16(p[:]))
×
179

×
180
                address = &tor.OnionAddr{
×
181
                        OnionService: onionService,
×
182
                        Port:         port,
×
183
                }
×
184
        case v3OnionAddr:
×
185
                var h [tor.V3DecodedLen]byte
×
186
                if _, err := r.Read(h[:]); err != nil {
×
187
                        return nil, err
×
188
                }
×
189

190
                var p [2]byte
×
191
                if _, err := r.Read(p[:]); err != nil {
×
192
                        return nil, err
×
193
                }
×
194

195
                onionService := tor.Base32Encoding.EncodeToString(h[:])
×
196
                onionService += tor.OnionSuffix
×
197
                port := int(binary.BigEndian.Uint16(p[:]))
×
198

×
199
                address = &tor.OnionAddr{
×
200
                        OnionService: onionService,
×
201
                        Port:         port,
×
202
                }
×
203
        default:
×
204
                return nil, ErrUnknownAddressType
×
205
        }
206

UNCOV
207
        return address, nil
×
208
}
209

210
// serializeAddr serializes an address into its raw bytes representation so that
211
// it can be deserialized without requiring address resolution.
UNCOV
212
func serializeAddr(w io.Writer, address net.Addr) error {
×
UNCOV
213
        switch addr := address.(type) {
×
UNCOV
214
        case *net.TCPAddr:
×
UNCOV
215
                return encodeTCPAddr(w, addr)
×
216
        case *tor.OnionAddr:
×
217
                return encodeOnionAddr(w, addr)
×
218
        default:
×
219
                return ErrUnknownAddressType
×
220
        }
221
}
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