forked from TrueCloudLab/frostfs-node
[#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:
parent
49d477f466
commit
e8885d72f4
4 changed files with 235 additions and 68 deletions
90
cmd/neofs-node/reputation/common/remote.go
Normal file
90
cmd/neofs-node/reputation/common/remote.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/network"
|
||||
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)
|
||||
}
|
||||
|
||||
// clientKeyRemoteProvider must provide remote writer and take into account
|
||||
// that requests must be sent via passed api client and must be signed with
|
||||
// passed private key.
|
||||
type clientKeyRemoteProvider interface {
|
||||
WithClient(apiClient.Client) reputationcommon.WriterProvider
|
||||
}
|
||||
|
||||
// remoteTrustProvider is implementation of reputation RemoteWriterProvider interface.
|
||||
// It caches clients, checks if it is the end of the route and checks either current
|
||||
// node is remote target or not.
|
||||
//
|
||||
// remoteTrustProvider requires to be provided with clientKeyRemoteProvider.
|
||||
type remoteTrustProvider struct {
|
||||
localAddrSrc network.LocalAddressSource
|
||||
deadEndProvider reputationcommon.WriterProvider
|
||||
clientCache clientCache
|
||||
remoteProvider clientKeyRemoteProvider
|
||||
}
|
||||
|
||||
// RemoteProviderPrm groups the required parameters of the remoteTrustProvider'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
|
||||
ClientCache clientCache
|
||||
WriterProvider clientKeyRemoteProvider
|
||||
}
|
||||
|
||||
func NewRemoteTrustProvider(prm RemoteProviderPrm) reputationrouter.RemoteWriterProvider {
|
||||
switch {
|
||||
case prm.LocalAddrSrc == nil:
|
||||
PanicOnPrmValue("LocalAddrSrc", prm.LocalAddrSrc)
|
||||
case prm.DeadEndProvider == nil:
|
||||
PanicOnPrmValue("DeadEndProvider", prm.DeadEndProvider)
|
||||
case prm.ClientCache == nil:
|
||||
PanicOnPrmValue("ClientCache", prm.ClientCache)
|
||||
case prm.WriterProvider == nil:
|
||||
PanicOnPrmValue("WriterProvider", prm.WriterProvider)
|
||||
}
|
||||
|
||||
return &remoteTrustProvider{
|
||||
localAddrSrc: prm.LocalAddrSrc,
|
||||
deadEndProvider: prm.DeadEndProvider,
|
||||
clientCache: prm.ClientCache,
|
||||
remoteProvider: prm.WriterProvider,
|
||||
}
|
||||
}
|
||||
|
||||
func (rtp *remoteTrustProvider) InitRemote(srv reputationcommon.ServerInfo) (reputationcommon.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 rtp.remoteProvider.WithClient(c), nil
|
||||
}
|
120
cmd/neofs-node/reputation/intermediate/remote.go
Normal file
120
cmd/neofs-node/reputation/intermediate/remote.go
Normal file
|
@ -0,0 +1,120 @@
|
|||
package intermediate
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||
reputationapi "github.com/nspcc-dev/neofs-api-go/pkg/reputation"
|
||||
"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"
|
||||
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
Key *ecdsa.PrivateKey
|
||||
}
|
||||
|
||||
// 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.Key == nil:
|
||||
common.PanicOnPrmValue("NetMapSource", prm.Key)
|
||||
}
|
||||
|
||||
return &RemoteProvider{
|
||||
key: prm.Key,
|
||||
}
|
||||
}
|
||||
|
||||
// RemoteProvider is an implementation of the clientKeyRemoteProvider interface.
|
||||
type RemoteProvider struct {
|
||||
key *ecdsa.PrivateKey
|
||||
}
|
||||
|
||||
func (rp RemoteProvider) WithClient(c apiClient.Client) reputationcommon.WriterProvider {
|
||||
return &TrustWriterProvider{
|
||||
client: c,
|
||||
key: rp.key,
|
||||
}
|
||||
}
|
||||
|
||||
type TrustWriterProvider struct {
|
||||
client apiClient.Client
|
||||
key *ecdsa.PrivateKey
|
||||
}
|
||||
|
||||
func (twp *TrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
|
||||
return &RemoteTrustWriter{
|
||||
ctx: ctx,
|
||||
client: twp.client,
|
||||
key: twp.key,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type RemoteTrustWriter struct {
|
||||
ctx reputationcommon.Context
|
||||
client apiClient.Client
|
||||
key *ecdsa.PrivateKey
|
||||
|
||||
buf []*apiClient.SendIntermediateTrustPrm
|
||||
}
|
||||
|
||||
// Write check if passed context contains required
|
||||
// data(returns ErrIncorrectContext if not) and
|
||||
// caches passed trusts(as SendIntermediateTrustPrm structs).
|
||||
func (rtp *RemoteTrustWriter) Write(ctx reputationcommon.Context, t reputation.Trust) error {
|
||||
eiContext, ok := ctx.(eigentrustcalc.Context)
|
||||
if !ok {
|
||||
return ErrIncorrectContext
|
||||
}
|
||||
|
||||
apiTrustingPeer := reputationapi.NewPeerID()
|
||||
apiTrustingPeer.SetPublicKey(t.TrustingPeer())
|
||||
|
||||
apiTrustedPeer := reputationapi.NewPeerID()
|
||||
apiTrustedPeer.SetPublicKey(t.Peer())
|
||||
|
||||
apiTrust := reputationapi.NewTrust()
|
||||
apiTrust.SetValue(t.Value().Float64())
|
||||
apiTrust.SetPeer(apiTrustedPeer)
|
||||
|
||||
apiPeerToPeerTrust := reputationapi.NewPeerToPeerTrust()
|
||||
apiPeerToPeerTrust.SetTrustingPeer(apiTrustingPeer)
|
||||
apiPeerToPeerTrust.SetTrust(apiTrust)
|
||||
|
||||
p := &apiClient.SendIntermediateTrustPrm{}
|
||||
p.SetEpoch(eiContext.Epoch())
|
||||
p.SetIteration(eiContext.I())
|
||||
p.SetTrust(apiPeerToPeerTrust)
|
||||
|
||||
rtp.buf = append(rtp.buf, p)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close sends all cached intermediate trusts.
|
||||
// If error occurs, returns in immediately and stops iteration.
|
||||
func (rtp *RemoteTrustWriter) Close() (err error) {
|
||||
for _, prm := range rtp.buf {
|
||||
_, err = rtp.client.SendIntermediateTrust(
|
||||
rtp.ctx,
|
||||
*prm,
|
||||
apiClient.WithKey(rtp.key),
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
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,
|
||||
return &RemoteProvider{
|
||||
key: prm.Key,
|
||||
clientCache: prm.ClientCache,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
4
go.sum
4
go.sum
|
@ -276,10 +276,6 @@ github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5
|
|||
github.com/nspcc-dev/neo-go v0.94.1 h1:AHUnADrzJ3JW2wO8xSp9dH8/Lo5UwTojkpuNqHuulSw=
|
||||
github.com/nspcc-dev/neo-go v0.94.1/go.mod h1:IrBT/UG3/Slhqgjge8r6zni5tNRsWmwwG9OnVdASheI=
|
||||
github.com/nspcc-dev/neofs-api-go v1.24.0/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.0 h1:hw1TTi3/3wCBB3KN6cycuM8Sn/MFnapSQptBx8KgDIY=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.0/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210409085742-8128d598d022 h1:K5Nl+Gkd1hDgt1skPl9Ptfzsn5KxwrnuAuToF0qCPfg=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210409085742-8128d598d022/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210422103707-0f7a14a69f49 h1:uZU/za2VMiqHT4ymqidVy7ZNNqpYhUGpoF0OrPkM5WI=
|
||||
github.com/nspcc-dev/neofs-api-go v1.25.1-0.20210422103707-0f7a14a69f49/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
|
||||
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
|
||||
|
|
Loading…
Reference in a new issue