From ed28ce24cd5ee224ee5799b23b0edaf9c29edf10 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Fri, 24 Mar 2023 14:57:34 +0300 Subject: [PATCH] [#168] node: Refactor reputation service Resolve funlen linter for initReputationService function Signed-off-by: Dmitrii Stepanov --- cmd/frostfs-node/reputation.go | 280 ++++++++++++++++++--------------- 1 file changed, 154 insertions(+), 126 deletions(-) diff --git a/cmd/frostfs-node/reputation.go b/cmd/frostfs-node/reputation.go index 8854b21cb..7b43443c2 100644 --- a/cmd/frostfs-node/reputation.go +++ b/cmd/frostfs-node/reputation.go @@ -33,7 +33,6 @@ import ( "go.uber.org/zap" ) -// nolint: funlen func initReputationService(c *cfg) { wrap, err := repClient.NewFromMorph(c.cfgMorph.client, c.cfgReputation.scriptHash, 0, repClient.TryNotary()) fatalOnErr(err) @@ -50,26 +49,7 @@ func initReputationService(c *cfg) { daughterStorage := daughters.New(daughters.Prm{}) consumerStorage := consumerstorage.New(consumerstorage.Prm{}) - // storing received daughter(of current node) trusts as a manager - daughterStorageWriterProvider := &intermediatereputation.DaughterStorageWriterProvider{ - Log: c.log, - Storage: daughterStorage, - } - - consumerStorageWriterProvider := &intermediatereputation.ConsumerStorageWriterProvider{ - Log: c.log, - Storage: consumerStorage, - } - localTrustLogger := &logger.Logger{Logger: c.log.With(zap.String("trust_type", "local"))} - intermediateTrustLogger := &logger.Logger{Logger: c.log.With(zap.String("trust_type", "intermediate"))} - - localTrustStorage := &localreputation.TrustStorage{ - Log: localTrustLogger, - Storage: c.cfgReputation.localTrustStorage, - NmSrc: nmSrc, - LocalKey: localKey, - } managerBuilder := reputationcommon.NewManagerBuilder( reputationcommon.ManagersPrm{ @@ -85,116 +65,15 @@ func initReputationService(c *cfg) { }, ) - intermediateRouteBuilder := intermediateroutes.New( - intermediateroutes.Prm{ - ManagerBuilder: managerBuilder, - Log: intermediateTrustLogger, - }, - ) + localTrustRouter := createLocalTrustRouter(c, localRouteBuilder, localTrustLogger, daughterStorage) - remoteLocalTrustProvider := common.NewRemoteTrustProvider( - common.RemoteProviderPrm{ - NetmapKeys: c, - DeadEndProvider: daughterStorageWriterProvider, - ClientCache: c.bgClientCache, - WriterProvider: localreputation.NewRemoteProvider( - localreputation.RemoteProviderPrm{ - Key: &c.key.PrivateKey, - Log: localTrustLogger, - }, - ), - Log: localTrustLogger, - }, - ) + intermediateTrustRouter := createIntermediateTrustRouter(c, consumerStorage, managerBuilder) - remoteIntermediateTrustProvider := common.NewRemoteTrustProvider( - common.RemoteProviderPrm{ - NetmapKeys: c, - DeadEndProvider: consumerStorageWriterProvider, - ClientCache: c.bgClientCache, - WriterProvider: intermediatereputation.NewRemoteProvider( - intermediatereputation.RemoteProviderPrm{ - Key: &c.key.PrivateKey, - Log: intermediateTrustLogger, - }, - ), - Log: intermediateTrustLogger, - }, - ) + eigenTrustController := createEigenTrustController(c, intermediateTrustRouter, localKey, wrap, daughterStorage, consumerStorage) - localTrustRouter := reputationrouter.New( - reputationrouter.Prm{ - LocalServerInfo: c, - RemoteWriterProvider: remoteLocalTrustProvider, - Builder: localRouteBuilder, - }, - reputationrouter.WithLogger(localTrustLogger)) + c.cfgReputation.localTrustCtrl = createLocalTrustController(c, localTrustLogger, localKey, localTrustRouter) - intermediateTrustRouter := reputationrouter.New( - reputationrouter.Prm{ - LocalServerInfo: c, - RemoteWriterProvider: remoteIntermediateTrustProvider, - Builder: intermediateRouteBuilder, - }, - reputationrouter.WithLogger(intermediateTrustLogger), - ) - - eigenTrustCalculator := eigentrustcalc.New( - eigentrustcalc.Prm{ - AlphaProvider: c.cfgNetmap.wrapper, - InitialTrustSource: intermediatereputation.InitialTrustSource{ - NetMap: nmSrc, - }, - IntermediateValueTarget: intermediateTrustRouter, - WorkerPool: c.cfgReputation.workerPool, - FinalResultTarget: intermediatereputation.NewFinalWriterProvider( - intermediatereputation.FinalWriterProviderPrm{ - PrivatKey: &c.key.PrivateKey, - PubKey: localKey, - Client: wrap, - }, - intermediatereputation.FinalWriterWithLogger(c.log), - ), - DaughterTrustSource: &intermediatereputation.DaughterTrustIteratorProvider{ - DaughterStorage: daughterStorage, - ConsumerStorage: consumerStorage, - }, - }, - eigentrustcalc.WithLogger(c.log), - ) - - eigenTrustController := eigentrustctrl.New( - eigentrustctrl.Prm{ - DaughtersTrustCalculator: &intermediatereputation.DaughtersTrustCalculator{ - Calculator: eigenTrustCalculator, - }, - IterationsProvider: c.cfgNetmap.wrapper, - WorkerPool: c.cfgReputation.workerPool, - }, - eigentrustctrl.WithLogger(c.log), - ) - - c.cfgReputation.localTrustCtrl = localtrustcontroller.New( - localtrustcontroller.Prm{ - LocalTrustSource: localTrustStorage, - LocalTrustTarget: localTrustRouter, - }, - localtrustcontroller.WithLogger(c.log), - ) - - addNewEpochAsyncNotificationHandler( - c, - func(ev event.Event) { - c.log.Debug("start reporting reputation on new epoch event") - - var reportPrm localtrustcontroller.ReportPrm - - // report collected values from previous epoch - reportPrm.SetEpoch(ev.(netmap.NewEpoch).EpochNumber() - 1) - - c.cfgReputation.localTrustCtrl.Report(reportPrm) - }, - ) + addReputationReportHandler(c) server := grpcreputation.New( reputationrpc.NewSignService( @@ -219,6 +98,26 @@ func initReputationService(c *cfg) { // initialize eigen trust block timer newEigenTrustIterTimer(c) + addEigenTrustEpochHandler(c, eigenTrustController) +} + +func addReputationReportHandler(c *cfg) { + addNewEpochAsyncNotificationHandler( + c, + func(ev event.Event) { + c.log.Debug("start reporting reputation on new epoch event") + + var reportPrm localtrustcontroller.ReportPrm + + // report collected values from previous epoch + reportPrm.SetEpoch(ev.(netmap.NewEpoch).EpochNumber() - 1) + + c.cfgReputation.localTrustCtrl.Report(reportPrm) + }, + ) +} + +func addEigenTrustEpochHandler(c *cfg, eigenTrustController *eigentrustctrl.Controller) { addNewEpochAsyncNotificationHandler( c, func(e event.Event) { @@ -255,6 +154,135 @@ func initReputationService(c *cfg) { ) } +func createLocalTrustRouter(c *cfg, localRouteBuilder *localroutes.Builder, localTrustLogger *logger.Logger, daughterStorage *daughters.Storage) *reputationrouter.Router { + // storing received daughter(of current node) trusts as a manager + daughterStorageWriterProvider := &intermediatereputation.DaughterStorageWriterProvider{ + Log: c.log, + Storage: daughterStorage, + } + + remoteLocalTrustProvider := common.NewRemoteTrustProvider( + common.RemoteProviderPrm{ + NetmapKeys: c, + DeadEndProvider: daughterStorageWriterProvider, + ClientCache: c.bgClientCache, + WriterProvider: localreputation.NewRemoteProvider( + localreputation.RemoteProviderPrm{ + Key: &c.key.PrivateKey, + Log: localTrustLogger, + }, + ), + Log: localTrustLogger, + }, + ) + + localTrustRouter := reputationrouter.New( + reputationrouter.Prm{ + LocalServerInfo: c, + RemoteWriterProvider: remoteLocalTrustProvider, + Builder: localRouteBuilder, + }, + reputationrouter.WithLogger(localTrustLogger)) + return localTrustRouter +} + +func createIntermediateTrustRouter(c *cfg, consumerStorage *consumerstorage.Storage, managerBuilder reputationcommon.ManagerBuilder) *reputationrouter.Router { + intermediateTrustLogger := &logger.Logger{Logger: c.log.With(zap.String("trust_type", "intermediate"))} + + consumerStorageWriterProvider := &intermediatereputation.ConsumerStorageWriterProvider{ + Log: c.log, + Storage: consumerStorage, + } + + remoteIntermediateTrustProvider := common.NewRemoteTrustProvider( + common.RemoteProviderPrm{ + NetmapKeys: c, + DeadEndProvider: consumerStorageWriterProvider, + ClientCache: c.bgClientCache, + WriterProvider: intermediatereputation.NewRemoteProvider( + intermediatereputation.RemoteProviderPrm{ + Key: &c.key.PrivateKey, + Log: intermediateTrustLogger, + }, + ), + Log: intermediateTrustLogger, + }, + ) + + intermediateRouteBuilder := intermediateroutes.New( + intermediateroutes.Prm{ + ManagerBuilder: managerBuilder, + Log: intermediateTrustLogger, + }, + ) + + intermediateTrustRouter := reputationrouter.New( + reputationrouter.Prm{ + LocalServerInfo: c, + RemoteWriterProvider: remoteIntermediateTrustProvider, + Builder: intermediateRouteBuilder, + }, + reputationrouter.WithLogger(intermediateTrustLogger), + ) + return intermediateTrustRouter +} + +func createEigenTrustController(c *cfg, intermediateTrustRouter *reputationrouter.Router, localKey []byte, wrap *repClient.Client, + daughterStorage *daughters.Storage, consumerStorage *consumerstorage.Storage) *eigentrustctrl.Controller { + eigenTrustCalculator := eigentrustcalc.New( + eigentrustcalc.Prm{ + AlphaProvider: c.cfgNetmap.wrapper, + InitialTrustSource: intermediatereputation.InitialTrustSource{ + NetMap: c.netMapSource, + }, + IntermediateValueTarget: intermediateTrustRouter, + WorkerPool: c.cfgReputation.workerPool, + FinalResultTarget: intermediatereputation.NewFinalWriterProvider( + intermediatereputation.FinalWriterProviderPrm{ + PrivatKey: &c.key.PrivateKey, + PubKey: localKey, + Client: wrap, + }, + intermediatereputation.FinalWriterWithLogger(c.log), + ), + DaughterTrustSource: &intermediatereputation.DaughterTrustIteratorProvider{ + DaughterStorage: daughterStorage, + ConsumerStorage: consumerStorage, + }, + }, + eigentrustcalc.WithLogger(c.log), + ) + + eigenTrustController := eigentrustctrl.New( + eigentrustctrl.Prm{ + DaughtersTrustCalculator: &intermediatereputation.DaughtersTrustCalculator{ + Calculator: eigenTrustCalculator, + }, + IterationsProvider: c.cfgNetmap.wrapper, + WorkerPool: c.cfgReputation.workerPool, + }, + eigentrustctrl.WithLogger(c.log), + ) + return eigenTrustController +} + +func createLocalTrustController(c *cfg, localTrustLogger *logger.Logger, localKey []byte, localTrustRouter *reputationrouter.Router) *localtrustcontroller.Controller { + localTrustStorage := &localreputation.TrustStorage{ + Log: localTrustLogger, + Storage: c.cfgReputation.localTrustStorage, + NmSrc: c.netMapSource, + LocalKey: localKey, + } + + return localtrustcontroller.New( + localtrustcontroller.Prm{ + LocalTrustSource: localTrustStorage, + LocalTrustTarget: localTrustRouter, + }, + localtrustcontroller.WithLogger(c.log), + ) +} + type reputationServer struct { *cfg log *logger.Logger