2021-03-23 18:48:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-03-24 09:57:50 +00:00
|
|
|
"context"
|
2021-04-03 08:07:11 +00:00
|
|
|
"crypto/ecdsa"
|
2021-03-23 18:51:31 +00:00
|
|
|
"encoding/hex"
|
2021-03-23 18:48:50 +00:00
|
|
|
|
2021-04-03 08:07:11 +00:00
|
|
|
"github.com/nspcc-dev/hrw"
|
|
|
|
apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
apiNetmap "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
reputationapi "github.com/nspcc-dev/neofs-api-go/pkg/reputation"
|
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-03-23 18:48:50 +00:00
|
|
|
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
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"
|
|
|
|
"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"
|
|
|
|
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
|
2021-04-03 08:07:11 +00:00
|
|
|
reputationroute "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route/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
|
|
|
)
|
|
|
|
|
|
|
|
type localTrustStorage struct {
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
storage *truststorage.Storage
|
|
|
|
|
|
|
|
nmSrc netmapcore.Source
|
|
|
|
|
|
|
|
localKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type localTrustIterator struct {
|
|
|
|
ctx trustcontroller.Context
|
|
|
|
|
|
|
|
storage *localTrustStorage
|
|
|
|
|
|
|
|
epochStorage *truststorage.EpochTrustValueStorage
|
|
|
|
}
|
|
|
|
|
2021-04-03 08:07:11 +00:00
|
|
|
type managerBuilder struct {
|
|
|
|
log *logger.Logger
|
|
|
|
nmSrc netmapcore.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLocalTrustProvider struct {
|
|
|
|
localAddrSrc network.LocalAddressSource
|
|
|
|
deadEndProvider trustcontroller.WriterProvider
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
clientCache interface {
|
|
|
|
Get(string) (apiClient.Client, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopReputationWriter struct{}
|
|
|
|
|
|
|
|
func (nopReputationWriter) Write(reputation.Trust) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopReputationWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLocalTrustWriter struct {
|
|
|
|
ctx trustcontroller.Context
|
|
|
|
client apiClient.Client
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
buf []*reputationapi.Trust
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *remoteLocalTrustWriter) Write(t reputation.Trust) error {
|
|
|
|
apiTrust := reputationapi.NewTrust()
|
|
|
|
|
|
|
|
apiPeer := reputationapi.NewPeerID()
|
|
|
|
apiPeer.SetPublicKey(t.Peer())
|
|
|
|
|
|
|
|
apiTrust.SetValue(t.Value().Float64())
|
|
|
|
apiTrust.SetPeer(apiPeer)
|
|
|
|
|
|
|
|
rtp.buf = append(rtp.buf, apiTrust)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *remoteLocalTrustWriter) Close() error {
|
|
|
|
prm := apiClient.SendLocalTrustPrm{}
|
|
|
|
|
|
|
|
prm.SetEpoch(rtp.ctx.Epoch())
|
|
|
|
prm.SetTrusts(rtp.buf)
|
|
|
|
|
|
|
|
_, err := rtp.client.SendLocalTrust(
|
|
|
|
rtp.ctx,
|
|
|
|
prm,
|
|
|
|
apiClient.WithKey(rtp.key),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLocalTrustWriterProvider struct {
|
|
|
|
client apiClient.Client
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type localTrustLogger struct {
|
|
|
|
ctx trustcontroller.Context
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localTrustLogger) Write(t reputation.Trust) error {
|
|
|
|
l.log.Info("new local trust",
|
|
|
|
zap.Uint64("epoch", l.ctx.Epoch()),
|
|
|
|
zap.String("peer", hex.EncodeToString(t.Peer().Bytes())),
|
|
|
|
zap.Stringer("value", t.Value()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*localTrustLogger) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) {
|
|
|
|
return &remoteLocalTrustWriter{
|
|
|
|
ctx: ctx,
|
|
|
|
client: rtwp.client,
|
|
|
|
key: rtwp.key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *remoteLocalTrustProvider) InitRemote(srv reputationroute.ServerInfo) (trustcontroller.WriterProvider, error) {
|
|
|
|
if srv == nil {
|
|
|
|
return rtp.deadEndProvider, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := srv.Address()
|
|
|
|
|
|
|
|
if rtp.localAddrSrc.LocalAddress().String() == srv.Address() {
|
|
|
|
// if local => return no-op writer
|
|
|
|
return trustcontroller.SimpleWriterProvider(new(nopReputationWriter)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ipAddr, err := network.IPAddrFromMultiaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not convert address to IP format")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := rtp.clientCache.Get(ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not initialize API client")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &remoteLocalTrustWriterProvider{
|
|
|
|
client: c,
|
|
|
|
key: rtp.key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildManagers sorts nodes in NetMap with HRW algorithms and
|
|
|
|
// takes the next node after the current one as the only manager.
|
|
|
|
func (mb *managerBuilder) BuildManagers(epoch uint64, p reputation.PeerID) ([]reputationroute.ServerInfo, error) {
|
|
|
|
nm, err := mb.nmSrc.GetNetMapByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a copy to keep order consistency of the origin netmap after sorting
|
|
|
|
nodes := make([]*apiNetmap.Node, len(nm.Nodes))
|
|
|
|
|
|
|
|
copy(nodes, nm.Nodes)
|
|
|
|
|
|
|
|
hrw.SortSliceByValue(nodes, epoch)
|
|
|
|
|
|
|
|
for i := range nodes {
|
|
|
|
if bytes.Equal(nodes[i].PublicKey(), p.Bytes()) {
|
|
|
|
managerIndex := i + 1
|
|
|
|
|
|
|
|
if managerIndex == len(nodes) {
|
|
|
|
managerIndex = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return []reputationroute.ServerInfo{nodes[managerIndex]}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:48:50 +00:00
|
|
|
func (s *localTrustStorage) InitIterator(ctx trustcontroller.Context) (trustcontroller.Iterator, error) {
|
|
|
|
epochStorage, err := s.storage.DataForEpoch(ctx.Epoch())
|
|
|
|
if err != nil && !errors.Is(err, truststorage.ErrNoPositiveTrust) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &localTrustIterator{
|
|
|
|
ctx: ctx,
|
|
|
|
storage: s,
|
|
|
|
epochStorage: epochStorage,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-03 08:07:11 +00:00
|
|
|
func (s *localTrustStorage) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) {
|
|
|
|
return &localTrustLogger{
|
|
|
|
ctx: ctx,
|
|
|
|
log: s.log,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:48:50 +00:00
|
|
|
func (it *localTrustIterator) Iterate(h reputation.TrustHandler) error {
|
|
|
|
if it.epochStorage != nil {
|
|
|
|
err := it.epochStorage.Iterate(h)
|
|
|
|
if !errors.Is(err, truststorage.ErrNoPositiveTrust) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nm, err := it.storage.nmSrc.GetNetMapByEpoch(it.ctx.Epoch())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out if local node is presented in netmap
|
|
|
|
localIndex := -1
|
|
|
|
|
|
|
|
for i := range nm.Nodes {
|
|
|
|
if bytes.Equal(nm.Nodes[i].PublicKey(), it.storage.localKey) {
|
|
|
|
localIndex = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(nm.Nodes)
|
|
|
|
if localIndex >= 0 && ln > 0 {
|
|
|
|
ln--
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate Pj http://ilpubs.stanford.edu:8090/562/1/2002-56.pdf Chapter 4.5.
|
|
|
|
p := reputation.TrustOne.Div(reputation.TrustValueFromInt(ln))
|
|
|
|
|
|
|
|
for i := range nm.Nodes {
|
|
|
|
if i == localIndex {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
trust := reputation.Trust{}
|
|
|
|
trust.SetPeer(reputation.PeerIDFromBytes(nm.Nodes[i].PublicKey()))
|
|
|
|
trust.SetValue(p)
|
|
|
|
|
|
|
|
if err := h(trust); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-23 18:51:31 +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)
|
|
|
|
|
|
|
|
c.cfgReputation.localTrustStorage = truststorage.New(truststorage.Prm{})
|
|
|
|
|
|
|
|
trustStorage := &localTrustStorage{
|
|
|
|
log: c.log,
|
|
|
|
storage: c.cfgReputation.localTrustStorage,
|
|
|
|
nmSrc: nmSrc,
|
|
|
|
localKey: crypto.MarshalPublicKey(&c.key.PublicKey),
|
|
|
|
}
|
|
|
|
|
2021-04-03 08:07:11 +00:00
|
|
|
managerBuilder := &managerBuilder{
|
|
|
|
log: c.log,
|
|
|
|
nmSrc: nmSrc,
|
|
|
|
}
|
|
|
|
|
|
|
|
routeBuilder := managers.New(managers.Prm{
|
|
|
|
ManagerBuilder: managerBuilder,
|
|
|
|
})
|
|
|
|
|
|
|
|
remoteLocalTrustProvider := &remoteLocalTrustProvider{
|
|
|
|
localAddrSrc: c,
|
|
|
|
deadEndProvider: trustStorage,
|
|
|
|
clientCache: cache.NewSDKClientCache(),
|
|
|
|
key: c.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
router := reputationroute.New(
|
|
|
|
reputationroute.Prm{
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
|
|
|
var reportPrm trustcontroller.ReportPrm
|
|
|
|
|
|
|
|
// report collected values from previous epoch
|
|
|
|
reportPrm.SetEpoch(ev.(netmap.NewEpoch).EpochNumber() - 1)
|
|
|
|
|
|
|
|
// TODO: implement and use worker pool [neofs-node#440]
|
|
|
|
go 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(
|
|
|
|
&loggingReputationServer{
|
|
|
|
log: c.log,
|
|
|
|
},
|
|
|
|
c.respSvc,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type loggingReputationServer struct {
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *loggingReputationServer) SendLocalTrust(_ context.Context, req *v2reputation.SendLocalTrustRequest) (*v2reputation.SendLocalTrustResponse, error) {
|
|
|
|
body := req.GetBody()
|
|
|
|
|
|
|
|
log := s.log.With(zap.Uint64("epoch", body.GetEpoch()))
|
|
|
|
|
|
|
|
for _, t := range body.GetTrusts() {
|
|
|
|
log.Info("local trust received",
|
2021-04-05 09:27:58 +00:00
|
|
|
zap.String("peer", hex.EncodeToString(t.GetPeer().GetValue())),
|
2021-03-24 09:57:50 +00:00
|
|
|
zap.Float64("value", t.GetValue()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (s *loggingReputationServer) SendIntermediateResult(_ context.Context, req *v2reputation.SendIntermediateResultRequest) (*v2reputation.SendIntermediateResultResponse, error) {
|
|
|
|
resp := new(v2reputation.SendIntermediateResultResponse)
|
|
|
|
|
|
|
|
// todo: implement me
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|