Skip to content

Commit b09b20c

Browse files
authored
Merge pull request #10133 from GeorgeTsagk/rpc-find-base-alias
Add `XFindBaseLocalChanAlias` RPC
2 parents f293566 + e7f7fe4 commit b09b20c

File tree

14 files changed

+669
-238
lines changed

14 files changed

+669
-238
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()

docs/release-notes/release-notes-0.20.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ a certain amount of msats.
6262
- Added support for [P2TR Fallback Addresses](
6363
https://github.com/lightningnetwork/lnd/pull/9975) in BOLT-11 invoices.
6464

65+
- A new experimental RPC endpoint
66+
[XFindBaseLocalChanAlias](https://github.com/lightningnetwork/lnd/pull/10133)
67+
was added for looking up the base scid for an scid alias. Aliases that were
68+
manually created via the `XAddLocalChanAliases` endpoint will get lost on
69+
restart.
70+
6571
## Functional Enhancements
6672
* [Add](https://github.com/lightningnetwork/lnd/pull/9677)
6773
`ConfirmationsUntilActive` and `ConfirmationHeight` field to the

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
}

0 commit comments

Comments
 (0)