Skip to content

Commit

Permalink
unify genesis access
Browse files Browse the repository at this point in the history
  • Loading branch information
holisticode committed Oct 24, 2022
1 parent 06f9ee1 commit 051b33f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
48 changes: 8 additions & 40 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package local

import (
"context"
"embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net"
"os"
"os/user"
Expand All @@ -19,14 +17,14 @@ 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"
"github.com/ava-labs/avalanchego/utils/beacon"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
coreth_params "github.com/ava-labs/coreth/params"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -106,8 +104,6 @@ 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 @@ -119,20 +115,12 @@ var (

// populate default network config from embedded default directory
func init() {
configsDir, err := fs.Sub(embeddedDefaultNetworkConfigDir, "default")
if err != nil {
panic(err)
}

// load genesis, updating validation start time
genesis, err := fs.ReadFile(configsDir, "genesis.json")
genesisMap, err := utils.LoadGenesisMap()
if err != nil {
panic(err)
}
var genesisMap map[string]interface{}
if err = json.Unmarshal(genesis, &genesisMap); err != nil {
panic(err)
}

startTime := time.Now().Unix()
lockTime := startTime + genesisLocktimeStartimeDelta
genesisMap["startTime"] = float64(startTime)
Expand Down Expand Up @@ -160,34 +148,14 @@ func init() {
}
}

// set the cchain genesis directly from coreth
// the whole of `cChainGenesis` should be set as a string, not a json object...
corethCChainGenesis := coreth_params.AvalancheLocalChainConfig
// but the part in coreth is only the "config" part.
// In order to set it easily, first we get the cChainGenesis item
cChainGenesis := genesisMap["cChainGenesis"]
// convert it to a map
cChainGenesisMap, ok := cChainGenesis.(map[string]interface{})
if !ok {
panic("expected field 'cChainGenesis' of genesisMap to be a map[string]interface{}, but it failed")
}
// set the `config` key to the actual coreth object
cChainGenesisMap["config"] = corethCChainGenesis
// and then marshal everything into a string
genesisBytes, err := json.Marshal(cChainGenesisMap)
if err != nil {
panic(err)
}
// this way the whole of `cChainGenesis` is a properly escaped string
genesisMap["cChainGenesis"] = string(genesisBytes)
// now we can marshal the *whole* thing into bytes
updatedGenesis, err := json.Marshal(genesisMap)
if err != nil {
panic(err)
}

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

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

for i := 0; i < len(defaultNetworkConfig.NodeConfigs); i++ {
flagsBytes, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/flags.json", i+1))
flagsBytes, err := os.ReadFile(filepath.Join(constants.LocalConfigDir, fmt.Sprintf("node%d/flags.json", i+1)))
if err != nil {
panic(err)
}
Expand All @@ -222,12 +190,12 @@ func init() {
panic(err)
}
defaultNetworkConfig.NodeConfigs[i].Flags = flags
stakingKey, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/staking.key", i+1))
stakingKey, err := os.ReadFile(filepath.Join(constants.LocalConfigDir, fmt.Sprintf("node%d/staking.key", i+1)))
if err != nil {
panic(err)
}
defaultNetworkConfig.NodeConfigs[i].StakingKey = string(stakingKey)
stakingCert, err := fs.ReadFile(configsDir, fmt.Sprintf("node%d/staking.crt", i+1))
stakingCert, err := os.ReadFile(filepath.Join(constants.LocalConfigDir, fmt.Sprintf("node%d/staking.crt", i+1)))
if err != nil {
panic(err)
}
Expand Down
13 changes: 3 additions & 10 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/units"
coreth_params "github.com/ava-labs/coreth/params"
)

var cChainConfig map[string]interface{}

const (
validatorStake = units.MegaAvax
defaultCChainConfigStr = `{"config":{"chainId":43112,"homesteadBlock":0,"daoForkBlock":0,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"muirGlacierBlock":0,"apricotPhase1BlockTimestamp":0,"apricotPhase2BlockTimestamp":0,"apricotPhase3BlockTimestamp":0,"apricotPhase4BlockTimestamp":0,"apricotPhase5BlockTimestamp":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x00","gasLimit":"0x5f5e100","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`
validatorStake = units.MegaAvax
)

func init() {
if err := json.Unmarshal([]byte(defaultCChainConfigStr), &cChainConfig); err != nil {
panic(err)
}
corethCChainGenesis := coreth_params.AvalancheLocalChainConfig
cChainGenesisJSON, err := json.Marshal(corethCChainGenesis)
var err error
cChainConfig, err = utils.LoadGenesisMap()
if err != nil {
panic(err)
}
cChainGenesisStr := string(cChainGenesisJSON)
cChainConfig["config"] = cChainGenesisStr
}

// AddrAndBalance holds both an address and its balance
Expand Down
44 changes: 44 additions & 0 deletions utils/chains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils

import (
"encoding/json"
"fmt"
"os"

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

func LoadGenesisMap() (map[string]interface{}, error) {
genesis, err := os.ReadFile(constants.LocalGenesisFile)
if err != nil {
panic(err)
}
var genesisMap map[string]interface{}
if err = json.Unmarshal(genesis, &genesisMap); err != nil {
panic(err)
}

cChainGenesis := genesisMap["cChainGenesis"]
// set the cchain genesis directly from coreth
// the whole of `cChainGenesis` should be set as a string, not a json object...
corethCChainGenesis := coreth_params.AvalancheLocalChainConfig
// but the part in coreth is only the "config" part.
// In order to set it easily, first we get the cChainGenesis item
// convert it to a map
cChainGenesisMap, ok := cChainGenesis.(map[string]interface{})
if !ok {
return nil, fmt.Errorf(
"expected field 'cChainGenesis' of genesisMap to be a map[string]interface{}, but it failed with type %T", cChainGenesisMap)
}
// set the `config` key to the actual coreth object
cChainGenesisMap["config"] = corethCChainGenesis
// and then marshal everything into a string
configBytes, err := json.Marshal(cChainGenesisMap)
if err != nil {
return nil, err
}
// this way the whole of `cChainGenesis` is a properly escaped string
genesisMap["cChainGenesis"] = string(configBytes)
return genesisMap, nil
}
7 changes: 7 additions & 0 deletions utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
// See the file LICENSE for licensing terms.
package constants

import "path/filepath"

const (
LogNameMain = "main"
LogNameControl = "control"
LogNameTest = "test"
)

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

0 comments on commit 051b33f

Please sign in to comment.