Skip to content

Commit

Permalink
goal: goal network create default templates (#4891)
Browse files Browse the repository at this point in the history
  • Loading branch information
mciccarello authored Feb 1, 2023
1 parent 513a88a commit b326672
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 16 deletions.
46 changes: 46 additions & 0 deletions cmd/goal/defaultNetworkTemplate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Genesis": {
"NetworkName": "Default Network Template",
"RewardsPoolBalance": 0,
"FirstPartKeyRound": 0,
"LastPartKeyRound": 30000,
"Wallets": [
{
"Name": "Wallet1",
"Stake": 40,
"Online": true
},
{
"Name": "Wallet2",
"Stake": 40,
"Online": true
},
{
"Name": "Wallet3",
"Stake": 20,
"Online": false
}
]
},
"Nodes": [
{
"Name": "Node",
"IsRelay": false,
"Wallets": [
{
"Name": "Wallet1",
"ParticipationOnly": false
},
{
"Name": "Wallet2",
"ParticipationOnly": false
},
{
"Name": "Wallet3",
"ParticipationOnly": false
}
]
}
]
}

30 changes: 25 additions & 5 deletions cmd/goal/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package main

import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

Expand All @@ -45,7 +48,6 @@ func init() {

networkCreateCmd.Flags().StringVarP(&networkName, "network", "n", "", "Specify the name to use for the private network")
networkCreateCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network")
networkCreateCmd.MarkFlagRequired("template")
networkCreateCmd.Flags().BoolVarP(&noImportKeys, "noimportkeys", "K", false, "Do not import root keys when creating the network (by default will import)")
networkCreateCmd.Flags().BoolVar(&noClean, "noclean", false, "Prevents auto-cleanup on error - for diagnosing problems")
networkCreateCmd.Flags().BoolVar(&devModeOverride, "devMode", false, "Forces the configuration to enable DevMode, returns an error if the template is not compatible with DevMode.")
Expand Down Expand Up @@ -73,6 +75,9 @@ The basic idea is that we create one or more data directories and wallets to for
},
}

//go:embed defaultNetworkTemplate.json
var defaultNetworkTemplate string

var networkCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a private named network from a template",
Expand All @@ -83,10 +88,25 @@ var networkCreateCmd = &cobra.Command{
if err != nil {
panic(err)
}
networkTemplateFile, err := filepath.Abs(networkTemplateFile)
if err != nil {
panic(err)

var templateReader io.Reader

if networkTemplateFile == "" {
templateReader = strings.NewReader(defaultNetworkTemplate)
} else {
networkTemplateFile, err = filepath.Abs(networkTemplateFile)
if err != nil {
panic(err)
}
file, err := os.Open(networkTemplateFile)
if err != nil {
reportErrorf(errorCreateNetwork, err)
}

defer file.Close()
templateReader = file
}

// Make sure target directory does not exist or is empty
if util.FileExists(networkRootDir) && !util.IsEmpty(networkRootDir) {
reportErrorf(infoNetworkAlreadyExists, networkRootDir)
Expand All @@ -104,7 +124,7 @@ var networkCreateCmd = &cobra.Command{
consensus, _ = config.PreloadConfigurableConsensusProtocols(dataDir)
}

network, err := netdeploy.CreateNetworkFromTemplate(networkName, networkRootDir, networkTemplateFile, binDir, !noImportKeys, nil, consensus, devModeOverride)
network, err := netdeploy.CreateNetworkFromTemplate(networkName, networkRootDir, templateReader, binDir, !noImportKeys, nil, consensus, devModeOverride)
if err != nil {
if noClean {
reportInfof(" ** failed ** - Preserving network rootdir '%s'", networkRootDir)
Expand Down
16 changes: 11 additions & 5 deletions netdeploy/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package netdeploy
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -57,25 +58,30 @@ type Network struct {

// CreateNetworkFromTemplate uses the specified template to deploy a new private network
// under the specified root directory.
func CreateNetworkFromTemplate(name, rootDir, templateFile, binDir string, importKeys bool, nodeExitCallback nodecontrol.AlgodExitErrorCallback, consensus config.ConsensusProtocols, overrideDevMode bool) (Network, error) {
func CreateNetworkFromTemplate(name, rootDir string, templateReader io.Reader, binDir string, importKeys bool, nodeExitCallback nodecontrol.AlgodExitErrorCallback, consensus config.ConsensusProtocols, overrideDevMode bool) (Network, error) {
n := Network{
rootDir: rootDir,
nodeExitCallback: nodeExitCallback,
}
n.cfg.Name = name
n.cfg.TemplateFile = templateFile

template, err := loadTemplate(templateFile)
var err error
template := defaultNetworkTemplate

err = loadTemplateFromReader(templateReader, &template)

if err == nil {
if overrideDevMode {
template.Genesis.DevMode = true
if len(template.Nodes) > 0 {
template.Nodes[0].IsRelay = false
}
}
err = template.Validate()
} else {
return n, err
}
if err != nil {

if err = template.Validate(); err != nil {
return n, err
}

Expand Down
10 changes: 5 additions & 5 deletions netdeploy/networkTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ func loadTemplate(templateFile string) (NetworkTemplate, error) {
}
defer f.Close()

if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
// for arm machines, use smaller key dilution
template.Genesis.PartKeyDilution = 100
}

err = loadTemplateFromReader(f, &template)
return template, err
}

func loadTemplateFromReader(reader io.Reader, template *NetworkTemplate) error {

if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
// for arm machines, use smaller key dilution
template.Genesis.PartKeyDilution = 100
}
dec := json.NewDecoder(reader)
return dec.Decode(template)
}
Expand Down
4 changes: 3 additions & 1 deletion test/framework/fixtures/libgoalFixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func (f *LibGoalFixture) setup(test TestingTB, testName string, templateFile str
os.RemoveAll(f.rootDir)
templateFile = filepath.Join(f.testDataDir, templateFile)
importKeys := false // Don't automatically import root keys when creating folders, we'll import on-demand
network, err := netdeploy.CreateNetworkFromTemplate("test", f.rootDir, templateFile, f.binDir, importKeys, f.nodeExitWithError, f.consensus, false)
file, err := os.Open(templateFile)
f.failOnError(err, "Template file could not be opened: %v")
network, err := netdeploy.CreateNetworkFromTemplate("test", f.rootDir, file, f.binDir, importKeys, f.nodeExitWithError, f.consensus, false)
f.failOnError(err, "CreateNetworkFromTemplate failed: %v")
f.network = network

Expand Down
12 changes: 12 additions & 0 deletions test/scripts/test_private_network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH}

${GOPATH}/bin/goal network delete -r ${NETROOTPATH}

# default network with no template specified

rm -rf ${NETROOTPATH}

${GOPATH}/bin/goal network create -r ${NETROOTPATH}

${GOPATH}/bin/goal network start -r ${NETROOTPATH}

${GOPATH}/bin/goal network stop -r ${NETROOTPATH}

${GOPATH}/bin/goal network delete -r ${NETROOTPATH}

echo "----------------------------------------------------------------------"
echo " DONE: test_private_network"
echo "----------------------------------------------------------------------"

0 comments on commit b326672

Please sign in to comment.