Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --chain dev option #1561

Merged
merged 24 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions chain/dev/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[global]
basepath = "~/.gossamer/dev"
log = "info"
metrics-port = 9876

[log]
core = ""
network = ""
rpc = ""
state = ""
runtime = ""
babe = ""
grandpa = ""
sync = ""

[init]
genesis = "./chain/dev/genesis-spec.json"

[account]
key = "alice"
unlock = ""

[core]
roles = 4
babe-authority = true
grandpa-authority = true

[network]
port = 7001
nobootstrap = false
nomdns = false

[rpc]
enabled = true
ws = true
port = 8545
host = "localhost"
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
ws-port = 8546
92 changes: 92 additions & 0 deletions chain/dev/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package dev

import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"
)

var (
// GlobalConfig

// DefaultName is the node name
DefaultName = string("Gossamer")
// DefaultID is the chain ID
DefaultID = string("dev")
// DefaultConfig is the toml configuration path
DefaultConfig = string("./chain/dev/config.toml")
// DefaultBasePath is the node base directory path
DefaultBasePath = string("~/.gossamer/dev")

// DefaultMetricsPort is the metrics server port
DefaultMetricsPort = uint32(9876)

// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

// InitConfig

// DefaultGenesis is the default genesis configuration path
DefaultGenesis = string("./chain/dev/genesis-spec.json")

// AccountConfig

// DefaultKey is the default account key
DefaultKey = string("alice")
// DefaultUnlock is the account to unlock
DefaultUnlock = string("")

// CoreConfig

// DefaultAuthority is true if the node is a block producer and a grandpa authority
DefaultAuthority = true
// DefaultRoles Default node roles
DefaultRoles = byte(4) // authority node (see Table D.2)
// DefaultBabeAuthority is true if the node is a block producer (overwrites previous settings)
DefaultBabeAuthority = true
// DefaultGrandpaAuthority is true if the node is a grandpa authority (overwrites previous settings)
DefaultGrandpaAuthority = true
// DefaultWasmInterpreter is the name of the wasm interpreter to use by default
DefaultWasmInterpreter = wasmer.Name

// NetworkConfig

// DefaultNetworkPort network port
DefaultNetworkPort = uint32(7001)
// DefaultNetworkBootnodes network bootnodes
DefaultNetworkBootnodes = []string(nil)
// DefaultNoBootstrap disables bootstrap
DefaultNoBootstrap = false
// DefaultNoMDNS disables mDNS discovery
DefaultNoMDNS = false

// RPCConfig

// DefaultRPCHTTPHost rpc host
DefaultRPCHTTPHost = string("localhost")
// DefaultRPCHTTPPort rpc port
DefaultRPCHTTPPort = uint32(8545)
// DefaultRPCModules rpc modules
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa"}
// DefaultRPCWSPort rpc websocket port
DefaultRPCWSPort = uint32(8546)
// DefaultRPCEnabled enables the RPC server
DefaultRPCEnabled = true
// DefaultWSEnabled enables the WS server
DefaultWSEnabled = true
)
111 changes: 111 additions & 0 deletions chain/dev/genesis-spec.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ var (
defaultGssmrConfigPath = "./chain/gssmr/config.toml"
defaultKusamaConfigPath = "./chain/kusama/config.toml"
defaultPolkadotConfigPath = "./chain/polkadot/config.toml"
defaultDevConfigPath = "./chain/dev/config.toml"

gossamerName = "gssmr"
kusamaName = "kusama"
polkadotName = "polkadot"
devName = "dev"
)

// loadConfigFile loads a default config file if --chain is specified, a specific
Expand Down Expand Up @@ -100,6 +102,11 @@ func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error)
tomlCfg = &ctoml.Config{}
cfg = dot.PolkadotConfig()
err = loadConfig(tomlCfg, defaultPolkadotConfigPath)
case devName:
logger.Info("loading toml configuration...", "config path", defaultDevConfigPath)
tomlCfg = &ctoml.Config{}
cfg = dot.DevConfig()
err = loadConfig(tomlCfg, defaultDevConfigPath)
default:
return nil, nil, fmt.Errorf("unknown chain id provided: %s", id)
}
Expand Down Expand Up @@ -572,11 +579,6 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC
cfg.GrandpaAuthority = false
}

if tomlCfg.BabeThresholdDenominator != 0 {
cfg.BabeThresholdDenominator = tomlCfg.BabeThresholdDenominator
cfg.BabeThresholdNumerator = tomlCfg.BabeThresholdNumerator
}

switch tomlCfg.WasmInterpreter {
case wasmer.Name:
cfg.WasmInterpreter = wasmer.Name
Expand All @@ -596,8 +598,6 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC
"babe-authority", cfg.BabeAuthority,
"grandpa-authority", cfg.GrandpaAuthority,
"epoch-length", cfg.EpochLength,
"babe-threshold-numerator", cfg.BabeThresholdNumerator,
"babe-threshold-denominator", cfg.BabeThresholdDenominator,
"wasm-interpreter", cfg.WasmInterpreter,
)
}
Expand Down Expand Up @@ -672,7 +672,7 @@ func setDotRPCConfig(ctx *cli.Context, tomlCfg ctoml.RPCConfig, cfg *dot.RPCConf
cfg.WSExternal = tomlCfg.WSExternal

// check --rpc flag and update node configuration
if enabled := ctx.GlobalBool(RPCEnabledFlag.Name); enabled {
if enabled := ctx.GlobalBool(RPCEnabledFlag.Name); enabled || cfg.Enabled {
cfg.Enabled = true
} else if ctx.IsSet(RPCEnabledFlag.Name) && !enabled {
cfg.Enabled = false
Expand Down Expand Up @@ -706,7 +706,7 @@ func setDotRPCConfig(ctx *cli.Context, tomlCfg ctoml.RPCConfig, cfg *dot.RPCConf
cfg.WSPort = uint32(wsport)
}

if WS := ctx.GlobalBool(WSFlag.Name); WS {
if WS := ctx.GlobalBool(WSFlag.Name); WS || cfg.WS {
cfg.WS = true
} else if ctx.IsSet(WSFlag.Name) && !WS {
cfg.WS = false
Expand Down
13 changes: 6 additions & 7 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func TestConfigFromChainFlag(t *testing.T) {
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name},
dot.PolkadotConfig(),
},
{
"Test gossamer --chain dev",
[]string{"chain", "name"},
[]interface{}{"dev", dot.DevConfig().Global.Name},
dot.DevConfig(),
},
}

for _, c := range testcases {
Expand Down Expand Up @@ -751,9 +757,6 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {
System: testCfg.System,
}

expected.Core.BabeThresholdNumerator = 0
expected.Core.BabeThresholdDenominator = 0

cfg, err := createDotConfig(ctx)
require.Nil(t, err)
updateDotConfigFromGenesisJSONRaw(*dotConfigToToml(testCfg), cfg)
Expand Down Expand Up @@ -812,8 +815,6 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
require.Nil(t, err)

cfg.Init.Genesis = genFile.Name()
expected.Core.BabeThresholdNumerator = 0
expected.Core.BabeThresholdDenominator = 0

db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: cfg.Global.BasePath,
Expand Down Expand Up @@ -851,8 +852,6 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) {
cfg.Core.Roles = types.FullNodeRole
cfg.Core.BabeAuthority = false
cfg.Core.GrandpaAuthority = false
cfg.Core.BabeThresholdNumerator = 0
cfg.Core.BabeThresholdDenominator = 0
cfg.Init.Genesis = genPath

err := dot.InitNode(cfg)
Expand Down
12 changes: 5 additions & 7 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
}

cfg.Core = ctoml.CoreConfig{
Roles: dcfg.Core.Roles,
BabeAuthority: dcfg.Core.BabeAuthority,
GrandpaAuthority: dcfg.Core.GrandpaAuthority,
EpochLength: dcfg.Core.EpochLength,
BabeThresholdNumerator: dcfg.Core.BabeThresholdNumerator,
BabeThresholdDenominator: dcfg.Core.BabeThresholdDenominator,
SlotDuration: dcfg.Core.SlotDuration,
Roles: dcfg.Core.Roles,
BabeAuthority: dcfg.Core.BabeAuthority,
GrandpaAuthority: dcfg.Core.GrandpaAuthority,
EpochLength: dcfg.Core.EpochLength,
SlotDuration: dcfg.Core.SlotDuration,
}

cfg.Network = ctoml.NetworkConfig{
Expand Down
1 change: 1 addition & 0 deletions cmd/gossamer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func TestMain(m *testing.M) {
defaultGssmrConfigPath = "../../chain/gssmr/config.toml"
defaultKusamaConfigPath = "../../chain/kusama/config.toml"
defaultPolkadotConfigPath = "../../chain/polkadot/config.toml"
defaultDevConfigPath = "../../chain/dev/config.toml"
os.Exit(m.Run())
}

Expand Down
65 changes: 57 additions & 8 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dot
import (
"encoding/json"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/chain/kusama"
"github.com/ChainSafe/gossamer/chain/polkadot"
Expand Down Expand Up @@ -90,14 +91,12 @@ type NetworkConfig struct {

// CoreConfig is to marshal/unmarshal toml core config vars
type CoreConfig struct {
Roles byte
BabeAuthority bool
GrandpaAuthority bool
BabeThresholdNumerator uint64
BabeThresholdDenominator uint64
SlotDuration uint64
EpochLength uint64
WasmInterpreter string
Roles byte
BabeAuthority bool
GrandpaAuthority bool
SlotDuration uint64
EpochLength uint64
WasmInterpreter string
}

// RPCConfig is to marshal/unmarshal toml RPC config vars
Expand Down Expand Up @@ -266,3 +265,53 @@ func PolkadotConfig() *Config {
},
}
}

// DevConfig returns the configuration for a development chain
func DevConfig() *Config {
return &Config{
Global: GlobalConfig{
Name: dev.DefaultName,
ID: dev.DefaultID,
BasePath: dev.DefaultBasePath,
LogLvl: dev.DefaultLvl,
MetricsPort: dev.DefaultMetricsPort,
},
Log: LogConfig{
CoreLvl: dev.DefaultLvl,
SyncLvl: dev.DefaultLvl,
NetworkLvl: dev.DefaultLvl,
RPCLvl: dev.DefaultLvl,
StateLvl: dev.DefaultLvl,
RuntimeLvl: dev.DefaultLvl,
BlockProducerLvl: dev.DefaultLvl,
FinalityGadgetLvl: dev.DefaultLvl,
},
Init: InitConfig{
Genesis: dev.DefaultGenesis,
},
Account: AccountConfig{
Key: dev.DefaultKey,
Unlock: dev.DefaultUnlock,
},
Core: CoreConfig{
Roles: dev.DefaultRoles,
BabeAuthority: dev.DefaultBabeAuthority,
GrandpaAuthority: dev.DefaultGrandpaAuthority,
WasmInterpreter: dev.DefaultWasmInterpreter,
},
Network: NetworkConfig{
Port: dev.DefaultNetworkPort,
Bootnodes: dev.DefaultNetworkBootnodes,
NoBootstrap: dev.DefaultNoBootstrap,
NoMDNS: dev.DefaultNoMDNS,
},
RPC: RPCConfig{
Port: dev.DefaultRPCHTTPPort,
Host: dev.DefaultRPCHTTPHost,
Modules: dev.DefaultRPCModules,
WSPort: dev.DefaultRPCWSPort,
Enabled: dev.DefaultRPCEnabled,
WS: dev.DefaultWSEnabled,
},
}
}
14 changes: 6 additions & 8 deletions dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ type NetworkConfig struct {

// CoreConfig is to marshal/unmarshal toml core config vars
type CoreConfig struct {
Roles byte `toml:"roles,omitempty"`
BabeAuthority bool `toml:"babe-authority"`
GrandpaAuthority bool `toml:"grandpa-authority"`
BabeThresholdNumerator uint64 `toml:"babe-threshold-numerator,omitempty"`
BabeThresholdDenominator uint64 `toml:"babe-threshold-denominator,omitempty"`
SlotDuration uint64 `toml:"slot-duration,omitempty"`
EpochLength uint64 `toml:"epoch-length,omitempty"`
WasmInterpreter string `toml:"wasm-interpreter,omitempty"`
Roles byte `toml:"roles,omitempty"`
BabeAuthority bool `toml:"babe-authority"`
GrandpaAuthority bool `toml:"grandpa-authority"`
SlotDuration uint64 `toml:"slot-duration,omitempty"`
EpochLength uint64 `toml:"epoch-length,omitempty"`
WasmInterpreter string `toml:"wasm-interpreter,omitempty"`
}

// RPCConfig is to marshal/unmarshal toml RPC config vars
Expand Down
7 changes: 1 addition & 6 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func InitNode(cfg *Config) error {
// create new state service
stateSrvc := state.NewService(cfg.Global.BasePath, cfg.Global.LogLvl)

if cfg.Core.BabeThresholdDenominator != 0 {
stateSrvc.BabeThresholdNumerator = cfg.Core.BabeThresholdNumerator
stateSrvc.BabeThresholdDenominator = cfg.Core.BabeThresholdDenominator
}

// initialise state service with genesis data, block, and trie
err = stateSrvc.Initialise(gen, header, t)
if err != nil {
Expand Down Expand Up @@ -164,7 +159,7 @@ func NodeInitialized(basepath string, expected bool) bool {
// load genesis data from initialised node database
_, err = state.NewBaseState(db).LoadGenesisData()
if err != nil {
logger.Warn(
logger.Debug(
"node has not been initialised",
"basepath", basepath,
"error", err,
Expand Down
Loading