Skip to content

Commit 5b14753

Browse files
ellemoutonmohamedawnallah
authored andcommitted
graph/db+chanbackup: dns add encoding/decoding for persistence
1 parent 444edbd commit 5b14753

File tree

7 files changed

+107
-11
lines changed

7 files changed

+107
-11
lines changed

chanbackup/backup_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func TestFetchStaticChanBackups(t *testing.T) {
161161
chanSource.addAddrsForNode(randomChan2.IdentityPub, []net.Addr{addr2})
162162
chanSource.addAddrsForNode(randomChan2.IdentityPub, []net.Addr{addr3})
163163
chanSource.addAddrsForNode(randomChan2.IdentityPub, []net.Addr{addr4})
164+
chanSource.addAddrsForNode(randomChan2.IdentityPub, []net.Addr{addr5})
164165

165166
// With the channel source populated, we'll now attempt to create a set
166167
// of backups for all the channels. This should succeed, as all items

chanbackup/multi_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestMultiPackUnpack(t *testing.T) {
2323
}
2424

2525
single := NewSingle(
26-
channel, []net.Addr{addr1, addr2, addr3, addr4},
26+
channel, []net.Addr{addr1, addr2, addr3, addr4, addr5},
2727
)
2828

2929
originalSingles = append(originalSingles, single)

chanbackup/single_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ var (
4141
OnionService: "3g2upl4pq6kufc4m.onion",
4242
Port: 9735,
4343
}
44-
addr4 = &lnwire.OpaqueAddrs{
44+
addr4 = &lnwire.DNSAddress{
45+
Hostname: "example.com",
46+
Port: 8080,
47+
}
48+
addr5 = &lnwire.OpaqueAddrs{
4549
// The first byte must be an address type we are not yet aware
4650
// of for it to be a valid OpaqueAddrs.
4751
Payload: []byte{math.MaxUint8, 1, 2, 3, 4},
@@ -320,7 +324,7 @@ func TestSinglePackUnpack(t *testing.T) {
320324
require.NoError(t, err, "unable to gen open channel")
321325

322326
singleChanBackup := NewSingle(
323-
channel, []net.Addr{addr1, addr2, addr3, addr4},
327+
channel, []net.Addr{addr1, addr2, addr3, addr4, addr5},
324328
)
325329

326330
keyRing := &lnencrypt.MockKeyRing{}
@@ -647,7 +651,7 @@ func TestSingleUnconfirmedChannel(t *testing.T) {
647651
channel.FundingBroadcastHeight = fundingBroadcastHeight
648652

649653
singleChanBackup := NewSingle(
650-
channel, []net.Addr{addr1, addr2, addr3, addr4},
654+
channel, []net.Addr{addr1, addr2, addr3, addr4, addr5},
651655
)
652656
keyRing := &lnencrypt.MockKeyRing{}
653657

graph/db/addr.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,36 @@ const (
3131
// opaqueAddrs denotes an address (or a set of addresses) that LND was
3232
// not able to parse since LND is not yet aware of the address type.
3333
opaqueAddrs addressType = 4
34+
35+
// dnsAddr denotes a DNS address type.
36+
dnsAddr addressType = 5
3437
)
3538

39+
// encodeDNSAddr encodes a DNS address.
40+
func encodeDNSAddr(w io.Writer, addr *lnwire.DNSAddress) error {
41+
if _, err := w.Write([]byte{byte(dnsAddr)}); err != nil {
42+
return err
43+
}
44+
45+
// Write the length of the hostname.
46+
hostLen := len(addr.Hostname)
47+
if _, err := w.Write([]byte{byte(hostLen)}); err != nil {
48+
return err
49+
}
50+
51+
if _, err := w.Write([]byte(addr.Hostname)); err != nil {
52+
return err
53+
}
54+
55+
var port [2]byte
56+
byteOrder.PutUint16(port[:], addr.Port)
57+
if _, err := w.Write(port[:]); err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}
63+
3664
// encodeTCPAddr serializes a TCP address into its compact raw bytes
3765
// representation.
3866
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
@@ -230,6 +258,30 @@ func DeserializeAddr(r io.Reader) (net.Addr, error) {
230258
Port: port,
231259
}
232260

261+
case dnsAddr:
262+
// Read the length of the hostname.
263+
var hostLen [1]byte
264+
if _, err := r.Read(hostLen[:]); err != nil {
265+
return nil, err
266+
}
267+
268+
// Read the hostname.
269+
hostname := make([]byte, hostLen[0])
270+
if _, err := r.Read(hostname); err != nil {
271+
return nil, err
272+
}
273+
274+
// Read the port.
275+
var port [2]byte
276+
if _, err := r.Read(port[:]); err != nil {
277+
return nil, err
278+
}
279+
280+
address = &lnwire.DNSAddress{
281+
Hostname: string(hostname),
282+
Port: binary.BigEndian.Uint16(port[:]),
283+
}
284+
233285
case opaqueAddrs:
234286
// Read the length of the payload.
235287
var l [2]byte
@@ -264,6 +316,8 @@ func SerializeAddr(w io.Writer, address net.Addr) error {
264316
return encodeOnionAddr(w, addr)
265317
case *lnwire.OpaqueAddrs:
266318
return encodeOpaqueAddrs(w, addr)
319+
case *lnwire.DNSAddress:
320+
return encodeDNSAddr(w, addr)
267321
default:
268322
return ErrUnknownAddressType
269323
}

graph/db/addr_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/lightningnetwork/lnd/lnwire"
910
"github.com/lightningnetwork/lnd/tor"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -38,6 +39,18 @@ var (
3839
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", //nolint:ll
3940
Port: 80,
4041
}
42+
43+
testOpaqueAddr = &lnwire.OpaqueAddrs{
44+
// NOTE: the first byte is a protocol level address type. So
45+
// for we set it to 0xff to guarantee that we do not know this
46+
// type yet.
47+
Payload: []byte{0xff, 0x02, 0x03, 0x04, 0x05, 0x06},
48+
}
49+
50+
testDNSAddr = &lnwire.DNSAddress{
51+
Hostname: "example.com",
52+
Port: 8080,
53+
}
4154
)
4255

4356
var addrTests = []struct {
@@ -57,6 +70,12 @@ var addrTests = []struct {
5770
{
5871
expAddr: testOnionV3Addr,
5972
},
73+
{
74+
expAddr: testOpaqueAddr,
75+
},
76+
{
77+
expAddr: testDNSAddr,
78+
},
6079

6180
// Invalid addresses.
6281
{

graph/db/graph_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ var (
3737
Port: 9000}
3838
anotherAddr, _ = net.ResolveTCPAddr("tcp",
3939
"[2001:db8:85a3:0:0:8a2e:370:7334]:80")
40-
testAddrs = []net.Addr{testAddr, anotherAddr}
41-
testOpaqueAddr = &lnwire.OpaqueAddrs{
42-
// NOTE: the first byte is a protocol level address type. So
43-
// for we set it to 0xff to guarantee that we do not know this
44-
// type yet.
45-
Payload: []byte{0xff, 0x02, 0x03, 0x04, 0x05, 0x06},
46-
}
40+
testAddrs = []net.Addr{testAddr, anotherAddr}
4741

4842
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571319d18" +
4943
"e949ddfa2965fb6caa1bf0314f882d7")
@@ -211,6 +205,8 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
211205
// Add one v2 and one v3 onion address.
212206
testOnionV2Addr,
213207
testOnionV3Addr,
208+
// Add a DNS host address.
209+
testDNSAddr,
214210
// Make sure to also test the opaque address type.
215211
testOpaqueAddr,
216212
}

graph/db/sql_store.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,7 @@ const (
35203520
addressTypeIPv6 dbAddressType = 2
35213521
addressTypeTorV2 dbAddressType = 3
35223522
addressTypeTorV3 dbAddressType = 4
3523+
addressTypeDNS dbAddressType = 5
35233524
addressTypeOpaque dbAddressType = math.MaxInt8
35243525
)
35253526

@@ -3547,6 +3548,7 @@ func upsertNodeAddresses(ctx context.Context, db SQLQueries, nodeID int64,
35473548
addressTypeIPv6: {},
35483549
addressTypeTorV2: {},
35493550
addressTypeTorV3: {},
3551+
addressTypeDNS: {},
35503552
addressTypeOpaque: {},
35513553
}
35523554
addAddr := func(t dbAddressType, addr net.Addr) {
@@ -3576,6 +3578,9 @@ func upsertNodeAddresses(ctx context.Context, db SQLQueries, nodeID int64,
35763578
"address")
35773579
}
35783580

3581+
case *lnwire.DNSAddress:
3582+
addAddr(addressTypeDNS, addr)
3583+
35793584
case *lnwire.OpaqueAddrs:
35803585
addAddr(addressTypeOpaque, addr)
35813586

@@ -4650,6 +4655,23 @@ func parseAddress(addrType dbAddressType, address string) (net.Addr, error) {
46504655
Port: port,
46514656
}, nil
46524657

4658+
case addressTypeDNS:
4659+
hostname, portStr, err := net.SplitHostPort(address)
4660+
if err != nil {
4661+
return nil, fmt.Errorf("unable to split DNS "+
4662+
"address: %v", address)
4663+
}
4664+
4665+
port, err := strconv.Atoi(portStr)
4666+
if err != nil {
4667+
return nil, err
4668+
}
4669+
4670+
return &lnwire.DNSAddress{
4671+
Hostname: hostname,
4672+
Port: uint16(port),
4673+
}, nil
4674+
46534675
case addressTypeOpaque:
46544676
opaque, err := hex.DecodeString(address)
46554677
if err != nil {

0 commit comments

Comments
 (0)