[#488] reputation: Add commentaries

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-29 09:33:09 +03:00 committed by Alex Vanin
parent 2c8c9f69c8
commit eb74a9cafc
8 changed files with 38 additions and 13 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)
}

View file

@ -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()
{

View file

@ -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),
}
}