From 49d477f466f1044cb15b1aaad1fc55bcbe0a9488 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 22 Apr 2021 17:56:25 +0300 Subject: [PATCH] [#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 --- .../reputation/{local => common}/managers.go | 8 +-- .../reputation/{local => common}/util.go | 4 +- cmd/neofs-node/reputation/local/remote.go | 11 ++-- pkg/services/reputation/common/deps.go | 9 ++++ .../managers => eigentrust/routes}/builder.go | 12 +++-- .../reputation/eigentrust/routes/calls.go | 23 ++++++++ .../reputation/local/managers/deps.go | 14 ----- .../reputation/local/routes/builder.go | 52 +++++++++++++++++++ .../local/{managers => routes}/calls.go | 4 +- 9 files changed, 106 insertions(+), 31 deletions(-) rename cmd/neofs-node/reputation/{local => common}/managers.go (91%) rename cmd/neofs-node/reputation/{local => common}/util.go (92%) rename pkg/services/reputation/{local/managers => eigentrust/routes}/builder.go (87%) create mode 100644 pkg/services/reputation/eigentrust/routes/calls.go delete mode 100644 pkg/services/reputation/local/managers/deps.go create mode 100644 pkg/services/reputation/local/routes/builder.go rename pkg/services/reputation/local/{managers => routes}/calls.go (87%) diff --git a/cmd/neofs-node/reputation/local/managers.go b/cmd/neofs-node/reputation/common/managers.go similarity index 91% rename from cmd/neofs-node/reputation/local/managers.go rename to cmd/neofs-node/reputation/common/managers.go index b38ccd338..ef06f249e 100644 --- a/cmd/neofs-node/reputation/local/managers.go +++ b/cmd/neofs-node/reputation/common/managers.go @@ -1,4 +1,4 @@ -package local +package common import ( "bytes" @@ -7,8 +7,8 @@ import ( 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" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" 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" ) @@ -36,10 +36,10 @@ type ManagersPrm struct { // // The created managerBuilder does not require additional // initialization and is completely ready for work. -func NewManagerBuilder(prm ManagersPrm, opts ...MngOption) managers.ManagerBuilder { +func NewManagerBuilder(prm ManagersPrm, opts ...MngOption) common.ManagerBuilder { switch { case prm.NetMapSource == nil: - panicOnPrmValue("NetMapSource", prm.NetMapSource) + PanicOnPrmValue("NetMapSource", prm.NetMapSource) } o := defaultMngOpts() diff --git a/cmd/neofs-node/reputation/local/util.go b/cmd/neofs-node/reputation/common/util.go similarity index 92% rename from cmd/neofs-node/reputation/local/util.go rename to cmd/neofs-node/reputation/common/util.go index 881a56b94..26b1b8ce0 100644 --- a/cmd/neofs-node/reputation/local/util.go +++ b/cmd/neofs-node/reputation/common/util.go @@ -1,4 +1,4 @@ -package local +package common import ( "context" @@ -41,6 +41,6 @@ func (*OnlyKeyRemoteServerInfo) Address() string { const invalidPrmValFmt = "invalid parameter %s (%T):%v" -func panicOnPrmValue(n string, v interface{}) { +func PanicOnPrmValue(n string, v interface{}) { panic(fmt.Sprintf(invalidPrmValFmt, n, v, v)) } diff --git a/cmd/neofs-node/reputation/local/remote.go b/cmd/neofs-node/reputation/local/remote.go index b0281b9e1..e4d9c2482 100644 --- a/cmd/neofs-node/reputation/local/remote.go +++ b/cmd/neofs-node/reputation/local/remote.go @@ -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) diff --git a/pkg/services/reputation/common/deps.go b/pkg/services/reputation/common/deps.go index 0478995c9..e70589d0c 100644 --- a/pkg/services/reputation/common/deps.go +++ b/pkg/services/reputation/common/deps.go @@ -5,6 +5,7 @@ import ( "io" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router" ) // Context wraps stdlib context @@ -53,3 +54,11 @@ type WriterProvider interface { // contexts, so specific ones may document their own behavior. InitWriter(Context) (Writer, error) } + +// ManagerBuilder defines an interface for providing a list +// of Managers for specific epoch. Implementation depends on trust value. +type ManagerBuilder interface { + // BuildManagers must compose list of managers. It depends on + // particular epoch and PeerID of the current route point. + BuildManagers(epoch uint64, p reputation.PeerID) ([]router.ServerInfo, error) +} diff --git a/pkg/services/reputation/local/managers/builder.go b/pkg/services/reputation/eigentrust/routes/builder.go similarity index 87% rename from pkg/services/reputation/local/managers/builder.go rename to pkg/services/reputation/eigentrust/routes/builder.go index 4aebfc6ad..f5159dc50 100644 --- a/pkg/services/reputation/local/managers/builder.go +++ b/pkg/services/reputation/eigentrust/routes/builder.go @@ -1,6 +1,10 @@ -package managers +package routes -import "fmt" +import ( + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" +) // Prm groups the required parameters of the Builder's constructor. // @@ -11,7 +15,7 @@ type Prm struct { // Manager builder for current node. // // Must not be nil. - ManagerBuilder ManagerBuilder + ManagerBuilder common.ManagerBuilder } // Builder represents component that routes node to its managers. @@ -21,7 +25,7 @@ type Prm struct { // and optional components. After successful creation, // the Builder is immediately ready to work through API. type Builder struct { - managerBuilder ManagerBuilder + managerBuilder common.ManagerBuilder } const invalidPrmValFmt = "invalid parameter %s (%T):%v" diff --git a/pkg/services/reputation/eigentrust/routes/calls.go b/pkg/services/reputation/eigentrust/routes/calls.go new file mode 100644 index 000000000..c7f3f07e0 --- /dev/null +++ b/pkg/services/reputation/eigentrust/routes/calls.go @@ -0,0 +1,23 @@ +package routes + +import ( + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + reputationroute "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router" + "github.com/pkg/errors" +) + +// NextStage builds Manager list for trusted 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, t reputation.Trust, passed []reputationroute.ServerInfo) ([]reputationroute.ServerInfo, error) { + if len(passed) > 1 { + return nil, nil + } + + route, err := b.managerBuilder.BuildManagers(epoch, t.Peer()) + if err != nil { + return nil, errors.Wrapf(err, "could not build managers for epoch: %d", epoch) + } + + return route, nil +} diff --git a/pkg/services/reputation/local/managers/deps.go b/pkg/services/reputation/local/managers/deps.go deleted file mode 100644 index 48c9d08bc..000000000 --- a/pkg/services/reputation/local/managers/deps.go +++ /dev/null @@ -1,14 +0,0 @@ -package managers - -import ( - "github.com/nspcc-dev/neofs-node/pkg/services/reputation" - reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router" -) - -// ManagerBuilder defines an interface for providing a list -// of Managers for specific epoch. Implementation depends on trust value. -type ManagerBuilder interface { - // BuildManagers must compose list of managers. It depends on - // particular epoch and PeerID of the current route point. - BuildManagers(epoch uint64, p reputation.PeerID) ([]reputationrouter.ServerInfo, error) -} diff --git a/pkg/services/reputation/local/routes/builder.go b/pkg/services/reputation/local/routes/builder.go new file mode 100644 index 000000000..f5159dc50 --- /dev/null +++ b/pkg/services/reputation/local/routes/builder.go @@ -0,0 +1,52 @@ +package routes + +import ( + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" +) + +// Prm groups the required parameters of the Builder'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 Prm struct { + // Manager builder for current node. + // + // Must not be nil. + ManagerBuilder common.ManagerBuilder +} + +// Builder represents component that routes node to its managers. +// +// For correct operation, Builder must be created using +// the constructor (New) based on the required parameters +// and optional components. After successful creation, +// the Builder is immediately ready to work through API. +type Builder struct { + managerBuilder common.ManagerBuilder +} + +const invalidPrmValFmt = "invalid parameter %s (%T):%v" + +func panicOnPrmValue(n string, v interface{}) { + panic(fmt.Sprintf(invalidPrmValFmt, n, v, v)) +} + +// New creates a new instance of the Builder. +// +// Panics if at least one value of the parameters is invalid. +// +// The created Builder does not require additional +// initialization and is completely ready for work. +func New(prm Prm) *Builder { + switch { + case prm.ManagerBuilder == nil: + panicOnPrmValue("ManagerBuilder", prm.ManagerBuilder) + } + + return &Builder{ + managerBuilder: prm.ManagerBuilder, + } +} diff --git a/pkg/services/reputation/local/managers/calls.go b/pkg/services/reputation/local/routes/calls.go similarity index 87% rename from pkg/services/reputation/local/managers/calls.go rename to pkg/services/reputation/local/routes/calls.go index b18288a58..21dce7264 100644 --- a/pkg/services/reputation/local/managers/calls.go +++ b/pkg/services/reputation/local/routes/calls.go @@ -1,4 +1,4 @@ -package managers +package routes import ( "github.com/nspcc-dev/neofs-node/pkg/services/reputation" @@ -6,7 +6,7 @@ import ( "github.com/pkg/errors" ) -// NextStage builds Manager list for node and returns it directly. +// NextStage builds Manager list for trusting 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, t reputation.Trust, passed []reputationroute.ServerInfo) ([]reputationroute.ServerInfo, error) {