From 2c8c9f69c850c6980ec287df184d5632f9f7fa25 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 29 Apr 2021 09:03:24 +0300 Subject: [PATCH] [#488] storage/calculator: Make `alpha` dynamic Delete reading `alpha` from env var. Cover retrieving `alpha` behind interface in intermediate calculator. Add TODO to decide if it is necessary to receive that param from global config or not. Signed-off-by: Pavel Karpy --- cmd/neofs-node/config.go | 6 ------ cmd/neofs-node/reputation/intermediate/calculator.go | 10 ++++++++++ .../reputation/eigentrust/calculator/calculator.go | 12 +++++------- .../reputation/eigentrust/calculator/calls.go | 12 ++++++++++++ .../reputation/eigentrust/calculator/deps.go | 6 ++++++ .../reputation/eigentrust/controller/deps.go | 2 +- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index a2658c12..c7bc59a5 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -102,7 +102,6 @@ const ( // config keys for cfgReputation cfgReputationContract = "reputation.scripthash" - cfgReputationAlpha = "reputation.alpha" cfgReputationWorkerPoolEnabled = "reputation.async_worker.enabled" cfgReputationWorkerPoolSize = "reputation.async_worker.size" @@ -320,9 +319,6 @@ type cfgControlService struct { } type cfgReputation struct { - // Alpha parameter from origin EigenTrust algorithm - // http://ilpubs.stanford.edu:8090/562/1/2002-56.pdf Ch.5.1. - alpha float64 workerPool util2.WorkerPool // pool for EigenTrust algorithm's iterations localTrustStorage *truststorage.Storage @@ -430,7 +426,6 @@ func initCfg(path string) *cfg { healthStatus: atomic.NewInt32(int32(control.HealthStatus_HEALTH_STATUS_UNDEFINED)), cfgReputation: cfgReputation{ scriptHash: u160Reputation, - alpha: viper.GetFloat64(cfgReputationAlpha), workerPool: reputationWorkerPool, }, } @@ -486,7 +481,6 @@ func defaultConfiguration(v *viper.Viper) { v.SetDefault(cfgContainerWorkerPoolEnabled, true) v.SetDefault(cfgContainerWorkerPoolSize, 10) - v.SetDefault(cfgReputationAlpha, 0.5) v.SetDefault(cfgReputationWorkerPoolEnabled, true) v.SetDefault(cfgReputationWorkerPoolSize, 10) diff --git a/cmd/neofs-node/reputation/intermediate/calculator.go b/cmd/neofs-node/reputation/intermediate/calculator.go index 11f69adf..2bd671eb 100644 --- a/cmd/neofs-node/reputation/intermediate/calculator.go +++ b/cmd/neofs-node/reputation/intermediate/calculator.go @@ -25,3 +25,13 @@ func (c *DaughtersTrustCalculator) Calculate(ctx eigentrustctrl.IterationContext c.Calculator.Calculate(calcPrm) } + +// AlphaProvider provides required alpha parameter of eigen trust algorithm. +// TODO: decide if `Alpha` should be dynamically read from global config. #497 +type AlphaProvider struct { + Alpha float64 +} + +func (ap AlphaProvider) EigenTrustAlpha() (float64, error) { + return ap.Alpha, nil +} diff --git a/pkg/services/reputation/eigentrust/calculator/calculator.go b/pkg/services/reputation/eigentrust/calculator/calculator.go index 883cecf8..9cb5acaf 100644 --- a/pkg/services/reputation/eigentrust/calculator/calculator.go +++ b/pkg/services/reputation/eigentrust/calculator/calculator.go @@ -18,7 +18,7 @@ type Prm struct { // http://ilpubs.stanford.edu:8090/562/1/2002-56.pdf Ch.5.1. // // Must be in range (0, 1). - Alpha float64 + AlphaProvider AlphaProvider // Source of initial node trust values // @@ -63,8 +63,8 @@ func panicOnPrmValue(n string, v interface{}) { // initialization and is completely ready for work. func New(prm Prm, opts ...Option) *Calculator { switch { - case prm.Alpha <= 0 || prm.Alpha >= 1: - panicOnPrmValue("Alpha", prm.Alpha) + case prm.AlphaProvider == nil: + panicOnPrmValue("AlphaProvider", prm.AlphaProvider) case prm.InitialTrustSource == nil: panicOnPrmValue("InitialTrustSource", prm.InitialTrustSource) case prm.DaughterTrustSource == nil: @@ -84,9 +84,7 @@ func New(prm Prm, opts ...Option) *Calculator { } return &Calculator{ - alpha: reputation.TrustValueFromFloat64(prm.Alpha), - beta: reputation.TrustValueFromFloat64(1 - prm.Alpha), - prm: prm, - opts: o, + prm: prm, + opts: o, } } diff --git a/pkg/services/reputation/eigentrust/calculator/calls.go b/pkg/services/reputation/eigentrust/calculator/calls.go index a4f24ce4..ef4ab52e 100644 --- a/pkg/services/reputation/eigentrust/calculator/calls.go +++ b/pkg/services/reputation/eigentrust/calculator/calls.go @@ -24,6 +24,18 @@ func (p *CalculatePrm) SetEpochIteration(ei eigentrust.EpochIteration) { } func (c *Calculator) Calculate(prm CalculatePrm) { + alpha, err := c.prm.AlphaProvider.EigenTrustAlpha() + if err != nil { + c.opts.log.Debug( + "failed to get alpha param", + zap.Error(err), + ) + return + } + + c.alpha = reputation.TrustValueFromFloat64(alpha) + c.beta = reputation.TrustValueFromFloat64(1 - alpha) + ctx := eigentrust.IterContext{ Context: context.Background(), EpochIteration: prm.ei, diff --git a/pkg/services/reputation/eigentrust/calculator/deps.go b/pkg/services/reputation/eigentrust/calculator/deps.go index f94de3d4..aed381a5 100644 --- a/pkg/services/reputation/eigentrust/calculator/deps.go +++ b/pkg/services/reputation/eigentrust/calculator/deps.go @@ -59,3 +59,9 @@ type IntermediateWriter interface { type IntermediateWriterProvider interface { InitIntermediateWriter(Context) (IntermediateWriter, error) } + +// AlphaProvider must provide information about required +// alpha parameter for eigen trust algorithm. +type AlphaProvider interface { + EigenTrustAlpha() (float64, error) +} diff --git a/pkg/services/reputation/eigentrust/controller/deps.go b/pkg/services/reputation/eigentrust/controller/deps.go index ade2f6a2..8c475265 100644 --- a/pkg/services/reputation/eigentrust/controller/deps.go +++ b/pkg/services/reputation/eigentrust/controller/deps.go @@ -34,7 +34,7 @@ type DaughtersTrustCalculator interface { Calculate(ctx IterationContext) } -// IterationsProvider must provides information about numbers +// IterationsProvider must provide information about numbers // of iterations for algorithm. type IterationsProvider interface { EigenTrustIterations() (uint64, error)