2021-04-10 12:02:51 +00:00
|
|
|
package eigentrustcalc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/eigentrust"
|
|
|
|
apireputation "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/reputation"
|
2021-04-10 12:02:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CalculatePrm struct {
|
|
|
|
last bool
|
|
|
|
|
|
|
|
ei eigentrust.EpochIteration
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
func (p *CalculatePrm) SetLast(last bool) {
|
|
|
|
p.last = last
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
func (p *CalculatePrm) SetEpochIteration(ei eigentrust.EpochIteration) {
|
|
|
|
p.ei = ei
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calculator) Calculate(prm CalculatePrm) {
|
2021-04-29 06:03:24 +00:00
|
|
|
alpha, err := c.prm.AlphaProvider.EigenTrustAlpha()
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug(
|
|
|
|
"failed to get alpha param",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.alpha = reputation.TrustValueFromFloat64(alpha)
|
|
|
|
c.beta = reputation.TrustValueFromFloat64(1 - alpha)
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
ctx := eigentrust.IterContext{
|
2021-04-10 12:02:51 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
EpochIteration: prm.ei,
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := ctx.I()
|
|
|
|
|
2021-03-01 19:01:45 +00:00
|
|
|
log := c.opts.log.With(
|
|
|
|
zap.Uint64("epoch", ctx.Epoch()),
|
|
|
|
zap.Uint32("iteration", iter),
|
|
|
|
)
|
|
|
|
|
2021-04-10 12:02:51 +00:00
|
|
|
if iter == 0 {
|
|
|
|
c.sendInitialValues(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrement iteration number to select the values collected
|
|
|
|
// on the previous stage
|
|
|
|
ctx.SetI(iter - 1)
|
|
|
|
|
|
|
|
consumersIter, err := c.prm.DaughterTrustSource.InitConsumersIterator(ctx)
|
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
log.Debug("consumers trust iterator's init failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue with initial iteration number
|
|
|
|
ctx.SetI(iter)
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
err = consumersIter.Iterate(func(daughter apireputation.PeerID, iter TrustIterator) error {
|
2021-04-10 12:02:51 +00:00
|
|
|
err := c.prm.WorkerPool.Submit(func() {
|
|
|
|
c.iterateDaughter(iterDaughterPrm{
|
|
|
|
lastIter: prm.last,
|
|
|
|
ctx: ctx,
|
|
|
|
id: daughter,
|
|
|
|
consumersIter: iter,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
log.Debug("worker pool submit failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't stop trying
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
log.Debug("iterate daughter's consumers failed",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type iterDaughterPrm struct {
|
|
|
|
lastIter bool
|
|
|
|
|
|
|
|
ctx Context
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
id apireputation.PeerID
|
2021-04-10 12:02:51 +00:00
|
|
|
|
|
|
|
consumersIter TrustIterator
|
|
|
|
}
|
|
|
|
|
2023-03-20 07:01:27 +00:00
|
|
|
// nolint: funlen
|
2021-04-10 12:02:51 +00:00
|
|
|
func (c *Calculator) iterateDaughter(p iterDaughterPrm) {
|
|
|
|
initTrust, err := c.prm.InitialTrustSource.InitialTrust(p.id)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("get initial trust failure",
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("daughter", p.id),
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
daughterIter, err := c.prm.DaughterTrustSource.InitDaughterIterator(p.ctx, p.id)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("daughter trust iterator's init failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := reputation.TrustZero
|
|
|
|
|
|
|
|
err = p.consumersIter.Iterate(func(trust reputation.Trust) error {
|
|
|
|
if !p.lastIter {
|
|
|
|
select {
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
return p.ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sum.Add(trust.Value())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("iterate over daughter's trusts failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha * Pd
|
|
|
|
initTrust.Mul(c.alpha)
|
|
|
|
|
|
|
|
sum.Mul(c.beta)
|
|
|
|
sum.Add(initTrust)
|
|
|
|
|
|
|
|
var intermediateTrust eigentrust.IterationTrust
|
|
|
|
|
|
|
|
intermediateTrust.SetEpoch(p.ctx.Epoch())
|
2021-04-29 05:30:41 +00:00
|
|
|
intermediateTrust.SetPeer(p.id)
|
2021-04-10 12:02:51 +00:00
|
|
|
intermediateTrust.SetI(p.ctx.I())
|
|
|
|
|
|
|
|
if p.lastIter {
|
|
|
|
finalWriter, err := c.prm.FinalResultTarget.InitIntermediateWriter(p.ctx)
|
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("init writer failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediateTrust.SetValue(sum)
|
|
|
|
|
|
|
|
err = finalWriter.WriteIntermediateTrust(intermediateTrust)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("write final result failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-29 05:30:41 +00:00
|
|
|
intermediateWriter, err := c.prm.IntermediateValueTarget.InitWriter(p.ctx)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("init writer failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = daughterIter.Iterate(func(trust reputation.Trust) error {
|
|
|
|
select {
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
return p.ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
val := trust.Value()
|
|
|
|
val.Mul(sum)
|
|
|
|
|
|
|
|
trust.SetValue(val)
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
err := intermediateWriter.Write(trust)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("write value failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("iterate daughter trusts failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-29 05:30:41 +00:00
|
|
|
|
|
|
|
err = intermediateWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Error(
|
2021-03-01 19:01:45 +00:00
|
|
|
"could not close writer",
|
2021-04-29 05:30:41 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calculator) sendInitialValues(ctx Context) {
|
|
|
|
daughterIter, err := c.prm.DaughterTrustSource.InitAllDaughtersIterator(ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("all daughters trust iterator's init failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
intermediateWriter, err := c.prm.IntermediateValueTarget.InitWriter(ctx)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("init writer failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
err = daughterIter.Iterate(func(daughter apireputation.PeerID, iterator TrustIterator) error {
|
2021-04-10 12:02:51 +00:00
|
|
|
return iterator.Iterate(func(trust reputation.Trust) error {
|
|
|
|
trusted := trust.Peer()
|
|
|
|
|
|
|
|
initTrust, err := c.prm.InitialTrustSource.InitialTrust(trusted)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("get initial trust failure",
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("peer", trusted),
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
// don't stop on single failure
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
initTrust.Mul(trust.Value())
|
2021-04-29 05:30:41 +00:00
|
|
|
trust.SetValue(initTrust)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
err = intermediateWriter.Write(trust)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("write value failure",
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
// don't stop on single failure
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug("iterate over all daughters failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-29 05:30:41 +00:00
|
|
|
|
|
|
|
err = intermediateWriter.Close()
|
|
|
|
if err != nil {
|
2021-03-01 19:01:45 +00:00
|
|
|
c.opts.log.Debug("could not close writer",
|
2021-04-29 05:30:41 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|