Skip to content

Commit

Permalink
genesis is a json + temp dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed May 1, 2024
1 parent fbbba03 commit d10e6f2
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 49 deletions.
54 changes: 30 additions & 24 deletions environments/local/local.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package local

import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

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

type Local struct {
Expand All @@ -28,23 +28,38 @@ func NewLocalEnv() environments.Actions {

func (l *Local) LoadConfig(cfg *network.Network) (string, error) {
l.networkCfg = cfg
l.id = hashObject(cfg)
l.id = hashObject(l.networkCfg)
baseTmpDir := filepath.Join(os.TempDir(), l.id)

// ensure paths exist, use temp dirs if not defined
for _, n := range l.networkCfg.Nodes {
if n.ConfigDir == "" {
n.ConfigDir = filepath.Join(baseTmpDir, n.ID, "config")
}

if n.DataDir == "" {
n.DataDir = filepath.Join(baseTmpDir, n.ID, "data")
}
}

return l.id, nil
}

func (l *Local) StartNetwork() error {
// speed up p2p bootstrap
var enodes []string
for _, node := range l.networkCfg.Nodes {
enodes = append(enodes, node.Enode)
}

enodeString := strings.Join(enodes, ",")
for _, node := range l.networkCfg.Nodes {
localNode, err := l.startNode(node, enodeString)
if err != nil {

for _, nodeCfg := range l.networkCfg.Nodes {
localNode := NewLocalNode(nodeCfg, enodeString)
if err := localNode.Start(); err != nil {
return fmt.Errorf("unable to start node - %w", err)
}
l.localNodes[node.ID] = localNode

l.localNodes[nodeCfg.ID] = localNode
}

return nil
Expand All @@ -65,26 +80,17 @@ func (l *Local) Info() error {
panic("implement me")
}

func (l *Local) startNode(nodeCfg *node.Node, enodeString string) (*Node, error) {
localNode := NewLocalNode(nodeCfg, enodeString)
return localNode, localNode.Start()
}

func hashObject(obj interface{}) string {
// Create a buffer to hold the encoded data
var buf bytes.Buffer

// New encoder that writes to the buffer
encoder := gob.NewEncoder(&buf)

// Encode the object; handle errors
if err := encoder.Encode(obj); err != nil {
log.Fatalf("Failed to encode object: %v", err)
// Serialize the object to JSON
jsonData, err := json.Marshal(obj)
if err != nil {
log.Fatalf("Failed to JSON encode object: %v", err)
}

// Compute SHA-256 checksum on the buffer's bytes
// Compute SHA-256 checksum on the JSON data
hash := sha256.New()
hash.Write(buf.Bytes())
hash.Write(jsonData)
hashBytes := hash.Sum(nil)

// Convert hash bytes to hex string
Expand Down
26 changes: 24 additions & 2 deletions environments/local/local_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package local

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -25,18 +26,39 @@ func NewLocalNode(nodeCfg *node.Node, enodes string) *Node {
}

func (n *Node) Start() error {
// ensure directories exist
if err := os.MkdirAll(n.nodeCfg.ConfigDir, 0777); err != nil {
return fmt.Errorf("unable to create configDir - %w", err)
}
if err := os.MkdirAll(n.nodeCfg.DataDir, 0777); err != nil {
return fmt.Errorf("unable to create configDir - %w", err)
}

// write keys to disk
if n.nodeCfg.Type == "masterNode" && n.nodeCfg.Key != "" {
err := os.WriteFile(filepath.Join(n.nodeCfg.ConfigDir, "master.key"), []byte(n.nodeCfg.Key), 0644)
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", filepath.Join(n.nodeCfg.ConfigDir, "master.key"), err)
return fmt.Errorf("failed to write master key file - %w", err)
}
}

// write genesis to disk
genesisPath := filepath.Join(n.nodeCfg.ConfigDir, "genesis.json")
genesisBytes, err := json.Marshal(n.nodeCfg.Genesis)
if err != nil {
return fmt.Errorf("unable to marshal genesis - %w", err)
}

err = os.WriteFile(genesisPath, genesisBytes, 0644)
if err != nil {
return fmt.Errorf("failed to write genesis file - %w", err)
}

cmd := &exec.Cmd{
Path: "/Users/pedro/go/src/github.com/vechain/thor/bin/thor",
Args: []string{
"thor",
"--network", n.nodeCfg.Genesis,
"--network", genesisPath,
"--data-dir", n.nodeCfg.DataDir,
"--config-dir", n.nodeCfg.ConfigDir,
"--api-addr", n.nodeCfg.APIAddr,
Expand Down
Loading

0 comments on commit d10e6f2

Please sign in to comment.