From eb74a9cafc520a0d5c5be1246fadc7c4849a2368 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 29 Apr 2021 09:33:09 +0300 Subject: [PATCH] [#488] reputation: Add commentaries Signed-off-by: Pavel Karpy --- cmd/neofs-node/reputation/common/util.go | 3 +++ .../reputation/intermediate/consumers.go | 5 +++++ .../reputation/intermediate/contract.go | 5 ++++- .../reputation/intermediate/daughters.go | 5 +++++ .../reputation/intermediate/storage.go | 3 +-- .../reputation/eigentrust/calculator/deps.go | 10 ++++++++++ .../eigentrust/storage/daughters/calls.go | 16 ++++++++-------- .../eigentrust/storage/daughters/storage.go | 4 ++-- 8 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cmd/neofs-node/reputation/common/util.go b/cmd/neofs-node/reputation/common/util.go index 26b1b8ce..a5a5dbd6 100644 --- a/cmd/neofs-node/reputation/common/util.go +++ b/cmd/neofs-node/reputation/common/util.go @@ -8,6 +8,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/services/reputation" ) +// EpochContext is a std context extended with epoch data. type EpochContext struct { context.Context E uint64 @@ -27,6 +28,8 @@ func (NopReputationWriter) Close() error { return nil } +// OnlyKeyRemoteServerInfo if implementation of reputation.ServerInfo +// interface but with only public key data. type OnlyKeyRemoteServerInfo struct { Key []byte } diff --git a/cmd/neofs-node/reputation/intermediate/consumers.go b/cmd/neofs-node/reputation/intermediate/consumers.go index 04814712..b812c096 100644 --- a/cmd/neofs-node/reputation/intermediate/consumers.go +++ b/cmd/neofs-node/reputation/intermediate/consumers.go @@ -14,11 +14,16 @@ import ( var ErrIncorrectContext = errors.New("could not write intermediate trust: passed context incorrect") +// ConsumerStorageWriterProvider is implementation of reputation.WriterProvider +// interface that provides ConsumerTrustWriter writer. type ConsumerStorageWriterProvider struct { Log *logger.Logger Storage *consumerstorage.Storage } +// ConsumerTrustWriter is implementation of reputation.Writer interface +// that writes passed consumer's Trust values to Consumer storage. After writing +// that values can be used in eigenTrust algorithm's iterations. type ConsumerTrustWriter struct { log *logger.Logger storage *consumerstorage.Storage diff --git a/cmd/neofs-node/reputation/intermediate/contract.go b/cmd/neofs-node/reputation/intermediate/contract.go index a86acddc..d11c5124 100644 --- a/cmd/neofs-node/reputation/intermediate/contract.go +++ b/cmd/neofs-node/reputation/intermediate/contract.go @@ -42,6 +42,8 @@ func NewFinalWriterProvider(prm FinalWriterProviderPrm, opts ...FinalWriterOptio } } +// FinalWriterProvider is implementation of reputation.eigentrust.calculator +// IntermediateWriterProvider interface. It inits FinalWriter. type FinalWriterProvider struct { prm FinalWriterProviderPrm opts *finalWriterOptions @@ -55,9 +57,10 @@ func (fwp FinalWriterProvider) InitIntermediateWriter( client: fwp.prm.Client, l: fwp.opts.log, }, nil - } +// FinalWriter is implementation of reputation.eigentrust.calculator IntermediateWriter +// interface that writes GlobalTrust to contract directly. type FinalWriter struct { privatKey *ecdsa.PrivateKey pubKey []byte diff --git a/cmd/neofs-node/reputation/intermediate/daughters.go b/cmd/neofs-node/reputation/intermediate/daughters.go index 271d31d3..f3f3eeae 100644 --- a/cmd/neofs-node/reputation/intermediate/daughters.go +++ b/cmd/neofs-node/reputation/intermediate/daughters.go @@ -8,11 +8,16 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/util/logger" ) +// DaughterStorageWriterProvider is implementation of reputation.WriterProvider +// interface that provides DaughterTrustWriter writer. type DaughterStorageWriterProvider struct { Log *logger.Logger Storage *daughters.Storage } +// DaughterTrustWriter is implementation of reputation.Writer interface +// that writes passed daughter's Trust values to Daughter storage. After writing +// that values can be used in eigenTrust algorithm's iterations. type DaughterTrustWriter struct { log *logger.Logger storage *daughters.Storage diff --git a/cmd/neofs-node/reputation/intermediate/storage.go b/cmd/neofs-node/reputation/intermediate/storage.go index bf62019c..c0a4d6de 100644 --- a/cmd/neofs-node/reputation/intermediate/storage.go +++ b/cmd/neofs-node/reputation/intermediate/storage.go @@ -33,7 +33,7 @@ type ErrNoData struct { func (e *ErrNoData) Error() string { if e.hasDaughter { - return fmt.Sprintf("no data in %d epoch for peer: %s", e.epoch, e.daughter) + return fmt.Sprintf("no data in %d epoch for peer: %s", e.epoch, string(e.daughter.Bytes())) } return fmt.Sprintf("no daughter data in %d epoch", e.epoch) @@ -72,7 +72,6 @@ func (ip *DaughterTrustIteratorProvider) InitAllDaughtersIterator( } return iter, nil - } // InitConsumersIterator returns iterator over all daughters diff --git a/pkg/services/reputation/eigentrust/calculator/deps.go b/pkg/services/reputation/eigentrust/calculator/deps.go index aed381a5..eef45fe1 100644 --- a/pkg/services/reputation/eigentrust/calculator/deps.go +++ b/pkg/services/reputation/eigentrust/calculator/deps.go @@ -18,10 +18,15 @@ type Context interface { I() uint32 } +// InitialTrustSource must provide initial(non-calculated) +// trusts to current node's daughter. Realization may depends +// on daughter. type InitialTrustSource interface { InitialTrust(reputation.PeerID) (reputation.TrustValue, error) } +// TrustIterator must iterate over all retrieved(or calculated) trusts +// and call passed TrustHandler on them. type TrustIterator interface { Iterate(reputation.TrustHandler) error } @@ -52,10 +57,15 @@ type DaughterTrustIteratorProvider interface { InitConsumersIterator(Context) (PeerTrustsIterator, error) } +// IntermediateWriter must write intermediate result to contract. +// It may depends on realization either trust is sent directly to contract +// or via redirecting to other node. type IntermediateWriter interface { WriteIntermediateTrust(eigentrust.IterationTrust) error } +// IntermediateWriterProvider must provide ready-to-work +// IntermediateWriter. type IntermediateWriterProvider interface { InitIntermediateWriter(Context) (IntermediateWriter, error) } diff --git a/pkg/services/reputation/eigentrust/storage/daughters/calls.go b/pkg/services/reputation/eigentrust/storage/daughters/calls.go index b61af14c..96fd39d1 100644 --- a/pkg/services/reputation/eigentrust/storage/daughters/calls.go +++ b/pkg/services/reputation/eigentrust/storage/daughters/calls.go @@ -9,14 +9,14 @@ import ( // Put saves daughter peer's trust to its provider for the epoch. func (x *Storage) Put(epoch uint64, trust reputation.Trust) { - var s *daughterStorage + var s *DaughterStorage x.mtx.Lock() { s = x.mItems[epoch] if s == nil { - s = &daughterStorage{ + s = &DaughterStorage{ mItems: make(map[reputation.PeerID]*DaughterTrusts, 1), } @@ -34,7 +34,7 @@ func (x *Storage) Put(epoch uint64, trust reputation.Trust) { // Returns false if there is no data for the epoch and daughter. func (x *Storage) DaughterTrusts(epoch uint64, daughter reputation.PeerID) (*DaughterTrusts, bool) { var ( - s *daughterStorage + s *DaughterStorage ok bool ) @@ -56,7 +56,7 @@ func (x *Storage) DaughterTrusts(epoch uint64, daughter reputation.PeerID) (*Dau // AllDaughterTrusts returns daughter iterator for the epoch. // // Returns false if there is no data for the epoch and daughter. -func (x *Storage) AllDaughterTrusts(epoch uint64) (*daughterStorage, bool) { +func (x *Storage) AllDaughterTrusts(epoch uint64) (*DaughterStorage, bool) { x.mtx.RLock() defer x.mtx.RUnlock() @@ -66,7 +66,7 @@ func (x *Storage) AllDaughterTrusts(epoch uint64) (*daughterStorage, bool) { } // maps IDs of daughter peers to repositories of the local trusts to their providers. -type daughterStorage struct { +type DaughterStorage struct { mtx sync.RWMutex mItems map[reputation.PeerID]*DaughterTrusts @@ -75,7 +75,7 @@ type daughterStorage struct { // Iterate passes IDs of the daughter peers with their trusts to h. // // Returns errors from h directly. -func (x *daughterStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error) { +func (x *DaughterStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error) { x.mtx.RLock() { @@ -91,7 +91,7 @@ func (x *daughterStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error return } -func (x *daughterStorage) put(trust reputation.Trust) { +func (x *DaughterStorage) put(trust reputation.Trust) { var dt *DaughterTrusts x.mtx.Lock() @@ -114,7 +114,7 @@ func (x *daughterStorage) put(trust reputation.Trust) { dt.put(trust) } -func (x *daughterStorage) daughterTrusts(id reputation.PeerID) (dt *DaughterTrusts, ok bool) { +func (x *DaughterStorage) daughterTrusts(id reputation.PeerID) (dt *DaughterTrusts, ok bool) { x.mtx.RLock() { diff --git a/pkg/services/reputation/eigentrust/storage/daughters/storage.go b/pkg/services/reputation/eigentrust/storage/daughters/storage.go index 999b9d02..26399fce 100644 --- a/pkg/services/reputation/eigentrust/storage/daughters/storage.go +++ b/pkg/services/reputation/eigentrust/storage/daughters/storage.go @@ -24,7 +24,7 @@ type Prm struct{} type Storage struct { mtx sync.RWMutex - mItems map[uint64]*daughterStorage + mItems map[uint64]*DaughterStorage } // New creates a new instance of the Storage. @@ -33,6 +33,6 @@ type Storage struct { // initialization and is completely ready for work. func New(_ Prm) *Storage { return &Storage{ - mItems: make(map[uint64]*daughterStorage), + mItems: make(map[uint64]*DaughterStorage), } }