Skip to content

Commit

Permalink
Configurable network configuration (#702)
Browse files Browse the repository at this point in the history
* Allow importing all network config fields

* Network config merge

* fix custom network test

* Test_CustomNetwork integrated as test case of TestLoadCustomNetworkConfig

* Default value for --network-base is internaltestnet
  • Loading branch information
fgimenez committed Jun 9, 2022
1 parent b0d1fad commit 6baf7a4
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 122 deletions.
4 changes: 2 additions & 2 deletions cmd/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func approveTokens(ctx *cli.Context) error {
a := ctx.String(flagAmount)
a := ctx.String(config.FlagAmount)
amount, _ := new(big.Float).SetString(a)
if amount == nil {
fmt.Println("Please, introduce a valid amount. Use '.' instead of ',' if it is a decimal number")
Expand All @@ -22,7 +22,7 @@ func approveTokens(ctx *cli.Context) error {
return err
}

if !ctx.Bool(flagYes) {
if !ctx.Bool(config.FlagYes) {
fmt.Print("*WARNING* Are you sure you want to approve ", amount,
" tokens to be spent by the smc <Name: PoE. Address: "+c.NetworkConfig.PoEAddr.String()+">? [y/N]: ")
var input string
Expand Down
31 changes: 15 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@ package main
import (
"os"

"github.com/hermeznetwork/hermez-core/config"
"github.com/hermeznetwork/hermez-core/log"
"github.com/urfave/cli/v2"
)

const (
flagYes = "yes"
flagCfg = "cfg"
flagNetwork = "network"
flagNetworkCfg = "network-cfg"
flagAmount = "amount"
flagRemoteMT = "remote-merkletree"
)

const (
// App name
appName = "hermez-node"
Expand All @@ -33,30 +25,37 @@ func main() {
app.Version = version
flags := []cli.Flag{
&cli.StringFlag{
Name: flagCfg,
Name: config.FlagCfg,
Aliases: []string{"c"},
Usage: "Configuration `FILE`",
Required: false,
},
&cli.StringFlag{
Name: flagNetwork,
Name: config.FlagNetwork,
Aliases: []string{"n"},
Usage: "Network: mainnet, testnet, internaltestnet, local. By default it uses mainnet",
Usage: "Network: mainnet, testnet, internaltestnet, local, custom, merge. By default it uses mainnet",
Required: false,
},
&cli.StringFlag{
Name: flagNetworkCfg,
Name: config.FlagNetworkCfg,
Aliases: []string{"nc"},
Usage: "Custom network configuration `FILE` when using --network custom parameter",
},
&cli.StringFlag{
Name: config.FlagNetworkBase,
Aliases: []string{"nb"},
Usage: "Base existing network configuration to be merged with the custom configuration passed with --network-cfg, by default it uses internaltestnet",
Value: "internaltestnet",
Required: false,
},
&cli.BoolFlag{
Name: flagYes,
Name: config.FlagYes,
Aliases: []string{"y"},
Usage: "Automatically accepts any confirmation to execute the command",
Required: false,
},
&cli.BoolFlag{
Name: flagRemoteMT,
Name: config.FlagRemoteMT,
Aliases: []string{"mt"},
Usage: "Connect to merkletree service instead of use local libraries",
Required: false,
Expand Down Expand Up @@ -89,7 +88,7 @@ func main() {
Usage: "Approve tokens to be spent by the smart contract",
Action: approveTokens,
Flags: append(flags, &cli.StringFlag{
Name: flagAmount,
Name: config.FlagAmount,
Aliases: []string{"am"},
Usage: "Amount that is gonna be approved",
Required: true,
Expand Down
4 changes: 2 additions & 2 deletions cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func registerSequencer(ctx *cli.Context) error {
url := ctx.Args().First()
var input string
if !ctx.Bool(flagYes) {
if !ctx.Bool(config.FlagYes) {
fmt.Print("*WARNING* Are you sure you want to register " +
"the sequencer in the rollup using the domain <" + url + ">? [y/N]: ")
if _, err := fmt.Scanln(&input); err != nil {
Expand Down Expand Up @@ -78,7 +78,7 @@ func registerSequencer(ctx *cli.Context) error {
}

// If Sequencer exists in the db
if !ctx.Bool(flagYes) {
if !ctx.Bool(config.FlagYes) {
fmt.Print("*WARNING* Sequencer is already registered. Do you want to update " +
"the sequencer url in the rollup usign the domain <" + url + ">? [y/N]: ")
if _, err := fmt.Scanln(&input); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func start(ctx *cli.Context) error {
grpcClientConns []*grpc.ClientConn
cancelFuncs []context.CancelFunc
)
if ctx.Bool(flagRemoteMT) {
if ctx.Bool(config.FlagRemoteMT) {
log.Debugf("running with remote MT")
srvCfg := &tree.ServerConfig{
Host: c.MTServer.Host,
Expand Down
12 changes: 8 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ import (
)

const (
flagCfg = "cfg"
flagNetwork = "network"
flagNetworkCfg = "network-cfg"
FlagYes = "yes"
FlagCfg = "cfg"
FlagNetwork = "network"
FlagNetworkCfg = "network-cfg"
FlagNetworkBase = "network-base"
FlagAmount = "amount"
FlagRemoteMT = "remote-merkletree"
)

// Config represents the configuration of the entire Hermez Node
Expand Down Expand Up @@ -56,7 +60,7 @@ func Load(ctx *cli.Context) (*Config, error) {
if err != nil {
return nil, err
}
configFilePath := ctx.String(flagCfg)
configFilePath := ctx.String(FlagCfg)
if configFilePath != "" {
dirName, fileName := filepath.Split(configFilePath)

Expand Down
46 changes: 0 additions & 46 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import (
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-core/config"
"github.com/hermeznetwork/hermez-core/encoding"
"github.com/hermeznetwork/hermez-core/pricegetter"
"github.com/hermeznetwork/hermez-core/sequencer"
"github.com/hermeznetwork/hermez-core/state/tree"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -96,49 +93,6 @@ func Test_Defaults(t *testing.T) {
}
}

func Test_CustomNetwork(t *testing.T) {
var err error

app := cli.NewApp()
var n string
flag.StringVar(&n, "network", "custom", "")
var nc string
flag.StringVar(&nc, "network-cfg", "./network-config.example.json", "")
ctx := cli.NewContext(app, flag.CommandLine, nil)

cfg, err := config.Load(ctx)
require.NoError(t, err)

assert.Equal(t, uint8(4), cfg.NetworkConfig.Arity)
assert.Equal(t, uint64(1), cfg.NetworkConfig.GenBlockNumber)
assert.Equal(t, common.HexToAddress("0xCF7ED3ACCA5A467E9E704C703E8D87F634FB0FC9").Hex(), cfg.NetworkConfig.PoEAddr.Hex())
assert.Equal(t, common.HexToAddress("0x37AFFAF737C3683AB73F6E1B0933B725AB9796AA").Hex(), cfg.NetworkConfig.MaticAddr.Hex())
assert.Equal(t, uint64(1337), cfg.NetworkConfig.L1ChainID)
assert.Equal(t, uint64(1000), cfg.NetworkConfig.L2DefaultChainID)
assert.Equal(t, uint64(123456), cfg.NetworkConfig.MaxCumulativeGasUsed)

assert.Equal(t, 3, len(cfg.NetworkConfig.Genesis.Balances))

assertBalance := func(t *testing.T, a, b string) {
balance, ok := big.NewInt(0).SetString(b, encoding.Base10)
assert.True(t, ok)

addr := common.HexToAddress(a)
balanceFound, found := cfg.NetworkConfig.Genesis.Balances[addr]
assert.True(t, found)

if !found {
return
}

assert.Equal(t, 0, balance.Cmp(balanceFound))
}

assertBalance(t, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", "1000000000000000000000")
assertBalance(t, "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", "2000000000000000000000")
assertBalance(t, "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", "3000000000000000000000")
}

func getValueFromStruct(path string, object interface{}) interface{} {
keySlice := strings.Split(path, ".")
v := reflect.ValueOf(object)
Expand Down
21 changes: 0 additions & 21 deletions config/network-config.example.json

This file was deleted.

Loading

0 comments on commit 6baf7a4

Please sign in to comment.