[#482] reputation/router: Use trusting peer

In route building use `trusting` peer
field of `reputation.Trust` struct
instead of `reputation.Peer`.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-17 19:15:38 +03:00 committed by Pavel Karpy
parent 1c92dc2414
commit 0a16aaacb1
5 changed files with 13 additions and 12 deletions

View file

@ -415,7 +415,8 @@ func (s *reputationServer) SendLocalTrust(ctx context.Context, req *v2reputation
}
for _, trust := range body.GetTrusts() {
if err = s.processTrust(body.GetEpoch(), apiToLocalTrust(trust), passedRoute, w); err != nil {
err = s.processTrust(body.GetEpoch(), apiToLocalTrust(trust, passedRoute[0].PublicKey()), passedRoute, w)
if err != nil {
return nil, errors.Wrap(err, "could not write one of trusts")
}
}
@ -434,18 +435,20 @@ func (s *reputationServer) SendIntermediateResult(_ context.Context, req *v2repu
return resp, nil
}
func apiToLocalTrust(t *v2reputation.Trust) reputation.Trust {
// apiToLocalTrust converts v2 Trust to local reputation.Trust, adding trustingPeer.
func apiToLocalTrust(t *v2reputation.Trust, trustingPeer []byte) reputation.Trust {
localTrust := reputation.Trust{}
localTrust.SetValue(reputation.TrustValueFromFloat64(t.GetValue()))
localTrust.SetPeer(reputation.PeerIDFromBytes(t.GetPeer().GetValue()))
localTrust.SetTrustingPeer(reputation.PeerIDFromBytes(trustingPeer))
return localTrust
}
func (s *reputationServer) processTrust(epoch uint64, t reputation.Trust,
passedRoute []reputationroute.ServerInfo, w trustcontroller.Writer) error {
err := reputationroute.CheckRoute(s.routeBuilder, epoch, reputation.PeerIDFromBytes(passedRoute[0].PublicKey()), passedRoute)
err := reputationroute.CheckRoute(s.routeBuilder, epoch, t, passedRoute)
if err != nil {
return errors.Wrap(err, "wrong route of reputation trust value")
}

View file

@ -69,9 +69,7 @@ func (w *trustWriter) Write(t reputation.Trust) error {
w.routeMtx.Lock()
defer w.routeMtx.Unlock()
localPeerID := reputation.PeerIDFromBytes(w.router.localSrvInfo.PublicKey())
route, err := w.router.routeBuilder.NextStage(w.ctx.Epoch(), localPeerID, w.ctx.passedRoute)
route, err := w.router.routeBuilder.NextStage(w.ctx.Epoch(), t, w.ctx.passedRoute)
if err != nil {
return err
} else if len(route) == 0 {

View file

@ -22,13 +22,13 @@ type ServerInfo interface {
// Builder groups methods to route values in the network.
type Builder interface {
// NextStage must return next group of route points
// for passed epoch and PeerID of the current route point.
// for passed epoch and trust values.
// Implementation must take into account already passed route points.
//
// Empty passed list means being at the starting point of the route.
//
// Must return empty list and no error if the endpoint of the route is reached.
NextStage(epoch uint64, p reputation.PeerID, passed []ServerInfo) ([]ServerInfo, error)
NextStage(epoch uint64, t reputation.Trust, passed []ServerInfo) ([]ServerInfo, error)
}
// RemoteWriterProvider describes the component

View file

@ -9,12 +9,12 @@ import (
// NextStage builds Manager list for node and returns it directly.
//
// If passed route has more than one point, then endpoint of the route is reached.
func (b *Builder) NextStage(epoch uint64, p reputation.PeerID, passed []reputationroute.ServerInfo) ([]reputationroute.ServerInfo, error) {
func (b *Builder) NextStage(epoch uint64, t reputation.Trust, passed []reputationroute.ServerInfo) ([]reputationroute.ServerInfo, error) {
if len(passed) > 1 {
return nil, nil
}
route, err := b.managerBuilder.BuildManagers(epoch, p)
route, err := b.managerBuilder.BuildManagers(epoch, t.TrustingPeer())
if err != nil {
return nil, errors.Wrapf(err, "could not build managers for epoch: %d", epoch)
}

View file

@ -12,9 +12,9 @@ var errWrongRoute = errors.New("wrong route")
// CheckRoute checks if the route is a route correctly constructed by the builder for value a.
//
// Returns nil if route is correct, otherwise an error clarifying the inconsistency.
func CheckRoute(builder Builder, epoch uint64, peer reputation.PeerID, route []ServerInfo) error {
func CheckRoute(builder Builder, epoch uint64, t reputation.Trust, route []ServerInfo) error {
for i := 1; i < len(route); i++ {
servers, err := builder.NextStage(epoch, peer, route[:i])
servers, err := builder.NextStage(epoch, t, route[:i])
if err != nil {
return err
} else if len(servers) == 0 {