Skip to content

Commit ccdbc3b

Browse files
committed
multi: thread new config values through client
1 parent 8aeeaef commit ccdbc3b

File tree

6 files changed

+31
-19
lines changed

6 files changed

+31
-19
lines changed

client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ type Client struct {
7979

8080
// NewClient returns a new instance to initiate swaps with.
8181
func NewClient(dbDir string, serverAddress string, insecure bool,
82-
tlsPathServer string, lnd *lndclient.LndServices) (*Client, func(),
83-
error) {
82+
tlsPathServer string, lnd *lndclient.LndServices, maxLSATCost,
83+
maxLSATFee btcutil.Amount) (*Client, func(), error) {
8484

8585
store, err := loopdb.NewBoltSwapStore(dbDir, lnd.ChainParams)
8686
if err != nil {
@@ -93,6 +93,7 @@ func NewClient(dbDir string, serverAddress string, insecure bool,
9393

9494
swapServerClient, err := newSwapServerClient(
9595
serverAddress, insecure, tlsPathServer, lsatStore, lnd,
96+
maxLSATCost, maxLSATFee,
9697
)
9798
if err != nil {
9899
return nil, nil, err

loopd/daemon.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ func daemon(config *config, lisCfg *listenerCfg) error {
5656
log.Infof("Swap server address: %v", config.SwapServer)
5757

5858
// Create an instance of the loop client library.
59-
swapClient, cleanup, err := getClient(
60-
config.Network, config.SwapServer, config.Insecure,
61-
config.TLSPathSwapSrv, &lnd.LndServices,
62-
)
59+
swapClient, cleanup, err := getClient(config, &lnd.LndServices)
6360
if err != nil {
6461
return err
6562
}

loopd/utils.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import (
44
"os"
55
"path/filepath"
66

7+
"github.com/btcsuite/btcutil"
78
"github.com/lightninglabs/loop"
89
"github.com/lightninglabs/loop/lndclient"
910
)
1011

1112
// getClient returns an instance of the swap client.
12-
func getClient(network, swapServer string, insecure bool, tlsPathServer string,
13-
lnd *lndclient.LndServices) (*loop.Client, func(), error) {
13+
func getClient(config *config, lnd *lndclient.LndServices) (*loop.Client,
14+
func(), error) {
1415

15-
storeDir, err := getStoreDir(network)
16+
storeDir, err := getStoreDir(config.Network)
1617
if err != nil {
1718
return nil, nil, err
1819
}
1920

2021
swapClient, cleanUp, err := loop.NewClient(
21-
storeDir, swapServer, insecure, tlsPathServer, lnd,
22+
storeDir, config.SwapServer, config.Insecure,
23+
config.TLSPathSwapSrv, lnd, btcutil.Amount(config.MaxLSATCost),
24+
btcutil.Amount(config.MaxLSATFee),
2225
)
2326
if err != nil {
2427
return nil, nil, err

loopd/view.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ func view(config *config, lisCfg *listenerCfg) error {
2323
}
2424
defer lnd.Close()
2525

26-
swapClient, cleanup, err := getClient(
27-
config.Network, config.SwapServer, config.Insecure,
28-
config.TLSPathSwapSrv, &lnd.LndServices,
29-
)
26+
swapClient, cleanup, err := getClient(config, &lnd.LndServices)
3027
if err != nil {
3128
return err
3229
}

lsat/interceptor.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99
"time"
1010

11+
"github.com/btcsuite/btcutil"
1112
"github.com/lightninglabs/loop/lndclient"
1213
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
1314
"github.com/lightningnetwork/lnd/lnwire"
@@ -67,19 +68,24 @@ type Interceptor struct {
6768
lnd *lndclient.LndServices
6869
store Store
6970
callTimeout time.Duration
71+
maxCost btcutil.Amount
72+
maxFee btcutil.Amount
7073
lock sync.Mutex
7174
}
7275

7376
// NewInterceptor creates a new gRPC client interceptor that uses the provided
7477
// lnd connection to automatically acquire and pay for LSAT tokens, unless the
7578
// indicated store already contains a usable token.
7679
func NewInterceptor(lnd *lndclient.LndServices, store Store,
77-
rpcCallTimeout time.Duration) *Interceptor {
80+
rpcCallTimeout time.Duration, maxCost,
81+
maxFee btcutil.Amount) *Interceptor {
7882

7983
return &Interceptor{
8084
lnd: lnd,
8185
store: store,
8286
callTimeout: rpcCallTimeout,
87+
maxCost: maxCost,
88+
maxFee: maxFee,
8389
}
8490
}
8591

@@ -226,6 +232,14 @@ func (i *Interceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
226232
return nil, fmt.Errorf("unable to decode invoice: %v", err)
227233
}
228234

235+
// Check that the charged amount does not exceed our maximum cost.
236+
maxCostMsat := lnwire.NewMSatFromSatoshis(i.maxCost)
237+
if invoice.MilliSat != nil && *invoice.MilliSat > maxCostMsat {
238+
return nil, fmt.Errorf("cannot pay for LSAT automatically, "+
239+
"cost of %d msat exceeds configured max cost of %d "+
240+
"msat", *invoice.MilliSat, maxCostMsat)
241+
}
242+
229243
// Create and store the pending token so we can resume the payment in
230244
// case the payment is interrupted somehow.
231245
token, err := tokenFromChallenge(macBytes, invoice.PaymentHash)
@@ -242,7 +256,7 @@ func (i *Interceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
242256
payCtx, cancel := context.WithTimeout(ctx, PaymentTimeout)
243257
defer cancel()
244258
respChan := i.lnd.Client.PayInvoice(
245-
payCtx, invoiceStr, DefaultMaxRoutingFeeSats, nil,
259+
payCtx, invoiceStr, i.maxFee, nil,
246260
)
247261
select {
248262
case result := <-respChan:

swap_server_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ type grpcSwapServerClient struct {
5252
var _ swapServerClient = (*grpcSwapServerClient)(nil)
5353

5454
func newSwapServerClient(address string, insecure bool, tlsPath string,
55-
lsatStore lsat.Store, lnd *lndclient.LndServices) (
56-
*grpcSwapServerClient, error) {
55+
lsatStore lsat.Store, lnd *lndclient.LndServices,
56+
maxLSATCost, maxLSATFee btcutil.Amount) (*grpcSwapServerClient, error) {
5757

5858
// Create the server connection with the interceptor that will handle
5959
// the LSAT protocol for us.
6060
clientInterceptor := lsat.NewInterceptor(
61-
lnd, lsatStore, serverRPCTimeout,
61+
lnd, lsatStore, serverRPCTimeout, maxLSATCost, maxLSATFee,
6262
)
6363
serverConn, err := getSwapServerConn(
6464
address, insecure, tlsPath, clientInterceptor,

0 commit comments

Comments
 (0)