[#86] Run timer for gas emission event

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-12 12:49:38 +03:00 committed by Alex Vanin
parent 6bc787bb19
commit 4308a6f522
4 changed files with 32 additions and 12 deletions

View file

@ -76,6 +76,7 @@ func defaultConfiguration(cfg *viper.Viper) {
cfg.SetDefault("contracts.gas", "668e0c1f9d7b70a99dd9e06eadd4c784d641afbc") cfg.SetDefault("contracts.gas", "668e0c1f9d7b70a99dd9e06eadd4c784d641afbc")
cfg.SetDefault("timers.epoch", "5s") cfg.SetDefault("timers.epoch", "5s")
cfg.SetDefault("timers.emit", "30s")
cfg.SetDefault("workers.netmap", "10") cfg.SetDefault("workers.netmap", "10")
cfg.SetDefault("workers.balance", "10") cfg.SetDefault("workers.balance", "10")

View file

@ -107,6 +107,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
server.localTimers = timers.New(&timers.Params{ server.localTimers = timers.New(&timers.Params{
Log: log, Log: log,
EpochDuration: cfg.GetDuration("timers.epoch"), EpochDuration: cfg.GetDuration("timers.epoch"),
AlphabetDuration: cfg.GetDuration("timers.emit"),
}) })
morphChain := &chainParams{ morphChain := &chainParams{

View file

@ -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() {}

View file

@ -21,18 +21,22 @@ type (
log *zap.Logger log *zap.Logger
epoch localTimer epoch localTimer
alphabet localTimer
} }
// Params for timers instance constructor. // Params for timers instance constructor.
Params struct { Params struct {
Log *zap.Logger Log *zap.Logger
EpochDuration time.Duration EpochDuration time.Duration
AlphabetDuration time.Duration
} }
) )
const ( const (
// EpochTimer is a type for HandlerInfo structure. // EpochTimer is a type for HandlerInfo structure.
EpochTimer = "EpochTimer" EpochTimer = "EpochTimer"
// AlphabetTimer is a type for HandlerInfo structure.
AlphabetTimer = "AlphabetTimer"
) )
// New creates instance of timers component. // New creates instance of timers component.
@ -40,12 +44,14 @@ func New(p *Params) *Timers {
return &Timers{ return &Timers{
log: p.Log, log: p.Log,
epoch: localTimer{duration: p.EpochDuration}, epoch: localTimer{duration: p.EpochDuration},
alphabet: localTimer{duration: p.AlphabetDuration},
} }
} }
// Start runs all available local timers. // Start runs all available local timers.
func (t *Timers) Start(ctx context.Context) { func (t *Timers) Start(ctx context.Context) {
t.epoch.timer = time.NewTimer(t.epoch.duration) t.epoch.timer = time.NewTimer(t.epoch.duration)
t.alphabet.timer = time.NewTimer(t.alphabet.duration)
go t.serve(ctx) go t.serve(ctx)
} }
@ -55,16 +61,19 @@ func (t *Timers) serve(ctx context.Context) {
case <-ctx.Done(): case <-ctx.Done():
t.log.Info("timers are getting stopped") t.log.Info("timers are getting stopped")
t.epoch.timer.Stop() t.epoch.timer.Stop()
t.alphabet.timer.Stop()
return return
case <-t.epoch.timer.C: case <-t.epoch.timer.C:
// reset timer so it can tick once again // reset timer so it can tick once again
t.epoch.timer.Reset(t.epoch.duration) t.epoch.timer.Reset(t.epoch.duration)
// call handler, it should be always set
// call handler if it is set
if t.epoch.handler != nil {
t.epoch.handler(NewEpochTick{}) 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() { switch h.GetType() {
case EpochTimer: case EpochTimer:
t.epoch.handler = h.Handler() t.epoch.handler = h.Handler()
case AlphabetTimer:
t.alphabet.handler = h.Handler()
default: default:
return errors.New("ir/timers: unknown handler type") return errors.New("ir/timers: unknown handler type")
} }