From 5b49f4c9d1ced62b3dee70e3d12d439f76d97c22 Mon Sep 17 00:00:00 2001 From: George Tsagkarelis Date: Wed, 6 Aug 2025 15:06:08 +0200 Subject: [PATCH] rfq+lndservices: remove scid alias of expired quotes We use the new LND RPC endpoint that looks up the base scid for an alias, in order to use it to delete the mapping shortly after. In addition we break the order handler main loop into it's go routine, which was previously never really running as it followed the HTLC interceptor setup which was a blocking call. --- lndservices/router_client.go | 7 +++++ rfq/manager.go | 5 ++++ rfq/order.go | 54 ++++++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lndservices/router_client.go b/lndservices/router_client.go index 572bf1445..e23eff0de 100644 --- a/lndservices/router_client.go +++ b/lndservices/router_client.go @@ -45,6 +45,13 @@ func (l *LndRouterClient) DeleteLocalAlias(ctx context.Context, alias, return l.lnd.Router.XDeleteLocalChanAlias(ctx, alias, baseScid) } +// FindBaseAlias finds the base channel ID for a given alias. +func (l *LndRouterClient) FindBaseAlias(ctx context.Context, + alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) { + + return l.lnd.Router.XFindBaseLocalChanAlias(ctx, alias) +} + // SubscribeHtlcEvents subscribes to a stream of events related to // HTLC updates. func (l *LndRouterClient) SubscribeHtlcEvents( diff --git a/rfq/manager.go b/rfq/manager.go index 66dba3005..812864afd 100644 --- a/rfq/manager.go +++ b/rfq/manager.go @@ -63,6 +63,10 @@ type ScidAliasManager interface { // Manager's maps. DeleteLocalAlias(ctx context.Context, alias, baseScid lnwire.ShortChannelID) error + + // FindBaseAlias finds the base channel ID for a given alias. + FindBaseAlias(ctx context.Context, + alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) } type ( @@ -260,6 +264,7 @@ func (m *Manager) startSubsystems(ctx context.Context) error { CleanupInterval: CacheCleanupInterval, HtlcInterceptor: m.cfg.HtlcInterceptor, HtlcSubscriber: m.cfg.HtlcSubscriber, + AliasManager: m.cfg.AliasManager, AcceptHtlcEvents: m.acceptHtlcEvents, SpecifierChecker: m.AssetMatchesSpecifier, NoOpHTLCs: m.cfg.NoOpHTLCs, diff --git a/rfq/order.go b/rfq/order.go index 270a0f735..78d0943a1 100644 --- a/rfq/order.go +++ b/rfq/order.go @@ -688,6 +688,10 @@ type OrderHandlerCfg struct { // intercept and accept/reject HTLCs. HtlcInterceptor HtlcInterceptor + // AliasManager is the SCID alias manager. This component is used to add + // and remove SCID aliases. + AliasManager ScidAliasManager + // AcceptHtlcEvents is a channel that receives accepted HTLCs. AcceptHtlcEvents chan<- *AcceptHtlcEvent @@ -918,7 +922,7 @@ func (h *OrderHandler) subscribeHtlcs(ctx context.Context) error { func (h *OrderHandler) Start() error { var startErr error h.startOnce.Do(func() { - // Start the main event loop in a separate goroutine. + // Start the HTLC interceptor in a separate go routine. h.Wg.Add(1) go func() { defer h.Wg.Done() @@ -932,6 +936,12 @@ func (h *OrderHandler) Start() error { "interception: %v", startErr) return } + }() + + // Start the main event loop in a separate go routine. + h.Wg.Add(1) + go func() { + defer h.Wg.Done() h.mainEventLoop() }() @@ -968,8 +978,8 @@ func (h *OrderHandler) RegisterAssetSalePolicy(buyAccept rfqmsg.BuyAccept) { h.policies.Store(policy.AcceptedQuoteId.Scid(), policy) } -// RegisterAssetPurchasePolicy generates and registers an asset buy policy with the -// order handler. This function takes an incoming sell accept message as an +// RegisterAssetPurchasePolicy generates and registers an asset buy policy with +// the order handler. This function takes an incoming sell accept message as an // argument. func (h *OrderHandler) RegisterAssetPurchasePolicy( sellAccept rfqmsg.SellAccept) { @@ -1112,9 +1122,41 @@ func (h *OrderHandler) cleanupStalePolicies() { h.policies.ForEach( func(scid SerialisedScid, policy Policy) error { - if policy.HasExpired() { - staleCounter++ - h.policies.Delete(scid) + if !policy.HasExpired() { + return nil + } + + staleCounter++ + + // Delete the local entry of this policy. + h.policies.Delete(scid) + + ctx, cancel := h.WithCtxQuitCustomTimeout( + h.DefaultTimeout, + ) + defer cancel() + + aliasScid := lnwire.NewShortChanIDFromInt( + uint64(scid), + ) + + // Find the base SCID for the alias. + baseScid, err := h.cfg.AliasManager.FindBaseAlias( + ctx, aliasScid, + ) + if err != nil { + log.Warnf("Error finding base SCID for alias "+ + "%d: %v", scid, err) + return nil + } + + // Delete the alias scid mapping on LND. + err = h.cfg.AliasManager.DeleteLocalAlias( + ctx, aliasScid, baseScid, + ) + if err != nil { + log.Warnf("Error deleting SCID alias %d: %v", + scid, err) } return nil