[#185] ir: Refactor ir start

Resolve funlen linter for Server.Start method

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-03-29 12:20:22 +03:00 committed by Gitea
parent 85fb9e77c4
commit c8a6978563

View file

@ -158,8 +158,6 @@ var (
) )
// Start runs all event providers. // Start runs all event providers.
//
// nolint: funlen
func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) { func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
s.setHealthStatus(control.HealthStatus_STARTING) s.setHealthStatus(control.HealthStatus_STARTING)
defer func() { defer func() {
@ -168,38 +166,25 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
} }
}() }()
for _, starter := range s.starters { err = s.launchStarters()
if err := starter(); err != nil { if err != nil {
return err return err
} }
}
err = s.initConfigFromBlockchain() err = s.initConfigFromBlockchain()
if err != nil { if err != nil {
return err return err
} }
if !s.mainNotaryConfig.disabled { err = s.initMainNotary(ctx)
err = s.initNotary(ctx,
s.depositMainNotary,
s.awaitMainNotaryDeposit,
"waiting to accept main notary deposit",
)
if err != nil { if err != nil {
return err return err
} }
}
if !s.sideNotaryConfig.disabled { err = s.initSideNotary(ctx)
err = s.initNotary(ctx,
s.depositSideNotary,
s.awaitSideNotaryDeposit,
"waiting to accept side notary deposit",
)
if err != nil { if err != nil {
return err return err
} }
}
prm := governance.VoteValidatorPrm{} prm := governance.VoteValidatorPrm{}
prm.Validators = s.predefinedValidators prm.Validators = s.predefinedValidators
@ -212,13 +197,7 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
zap.String("error", err.Error())) zap.String("error", err.Error()))
} }
// tick initial epoch s.tickInitialExpoch()
initialEpochTicker := timer.NewOneTickTimer(
timer.StaticBlockMeter(s.initialEpochTickDelta),
func() {
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
})
s.addBlockTimer(initialEpochTicker)
morphErr := make(chan error) morphErr := make(chan error)
mainnnetErr := make(chan error) mainnnetErr := make(chan error)
@ -235,37 +214,12 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
} }
}() }()
s.morphListener.RegisterBlockHandler(func(b *block.Block) { s.registerMorphNewBlockEventHandler()
s.log.Debug("new block", s.registerMainnetNewBlockEventHandler()
zap.Uint32("index", b.Index),
)
err = s.persistate.SetUInt32(persistateSideChainLastBlockKey, b.Index) if err := s.startRunners(intError); err != nil {
if err != nil {
s.log.Warn("can't update persistent state",
zap.String("chain", "side"),
zap.Uint32("block_index", b.Index))
}
s.tickTimers(b.Index)
})
if !s.withoutMainNet {
s.mainnetListener.RegisterBlockHandler(func(b *block.Block) {
err = s.persistate.SetUInt32(persistateMainChainLastBlockKey, b.Index)
if err != nil {
s.log.Warn("can't update persistent state",
zap.String("chain", "main"),
zap.Uint32("block_index", b.Index))
}
})
}
for _, runner := range s.runners {
if err := runner(intError); err != nil {
return err return err
} }
}
go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events
go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events
@ -279,6 +233,85 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
return nil return nil
} }
func (s *Server) registerMorphNewBlockEventHandler() {
s.morphListener.RegisterBlockHandler(func(b *block.Block) {
s.log.Debug("new block",
zap.Uint32("index", b.Index),
)
err := s.persistate.SetUInt32(persistateSideChainLastBlockKey, b.Index)
if err != nil {
s.log.Warn("can't update persistent state",
zap.String("chain", "side"),
zap.Uint32("block_index", b.Index))
}
s.tickTimers(b.Index)
})
}
func (s *Server) registerMainnetNewBlockEventHandler() {
if !s.withoutMainNet {
s.mainnetListener.RegisterBlockHandler(func(b *block.Block) {
err := s.persistate.SetUInt32(persistateMainChainLastBlockKey, b.Index)
if err != nil {
s.log.Warn("can't update persistent state",
zap.String("chain", "main"),
zap.Uint32("block_index", b.Index))
}
})
}
}
func (s *Server) startRunners(errCh chan<- error) error {
for _, runner := range s.runners {
if err := runner(errCh); err != nil {
return err
}
}
return nil
}
func (s *Server) launchStarters() error {
for _, starter := range s.starters {
if err := starter(); err != nil {
return err
}
}
return nil
}
func (s *Server) initMainNotary(ctx context.Context) error {
if !s.mainNotaryConfig.disabled {
return s.initNotary(ctx,
s.depositMainNotary,
s.awaitMainNotaryDeposit,
"waiting to accept main notary deposit",
)
}
return nil
}
func (s *Server) initSideNotary(ctx context.Context) error {
if !s.sideNotaryConfig.disabled {
return s.initNotary(ctx,
s.depositSideNotary,
s.awaitSideNotaryDeposit,
"waiting to accept side notary deposit",
)
}
return nil
}
func (s *Server) tickInitialExpoch() {
initialEpochTicker := timer.NewOneTickTimer(
timer.StaticBlockMeter(s.initialEpochTickDelta),
func() {
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
})
s.addBlockTimer(initialEpochTicker)
}
func (s *Server) startWorkers(ctx context.Context) { func (s *Server) startWorkers(ctx context.Context) {
for _, w := range s.workers { for _, w := range s.workers {
go w(ctx) go w(ctx)