Skip to content

Commit

Permalink
fix(lib/babe): add --babe-lead flag, update epoch handling logic (C…
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored Oct 21, 2021
1 parent 5e42215 commit 7abcce6
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 60 deletions.
1 change: 1 addition & 0 deletions chain/dev/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ unlock = ""
roles = 4
babe-authority = true
grandpa-authority = true
babe-lead = true

[network]
port = 7001
Expand Down
1 change: 1 addition & 0 deletions chain/gssmr/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ port = 7001
nobootstrap = false
nomdns = false
discovery-interval = 10
min-peers = 1

[rpc]
enabled = false
Expand Down
2 changes: 2 additions & 0 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ var (
DefaultNoBootstrap = false
// DefaultNoMDNS disables mDNS discovery
DefaultNoMDNS = false
// DefaultMinPeers is the default minimum desired peer count
DefaultMinPeers = 1

// DefaultDiscoveryInterval is the default interval for searching for DHT peers
DefaultDiscoveryInterval = time.Second * 10
Expand Down
1 change: 1 addition & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC
cfg.Roles = tomlCfg.Roles
cfg.BabeAuthority = tomlCfg.Roles == types.AuthorityRole
cfg.GrandpaAuthority = tomlCfg.Roles == types.AuthorityRole
cfg.BABELead = ctx.GlobalBool(BABELeadFlag.Name)

// check --roles flag and update node configuration
if roles := ctx.GlobalString(RolesFlag.Name); roles != "" {
Expand Down
6 changes: 6 additions & 0 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
MinPeers: testCfg.Network.MinPeers,
},
},
{
Expand All @@ -403,6 +404,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
MinPeers: testCfg.Network.MinPeers,
},
},
{
Expand All @@ -416,6 +418,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
MinPeers: testCfg.Network.MinPeers,
},
},
{
Expand All @@ -429,6 +432,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: true,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
MinPeers: testCfg.Network.MinPeers,
},
},
{
Expand All @@ -442,6 +446,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: true,
DiscoveryInterval: time.Second * 10,
MinPeers: testCfg.Network.MinPeers,
},
},
}
Expand Down Expand Up @@ -816,6 +821,7 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
MinPeers: testCfg.Network.MinPeers,
},
RPC: testCfg.RPC,
System: testCfg.System,
Expand Down
2 changes: 2 additions & 0 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
NoBootstrap: dcfg.Network.NoBootstrap,
NoMDNS: dcfg.Network.NoMDNS,
DiscoveryInterval: int(dcfg.Network.DiscoveryInterval / time.Second),
MinPeers: dcfg.Network.MinPeers,
MaxPeers: dcfg.Network.MaxPeers,
}

cfg.RPC = ctoml.RPCConfig{
Expand Down
3 changes: 3 additions & 0 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestExportCommand(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
MinPeers: testCfg.Network.MinPeers,
},
RPC: testCfg.RPC,
},
Expand Down Expand Up @@ -119,6 +120,7 @@ func TestExportCommand(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
MinPeers: testCfg.Network.MinPeers,
},
RPC: testCfg.RPC,
},
Expand Down Expand Up @@ -151,6 +153,7 @@ func TestExportCommand(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: testCfg.Network.DiscoveryInterval,
MinPeers: testCfg.Network.MinPeers,
},
RPC: testCfg.RPC,
},
Expand Down
11 changes: 11 additions & 0 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ var (
}
)

// BABE flags
var (
BABELeadFlag = cli.BoolFlag{
Name: "babe-lead",
Usage: `specify whether node should build block 1 of the network. only used when starting a new network`,
}
)

// flag sets that are shared by multiple commands
var (
// GlobalFlags are flags that are valid for use with the root command and all subcommands
Expand Down Expand Up @@ -366,6 +374,9 @@ var (

// telemetry flags
NoTelemetryFlag,

// BABE flags
BABELeadFlag,
}
)

Expand Down
2 changes: 2 additions & 0 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type NetworkConfig struct {
type CoreConfig struct {
Roles byte
BabeAuthority bool
BABELead bool
GrandpaAuthority bool
WasmInterpreter string
}
Expand Down Expand Up @@ -183,6 +184,7 @@ func GssmrConfig() *Config {
NoBootstrap: gssmr.DefaultNoBootstrap,
NoMDNS: gssmr.DefaultNoMDNS,
DiscoveryInterval: gssmr.DefaultDiscoveryInterval,
MinPeers: gssmr.DefaultMinPeers,
},
RPC: RPCConfig{
Port: gssmr.DefaultRPCHTTPPort,
Expand Down
1 change: 1 addition & 0 deletions dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type CoreConfig struct {
SlotDuration uint64 `toml:"slot-duration,omitempty"`
EpochLength uint64 `toml:"epoch-length,omitempty"`
WasmInterpreter string `toml:"wasm-interpreter,omitempty"`
BABELead bool `toml:"babe-lead,omitempty"`
}

// RPCConfig is to marshal/unmarshal toml RPC config vars
Expand Down
31 changes: 1 addition & 30 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,6 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
return err
}

// check if block production epoch transitioned
if err := s.handleCurrentSlot(&block.Header); err != nil {
logger.Warn("failed to handle epoch for block", "block", block.Header.Hash(), "error", err)
return err
}

go func() {
s.Lock()
defer s.Unlock()
Expand Down Expand Up @@ -313,29 +307,6 @@ func (s *Service) handleCodeSubstitution(hash common.Hash, state *rtstorage.Trie
return nil
}

func (s *Service) handleCurrentSlot(header *types.Header) error {
head := s.blockState.BestBlockHash()
if header.Hash() != head {
return nil
}

epoch, err := s.epochState.GetEpochForBlock(header)
if err != nil {
return err
}

currEpoch, err := s.epochState.GetCurrentEpoch()
if err != nil {
return err
}

if currEpoch == epoch {
return nil
}

return s.epochState.SetCurrentEpoch(epoch)
}

// handleBlocksAsync handles a block asynchronously; the handling performed by this function
// does not need to be completed before the next block can be imported.
func (s *Service) handleBlocksAsync() {
Expand Down Expand Up @@ -399,7 +370,7 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
}

for _, ext := range *body {
logger.Info("validating transaction on re-org chain", "extrinsic", ext)
logger.Trace("validating transaction on re-org chain", "extrinsic", ext)
encExt, err := scale.Marshal(ext)
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,6 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
}
nodeSrvcs = append(nodeSrvcs, coreSrvc)

bp, err := createBABEService(cfg, stateSrvc, ks.Babe, coreSrvc)
if err != nil {
return nil, err
}
nodeSrvcs = append(nodeSrvcs, bp)

fg, err := createGRANDPAService(cfg, stateSrvc, dh, ks.Gran, networkSrvc)
if err != nil {
return nil, err
Expand All @@ -305,6 +299,12 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
}
nodeSrvcs = append(nodeSrvcs, syncer)

bp, err := createBABEService(cfg, stateSrvc, ks.Babe, coreSrvc)
if err != nil {
return nil, err
}
nodeSrvcs = append(nodeSrvcs, bp)

sysSrvc, err := createSystemService(&cfg.System, stateSrvc)
if err != nil {
return nil, fmt.Errorf("failed to create system service: %s", err)
Expand Down
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func createBABEService(cfg *Config, st *state.Service, ks keystore.Keystore, cs
BlockImportHandler: cs,
Authority: cfg.Core.BabeAuthority,
IsDev: cfg.Global.ID == "dev",
Lead: cfg.Core.BABELead,
}

if cfg.Core.BabeAuthority {
Expand Down
Loading

0 comments on commit 7abcce6

Please sign in to comment.