Skip to content

Commit 71b2338

Browse files
committed
graph/db: de(ser)ialise opaque node addrs
In this commit, we fix the bug demonstrated in the prior commit. We correctly handle the persistence of lnwire.OpaqueAddrs.
1 parent d68d24d commit 71b2338

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

graph/db/addr.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net"
99

10+
"github.com/lightningnetwork/lnd/lnwire"
1011
"github.com/lightningnetwork/lnd/tor"
1112
)
1213

@@ -26,6 +27,10 @@ const (
2627

2728
// v3OnionAddr denotes a version 3 Tor (prop224) onion service address.
2829
v3OnionAddr addressType = 3
30+
31+
// opaqueAddrs denotes an address (or a set of addresses) that LND was
32+
// not able to parse since LND is not yet aware of the address type.
33+
opaqueAddrs addressType = 4
2934
)
3035

3136
// encodeTCPAddr serializes a TCP address into its compact raw bytes
@@ -121,6 +126,27 @@ func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
121126
return nil
122127
}
123128

129+
// encodeOpaqueAddrs serializes the lnwire.OpaqueAddrs type to a raw set of
130+
// bytes that we will persist.
131+
func encodeOpaqueAddrs(w io.Writer, addr *lnwire.OpaqueAddrs) error {
132+
// Write the type byte.
133+
if _, err := w.Write([]byte{byte(opaqueAddrs)}); err != nil {
134+
return err
135+
}
136+
137+
// Write the length of the payload.
138+
var l [2]byte
139+
binary.BigEndian.PutUint16(l[:], uint16(len(addr.Payload)))
140+
if _, err := w.Write(l[:]); err != nil {
141+
return err
142+
}
143+
144+
// Write the payload.
145+
_, err := w.Write(addr.Payload)
146+
147+
return err
148+
}
149+
124150
// DeserializeAddr reads the serialized raw representation of an address and
125151
// deserializes it into the actual address. This allows us to avoid address
126152
// resolution within the channeldb package.
@@ -147,6 +173,7 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
147173
IP: net.IP(ip[:]),
148174
Port: int(binary.BigEndian.Uint16(port[:])),
149175
}
176+
150177
case tcp6Addr:
151178
var ip [16]byte
152179
if _, err := r.Read(ip[:]); err != nil {
@@ -162,6 +189,7 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
162189
IP: net.IP(ip[:]),
163190
Port: int(binary.BigEndian.Uint16(port[:])),
164191
}
192+
165193
case v2OnionAddr:
166194
var h [tor.V2DecodedLen]byte
167195
if _, err := r.Read(h[:]); err != nil {
@@ -181,6 +209,7 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
181209
OnionService: onionService,
182210
Port: port,
183211
}
212+
184213
case v3OnionAddr:
185214
var h [tor.V3DecodedLen]byte
186215
if _, err := r.Read(h[:]); err != nil {
@@ -200,6 +229,24 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
200229
OnionService: onionService,
201230
Port: port,
202231
}
232+
233+
case opaqueAddrs:
234+
// Read the length of the payload.
235+
var l [2]byte
236+
if _, err := r.Read(l[:]); err != nil {
237+
return nil, err
238+
}
239+
240+
// Read the payload.
241+
payload := make([]byte, binary.BigEndian.Uint16(l[:]))
242+
if _, err := r.Read(payload); err != nil {
243+
return nil, err
244+
}
245+
246+
address = &lnwire.OpaqueAddrs{
247+
Payload: payload,
248+
}
249+
203250
default:
204251
return nil, ErrUnknownAddressType
205252
}
@@ -215,6 +262,8 @@ func SerializeAddr(w io.Writer, address net.Addr) error {
215262
return encodeTCPAddr(w, addr)
216263
case *tor.OnionAddr:
217264
return encodeOnionAddr(w, addr)
265+
case *lnwire.OpaqueAddrs:
266+
return encodeOpaqueAddrs(w, addr)
218267
default:
219268
return ErrUnknownAddressType
220269
}

graph/db/graph_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,12 +4074,9 @@ var testNodeAnn = "01012674c2e7ef68c73a086b7de2603f4ef1567358df84bb4edaa06c" +
40744074
"80000000d0000000005cd2a001260706204c"
40754075

40764076
// TestLightningNodePersistence takes a raw serialized node announcement
4077-
// message, converts it to our internal models.LightningNode type and attempts
4078-
// to persist this to disk.
4079-
//
4080-
// NOTE: Currently, this tests demonstrates that we are _unable_ to do this if
4081-
// the node announcement has an address type unknown to LND. This will be fixed
4082-
// in an upcoming commit.
4077+
// message, converts it to our internal models.LightningNode type, persists it
4078+
// to disk, reads it again and converts it back to a wire message and asserts
4079+
// that the two messages are equal.
40834080
func TestLightningNodePersistence(t *testing.T) {
40844081
t.Parallel()
40854082

@@ -4100,8 +4097,22 @@ func TestLightningNodePersistence(t *testing.T) {
41004097
// Convert the wire message to our internal node representation.
41014098
node := models.NodeFromWireAnnouncement(na)
41024099

4103-
// Attempt to persist the node to disk. This currently fails due to the
4104-
// unknown address type.
4100+
// Persist the node to disk.
41054101
err = graph.AddLightningNode(node)
4106-
require.ErrorContains(t, err, "address type cannot be resolved")
4102+
require.NoError(t, err)
4103+
4104+
// Read the node from disk.
4105+
diskNode, err := graph.FetchLightningNode(node.PubKeyBytes)
4106+
require.NoError(t, err)
4107+
4108+
// Convert it back to a wire message.
4109+
wireMsg, err := diskNode.NodeAnnouncement(true)
4110+
require.NoError(t, err)
4111+
4112+
// Encode it and compare against the original.
4113+
var b bytes.Buffer
4114+
_, err = lnwire.WriteMessage(&b, wireMsg, 0)
4115+
require.NoError(t, err)
4116+
4117+
require.Equal(t, nodeAnnBytes, b.Bytes())
41074118
}

0 commit comments

Comments
 (0)