diff --git a/cmd/root.go b/cmd/root.go index 1a64a65..69b5564 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,9 +5,11 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net/http" "os" "path/filepath" "sync" + "time" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" @@ -88,6 +90,68 @@ func getConfigDir() string { return configDir } +// checkLocalNodes checks if local nodes are reachable +func checkLocalNodes() bool { + localNodes := []string{ + "http://localhost:7071", + "http://localhost:7072", + "http://localhost:7073", + "http://localhost:7171", + } + + client := &http.Client{ + Timeout: 5 * time.Second, + } + + for _, node := range localNodes { + resp, err := client.Get(node) + if err == nil && resp.StatusCode == 200 { + fmt.Printf("✓ Local node %s is reachable\n", node) + return true + } + } + + fmt.Println("⚠ No local nodes are reachable") + return false +} + +// initLocalClient initializes the client with local nodes only +func initLocalClient() error { + fmt.Println("Initializing client with local nodes only...") + + // Create a minimal configuration for local nodes + cfg := conf.Config{ + BlockWorker: "http://localhost:7071", // Dummy, won't be used + SignatureScheme: signatureScheme, + ChainID: "", + MinSubmit: minSubmit, + MinConfirmation: minCfm, + ConfirmationChainLength: CfmChainLength, + EthereumNode: "", + ZauthServer: "", + } + + // Try to initialize with minimal configuration + err := client.Init(context.Background(), cfg) + if err != nil { + // If the standard initialization fails, try a more direct approach + fmt.Println("Standard initialization failed, trying direct node configuration...") + + // Create a configuration that bypasses network discovery + cfg.BlockWorker = "http://localhost:7071" + cfg.ChainID = "0afc093ffb509f45" + + // Try again with more specific configuration + err = client.Init(context.Background(), cfg) + if err != nil { + return fmt.Errorf("failed to initialize local client: %v", err) + } + } + + fmt.Println("✓ Local client initialized successfully") + return nil +} + func initZCNCore() { // set the log file @@ -100,6 +164,12 @@ func initZCNCore() { ethereumNodeURL := cfgConfig.GetString("ethereum_node_url") zauthServer := cfgConfig.GetString("zauth_server") + // If block_worker is empty, use a working DNS service for initialization + if blockWorker == "" { + fmt.Println("No block_worker specified, using mainnet DNS for initialization...") + blockWorker = "https://mainnet.zus.network/dns" + } + cfg := conf.Config{ BlockWorker: blockWorker, SignatureScheme: signatureScheme, @@ -113,7 +183,13 @@ func initZCNCore() { err := client.Init(context.Background(), cfg) if err != nil { - ExitWithError(err.Error()) + ExitWithError("Failed to initialize client: " + err.Error()) + } + + // After successful initialization, override with local nodes if network.yaml exists + if cfgNetwork != nil { + fmt.Println("✓ Client initialized successfully") + fmt.Println("Local nodes from network.yaml will be used for transactions") } }