Skip to content

Commit 921f4d0

Browse files
committed
multi: integrate initiator string to various calls
1 parent 1d47bef commit 921f4d0

File tree

9 files changed

+73
-35
lines changed

9 files changed

+73
-35
lines changed

client.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func (s *Client) LoopOut(globalCtx context.Context,
397397
}
398398

399399
// Calculate htlc expiry height.
400-
terms, err := s.Server.GetLoopOutTerms(globalCtx)
400+
terms, err := s.Server.GetLoopOutTerms(globalCtx, request.Initiator)
401401
if err != nil {
402402
return nil, err
403403
}
@@ -456,7 +456,7 @@ func (s *Client) getExpiry(height int32, terms *LoopOutTerms,
456456
func (s *Client) LoopOutQuote(ctx context.Context,
457457
request *LoopOutQuoteRequest) (*LoopOutQuote, error) {
458458

459-
terms, err := s.Server.GetLoopOutTerms(ctx)
459+
terms, err := s.Server.GetLoopOutTerms(ctx, request.Initiator)
460460
if err != nil {
461461
return nil, err
462462
}
@@ -477,6 +477,7 @@ func (s *Client) LoopOutQuote(ctx context.Context,
477477

478478
quote, err := s.Server.GetLoopOutQuote(
479479
ctx, request.Amount, expiry, request.SwapPublicationDeadline,
480+
request.Initiator,
480481
)
481482
if err != nil {
482483
return nil, err
@@ -528,10 +529,10 @@ func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
528529
}
529530

530531
// LoopOutTerms returns the terms on which the server executes swaps.
531-
func (s *Client) LoopOutTerms(ctx context.Context) (
532+
func (s *Client) LoopOutTerms(ctx context.Context, initiator string) (
532533
*LoopOutTerms, error) {
533534

534-
return s.Server.GetLoopOutTerms(ctx)
535+
return s.Server.GetLoopOutTerms(ctx, initiator)
535536
}
536537

537538
// waitForInitialized for swaps to be resumed and executor ready.
@@ -601,7 +602,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
601602
request *LoopInQuoteRequest) (*LoopInQuote, error) {
602603

603604
// Retrieve current server terms to calculate swap fee.
604-
terms, err := s.Server.GetLoopInTerms(ctx)
605+
terms, err := s.Server.GetLoopInTerms(ctx, request.Initiator)
605606
if err != nil {
606607
return nil, err
607608
}
@@ -641,7 +642,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
641642

642643
quote, err := s.Server.GetLoopInQuote(
643644
ctx, request.Amount, s.lndServices.NodePubkey, request.LastHop,
644-
request.RouteHints,
645+
request.RouteHints, request.Initiator,
645646
)
646647
if err != nil {
647648
return nil, err
@@ -721,10 +722,10 @@ func (s *Client) estimateFee(ctx context.Context, amt btcutil.Amount,
721722
}
722723

723724
// LoopInTerms returns the terms on which the server executes swaps.
724-
func (s *Client) LoopInTerms(ctx context.Context) (
725+
func (s *Client) LoopInTerms(ctx context.Context, initiator string) (
725726
*LoopInTerms, error) {
726727

727-
return s.Server.GetLoopInTerms(ctx)
728+
return s.Server.GetLoopInTerms(ctx, initiator)
728729
}
729730

730731
// wrapGrpcError wraps the non-nil error provided with a message providing

interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ type LoopOutQuoteRequest struct {
129129
// available routes and off-chain fee estimates. To apply these maximum
130130
// values properly, the server needs to be queried for its required
131131
// final cltv delta values for the off-chain payments.
132+
133+
// Initiator is an optional string that identifies what software
134+
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
135+
// appended to the user agent string.
136+
Initiator string
132137
}
133138

134139
// LoopOutTerms are the server terms on which it executes swaps.
@@ -267,6 +272,11 @@ type LoopInQuoteRequest struct {
267272
// private. In which case, loop will generate hophints to assist with
268273
// probing and payment.
269274
Private bool
275+
276+
// Initiator is an optional string that identifies what software
277+
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
278+
// appended to the user agent string.
279+
Initiator string
270280
}
271281

272282
// LoopInQuote contains estimates for the fees making up the total swap cost

liquidity/liquidity.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ type Config struct {
178178

179179
// Restrictions returns the restrictions that the server applies to
180180
// swaps.
181-
Restrictions func(ctx context.Context, swapType swap.Type) (
182-
*Restrictions, error)
181+
Restrictions func(ctx context.Context, swapType swap.Type,
182+
initiator string) (*Restrictions, error)
183183

184184
// Lnd provides us with access to lnd's rpc servers.
185185
Lnd *lndclient.LndServices
@@ -212,10 +212,12 @@ type Config struct {
212212
request *loop.LoopInRequest) (*loop.LoopInSwapInfo, error)
213213

214214
// LoopInTerms returns the terms for a loop in swap.
215-
LoopInTerms func(ctx context.Context) (*loop.LoopInTerms, error)
215+
LoopInTerms func(ctx context.Context,
216+
initiator string) (*loop.LoopInTerms, error)
216217

217218
// LoopOutTerms returns the terms for a loop out swap.
218-
LoopOutTerms func(ctx context.Context) (*loop.LoopOutTerms, error)
219+
LoopOutTerms func(ctx context.Context,
220+
initiator string) (*loop.LoopOutTerms, error)
219221

220222
// Clock allows easy mocking of time in unit tests.
221223
Clock clock.Clock
@@ -355,7 +357,9 @@ func (m *Manager) SetParameters(ctx context.Context,
355357
func (m *Manager) setParameters(ctx context.Context,
356358
params Parameters) error {
357359

358-
restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
360+
restrictions, err := m.cfg.Restrictions(
361+
ctx, swap.TypeOut, getInitiator(m.params),
362+
)
359363
if err != nil {
360364
return err
361365
}
@@ -566,7 +570,9 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {
566570
return nil
567571
}
568572

569-
restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
573+
restrictions, err := m.cfg.Restrictions(
574+
ctx, swap.TypeOut, getInitiator(m.params),
575+
)
570576
if err != nil {
571577
return err
572578
}
@@ -999,7 +1005,9 @@ func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
9991005
func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
10001006
*Restrictions, error) {
10011007

1002-
restrictions, err := m.cfg.Restrictions(ctx, swapType)
1008+
restrictions, err := m.cfg.Restrictions(
1009+
ctx, swapType, getInitiator(m.params),
1010+
)
10031011
if err != nil {
10041012
return nil, err
10051013
}
@@ -1487,6 +1495,14 @@ func (m *Manager) checkSummaryInflight(
14871495
return allowedSwaps, nil
14881496
}
14891497

1498+
func getInitiator(params Parameters) string {
1499+
if params.EasyAutoloop {
1500+
return "easy-autoloop"
1501+
}
1502+
1503+
return "autoloop"
1504+
}
1505+
14901506
// isAutoloopLabel is a helper function that returns a flag indicating whether
14911507
// the provided label corresponds to an autoloop swap.
14921508
func isAutoloopLabel(label string) bool {

liquidity/loopin_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
9090
Amount: amount,
9191
LastHop: &pubkey,
9292
HtlcConfTarget: params.HtlcConfTarget,
93+
Initiator: getInitiator(params),
9394
})
9495
if err != nil {
9596
// If the server fails our quote, we're not reachable right
@@ -113,7 +114,7 @@ func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
113114
MaxMinerFee: quote.MinerFee,
114115
HtlcConfTarget: params.HtlcConfTarget,
115116
LastHop: &pubkey,
116-
Initiator: autoloopSwapInitiator,
117+
Initiator: getInitiator(params),
117118
}
118119

119120
if params.Autoloop {

liquidity/loopout_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
101101
Amount: amount,
102102
SweepConfTarget: params.SweepConfTarget,
103103
SwapPublicationDeadline: b.cfg.Clock.Now(),
104+
Initiator: getInitiator(params),
104105
},
105106
)
106107
if err != nil {
@@ -144,7 +145,7 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
144145
MaxSwapFee: quote.SwapFee,
145146
MaxPrepayAmount: quote.PrepayAmount,
146147
SweepConfTarget: params.SweepConfTarget,
147-
Initiator: autoloopSwapInitiator,
148+
Initiator: getInitiator(params),
148149
}
149150

150151
if params.Autoloop {

loopd/swapclient_server.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
// to specify. This is driven by the minimum confirmation target allowed
3939
// by the backing fee estimator.
4040
minConfTarget = 2
41+
42+
defaultLoopdInitiator = "loopd"
4143
)
4244

4345
var (
@@ -511,7 +513,7 @@ func (s *swapClientServer) LoopOutTerms(ctx context.Context,
511513

512514
log.Infof("Loop out terms request received")
513515

514-
terms, err := s.impl.LoopOutTerms(ctx)
516+
terms, err := s.impl.LoopOutTerms(ctx, defaultLoopdInitiator)
515517
if err != nil {
516518
log.Errorf("Terms request: %v", err)
517519
return nil, err
@@ -542,6 +544,7 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
542544
SwapPublicationDeadline: time.Unix(
543545
int64(req.SwapPublicationDeadline), 0,
544546
),
547+
Initiator: defaultLoopdInitiator,
545548
})
546549
if err != nil {
547550
return nil, err
@@ -562,7 +565,7 @@ func (s *swapClientServer) GetLoopInTerms(ctx context.Context,
562565

563566
log.Infof("Loop in terms request received")
564567

565-
terms, err := s.impl.LoopInTerms(ctx)
568+
terms, err := s.impl.LoopInTerms(ctx, defaultLoopdInitiator)
566569
if err != nil {
567570
log.Errorf("Terms request: %v", err)
568571
return nil, err
@@ -616,6 +619,7 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
616619
LastHop: lastHop,
617620
RouteHints: routeHints,
618621
Private: req.Private,
622+
Initiator: defaultLoopdInitiator,
619623
})
620624
if err != nil {
621625
return nil, err

loopd/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
7575
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
7676
LoopOut: client.LoopOut,
7777
LoopIn: client.LoopIn,
78-
Restrictions: func(ctx context.Context,
79-
swapType swap.Type) (*liquidity.Restrictions, error) {
78+
Restrictions: func(ctx context.Context, swapType swap.Type,
79+
initiator string) (*liquidity.Restrictions, error) {
8080

8181
if swapType == swap.TypeOut {
82-
outTerms, err := client.Server.GetLoopOutTerms(ctx)
82+
outTerms, err := client.Server.GetLoopOutTerms(ctx, initiator)
8383
if err != nil {
8484
return nil, err
8585
}
@@ -89,7 +89,7 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
8989
), nil
9090
}
9191

92-
inTerms, err := client.Server.GetLoopInTerms(ctx)
92+
inTerms, err := client.Server.GetLoopInTerms(ctx, initiator)
9393
if err != nil {
9494
return nil, err
9595
}

loopin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
121121
// hints.
122122
quote, err := cfg.server.GetLoopInQuote(
123123
globalCtx, request.Amount, cfg.lnd.NodePubkey, request.LastHop,
124-
request.RouteHints,
124+
request.RouteHints, request.Initiator,
125125
)
126126
if err != nil {
127127
return nil, wrapGrpcError("loop in terms", err)

swap_server_client.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ func (r RoutingPluginType) String() string {
6868
}
6969

7070
type swapServerClient interface {
71-
GetLoopOutTerms(ctx context.Context) (
71+
GetLoopOutTerms(ctx context.Context, initiator string) (
7272
*LoopOutTerms, error)
7373

7474
GetLoopOutQuote(ctx context.Context, amt btcutil.Amount, expiry int32,
75-
swapPublicationDeadline time.Time) (
75+
swapPublicationDeadline time.Time, initiator string) (
7676
*LoopOutQuote, error)
7777

78-
GetLoopInTerms(ctx context.Context) (
78+
GetLoopInTerms(ctx context.Context, initiator string) (
7979
*LoopInTerms, error)
8080

8181
GetLoopInQuote(ctx context.Context, amt btcutil.Amount,
8282
pubKey route.Vertex, lastHop *route.Vertex,
83-
routeHints [][]zpay32.HopHint) (*LoopInQuote, error)
83+
routeHints [][]zpay32.HopHint,
84+
initiator string) (*LoopInQuote, error)
8485

8586
Probe(ctx context.Context, amt btcutil.Amount, target route.Vertex,
8687
lastHop *route.Vertex, routeHints [][]zpay32.HopHint) error
@@ -180,14 +181,15 @@ func newSwapServerClient(cfg *ClientConfig, lsatStore lsat.Store) (
180181
}, nil
181182
}
182183

183-
func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
184-
*LoopOutTerms, error) {
184+
func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context,
185+
initiator string) (*LoopOutTerms, error) {
185186

186187
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
187188
defer rpcCancel()
188189
terms, err := s.server.LoopOutTerms(rpcCtx,
189190
&looprpc.ServerLoopOutTermsRequest{
190191
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
192+
UserAgent: UserAgent(initiator),
191193
},
192194
)
193195
if err != nil {
@@ -203,8 +205,8 @@ func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
203205
}
204206

205207
func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
206-
amt btcutil.Amount, expiry int32, swapPublicationDeadline time.Time) (
207-
*LoopOutQuote, error) {
208+
amt btcutil.Amount, expiry int32, swapPublicationDeadline time.Time,
209+
initiator string) (*LoopOutQuote, error) {
208210

209211
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
210212
defer rpcCancel()
@@ -214,6 +216,7 @@ func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
214216
SwapPublicationDeadline: swapPublicationDeadline.Unix(),
215217
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
216218
Expiry: expiry,
219+
UserAgent: UserAgent(initiator),
217220
},
218221
)
219222
if err != nil {
@@ -237,14 +240,15 @@ func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
237240
}, nil
238241
}
239242

240-
func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
241-
*LoopInTerms, error) {
243+
func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context,
244+
initiator string) (*LoopInTerms, error) {
242245

243246
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
244247
defer rpcCancel()
245248
terms, err := s.server.LoopInTerms(rpcCtx,
246249
&looprpc.ServerLoopInTermsRequest{
247250
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
251+
UserAgent: UserAgent(initiator),
248252
},
249253
)
250254
if err != nil {
@@ -259,7 +263,7 @@ func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
259263

260264
func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
261265
amt btcutil.Amount, pubKey route.Vertex, lastHop *route.Vertex,
262-
routeHints [][]zpay32.HopHint) (*LoopInQuote, error) {
266+
routeHints [][]zpay32.HopHint, initiator string) (*LoopInQuote, error) {
263267

264268
err := s.Probe(ctx, amt, pubKey, lastHop, routeHints)
265269
if err != nil && status.Code(err) != codes.Unavailable {
@@ -273,6 +277,7 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
273277
Amt: uint64(amt),
274278
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
275279
Pubkey: pubKey[:],
280+
UserAgent: UserAgent(initiator),
276281
}
277282

278283
if lastHop != nil {

0 commit comments

Comments
 (0)