2021-04-21 10:39:08 +00:00
|
|
|
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 {
|
2021-04-29 05:30:41 +00:00
|
|
|
DaughterStorage *daughters.Storage
|
|
|
|
ConsumerStorage *consumerstorage.Storage
|
2021-04-21 10:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrNoData struct {
|
|
|
|
hasDaughter bool
|
|
|
|
daughter reputation.PeerID
|
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrNoData) Error() string {
|
|
|
|
if e.hasDaughter {
|
2021-04-29 06:33:09 +00:00
|
|
|
return fmt.Sprintf("no data in %d epoch for peer: %s", e.epoch, string(e.daughter.Bytes()))
|
2021-04-21 10:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-04-29 05:30:41 +00:00
|
|
|
daughterIterator, ok := ip.DaughterStorage.DaughterTrusts(ctx.Epoch(), p)
|
2021-04-21 10:39:08 +00:00
|
|
|
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) {
|
2021-04-29 05:30:41 +00:00
|
|
|
iter, ok := ip.DaughterStorage.AllDaughterTrusts(ctx.Epoch())
|
2021-04-21 10:39:08 +00:00
|
|
|
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) {
|
2021-04-29 05:30:41 +00:00
|
|
|
consumerIterator, ok := ip.ConsumerStorage.Consumers(ctx.Epoch(), ctx.I())
|
2021-04-21 10:39:08 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, &ErrNoData{epoch: ctx.Epoch()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumerIterator, nil
|
|
|
|
}
|