Skip to content

Commit 7e9414f

Browse files
committed
config: reject non-positive maxpeers
In this commit, we reject maxpeers values that cannot represent a usable peer budget. A zero value reached connmgr as TargetOutbound=0, where zero means to use the default target of eight. The server then rejected every completed peer and immediately created a replacement request. Rejecting the value during configuration avoids that outbound reconnect loop.
1 parent 58ee9ef commit 7e9414f

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ func minUint32(a, b uint32) uint32 {
9595
return b
9696
}
9797

98+
// validateMaxPeers ensures btcd has a positive total peer budget.
99+
func validateMaxPeers(maxPeers int) error {
100+
if maxPeers <= 0 {
101+
return fmt.Errorf("maxpeers must be greater than zero: %d", maxPeers)
102+
}
103+
104+
return nil
105+
}
106+
98107
// config defines the configuration options for btcd.
99108
//
100109
// See loadConfig for details on the configuration load process.
@@ -129,7 +138,7 @@ type config struct {
129138
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"`
130139
LogDir string `long:"logdir" description:"Directory to log output."`
131140
MaxOrphanTxs int `long:"maxorphantx" description:"Max number of orphan transactions to keep in memory"`
132-
MaxPeers int `long:"maxpeers" description:"Max number of inbound and outbound peers. Outbound slots for the configured peer mode are reserved before inbound capacity is calculated"`
141+
MaxPeers int `long:"maxpeers" description:"Max number of inbound and outbound peers. Must be greater than zero. Outbound slots for the configured peer mode are reserved before inbound capacity is calculated"`
133142
MiningAddrs []string `long:"miningaddr" description:"Add the specified payment address to the list of addresses to use for generated blocks -- At least one address is required if the generate option is set"`
134143
MinRelayTxFee float64 `long:"minrelaytxfee" description:"The minimum transaction fee in BTC/kB to be considered a non-zero fee."`
135144
DisableBanning bool `long:"nobanning" description:"Disable banning of misbehaving peers"`
@@ -615,6 +624,13 @@ func loadConfig() (*config, []string, error) {
615624
return nil, nil, err
616625
}
617626

627+
if err := validateMaxPeers(cfg.MaxPeers); err != nil {
628+
err := fmt.Errorf("%s: %w", funcName, err)
629+
fmt.Fprintln(os.Stderr, err)
630+
fmt.Fprintln(os.Stderr, usageMessage)
631+
return nil, nil, err
632+
}
633+
618634
// If mainnet is active, then we won't allow the stall handler to be
619635
// disabled.
620636
if activeNetParams.Params.Net == wire.MainNet && cfg.DisableStallHandler {

config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import (
88
"testing"
99
)
1010

11+
func TestValidateMaxPeers(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
maxPeers int
15+
wantErr bool
16+
}{
17+
{name: "negative", maxPeers: -1, wantErr: true},
18+
{name: "zero", wantErr: true},
19+
{name: "positive", maxPeers: 1},
20+
}
21+
22+
for _, test := range tests {
23+
t.Run(test.name, func(t *testing.T) {
24+
err := validateMaxPeers(test.maxPeers)
25+
if test.wantErr && err == nil {
26+
t.Fatal("expected validation error")
27+
}
28+
if !test.wantErr && err != nil {
29+
t.Fatalf("unexpected validation error: %v", err)
30+
}
31+
})
32+
}
33+
}
34+
1135
var (
1236
rpcuserRegexp = regexp.MustCompile("(?m)^rpcuser=.+$")
1337
rpcpassRegexp = regexp.MustCompile("(?m)^rpcpass=.+$")

sample-btcd.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
; connect=fe80::1
107107
; connect=[fe80::2]:8333
108108

109-
; Maximum number of inbound and outbound peers.
109+
; Maximum number of inbound and outbound peers. Must be greater than zero.
110110
; maxpeers=125
111111

112112
; Disable banning of misbehaving peers.

0 commit comments

Comments
 (0)