Skip to content

Commit 04a2969

Browse files
authored
feat: add --chain dev option (#1561)
1 parent d445c97 commit 04a2969

29 files changed

+565
-199
lines changed

chain/dev/config.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[global]
2+
basepath = "~/.gossamer/dev"
3+
log = "info"
4+
metrics-port = 9876
5+
6+
[log]
7+
core = ""
8+
network = ""
9+
rpc = ""
10+
state = ""
11+
runtime = ""
12+
babe = ""
13+
grandpa = ""
14+
sync = ""
15+
16+
[init]
17+
genesis = "./chain/dev/genesis-spec.json"
18+
19+
[account]
20+
key = "alice"
21+
unlock = ""
22+
23+
[core]
24+
roles = 4
25+
babe-authority = true
26+
grandpa-authority = true
27+
28+
[network]
29+
port = 7001
30+
nobootstrap = false
31+
nomdns = false
32+
33+
[rpc]
34+
enabled = true
35+
ws = true
36+
port = 8545
37+
host = "localhost"
38+
modules = ["system", "author", "chain", "state", "rpc", "grandpa"]
39+
ws-port = 8546

chain/dev/defaults.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2019 ChainSafe Systems (ON) Corp.
2+
// This file is part of gossamer.
3+
//
4+
// The gossamer library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The gossamer library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package dev
18+
19+
import (
20+
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
21+
log "github.com/ChainSafe/log15"
22+
)
23+
24+
var (
25+
// GlobalConfig
26+
27+
// DefaultName is the node name
28+
DefaultName = string("Gossamer")
29+
// DefaultID is the chain ID
30+
DefaultID = string("dev")
31+
// DefaultConfig is the toml configuration path
32+
DefaultConfig = string("./chain/dev/config.toml")
33+
// DefaultBasePath is the node base directory path
34+
DefaultBasePath = string("~/.gossamer/dev")
35+
36+
// DefaultMetricsPort is the metrics server port
37+
DefaultMetricsPort = uint32(9876)
38+
39+
// DefaultLvl is the default log level
40+
DefaultLvl = log.LvlInfo
41+
42+
// InitConfig
43+
44+
// DefaultGenesis is the default genesis configuration path
45+
DefaultGenesis = string("./chain/dev/genesis-spec.json")
46+
47+
// AccountConfig
48+
49+
// DefaultKey is the default account key
50+
DefaultKey = string("alice")
51+
// DefaultUnlock is the account to unlock
52+
DefaultUnlock = string("")
53+
54+
// CoreConfig
55+
56+
// DefaultAuthority is true if the node is a block producer and a grandpa authority
57+
DefaultAuthority = true
58+
// DefaultRoles Default node roles
59+
DefaultRoles = byte(4) // authority node (see Table D.2)
60+
// DefaultBabeAuthority is true if the node is a block producer (overwrites previous settings)
61+
DefaultBabeAuthority = true
62+
// DefaultGrandpaAuthority is true if the node is a grandpa authority (overwrites previous settings)
63+
DefaultGrandpaAuthority = true
64+
// DefaultWasmInterpreter is the name of the wasm interpreter to use by default
65+
DefaultWasmInterpreter = wasmer.Name
66+
67+
// NetworkConfig
68+
69+
// DefaultNetworkPort network port
70+
DefaultNetworkPort = uint32(7001)
71+
// DefaultNetworkBootnodes network bootnodes
72+
DefaultNetworkBootnodes = []string(nil)
73+
// DefaultNoBootstrap disables bootstrap
74+
DefaultNoBootstrap = false
75+
// DefaultNoMDNS disables mDNS discovery
76+
DefaultNoMDNS = false
77+
78+
// RPCConfig
79+
80+
// DefaultRPCHTTPHost rpc host
81+
DefaultRPCHTTPHost = string("localhost")
82+
// DefaultRPCHTTPPort rpc port
83+
DefaultRPCHTTPPort = uint32(8545)
84+
// DefaultRPCModules rpc modules
85+
DefaultRPCModules = []string{"system", "author", "chain", "state", "rpc", "grandpa"}
86+
// DefaultRPCWSPort rpc websocket port
87+
DefaultRPCWSPort = uint32(8546)
88+
// DefaultRPCEnabled enables the RPC server
89+
DefaultRPCEnabled = true
90+
// DefaultWSEnabled enables the WS server
91+
DefaultWSEnabled = true
92+
)

chain/dev/genesis-spec.json

Lines changed: 111 additions & 0 deletions
Large diffs are not rendered by default.

cmd/gossamer/config.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ var (
4444
defaultGssmrConfigPath = "./chain/gssmr/config.toml"
4545
defaultKusamaConfigPath = "./chain/kusama/config.toml"
4646
defaultPolkadotConfigPath = "./chain/polkadot/config.toml"
47+
defaultDevConfigPath = "./chain/dev/config.toml"
4748

4849
gossamerName = "gssmr"
4950
kusamaName = "kusama"
5051
polkadotName = "polkadot"
52+
devName = "dev"
5153
)
5254

5355
// loadConfigFile loads a default config file if --chain is specified, a specific
@@ -100,6 +102,11 @@ func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error)
100102
tomlCfg = &ctoml.Config{}
101103
cfg = dot.PolkadotConfig()
102104
err = loadConfig(tomlCfg, defaultPolkadotConfigPath)
105+
case devName:
106+
logger.Info("loading toml configuration...", "config path", defaultDevConfigPath)
107+
tomlCfg = &ctoml.Config{}
108+
cfg = dot.DevConfig()
109+
err = loadConfig(tomlCfg, defaultDevConfigPath)
103110
default:
104111
return nil, nil, fmt.Errorf("unknown chain id provided: %s", id)
105112
}
@@ -572,11 +579,6 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC
572579
cfg.GrandpaAuthority = false
573580
}
574581

575-
if tomlCfg.BabeThresholdDenominator != 0 {
576-
cfg.BabeThresholdDenominator = tomlCfg.BabeThresholdDenominator
577-
cfg.BabeThresholdNumerator = tomlCfg.BabeThresholdNumerator
578-
}
579-
580582
switch tomlCfg.WasmInterpreter {
581583
case wasmer.Name:
582584
cfg.WasmInterpreter = wasmer.Name
@@ -596,8 +598,6 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC
596598
"babe-authority", cfg.BabeAuthority,
597599
"grandpa-authority", cfg.GrandpaAuthority,
598600
"epoch-length", cfg.EpochLength,
599-
"babe-threshold-numerator", cfg.BabeThresholdNumerator,
600-
"babe-threshold-denominator", cfg.BabeThresholdDenominator,
601601
"wasm-interpreter", cfg.WasmInterpreter,
602602
)
603603
}
@@ -672,7 +672,7 @@ func setDotRPCConfig(ctx *cli.Context, tomlCfg ctoml.RPCConfig, cfg *dot.RPCConf
672672
cfg.WSExternal = tomlCfg.WSExternal
673673

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

709-
if WS := ctx.GlobalBool(WSFlag.Name); WS {
709+
if WS := ctx.GlobalBool(WSFlag.Name); WS || cfg.WS {
710710
cfg.WS = true
711711
} else if ctx.IsSet(WSFlag.Name) && !WS {
712712
cfg.WS = false

cmd/gossamer/config_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func TestConfigFromChainFlag(t *testing.T) {
6262
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name},
6363
dot.PolkadotConfig(),
6464
},
65+
{
66+
"Test gossamer --chain dev",
67+
[]string{"chain", "name"},
68+
[]interface{}{"dev", dot.DevConfig().Global.Name},
69+
dot.DevConfig(),
70+
},
6571
}
6672

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

754-
expected.Core.BabeThresholdNumerator = 0
755-
expected.Core.BabeThresholdDenominator = 0
756-
757760
cfg, err := createDotConfig(ctx)
758761
require.Nil(t, err)
759762
updateDotConfigFromGenesisJSONRaw(*dotConfigToToml(testCfg), cfg)
@@ -812,8 +815,6 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
812815
require.Nil(t, err)
813816

814817
cfg.Init.Genesis = genFile.Name()
815-
expected.Core.BabeThresholdNumerator = 0
816-
expected.Core.BabeThresholdDenominator = 0
817818

818819
db, err := chaindb.NewBadgerDB(&chaindb.Config{
819820
DataDir: cfg.Global.BasePath,
@@ -851,8 +852,6 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) {
851852
cfg.Core.Roles = types.FullNodeRole
852853
cfg.Core.BabeAuthority = false
853854
cfg.Core.GrandpaAuthority = false
854-
cfg.Core.BabeThresholdNumerator = 0
855-
cfg.Core.BabeThresholdDenominator = 0
856855
cfg.Init.Genesis = genPath
857856

858857
err := dot.InitNode(cfg)

cmd/gossamer/export.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
107107
}
108108

109109
cfg.Core = ctoml.CoreConfig{
110-
Roles: dcfg.Core.Roles,
111-
BabeAuthority: dcfg.Core.BabeAuthority,
112-
GrandpaAuthority: dcfg.Core.GrandpaAuthority,
113-
EpochLength: dcfg.Core.EpochLength,
114-
BabeThresholdNumerator: dcfg.Core.BabeThresholdNumerator,
115-
BabeThresholdDenominator: dcfg.Core.BabeThresholdDenominator,
116-
SlotDuration: dcfg.Core.SlotDuration,
110+
Roles: dcfg.Core.Roles,
111+
BabeAuthority: dcfg.Core.BabeAuthority,
112+
GrandpaAuthority: dcfg.Core.GrandpaAuthority,
113+
EpochLength: dcfg.Core.EpochLength,
114+
SlotDuration: dcfg.Core.SlotDuration,
117115
}
118116

119117
cfg.Network = ctoml.NetworkConfig{

cmd/gossamer/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func TestMain(m *testing.M) {
214214
defaultGssmrConfigPath = "../../chain/gssmr/config.toml"
215215
defaultKusamaConfigPath = "../../chain/kusama/config.toml"
216216
defaultPolkadotConfigPath = "../../chain/polkadot/config.toml"
217+
defaultDevConfigPath = "../../chain/dev/config.toml"
217218
os.Exit(m.Run())
218219
}
219220

dot/config.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package dot
1919
import (
2020
"encoding/json"
2121

22+
"github.com/ChainSafe/gossamer/chain/dev"
2223
"github.com/ChainSafe/gossamer/chain/gssmr"
2324
"github.com/ChainSafe/gossamer/chain/kusama"
2425
"github.com/ChainSafe/gossamer/chain/polkadot"
@@ -90,14 +91,12 @@ type NetworkConfig struct {
9091

9192
// CoreConfig is to marshal/unmarshal toml core config vars
9293
type CoreConfig struct {
93-
Roles byte
94-
BabeAuthority bool
95-
GrandpaAuthority bool
96-
BabeThresholdNumerator uint64
97-
BabeThresholdDenominator uint64
98-
SlotDuration uint64
99-
EpochLength uint64
100-
WasmInterpreter string
94+
Roles byte
95+
BabeAuthority bool
96+
GrandpaAuthority bool
97+
SlotDuration uint64
98+
EpochLength uint64
99+
WasmInterpreter string
101100
}
102101

103102
// RPCConfig is to marshal/unmarshal toml RPC config vars
@@ -266,3 +265,53 @@ func PolkadotConfig() *Config {
266265
},
267266
}
268267
}
268+
269+
// DevConfig returns the configuration for a development chain
270+
func DevConfig() *Config {
271+
return &Config{
272+
Global: GlobalConfig{
273+
Name: dev.DefaultName,
274+
ID: dev.DefaultID,
275+
BasePath: dev.DefaultBasePath,
276+
LogLvl: dev.DefaultLvl,
277+
MetricsPort: dev.DefaultMetricsPort,
278+
},
279+
Log: LogConfig{
280+
CoreLvl: dev.DefaultLvl,
281+
SyncLvl: dev.DefaultLvl,
282+
NetworkLvl: dev.DefaultLvl,
283+
RPCLvl: dev.DefaultLvl,
284+
StateLvl: dev.DefaultLvl,
285+
RuntimeLvl: dev.DefaultLvl,
286+
BlockProducerLvl: dev.DefaultLvl,
287+
FinalityGadgetLvl: dev.DefaultLvl,
288+
},
289+
Init: InitConfig{
290+
Genesis: dev.DefaultGenesis,
291+
},
292+
Account: AccountConfig{
293+
Key: dev.DefaultKey,
294+
Unlock: dev.DefaultUnlock,
295+
},
296+
Core: CoreConfig{
297+
Roles: dev.DefaultRoles,
298+
BabeAuthority: dev.DefaultBabeAuthority,
299+
GrandpaAuthority: dev.DefaultGrandpaAuthority,
300+
WasmInterpreter: dev.DefaultWasmInterpreter,
301+
},
302+
Network: NetworkConfig{
303+
Port: dev.DefaultNetworkPort,
304+
Bootnodes: dev.DefaultNetworkBootnodes,
305+
NoBootstrap: dev.DefaultNoBootstrap,
306+
NoMDNS: dev.DefaultNoMDNS,
307+
},
308+
RPC: RPCConfig{
309+
Port: dev.DefaultRPCHTTPPort,
310+
Host: dev.DefaultRPCHTTPHost,
311+
Modules: dev.DefaultRPCModules,
312+
WSPort: dev.DefaultRPCWSPort,
313+
Enabled: dev.DefaultRPCEnabled,
314+
WS: dev.DefaultWSEnabled,
315+
},
316+
}
317+
}

dot/config/toml/config.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ type NetworkConfig struct {
7373

7474
// CoreConfig is to marshal/unmarshal toml core config vars
7575
type CoreConfig struct {
76-
Roles byte `toml:"roles,omitempty"`
77-
BabeAuthority bool `toml:"babe-authority"`
78-
GrandpaAuthority bool `toml:"grandpa-authority"`
79-
BabeThresholdNumerator uint64 `toml:"babe-threshold-numerator,omitempty"`
80-
BabeThresholdDenominator uint64 `toml:"babe-threshold-denominator,omitempty"`
81-
SlotDuration uint64 `toml:"slot-duration,omitempty"`
82-
EpochLength uint64 `toml:"epoch-length,omitempty"`
83-
WasmInterpreter string `toml:"wasm-interpreter,omitempty"`
76+
Roles byte `toml:"roles,omitempty"`
77+
BabeAuthority bool `toml:"babe-authority"`
78+
GrandpaAuthority bool `toml:"grandpa-authority"`
79+
SlotDuration uint64 `toml:"slot-duration,omitempty"`
80+
EpochLength uint64 `toml:"epoch-length,omitempty"`
81+
WasmInterpreter string `toml:"wasm-interpreter,omitempty"`
8482
}
8583

8684
// RPCConfig is to marshal/unmarshal toml RPC config vars

dot/node.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ func InitNode(cfg *Config) error {
9393
// create new state service
9494
stateSrvc := state.NewService(cfg.Global.BasePath, cfg.Global.LogLvl)
9595

96-
if cfg.Core.BabeThresholdDenominator != 0 {
97-
stateSrvc.BabeThresholdNumerator = cfg.Core.BabeThresholdNumerator
98-
stateSrvc.BabeThresholdDenominator = cfg.Core.BabeThresholdDenominator
99-
}
100-
10196
// initialise state service with genesis data, block, and trie
10297
err = stateSrvc.Initialise(gen, header, t)
10398
if err != nil {
@@ -164,7 +159,7 @@ func NodeInitialized(basepath string, expected bool) bool {
164159
// load genesis data from initialised node database
165160
_, err = state.NewBaseState(db).LoadGenesisData()
166161
if err != nil {
167-
logger.Warn(
162+
logger.Debug(
168163
"node has not been initialised",
169164
"basepath", basepath,
170165
"error", err,

0 commit comments

Comments
 (0)