2021-03-23 18:48:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-24 09:57:50 +00:00
|
|
|
"context"
|
2021-03-23 18:48:50 +00:00
|
|
|
|
2021-03-24 09:57:50 +00:00
|
|
|
v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation"
|
|
|
|
v2reputationgrpc "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc"
|
2021-03-23 18:54:00 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2021-04-21 04:56:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/intermediate"
|
2021-04-18 08:51:49 +00:00
|
|
|
localreputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/local"
|
2021-03-23 18:54:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
2021-04-03 08:07:11 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2021-03-24 09:57:50 +00:00
|
|
|
grpcreputation "github.com/nspcc-dev/neofs-node/pkg/network/transport/reputation/grpc"
|
2021-03-23 18:48:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
2021-04-17 17:13:29 +00:00
|
|
|
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
|
2021-04-17 19:17:30 +00:00
|
|
|
reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router"
|
2021-04-21 04:56:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/storage/daughters"
|
2021-03-23 18:48:50 +00:00
|
|
|
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
|
2021-04-17 19:17:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/managers"
|
2021-03-23 18:48:50 +00:00
|
|
|
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
|
2021-03-24 09:57:50 +00:00
|
|
|
reputationrpc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/rpc"
|
2021-03-23 18:48:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"github.com/pkg/errors"
|
2021-03-23 18:51:31 +00:00
|
|
|
"go.uber.org/zap"
|
2021-03-23 18:48:50 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 18:54:00 +00:00
|
|
|
func initReputationService(c *cfg) {
|
|
|
|
// consider sharing this between application components
|
|
|
|
nmSrc := newCachedNetmapStorage(c.cfgNetmap.state, c.cfgNetmap.wrapper)
|
|
|
|
|
2021-04-21 04:56:38 +00:00
|
|
|
// storing calculated trusts as a daughter
|
2021-03-23 18:54:00 +00:00
|
|
|
c.cfgReputation.localTrustStorage = truststorage.New(truststorage.Prm{})
|
|
|
|
|
2021-04-21 04:56:38 +00:00
|
|
|
// storing received trusts as a manager
|
|
|
|
daughterStorage := &intermediate.DaughterStorage{
|
|
|
|
Log: c.log,
|
|
|
|
Storage: daughters.New(daughters.Prm{}),
|
|
|
|
}
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
trustStorage := &localreputation.TrustStorage{
|
|
|
|
Log: c.log,
|
|
|
|
Storage: c.cfgReputation.localTrustStorage,
|
|
|
|
NmSrc: nmSrc,
|
|
|
|
LocalKey: crypto.MarshalPublicKey(&c.key.PublicKey),
|
2021-03-23 18:54:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
managerBuilder := localreputation.NewManagerBuilder(
|
|
|
|
localreputation.ManagersPrm{
|
|
|
|
NetMapSource: nmSrc,
|
|
|
|
},
|
|
|
|
localreputation.WithLogger(c.log),
|
|
|
|
)
|
2021-04-03 08:07:11 +00:00
|
|
|
|
|
|
|
routeBuilder := managers.New(managers.Prm{
|
|
|
|
ManagerBuilder: managerBuilder,
|
|
|
|
})
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
remoteLocalTrustProvider := localreputation.NewRemoteTrustProvider(
|
|
|
|
localreputation.RemoteProviderPrm{
|
|
|
|
LocalAddrSrc: c,
|
2021-04-21 04:56:38 +00:00
|
|
|
DeadEndProvider: daughterStorage,
|
2021-04-18 08:51:49 +00:00
|
|
|
ClientCache: cache.NewSDKClientCache(),
|
|
|
|
Key: c.key,
|
|
|
|
},
|
|
|
|
)
|
2021-04-03 08:07:11 +00:00
|
|
|
|
2021-04-17 19:17:30 +00:00
|
|
|
router := reputationrouter.New(
|
|
|
|
reputationrouter.Prm{
|
2021-04-03 08:07:11 +00:00
|
|
|
LocalServerInfo: c,
|
|
|
|
RemoteWriterProvider: remoteLocalTrustProvider,
|
|
|
|
Builder: routeBuilder,
|
|
|
|
})
|
|
|
|
|
2021-03-23 18:54:00 +00:00
|
|
|
c.cfgReputation.localTrustCtrl = trustcontroller.New(trustcontroller.Prm{
|
|
|
|
LocalTrustSource: trustStorage,
|
2021-04-03 08:07:11 +00:00
|
|
|
LocalTrustTarget: router,
|
2021-03-23 18:54:00 +00:00
|
|
|
})
|
|
|
|
|
2021-04-13 12:53:58 +00:00
|
|
|
addNewEpochAsyncNotificationHandler(
|
|
|
|
c,
|
|
|
|
func(ev event.Event) {
|
|
|
|
var reportPrm trustcontroller.ReportPrm
|
2021-03-23 18:54:00 +00:00
|
|
|
|
2021-04-13 12:53:58 +00:00
|
|
|
// report collected values from previous epoch
|
|
|
|
reportPrm.SetEpoch(ev.(netmap.NewEpoch).EpochNumber() - 1)
|
2021-03-23 18:54:00 +00:00
|
|
|
|
2021-04-13 12:53:58 +00:00
|
|
|
c.cfgReputation.localTrustCtrl.Report(reportPrm)
|
|
|
|
},
|
|
|
|
)
|
2021-03-24 09:57:50 +00:00
|
|
|
|
|
|
|
v2reputationgrpc.RegisterReputationServiceServer(c.cfgGRPC.server,
|
|
|
|
grpcreputation.New(
|
|
|
|
reputationrpc.NewSignService(
|
|
|
|
c.key,
|
|
|
|
reputationrpc.NewResponseService(
|
2021-04-06 17:15:09 +00:00
|
|
|
&reputationServer{
|
|
|
|
cfg: c,
|
|
|
|
log: c.log,
|
|
|
|
router: router,
|
|
|
|
routeBuilder: routeBuilder,
|
2021-03-24 09:57:50 +00:00
|
|
|
},
|
|
|
|
c.respSvc,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-04-15 14:57:29 +00:00
|
|
|
|
|
|
|
// initialize eigen trust block timer
|
|
|
|
durationMeter := NewEigenTrustDuration(c.cfgNetmap.wrapper)
|
|
|
|
|
|
|
|
newEigenTrustIterTimer(c, durationMeter, func() {
|
|
|
|
c.log.Debug("todo: start next EigenTrust iteration round")
|
|
|
|
})
|
|
|
|
|
|
|
|
addNewEpochAsyncNotificationHandler(
|
|
|
|
c,
|
|
|
|
func(e event.Event) {
|
|
|
|
durationMeter.Update() // recalculate duration of one iteration round
|
|
|
|
|
|
|
|
err := c.cfgMorph.eigenTrustTimer.Reset() // start iteration rounds again
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't reset block timer to start eigen trust calculations again",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-03-24 09:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
type reputationServer struct {
|
|
|
|
*cfg
|
|
|
|
log *logger.Logger
|
2021-04-17 17:13:29 +00:00
|
|
|
router reputationcommon.WriterProvider
|
2021-04-17 19:17:30 +00:00
|
|
|
routeBuilder reputationrouter.Builder
|
2021-04-06 17:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *reputationServer) SendLocalTrust(ctx context.Context, req *v2reputation.SendLocalTrustRequest) (*v2reputation.SendLocalTrustResponse, error) {
|
2021-04-17 19:17:30 +00:00
|
|
|
var passedRoute []reputationrouter.ServerInfo
|
2021-04-06 17:15:09 +00:00
|
|
|
|
|
|
|
for hdr := req.GetVerificationHeader(); hdr != nil; hdr = hdr.GetOrigin() {
|
2021-04-18 08:51:49 +00:00
|
|
|
passedRoute = append(passedRoute, &localreputation.OnlyKeyRemoteServerInfo{
|
|
|
|
Key: hdr.GetBodySignature().GetKey(),
|
2021-04-06 17:15:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for left, right := 0, len(passedRoute)-1; left < right; left, right = left+1, right-1 {
|
|
|
|
passedRoute[left], passedRoute[right] = passedRoute[right], passedRoute[left]
|
|
|
|
}
|
|
|
|
|
|
|
|
passedRoute = append(passedRoute, s)
|
|
|
|
|
2021-03-24 09:57:50 +00:00
|
|
|
body := req.GetBody()
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
eCtx := &localreputation.EpochContext{
|
2021-04-06 17:15:09 +00:00
|
|
|
Context: ctx,
|
2021-04-18 08:51:49 +00:00
|
|
|
E: body.GetEpoch(),
|
2021-04-06 17:15:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 19:17:30 +00:00
|
|
|
w, err := s.router.InitWriter(reputationrouter.NewRouteContext(eCtx, passedRoute))
|
2021-04-06 17:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not initialize local trust writer")
|
|
|
|
}
|
2021-03-24 09:57:50 +00:00
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
for _, trust := range body.GetTrusts() {
|
2021-04-17 16:15:38 +00:00
|
|
|
err = s.processTrust(body.GetEpoch(), apiToLocalTrust(trust, passedRoute[0].PublicKey()), passedRoute, w)
|
|
|
|
if err != nil {
|
2021-04-06 17:15:09 +00:00
|
|
|
return nil, errors.Wrap(err, "could not write one of trusts")
|
|
|
|
}
|
2021-03-24 09:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(v2reputation.SendLocalTrustResponse)
|
|
|
|
resp.SetBody(new(v2reputation.SendLocalTrustResponseBody))
|
|
|
|
|
|
|
|
return resp, nil
|
2021-03-23 18:54:00 +00:00
|
|
|
}
|
2021-04-05 09:27:58 +00:00
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
func (s *reputationServer) SendIntermediateResult(_ context.Context, req *v2reputation.SendIntermediateResultRequest) (*v2reputation.SendIntermediateResultResponse, error) {
|
2021-04-05 09:27:58 +00:00
|
|
|
resp := new(v2reputation.SendIntermediateResultResponse)
|
|
|
|
|
|
|
|
// todo: implement me
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-04-06 17:15:09 +00:00
|
|
|
|
2021-04-17 16:15:38 +00:00
|
|
|
// apiToLocalTrust converts v2 Trust to local reputation.Trust, adding trustingPeer.
|
|
|
|
func apiToLocalTrust(t *v2reputation.Trust, trustingPeer []byte) reputation.Trust {
|
2021-04-06 17:15:09 +00:00
|
|
|
localTrust := reputation.Trust{}
|
|
|
|
|
|
|
|
localTrust.SetValue(reputation.TrustValueFromFloat64(t.GetValue()))
|
|
|
|
localTrust.SetPeer(reputation.PeerIDFromBytes(t.GetPeer().GetValue()))
|
2021-04-17 16:15:38 +00:00
|
|
|
localTrust.SetTrustingPeer(reputation.PeerIDFromBytes(trustingPeer))
|
2021-04-06 17:15:09 +00:00
|
|
|
|
|
|
|
return localTrust
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *reputationServer) processTrust(epoch uint64, t reputation.Trust,
|
2021-04-17 19:17:30 +00:00
|
|
|
passedRoute []reputationrouter.ServerInfo, w reputationcommon.Writer) error {
|
|
|
|
err := reputationrouter.CheckRoute(s.routeBuilder, epoch, t, passedRoute)
|
2021-04-06 17:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "wrong route of reputation trust value")
|
|
|
|
}
|
|
|
|
|
2021-04-21 04:56:38 +00:00
|
|
|
return w.Write(&localreputation.EpochContext{E: epoch}, t)
|
2021-04-06 17:15:09 +00:00
|
|
|
}
|