From f7aa79f0b69109325c242898ed660737c7f8896a Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 23 Apr 2021 17:51:51 +0300 Subject: [PATCH] [#488] cmd/reputation/storage: Add consumers storage Add consumer storage wrapper that implements `WriterProvider` interface. Change field naming in daughters storage package. Signed-off-by: Pavel Karpy --- .../reputation/intermediate/consumers.go | 53 +++++++++++++++++++ .../reputation/intermediate/daughters.go | 4 +- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 cmd/neofs-node/reputation/intermediate/consumers.go diff --git a/cmd/neofs-node/reputation/intermediate/consumers.go b/cmd/neofs-node/reputation/intermediate/consumers.go new file mode 100644 index 000000000..9977454f3 --- /dev/null +++ b/cmd/neofs-node/reputation/intermediate/consumers.go @@ -0,0 +1,53 @@ +package intermediate + +import ( + "errors" + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" + reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust" + eigencalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator" + consumerstorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/storage/consumers" + "github.com/nspcc-dev/neofs-node/pkg/util/logger" +) + +var ErrIncorrectContext = errors.New("could not write intermediate trust: passed context incorrect") + +type ConsumerStorageWriterProvider struct { + Log *logger.Logger + Storage *consumerstorage.Storage +} + +type ConsumerTrustWriter struct { + log *logger.Logger + storage *consumerstorage.Storage +} + +func (w *ConsumerTrustWriter) Write(ctx common.Context, t reputation.Trust) error { + eiCtx, ok := ctx.(eigencalc.Context) + if !ok { + return ErrIncorrectContext + } + + trust := eigentrust.IterationTrust{Trust: t} + + trust.SetEpoch(eiCtx.Epoch()) + trust.SetI(eiCtx.I()) + + fmt.Println("decided to save consumers trusts to storage for epoch and iteration: ", eiCtx.Epoch(), eiCtx.I()) + w.storage.Put(trust) + return nil +} + +func (w *ConsumerTrustWriter) Close() error { + return nil +} + +func (s *ConsumerStorageWriterProvider) InitWriter(_ reputationcommon.Context) (reputationcommon.Writer, error) { + return &ConsumerTrustWriter{ + log: s.Log, + storage: s.Storage, + }, nil +} diff --git a/cmd/neofs-node/reputation/intermediate/daughters.go b/cmd/neofs-node/reputation/intermediate/daughters.go index 32018ce97..271d31d3d 100644 --- a/cmd/neofs-node/reputation/intermediate/daughters.go +++ b/cmd/neofs-node/reputation/intermediate/daughters.go @@ -8,7 +8,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/util/logger" ) -type DaughterStorage struct { +type DaughterStorageWriterProvider struct { Log *logger.Logger Storage *daughters.Storage } @@ -27,7 +27,7 @@ func (w *DaughterTrustWriter) Close() error { return nil } -func (s *DaughterStorage) InitWriter(_ reputationcommon.Context) (reputationcommon.Writer, error) { +func (s *DaughterStorageWriterProvider) InitWriter(_ reputationcommon.Context) (reputationcommon.Writer, error) { return &DaughterTrustWriter{ log: s.Log, storage: s.Storage,