@@ -87,18 +87,22 @@ type Manager struct {
8787 // negotiated option-scid-alias feature bit.
8888 aliasToBase map [lnwire.ShortChannelID ]lnwire.ShortChannelID
8989
90+ // peerAlias is a cache for the alias SCIDs that our peers send us in
91+ // the funding_locked TLV. The keys are the ChannelID generated from
92+ // the FundingOutpoint and the values are the remote peer's alias SCID.
93+ // The values should match the ones stored in the "invoice-alias-bucket"
94+ // bucket.
95+ peerAlias map [lnwire.ChannelID ]lnwire.ShortChannelID
96+
9097 sync.RWMutex
9198}
9299
93100// NewManager initializes an alias Manager from the passed database backend.
94101func NewManager (db kvdb.Backend ) (* Manager , error ) {
95102 m := & Manager {backend : db }
96- m .baseToSet = make (
97- map [lnwire.ShortChannelID ][]lnwire.ShortChannelID ,
98- )
99- m .aliasToBase = make (
100- map [lnwire.ShortChannelID ]lnwire.ShortChannelID ,
101- )
103+ m .baseToSet = make (map [lnwire.ShortChannelID ][]lnwire.ShortChannelID )
104+ m .aliasToBase = make (map [lnwire.ShortChannelID ]lnwire.ShortChannelID )
105+ m .peerAlias = make (map [lnwire.ChannelID ]lnwire.ShortChannelID )
102106
103107 err := m .populateMaps ()
104108 return m , err
@@ -115,6 +119,10 @@ func (m *Manager) populateMaps() error {
115119 // populate the Manager's actual maps.
116120 aliasMap := make (map [lnwire.ShortChannelID ]lnwire.ShortChannelID )
117121
122+ // This map caches the ChannelID/alias SCIDs stored in the database and
123+ // is used to populate the Manager's cache.
124+ peerAliasMap := make (map [lnwire.ChannelID ]lnwire.ShortChannelID )
125+
118126 err := kvdb .Update (m .backend , func (tx kvdb.RwTx ) error {
119127 baseConfBucket , err := tx .CreateTopLevelBucket (confirmedBucket )
120128 if err != nil {
@@ -152,12 +160,34 @@ func (m *Manager) populateMaps() error {
152160 aliasMap [aliasScid ] = baseScid
153161 return nil
154162 })
163+ if err != nil {
164+ return err
165+ }
166+
167+ invAliasBucket , err := tx .CreateTopLevelBucket (
168+ invoiceAliasBucket ,
169+ )
170+ if err != nil {
171+ return err
172+ }
173+
174+ err = invAliasBucket .ForEach (func (k , v []byte ) error {
175+ var chanID lnwire.ChannelID
176+ copy (chanID [:], k )
177+ alias := lnwire .NewShortChanIDFromInt (
178+ byteOrder .Uint64 (v ),
179+ )
180+
181+ peerAliasMap [chanID ] = alias
182+
183+ return nil
184+ })
185+
155186 return err
156187 }, func () {
157188 baseConfMap = make (map [lnwire.ShortChannelID ]struct {})
158- aliasMap = make (
159- map [lnwire.ShortChannelID ]lnwire.ShortChannelID ,
160- )
189+ aliasMap = make (map [lnwire.ShortChannelID ]lnwire.ShortChannelID )
190+ peerAliasMap = make (map [lnwire.ChannelID ]lnwire.ShortChannelID )
161191 })
162192 if err != nil {
163193 return err
@@ -176,6 +206,9 @@ func (m *Manager) populateMaps() error {
176206 m .aliasToBase [aliasSCID ] = baseSCID
177207 }
178208
209+ // Populate the peer alias cache.
210+ m .peerAlias = peerAliasMap
211+
179212 return nil
180213}
181214
@@ -242,7 +275,9 @@ func (m *Manager) AddLocalAlias(alias, baseScid lnwire.ShortChannelID,
242275
243276// GetAliases fetches the set of aliases stored under a given base SCID from
244277// write-through caches.
245- func (m * Manager ) GetAliases (base lnwire.ShortChannelID ) []lnwire.ShortChannelID {
278+ func (m * Manager ) GetAliases (
279+ base lnwire.ShortChannelID ) []lnwire.ShortChannelID {
280+
246281 m .RLock ()
247282 defer m .RUnlock ()
248283
@@ -310,7 +345,10 @@ func (m *Manager) DeleteSixConfs(baseScid lnwire.ShortChannelID) error {
310345func (m * Manager ) PutPeerAlias (chanID lnwire.ChannelID ,
311346 alias lnwire.ShortChannelID ) error {
312347
313- return kvdb .Update (m .backend , func (tx kvdb.RwTx ) error {
348+ m .Lock ()
349+ defer m .Unlock ()
350+
351+ err := kvdb .Update (m .backend , func (tx kvdb.RwTx ) error {
314352 bucket , err := tx .CreateTopLevelBucket (invoiceAliasBucket )
315353 if err != nil {
316354 return err
@@ -320,36 +358,30 @@ func (m *Manager) PutPeerAlias(chanID lnwire.ChannelID,
320358 byteOrder .PutUint64 (scratch [:], alias .ToUint64 ())
321359 return bucket .Put (chanID [:], scratch [:])
322360 }, func () {})
323- }
324-
325- // GetPeerAlias retrieves a peer's alias SCID by the channel's ChanID.
326- func (m * Manager ) GetPeerAlias (chanID lnwire.ChannelID ) (
327- lnwire.ShortChannelID , error ) {
361+ if err != nil {
362+ return err
363+ }
328364
329- var alias lnwire.ShortChannelID
365+ // Now that the database state has been updated, we can update it in
366+ // our cache.
367+ m .peerAlias [chanID ] = alias
330368
331- err := kvdb .Update (m .backend , func (tx kvdb.RwTx ) error {
332- bucket , err := tx .CreateTopLevelBucket (invoiceAliasBucket )
333- if err != nil {
334- return err
335- }
369+ return nil
370+ }
336371
337- aliasBytes := bucket .Get (chanID [:])
338- if aliasBytes == nil {
339- return nil
340- }
372+ // GetPeerAlias retrieves a peer's alias SCID by the channel's ChanID.
373+ func (m * Manager ) GetPeerAlias (chanID lnwire.ChannelID ) (lnwire.ShortChannelID ,
374+ error ) {
341375
342- alias = lnwire .NewShortChanIDFromInt (
343- byteOrder .Uint64 (aliasBytes ),
344- )
345- return nil
346- }, func () {})
376+ m .RLock ()
377+ defer m .RUnlock ()
347378
348- if alias == hop .Source {
349- return alias , errNoPeerAlias
379+ alias , ok := m .peerAlias [chanID ]
380+ if ! ok || alias == hop .Source {
381+ return lnwire.ShortChannelID {}, errNoPeerAlias
350382 }
351383
352- return alias , err
384+ return alias , nil
353385}
354386
355387// RequestAlias returns a new ALIAS ShortChannelID to the caller by allocating
0 commit comments