Skip to content

Commit 5c34dd1

Browse files
committed
loopd: refactor loop out request validation
This commit moves loop out request validation for labels and confirmation targets into its own function for the purpose of easy testing and also to make the additions of future request validation easy to add and test.
1 parent 9ce7fe4 commit 5c34dd1

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

loopd/swapclient_server.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const (
3434
minConfTarget = 2
3535
)
3636

37+
var (
38+
// errConfTargetTooLow is returned when the chosen confirmation target
39+
// is below the allowed minimum.
40+
errConfTargetTooLow = errors.New("confirmation target too low")
41+
)
42+
3743
// swapClientServer implements the grpc service exposed by loopd.
3844
type swapClientServer struct {
3945
network lndclient.Network
@@ -58,13 +64,6 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
5864

5965
log.Infof("Loop out request received")
6066

61-
sweepConfTarget, err := validateConfTarget(
62-
in.SweepConfTarget, loop.DefaultSweepConfTarget,
63-
)
64-
if err != nil {
65-
return nil, err
66-
}
67-
6867
var sweepAddr btcutil.Address
6968
if in.Dest == "" {
7069
// Generate sweep address if none specified.
@@ -83,8 +82,9 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
8382
}
8483
}
8584

86-
// Check that the label is valid.
87-
if err := labels.Validate(in.Label); err != nil {
85+
sweepConfTarget, err := validateLoopOutRequest(in.SweepConfTarget,
86+
in.Label)
87+
if err != nil {
8888
return nil, err
8989
}
9090

@@ -894,8 +894,9 @@ func validateConfTarget(target, defaultTarget int32) (int32, error) {
894894

895895
// Ensure the target respects our minimum threshold.
896896
case target < minConfTarget:
897-
return 0, fmt.Errorf("a confirmation target of at least %v "+
898-
"must be provided", minConfTarget)
897+
return 0, fmt.Errorf("%w: A confirmation target of at "+
898+
"least %v must be provided", errConfTargetTooLow,
899+
minConfTarget)
899900

900901
default:
901902
return target, nil
@@ -920,3 +921,14 @@ func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
920921

921922
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
922923
}
924+
925+
// validateLoopOutRequest validates the confirmation target and label of the
926+
// loop out request.
927+
func validateLoopOutRequest(confTarget int32, label string) (int32, error) {
928+
// Check that the label is valid.
929+
if err := labels.Validate(label); err != nil {
930+
return 0, err
931+
}
932+
933+
return validateConfTarget(confTarget, loop.DefaultSweepConfTarget)
934+
}

loopd/swapclient_server_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package loopd
22

33
import (
4+
"errors"
45
"testing"
56

7+
"github.com/btcsuite/btcd/chaincfg"
8+
"github.com/btcsuite/btcutil"
69
"github.com/lightninglabs/loop"
10+
"github.com/lightninglabs/loop/labels"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
var (
15+
testnetAddr, _ = btcutil.NewAddressScriptHash(
16+
[]byte{123}, &chaincfg.TestNet3Params,
17+
)
18+
19+
mainnetAddr, _ = btcutil.NewAddressScriptHash(
20+
[]byte{123}, &chaincfg.MainNetParams,
21+
)
722
)
823

924
// TestValidateConfTarget tests all failure and success cases for our conf
@@ -143,3 +158,50 @@ func TestValidateLoopInRequest(t *testing.T) {
143158
})
144159
}
145160
}
161+
162+
// TestValidateLoopOutRequest tests validation of loop out requests.
163+
func TestValidateLoopOutRequest(t *testing.T) {
164+
tests := []struct {
165+
name string
166+
confTarget int32
167+
label string
168+
err error
169+
expectedTarget int32
170+
}{
171+
{
172+
name: "invalid label",
173+
label: labels.Reserved,
174+
confTarget: 2,
175+
err: labels.ErrReservedPrefix,
176+
expectedTarget: 0,
177+
},
178+
{
179+
name: "invalid conf target",
180+
label: "label ok",
181+
confTarget: 1,
182+
err: errConfTargetTooLow,
183+
expectedTarget: 0,
184+
},
185+
{
186+
name: "default conf target",
187+
label: "label ok",
188+
confTarget: 0,
189+
err: nil,
190+
expectedTarget: 9,
191+
},
192+
}
193+
194+
for _, test := range tests {
195+
test := test
196+
197+
t.Run(test.name, func(t *testing.T) {
198+
t.Parallel()
199+
200+
conf, err := validateLoopOutRequest(
201+
test.confTarget, test.label,
202+
)
203+
require.True(t, errors.Is(err, test.err))
204+
require.Equal(t, test.expectedTarget, conf)
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)