Skip to content

Commit 472a2f9

Browse files
committed
multi: add base lookup option to AddLocalAlias
We add an extra option to the AddLocalAlias method which only controls whether we store a reverse lookup from the alias back to the base scid it corresponds to. The previous flag "gossip" is still maintained, and in a way supercedes the new flag (it will also store the base scid lookup even if the base lookup flag isn't set). The only call that sets this option is the XAddLocalChanAlias RPC endpoint, where we want to make sure that a reverse lookup is stored in the alias manager in order to later expose it via the new RPC method.
1 parent f293566 commit 472a2f9

File tree

6 files changed

+63
-13
lines changed

6 files changed

+63
-13
lines changed

aliasmgr/aliasmgr.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,43 @@ func (m *Manager) populateMaps() error {
239239
return nil
240240
}
241241

242+
// addAliasCfg is a struct that hosts various options related to adding a local
243+
// alias to the alias manager.
244+
type addAliasCfg struct {
245+
// baseLookup signals that the alias should also store a reverse look-up
246+
// to the base scid.
247+
baseLookup bool
248+
}
249+
250+
// AddLocalAliasOption is a functional option that modifies the configuration
251+
// for adding a local alias.
252+
type AddLocalAliasOption func(cfg *addAliasCfg)
253+
254+
// WithBaseLookup is a functional option that controls whether a reverse lookup
255+
// will be stored from the alias to the base scid.
256+
func WithBaseLookup() AddLocalAliasOption {
257+
return func(cfg *addAliasCfg) {
258+
cfg.baseLookup = true
259+
}
260+
}
261+
242262
// AddLocalAlias adds a database mapping from the passed alias to the passed
243263
// base SCID. The gossip boolean marks whether or not to create a mapping
244264
// that the gossiper will use. It is set to false for the upgrade path where
245265
// the feature-bit is toggled on and there are existing channels. The linkUpdate
246-
// flag is used to signal whether this function should also trigger an update
247-
// on the htlcswitch scid alias maps.
266+
// flag is used to signal whether this function should also trigger an update on
267+
// the htlcswitch scid alias maps.
268+
//
269+
// NOTE: The following aliases will not be persisted (will be lost on restart):
270+
// - Aliases that were created without gossip flag.
271+
// - Aliases that correspond to confirmed channels.
248272
func (m *Manager) AddLocalAlias(alias, baseScid lnwire.ShortChannelID,
249-
gossip, linkUpdate bool) error {
273+
gossip, linkUpdate bool, opts ...AddLocalAliasOption) error {
274+
275+
cfg := addAliasCfg{}
276+
for _, opt := range opts {
277+
opt(&cfg)
278+
}
250279

251280
// We need to lock the manager for the whole duration of this method,
252281
// except for the very last part where we call the link updater. In
@@ -302,8 +331,9 @@ func (m *Manager) AddLocalAlias(alias, baseScid lnwire.ShortChannelID,
302331
// Update the aliasToBase and baseToSet maps.
303332
m.baseToSet[baseScid] = append(m.baseToSet[baseScid], alias)
304333

305-
// Only store the gossiper map if gossip is true.
306-
if gossip {
334+
// Only store the gossiper map if gossip is true, or if the caller
335+
// explicitly asked to store this reverse mapping.
336+
if gossip || cfg.baseLookup {
307337
m.aliasToBase[alias] = baseScid
308338
}
309339

@@ -342,7 +372,9 @@ func (m *Manager) GetAliases(
342372
}
343373

344374
// FindBaseSCID finds the base SCID for a given alias. This is used in the
345-
// gossiper to find the correct SCID to lookup in the graph database.
375+
// gossiper to find the correct SCID to lookup in the graph database. It can
376+
// also be used to look up the base for manual aliases that were added over the
377+
// RPC.
346378
func (m *Manager) FindBaseSCID(
347379
alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) {
348380

@@ -446,7 +478,7 @@ func (m *Manager) DeleteLocalAlias(alias,
446478
}
447479

448480
// Finally, we'll delete the aliasToBase mapping from the Manager's
449-
// cache (but this is only set if we gossip the alias).
481+
// cache.
450482
delete(m.aliasToBase, alias)
451483

452484
// We definitely need to unlock the Manager before calling the link

aliasmgr/aliasmgr_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,19 @@ func TestAliasLifecycle(t *testing.T) {
179179
require.Equal(t, StartingAlias, firstRequested)
180180

181181
// We now manually add the next alias from the range as a custom alias.
182+
// This time we also use the base lookup option, in order to be able to
183+
// go from alias back to the base scid.
182184
secondAlias := getNextScid(firstRequested)
183-
err = aliasStore.AddLocalAlias(secondAlias, baseScid, false, true)
185+
err = aliasStore.AddLocalAlias(
186+
secondAlias, baseScid, false, true, WithBaseLookup(),
187+
)
188+
require.NoError(t, err)
189+
190+
baseLookup, err := aliasStore.FindBaseSCID(secondAlias)
184191
require.NoError(t, err)
185192

193+
require.Equal(t, baseScid, baseLookup)
194+
186195
// When we now request another alias from the allocation list, we expect
187196
// the third one (tx position 2) to be returned.
188197
thirdRequested, err := aliasStore.RequestAlias()

funding/interfaces.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package funding
22

33
import (
4+
"github.com/lightningnetwork/lnd/aliasmgr"
45
"github.com/lightningnetwork/lnd/lnpeer"
56
"github.com/lightningnetwork/lnd/lnwire"
67
)
@@ -36,8 +37,8 @@ type aliasHandler interface {
3637
GetPeerAlias(lnwire.ChannelID) (lnwire.ShortChannelID, error)
3738

3839
// AddLocalAlias persists an alias to an underlying alias store.
39-
AddLocalAlias(lnwire.ShortChannelID, lnwire.ShortChannelID, bool,
40-
bool) error
40+
AddLocalAlias(lnwire.ShortChannelID, lnwire.ShortChannelID, bool, bool,
41+
...aliasmgr.AddLocalAliasOption) error
4142

4243
// GetAliases returns the set of aliases given the main SCID of a
4344
// channel. This SCID will be an alias for zero-conf channels and will

funding/manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/btcsuite/btcd/chaincfg/chainhash"
2222
"github.com/btcsuite/btcd/wire"
2323
"github.com/btcsuite/btcwallet/wallet"
24+
"github.com/lightningnetwork/lnd/aliasmgr"
2425
"github.com/lightningnetwork/lnd/chainntnfs"
2526
"github.com/lightningnetwork/lnd/chainreg"
2627
acpt "github.com/lightningnetwork/lnd/chanacceptor"
@@ -162,7 +163,8 @@ func (m *mockAliasMgr) GetPeerAlias(lnwire.ChannelID) (lnwire.ShortChannelID,
162163
}
163164

164165
func (m *mockAliasMgr) AddLocalAlias(lnwire.ShortChannelID,
165-
lnwire.ShortChannelID, bool, bool) error {
166+
lnwire.ShortChannelID, bool, bool,
167+
...aliasmgr.AddLocalAliasOption) error {
166168

167169
return nil
168170
}

lnrpc/routerrpc/router_server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,8 +1713,13 @@ func (s *Server) XAddLocalChanAliases(_ context.Context,
17131713
rpcAlias)
17141714
}
17151715

1716+
// We set the baseLookup flag as we want the alias
1717+
// manager to keep a mapping from the alias back to its
1718+
// base scid, in order to be able to provide it via the
1719+
// FindBaseLocalChanAlias RPC.
17161720
err = s.cfg.AliasMgr.AddLocalAlias(
17171721
aliasScid, baseScid, false, true,
1722+
aliasmgr.WithBaseLookup(),
17181723
)
17191724
if err != nil {
17201725
return nil, fmt.Errorf("error adding scid "+

peer/brontide.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/btcsuite/btcd/txscript"
2020
"github.com/btcsuite/btcd/wire"
2121
"github.com/btcsuite/btclog/v2"
22+
"github.com/lightningnetwork/lnd/aliasmgr"
2223
"github.com/lightningnetwork/lnd/brontide"
2324
"github.com/lightningnetwork/lnd/buffer"
2425
"github.com/lightningnetwork/lnd/chainntnfs"
@@ -404,8 +405,8 @@ type Config struct {
404405
RequestAlias func() (lnwire.ShortChannelID, error)
405406

406407
// AddLocalAlias persists an alias to an underlying alias store.
407-
AddLocalAlias func(alias, base lnwire.ShortChannelID,
408-
gossip, liveUpdate bool) error
408+
AddLocalAlias func(alias, base lnwire.ShortChannelID, gossip,
409+
liveUpdate bool, opts ...aliasmgr.AddLocalAliasOption) error
409410

410411
// AuxLeafStore is an optional store that can be used to store auxiliary
411412
// leaves for certain custom channel types.

0 commit comments

Comments
 (0)