[#488] reputation/managers: Implement route builders

Add implementation of Builder interface for
intermediate trusts. Move all code associated
with managers to `common` directory in `cmd`
and `pkg/services/reputation`

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-22 17:56:25 +03:00 committed by Alex Vanin
parent 6ffc109a75
commit 49d477f466
9 changed files with 106 additions and 31 deletions

View file

@ -1,106 +0,0 @@
package local
import (
"bytes"
"github.com/nspcc-dev/hrw"
apiNetmap "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/managers"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
)
// managerBuilder is implementation of reputation ManagerBuilder interface.
// It sorts nodes in NetMap with HRW algorithms and
// takes the next node after the current one as the only manager.
type managerBuilder struct {
log *logger.Logger
nmSrc netmapcore.Source
}
// ManagersPrm groups the required parameters of the managerBuilder's constructor.
//
// All values must comply with the requirements imposed on them.
// Passing incorrect parameter values will result in constructor
// failure (error or panic depending on the implementation).
type ManagersPrm struct {
NetMapSource netmapcore.Source
}
// NewManagerBuilder creates a new instance of the managerBuilder.
//
// Panics if at least one value of the parameters is invalid.
//
// The created managerBuilder does not require additional
// initialization and is completely ready for work.
func NewManagerBuilder(prm ManagersPrm, opts ...MngOption) managers.ManagerBuilder {
switch {
case prm.NetMapSource == nil:
panicOnPrmValue("NetMapSource", prm.NetMapSource)
}
o := defaultMngOpts()
for i := range opts {
opts[i](o)
}
return &managerBuilder{
log: o.log,
nmSrc: prm.NetMapSource,
}
}
// 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) ([]reputationrouter.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 []reputationrouter.ServerInfo{nodes[managerIndex]}, nil
}
}
return nil, nil
}
type mngOptions struct {
log *logger.Logger
}
type MngOption func(*mngOptions)
func defaultMngOpts() *mngOptions {
return &mngOptions{
log: zap.L(),
}
}
// WithLogger returns MngOption to specify logging component.
func WithLogger(l *logger.Logger) MngOption {
return func(o *mngOptions) {
if l != nil {
o.log = l
}
}
}

View file

@ -5,6 +5,7 @@ import (
apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
reputationapi "github.com/nspcc-dev/neofs-api-go/pkg/reputation"
reputationutil "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/common"
"github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
@ -41,13 +42,13 @@ type RemoteProviderPrm struct {
func NewRemoteTrustProvider(prm RemoteProviderPrm) reputationrouter.RemoteWriterProvider {
switch {
case prm.LocalAddrSrc == nil:
panicOnPrmValue("LocalAddrSrc", prm.LocalAddrSrc)
reputationutil.PanicOnPrmValue("LocalAddrSrc", prm.LocalAddrSrc)
case prm.DeadEndProvider == nil:
panicOnPrmValue("DeadEndProvider", prm.DeadEndProvider)
reputationutil.PanicOnPrmValue("DeadEndProvider", prm.DeadEndProvider)
case prm.Key == nil:
panicOnPrmValue("Key", prm.Key)
reputationutil.PanicOnPrmValue("Key", prm.Key)
case prm.ClientCache == nil:
panicOnPrmValue("ClientCache", prm.ClientCache)
reputationutil.PanicOnPrmValue("ClientCache", prm.ClientCache)
}
return &remoteTrustProvider{
@ -67,7 +68,7 @@ func (rtp *remoteTrustProvider) InitRemote(srv reputationrouter.ServerInfo) (rep
if rtp.localAddrSrc.LocalAddress().String() == srv.Address() {
// if local => return no-op writer
return trustcontroller.SimpleWriterProvider(new(NopReputationWriter)), nil
return trustcontroller.SimpleWriterProvider(new(reputationutil.NopReputationWriter)), nil
}
ipAddr, err := network.IPAddrFromMultiaddr(addr)

View file

@ -1,46 +0,0 @@
package local
import (
"context"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
)
type EpochContext struct {
context.Context
E uint64
}
func (ctx *EpochContext) Epoch() uint64 {
return ctx.E
}
type NopReputationWriter struct{}
func (NopReputationWriter) Write(common.Context, reputation.Trust) error {
return nil
}
func (NopReputationWriter) Close() error {
return nil
}
type OnlyKeyRemoteServerInfo struct {
Key []byte
}
func (i *OnlyKeyRemoteServerInfo) PublicKey() []byte {
return i.Key
}
func (*OnlyKeyRemoteServerInfo) Address() string {
return ""
}
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
func panicOnPrmValue(n string, v interface{}) {
panic(fmt.Sprintf(invalidPrmValFmt, n, v, v))
}