mirror of
https://github.com/luxfi/sdk.git
synced 2026-10-30 15:19:23 +00:00
Lux Partners Limited is not the entity behind this work. Every place the old name stood — copyright lines, site footers, brand config, legal and compliance text — now names the company that actually holds it. lux.fund is the exception and is not renamed to match: that site belongs to Lux Assets Management LLC, a different company, and it says so. Two kinds of line were left for a person rather than rewritten. A dated news item recording that Lux Partners Limited was established in the Isle of Man is a historical fact, and renaming it would falsify the record. And a sentence asserting that the company is domiciled in the Isle of Man is a claim about a jurisdiction, not a name — an Inc or an LLC is not an Isle of Man company, so swapping the name there would assert something untrue. Co-authored-by: Hanzo Dev <[email protected]>
278 lines
7.5 KiB
Go
278 lines
7.5 KiB
Go
// Copyright (C) 2020-2025, Lux Industries Inc All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
// Package network provides network configuration and management utilities.
|
|
package network
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
log "github.com/luxfi/log"
|
|
"github.com/luxfi/sdk/config"
|
|
"github.com/luxfi/sdk/netrunner"
|
|
)
|
|
|
|
// NetworkManager handles all network operations using netrunner
|
|
type NetworkManager struct {
|
|
config *config.NetworkConfig
|
|
logger log.Logger
|
|
networks map[string]*Network
|
|
// TODO: netrunnerClient will be used for network orchestration
|
|
_ *netrunner.Client
|
|
}
|
|
|
|
// Network represents a managed Lux network
|
|
type Network struct {
|
|
ID string
|
|
Name string
|
|
Type NetworkType
|
|
Status NetworkStatus
|
|
Nodes []*Node
|
|
ChainIDs []string
|
|
CreatedAt time.Time
|
|
// netrunner *netrunner.Network // TODO: Add netrunner integration
|
|
}
|
|
|
|
// Node represents a node in the network
|
|
type Node struct {
|
|
ID string
|
|
NodeID string
|
|
Type NodeType
|
|
Status NodeStatus
|
|
Endpoint string
|
|
StakeAmount uint64
|
|
PublicKey string
|
|
}
|
|
|
|
// NetworkType defines the type of network
|
|
type NetworkType string
|
|
|
|
const (
|
|
NetworkTypeMainnet NetworkType = "mainnet"
|
|
NetworkTypeTestnet NetworkType = "testnet"
|
|
NetworkTypeLocal NetworkType = "local"
|
|
NetworkTypeCustom NetworkType = "custom"
|
|
)
|
|
|
|
// NetworkStatus defines the status of a network
|
|
type NetworkStatus string
|
|
|
|
const (
|
|
NetworkStatusCreating NetworkStatus = "creating"
|
|
NetworkStatusRunning NetworkStatus = "running"
|
|
NetworkStatusStopped NetworkStatus = "stopped"
|
|
NetworkStatusError NetworkStatus = "error"
|
|
)
|
|
|
|
// NodeType defines the type of node
|
|
type NodeType string
|
|
|
|
const (
|
|
NodeTypeValidator NodeType = "validator"
|
|
NodeTypeAPI NodeType = "api"
|
|
NodeTypeFull NodeType = "full"
|
|
NodeTypeLight NodeType = "light"
|
|
)
|
|
|
|
// NodeStatus defines the status of a node
|
|
type NodeStatus string
|
|
|
|
const (
|
|
NodeStatusBootstrapping NodeStatus = "bootstrapping"
|
|
NodeStatusHealthy NodeStatus = "healthy"
|
|
NodeStatusUnhealthy NodeStatus = "unhealthy"
|
|
NodeStatusStopped NodeStatus = "stopped"
|
|
)
|
|
|
|
// NewNetworkManager creates a new network manager
|
|
func NewNetworkManager(config *config.NetworkConfig, logger log.Logger) (*NetworkManager, error) {
|
|
// Initialize network manager without netrunner client for now
|
|
// Netrunner integration will be added when the API is stabilized
|
|
manager := &NetworkManager{
|
|
config: config,
|
|
logger: logger,
|
|
networks: make(map[string]*Network),
|
|
}
|
|
|
|
// If netrunner endpoint is configured, log that it will be used in future
|
|
if config != nil && config.NetrunnerEndpoint != "" {
|
|
logger.Info("netrunner endpoint configured but not yet integrated",
|
|
"endpoint", config.NetrunnerEndpoint)
|
|
}
|
|
|
|
return manager, nil
|
|
}
|
|
|
|
// CreateNetwork creates a new network
|
|
func (nm *NetworkManager) CreateNetwork(ctx context.Context, params *NetworkParams) (*Network, error) {
|
|
nm.logger.Info("creating network", "name", params.Name, "type", params.Type)
|
|
|
|
// Validate network type
|
|
switch params.Type {
|
|
case NetworkTypeMainnet, NetworkTypeTestnet, NetworkTypeLocal, NetworkTypeCustom:
|
|
// Valid network type
|
|
default:
|
|
return nil, fmt.Errorf("unsupported network type: %s", params.Type)
|
|
}
|
|
|
|
// TODO: Implement actual network creation with netrunner
|
|
// For now, create a mock network
|
|
network := &Network{
|
|
ID: fmt.Sprintf("network-%d-%d", time.Now().UnixNano(), len(nm.networks)),
|
|
Name: params.Name,
|
|
Type: params.Type,
|
|
Status: NetworkStatusRunning,
|
|
Nodes: nm.createMockNodes(params.NumNodes),
|
|
ChainIDs: []string{"chain-1", "chain-2"},
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
nm.networks[network.ID] = network
|
|
return network, nil
|
|
}
|
|
|
|
// StartNetwork starts a stopped network
|
|
func (nm *NetworkManager) StartNetwork(ctx context.Context, networkID string) error {
|
|
network, ok := nm.networks[networkID]
|
|
if !ok {
|
|
return fmt.Errorf("network %s not found", networkID)
|
|
}
|
|
|
|
// TODO: Implement actual network start with netrunner
|
|
network.Status = NetworkStatusRunning
|
|
return nil
|
|
}
|
|
|
|
// StopNetwork stops a running network
|
|
func (nm *NetworkManager) StopNetwork(ctx context.Context, networkID string) error {
|
|
network, ok := nm.networks[networkID]
|
|
if !ok {
|
|
return fmt.Errorf("network %s not found", networkID)
|
|
}
|
|
|
|
// TODO: Implement actual network stop with netrunner
|
|
network.Status = NetworkStatusStopped
|
|
return nil
|
|
}
|
|
|
|
// DeleteNetwork deletes a network
|
|
func (nm *NetworkManager) DeleteNetwork(ctx context.Context, networkID string) error {
|
|
// TODO: Implement actual network deletion with netrunner
|
|
delete(nm.networks, networkID)
|
|
return nil
|
|
}
|
|
|
|
// GetNetwork returns a network by ID
|
|
func (nm *NetworkManager) GetNetwork(networkID string) (*Network, error) {
|
|
network, ok := nm.networks[networkID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("network %s not found", networkID)
|
|
}
|
|
return network, nil
|
|
}
|
|
|
|
// ListNetworks returns all networks
|
|
func (nm *NetworkManager) ListNetworks() []*Network {
|
|
networks := make([]*Network, 0, len(nm.networks))
|
|
for _, network := range nm.networks {
|
|
networks = append(networks, network)
|
|
}
|
|
return networks
|
|
}
|
|
|
|
// AddNode adds a new node to the network
|
|
func (nm *NetworkManager) AddNode(ctx context.Context, networkID string, nodeParams *NodeParams) (*Node, error) {
|
|
network, ok := nm.networks[networkID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("network %s not found", networkID)
|
|
}
|
|
|
|
// TODO: Implement actual node addition with netrunner
|
|
node := &Node{
|
|
ID: fmt.Sprintf("node-%d", time.Now().Unix()),
|
|
NodeID: fmt.Sprintf("NodeID-%d", time.Now().Unix()),
|
|
Type: nodeParams.Type,
|
|
Status: NodeStatusBootstrapping,
|
|
Endpoint: "http://127.0.0.1:9650",
|
|
StakeAmount: nodeParams.StakeAmount,
|
|
}
|
|
|
|
network.Nodes = append(network.Nodes, node)
|
|
return node, nil
|
|
}
|
|
|
|
// RemoveNode removes a node from the network
|
|
func (nm *NetworkManager) RemoveNode(ctx context.Context, networkID, nodeID string) error {
|
|
network, ok := nm.networks[networkID]
|
|
if !ok {
|
|
return fmt.Errorf("network %s not found", networkID)
|
|
}
|
|
|
|
// TODO: Implement actual node removal with netrunner
|
|
// Remove node from network
|
|
for i, node := range network.Nodes {
|
|
if node.ID == nodeID {
|
|
network.Nodes = append(network.Nodes[:i], network.Nodes[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetNodeStatus returns the status of a node
|
|
func (nm *NetworkManager) GetNodeStatus(ctx context.Context, networkID, nodeID string) (*NodeStatus, error) {
|
|
// TODO: Implement actual node status retrieval with netrunner
|
|
status := NodeStatusHealthy
|
|
return &status, nil
|
|
}
|
|
|
|
// createMockNodes creates mock nodes for testing
|
|
func (nm *NetworkManager) createMockNodes(numNodes int) []*Node {
|
|
nodes := make([]*Node, numNodes)
|
|
for i := 0; i < numNodes; i++ {
|
|
nodes[i] = &Node{
|
|
ID: fmt.Sprintf("node-%d", i),
|
|
NodeID: fmt.Sprintf("NodeID-%d", i),
|
|
Type: NodeTypeValidator,
|
|
Status: NodeStatusHealthy,
|
|
Endpoint: fmt.Sprintf("http://127.0.0.1:%d", 9650+i),
|
|
StakeAmount: 2000,
|
|
}
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
// NetworkParams defines parameters for creating a network
|
|
type NetworkParams struct {
|
|
Name string
|
|
Type NetworkType
|
|
NumNodes int
|
|
BinaryPath string
|
|
ConfigPath string
|
|
DataDir string
|
|
LogLevel string
|
|
HTTPPort int
|
|
StakingPort int
|
|
EnableStaking bool
|
|
EnableMonitoring bool
|
|
ChainConfigs []ChainConfig
|
|
}
|
|
|
|
// NodeParams defines parameters for adding a node
|
|
type NodeParams struct {
|
|
Name string
|
|
Type NodeType
|
|
StakeAmount uint64
|
|
}
|
|
|
|
// ChainConfig defines configuration for a chain
|
|
type ChainConfig struct {
|
|
ChainID string
|
|
VMType string
|
|
Genesis []byte
|
|
Config []byte
|
|
}
|