Skip to content

Commit

Permalink
multi: integrate initiator string to various calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Jun 20, 2023
1 parent 055a44e commit 018071d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 35 deletions.
17 changes: 9 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (s *Client) LoopOut(globalCtx context.Context,
}

// Calculate htlc expiry height.
terms, err := s.Server.GetLoopOutTerms(globalCtx)
terms, err := s.Server.GetLoopOutTerms(globalCtx, request.Initiator)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -458,7 +458,7 @@ func (s *Client) getExpiry(height int32, terms *LoopOutTerms,
func (s *Client) LoopOutQuote(ctx context.Context,
request *LoopOutQuoteRequest) (*LoopOutQuote, error) {

terms, err := s.Server.GetLoopOutTerms(ctx)
terms, err := s.Server.GetLoopOutTerms(ctx, request.Initiator)
if err != nil {
return nil, err
}
Expand All @@ -479,6 +479,7 @@ func (s *Client) LoopOutQuote(ctx context.Context,

quote, err := s.Server.GetLoopOutQuote(
ctx, request.Amount, expiry, request.SwapPublicationDeadline,
request.Initiator,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -530,10 +531,10 @@ func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
}

// LoopOutTerms returns the terms on which the server executes swaps.
func (s *Client) LoopOutTerms(ctx context.Context) (
func (s *Client) LoopOutTerms(ctx context.Context, initiator string) (
*LoopOutTerms, error) {

return s.Server.GetLoopOutTerms(ctx)
return s.Server.GetLoopOutTerms(ctx, initiator)
}

// waitForInitialized for swaps to be resumed and executor ready.
Expand Down Expand Up @@ -603,7 +604,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
request *LoopInQuoteRequest) (*LoopInQuote, error) {

// Retrieve current server terms to calculate swap fee.
terms, err := s.Server.GetLoopInTerms(ctx)
terms, err := s.Server.GetLoopInTerms(ctx, request.Initiator)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -643,7 +644,7 @@ func (s *Client) LoopInQuote(ctx context.Context,

quote, err := s.Server.GetLoopInQuote(
ctx, request.Amount, s.lndServices.NodePubkey, request.LastHop,
request.RouteHints,
request.RouteHints, request.Initiator,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -723,10 +724,10 @@ func (s *Client) estimateFee(ctx context.Context, amt btcutil.Amount,
}

// LoopInTerms returns the terms on which the server executes swaps.
func (s *Client) LoopInTerms(ctx context.Context) (
func (s *Client) LoopInTerms(ctx context.Context, initiator string) (
*LoopInTerms, error) {

return s.Server.GetLoopInTerms(ctx)
return s.Server.GetLoopInTerms(ctx, initiator)
}

// wrapGrpcError wraps the non-nil error provided with a message providing
Expand Down
10 changes: 10 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ type LoopOutQuoteRequest struct {
// available routes and off-chain fee estimates. To apply these maximum
// values properly, the server needs to be queried for its required
// final cltv delta values for the off-chain payments.

// Initiator is an optional string that identifies what software
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
// appended to the user agent string.
Initiator string
}

// LoopOutTerms are the server terms on which it executes swaps.
Expand Down Expand Up @@ -267,6 +272,11 @@ type LoopInQuoteRequest struct {
// private. In which case, loop will generate hophints to assist with
// probing and payment.
Private bool

// Initiator is an optional string that identifies what software
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
// appended to the user agent string.
Initiator string
}

// LoopInQuote contains estimates for the fees making up the total swap cost
Expand Down
36 changes: 28 additions & 8 deletions liquidity/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const (

// DefaultAutoloopTicker is the default amount of time between automated
// swap checks.
DefaultAutoloopTicker = time.Minute * 20
DefaultAutoloopTicker = time.Second * 20

// autoloopSwapInitiator is the value we send in the initiator field of
// a swap request when issuing an automatic swap.
Expand Down Expand Up @@ -169,8 +169,8 @@ type Config struct {

// Restrictions returns the restrictions that the server applies to
// swaps.
Restrictions func(ctx context.Context, swapType swap.Type) (
*Restrictions, error)
Restrictions func(ctx context.Context, swapType swap.Type,
initiator string) (*Restrictions, error)

// Lnd provides us with access to lnd's rpc servers.
Lnd *lndclient.LndServices
Expand Down Expand Up @@ -203,10 +203,12 @@ type Config struct {
request *loop.LoopInRequest) (*loop.LoopInSwapInfo, error)

// LoopInTerms returns the terms for a loop in swap.
LoopInTerms func(ctx context.Context) (*loop.LoopInTerms, error)
LoopInTerms func(ctx context.Context,
initiator string) (*loop.LoopInTerms, error)

// LoopOutTerms returns the terms for a loop out swap.
LoopOutTerms func(ctx context.Context) (*loop.LoopOutTerms, error)
LoopOutTerms func(ctx context.Context,
initiator string) (*loop.LoopOutTerms, error)

// Clock allows easy mocking of time in unit tests.
Clock clock.Clock
Expand Down Expand Up @@ -346,7 +348,9 @@ func (m *Manager) SetParameters(ctx context.Context,
func (m *Manager) setParameters(ctx context.Context,
params Parameters) error {

restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
restrictions, err := m.cfg.Restrictions(
ctx, swap.TypeOut, getInitiator(m.params),
)
if err != nil {
return err
}
Expand Down Expand Up @@ -555,7 +559,9 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {
return nil
}

restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
restrictions, err := m.cfg.Restrictions(
ctx, swap.TypeOut, getInitiator(m.params),
)
if err != nil {
return err
}
Expand Down Expand Up @@ -988,7 +994,9 @@ func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
*Restrictions, error) {

restrictions, err := m.cfg.Restrictions(ctx, swapType)
restrictions, err := m.cfg.Restrictions(
ctx, swapType, getInitiator(m.params),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1476,6 +1484,18 @@ func (m *Manager) checkSummaryInflight(
return allowedSwaps, nil
}

func getInitiator(params Parameters) string {
if params.Autoloop {
if params.EasyAutoloop {
return "easy-autoloop"
}

return "autoloop"
}

return "unknown"
}

// isAutoloopLabel is a helper function that returns a flag indicating whether
// the provided label corresponds to an autoloop swap.
func isAutoloopLabel(label string) bool {
Expand Down
3 changes: 2 additions & 1 deletion liquidity/loopin_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
Amount: amount,
LastHop: &pubkey,
HtlcConfTarget: params.HtlcConfTarget,
Initiator: getInitiator(params),
})
if err != nil {
// If the server fails our quote, we're not reachable right
Expand All @@ -113,7 +114,7 @@ func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
MaxMinerFee: quote.MinerFee,
HtlcConfTarget: params.HtlcConfTarget,
LastHop: &pubkey,
Initiator: autoloopSwapInitiator,
Initiator: getInitiator(params),
}

if params.Autoloop {
Expand Down
1 change: 1 addition & 0 deletions liquidity/loopout_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
Amount: amount,
SweepConfTarget: params.SweepConfTarget,
SwapPublicationDeadline: b.cfg.Clock.Now(),
Initiator: getInitiator(params),
},
)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
// to specify. This is driven by the minimum confirmation target allowed
// by the backing fee estimator.
minConfTarget = 2

defaultLoopdInitiator = "loopd"
)

var (
Expand Down Expand Up @@ -445,7 +447,7 @@ func (s *swapClientServer) LoopOutTerms(ctx context.Context,

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

terms, err := s.impl.LoopOutTerms(ctx)
terms, err := s.impl.LoopOutTerms(ctx, defaultLoopdInitiator)
if err != nil {
log.Errorf("Terms request: %v", err)
return nil, err
Expand Down Expand Up @@ -476,6 +478,7 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
SwapPublicationDeadline: time.Unix(
int64(req.SwapPublicationDeadline), 0,
),
Initiator: defaultLoopdInitiator,
})
if err != nil {
return nil, err
Expand All @@ -496,7 +499,7 @@ func (s *swapClientServer) GetLoopInTerms(ctx context.Context,

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

terms, err := s.impl.LoopInTerms(ctx)
terms, err := s.impl.LoopInTerms(ctx, defaultLoopdInitiator)
if err != nil {
log.Errorf("Terms request: %v", err)
return nil, err
Expand Down Expand Up @@ -550,6 +553,7 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
LastHop: lastHop,
RouteHints: routeHints,
Private: req.Private,
Initiator: defaultLoopdInitiator,
})
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions loopd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
LoopOut: client.LoopOut,
LoopIn: client.LoopIn,
Restrictions: func(ctx context.Context,
swapType swap.Type) (*liquidity.Restrictions, error) {
Restrictions: func(ctx context.Context, swapType swap.Type,
initiator string) (*liquidity.Restrictions, error) {

if swapType == swap.TypeOut {
outTerms, err := client.Server.GetLoopOutTerms(ctx)
outTerms, err := client.Server.GetLoopOutTerms(ctx, initiator)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +56,7 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
), nil
}

inTerms, err := client.Server.GetLoopInTerms(ctx)
inTerms, err := client.Server.GetLoopInTerms(ctx, initiator)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion loopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
// route hints.
quote, err := cfg.server.GetLoopInQuote(
globalCtx, request.Amount, cfg.lnd.NodePubkey, request.LastHop,
request.RouteHints,
request.RouteHints, request.Initiator,
)
if err != nil {
return nil, wrapGrpcError("loop in terms", err)
Expand Down
27 changes: 16 additions & 11 deletions swap_server_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ func (r RoutingPluginType) String() string {
}

type swapServerClient interface {
GetLoopOutTerms(ctx context.Context) (
GetLoopOutTerms(ctx context.Context, initiator string) (
*LoopOutTerms, error)

GetLoopOutQuote(ctx context.Context, amt btcutil.Amount, expiry int32,
swapPublicationDeadline time.Time) (
swapPublicationDeadline time.Time, initiator string) (
*LoopOutQuote, error)

GetLoopInTerms(ctx context.Context) (
GetLoopInTerms(ctx context.Context, initiator string) (
*LoopInTerms, error)

GetLoopInQuote(ctx context.Context, amt btcutil.Amount,
pubKey route.Vertex, lastHop *route.Vertex,
routeHints [][]zpay32.HopHint) (*LoopInQuote, error)
routeHints [][]zpay32.HopHint,
initiator string) (*LoopInQuote, error)

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

func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
*LoopOutTerms, error) {
func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context,
initiator string) (*LoopOutTerms, error) {

rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
defer rpcCancel()
terms, err := s.server.LoopOutTerms(rpcCtx,
&looprpc.ServerLoopOutTermsRequest{
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
UserAgent: UserAgent(initiator),
},
)
if err != nil {
Expand All @@ -203,8 +205,8 @@ func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
}

func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
amt btcutil.Amount, expiry int32, swapPublicationDeadline time.Time) (
*LoopOutQuote, error) {
amt btcutil.Amount, expiry int32, swapPublicationDeadline time.Time,
initiator string) (*LoopOutQuote, error) {

rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
defer rpcCancel()
Expand All @@ -214,6 +216,7 @@ func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
SwapPublicationDeadline: swapPublicationDeadline.Unix(),
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
Expiry: expiry,
UserAgent: UserAgent(initiator),
},
)
if err != nil {
Expand All @@ -237,14 +240,15 @@ func (s *grpcSwapServerClient) GetLoopOutQuote(ctx context.Context,
}, nil
}

func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
*LoopInTerms, error) {
func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context,
initiator string) (*LoopInTerms, error) {

rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
defer rpcCancel()
terms, err := s.server.LoopInTerms(rpcCtx,
&looprpc.ServerLoopInTermsRequest{
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
UserAgent: UserAgent(initiator),
},
)
if err != nil {
Expand All @@ -259,7 +263,7 @@ func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (

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

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

if lastHop != nil {
Expand Down

0 comments on commit 018071d

Please sign in to comment.