From b293ea3ba15aad9e4e7a7fe566d9bb504facdcae Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 21 Apr 2021 13:39:08 +0300 Subject: [PATCH] [#488] reputation/intermediate: Add implementation of `DaughterTrustIteratorProvider` interface Signed-off-by: Pavel Karpy --- .../reputation/intermediate/storage.go | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 cmd/neofs-node/reputation/intermediate/storage.go diff --git a/cmd/neofs-node/reputation/intermediate/storage.go b/cmd/neofs-node/reputation/intermediate/storage.go new file mode 100644 index 000000000..c3053e346 --- /dev/null +++ b/cmd/neofs-node/reputation/intermediate/storage.go @@ -0,0 +1,92 @@ +package intermediate + +import ( + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + eigentrustcalc "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/services/reputation/eigentrust/storage/daughters" +) + +// InitialTrustSource is implementation of the +// reputation/eigentrust/calculator's InitialTrustSource interface. +type InitialTrustSource struct{} + +// InitialTrust returns `1` as initial trust value. +func (i InitialTrustSource) InitialTrust(reputation.PeerID) (reputation.TrustValue, error) { + return reputation.TrustOne, nil +} + +// DaughterTrustIteratorProvider is implementation of the +// reputation/eigentrust/calculator's DaughterTrustIteratorProvider interface. +type DaughterTrustIteratorProvider struct { + daughterStorage *daughters.Storage + consumerStorage *consumerstorage.Storage +} + +type ErrNoData struct { + hasDaughter bool + daughter reputation.PeerID + epoch uint64 +} + +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 daughter data in %d epoch", e.epoch) +} + +// InitDaughterIterator returns iterator over received +// local trusts for ctx.Epoch() epoch from daughter p. +// +// Returns ErrNoData if there is no trust data for +// specified epoch and daughter's PeerId. +func (ip *DaughterTrustIteratorProvider) InitDaughterIterator(ctx eigentrustcalc.Context, + p reputation.PeerID) (eigentrustcalc.TrustIterator, error) { + daughterIterator, ok := ip.daughterStorage.DaughterTrusts(ctx.Epoch(), p) + if !ok { + return nil, &ErrNoData{ + daughter: p, + hasDaughter: true, + epoch: ctx.Epoch(), + } + } + + return daughterIterator, nil +} + +// InitAllDaughtersIterator returns iterator over all +// daughters of the current node(manager) and all local +// trusts received from them for ctx.Epoch() epoch. +// +// Returns ErrNoData if there is no trust data for +// specified epoch. +func (ip *DaughterTrustIteratorProvider) InitAllDaughtersIterator( + ctx eigentrustcalc.Context) (eigentrustcalc.PeerTrustsIterator, error) { + iter, ok := ip.daughterStorage.AllDaughterTrusts(ctx.Epoch()) + if !ok { + return nil, &ErrNoData{epoch: ctx.Epoch()} + } + + return iter, nil + +} + +// InitConsumersIterator returns iterator over all daughters +// of the current node(manager) and all their consumers' local +// trusts for ctx.Epoch() epoch and ctx.I() iteration. + +// Returns ErrNoData if there is no trust data for +// specified epoch and iteration. +func (ip *DaughterTrustIteratorProvider) InitConsumersIterator( + ctx eigentrustcalc.Context) (eigentrustcalc.PeerTrustsIterator, error) { + consumerIterator, ok := ip.consumerStorage.Consumers(ctx.Epoch(), ctx.I()) + if !ok { + return nil, &ErrNoData{epoch: ctx.Epoch()} + } + + return consumerIterator, nil +}