2021-04-23 14:51:51 +00:00
|
|
|
package intermediate
|
|
|
|
|
|
|
|
import (
|
2021-03-01 19:01:45 +00:00
|
|
|
"encoding/hex"
|
|
|
|
|
2021-04-23 14:51:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
|
|
|
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"
|
2021-03-01 19:01:45 +00:00
|
|
|
"go.uber.org/zap"
|
2021-04-23 14:51:51 +00:00
|
|
|
)
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
var ErrIncorrectContextPanicMsg = "could not write intermediate trust: passed context incorrect"
|
2021-04-23 14:51:51 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ConsumerStorageWriterProvider is an implementation of the reputation.WriterProvider
|
2021-04-29 06:33:09 +00:00
|
|
|
// interface that provides ConsumerTrustWriter writer.
|
2021-04-23 14:51:51 +00:00
|
|
|
type ConsumerStorageWriterProvider struct {
|
|
|
|
Log *logger.Logger
|
|
|
|
Storage *consumerstorage.Storage
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ConsumerTrustWriter is an implementation of the reputation.Writer interface
|
|
|
|
// that writes passed consumer's Trust values to the Consumer storage. After writing
|
|
|
|
// that, values can be used in eigenTrust algorithm's iterations.
|
2021-04-23 14:51:51 +00:00
|
|
|
type ConsumerTrustWriter struct {
|
|
|
|
log *logger.Logger
|
|
|
|
storage *consumerstorage.Storage
|
2021-05-04 06:16:51 +00:00
|
|
|
eiCtx eigencalc.Context
|
2021-04-23 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
func (w *ConsumerTrustWriter) Write(t reputation.Trust) error {
|
2021-03-01 19:01:45 +00:00
|
|
|
w.log.Debug("writing received consumer's trusts",
|
|
|
|
zap.Uint64("epoch", w.eiCtx.Epoch()),
|
|
|
|
zap.Uint32("iteration", w.eiCtx.I()),
|
|
|
|
zap.String("trusting_peer", hex.EncodeToString(t.TrustingPeer().Bytes())),
|
|
|
|
zap.String("trusted_peer", hex.EncodeToString(t.Peer().Bytes())),
|
|
|
|
)
|
|
|
|
|
2021-04-23 14:51:51 +00:00
|
|
|
trust := eigentrust.IterationTrust{Trust: t}
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
trust.SetEpoch(w.eiCtx.Epoch())
|
|
|
|
trust.SetI(w.eiCtx.I())
|
2021-04-23 14:51:51 +00:00
|
|
|
|
|
|
|
w.storage.Put(trust)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ConsumerTrustWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
func (s *ConsumerStorageWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
|
|
|
|
eiCtx, ok := ctx.(eigencalc.Context)
|
|
|
|
if !ok {
|
|
|
|
panic(ErrIncorrectContextPanicMsg)
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:51:51 +00:00
|
|
|
return &ConsumerTrustWriter{
|
|
|
|
log: s.Log,
|
|
|
|
storage: s.Storage,
|
2021-05-04 06:16:51 +00:00
|
|
|
eiCtx: eiCtx,
|
2021-04-23 14:51:51 +00:00
|
|
|
}, nil
|
|
|
|
}
|