Skip to content

Commit

Permalink
Use preset mainnet and testnet genesis
Browse files Browse the repository at this point in the history
Allow custom path by using a prefix as well
  • Loading branch information
R-Santev committed Jan 24, 2025
1 parent accb799 commit 324aa28
Show file tree
Hide file tree
Showing 6 changed files with 593 additions and 5 deletions.
275 changes: 275 additions & 0 deletions chain/public-configs/genesis-mainnet.json

Large diffs are not rendered by default.

279 changes: 279 additions & 0 deletions chain/public-configs/genesis-testnet.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions chain/public-configs/public-configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package publicconfigs

const (
GenesisPathMainnet = "./chain/public-configs/genesis-mainnet.json"
GenesisPathTestnet = "./chain/public-configs/genesis-testnet.json"
)
2 changes: 1 addition & 1 deletion command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func DefaultConfig() *Config {
defaultNetworkConfig := network.DefaultConfig()

return &Config{
GenesisPath: "./genesis.json",
GenesisPath: "",
DataDir: "",
BlockGasTarget: "0x5f5e100", // Special value signaling the parent gas limit should be applied
Network: &Network{
Expand Down
26 changes: 24 additions & 2 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package server
import (
"errors"
"net"
"strings"

"github.com/0xPolygon/polygon-edge/chain"
publicconfigs "github.com/0xPolygon/polygon-edge/chain/public-configs"
"github.com/0xPolygon/polygon-edge/command/server/config"
"github.com/0xPolygon/polygon-edge/network"
"github.com/0xPolygon/polygon-edge/secrets"
Expand All @@ -15,7 +17,7 @@ import (

const (
configFlag = "config"
genesisPathFlag = "chain"
genesisFlag = "chain"
dataDirFlag = "data-dir"
libp2pAddressFlag = "libp2p"
prometheusAddressFlag = "prometheus"
Expand Down Expand Up @@ -67,7 +69,8 @@ var (
)

var (
errInvalidNATAddress = errors.New("could not parse NAT IP address")
errInvalidNATAddress = errors.New("could not parse NAT IP address")
errGenesisCustomPrexifMissing = errors.New("genesis path must start with 'custom:'")
)

type serverParams struct {
Expand Down Expand Up @@ -148,6 +151,25 @@ func (p *serverParams) setJSONLogFormat(jsonLogFormat bool) {
p.rawConfig.JSONLogFormat = jsonLogFormat
}

func (p *serverParams) setRawGenesisPath(genesisFlag string) error {
switch genesisFlag {
case "mainnet":
p.rawConfig.GenesisPath = publicconfigs.GenesisPathMainnet
case "testnet":
p.rawConfig.GenesisPath = publicconfigs.GenesisPathTestnet
default:
const customPrefix = "custom:"
if !strings.HasPrefix(genesisFlag, customPrefix) {
return errGenesisCustomPrexifMissing
} else {
actualPath := strings.TrimPrefix(genesisFlag, customPrefix)
p.rawConfig.GenesisPath = actualPath
}
}

return nil
}

func (p *serverParams) generateConfig() *server.Config {
return &server.Config{
Chain: p.genesisConfig,
Expand Down
10 changes: 8 additions & 2 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func setFlags(cmd *cobra.Command) {

cmd.Flags().StringVar(
&params.rawConfig.GenesisPath,
genesisPathFlag,
genesisFlag,
defaultConfig.GenesisPath,
"the genesis file used for starting the chain",
"the genesis file used for starting the chain."+
`Can be "mainnet", "testnet" or "custom"<path_to_custom_genesis_file>"`,
)

cmd.Flags().StringVar(
Expand Down Expand Up @@ -301,6 +302,11 @@ func runPreRun(cmd *cobra.Command, _ []string) error {
}
}

// Before raw params are initialized, set the actual genesis path based on --chain flag
if err := params.setRawGenesisPath(params.rawConfig.GenesisPath); err != nil {
return err
}

if err := params.initRawParams(); err != nil {
return err
}
Expand Down

0 comments on commit 324aa28

Please sign in to comment.