2021-04-23 14:51:51 +00:00
|
|
|
package intermediate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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
|
|
|
|
2021-04-29 06:33:09 +00:00
|
|
|
// ConsumerStorageWriterProvider is implementation of reputation.WriterProvider
|
|
|
|
// interface that provides ConsumerTrustWriter writer.
|
2021-04-23 14:51:51 +00:00
|
|
|
type ConsumerStorageWriterProvider struct {
|
|
|
|
Log *logger.Logger
|
|
|
|
Storage *consumerstorage.Storage
|
|
|
|
}
|
|
|
|
|
2021-04-29 06:33:09 +00:00
|
|
|
// 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.
|
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-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
|
|
|
|
}
|