From a6f2394deced99a8a12ccd1cbecf30717ef9e6da Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 23 Mar 2021 21:54:00 +0300 Subject: [PATCH] [#428] cmd/node: Trigger local trust controller on new epoch Implement and call `initReputationService` func that constructs local trust storage and controller, and subscribes the controller on new epoch notification. Event handler calls `Controller.Report` method to process collected values. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 10 ++++++++++ cmd/neofs-node/main.go | 1 + cmd/neofs-node/reputation.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 9afd42e6b..2150b246c 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -32,6 +32,8 @@ import ( netmap2 "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/services/control" + trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" + truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage" tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" "github.com/nspcc-dev/neofs-node/pkg/services/util/response" util2 "github.com/nspcc-dev/neofs-node/pkg/util" @@ -200,6 +202,8 @@ type cfg struct { healthStatus *atomic.Int32 closers []func() + + cfgReputation cfgReputation } type cfgGRPC struct { @@ -287,6 +291,12 @@ type cfgControlService struct { server *grpc.Server } +type cfgReputation struct { + localTrustStorage *truststorage.Storage + + localTrustCtrl *trustcontroller.Controller +} + const ( _ BootstrapType = iota StorageNode diff --git a/cmd/neofs-node/main.go b/cmd/neofs-node/main.go index 2cfc43e14..144c20a6a 100644 --- a/cmd/neofs-node/main.go +++ b/cmd/neofs-node/main.go @@ -46,6 +46,7 @@ func initApp(c *cfg) { initAccountingService(c) initContainerService(c) initSessionService(c) + initReputationService(c) initObjectService(c) initProfiler(c) initMetrics(c) diff --git a/cmd/neofs-node/reputation.go b/cmd/neofs-node/reputation.go index 821c8517f..fa463ca76 100644 --- a/cmd/neofs-node/reputation.go +++ b/cmd/neofs-node/reputation.go @@ -4,7 +4,10 @@ import ( "bytes" "encoding/hex" + crypto "github.com/nspcc-dev/neofs-crypto" netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" + "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage" @@ -117,3 +120,32 @@ func (l *localTrustLogger) Write(t reputation.Trust) error { func (*localTrustLogger) Close() error { return nil } + +func initReputationService(c *cfg) { + // consider sharing this between application components + nmSrc := newCachedNetmapStorage(c.cfgNetmap.state, c.cfgNetmap.wrapper) + + c.cfgReputation.localTrustStorage = truststorage.New(truststorage.Prm{}) + + trustStorage := &localTrustStorage{ + log: c.log, + storage: c.cfgReputation.localTrustStorage, + nmSrc: nmSrc, + localKey: crypto.MarshalPublicKey(&c.key.PublicKey), + } + + c.cfgReputation.localTrustCtrl = trustcontroller.New(trustcontroller.Prm{ + LocalTrustSource: trustStorage, + LocalTrustTarget: trustStorage, + }) + + addNewEpochNotificationHandler(c, func(ev event.Event) { + var reportPrm trustcontroller.ReportPrm + + // report collected values from previous epoch + reportPrm.SetEpoch(ev.(netmap.NewEpoch).EpochNumber() - 1) + + // TODO: implement and use worker pool [neofs-node#440] + go c.cfgReputation.localTrustCtrl.Report(reportPrm) + }) +}