@@ -2,9 +2,13 @@ package config
22
33import (
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
149190func 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}
0 commit comments