Skip to content

Commit b2a31e8

Browse files
committed
rfq: separate AuxChanNegotiator, AssetSalePolicy
This refactor avoids storing a AuxChannelNegotiator reference in the AssetSalePolicy. Instead, it's gotten from the OrderHandler and passed as function arguments to where it's needed.
1 parent 74d98bf commit b2a31e8

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

rfq/order.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ type Policy interface {
8686
// GenerateInterceptorResponse generates an interceptor response for the
8787
// HTLC interceptor from the policy.
8888
GenerateInterceptorResponse(
89-
lndclient.InterceptedHtlc) (*lndclient.InterceptedHtlcResponse,
90-
error)
89+
lndclient.InterceptedHtlc,
90+
*tapfeatures.AuxChannelNegotiator,
91+
) (*lndclient.InterceptedHtlcResponse, error)
9192
}
9293

9394
// AssetSalePolicy is a struct that holds the terms which determine whether an
@@ -127,10 +128,6 @@ type AssetSalePolicy struct {
127128
// wants us to produce NoOp HTLCs.
128129
NoOpHTLCs bool
129130

130-
// auxChannelNegotiator is used to query the supported feature bits that
131-
// are supported by a peer, or a channel.
132-
auxChanNegotiator *tapfeatures.AuxChannelNegotiator
133-
134131
// peer is the peer pub key of the peer we established this policy with.
135132
peer route.Vertex
136133

@@ -140,10 +137,7 @@ type AssetSalePolicy struct {
140137
}
141138

142139
// NewAssetSalePolicy creates a new asset sale policy.
143-
func NewAssetSalePolicy(quote rfqmsg.BuyAccept, noop bool,
144-
chanNegotiator *tapfeatures.AuxChannelNegotiator,
145-
peer route.Vertex) *AssetSalePolicy {
146-
140+
func NewAssetSalePolicy(quote rfqmsg.BuyAccept, noop bool) *AssetSalePolicy {
147141
htlcToAmtMap := make(map[models.CircuitKey]lnwire.MilliSatoshi)
148142

149143
return &AssetSalePolicy{
@@ -154,8 +148,7 @@ func NewAssetSalePolicy(quote rfqmsg.BuyAccept, noop bool,
154148
expiry: uint64(quote.AssetRate.Expiry.Unix()),
155149
htlcToAmt: htlcToAmtMap,
156150
NoOpHTLCs: noop,
157-
auxChanNegotiator: chanNegotiator,
158-
peer: peer,
151+
peer: quote.Peer,
159152
}
160153
}
161154

@@ -265,8 +258,9 @@ func (c *AssetSalePolicy) Scid() uint64 {
265258

266259
// GenerateInterceptorResponse generates an interceptor response for the policy.
267260
func (c *AssetSalePolicy) GenerateInterceptorResponse(
268-
htlc lndclient.InterceptedHtlc) (*lndclient.InterceptedHtlcResponse,
269-
error) {
261+
htlc lndclient.InterceptedHtlc,
262+
auxChanNegotiator *tapfeatures.AuxChannelNegotiator,
263+
) (*lndclient.InterceptedHtlcResponse, error) {
270264

271265
outgoingAmt := rfqmath.DefaultOnChainHtlcMSat
272266

@@ -304,7 +298,7 @@ func (c *AssetSalePolicy) GenerateInterceptorResponse(
304298
fn.None[[]rfqmsg.ID](),
305299
)
306300

307-
peerFeatures := c.auxChanNegotiator.GetPeerFeatures(c.peer)
301+
peerFeatures := auxChanNegotiator.GetPeerFeatures(c.peer)
308302
supportNoOp := peerFeatures.HasFeature(tapfeatures.NoOpHTLCsOptional)
309303

310304
// We are about to create an outgoing HTLC that carries assets. Let's
@@ -510,8 +504,9 @@ func (c *AssetPurchasePolicy) Scid() uint64 {
510504

511505
// GenerateInterceptorResponse generates an interceptor response for the policy.
512506
func (c *AssetPurchasePolicy) GenerateInterceptorResponse(
513-
htlc lndclient.InterceptedHtlc) (*lndclient.InterceptedHtlcResponse,
514-
error) {
507+
htlc lndclient.InterceptedHtlc,
508+
_ *tapfeatures.AuxChannelNegotiator,
509+
) (*lndclient.InterceptedHtlcResponse, error) {
515510

516511
htlcRecord, err := parseHtlcCustomRecords(htlc.InWireCustomRecords)
517512
if err != nil {
@@ -639,19 +634,20 @@ func (a *AssetForwardPolicy) Scid() uint64 {
639634

640635
// GenerateInterceptorResponse generates an interceptor response for the policy.
641636
func (a *AssetForwardPolicy) GenerateInterceptorResponse(
642-
htlc lndclient.InterceptedHtlc) (*lndclient.InterceptedHtlcResponse,
643-
error) {
637+
htlc lndclient.InterceptedHtlc,
638+
auxChanNegotiator *tapfeatures.AuxChannelNegotiator,
639+
) (*lndclient.InterceptedHtlcResponse, error) {
644640

645641
incomingResponse, err := a.incomingPolicy.GenerateInterceptorResponse(
646-
htlc,
642+
htlc, auxChanNegotiator,
647643
)
648644
if err != nil {
649645
return nil, fmt.Errorf("error generating incoming interceptor "+
650646
"response: %w", err)
651647
}
652648

653649
outgoingResponse, err := a.outgoingPolicy.GenerateInterceptorResponse(
654-
htlc,
650+
htlc, auxChanNegotiator,
655651
)
656652
if err != nil {
657653
return nil, fmt.Errorf("error generating outgoing interceptor "+
@@ -815,7 +811,7 @@ func (h *OrderHandler) handleIncomingHtlc(ctx context.Context,
815811
log.Debug("HTLC complies with policy. Broadcasting accept event.")
816812
h.cfg.AcceptHtlcEvents <- NewAcceptHtlcEvent(htlc, policy)
817813

818-
return policy.GenerateInterceptorResponse(htlc)
814+
return policy.GenerateInterceptorResponse(htlc, h.cfg.AuxChanNegotiator)
819815
}
820816

821817
// setupHtlcIntercept sets up HTLC interception.
@@ -962,10 +958,7 @@ func (h *OrderHandler) RegisterAssetSalePolicy(buyAccept rfqmsg.BuyAccept) {
962958
log.Debugf("Order handler is registering an asset sale policy given a "+
963959
"buy accept message: %s", buyAccept.String())
964960

965-
policy := NewAssetSalePolicy(
966-
buyAccept, h.cfg.NoOpHTLCs, h.cfg.AuxChanNegotiator,
967-
buyAccept.Peer,
968-
)
961+
policy := NewAssetSalePolicy(buyAccept, h.cfg.NoOpHTLCs)
969962

970963
h.policies.Store(policy.AcceptedQuoteId.Scid(), policy)
971964
}

0 commit comments

Comments
 (0)