Skip to content

Commit

Permalink
349 add multi network genesis configuration (#107)
Browse files Browse the repository at this point in the history
* Use preset mainnet and testnet genesis

Allow custom path by using a prefix as well

* polish implementation and update readme

* Embed public chain configs into the final binary

* turn off genesis predeploy command

* migrate to docker folder and update the docker compose node setup

* move devnet_cluster and update it
  • Loading branch information
R-Santev authored Jan 28, 2025
1 parent accb799 commit 58df7ac
Show file tree
Hide file tree
Showing 21 changed files with 660 additions and 71 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ hydra secrets generate --type encrypted-local --name node --extra "coingecko-api
Run your node with the following command from its directory:

```
hydra server --data-dir ./node-secrets --chain ./genesis.json --grpc-address :9632 --libp2p 0.0.0.0:1478 --jsonrpc 0.0.0.0:8545 --secrets-config ./secretsManagerConfig.json
hydra server --data-dir ./node-secrets --chain mainnet --grpc-address :9632 --libp2p 0.0.0.0:1478 --jsonrpc 0.0.0.0:8545 --secrets-config ./secretsManagerConfig.json
```

**Important!** Set the `--chain` flag to `testnet` for the Testnet network. For the Mainnet network, the `--chain` flag can be ommitted. You can also specify a custom genesis.json file path by setting `--chain` to "custom:<path_to_genesis_file>".

This process may take some time, as the node needs to fully sync with the blockchain. Once the syncing process is complete, you can proceed with the next steps.

### Prepare account to be a validator
Expand Down
10 changes: 9 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/umbracle/ethgo"

publicconfigs "github.com/0xPolygon/polygon-edge/chain/public-configs"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -377,7 +378,14 @@ func (g *GenesisAccount) UnmarshalJSON(data []byte) error {
}

func Import(chain string) (*Chain, error) {
return ImportFromFile(chain)
switch chain {
case "mainnet":
return importChain(publicconfigs.GetMainnetGenesis())
case "testnet":
return importChain(publicconfigs.GetTestnetGenesis())
default:
return ImportFromFile(chain)
}
}

// ImportFromFile imports a chain from a filepath
Expand Down
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.

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

import (
_ "embed"
)

//go:embed genesis-mainnet.json
var mainnetConfig []byte

//go:embed genesis-testnet.json
var testnetConfig []byte

func GetMainnetGenesis() []byte {
return mainnetConfig
}

func GetTestnetGenesis() []byte {
return testnetConfig
}
6 changes: 0 additions & 6 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/cobra"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/genesis/predeploy"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/ibft"
"github.com/0xPolygon/polygon-edge/helper/common"
Expand All @@ -24,11 +23,6 @@ func GetCommand() *cobra.Command {
setFlags(genesisCmd)
setLegacyFlags(genesisCmd)

genesisCmd.AddCommand(
// genesis predeploy
predeploy.GetCommand(),
)

return genesisCmd
}

Expand Down
4 changes: 2 additions & 2 deletions command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Config defines the server configuration params
type Config struct {
GenesisPath string `json:"chain_config" yaml:"chain_config"`
GenesisFile string `json:"chain_config" yaml:"chain_config"`
SecretsConfigPath string `json:"secrets_config" yaml:"secrets_config"`
DataDir string `json:"data_dir" yaml:"data_dir"`
BlockGasTarget string `json:"block_gas_target" yaml:"block_gas_target"`
Expand Down Expand Up @@ -104,7 +104,7 @@ func DefaultConfig() *Config {
defaultNetworkConfig := network.DefaultConfig()

return &Config{
GenesisPath: "./genesis.json",
GenesisFile: "mainnet", // will be resolved to the actual path
DataDir: "",
BlockGasTarget: "0x5f5e100", // Special value signaling the parent gas limit should be applied
Network: &Network{
Expand Down
2 changes: 1 addition & 1 deletion command/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *serverParams) initGenesisConfig() error {
var parseErr error

if p.genesisConfig, parseErr = chain.Import(
p.rawConfig.GenesisPath,
p.rawConfig.GenesisFile,
); parseErr != nil {
return parseErr
}
Expand Down
22 changes: 20 additions & 2 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"errors"
"net"
"strings"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/command/server/config"
Expand All @@ -15,7 +16,7 @@ import (

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

var (
errInvalidNATAddress = errors.New("could not parse NAT IP address")
errInvalidNATAddress = errors.New("could not parse NAT IP address")
errInvalidGenesisFileFlag = errors.New("genesis path must be 'mainnet', 'testnet' or must start with 'custom:'")
)

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

func (p *serverParams) setGenesisFileFlag(genesisFlag string) error {
if genesisFlag == "mainnet" || genesisFlag == "testnet" {
return nil
}

const customPrefix = "custom:"
if !strings.HasPrefix(genesisFlag, customPrefix) {
return errInvalidGenesisFileFlag
}

actualPath := strings.TrimPrefix(genesisFlag, customPrefix)
p.rawConfig.GenesisFile = actualPath

return nil
}

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

cmd.Flags().StringVar(
&params.rawConfig.GenesisPath,
genesisPathFlag,
defaultConfig.GenesisPath,
"the genesis file used for starting the chain",
&params.rawConfig.GenesisFile,
genesisFlag,
defaultConfig.GenesisFile,
"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 (if custom) based on --chain flag
if err := params.setGenesisFileFlag(params.rawConfig.GenesisFile); err != nil {
return err
}

if err := params.initRawParams(); err != nil {
return err
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
"--data-dir",
"/data/data-1",
"--chain",
"/data/genesis.json",
"custom:/data/genesis.json",
"--secrets-config",
"/data/secretsManagerConfig.json",
"--grpc-address",
Expand Down Expand Up @@ -58,7 +58,7 @@ services:
"--data-dir",
"/data/data-2",
"--chain",
"/data/genesis.json",
"custom:/data/genesis.json",
"--secrets-config",
"/data/secretsManagerConfig.json",
"--grpc-address",
Expand Down Expand Up @@ -93,7 +93,7 @@ services:
"--data-dir",
"/data/data-3",
"--chain",
"/data/genesis.json",
"custom:/data/genesis.json",
"--secrets-config",
"/data/secretsManagerConfig.json",
"--grpc-address",
Expand Down Expand Up @@ -128,7 +128,7 @@ services:
"--data-dir",
"/data/data-4",
"--chain",
"/data/genesis.json",
"custom:/data/genesis.json",
"--secrets-config",
"/data/secretsManagerConfig.json",
"--grpc-address",
Expand Down Expand Up @@ -163,7 +163,7 @@ services:
"--data-dir",
"/data/data-5",
"--chain",
"/data/genesis.json",
"custom:/data/genesis.json",
"--secrets-config",
"/data/secretsManagerConfig.json",
"--grpc-address",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ USER root
RUN apk add --no-cache bash curl

COPY ./scripts/secrets.sh .
COPY ./scripts/genesis.sh .
COPY ./scripts/run.sh .

# Make scripts executable
RUN chmod +x genesis.sh run.sh secrets.sh
RUN chmod +x secrets.sh run.sh

EXPOSE 9632 1478 8545

Expand Down
27 changes: 27 additions & 0 deletions docker/hydra_docker_compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Docker Compose setup

The purpose of the docker compose setup is to provide simple and easy-to-use setup.

_NB!_ Use more secure setup for validators.

## Installation

Ensure you have Docker installed and pull the image.

```
docker pull rsantev/hydragon-docker-compose:latest
```

## Run node

Add the needed secrets in the docker compose file in the `environment` section of the `hydra-node` service:
KEY
BLS_KEY
SIG
P2P_KEY

Run:

```
docker compose up
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ version: "3.8"

services:
hydra-node:
image: rsantev/hydra-testnet:latest
image: rsantev/hydragon-docker:latest
container_name: rpc-node-1
command:
[
command: [
"server",
"--data-dir",
"./node",
"--chain",
"genesis.json",
"testnet", # mainnet or testnet
"--grpc-address",
"127.0.0.1:9632",
"--libp2p",
Expand All @@ -25,12 +24,12 @@ services:
"--json-rpc-block-range-limit",
"0",
]
platform: linux/amd64
volumes:
- ./node:/app/node
environment:
# CoinGecko API Key for generating secretsManagerConfig.json
CG_KEY: ${COINGECKO_API_KEY}

ports:
- "10000:9632"
- "10001:1478"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ set -e
# Run secrets.sh
./secrets.sh

# Run secrets.sh
./genesis.sh

# Execute specified hydra command with all arguments
exec hydra "$@"
File renamed without changes.
23 changes: 0 additions & 23 deletions h_devnet/README.md

This file was deleted.

11 changes: 0 additions & 11 deletions h_devnet/scripts/genesis.sh

This file was deleted.

10 changes: 5 additions & 5 deletions h_docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ docker push rsantev/hydra-client:<commit-tag>

Repeat this and push it with the tag `latest`.

4. Build hydragon devnet (or testnet) image
4. Build hydragon docker compose image

```
docker build --platform linux/amd64 -t rsantev/hydragon-devnet:latest ./h_devnet
docker build --platform linux/amd64 -t rsantev/hydragon-docker:latest ./docker/hydra_docker_compose
```

5. Push hydragon devnet image to DockerHub
5. Push to DockerHub

```
docker push rsantev/hydragon-devnet:latest
docker push rsantev/hydragon-docker:latest
```

### Build devnet cluster docker image

4. Build hydragon devnet cluster image

```
cd h_devnet/devnet_cluster \
cd docker/hydra_devnet_cluster \
docker build --platform linux/amd64 -t rsantev/devnet-cluster:latest .
```

Expand Down

0 comments on commit 58df7ac

Please sign in to comment.