Skip to content

Commit e3a9299

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 a13eff8 commit e3a9299

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
@@ -63,6 +63,10 @@ type ScidAliasManager interface {
6363
// Manager's maps.
6464
DeleteLocalAlias(ctx context.Context, alias,
6565
baseScid lnwire.ShortChannelID) error
66+
67+
// FindBaseAlias finds the base channel ID for a given alias.
68+
FindBaseAlias(ctx context.Context,
69+
alias lnwire.ShortChannelID) (lnwire.ShortChannelID, error)
6670
}
6771

6872
type (
@@ -261,6 +265,7 @@ func (m *Manager) startSubsystems(ctx context.Context) error {
261265
HtlcInterceptor: m.cfg.HtlcInterceptor,
262266
HtlcSubscriber: m.cfg.HtlcSubscriber,
263267
AcceptHtlcEvents: m.acceptHtlcEvents,
268+
AliasManager: m.cfg.AliasManager,
264269
SpecifierChecker: m.AssetMatchesSpecifier,
265270
NoOpHTLCs: m.cfg.NoOpHTLCs,
266271
AuxChanNegotiator: m.cfg.AuxChanNegotiator,

rfq/order.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ type OrderHandlerCfg struct {
688688
// intercept and accept/reject HTLCs.
689689
HtlcInterceptor HtlcInterceptor
690690

691+
// AliasManager is the SCID alias manager. This component is used to add
692+
// and remove SCID aliases.
693+
AliasManager ScidAliasManager
694+
691695
// AcceptHtlcEvents is a channel that receives accepted HTLCs.
692696
AcceptHtlcEvents chan<- *AcceptHtlcEvent
693697

@@ -918,7 +922,7 @@ func (h *OrderHandler) subscribeHtlcs(ctx context.Context) error {
918922
func (h *OrderHandler) Start() error {
919923
var startErr error
920924
h.startOnce.Do(func() {
921-
// Start the main event loop in a separate goroutine.
925+
// Start the HTLC interceptor in a separate go routine.
922926
h.Wg.Add(1)
923927
go func() {
924928
defer h.Wg.Done()
@@ -932,6 +936,12 @@ func (h *OrderHandler) Start() error {
932936
"interception: %v", startErr)
933937
return
934938
}
939+
}()
940+
941+
// Start the main event loop in a separate go routine.
942+
h.Wg.Add(1)
943+
go func() {
944+
defer h.Wg.Done()
935945

936946
h.mainEventLoop()
937947
}()
@@ -968,8 +978,8 @@ func (h *OrderHandler) RegisterAssetSalePolicy(buyAccept rfqmsg.BuyAccept) {
968978
h.policies.Store(policy.AcceptedQuoteId.Scid(), policy)
969979
}
970980

971-
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with the
972-
// order handler. This function takes an incoming sell accept message as an
981+
// RegisterAssetPurchasePolicy generates and registers an asset buy policy with
982+
// the order handler. This function takes an incoming sell accept message as an
973983
// argument.
974984
func (h *OrderHandler) RegisterAssetPurchasePolicy(
975985
sellAccept rfqmsg.SellAccept) {
@@ -1112,9 +1122,41 @@ func (h *OrderHandler) cleanupStalePolicies() {
11121122

11131123
h.policies.ForEach(
11141124
func(scid SerialisedScid, policy Policy) error {
1115-
if policy.HasExpired() {
1116-
staleCounter++
1117-
h.policies.Delete(scid)
1125+
if !policy.HasExpired() {
1126+
return nil
1127+
}
1128+
1129+
staleCounter++
1130+
1131+
// Delete the local entry of this policy.
1132+
h.policies.Delete(scid)
1133+
1134+
ctx, cancel := h.WithCtxQuitCustomTimeout(
1135+
h.DefaultTimeout,
1136+
)
1137+
defer cancel()
1138+
1139+
aliasScid := lnwire.NewShortChanIDFromInt(
1140+
uint64(scid),
1141+
)
1142+
1143+
// Find the base SCID for the alias.
1144+
baseScid, err := h.cfg.AliasManager.FindBaseAlias(
1145+
ctx, aliasScid,
1146+
)
1147+
if err != nil {
1148+
log.Warnf("Error finding base SCID for alias "+
1149+
"%d: %v", scid, err)
1150+
return nil
1151+
}
1152+
1153+
// Delete the alias scid mapping on LND.
1154+
err = h.cfg.AliasManager.DeleteLocalAlias(
1155+
ctx, aliasScid, baseScid,
1156+
)
1157+
if err != nil {
1158+
log.Warnf("Error deleting SCID alias %d: %v",
1159+
scid, err)
11181160
}
11191161

11201162
return nil

0 commit comments

Comments
 (0)