2021-04-29 05:30:41 +00:00
|
|
|
package intermediate
|
|
|
|
|
|
|
|
import (
|
2021-05-04 10:10:41 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2021-05-04 06:16:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
2021-04-29 05:30:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
|
|
|
|
eigencalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
|
|
|
eigentrustctrl "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/controller"
|
|
|
|
)
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
// InitialTrustSource is implementation of the
|
|
|
|
// reputation/eigentrust/calculator's InitialTrustSource interface.
|
|
|
|
type InitialTrustSource struct {
|
2021-05-04 10:10:41 +00:00
|
|
|
NetMap netmap.Source
|
2021-05-04 06:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:10:41 +00:00
|
|
|
var ErrEmptyNetMap = errors.New("empty NepMap")
|
|
|
|
|
2021-05-04 06:16:51 +00:00
|
|
|
// InitialTrust returns `initialTrust` as initial trust value.
|
|
|
|
func (i InitialTrustSource) InitialTrust(reputation.PeerID) (reputation.TrustValue, error) {
|
2021-05-04 10:10:41 +00:00
|
|
|
nm, err := i.NetMap.GetNetMap(1)
|
|
|
|
if err != nil {
|
|
|
|
return reputation.TrustZero, fmt.Errorf("failed to get NetMap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeCount := reputation.TrustValueFromFloat64(float64(len(nm.Nodes)))
|
|
|
|
if nodeCount == 0 {
|
|
|
|
return reputation.TrustZero, ErrEmptyNetMap
|
|
|
|
}
|
|
|
|
|
|
|
|
return reputation.TrustOne.Div(nodeCount), nil
|
2021-05-04 06:16:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
// DaughtersTrustCalculator wraps EigenTrust calculator and implements
|
|
|
|
// eigentrust/calculator's DaughtersTrustCalculator interface.
|
|
|
|
type DaughtersTrustCalculator struct {
|
|
|
|
Calculator *eigencalc.Calculator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate converts and passes values to wrapped calculator.
|
|
|
|
func (c *DaughtersTrustCalculator) Calculate(ctx eigentrustctrl.IterationContext) {
|
|
|
|
calcPrm := eigencalc.CalculatePrm{}
|
|
|
|
epochIteration := eigentrust.EpochIteration{}
|
|
|
|
|
|
|
|
epochIteration.SetEpoch(ctx.Epoch())
|
|
|
|
epochIteration.SetI(ctx.I())
|
|
|
|
|
|
|
|
calcPrm.SetLast(ctx.Last())
|
|
|
|
calcPrm.SetEpochIteration(epochIteration)
|
|
|
|
|
|
|
|
c.Calculator.Calculate(calcPrm)
|
|
|
|
}
|
2021-04-29 06:03:24 +00:00
|
|
|
|
|
|
|
// AlphaProvider provides required alpha parameter of eigen trust algorithm.
|
|
|
|
// TODO: decide if `Alpha` should be dynamically read from global config. #497
|
|
|
|
type AlphaProvider struct {
|
|
|
|
Alpha float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap AlphaProvider) EigenTrustAlpha() (float64, error) {
|
|
|
|
return ap.Alpha, nil
|
|
|
|
}
|