From 96a65df32b2f4a531ada01149b808b50a5499487 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 14 Oct 2021 14:04:09 +0300 Subject: [PATCH] [#873] innerring: Reuse atomic IR's epochDuration instead of RPC call Signed-off-by: Pavel Karpy --- pkg/innerring/blocktimer.go | 12 +++++++----- pkg/innerring/config/config.go | 16 ---------------- pkg/innerring/innerring.go | 16 +++++----------- pkg/innerring/processors/netmap/process_epoch.go | 12 ++++++------ 4 files changed, 18 insertions(+), 38 deletions(-) diff --git a/pkg/innerring/blocktimer.go b/pkg/innerring/blocktimer.go index 0150841895..3592d74a9e 100644 --- a/pkg/innerring/blocktimer.go +++ b/pkg/innerring/blocktimer.go @@ -16,6 +16,7 @@ import ( type ( epochState interface { EpochCounter() uint64 + EpochDuration() uint64 } subEpochEventHandler struct { @@ -32,11 +33,10 @@ type ( newEpochHandlers []newEpochHandler cnrWrapper *container.Wrapper // to invoke stop container estimation - epoch epochState // to specify which epoch to stop + epoch epochState // to specify which epoch to stop, and epoch duration - epochDuration timer.BlockMeter // in blocks - stopEstimationDMul uint32 // X: X/Y of epoch in blocks - stopEstimationDDiv uint32 // Y: X/Y of epoch in blocks + stopEstimationDMul uint32 // X: X/Y of epoch in blocks + stopEstimationDDiv uint32 // Y: X/Y of epoch in blocks collectBasicIncome subEpochEventHandler distributeBasicIncome subEpochEventHandler @@ -80,7 +80,9 @@ func (s *Server) tickTimers() { func newEpochTimer(args *epochTimerArgs) *timer.BlockTimer { epochTimer := timer.NewBlockTimer( - args.epochDuration, + func() (uint32, error) { + return uint32(args.epoch.EpochDuration()), nil + }, func() { for _, handler := range args.newEpochHandlers { handler() diff --git a/pkg/innerring/config/config.go b/pkg/innerring/config/config.go index dcfbe65c6b..4ece4fa95e 100644 --- a/pkg/innerring/config/config.go +++ b/pkg/innerring/config/config.go @@ -52,22 +52,6 @@ func (c *GlobalConfig) AuditFee() (uint64, error) { return c.nm.AuditFee() } -func (c *GlobalConfig) EpochDuration() (uint32, error) { - if isDebug() { - value := c.cfg.GetUint32("timers.epoch") - if value != 0 { - return value, nil - } - } - - epochDuration, err := c.nm.EpochDuration() - if err != nil { - return 0, err - } - - return uint32(epochDuration), nil -} - func isDebug() bool { return misc.Debug == "true" } diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 443262aa81..394a51aecb 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -780,7 +780,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error newEpochHandlers: server.newEpochHandlers(log), cnrWrapper: cnrClient, epoch: server, - epochDuration: globalConfig.EpochDuration, stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"), stopEstimationDDiv: cfg.GetUint32("timers.stop_estimation.div"), collectBasicIncome: subEpochEventHandler{ @@ -956,16 +955,16 @@ func (s *Server) initConfigFromBlockchain() error { return fmt.Errorf("can't read balance contract precision: %w", err) } + s.epochCounter.Store(epoch) + s.epochDuration.Store(epochDuration) + s.precision.SetBalancePrecision(balancePrecision) + // get next epoch delta tick s.initialEpochTickDelta, err = s.nextEpochBlockDelta() if err != nil { return err } - s.epochCounter.Store(epoch) - s.epochDuration.Store(epochDuration) - s.precision.SetBalancePrecision(balancePrecision) - s.log.Debug("read config from blockchain", zap.Bool("active", s.IsActive()), zap.Bool("alphabet", s.IsAlphabet()), @@ -988,12 +987,7 @@ func (s *Server) nextEpochBlockDelta() (uint32, error) { return 0, fmt.Errorf("can't get side chain height: %w", err) } - epochDuration, err := s.netmapClient.EpochDuration() - if err != nil { - return 0, fmt.Errorf("can't get epoch duration: %w", err) - } - - delta := uint32(epochDuration) + epochBlock + delta := uint32(s.epochDuration.Load()) + epochBlock if delta < blockHeight { return 0, nil } diff --git a/pkg/innerring/processors/netmap/process_epoch.go b/pkg/innerring/processors/netmap/process_epoch.go index 9854936a1e..9c7f1324c9 100644 --- a/pkg/innerring/processors/netmap/process_epoch.go +++ b/pkg/innerring/processors/netmap/process_epoch.go @@ -10,12 +10,6 @@ import ( // Process new epoch notification by setting global epoch value and resetting // local epoch timer. func (np *Processor) processNewEpoch(epoch uint64) { - np.epochState.SetEpochCounter(epoch) - if err := np.epochTimer.ResetEpochTimer(); err != nil { - np.log.Warn("can't reset epoch timer", - zap.String("error", err.Error())) - } - epochDuration, err := np.netmapClient.EpochDuration() if err != nil { np.log.Warn("can't get epoch duration", @@ -24,6 +18,12 @@ func (np *Processor) processNewEpoch(epoch uint64) { np.epochState.SetEpochDuration(epochDuration) } + np.epochState.SetEpochCounter(epoch) + if err := np.epochTimer.ResetEpochTimer(); err != nil { + np.log.Warn("can't reset epoch timer", + zap.String("error", err.Error())) + } + // get new netmap snapshot networkMap, err := np.netmapClient.Snapshot() if err != nil {