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.
This commit is contained in:
George Tsagkarelis committed 2025-09-25 11:15:27 +02:00
1 parent f293566849
commit 472a2f967e
6 files changed
+63 -13

No files matched your search

+39 -7
View File
@@ -239,14 +239,43 @@ func (m *Manager) populateMaps() error {
return nil return nil
} }
// addAliasCfg is a struct that hosts various options related to adding a local
// alias to the alias manager.
type addAliasCfg struct {
// baseLookup signals that the alias should also store a reverse look-up
// to the base scid.
baseLookup bool
}
// AddLocalAliasOption is a functional option that modifies the configuration
// for adding a local alias.
type AddLocalAliasOption func(cfg *addAliasCfg)
// WithBaseLookup is a functional option that controls whether a reverse lookup
// will be stored from the alias to the base scid.
func WithBaseLookup() AddLocalAliasOption {
return func(cfg *addAliasCfg) {
cfg.baseLookup = true
}
}
// AddLocalAlias adds a database mapping from the passed alias to the passed // AddLocalAlias adds a database mapping from the passed alias to the passed
// base SCID. The gossip boolean marks whether or not to create a mapping // base SCID. The gossip boolean marks whether or not to create a mapping
// that the gossiper will use. It is set to false for the upgrade path where // that the gossiper will use. It is set to false for the upgrade path where
// the feature-bit is toggled on and there are existing channels. The linkUpdate // the feature-bit is toggled on and there are existing channels. The linkUpdate
// flag is used to signal whether this function should also trigger an update // flag is used to signal whether this function should also trigger an update on
// on the htlcswitch scid alias maps. // the htlcswitch scid alias maps.
//
// NOTE: The following aliases will not be persisted (will be lost on restart):
// - Aliases that were created without gossip flag.
// - Aliases that correspond to confirmed channels.
func (m *Manager) AddLocalAlias(alias, baseScid lnwire.ShortChannelID, func (m *Manager) AddLocalAlias(alias, baseScid lnwire.ShortChannelID,
gossip, linkUpdate bool) error { gossip, linkUpdate bool, opts ...AddLocalAliasOption) error {
cfg := addAliasCfg{}
for _, opt := range opts {
opt(&cfg)
}
// We need to lock the manager for the whole duration of this method, // We need to lock the manager for the whole duration of this method,
// except for the very last part where we call the link updater. In // 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,
// Update the aliasToBase and baseToSet maps. // Update the aliasToBase and baseToSet maps.
m.baseToSet[baseScid] = append(m.baseToSet[baseScid], alias) m.baseToSet[baseScid] = append(m.baseToSet[baseScid], alias)
// Only store the gossiper map if gossip is true. // Only store the gossiper map if gossip is true, or if the caller
if gossip { // explicitly asked to store this reverse mapping.
if gossip || cfg.baseLookup {
m.aliasToBase[alias] = baseScid m.aliasToBase[alias] = baseScid
} }
@@ -342,7 +372,9 @@ func (m *Manager) GetAliases(
} }
// FindBaseSCID finds the base SCID for a given alias. This is used in the // FindBaseSCID finds the base SCID for a given alias. This is used in the
// gossiper to find the correct SCID to lookup in the graph database. // gossiper to find the correct SCID to lookup in the graph database. It can
// also be used to look up the base for manual aliases that were added over the
// RPC.
func (m *Manager) FindBaseSCID( func (m *Manager) FindBaseSCID(
alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) { alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) {
@@ -446,7 +478,7 @@ func (m *Manager) DeleteLocalAlias(alias,
} }
// Finally, we'll delete the aliasToBase mapping from the Manager's // Finally, we'll delete the aliasToBase mapping from the Manager's
// cache (but this is only set if we gossip the alias). // cache.
delete(m.aliasToBase, alias) delete(m.aliasToBase, alias)
// We definitely need to unlock the Manager before calling the link // We definitely need to unlock the Manager before calling the link
+10 -1
View File
@@ -179,10 +179,19 @@ func TestAliasLifecycle(t *testing.T) {
require.Equal(t, StartingAlias, firstRequested) require.Equal(t, StartingAlias, firstRequested)
// We now manually add the next alias from the range as a custom alias. // We now manually add the next alias from the range as a custom alias.
// This time we also use the base lookup option, in order to be able to
// go from alias back to the base scid.
secondAlias := getNextScid(firstRequested) secondAlias := getNextScid(firstRequested)
err = aliasStore.AddLocalAlias(secondAlias, baseScid, false, true) err = aliasStore.AddLocalAlias(
secondAlias, baseScid, false, true, WithBaseLookup(),
)
require.NoError(t, err) require.NoError(t, err)
baseLookup, err := aliasStore.FindBaseSCID(secondAlias)
require.NoError(t, err)
require.Equal(t, baseScid, baseLookup)
// When we now request another alias from the allocation list, we expect // When we now request another alias from the allocation list, we expect
// the third one (tx position 2) to be returned. // the third one (tx position 2) to be returned.
thirdRequested, err := aliasStore.RequestAlias() thirdRequested, err := aliasStore.RequestAlias()
+3 -2
View File
@@ -1,6 +1,7 @@
package funding package funding
import ( import (
"github.com/lightningnetwork/lnd/aliasmgr"
"github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@@ -36,8 +37,8 @@ type aliasHandler interface {
GetPeerAlias(lnwire.ChannelID) (lnwire.ShortChannelID, error) GetPeerAlias(lnwire.ChannelID) (lnwire.ShortChannelID, error)
// AddLocalAlias persists an alias to an underlying alias store. // AddLocalAlias persists an alias to an underlying alias store.
AddLocalAlias(lnwire.ShortChannelID, lnwire.ShortChannelID, bool, AddLocalAlias(lnwire.ShortChannelID, lnwire.ShortChannelID, bool, bool,
bool) error ...aliasmgr.AddLocalAliasOption) error
// GetAliases returns the set of aliases given the main SCID of a // GetAliases returns the set of aliases given the main SCID of a
// channel. This SCID will be an alias for zero-conf channels and will // channel. This SCID will be an alias for zero-conf channels and will
+3 -1
View File
@@ -21,6 +21,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet" "github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/aliasmgr"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainreg" "github.com/lightningnetwork/lnd/chainreg"
acpt "github.com/lightningnetwork/lnd/chanacceptor" acpt "github.com/lightningnetwork/lnd/chanacceptor"
@@ -162,7 +163,8 @@ func (m *mockAliasMgr) GetPeerAlias(lnwire.ChannelID) (lnwire.ShortChannelID,
} }
func (m *mockAliasMgr) AddLocalAlias(lnwire.ShortChannelID, func (m *mockAliasMgr) AddLocalAlias(lnwire.ShortChannelID,
lnwire.ShortChannelID, bool, bool) error { lnwire.ShortChannelID, bool, bool,
...aliasmgr.AddLocalAliasOption) error {
return nil return nil
} }
+5
View File
@@ -1713,8 +1713,13 @@ func (s *Server) XAddLocalChanAliases(_ context.Context,
rpcAlias) rpcAlias)
} }
// We set the baseLookup flag as we want the alias
// manager to keep a mapping from the alias back to its
// base scid, in order to be able to provide it via the
// FindBaseLocalChanAlias RPC.
err = s.cfg.AliasMgr.AddLocalAlias( err = s.cfg.AliasMgr.AddLocalAlias(
aliasScid, baseScid, false, true, aliasScid, baseScid, false, true,
aliasmgr.WithBaseLookup(),
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("error adding scid "+ return nil, fmt.Errorf("error adding scid "+
+3 -2
View File
@@ -19,6 +19,7 @@ import (
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog/v2" "github.com/btcsuite/btclog/v2"
"github.com/lightningnetwork/lnd/aliasmgr"
"github.com/lightningnetwork/lnd/brontide" "github.com/lightningnetwork/lnd/brontide"
"github.com/lightningnetwork/lnd/buffer" "github.com/lightningnetwork/lnd/buffer"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
@@ -404,8 +405,8 @@ type Config struct {
RequestAlias func() (lnwire.ShortChannelID, error) RequestAlias func() (lnwire.ShortChannelID, error)
// AddLocalAlias persists an alias to an underlying alias store. // AddLocalAlias persists an alias to an underlying alias store.
AddLocalAlias func(alias, base lnwire.ShortChannelID, AddLocalAlias func(alias, base lnwire.ShortChannelID, gossip,
gossip, liveUpdate bool) error liveUpdate bool, opts ...aliasmgr.AddLocalAliasOption) error
// AuxLeafStore is an optional store that can be used to store auxiliary // AuxLeafStore is an optional store that can be used to store auxiliary
// leaves for certain custom channel types. // leaves for certain custom channel types.