Skip to content

Commit d81ce61

Browse files
committed
config: add sanity check to prevent non-negative worker counts
1 parent 76116f0 commit d81ce61

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,12 @@ func loadConfig() (*config, error) {
990990
"minbackoff")
991991
}
992992

993+
// Assert that all worker pools will have a positive number of
994+
// workers, otherwise the pools will rendered useless.
995+
if err := cfg.Workers.Validate(); err != nil {
996+
return nil, err
997+
}
998+
993999
// Finally, ensure that the user's color is correctly formatted,
9941000
// otherwise the server will not be able to start after the unlocking
9951001
// the wallet.

lncfg/workers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package lncfg
22

3+
import "fmt"
4+
35
const (
46
// DefaultReadWorkers is the default maximum number of concurrent
57
// workers used by the daemon's read pool.
@@ -26,3 +28,22 @@ type Workers struct {
2628
// Sig is the maximum number of concurrent sig pool workers.
2729
Sig int `long:"sig" description:"Maximum number of concurrent sig pool workers."`
2830
}
31+
32+
// Validate checks the Workers configuration to ensure that the input values are
33+
// sane.
34+
func (w *Workers) Validate() error {
35+
if w.Read <= 0 {
36+
return fmt.Errorf("number of read workers (%d) must be "+
37+
"positive", w.Read)
38+
}
39+
if w.Write <= 0 {
40+
return fmt.Errorf("number of write workers (%d) must be "+
41+
"positive", w.Write)
42+
}
43+
if w.Sig <= 0 {
44+
return fmt.Errorf("number of sig workers (%d) must be "+
45+
"positive", w.Sig)
46+
}
47+
48+
return nil
49+
}

lncfg/workers_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package lncfg_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lightningnetwork/lnd/lncfg"
7+
)
8+
9+
const (
10+
maxUint = ^uint(0)
11+
maxInt = int(maxUint >> 1)
12+
minInt = -maxInt - 1
13+
)
14+
15+
// TestValidateWorkers asserts that validating the Workers config only succeeds
16+
// if all fields specify a positive number of workers.
17+
func TestValidateWorkers(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
cfg *lncfg.Workers
21+
valid bool
22+
}{
23+
{
24+
name: "min valid",
25+
cfg: &lncfg.Workers{
26+
Read: 1,
27+
Write: 1,
28+
Sig: 1,
29+
},
30+
valid: true,
31+
},
32+
{
33+
name: "max valid",
34+
cfg: &lncfg.Workers{
35+
Read: maxInt,
36+
Write: maxInt,
37+
Sig: maxInt,
38+
},
39+
valid: true,
40+
},
41+
{
42+
name: "read max invalid",
43+
cfg: &lncfg.Workers{
44+
Read: 0,
45+
Write: 1,
46+
Sig: 1,
47+
},
48+
},
49+
{
50+
name: "write max invalid",
51+
cfg: &lncfg.Workers{
52+
Read: 1,
53+
Write: 0,
54+
Sig: 1,
55+
},
56+
},
57+
{
58+
name: "sig max invalid",
59+
cfg: &lncfg.Workers{
60+
Read: 1,
61+
Write: 1,
62+
Sig: 0,
63+
},
64+
},
65+
{
66+
name: "read min invalid",
67+
cfg: &lncfg.Workers{
68+
Read: minInt,
69+
Write: 1,
70+
Sig: 1,
71+
},
72+
},
73+
{
74+
name: "write min invalid",
75+
cfg: &lncfg.Workers{
76+
Read: 1,
77+
Write: minInt,
78+
Sig: 1,
79+
},
80+
},
81+
{
82+
name: "sig min invalid",
83+
cfg: &lncfg.Workers{
84+
Read: 1,
85+
Write: 1,
86+
Sig: minInt,
87+
},
88+
},
89+
}
90+
91+
for _, test := range tests {
92+
t.Run(test.name, func(t *testing.T) {
93+
err := test.cfg.Validate()
94+
switch {
95+
case test.valid && err != nil:
96+
t.Fatalf("valid config was invalid: %v", err)
97+
case !test.valid && err == nil:
98+
t.Fatalf("invalid config was valid")
99+
}
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)