Skip to content

Commit 06ba35f

Browse files
authored
Refactor enviroment usage (#58)
* Remove unused Node Types and default certain configs to avoid repetition * Move to SDK usage * move direct usage of local env to client + public networks + tests * start network on tests * update readme * update thor * move env to internal * lint * pr comments * Initial refactor * refactor actions * moving envs to overseer * working overseer * overseer is now launch * update readme * Enabling custom genesis configs for hayabusa * update docker * pr comments
1 parent 9e34b16 commit 06ba35f

31 files changed

+1505
-915
lines changed

.github/README.md

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,44 @@ networkHub is a Go SDK framework designed to streamline the process of launching
88

99
## Quick Start
1010

11-
### **Launch Local Custom Network**:
11+
### **Launch Local Custom Network** (Simplest Way):
1212
```go
1313
package main
1414

1515
import (
1616
"log"
1717
"github.com/vechain/networkhub/client"
1818
"github.com/vechain/networkhub/preset"
19+
"github.com/vechain/networkhub/thorbuilder"
1920
)
2021

2122
func main() {
22-
// Create a local three-node network
23-
network := preset.LocalThreeMasterNodesNetwork()
23+
// Step 1: Use a preset network configuration (3 nodes local network)
24+
network := preset.LocalThreeNodesNetwork()
25+
26+
// Step 2: Configure thor builder for automatic binary management
27+
cfg := thorbuilder.DefaultConfig()
28+
network.ThorBuilder = cfg
2429

25-
// Set the thor binary path for all nodes
26-
for _, node := range network.Nodes {
27-
node.SetExecArtifact("/path/to/thor/binary")
30+
// Step 3: Create client and start network
31+
client, err := client.New(network)
32+
if err != nil {
33+
log.Fatal(err)
2834
}
35+
defer client.Stop()
2936

30-
// Create client and start network
31-
c, err := client.NewWithNetwork(network)
37+
// Step 4: Start the network
38+
err = client.Start()
3239
if err != nil {
3340
log.Fatal(err)
3441
}
35-
defer c.Stop()
3642

37-
log.Println("Network started successfully!")
38-
// Your network is ready for use
43+
log.Println("✅ 3-node VeChain network started successfully!")
44+
log.Printf("🌐 First node API: %s", network.Nodes[0].GetHTTPAddr())
45+
46+
// Your network is ready for use!
47+
// The thor binary is automatically downloaded and built
48+
// All nodes are configured with genesis, keys, and networking
3949
}
4050
```
4151

@@ -51,31 +61,88 @@ import (
5161

5262
func main() {
5363
// Connect to VeChain testnet (auto-starts)
54-
testnet, _ := preset.NewTestnetNetwork("dev")
55-
testnetClient, err := client.NewWithNetwork(testnet)
64+
testnet, err := preset.NewTestnetNetwork()
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
69+
testnetClient, err := client.New(testnet)
5670
if err != nil {
5771
log.Fatal(err)
5872
}
5973
defer testnetClient.Stop()
6074

61-
// Connect to VeChain mainnet (auto-starts)
62-
mainnet, _ := preset.NewMainnetNetwork("main")
63-
mainnetClient, err := client.NewWithNetwork(mainnet)
75+
// Connect to VeChain mainnet (auto-starts)
76+
mainnet, err := preset.NewMainnetNetwork()
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
81+
mainnetClient, err := client.New(mainnet)
6482
if err != nil {
6583
log.Fatal(err)
6684
}
6785
defer mainnetClient.Stop()
6886

69-
log.Println("Connected to public networks!")
87+
log.Println("✅ Connected to VeChain public networks!")
88+
// Networks auto-start when connecting to public networks
7089
}
7190
```
7291

92+
## Key Features
93+
94+
- **🚀 Simple API**: Get a VeChain network running in just 4 lines of code
95+
- **🔧 Automatic Thor Management**: Thor binary is automatically downloaded, built, and configured
96+
- **🌐 Multiple Environments**: Support for both Local and Docker environments
97+
- **📦 Built-in Presets**: Pre-configured networks for common use cases
98+
- **🏗️ Custom Networks**: Full control over genesis, nodes, and network parameters
99+
- **🔗 Public Network Support**: Easy connection to VeChain mainnet and testnet
100+
- **⚙️ Node Management**: Dynamically add and remove nodes from running networks
101+
- **🏥 Health Monitoring**: Built-in network health checks and validation
102+
- **🐳 Docker Support**: Run networks in Docker containers with proper networking
103+
- **🔑 Key Management**: Automatic private key and genesis configuration
104+
73105
## Purpose and Scope
74106
networkHub enables teams to quickly deploy custom networks and connect to public VeChain networks, facilitating development and testing in both isolated and live environments. The SDK approach provides full programmatic control over network lifecycle management.
75107

108+
## Architecture
109+
110+
The framework is built around a **Launcher** architecture that orchestrates node management across different environments:
111+
112+
- **Client**: High-level API for network management
113+
- **Launcher**: Central orchestrator for network operations (previously called "Overseer")
114+
- **Environments**: Support for Local process execution and Docker containers
115+
- **Presets**: Pre-configured network templates for common scenarios
116+
- **ThorBuilder**: Automatic Thor binary management and building
117+
118+
## Available Presets
119+
120+
### Local Networks
121+
- `preset.LocalThreeNodesNetwork()` - 3-node local network with authority nodes
122+
- `preset.LocalSixNodesNetwork()` - 6-node local network for larger testing scenarios
123+
124+
### Public Networks
125+
- `preset.NewTestnetNetwork()` - Connect to VeChain testnet
126+
- `preset.NewMainnetNetwork()` - Connect to VeChain mainnet
127+
128+
## Environments
129+
130+
### Local Environment
131+
Runs Thor nodes as local processes on your machine:
132+
```go
133+
network.Environment = environments.Local
134+
```
135+
136+
### Docker Environment
137+
Runs Thor nodes in Docker containers with proper networking:
138+
```go
139+
network.Environment = environments.Docker
140+
```
141+
76142
## Technical Requirements
77-
- **Git**: For cloning the repository.
78-
- **Golang**: Version 1.19 or higher.
143+
- **Git**: For cloning the repository
144+
- **Golang**: Version 1.19 or higher
145+
- **Docker**: Required for Docker environment (optional for Local environment)
79146

80147
## Thorbuilder Package
81148
The `thorbuilder` package is a key component of the networkHub framework that provides flexible configuration options for building Thor binaries from source. It supports both local builds and Docker image creation, with options for reusable builds and debug configurations.

client/client.go

Lines changed: 25 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,49 @@ import (
55
"strings"
66

77
"github.com/vechain/networkhub/internal/environments"
8-
"github.com/vechain/networkhub/internal/environments/docker"
9-
"github.com/vechain/networkhub/internal/environments/local"
8+
"github.com/vechain/networkhub/internal/environments/launcher"
109
"github.com/vechain/networkhub/network"
1110
"github.com/vechain/networkhub/network/node"
12-
"github.com/vechain/networkhub/thorbuilder"
1311
)
1412

1513
type Client struct {
16-
network *network.Network
17-
environment environments.Actions
18-
factories map[string]environments.Factory
14+
network *network.Network
15+
actions environments.Actions
1916
}
2017

21-
func New() *Client {
22-
factories := map[string]environments.Factory{
23-
"local": local.NewFactory(),
24-
"docker": docker.NewFactory(),
25-
}
26-
27-
return &Client{
28-
factories: factories,
18+
func New(net *network.Network) (*Client, error) {
19+
env, err := launcher.New(net)
20+
if err != nil {
21+
return nil, err
2922
}
30-
}
3123

32-
func NewWithNetwork(net *network.Network) (*Client, error) {
33-
c := New()
34-
if err := c.LoadNetwork(net); err != nil {
35-
return nil, err
24+
c := &Client{
25+
network: net,
26+
actions: env,
3627
}
3728

38-
if strings.Contains(net.ID(), "mainnet") || strings.Contains(net.ID(), "testnet") {
29+
// Auto-start for public networks (testnet/mainnet)
30+
if strings.Contains(net.ID(), network.Mainnet) || strings.Contains(net.ID(), network.Testnet) {
3931
if err := c.Start(); err != nil {
4032
return nil, err
4133
}
4234
}
35+
4336
return c, nil
4437
}
4538

4639
func (c *Client) Stop() error {
47-
if c.environment == nil {
40+
if c.actions == nil {
4841
return fmt.Errorf("no network loaded")
4942
}
50-
return c.environment.StopNetwork()
43+
return c.actions.StopNetwork()
5144
}
5245

5346
func (c *Client) Start() error {
54-
if c.environment == nil {
47+
if c.actions == nil {
5548
return fmt.Errorf("no network loaded")
5649
}
57-
return c.environment.StartNetwork()
50+
return c.actions.StartNetwork()
5851
}
5952

6053
func (c *Client) GetNetwork() (*network.Network, error) {
@@ -64,71 +57,29 @@ func (c *Client) GetNetwork() (*network.Network, error) {
6457
return c.network, nil
6558
}
6659

67-
func (c *Client) LoadNetwork(net *network.Network) error {
68-
factory, ok := c.factories[net.Environment]
69-
if !ok {
70-
return fmt.Errorf("unsupported environment: %s", net.Environment)
71-
}
72-
73-
// Handle thor binary management at client level
74-
if net.ThorBuilder != nil {
75-
execPath, err := thorbuilder.NewAndBuild(net.ThorBuilder)
76-
if err != nil {
77-
return fmt.Errorf("failed to build thor binary: %w", err)
78-
}
79-
80-
// Set exec artifact for all nodes that don't have one
81-
for _, nodeConfig := range net.Nodes {
82-
if nodeConfig.GetExecArtifact() == "" {
83-
nodeConfig.SetExecArtifact(execPath)
84-
}
85-
}
86-
}
87-
88-
env := factory.New()
89-
_, err := env.LoadConfig(net)
90-
if err != nil {
91-
return fmt.Errorf("failed to load network config: %w", err)
92-
}
93-
94-
c.network = net
95-
c.environment = env
96-
return nil
97-
}
98-
9960
func (c *Client) Nodes() (map[string]node.Lifecycle, error) {
100-
if c.environment == nil {
61+
if c.actions == nil {
10162
return nil, fmt.Errorf("no network loaded")
10263
}
103-
return c.environment.Nodes(), nil
64+
return c.actions.Nodes(), nil
10465
}
10566

10667
func (c *Client) AddNode(nodeConfig node.Config) error {
10768
if c.network == nil {
10869
return fmt.Errorf("no network loaded")
10970
}
11071

111-
if c.environment == nil {
72+
if c.actions == nil {
11273
return fmt.Errorf("environment not initialized")
11374
}
11475

115-
// Set exec artifact for the new node if it doesn't have one and we have a thor builder
116-
if nodeConfig.GetExecArtifact() == "" && c.network.ThorBuilder != nil {
117-
execPath, err := thorbuilder.NewAndBuild(c.network.ThorBuilder)
118-
if err != nil {
119-
return fmt.Errorf("failed to build thor binary for new node: %w", err)
120-
}
121-
122-
nodeConfig.SetExecArtifact(execPath)
123-
}
124-
12576
// Use environment's AddNode method
126-
if err := c.environment.AddNode(nodeConfig); err != nil {
77+
if err := c.actions.AddNode(nodeConfig); err != nil {
12778
return fmt.Errorf("failed to add node to environment: %w", err)
12879
}
12980

13081
// Update client's network reference
131-
c.network = c.environment.Config()
82+
c.network = c.actions.Config()
13283

13384
return nil
13485
}
@@ -138,17 +89,17 @@ func (c *Client) RemoveNode(nodeID string) error {
13889
return fmt.Errorf("no network loaded")
13990
}
14091

141-
if c.environment == nil {
92+
if c.actions == nil {
14293
return fmt.Errorf("environment not initialized")
14394
}
14495

14596
// Use environment's RemoveNode method
146-
if err := c.environment.RemoveNode(nodeID); err != nil {
97+
if err := c.actions.RemoveNode(nodeID); err != nil {
14798
return fmt.Errorf("failed to remove node from environment: %w", err)
14899
}
149100

150101
// Update client's network reference
151-
c.network = c.environment.Config()
102+
c.network = c.actions.Config()
152103

153104
return nil
154105
}

0 commit comments

Comments
 (0)