Skip to content

Commit cc99a90

Browse files
committed
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.
1 parent 67f7a7c commit cc99a90

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

lndservices/router_client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ func (l *LndRouterClient) DeleteLocalAlias(ctx context.Context, alias,
4545
return l.lnd.Router.XDeleteLocalChanAlias(ctx, alias, baseScid)
4646
}
4747

48+
// FindBaseAlias finds the base channel ID for a given alias.
49+
func (l *LndRouterClient) FindBaseAlias(ctx context.Context,
50+
alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error) {
51+
52+
return l.lnd.Router.XFindBaseLocalChanAlias(ctx, alias)
53+
}
54+
4855
// SubscribeHtlcEvents subscribes to a stream of events related to
4956
// HTLC updates.
5057
func (l *LndRouterClient) SubscribeHtlcEvents(

rfq/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type ScidAliasManager interface {
6262
// Manager's maps.
6363
DeleteLocalAlias(ctx context.Context, alias,
6464
baseScid lnwire.ShortChannelID) error
65+
66+
// FindBaseAlias finds the base channel ID for a given alias.
67+
FindBaseAlias(ctx context.Context,
68+
alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error)
6569
}
6670

6771
type (
@@ -241,6 +245,7 @@ func (m *Manager) startSubsystems(ctx context.Context) error {
241245
CleanupInterval: CacheCleanupInterval,
242246
HtlcInterceptor: m.cfg.HtlcInterceptor,
243247
HtlcSubscriber: m.cfg.HtlcSubscriber,
248+
AliasManager: m.cfg.AliasManager,
244249
AcceptHtlcEvents: m.acceptHtlcEvents,
245250
SpecifierChecker: m.AssetMatchesSpecifier,
246251
})

rfq/order.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ type OrderHandlerCfg struct {
659659
// intercept and accept/reject HTLCs.
660660
HtlcInterceptor HtlcInterceptor
661661

662+
// AliasManager is the SCID alias manager. This component is used to add
663+
// and remove SCID aliases.
664+
AliasManager ScidAliasManager
665+
662666
// AcceptHtlcEvents is a channel that receives accepted HTLCs.
663667
AcceptHtlcEvents chan<- *AcceptHtlcEvent
664668

@@ -866,7 +870,7 @@ func (h *OrderHandler) subscribeHtlcs(ctx context.Context) error {
866870
func (h *OrderHandler) Start() error {
867871
var startErr error
868872
h.startOnce.Do(func() {
869-
// Start the main event loop in a separate goroutine.
873+
// Start the HTLC interceptor in a separate go routine.
870874
h.Wg.Add(1)
871875
go func() {
872876
defer h.Wg.Done()
@@ -880,6 +884,12 @@ func (h *OrderHandler) Start() error {
880884
"interception: %v", startErr)
881885
return
882886
}
887+
}()
888+
889+
// Start the main event loop in a separate go routine.
890+
h.Wg.Add(1)
891+
go func() {
892+
defer h.Wg.Done()
883893

884894
h.mainEventLoop()
885895
}()
@@ -913,8 +923,8 @@ func (h *OrderHandler) RegisterAssetSalePolicy(buyAccept rfqmsg.BuyAccept) {
913923
h.policies.Store(policy.AcceptedQuoteId.Scid(), policy)
914924
}
915925

916-
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with the
917-
// order handler. This function takes an incoming sell accept message as an
926+
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with
927+
// the order handler. This function takes an incoming sell accept message as an
918928
// argument.
919929
func (h *OrderHandler) RegisterAssetPurchasePolicy(
920930
sellAccept rfqmsg.SellAccept) {
@@ -1057,9 +1067,41 @@ func (h *OrderHandler) cleanupStalePolicies() {
10571067

10581068
h.policies.ForEach(
10591069
func(scid SerialisedScid, policy Policy) error {
1060-
if policy.HasExpired() {
1061-
staleCounter++
1062-
h.policies.Delete(scid)
1070+
if !policy.HasExpired() {
1071+
return nil
1072+
}
1073+
1074+
staleCounter++
1075+
1076+
// Delete the local entry of this policy.
1077+
h.policies.Delete(scid)
1078+
1079+
ctx, cancel := h.WithCtxQuitCustomTimeout(
1080+
h.DefaultTimeout,
1081+
)
1082+
defer cancel()
1083+
1084+
aliasScid := lnwire.NewShortChanIDFromInt(
1085+
uint64(scid),
1086+
)
1087+
1088+
// Find the base SCID for the alias.
1089+
baseScid, err := h.cfg.AliasManager.FindBaseAlias(
1090+
ctx, aliasScid,
1091+
)
1092+
if err != nil {
1093+
log.Warnf("Error finding base SCID for alias "+
1094+
"%d: %v", scid, err)
1095+
return nil
1096+
}
1097+
1098+
// Delete the alias scid mapping on LND.
1099+
err = h.cfg.AliasManager.DeleteLocalAlias(
1100+
ctx, aliasScid, baseScid,
1101+
)
1102+
if err != nil {
1103+
log.Warnf("Error deleting SCID alias %d: %v",
1104+
scid, err)
10631105
}
10641106

10651107
return nil

0 commit comments

Comments
 (0)