[#873] innerring: Reuse atomic IR's epochDuration instead of RPC call

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-10-14 14:04:09 +03:00 committed by Alex Vanin
parent 505d92bb06
commit 96a65df32b
4 changed files with 18 additions and 38 deletions

View file

@ -16,6 +16,7 @@ import (
type (
epochState interface {
EpochCounter() uint64
EpochDuration() uint64
}
subEpochEventHandler struct {
@ -32,9 +33,8 @@ 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
@ -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()

View file

@ -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"
}

View file

@ -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
}

View file

@ -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 {