Skip to content

Commit 9b7aa74

Browse files
committed
loopd: quoting for static address loop-ins
1 parent 961e273 commit 9b7aa74

File tree

7 files changed

+105
-20
lines changed

7 files changed

+105
-20
lines changed

client.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,9 @@ func (s *Client) LoopIn(globalCtx context.Context,
646646
return swapInfo, nil
647647
}
648648

649-
// LoopInQuote takes an amount and returns a break down of estimated
650-
// costs for the client. Both the swap server and the on-chain fee estimator are
651-
// queried to get to build the quote response.
649+
// LoopInQuote takes an amount and returns a breakdown of estimated costs for
650+
// the client. Both the swap server and the on-chain fee estimator are queried
651+
// to get to build the quote response.
652652
func (s *Client) LoopInQuote(ctx context.Context,
653653
request *LoopInQuoteRequest) (*LoopInQuote, error) {
654654

@@ -694,7 +694,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
694694

695695
quote, err := s.Server.GetLoopInQuote(
696696
ctx, request.Amount, s.lndServices.NodePubkey, request.LastHop,
697-
request.RouteHints, request.Initiator,
697+
request.RouteHints, request.Initiator, request.NumDeposits,
698698
)
699699
if err != nil {
700700
return nil, err
@@ -704,7 +704,9 @@ func (s *Client) LoopInQuote(ctx context.Context,
704704

705705
// We don't calculate the on-chain fee if the HTLC is going to be
706706
// published externally.
707-
if request.ExternalHtlc {
707+
// We also don't calculate the on-chain fee if the loop in is funded by
708+
// static address deposits because we don't publish the HTLC on-chain.
709+
if request.ExternalHtlc || request.NumDeposits > 0 {
708710
return &LoopInQuote{
709711
SwapFee: swapFee,
710712
MinerFee: 0,

interface.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ type LoopInQuoteRequest struct {
288288
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
289289
// appended to the user agent string.
290290
Initiator string
291+
292+
// The number of static address deposits the client wants to quote for.
293+
// If the number of deposits exceeds one the server will apply a
294+
// per-input service fee. This is to cover for the increased on-chain
295+
// fee the server has to pay when the sweeping transaction is broadcast.
296+
NumDeposits uint32
291297
}
292298

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

loopd/swapclient_server.go

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,13 +748,53 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
748748

749749
log.Infof("Loop in quote request received")
750750

751+
var (
752+
numDeposits = uint32(len(req.DepositOutpoints))
753+
err error
754+
)
755+
751756
htlcConfTarget, err := validateLoopInRequest(
752-
req.ConfTarget, req.ExternalHtlc,
757+
req.ConfTarget, req.ExternalHtlc, numDeposits, req.Amt,
753758
)
754759
if err != nil {
755760
return nil, err
756761
}
757762

763+
// Retrieve deposits to calculate their total value.
764+
var summary *looprpc.StaticAddressSummaryResponse
765+
amount := btcutil.Amount(req.Amt)
766+
if len(req.DepositOutpoints) > 0 {
767+
summary, err = s.GetStaticAddressSummary(
768+
ctx, &looprpc.StaticAddressSummaryRequest{
769+
Outpoints: req.DepositOutpoints,
770+
},
771+
)
772+
if err != nil {
773+
return nil, err
774+
}
775+
776+
if summary == nil {
777+
return nil, fmt.Errorf("no summary returned for " +
778+
"deposit outpoints")
779+
}
780+
781+
// The requested amount should be 0 here if the request
782+
// contained deposit outpoints.
783+
if amount != 0 && len(summary.FilteredDeposits) > 0 {
784+
return nil, fmt.Errorf("amount should be 0 for " +
785+
"deposit quotes")
786+
}
787+
788+
// In case we quote for deposits we send the server both the
789+
// total value and the number of deposits. This is so the server
790+
// can probe the total amount and calculate the per input fee.
791+
if amount == 0 && len(summary.FilteredDeposits) > 0 {
792+
for _, deposit := range summary.FilteredDeposits {
793+
amount += btcutil.Amount(deposit.Value)
794+
}
795+
}
796+
}
797+
758798
var (
759799
routeHints [][]zpay32.HopHint
760800
lastHop *route.Vertex
@@ -778,13 +818,14 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
778818
}
779819

780820
quote, err := s.impl.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
781-
Amount: btcutil.Amount(req.Amt),
821+
Amount: amount,
782822
HtlcConfTarget: htlcConfTarget,
783823
ExternalHtlc: req.ExternalHtlc,
784824
LastHop: lastHop,
785825
RouteHints: routeHints,
786826
Private: req.Private,
787827
Initiator: defaultLoopdInitiator,
828+
NumDeposits: numDeposits,
788829
})
789830
if err != nil {
790831
return nil, err
@@ -881,7 +922,7 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
881922
log.Infof("Loop in request received")
882923

883924
htlcConfTarget, err := validateLoopInRequest(
884-
in.HtlcConfTarget, in.ExternalHtlc,
925+
in.HtlcConfTarget, in.ExternalHtlc, 0, in.Amt,
885926
)
886927
if err != nil {
887928
return nil, err
@@ -1725,7 +1766,13 @@ func validateConfTarget(target, defaultTarget int32) (int32, error) {
17251766

17261767
// validateLoopInRequest fails if the mutually exclusive conf target and
17271768
// external parameters are both set.
1728-
func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
1769+
func validateLoopInRequest(htlcConfTarget int32, external bool,
1770+
numDeposits uint32, amount int64) (int32, error) {
1771+
1772+
if amount == 0 && numDeposits == 0 {
1773+
return 0, errors.New("either amount or deposits must be set")
1774+
}
1775+
17291776
// If the htlc is going to be externally set, the htlcConfTarget should
17301777
// not be set, because it has no relevance when the htlc is external.
17311778
if external && htlcConfTarget != 0 {
@@ -1739,6 +1786,12 @@ func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
17391786
return 0, nil
17401787
}
17411788

1789+
// If the loop in uses static address deposits, we do not need to set a
1790+
// confirmation target since the HTLC won't be published by the client.
1791+
if numDeposits > 0 {
1792+
return 0, nil
1793+
}
1794+
17421795
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
17431796
}
17441797

loopd/swapclient_server_test.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,55 +146,76 @@ func TestValidateConfTarget(t *testing.T) {
146146
func TestValidateLoopInRequest(t *testing.T) {
147147
tests := []struct {
148148
name string
149+
amount int64
150+
numDeposits uint32
149151
external bool
150152
confTarget int32
151153
expectErr bool
152154
expectedTarget int32
153155
}{
154156
{
155157
name: "external and htlc conf set",
158+
amount: 100_000,
156159
external: true,
157160
confTarget: 1,
158161
expectErr: true,
159162
expectedTarget: 0,
160163
},
161164
{
162165
name: "external and no conf",
166+
amount: 100_000,
163167
external: true,
164168
confTarget: 0,
165169
expectErr: false,
166170
expectedTarget: 0,
167171
},
168172
{
169173
name: "not external, zero conf",
174+
amount: 100_000,
170175
external: false,
171176
confTarget: 0,
172177
expectErr: false,
173178
expectedTarget: loop.DefaultHtlcConfTarget,
174179
},
175180
{
176181
name: "not external, bad conf",
182+
amount: 100_000,
177183
external: false,
178184
confTarget: 1,
179185
expectErr: true,
180186
expectedTarget: 0,
181187
},
182188
{
183189
name: "not external, ok conf",
190+
amount: 100_000,
184191
external: false,
185192
confTarget: 5,
186193
expectErr: false,
187194
expectedTarget: 5,
188195
},
196+
{
197+
name: "not external, amount no deposit",
198+
amount: 100_000,
199+
numDeposits: 0,
200+
external: false,
201+
expectErr: false,
202+
expectedTarget: loop.DefaultHtlcConfTarget,
203+
},
204+
{
205+
name: "not external, deposit no amount",
206+
amount: 100_000,
207+
numDeposits: 1,
208+
external: false,
209+
expectErr: false,
210+
},
189211
}
190212

191213
for _, test := range tests {
192-
test := test
193-
194214
t.Run(test.name, func(t *testing.T) {
195215
external := test.external
196216
conf, err := validateLoopInRequest(
197-
test.confTarget, external,
217+
test.confTarget, external, test.numDeposits,
218+
test.amount,
198219
)
199220

200221
if test.expectErr {

loopin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
128128
// hints.
129129
quote, err := cfg.server.GetLoopInQuote(
130130
globalCtx, request.Amount, cfg.lnd.NodePubkey, request.LastHop,
131-
request.RouteHints, request.Initiator,
131+
request.RouteHints, request.Initiator, 0,
132132
)
133133
if err != nil {
134134
return nil, wrapGrpcError("loop in terms", err)

server_mock_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func (s *serverMock) GetLoopInTerms(ctx context.Context, initiator string) (
225225
}
226226

227227
func (s *serverMock) GetLoopInQuote(context.Context, btcutil.Amount,
228-
route.Vertex, *route.Vertex, [][]zpay32.HopHint, string) (*LoopInQuote, error) {
228+
route.Vertex, *route.Vertex, [][]zpay32.HopHint, string,
229+
uint32) (*LoopInQuote, error) {
229230

230231
return &LoopInQuote{
231232
SwapFee: testSwapFee,

swap_server_client.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type swapServerClient interface {
8282
GetLoopInQuote(ctx context.Context, amt btcutil.Amount,
8383
pubKey route.Vertex, lastHop *route.Vertex,
8484
routeHints [][]zpay32.HopHint,
85-
initiator string) (*LoopInQuote, error)
85+
initiator string, numDeposits uint32) (*LoopInQuote, error)
8686

8787
Probe(ctx context.Context, amt btcutil.Amount, target route.Vertex,
8888
lastHop *route.Vertex, routeHints [][]zpay32.HopHint) error
@@ -268,7 +268,8 @@ func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context,
268268

269269
func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
270270
amt btcutil.Amount, pubKey route.Vertex, lastHop *route.Vertex,
271-
routeHints [][]zpay32.HopHint, initiator string) (*LoopInQuote, error) {
271+
routeHints [][]zpay32.HopHint, initiator string,
272+
numDeposits uint32) (*LoopInQuote, error) {
272273

273274
err := s.Probe(ctx, amt, pubKey, lastHop, routeHints)
274275
if err != nil && status.Code(err) != codes.Unavailable {
@@ -279,10 +280,11 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
279280
defer rpcCancel()
280281

281282
req := &swapserverrpc.ServerLoopInQuoteRequest{
282-
Amt: uint64(amt),
283-
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
284-
Pubkey: pubKey[:],
285-
UserAgent: UserAgent(initiator),
283+
Amt: uint64(amt),
284+
ProtocolVersion: loopdb.CurrentRPCProtocolVersion(),
285+
Pubkey: pubKey[:],
286+
UserAgent: UserAgent(initiator),
287+
NumStaticAddressDeposits: numDeposits,
286288
}
287289

288290
if lastHop != nil {

0 commit comments

Comments
 (0)