Skip to content

Commit 3550a6f

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 3550a6f

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

rfq/order.go

Lines changed: 19 additions & 25 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,9 +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 {
140+
func NewAssetSalePolicy(quote rfqmsg.BuyAccept, noop bool) *AssetSalePolicy {
146141

147142
htlcToAmtMap := make(map[models.CircuitKey]lnwire.MilliSatoshi)
148143

@@ -154,8 +149,7 @@ func NewAssetSalePolicy(quote rfqmsg.BuyAccept, noop bool,
154149
expiry: uint64(quote.AssetRate.Expiry.Unix()),
155150
htlcToAmt: htlcToAmtMap,
156151
NoOpHTLCs: noop,
157-
auxChanNegotiator: chanNegotiator,
158-
peer: peer,
152+
peer: quote.Peer,
159153
}
160154
}
161155

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

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

271266
outgoingAmt := rfqmath.DefaultOnChainHtlcMSat
272267

@@ -304,7 +299,7 @@ func (c *AssetSalePolicy) GenerateInterceptorResponse(
304299
fn.None[[]rfqmsg.ID](),
305300
)
306301

307-
peerFeatures := c.auxChanNegotiator.GetPeerFeatures(c.peer)
302+
peerFeatures := auxChanNegotiator.GetPeerFeatures(c.peer)
308303
supportNoOp := peerFeatures.HasFeature(tapfeatures.NoOpHTLCsOptional)
309304

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

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

516512
htlcRecord, err := parseHtlcCustomRecords(htlc.InWireCustomRecords)
517513
if err != nil {
@@ -639,19 +635,20 @@ func (a *AssetForwardPolicy) Scid() uint64 {
639635

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

645642
incomingResponse, err := a.incomingPolicy.GenerateInterceptorResponse(
646-
htlc,
643+
htlc, auxChanNegotiator,
647644
)
648645
if err != nil {
649646
return nil, fmt.Errorf("error generating incoming interceptor "+
650647
"response: %w", err)
651648
}
652649

653650
outgoingResponse, err := a.outgoingPolicy.GenerateInterceptorResponse(
654-
htlc,
651+
htlc, auxChanNegotiator,
655652
)
656653
if err != nil {
657654
return nil, fmt.Errorf("error generating outgoing interceptor "+
@@ -815,7 +812,7 @@ func (h *OrderHandler) handleIncomingHtlc(ctx context.Context,
815812
log.Debug("HTLC complies with policy. Broadcasting accept event.")
816813
h.cfg.AcceptHtlcEvents <- NewAcceptHtlcEvent(htlc, policy)
817814

818-
return policy.GenerateInterceptorResponse(htlc)
815+
return policy.GenerateInterceptorResponse(htlc, h.cfg.AuxChanNegotiator)
819816
}
820817

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

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

970964
h.policies.Store(policy.AcceptedQuoteId.Scid(), policy)
971965
}

0 commit comments

Comments
 (0)