Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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")
}
}

Expand Down
Loading