Skip to content

Commit

Permalink
fix paths issue with embed...
Browse files Browse the repository at this point in the history
  • Loading branch information
holisticode committed Oct 25, 2022
1 parent 0ab1f7d commit 7e4ebc5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
22 changes: 15 additions & 7 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package local

import (
"context"
"embed"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net"
"os"
"os/user"
Expand All @@ -17,7 +20,6 @@ import (
"github.com/ava-labs/avalanche-network-runner/network/node"
"github.com/ava-labs/avalanche-network-runner/network/node/status"
"github.com/ava-labs/avalanche-network-runner/utils"
"github.com/ava-labs/avalanche-network-runner/utils/constants"
"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/network/peer"
"github.com/ava-labs/avalanchego/staking"
Expand Down Expand Up @@ -104,6 +106,8 @@ type localNetwork struct {
}

var (
//go:embed default
embeddedDefaultNetworkConfigDir embed.FS
// Pre-defined network configuration.
// [defaultNetworkConfig] should not be modified.
// TODO add method Copy() to network.Config to prevent
Expand All @@ -116,7 +120,7 @@ var (
// populate default network config from embedded default directory
func init() {
// load genesis, updating validation start time
genesisMap, err := utils.LoadLocalGenesis()
genesisMap, err := network.LoadLocalGenesis()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -155,7 +159,11 @@ func init() {
}

// load network flags
flagsBytes, err := os.ReadFile(filepath.Join("..", constants.LocalConfigDir, "flags.json"))
configsDir, err := fs.Sub(embeddedDefaultNetworkConfigDir, "default")
if err != nil {
panic(err)
}
flagsBytes, err := fs.ReadFile(configsDir, "flags.json")
if err != nil {
panic(err)
}
Expand All @@ -165,7 +173,7 @@ func init() {
}

// load chain config
cChainConfig, err := os.ReadFile(filepath.Join("..", constants.LocalConfigDir, "cchain_config.json"))
cChainConfig, err := fs.ReadFile(configsDir, "cchain_config.json")
if err != nil {
panic(err)
}
Expand All @@ -181,7 +189,7 @@ func init() {
}

for i := 0; i < len(defaultNetworkConfig.NodeConfigs); i++ {
flagsBytes, err := os.ReadFile(filepath.Join("..", constants.LocalConfigDir, fmt.Sprintf("node%d/flags.json", i+1)))
flagsBytes, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/flags.json", i+1))
if err != nil {
panic(err)
}
Expand All @@ -190,12 +198,12 @@ func init() {
panic(err)
}
defaultNetworkConfig.NodeConfigs[i].Flags = flags
stakingKey, err := os.ReadFile(filepath.Join("..", constants.LocalConfigDir, fmt.Sprintf("node%d/staking.key", i+1)))
stakingKey, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/staking.key", i+1))
if err != nil {
panic(err)
}
defaultNetworkConfig.NodeConfigs[i].StakingKey = string(stakingKey)
stakingCert, err := os.ReadFile(filepath.Join("..", constants.LocalConfigDir, fmt.Sprintf("node%d/staking.crt", i+1)))
stakingCert, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/staking.crt", i+1))
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (

func init() {
var err error
genesisMap, err := utils.LoadLocalGenesis()
genesisMap, err := LoadLocalGenesis()
if err != nil {
panic(err)
}
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions utils/chains.go → network/genesis.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package utils
package network

import (
_ "embed"
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/ava-labs/avalanche-network-runner/utils/constants"
coreth_params "github.com/ava-labs/coreth/params"
)

//go:embed default/genesis.json
var genesisBytes []byte

// LoadLocalGenesis loads the local network genesis from disk
// and returns it as a map[string]interface{}
func LoadLocalGenesis() (map[string]interface{}, error) {
genesis, err := os.ReadFile(filepath.Join("..", constants.LocalGenesisFile))
if err != nil {
return nil, err
}
var genesisMap map[string]interface{}
if err = json.Unmarshal(genesis, &genesisMap); err != nil {
var (
genesisMap map[string]interface{}
err error
)
if err = json.Unmarshal(genesisBytes, &genesisMap); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const (

var (
LocalConfigDir = filepath.Join("local", "default")
LocalGenesisFile = filepath.Join(LocalConfigDir, "genesis.json")
LocalGenesisFile = "genesis.json"
)

0 comments on commit 7e4ebc5

Please sign in to comment.