From cc12fb3204748b95e3746c54e2183d55788638ab Mon Sep 17 00:00:00 2001 From: George Tsagkarelis Date: Mon, 4 Mar 2024 21:46:46 +0100 Subject: [PATCH] aliasmgr: add delete local alias method --- aliasmgr/aliasmgr.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/aliasmgr/aliasmgr.go b/aliasmgr/aliasmgr.go index f33eb391e..737ce8219 100644 --- a/aliasmgr/aliasmgr.go +++ b/aliasmgr/aliasmgr.go @@ -340,6 +340,59 @@ func (m *Manager) DeleteSixConfs(baseScid lnwire.ShortChannelID) error { return nil } +// DeleteLocalAlias removes a mapping from the database and the Manager's maps. +func (m *Manager) DeleteLocalAlias(alias, + baseScid lnwire.ShortChannelID) error { + + m.Lock() + defer m.Unlock() + + err := kvdb.Update(m.backend, func(tx kvdb.RwTx) error { + aliasToBaseBucket, err := tx.CreateTopLevelBucket(aliasBucket) + if err != nil { + return err + } + + var aliasBytes [8]byte + byteOrder.PutUint64(aliasBytes[:], alias.ToUint64()) + + return aliasToBaseBucket.Delete(aliasBytes[:]) + }, func() {}) + if err != nil { + return err + } + + // Now that the database state has been updated, we'll delete the + // mapping from the Manager's maps. + aliasSet, ok := m.baseToSet[baseScid] + if !ok { + return nil + } + + // We'll iterate through the alias set and remove the alias from the + // set. + for i, a := range aliasSet { + if a.ToUint64() == alias.ToUint64() { + aliasSet = append(aliasSet[:i], aliasSet[i+1:]...) + break + } + } + + // If the alias set is empty, we'll delete the base SCID from the + // baseToSet map. + if len(aliasSet) == 0 { + delete(m.baseToSet, baseScid) + } else { + m.baseToSet[baseScid] = aliasSet + } + + // Finally, we'll delete the aliasToBase mapping from the Manager's + // cache. + delete(m.aliasToBase, alias) + + return nil +} + // PutPeerAlias stores the peer's alias SCID once we learn of it in the // channel_ready message. func (m *Manager) PutPeerAlias(chanID lnwire.ChannelID,