Skip to content

Commit f726fc2

Browse files
committed
loopd: add missing zero case to validateConfTarget
Update validate function to return default target for zero value confirmation targets, as indicated by the comment. This change introduces a behaviour change for direct rpc calls to loopd (ie, those not made by the loop cli tool). Previously, these calls would fail and indicate that the conf target must be > 2, now they will succeed with the default conf target. The loop cli tool is unaffected because we already set the default value. When clients upgrade from a previous version of loopd which did not have this check, preexisting loops will be unaffected, because loop ins had the default of 6 confirmations set, and loop outs with <2 conf target would not have been created.
1 parent 1f5aeff commit f726fc2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

loopd/swapclient_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ func (s *swapClientServer) processStatusUpdates(mainCtx context.Context) {
477477
// isn't specified (0 value), then the default target is used.
478478
func validateConfTarget(target, defaultTarget int32) (int32, error) {
479479
switch {
480+
case target == 0:
481+
return defaultTarget, nil
482+
480483
// Ensure the target respects our minimum threshold.
481484
case target < minConfTarget:
482485
return 0, fmt.Errorf("a confirmation target of at least %v "+

loopd/swapclient_server_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package loopd
2+
3+
import "testing"
4+
5+
// TestValidateConfTarget tests all failure and success cases for our conf
6+
// target validation function, including the case where we replace a zero
7+
// target with the default provided.
8+
func TestValidateConfTarget(t *testing.T) {
9+
const (
10+
// Various input confirmation values for tests.
11+
zeroConf int32 = 0
12+
oneConf int32 = 1
13+
twoConf int32 = 2
14+
fiveConf int32 = 5
15+
16+
// defaultConf is the default confirmation target we use for
17+
// all tests.
18+
defaultConf = 6
19+
)
20+
21+
tests := []struct {
22+
name string
23+
confTarget int32
24+
expectedTarget int32
25+
expectErr bool
26+
}{
27+
{
28+
name: "zero conf, get default",
29+
confTarget: zeroConf,
30+
expectedTarget: defaultConf,
31+
expectErr: false,
32+
},
33+
{
34+
name: "one conf, get error",
35+
confTarget: oneConf,
36+
expectErr: true,
37+
},
38+
{
39+
name: "two conf, ok",
40+
confTarget: twoConf,
41+
expectedTarget: twoConf,
42+
expectErr: false,
43+
},
44+
{
45+
name: "five conf, ok",
46+
confTarget: fiveConf,
47+
expectedTarget: fiveConf,
48+
expectErr: false,
49+
},
50+
}
51+
52+
for _, test := range tests {
53+
test := test
54+
55+
t.Run(test.name, func(t *testing.T) {
56+
target, err := validateConfTarget(
57+
test.confTarget, defaultConf,
58+
)
59+
60+
haveErr := err != nil
61+
if haveErr != test.expectErr {
62+
t.Fatalf("expected err: %v, got: %v",
63+
test.expectErr, err)
64+
}
65+
66+
if target != test.expectedTarget {
67+
t.Fatalf("expected: %v, got: %v",
68+
test.expectedTarget, target)
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)