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

chore(lib/grandpa): make interval configurable #1903

Merged
merged 10 commits into from
Nov 4, 2021
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
grandpa-interval = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you put this under core instead of network, as it's consensus related?

qdm12 marked this conversation as resolved.
Show resolved Hide resolved

[rpc]
enabled = false
Expand Down
3 changes: 3 additions & 0 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ var (
// DefaultDiscoveryInterval is the default interval for searching for DHT peers
DefaultDiscoveryInterval = time.Second * 10

// DefaultGrandpaInterval
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// DefaultGrandpaInterval ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'd mention that this is the time for a grandpa sub-round

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this in there. Still working on understanding all of the polkadot pieces...

DefaultGrandpaInterval = time.Second

// RPCConfig

// DefaultRPCHTTPHost rpc host
Expand Down
7 changes: 7 additions & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,12 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
cfg.DiscoveryInterval = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be set to the default value if it's not set

}

if tomlCfg.GrandpaInterval > 0 {
cfg.GrandpaInterval = time.Second * time.Duration(tomlCfg.GrandpaInterval)
} else {
cfg.GrandpaInterval = 0
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, please move to core config

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless tomlCfg.GrandpaInterval can be negative, I think we can simplify to:

Suggested change
if tomlCfg.GrandpaInterval > 0 {
cfg.GrandpaInterval = time.Second * time.Duration(tomlCfg.GrandpaInterval)
} else {
cfg.GrandpaInterval = 0
}
cfg.GrandpaInterval = time.Second * time.Duration(tomlCfg.GrandpaInterval)


// check --port flag and update node configuration
if port := ctx.GlobalUint(PortFlag.Name); port != 0 {
cfg.Port = uint32(port)
Expand Down Expand Up @@ -676,6 +682,7 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
"maxpeers", cfg.MaxPeers,
"persistent-peers", cfg.PersistentPeers,
"discovery-interval", cfg.DiscoveryInterval,
"grandpa-interval", cfg.GrandpaInterval,
)
}

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,
GrandpaInterval: time.Second,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this value be replaced with testCfg.Network.GrandpaInterval ?
If so then do it at other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I have updated this.

},
},
{
Expand All @@ -403,6 +404,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
GrandpaInterval: time.Second,
},
},
{
Expand All @@ -416,6 +418,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
GrandpaInterval: time.Second,
},
},
{
Expand All @@ -429,6 +432,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: true,
NoMDNS: testCfg.Network.NoMDNS,
DiscoveryInterval: time.Second * 10,
GrandpaInterval: time.Second,
},
},
{
Expand All @@ -442,6 +446,7 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NoBootstrap: testCfg.Network.NoBootstrap,
NoMDNS: true,
DiscoveryInterval: time.Second * 10,
GrandpaInterval: time.Second,
},
},
}
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,
GrandpaInterval: testCfg.Network.GrandpaInterval,
},
RPC: testCfg.RPC,
System: testCfg.System,
Expand Down
1 change: 1 addition & 0 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
NoBootstrap: dcfg.Network.NoBootstrap,
NoMDNS: dcfg.Network.NoMDNS,
DiscoveryInterval: int(dcfg.Network.DiscoveryInterval / time.Second),
GrandpaInterval: int(dcfg.Network.GrandpaInterval / time.Second),
}

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,
GrandpaInterval: testCfg.Network.GrandpaInterval,
},
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,
GrandpaInterval: testCfg.Network.GrandpaInterval,
},
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,
GrandpaInterval: testCfg.Network.GrandpaInterval,
},
RPC: testCfg.RPC,
},
Expand Down
2 changes: 2 additions & 0 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type NetworkConfig struct {
MaxPeers int
PersistentPeers []string
DiscoveryInterval time.Duration
GrandpaInterval time.Duration
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down Expand Up @@ -185,6 +186,7 @@ func GssmrConfig() *Config {
NoBootstrap: gssmr.DefaultNoBootstrap,
NoMDNS: gssmr.DefaultNoMDNS,
DiscoveryInterval: gssmr.DefaultDiscoveryInterval,
GrandpaInterval: gssmr.DefaultGrandpaInterval,
},
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 @@ -72,6 +72,7 @@ type NetworkConfig struct {
MaxPeers int `toml:"max-peers,omitempty"`
PersistentPeers []string `toml:"persistent-peers,omitempty"`
DiscoveryInterval int `toml:"discovery-interval,omitempty"`
GrandpaInterval int `toml:"grandpa-interval,omitempty"`
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func createGRANDPAService(cfg *Config, st *state.Service, dh *digest.Handler, ks
Voters: voters,
Authority: cfg.Core.GrandpaAuthority,
Network: net,
Interval: cfg.Network.GrandpaInterval,
}

if cfg.Core.GrandpaAuthority {
Expand Down
14 changes: 8 additions & 6 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const (
)

var (
interval = time.Second // TODO: make this configurable; currently 1s is same as substrate; total round length is interval * 2
logger = log.New("pkg", "grandpa")
logger = log.New("pkg", "grandpa")
)

// Service represents the current state of the grandpa protocol
Expand All @@ -62,6 +61,7 @@ type Service struct {
resumed chan struct{} // this channel will be closed when the service resumes
messageHandler *MessageHandler
network Network
interval time.Duration

// current state information
state *State // current state
Expand Down Expand Up @@ -92,6 +92,7 @@ type Config struct {
Voters []Voter
Keypair *ed25519.Keypair
Authority bool
Interval time.Duration
}

// NewService returns a new GRANDPA Service instance.
Expand Down Expand Up @@ -166,6 +167,7 @@ func NewService(cfg *Config) (*Service, error) {
resumed: make(chan struct{}),
network: cfg.Network,
finalisedCh: finalisedCh,
interval: cfg.Interval,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a check in this function that will error if cfg.Interval == 0?

}

s.messageHandler = NewMessageHandler(s, s.blockState)
Expand Down Expand Up @@ -464,7 +466,7 @@ func (s *Service) playGrandpaRound() error {

logger.Debug("receiving pre-vote messages...")
go s.receiveMessages(ctx)
time.Sleep(interval)
time.Sleep(s.interval)
qdm12 marked this conversation as resolved.
Show resolved Hide resolved

if s.paused.Load().(bool) {
return ErrServicePaused
Expand Down Expand Up @@ -493,7 +495,7 @@ func (s *Service) playGrandpaRound() error {
go s.sendVoteMessage(prevote, vm, roundComplete)

logger.Debug("receiving pre-commit messages...")
time.Sleep(interval)
time.Sleep(s.interval)

if s.paused.Load().(bool) {
return ErrServicePaused
Expand Down Expand Up @@ -526,7 +528,7 @@ func (s *Service) playGrandpaRound() error {
}

func (s *Service) sendVoteMessage(stage Subround, msg *VoteMessage, roundComplete <-chan struct{}) {
ticker := time.NewTicker(interval * 4)
ticker := time.NewTicker(s.interval * 4)
defer ticker.Stop()

for {
Expand All @@ -550,7 +552,7 @@ func (s *Service) sendVoteMessage(stage Subround, msg *VoteMessage, roundComplet

// attemptToFinalize loops until the round is finalisable
func (s *Service) attemptToFinalize() error {
ticker := time.NewTicker(interval / 100)
ticker := time.NewTicker(s.interval / 100)

for {
select {
Expand Down
1 change: 1 addition & 0 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func newTestService(t *testing.T) (*Service, *state.Service) {
Keypair: kr.Alice().(*ed25519.Keypair),
Authority: true,
Network: net,
Interval: time.Second,
}

gs, err := NewService(cfg)
Expand Down
1 change: 1 addition & 0 deletions lib/grandpa/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func setupGrandpa(t *testing.T, kp *ed25519.Keypair) (*Service, chan *networkVot
LogLvl: log.LvlInfo,
Authority: true,
Network: net,
Interval: time.Second,
}

gs, err := NewService(cfg)
Expand Down