diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index 99f0477a..2b3fd7e1 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -76,6 +76,7 @@ func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("contracts.gas", "668e0c1f9d7b70a99dd9e06eadd4c784d641afbc") cfg.SetDefault("timers.epoch", "5s") + cfg.SetDefault("timers.emit", "30s") cfg.SetDefault("workers.netmap", "10") cfg.SetDefault("workers.balance", "10") diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 4931d4ad..a7278a60 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -105,8 +105,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // create local timer instance server.localTimers = timers.New(&timers.Params{ - Log: log, - EpochDuration: cfg.GetDuration("timers.epoch"), + Log: log, + EpochDuration: cfg.GetDuration("timers.epoch"), + AlphabetDuration: cfg.GetDuration("timers.emit"), }) morphChain := &chainParams{ diff --git a/pkg/innerring/timers/alphabet.go b/pkg/innerring/timers/alphabet.go new file mode 100644 index 00000000..d4ac29e5 --- /dev/null +++ b/pkg/innerring/timers/alphabet.go @@ -0,0 +1,7 @@ +package timers + +// NewAlphabetEmitTick is a event for gas emission from alphabet contract. +type NewAlphabetEmitTick struct{} + +// MorphEvent implements Event interface. +func (NewAlphabetEmitTick) MorphEvent() {} diff --git a/pkg/innerring/timers/timers.go b/pkg/innerring/timers/timers.go index 8c019719..22eff892 100644 --- a/pkg/innerring/timers/timers.go +++ b/pkg/innerring/timers/timers.go @@ -20,32 +20,38 @@ type ( Timers struct { log *zap.Logger - epoch localTimer + epoch localTimer + alphabet localTimer } // Params for timers instance constructor. Params struct { - Log *zap.Logger - EpochDuration time.Duration + Log *zap.Logger + EpochDuration time.Duration + AlphabetDuration time.Duration } ) const ( // EpochTimer is a type for HandlerInfo structure. EpochTimer = "EpochTimer" + // AlphabetTimer is a type for HandlerInfo structure. + AlphabetTimer = "AlphabetTimer" ) // New creates instance of timers component. func New(p *Params) *Timers { return &Timers{ - log: p.Log, - epoch: localTimer{duration: p.EpochDuration}, + log: p.Log, + epoch: localTimer{duration: p.EpochDuration}, + alphabet: localTimer{duration: p.AlphabetDuration}, } } // Start runs all available local timers. func (t *Timers) Start(ctx context.Context) { t.epoch.timer = time.NewTimer(t.epoch.duration) + t.alphabet.timer = time.NewTimer(t.alphabet.duration) go t.serve(ctx) } @@ -55,16 +61,19 @@ func (t *Timers) serve(ctx context.Context) { case <-ctx.Done(): t.log.Info("timers are getting stopped") t.epoch.timer.Stop() + t.alphabet.timer.Stop() return case <-t.epoch.timer.C: // reset timer so it can tick once again t.epoch.timer.Reset(t.epoch.duration) - - // call handler if it is set - if t.epoch.handler != nil { - t.epoch.handler(NewEpochTick{}) - } + // call handler, it should be always set + t.epoch.handler(NewEpochTick{}) + case <-t.alphabet.timer.C: + // reset timer so it can tick once again + t.alphabet.timer.Reset(t.alphabet.duration) + // call handler, it should be always set + t.alphabet.handler(NewAlphabetEmitTick{}) } } } @@ -78,6 +87,8 @@ func (t *Timers) RegisterHandler(h event.HandlerInfo) error { switch h.GetType() { case EpochTimer: t.epoch.handler = h.Handler() + case AlphabetTimer: + t.alphabet.handler = h.Handler() default: return errors.New("ir/timers: unknown handler type") }