[#185] ir: Refactor ir start

Resolve funlen linter for Server.Start method

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
pull/168/head
Dmitrii Stepanov 2023-03-29 12:20:22 +03:00 committed by Gitea
parent 85fb9e77c4
commit c8a6978563
1 changed files with 93 additions and 60 deletions

View File

@ -158,8 +158,6 @@ var (
)
// Start runs all event providers.
//
// nolint: funlen
func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
s.setHealthStatus(control.HealthStatus_STARTING)
defer func() {
@ -168,10 +166,9 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
}
}()
for _, starter := range s.starters {
if err := starter(); err != nil {
return err
}
err = s.launchStarters()
if err != nil {
return err
}
err = s.initConfigFromBlockchain()
@ -179,26 +176,14 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
return err
}
if !s.mainNotaryConfig.disabled {
err = s.initNotary(ctx,
s.depositMainNotary,
s.awaitMainNotaryDeposit,
"waiting to accept main notary deposit",
)
if err != nil {
return err
}
err = s.initMainNotary(ctx)
if err != nil {
return err
}
if !s.sideNotaryConfig.disabled {
err = s.initNotary(ctx,
s.depositSideNotary,
s.awaitSideNotaryDeposit,
"waiting to accept side notary deposit",
)
if err != nil {
return err
}
err = s.initSideNotary(ctx)
if err != nil {
return err
}
prm := governance.VoteValidatorPrm{}
@ -212,13 +197,7 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
zap.String("error", err.Error()))
}
// tick initial epoch
initialEpochTicker := timer.NewOneTickTimer(
timer.StaticBlockMeter(s.initialEpochTickDelta),
func() {
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
})
s.addBlockTimer(initialEpochTicker)
s.tickInitialExpoch()
morphErr := make(chan error)
mainnnetErr := make(chan error)
@ -235,36 +214,11 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
}
}()
s.morphListener.RegisterBlockHandler(func(b *block.Block) {
s.log.Debug("new block",
zap.Uint32("index", b.Index),
)
s.registerMorphNewBlockEventHandler()
s.registerMainnetNewBlockEventHandler()
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)
})
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
}
if err := s.startRunners(intError); err != nil {
return err
}
go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events
@ -279,6 +233,85 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
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) {
for _, w := range s.workers {
go w(ctx)