[#488] reputation/remoteProvider: Implement intermediate remote

Move common remoteProvider code to cmd/reputation/common.
Hide WriterProvider initialization behind interface and
add implementation of that interface to local and
intermediate packages in cmd/reputation directory.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-23 17:49:23 +03:00 committed by Alex Vanin
parent 49d477f466
commit e8885d72f4
4 changed files with 235 additions and 68 deletions

View file

@ -5,98 +5,59 @@ 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/cmd/neofs-node/reputation/common"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router"
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
"github.com/pkg/errors"
)
type clientCache interface {
Get(string) (apiClient.Client, error)
}
// remoteTrustProvider is implementation of reputation RemoteWriterProvider interface.
type remoteTrustProvider struct {
localAddrSrc network.LocalAddressSource
deadEndProvider reputationcommon.WriterProvider
key *ecdsa.PrivateKey
clientCache clientCache
}
// RemoteProviderPrm groups the required parameters of the remoteTrustProvider's constructor.
// RemoteProviderPrm groups the required parameters of the RemoteProvider'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 RemoteProviderPrm struct {
LocalAddrSrc network.LocalAddressSource
DeadEndProvider reputationcommon.WriterProvider
Key *ecdsa.PrivateKey
ClientCache clientCache
Key *ecdsa.PrivateKey
}
func NewRemoteTrustProvider(prm RemoteProviderPrm) reputationrouter.RemoteWriterProvider {
// NewRemoteProvider creates a new instance of the RemoteProvider.
//
// Panics if at least one value of the parameters is invalid.
//
// The created RemoteProvider does not require additional
// initialization and is completely ready for work.
func NewRemoteProvider(prm RemoteProviderPrm) *RemoteProvider {
switch {
case prm.LocalAddrSrc == nil:
reputationutil.PanicOnPrmValue("LocalAddrSrc", prm.LocalAddrSrc)
case prm.DeadEndProvider == nil:
reputationutil.PanicOnPrmValue("DeadEndProvider", prm.DeadEndProvider)
case prm.Key == nil:
reputationutil.PanicOnPrmValue("Key", prm.Key)
case prm.ClientCache == nil:
reputationutil.PanicOnPrmValue("ClientCache", prm.ClientCache)
common.PanicOnPrmValue("NetMapSource", prm.Key)
}
return &remoteTrustProvider{
localAddrSrc: prm.LocalAddrSrc,
deadEndProvider: prm.DeadEndProvider,
key: prm.Key,
clientCache: prm.ClientCache,
return &RemoteProvider{
key: prm.Key,
}
}
func (rtp *remoteTrustProvider) InitRemote(srv reputationrouter.ServerInfo) (reputationcommon.WriterProvider, error) {
if srv == nil {
return rtp.deadEndProvider, nil
}
// RemoteProvider is an implementation of the clientKeyRemoteProvider interface.
type RemoteProvider struct {
key *ecdsa.PrivateKey
}
addr := srv.Address()
if rtp.localAddrSrc.LocalAddress().String() == srv.Address() {
// if local => return no-op writer
return trustcontroller.SimpleWriterProvider(new(reputationutil.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 &RemoteTrustWriterProvider{
func (rp RemoteProvider) WithClient(c apiClient.Client) reputationcommon.WriterProvider {
return &TrustWriterProvider{
client: c,
key: rtp.key,
}, nil
key: rp.key,
}
}
type RemoteTrustWriterProvider struct {
type TrustWriterProvider struct {
client apiClient.Client
key *ecdsa.PrivateKey
}
func (rtwp *RemoteTrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
func (twp *TrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
return &RemoteTrustWriter{
ctx: ctx,
client: rtwp.client,
key: rtwp.key,
client: twp.client,
key: twp.key,
}, nil
}