Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forked networks update #8

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package consts

import (
"github.com/ethereum/go-ethereum/common/hexutil"
"math/big"

"github.com/ethereum/go-ethereum/common/hexutil"
)

var (
Expand Down
22 changes: 6 additions & 16 deletions entrypoint/client/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package client

import (
"encoding/json"
"fmt"
"github.com/vechain/networkhub/environments/docker"

"github.com/vechain/networkhub/environments/local"
"github.com/vechain/networkhub/hub"
"github.com/vechain/networkhub/network"
Expand All @@ -19,11 +18,11 @@ type Client struct {
func New() *Client {
envManager := hub.NewNetworkHub()
envManager.RegisterEnvironment("local", local.NewLocalEnv)
envManager.RegisterEnvironment("docker", docker.NewDockerEnv)
//envManager.RegisterEnvironment("docker", docker.NewDockerEnv)

presets := preset.NewPresetNetworks()
presets.Register("threeMasterNodesNetwork", preset.LocalThreeMasterNodesNetwork)
presets.Register("sixNodesNetwork", preset.LocalSixNodesNetwork)
//presets.Register("threeMasterNodesNetwork", preset.LocalThreeMasterNodesNetwork)
//presets.Register("sixNodesNetwork", preset.LocalSixNodesNetwork)

return &Client{
networkHub: envManager,
Expand All @@ -40,15 +39,6 @@ func (c *Client) Start(id string) error {
return c.networkHub.StartNetwork(id)
}

func (c *Client) Config(config string) (string, error) {
var netCfg network.Network

if err := json.Unmarshal([]byte(config), &netCfg); err != nil {
return "", err
}
return c.config(&netCfg)
}

func (c *Client) LoadExistingNetworks() error {
nets, err := c.storage.LoadExistingNetworks()
if err != nil {
Expand All @@ -74,10 +64,10 @@ func (c *Client) Preset(presetNetwork string, environment, artifactPath string)
if err != nil {
return "", fmt.Errorf("unable to load network preset: %w", err)
}
return c.config(netCfg)
return c.Config(netCfg)
}

func (c *Client) config(netCfg *network.Network) (string, error) {
func (c *Client) Config(netCfg *network.Network) (string, error) {
networkID, err := c.networkHub.LoadNetworkConfig(netCfg)
if err != nil {
return "", fmt.Errorf("unable to load config: %w", err)
Expand Down
8 changes: 5 additions & 3 deletions entrypoint/client/storage_inmem.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package client

import (
"github.com/vechain/networkhub/network"
"log/slog"

"github.com/vechain/networkhub/network"
)

type Storage struct {
path string
storage map[string]*network.Network
}

func NewInMemStorage() *Storage {
return &Storage{}
return &Storage{
storage: map[string]*network.Network{},
}
}

func (s *Storage) Store(networkID string, net *network.Network) error {
Expand Down
22 changes: 11 additions & 11 deletions environments/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func (d *Docker) LoadConfig(cfg *network.Network) (string, error) {

for i, node := range cfg.Nodes {
// use preset dirs if not defined
if node.ConfigDir == "" {
node.ConfigDir = "/home/thor"
if node.GetConfigDir() == "" {
node.SetConfigDir("/home/thor")
}
if node.DataDir == "" {
node.DataDir = "/home/thor"
if node.GetDataDir() == "" {
node.SetDataDir("/home/thor")
}

// ensure API ports are exposed to the localhost
split := strings.Split(node.APIAddr, ":")
split := strings.Split(node.GetAPIAddr(), ":")
if len(split) != 2 {
return "", fmt.Errorf("unable to parse API Addr")
}
Expand All @@ -56,7 +56,7 @@ func (d *Docker) LoadConfig(cfg *network.Network) (string, error) {
return "", err
}

d.exposedPorts[node.ID] = &exposedPort{
d.exposedPorts[node.GetID()] = &exposedPort{
hostPort: fmt.Sprintf("%d", exposedAPIPort+i),
containerPort: split[1],
}
Expand All @@ -73,30 +73,30 @@ func (d *Docker) StartNetwork() error {

for _, nodeCfg := range d.networkCfg.Nodes {
// calculate the node ip address
nextIpAddr, err := d.ipManager.NextIP(nodeCfg.ID)
nextIpAddr, err := d.ipManager.NextIP(nodeCfg.GetID())
if err != nil {
return err
}

// speed up p2p bootstrap
var enodes []string
for _, node := range d.networkCfg.Nodes {
if node.ID == nodeCfg.ID {
if node.GetID() == nodeCfg.GetID() {
break
}
enode, err := node.Enode(d.ipManager.GetNodeIP(node.ID))
enode, err := node.Enode(d.ipManager.GetNodeIP(node.GetID()))
if err != nil {
return err
}
enodes = append(enodes, enode)
}

dockerNode := NewDockerNode(nodeCfg, enodes, d.networkID, d.exposedPorts[nodeCfg.ID], nextIpAddr)
dockerNode := NewDockerNode(nodeCfg, enodes, d.networkID, d.exposedPorts[nodeCfg.GetID()], nextIpAddr)
if err := dockerNode.Start(); err != nil {
return fmt.Errorf("unable to start node - %w", err)
}

d.dockerNodes[nodeCfg.ID] = dockerNode
d.dockerNodes[nodeCfg.GetID()] = dockerNode
}

return nil
Expand Down
24 changes: 12 additions & 12 deletions environments/docker/docker_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// NewDockerNode initializes a new DockerNode
func NewDockerNode(cfg *node.Node, enodes []string, networkID string, exposedPorts *exposedPort, ipAddr string) *Node {
func NewDockerNode(cfg node.Node, enodes []string, networkID string, exposedPorts *exposedPort, ipAddr string) *Node {
return &Node{
cfg: cfg,
enodes: enodes,
Expand All @@ -27,7 +27,7 @@ func NewDockerNode(cfg *node.Node, enodes []string, networkID string, exposedPor

// Node represents a Docker container node
type Node struct {
cfg *node.Node
cfg node.Node
enodes []string
id string
networkID string
Expand All @@ -44,11 +44,11 @@ func (n *Node) Start() error {
}

// Check if the Docker image is available locally
_, _, err = cli.ImageInspectWithRaw(ctx, n.cfg.ExecArtifact)
_, _, err = cli.ImageInspectWithRaw(ctx, n.cfg.GetExecArtifact())
if err != nil {
if client.IsErrNotFound(err) {
// Pull the Docker image
_, err = cli.ImagePull(ctx, n.cfg.ExecArtifact, image.PullOptions{})
_, err = cli.ImagePull(ctx, n.cfg.GetExecArtifact(), image.PullOptions{})
if err != nil {
return fmt.Errorf("failed to pull Docker image: %w", err)
}
Expand Down Expand Up @@ -78,15 +78,15 @@ func (n *Node) Start() error {
"thor " +
"--network genesis.json " +
"--nat none " +
fmt.Sprintf("--config-dir='%s' ", n.cfg.ConfigDir) +
fmt.Sprintf("--api-addr='%s' ", n.cfg.APIAddr) +
fmt.Sprintf("--api-cors='%s' ", n.cfg.APICORS) +
fmt.Sprintf("--p2p-port=%d ", n.cfg.P2PListenPort) +
fmt.Sprintf("--config-dir='%s' ", n.cfg.GetConfigDir()) +
fmt.Sprintf("--api-addr='%s' ", n.cfg.GetAPIAddr()) +
fmt.Sprintf("--api-cors='%s' ", n.cfg.GetAPICORS()) +
fmt.Sprintf("--p2p-port=%d ", n.cfg.GetP2PListenPort()) +
fmt.Sprintf("--bootnode=%s", enodeString),
}

//serialize genesis
genesisBytes, err := json.Marshal(n.cfg.Genesis)
genesisBytes, err := json.Marshal(n.cfg.GetGenesis())
if err != nil {
return fmt.Errorf("unable to marshal genesis - %w", err)
}
Expand All @@ -104,12 +104,12 @@ func (n *Node) Start() error {

// Construct Docker container configuration
config := &container.Config{
Image: n.cfg.ExecArtifact,
Image: n.cfg.GetExecArtifact(),
Cmd: cmd,
Entrypoint: []string{},
Env: []string{
fmt.Sprintf("GENESIS=%s", string(genesisBytes)),
fmt.Sprintf("PRIVATEKEY=%s", n.cfg.Key),
fmt.Sprintf("PRIVATEKEY=%s", n.cfg.GetKey()),
},
ExposedPorts: exposedPorts,
}
Expand All @@ -130,7 +130,7 @@ func (n *Node) Start() error {
}

// Create the Docker container
resp, err := cli.ContainerCreate(ctx, config, hostConfig, networkConfig, nil, n.cfg.ID)
resp, err := cli.ContainerCreate(ctx, config, hostConfig, networkConfig, nil, n.cfg.GetID())
if err != nil {
return fmt.Errorf("failed to create Docker container: %w", err)
}
Expand Down
81 changes: 81 additions & 0 deletions environments/local/local_downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package local

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

type Downloader struct {
repoURL string
branch string
downloadDir string
}

func NewDownloader(branch string) *Downloader {
if branch == "" {
branch = "master"
}
downloadDir := filepath.Join(os.TempDir(), "thor_repo")
return &Downloader{
repoURL: "https://github.com/vechain/thor",
branch: branch,
downloadDir: downloadDir,
}
}

func (d *Downloader) CloneAndBuildThor() (string, error) {
if err := d.DownloadRepo(); err != nil {
return "", fmt.Errorf("unable to clone repo: %w", err)
}

return d.BuildThor()
}

func (d *Downloader) DownloadRepo() error {
repoDir := filepath.Join(d.downloadDir, "thor")

// Check if the repo directory already exists
if _, err := os.Stat(repoDir); !os.IsNotExist(err) {
fmt.Printf("Repository already exists at %s, skipping download.\n", repoDir)
return nil
}

_, err := git.PlainClone(repoDir, false, &git.CloneOptions{
URL: d.repoURL,
ReferenceName: plumbing.NewBranchReferenceName(d.branch), // git.ReferenceName(fmt.Sprintf("refs/heads/%s", d.branch)),
SingleBranch: true,
Depth: 1,
})
if err != nil {
return fmt.Errorf("failed to clone repo: %w", err)
}
return nil
}

func (d *Downloader) BuildThor() (string, error) {
repoDir := filepath.Join(d.downloadDir, "thor")
thorPath := filepath.Join(repoDir, "bin", "thor") // Assuming 'bin/thor' is the path to the built thor binary

// Check if the thor binary already exists
if _, err := os.Stat(thorPath); !os.IsNotExist(err) {
fmt.Printf("Thor binary already exists at %s, skipping build.\n", thorPath)
return thorPath, nil
}

cmd := exec.Command("make", "thor", "cmd")
cmd.Dir = repoDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
return "", fmt.Errorf("failed to build thor: %w", err)
}

return thorPath, nil
}
21 changes: 21 additions & 0 deletions environments/local/local_downloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package local

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestDownloader(t *testing.T) {
t.Skip()
downloader := NewDownloader("") // Pass a branch name here if needed

err := downloader.DownloadRepo()
require.NoError(t, err)

thorPath, err := downloader.BuildThor()
require.NoError(t, err)

fmt.Printf("Built thor app at: %s\n", thorPath)
}
7 changes: 4 additions & 3 deletions environments/local/local_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ func (n *Node) Start() error {
}

fmt.Println(cmd)
//if n.nodeCfg.GetID() == "node1" {
// return nil
//}
if n.nodeCfg.GetFakeExecution() {
fmt.Println("FakeExecution enabled - Not starting node: ", n.nodeCfg.GetID())
return nil
}
// Start the command and check for errors
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start thor command: %w", err)
Expand Down
Loading
Loading