Skip to content

Commit 79ae4a4

Browse files
committed
multi: remove tor v2 onion address encoder paths
Drop the v2 onion encoder branches from lnwire.WriteOnionAddr, graph/db.encodeOnionAddr, graph/db.collectAddressRecords, and the tor.OnionHostToFakeIP helper. lnd no longer produces v2 onion addresses on any code path. Decoders for v2 addresses are intentionally retained in lnwire, graph/db, and tor so that existing graph databases and peer-announced v2 addresses continue to deserialize. Update unit tests to use v3 onion fixtures.
1 parent 20021f6 commit 79ae4a4

24 files changed

Lines changed: 199 additions & 156 deletions

chanbackup/single_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var (
3838
addr1, _ = net.ResolveTCPAddr("tcp", "10.0.0.2:9000")
3939
addr2, _ = net.ResolveTCPAddr("tcp", "10.0.0.3:9000")
4040
addr3 = &tor.OnionAddr{
41-
OnionService: "3g2upl4pq6kufc4m.onion",
42-
Port: 9735,
41+
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzd" +
42+
"di37ktceo3ah7ngmcopnpyyd.onion",
43+
Port: 9735,
4344
}
4445
addr4 = &lnwire.DNSAddress{
4546
Hostname: "example.com",

channeldb/codec.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,24 @@ func WriteElement(w io.Writer, element interface{}) error {
189189
}
190190

191191
case net.Addr:
192-
if err := graphdb.SerializeAddr(w, e); err != nil {
192+
// Route through the filter so a stale v2 onion address is
193+
// dropped (with a warning) rather than failing the whole
194+
// write with "unknown onion service length".
195+
filtered := graphdb.FilterSerializableAddrs([]net.Addr{e})
196+
if len(filtered) == 0 {
197+
return nil
198+
}
199+
if err := graphdb.SerializeAddr(w, filtered[0]); err != nil {
193200
return err
194201
}
195202

196203
case []net.Addr:
197-
if err := WriteElement(w, uint32(len(e))); err != nil {
204+
addrs := graphdb.FilterSerializableAddrs(e)
205+
if err := WriteElement(w, uint32(len(addrs))); err != nil {
198206
return err
199207
}
200208

201-
for _, addr := range e {
209+
for _, addr := range addrs {
202210
if err := graphdb.SerializeAddr(w, addr); err != nil {
203211
return err
204212
}

channeldb/nodes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,14 @@ func serializeLinkNode(w io.Writer, l *LinkNode) error {
358358
return err
359359
}
360360

361-
numAddrs := uint32(len(l.Addresses))
361+
addresses := graphdb.FilterSerializableAddrs(l.Addresses)
362+
numAddrs := uint32(len(addresses))
362363
byteOrder.PutUint32(buf[:4], numAddrs)
363364
if _, err := w.Write(buf[:4]); err != nil {
364365
return err
365366
}
366367

367-
for _, addr := range l.Addresses {
368+
for _, addr := range addresses {
368369
if err := graphdb.SerializeAddr(w, addr); err != nil {
369370
return err
370371
}

docs/configuring_tor.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ by using Tor for anonymous networking to establish connections.
1515

1616
With widespread usage of Onion Services within the network, concerns about the
1717
difficulty of proper NAT traversal are alleviated, as usage of onion services
18-
allows nodes to accept inbound connections even if they're behind a NAT. At the
19-
time of writing this documentation, `lnd` supports both types of onion services:
20-
v2 and v3.
18+
allows nodes to accept inbound connections even if they're behind a NAT. `lnd`
19+
supports v3 onion services only; legacy v2 onion service support has been
20+
removed.
2121

22-
Before following the remainder of this documentation, you should ensure that you
23-
already have Tor installed locally. **If you want to run v3 Onion Services, make
24-
sure that you run at least version 0.3.3.6.**
22+
Before following the remainder of this documentation, you should ensure that
23+
you already have Tor installed locally. **Make sure that you run at least
24+
version 0.3.3.6 of Tor in order to use v3 Onion Services.**
2525
Official instructions to install the latest release of Tor can be found
2626
[here](https://www.torproject.org/docs/tor-doc-unix.html.en).
2727

graph/db/addr.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
104104
var suffixIndex int
105105
hostLen := len(addr.OnionService)
106106
switch hostLen {
107-
case tor.V2Len:
108-
if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
109-
return err
110-
}
111-
suffixIndex = tor.V2Len - tor.OnionSuffixLen
112107
case tor.V3Len:
113108
if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
114109
return err
@@ -131,12 +126,7 @@ func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
131126
}
132127

133128
// Sanity check the decoded length.
134-
switch {
135-
case hostLen == tor.V2Len && len(host) != tor.V2DecodedLen:
136-
return fmt.Errorf("onion service %v decoded to invalid host %x",
137-
addr.OnionService, host)
138-
139-
case hostLen == tor.V3Len && len(host) != tor.V3DecodedLen:
129+
if hostLen == tor.V3Len && len(host) != tor.V3DecodedLen {
140130
return fmt.Errorf("onion service %v decoded to invalid host %x",
141131
addr.OnionService, host)
142132
}
@@ -322,3 +312,26 @@ func SerializeAddr(w io.Writer, address net.Addr) error {
322312
return ErrUnknownAddressType
323313
}
324314
}
315+
316+
// FilterSerializableAddrs returns the subset of addresses that this package
317+
// can currently serialize. Legacy Tor v2 onion addresses are dropped (with a
318+
// warning) since lnd no longer persists them; pre-existing v2 addresses on
319+
// disk can still be deserialized, but any rewrite path must call this helper
320+
// before counting and encoding the address slice so that node updates do not
321+
// fail when a stale v2 address is present.
322+
func FilterSerializableAddrs(addrs []net.Addr) []net.Addr {
323+
out := make([]net.Addr, 0, len(addrs))
324+
for _, a := range addrs {
325+
onion, ok := a.(*tor.OnionAddr)
326+
if ok && len(onion.OnionService) == tor.V2Len {
327+
log.Warnf("Dropping legacy v2 onion address %v "+
328+
"during serialization; v2 is no longer "+
329+
"supported", onion)
330+
331+
continue
332+
}
333+
out = append(out, a)
334+
}
335+
336+
return out
337+
}

graph/db/addr_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ var (
2929
Port: 65535,
3030
}
3131

32-
testOnionV2Addr = &tor.OnionAddr{
33-
OnionService: "3g2upl4pq6kufc4m.onion",
34-
Port: 9735,
35-
}
36-
3732
testOnionV3Addr = &tor.OnionAddr{
3833
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion", //nolint:ll
3934
Port: 80,
@@ -63,9 +58,6 @@ var addrTests = []struct {
6358
{
6459
expAddr: testIPV6Addr,
6560
},
66-
{
67-
expAddr: testOnionV2Addr,
68-
},
6961
{
7062
expAddr: testOnionV3Addr,
7163
},

graph/db/graph_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ func testNodeInsertionAndDeletion(t *testing.T, v lnwire.GossipVersion) {
400400
// Add 2 IPV6 addresses.
401401
testIPV6Addr,
402402
anotherAddr,
403-
// Add one v2 and one v3 onion address.
404-
testOnionV2Addr,
403+
// Add a v3 onion address.
405404
testOnionV3Addr,
406405
// Add a DNS host address.
407406
testDNSAddr,
@@ -439,7 +438,6 @@ func testNodeInsertionAndDeletion(t *testing.T, v lnwire.GossipVersion) {
439438

440439
// Finally, update the set to only contain the Tor addresses.
441440
expAddrs = []net.Addr{
442-
testOnionV2Addr,
443441
testOnionV3Addr,
444442
}
445443
node = nodeWithAddrs(expAddrs)

graph/db/kv_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4699,13 +4699,14 @@ func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket,
46994699
return err
47004700
}
47014701

4702-
numAddresses := uint16(len(node.Addresses))
4702+
addresses := FilterSerializableAddrs(node.Addresses)
4703+
numAddresses := uint16(len(addresses))
47034704
byteOrder.PutUint16(scratch[:2], numAddresses)
47044705
if _, err := b.Write(scratch[:2]); err != nil {
47054706
return err
47064707
}
47074708

4708-
for _, address := range node.Addresses {
4709+
for _, address := range addresses {
47094710
if err := SerializeAddr(&b, address); err != nil {
47104711
return err
47114712
}

graph/db/sql_store.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,7 +4821,6 @@ func collectAddressRecords(addresses []net.Addr) (map[dbAddressType][]string,
48214821
newAddresses := map[dbAddressType][]string{
48224822
addressTypeIPv4: {},
48234823
addressTypeIPv6: {},
4824-
addressTypeTorV2: {},
48254824
addressTypeTorV3: {},
48264825
addressTypeDNS: {},
48274826
addressTypeOpaque: {},
@@ -4844,10 +4843,12 @@ func collectAddressRecords(addresses []net.Addr) (map[dbAddressType][]string,
48444843

48454844
case *tor.OnionAddr:
48464845
switch len(addr.OnionService) {
4847-
case tor.V2Len:
4848-
addAddr(addressTypeTorV2, addr)
48494846
case tor.V3Len:
48504847
addAddr(addressTypeTorV3, addr)
4848+
case tor.V2Len:
4849+
log.Warnf("Dropping legacy v2 onion address "+
4850+
"%v during upsert; v2 is no longer "+
4851+
"supported", addr)
48514852
default:
48524853
return nil, fmt.Errorf("invalid length for " +
48534854
"a tor address")

itest/lnd_channel_graph_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ func testNodeAnnouncement(ht *lntest.HarnessTest) {
378378
advertisedAddrs := []string{
379379
"192.168.1.1:8333",
380380
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
381-
"bkb6azqggsaiskzi.onion:9735",
382381
"fomvuglh6h6vcag73xo5t5gv56ombih3zr2xvplkpbfd7wrog4swj" +
383382
"wid.onion:1234",
384383
}
@@ -435,7 +434,6 @@ func testUpdateNodeAnnouncement(ht *lntest.HarnessTest) {
435434
extraAddrs := []string{
436435
"192.168.1.1:8333",
437436
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
438-
"bkb6azqggsaiskzi.onion:9735",
439437
"fomvuglh6h6vcag73xo5t5gv56ombih3zr2xvplkpbfd7wrog4swj" +
440438
"wid.onion:1234",
441439
}

0 commit comments

Comments
 (0)