mirror of
https://github.com/DeBrosOfficial/network.git
synced 2025-10-06 13:49:07 +00:00
Add Go debug configurations and bump version to 0.42.1
The debug configurations and version bump changes are simple enough to be captured in the subject line alone, so no body is needed.
This commit is contained in:
parent
6b2512a983
commit
9d7c094360
@ -27,5 +27,34 @@
|
|||||||
"GATEWAY_API_KEY": "ak_iGustrsFk9H8uXpwczCATe5U:default"
|
"GATEWAY_API_KEY": "ak_iGustrsFk9H8uXpwczCATe5U:default"
|
||||||
},
|
},
|
||||||
"args": ["-test.v"]
|
"args": ["-test.v"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"adapter": "Delve",
|
||||||
|
"label": "Gateway Go 8005 Port (Delve)",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "./cmd/gateway",
|
||||||
|
"env": {
|
||||||
|
"GATEWAY_ADDR": ":8005",
|
||||||
|
"GATEWAY_BOOTSTRAP_PEERS": "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWSHHwEY6cga3ng7tD1rzStAU58ogQXVMX3LZJ6Gqf6dee",
|
||||||
|
"GATEWAY_NAMESPACE": "default",
|
||||||
|
"GATEWAY_API_KEY": "ak_iGustrsFk9H8uXpwczCATe5U:default"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"adapter": "Delve",
|
||||||
|
"label": "Network CLI - peers (Delve)",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "./cmd/cli",
|
||||||
|
"args": ["peers"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"adapter": "Delve",
|
||||||
|
"label": "Network CLI - PubSub Subscribe (Delve)",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "./cmd/cli",
|
||||||
|
"args": ["pubsub", "subscribe", "monitoring"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
2
Makefile
2
Makefile
@ -21,7 +21,7 @@ test-e2e:
|
|||||||
|
|
||||||
.PHONY: build clean test run-node run-node2 run-node3 run-example deps tidy fmt vet lint clear-ports
|
.PHONY: build clean test run-node run-node2 run-node3 run-example deps tidy fmt vet lint clear-ports
|
||||||
|
|
||||||
VERSION := 0.42.0-beta
|
VERSION := 0.42.1-beta
|
||||||
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
||||||
DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
|
DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||||
LDFLAGS := -X 'main.version=$(VERSION)' -X 'main.commit=$(COMMIT)' -X 'main.date=$(DATE)'
|
LDFLAGS := -X 'main.version=$(VERSION)' -X 'main.commit=$(COMMIT)' -X 'main.date=$(DATE)'
|
||||||
|
@ -421,38 +421,25 @@ func handlePeerID() {
|
|||||||
func createClient() (client.NetworkClient, error) {
|
func createClient() (client.NetworkClient, error) {
|
||||||
config := client.DefaultClientConfig("network-cli")
|
config := client.DefaultClientConfig("network-cli")
|
||||||
|
|
||||||
// Check for existing credentials
|
// Check for existing credentials using enhanced authentication
|
||||||
creds, err := auth.GetValidCredentials()
|
creds, err := auth.GetValidEnhancedCredentials()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No valid credentials found, trigger authentication flow
|
// No valid credentials found, use the enhanced authentication flow
|
||||||
fmt.Printf("🔐 Authentication required for DeBros Network CLI\n")
|
|
||||||
fmt.Printf("💡 This will open your browser to authenticate with your wallet\n")
|
|
||||||
|
|
||||||
gatewayURL := auth.GetDefaultGatewayURL()
|
gatewayURL := auth.GetDefaultGatewayURL()
|
||||||
fmt.Printf("🌐 Gateway: %s\n\n", gatewayURL)
|
|
||||||
|
|
||||||
// Perform wallet authentication
|
newCreds, authErr := auth.GetOrPromptForCredentials(gatewayURL)
|
||||||
newCreds, authErr := auth.PerformWalletAuthentication(gatewayURL)
|
|
||||||
if authErr != nil {
|
if authErr != nil {
|
||||||
return nil, fmt.Errorf("authentication failed: %w", authErr)
|
return nil, fmt.Errorf("authentication failed: %w", authErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save credentials
|
|
||||||
if saveErr := auth.SaveCredentialsForDefaultGateway(newCreds); saveErr != nil {
|
|
||||||
fmt.Printf("⚠️ Warning: failed to save credentials: %v\n", saveErr)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("💾 Credentials saved to ~/.debros/credentials.json\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
creds = newCreds
|
creds = newCreds
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure client with API key
|
// Configure client with API key
|
||||||
config.APIKey = creds.APIKey
|
config.APIKey = creds.APIKey
|
||||||
|
|
||||||
// Update last used time
|
// Update last used time - the enhanced store handles saving automatically
|
||||||
creds.UpdateLastUsed()
|
creds.UpdateLastUsed()
|
||||||
auth.SaveCredentialsForDefaultGateway(creds) // Best effort save
|
|
||||||
|
|
||||||
networkClient, err := client.NewClient(config)
|
networkClient, err := client.NewClient(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
BIN
network-cli
Executable file
BIN
network-cli
Executable file
Binary file not shown.
@ -189,27 +189,6 @@ func HasValidCredentials() (bool, error) {
|
|||||||
return exists && creds.IsValid(), nil
|
return exists && creds.IsValid(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValidCredentials returns valid credentials for the default gateway
|
|
||||||
func GetValidCredentials() (*Credentials, error) {
|
|
||||||
store, err := LoadCredentials()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
gatewayURL := GetDefaultGatewayURL()
|
|
||||||
creds, exists := store.GetCredentialsForGateway(gatewayURL)
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
return nil, fmt.Errorf("no credentials found for gateway %s", gatewayURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !creds.IsValid() {
|
|
||||||
return nil, fmt.Errorf("credentials for gateway %s are expired or invalid", gatewayURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
return creds, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaveCredentialsForDefaultGateway saves credentials for the default gateway
|
// SaveCredentialsForDefaultGateway saves credentials for the default gateway
|
||||||
func SaveCredentialsForDefaultGateway(creds *Credentials) error {
|
func SaveCredentialsForDefaultGateway(creds *Credentials) error {
|
||||||
store, err := LoadCredentials()
|
store, err := LoadCredentials()
|
||||||
|
@ -32,7 +32,7 @@ const (
|
|||||||
AuthChoiceExit
|
AuthChoiceExit
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadEnhancedCredentials loads the enhanced credential store
|
// LoadEnhancedCredentials loads the enhanced credential store, with migration support from legacy v2.0 format
|
||||||
func LoadEnhancedCredentials() (*EnhancedCredentialStore, error) {
|
func LoadEnhancedCredentials() (*EnhancedCredentialStore, error) {
|
||||||
credPath, err := GetCredentialsPath()
|
credPath, err := GetCredentialsPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -52,39 +52,94 @@ func LoadEnhancedCredentials() (*EnhancedCredentialStore, error) {
|
|||||||
return nil, fmt.Errorf("failed to read credentials file: %w", err)
|
return nil, fmt.Errorf("failed to read credentials file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to parse as enhanced store first
|
// First, try to parse as the proper enhanced store
|
||||||
var enhancedStore EnhancedCredentialStore
|
var enhancedStore EnhancedCredentialStore
|
||||||
if err := json.Unmarshal(data, &enhancedStore); err == nil && enhancedStore.Version == "2.0" {
|
if err := json.Unmarshal(data, &enhancedStore); err == nil && enhancedStore.Version == "2.0" {
|
||||||
// Initialize maps if nil
|
// Check if it's already in the enhanced format (has credentials arrays)
|
||||||
if enhancedStore.Gateways == nil {
|
hasCredentialsArrays := true
|
||||||
enhancedStore.Gateways = make(map[string]*GatewayCredentials)
|
for _, gwCreds := range enhancedStore.Gateways {
|
||||||
|
if gwCreds == nil || gwCreds.Credentials == nil {
|
||||||
|
hasCredentialsArrays = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasCredentialsArrays {
|
||||||
|
// Already in enhanced format, just sanitize indices
|
||||||
|
if enhancedStore.Gateways == nil {
|
||||||
|
enhancedStore.Gateways = make(map[string]*GatewayCredentials)
|
||||||
|
}
|
||||||
|
for _, gw := range enhancedStore.Gateways {
|
||||||
|
if len(gw.Credentials) == 0 {
|
||||||
|
gw.DefaultIndex = 0
|
||||||
|
gw.LastUsedIndex = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if gw.DefaultIndex < 0 || gw.DefaultIndex >= len(gw.Credentials) {
|
||||||
|
gw.DefaultIndex = 0
|
||||||
|
}
|
||||||
|
if gw.LastUsedIndex < 0 || gw.LastUsedIndex >= len(gw.Credentials) {
|
||||||
|
gw.LastUsedIndex = gw.DefaultIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &enhancedStore, nil
|
||||||
}
|
}
|
||||||
return &enhancedStore, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to old format and migrate
|
// Parse as legacy v2.0 format (single credential per gateway) and migrate
|
||||||
var oldStore CredentialStore
|
var legacyStore struct {
|
||||||
if err := json.Unmarshal(data, &oldStore); err != nil {
|
Gateways map[string]*Credentials `json:"gateways"`
|
||||||
return nil, fmt.Errorf("failed to parse credentials file: %w", err)
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migrate old format to new
|
if err := json.Unmarshal(data, &legacyStore); err != nil {
|
||||||
enhancedStore = EnhancedCredentialStore{
|
return nil, fmt.Errorf("invalid credentials file format: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if legacyStore.Version != "2.0" {
|
||||||
|
return nil, fmt.Errorf("unsupported credentials version %q; expected \"2.0\"", legacyStore.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert legacy format to enhanced format
|
||||||
|
enhanced := &EnhancedCredentialStore{
|
||||||
Gateways: make(map[string]*GatewayCredentials),
|
Gateways: make(map[string]*GatewayCredentials),
|
||||||
Version: "2.0",
|
Version: "2.0",
|
||||||
}
|
}
|
||||||
|
|
||||||
for gatewayURL, creds := range oldStore.Gateways {
|
for gwURL, legacyCred := range legacyStore.Gateways {
|
||||||
if creds != nil {
|
if legacyCred == nil {
|
||||||
enhancedStore.Gateways[gatewayURL] = &GatewayCredentials{
|
// Create empty gateway entry
|
||||||
Credentials: []*Credentials{creds},
|
enhanced.Gateways[gwURL] = &GatewayCredentials{
|
||||||
|
Credentials: []*Credentials{},
|
||||||
|
DefaultIndex: 0,
|
||||||
|
LastUsedIndex: 0,
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add if it looks like a valid credential (has wallet or api key)
|
||||||
|
if legacyCred.Wallet != "" || legacyCred.APIKey != "" {
|
||||||
|
enhanced.Gateways[gwURL] = &GatewayCredentials{
|
||||||
|
Credentials: []*Credentials{legacyCred},
|
||||||
|
DefaultIndex: 0,
|
||||||
|
LastUsedIndex: 0,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create empty gateway entry
|
||||||
|
enhanced.Gateways[gwURL] = &GatewayCredentials{
|
||||||
|
Credentials: []*Credentials{},
|
||||||
DefaultIndex: 0,
|
DefaultIndex: 0,
|
||||||
LastUsedIndex: 0,
|
LastUsedIndex: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &enhancedStore, nil
|
// Auto-save the migrated format
|
||||||
|
if err := enhanced.Save(); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Warning: failed to save migrated credentials: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return enhanced, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save saves the enhanced credential store
|
// Save saves the enhanced credential store
|
||||||
|
@ -3,6 +3,7 @@ package node
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p/core/peer"
|
"github.com/libp2p/go-libp2p/core/peer"
|
||||||
@ -44,16 +45,37 @@ func logDetailedPeerInfo(n *Node, currentPeerCount int, peers []peer.ID) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCPUUsagePercent(n *Node, interval time.Duration) (uint64, error) {
|
||||||
|
before, err := cpu.Get()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
time.Sleep(interval)
|
||||||
|
after, err := cpu.Get()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
idle := float64(after.Idle - before.Idle)
|
||||||
|
total := float64(after.Total - before.Total)
|
||||||
|
if total == 0 {
|
||||||
|
return 0, errors.New("Failed to get CPU usage")
|
||||||
|
}
|
||||||
|
usagePercent := (1.0 - idle/total) * 100.0
|
||||||
|
return uint64(usagePercent), nil
|
||||||
|
}
|
||||||
|
|
||||||
func logSystemUsage(n *Node) (*memory.Stats, uint64) {
|
func logSystemUsage(n *Node) (*memory.Stats, uint64) {
|
||||||
mem, _ := memory.Get()
|
mem, _ := memory.Get()
|
||||||
cpuBefore, _ := cpu.Get()
|
|
||||||
time.Sleep(3 * time.Second)
|
totalCpu, err := GetCPUUsagePercent(n, 3*time.Second)
|
||||||
cpuAfter, _ := cpu.Get()
|
if err != nil {
|
||||||
totalCpu := cpuAfter.Total - cpuBefore.Total
|
n.logger.Error("Failed to get CPU usage", zap.Error(err))
|
||||||
|
return mem, 0
|
||||||
|
}
|
||||||
|
|
||||||
n.logger.Debug("Node CPU usage",
|
n.logger.Debug("Node CPU usage",
|
||||||
zap.Float64("cpu_usage", float64(totalCpu)),
|
zap.Float64("cpu_usage", float64(totalCpu)),
|
||||||
zap.Float64("memory_usage", float64(mem.Used)))
|
zap.Float64("memory_usage_percent", float64(mem.Used)/float64(mem.Total)*100))
|
||||||
|
|
||||||
return mem, totalCpu
|
return mem, totalCpu
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user