Skip to content

Commit 4e24d11

Browse files
authored
Merge pull request #77 from Chia-Network/retry-conns
Add retry to trusted peer connections
2 parents 0f40693 + 8c8bfd8 commit 4e24d11

File tree

3 files changed

+89
-29
lines changed

3 files changed

+89
-29
lines changed

cmd/config/addtrustedpeer.go

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package config
22

33
import (
44
"encoding/hex"
5+
"errors"
6+
"fmt"
57
"net"
8+
"os"
69
"path"
710
"strconv"
11+
"time"
812

913
"github.com/chia-network/go-chia-libs/pkg/config"
1014
"github.com/chia-network/go-chia-libs/pkg/peerprotocol"
@@ -76,19 +80,32 @@ chia-tools config add-trusted-peer node.chia.net 8444`,
7680
ips = append(ips, ip)
7781
}
7882

83+
var errs []error
7984
for _, ip := range ips {
80-
addTrustedPeer(cfg, chiaRoot, ip, port)
85+
err = addTrustedPeer(cfg, chiaRoot, ip, port)
86+
if err != nil {
87+
errs = append(errs, err)
88+
}
89+
}
90+
if len(errs) > 0 {
91+
for _, err := range errs {
92+
slogs.Logr.Error("error adding trusted peer", "error", err)
93+
}
94+
os.Exit(1)
8195
}
8296
},
8397
}
8498

85-
func addTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) {
86-
peerIDStr := getPeerID(cfg, chiaRoot, ip, port)
99+
func addTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) error {
100+
peerIDStr, err := getPeerID(cfg, chiaRoot, ip, port)
101+
if err != nil {
102+
return err
103+
}
87104
slogs.Logr.Info("peer id received", "peer", peerIDStr)
88105

89106
if !utils.ConfirmAction("Would you like trust this peer? (y/N)", skipConfirm) {
90107
slogs.Logr.Error("Cancelled")
91-
return
108+
return nil
92109
}
93110
cfg.Wallet.TrustedPeers[peerIDStr] = "Does_not_matter"
94111

@@ -108,45 +125,70 @@ func addTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uin
108125
cfg.Wallet.FullNodePeers = append(cfg.Wallet.FullNodePeers, peerToAdd)
109126
}
110127

111-
err := cfg.Save()
128+
err = cfg.Save()
112129
if err != nil {
113-
slogs.Logr.Fatal("error saving config", "error", err)
130+
return fmt.Errorf("error saving config: %w", err)
114131
}
115132

116133
slogs.Logr.Info("Added trusted peer. Restart your chia services for the configuration to take effect")
134+
return nil
117135
}
118136

119-
func getPeerID(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) string {
137+
func getPeerID(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) (string, error) {
120138
slogs.Logr.Info("Attempting to get peer id", "peer", ip.String(), "port", port)
121139

122140
keypair, err := cfg.FullNode.SSL.LoadPublicKeyPair(chiaRoot)
123141
if err != nil {
124-
slogs.Logr.Fatal("Error loading certs from CHIA_ROOT", "CHIA_ROOT", chiaRoot, "error", err)
142+
return "", fmt.Errorf("error loading certs from CHIA_ROOT: %w", err)
125143
}
126144
if keypair == nil {
127-
slogs.Logr.Fatal("Error loading certs from CHIA_ROOT", "CHIA_ROOT", chiaRoot, "error", "keypair was nil")
145+
return "", errors.New("error loading certs from CHIA_ROOT, keypair was nil")
128146
}
129147

130148
slogs.Logr.Debug("Attempting connection to peer")
131-
conn, err := peerprotocol.NewConnection(
132-
&ip,
133-
peerprotocol.WithPeerPort(port),
134-
peerprotocol.WithNetworkID(*cfg.SelectedNetwork),
135-
peerprotocol.WithPeerKeyPair(*keypair),
136-
)
137-
if err != nil {
138-
slogs.Logr.Fatal("Error creating connection", "error", err)
139-
}
149+
for i := uint(0); i <= retries; i++ {
150+
if i > 0 {
151+
slogs.Logr.Debug("Retrying connection attempt", "attempt", i+1, "max_attempts", retries+1, "sleep_seconds", i)
152+
time.Sleep(time.Duration(i) * time.Second)
153+
}
140154

141-
peerID, err := conn.PeerID()
142-
if err != nil {
143-
slogs.Logr.Fatal("Error getting peer id", "error", err)
155+
conn, err := peerprotocol.NewConnection(
156+
&ip,
157+
peerprotocol.WithPeerPort(port),
158+
peerprotocol.WithNetworkID(*cfg.SelectedNetwork),
159+
peerprotocol.WithPeerKeyPair(*keypair),
160+
peerprotocol.WithHandshakeTimeout(time.Second*3),
161+
)
162+
if err != nil {
163+
if i == retries {
164+
return "", fmt.Errorf("error creating connection to %s:%d after all retry attempts: %w", ip.String(), port, err)
165+
}
166+
continue
167+
}
168+
169+
peerID, err := conn.PeerID()
170+
if err != nil {
171+
if i == retries {
172+
return "", fmt.Errorf("error getting peer id for %s:%d after all retry attempts: %w", ip.String(), port, err)
173+
}
174+
continue
175+
}
176+
177+
slogs.Logr.Debug("Connection successful")
178+
peerIDStr := hex.EncodeToString(peerID[:])
179+
if peerIDStr == "" {
180+
return "", fmt.Errorf("peer id for %s:%d was empty after all retry attempts", ip.String(), port)
181+
}
182+
183+
return peerIDStr, nil
144184
}
145-
slogs.Logr.Debug("Connection successful")
146-
return hex.EncodeToString(peerID[:])
185+
186+
// This should never be reached due to the fatal errors above
187+
return "", fmt.Errorf("error connecting to peer after all retry attempts")
147188
}
148189

149190
func init() {
150191
addTrustedPeerCmd.Flags().BoolVarP(&skipConfirm, "yes", "y", false, "Skip confirmation")
192+
addTrustedPeerCmd.Flags().UintVarP(&retries, "retries", "r", 3, "Number of times to retry connecting to the peer")
151193
configCmd.AddCommand(addTrustedPeerCmd)
152194
}

cmd/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
var (
1111
skipConfirm bool
12+
retries uint
1213
)
1314

1415
// configCmd represents the config command

cmd/config/removetrustedpeer.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package config
22

33
import (
4+
"fmt"
45
"net"
6+
"os"
57
"path"
68
"strconv"
79

@@ -86,19 +88,32 @@ chia-tools config remove-trusted-peer --all`,
8688
ips = append(ips, ip)
8789
}
8890

91+
var errs []error
8992
for _, ip := range ips {
90-
removeTrustedPeer(cfg, chiaRoot, ip, port)
93+
err = removeTrustedPeer(cfg, chiaRoot, ip, port)
94+
if err != nil {
95+
errs = append(errs, err)
96+
}
97+
}
98+
if len(errs) > 0 {
99+
for _, err := range errs {
100+
slogs.Logr.Error("error adding trusted peer", "error", err)
101+
}
102+
os.Exit(1)
91103
}
92104
},
93105
}
94106

95-
func removeTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) {
96-
peerIDStr := getPeerID(cfg, chiaRoot, ip, port)
107+
func removeTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port uint16) error {
108+
peerIDStr, err := getPeerID(cfg, chiaRoot, ip, port)
109+
if err != nil {
110+
return err
111+
}
97112
slogs.Logr.Info("peer id received", "peer", peerIDStr)
98113

99114
if !utils.ConfirmAction("Would you like stop trusting this peer? (y/N)", skipConfirm) {
100115
slogs.Logr.Error("Cancelled")
101-
return
116+
return nil
102117
}
103118

104119
// Remove trusted peer
@@ -113,12 +128,13 @@ func removeTrustedPeer(cfg *config.ChiaConfig, chiaRoot string, ip net.IP, port
113128
}
114129
cfg.Wallet.FullNodePeers = fullNodePeers
115130

116-
err := cfg.Save()
131+
err = cfg.Save()
117132
if err != nil {
118-
slogs.Logr.Fatal("error saving config", "error", err)
133+
return fmt.Errorf("error saving config: %w", err)
119134
}
120135

121136
slogs.Logr.Info("Removed trusted peer. Restart your chia services for the configuration to take effect")
137+
return nil
122138
}
123139

124140
func removeAllTrustedPeers(cfg *config.ChiaConfig) {
@@ -149,5 +165,6 @@ func removeAllTrustedPeers(cfg *config.ChiaConfig) {
149165
func init() {
150166
removeTrustedPeerCmd.Flags().BoolVarP(&skipConfirm, "yes", "y", false, "Skip confirmation")
151167
removeTrustedPeerCmd.Flags().BoolVarP(&removeAll, "all", "a", false, "Remove all trusted peers from the config file")
168+
removeTrustedPeerCmd.Flags().UintVarP(&retries, "retries", "r", 3, "Number of times to retry connecting to the peer")
152169
configCmd.AddCommand(removeTrustedPeerCmd)
153170
}

0 commit comments

Comments
 (0)