Skip to content

Commit c3f3e7a

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 2806718 commit c3f3e7a

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 (
@@ -254,6 +258,7 @@ func (m *Manager) startSubsystems(ctx context.Context) error {
254258
CleanupInterval: CacheCleanupInterval,
255259
HtlcInterceptor: m.cfg.HtlcInterceptor,
256260
HtlcSubscriber: m.cfg.HtlcSubscriber,
261+
AliasManager: m.cfg.AliasManager,
257262
AcceptHtlcEvents: m.acceptHtlcEvents,
258263
SpecifierChecker: m.AssetMatchesSpecifier,
259264
NoOpHTLCs: m.cfg.NoOpHTLCs,

rfq/order.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,10 @@ type OrderHandlerCfg struct {
672672
// intercept and accept/reject HTLCs.
673673
HtlcInterceptor HtlcInterceptor
674674

675+
// AliasManager is the SCID alias manager. This component is used to add
676+
// and remove SCID aliases.
677+
AliasManager ScidAliasManager
678+
675679
// AcceptHtlcEvents is a channel that receives accepted HTLCs.
676680
AcceptHtlcEvents chan<- *AcceptHtlcEvent
677681

@@ -897,7 +901,7 @@ func (h *OrderHandler) subscribeHtlcs(ctx context.Context) error {
897901
func (h *OrderHandler) Start() error {
898902
var startErr error
899903
h.startOnce.Do(func() {
900-
// Start the main event loop in a separate goroutine.
904+
// Start the HTLC interceptor in a separate go routine.
901905
h.Wg.Add(1)
902906
go func() {
903907
defer h.Wg.Done()
@@ -911,6 +915,12 @@ func (h *OrderHandler) Start() error {
911915
"interception: %v", startErr)
912916
return
913917
}
918+
}()
919+
920+
// Start the main event loop in a separate go routine.
921+
h.Wg.Add(1)
922+
go func() {
923+
defer h.Wg.Done()
914924

915925
h.mainEventLoop()
916926
}()
@@ -944,8 +954,8 @@ func (h *OrderHandler) RegisterAssetSalePolicy(buyAccept rfqmsg.BuyAccept) {
944954
h.policies.Store(policy.AcceptedQuoteId.Scid(), policy)
945955
}
946956

947-
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with the
948-
// order handler. This function takes an incoming sell accept message as an
957+
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with
958+
// the order handler. This function takes an incoming sell accept message as an
949959
// argument.
950960
func (h *OrderHandler) RegisterAssetPurchasePolicy(
951961
sellAccept rfqmsg.SellAccept) {
@@ -1088,9 +1098,41 @@ func (h *OrderHandler) cleanupStalePolicies() {
10881098

10891099
h.policies.ForEach(
10901100
func(scid SerialisedScid, policy Policy) error {
1091-
if policy.HasExpired() {
1092-
staleCounter++
1093-
h.policies.Delete(scid)
1101+
if !policy.HasExpired() {
1102+
return nil
1103+
}
1104+
1105+
staleCounter++
1106+
1107+
// Delete the local entry of this policy.
1108+
h.policies.Delete(scid)
1109+
1110+
ctx, cancel := h.WithCtxQuitCustomTimeout(
1111+
h.DefaultTimeout,
1112+
)
1113+
defer cancel()
1114+
1115+
aliasScid := lnwire.NewShortChanIDFromInt(
1116+
uint64(scid),
1117+
)
1118+
1119+
// Find the base SCID for the alias.
1120+
baseScid, err := h.cfg.AliasManager.FindBaseAlias(
1121+
ctx, aliasScid,
1122+
)
1123+
if err != nil {
1124+
log.Warnf("Error finding base SCID for alias "+
1125+
"%d: %v", scid, err)
1126+
return nil
1127+
}
1128+
1129+
// Delete the alias scid mapping on LND.
1130+
err = h.cfg.AliasManager.DeleteLocalAlias(
1131+
ctx, aliasScid, baseScid,
1132+
)
1133+
if err != nil {
1134+
log.Warnf("Error deleting SCID alias %d: %v",
1135+
scid, err)
10941136
}
10951137

10961138
return nil

0 commit comments

Comments
 (0)