diff --git a/cmd/neofs-node/reputation/intermediate/remote.go b/cmd/neofs-node/reputation/intermediate/remote.go index ca8283d01..7bb8108dc 100644 --- a/cmd/neofs-node/reputation/intermediate/remote.go +++ b/cmd/neofs-node/reputation/intermediate/remote.go @@ -6,6 +6,7 @@ import ( 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" + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/internal/client" "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" @@ -72,13 +73,9 @@ type RemoteTrustWriter struct { eiCtx eigentrustcalc.Context client apiClient.Client key *ecdsa.PrivateKey - - buf []*apiClient.AnnounceIntermediateTrustPrm } -// Write check if passed context contains required -// data(returns ErrIncorrectContext if not) and -// caches passed trusts(as SendIntermediateTrustPrm structs). +// Write sends trust value to remote node via ReputationService.AnnounceIntermediateResult RPC. func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error { apiTrustingPeer := reputationapi.NewPeerID() apiTrustingPeer.SetPublicKey(t.TrustingPeer()) @@ -94,29 +91,20 @@ func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error { apiPeerToPeerTrust.SetTrustingPeer(apiTrustingPeer) apiPeerToPeerTrust.SetTrust(apiTrust) - p := &apiClient.AnnounceIntermediateTrustPrm{} + var p internalclient.AnnounceIntermediatePrm + + p.SetContext(rtp.eiCtx) + p.SetClient(rtp.client) + p.SetPrivateKey(rtp.key) p.SetEpoch(rtp.eiCtx.Epoch()) p.SetIteration(rtp.eiCtx.I()) p.SetTrust(apiPeerToPeerTrust) - rtp.buf = append(rtp.buf, p) + _, err := internalclient.AnnounceIntermediate(p) + return err +} + +func (rtp *RemoteTrustWriter) Close() error { 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.AnnounceIntermediateTrust( - rtp.eiCtx, - *prm, - apiClient.WithKey(rtp.key), - ) - if err != nil { - return - } - } - - return -} diff --git a/cmd/neofs-node/reputation/internal/client/client.go b/cmd/neofs-node/reputation/internal/client/client.go new file mode 100644 index 000000000..eecdcaaf3 --- /dev/null +++ b/cmd/neofs-node/reputation/internal/client/client.go @@ -0,0 +1,104 @@ +package internal + +import ( + "context" + "crypto/ecdsa" + + "github.com/nspcc-dev/neofs-api-go/pkg/client" + "github.com/nspcc-dev/neofs-api-go/pkg/reputation" +) + +type commonPrm struct { + cli client.Client + + ctx context.Context + + epoch uint64 + + opts []client.CallOption +} + +// SetClient sets base client for NeoFS API communication. +// +// Required parameter. +func (x *commonPrm) SetClient(cli client.Client) { + x.cli = cli +} + +// SetContext sets context.Context for network communication. +// +// Required parameter. +func (x *commonPrm) SetContext(ctx context.Context) { + x.ctx = ctx +} + +// SetPrivateKey sets private key to sign the request(s). +// +// Required parameter. +func (x *commonPrm) SetPrivateKey(key *ecdsa.PrivateKey) { + x.opts = append(x.opts, client.WithKey(key)) +} + +// AnnounceLocalPrm groups parameters of AnnounceLocal operation. +type AnnounceLocalPrm struct { + commonPrm + + cliPrm client.AnnounceLocalTrustPrm +} + +// SetEpoch sets epoch in which the trust was assessed. +func (x *AnnounceLocalPrm) SetEpoch(epoch uint64) { + x.cliPrm.SetEpoch(epoch) +} + +// SetTrusts sets list of local trust values. +func (x *AnnounceLocalPrm) SetTrusts(ts []*reputation.Trust) { + x.cliPrm.SetTrusts(ts) +} + +// AnnounceLocalRes groups resulting values of AnnounceLocal operation. +type AnnounceLocalRes struct{} + +// AnnounceLocal sends estimations of local trust to the remote node. +// +// Client, context and key must be set. +func AnnounceLocal(prm AnnounceLocalPrm) (res AnnounceLocalRes, err error) { + _, err = prm.cli.AnnounceLocalTrust(prm.ctx, prm.cliPrm, prm.opts...) + + return +} + +// AnnounceIntermediatePrm groups parameters of AnnounceIntermediate operation. +type AnnounceIntermediatePrm struct { + commonPrm + + cliPrm client.AnnounceIntermediateTrustPrm +} + +// SetEpoch sets number of the epoch when the trust calculation's iteration was executed. +func (x *AnnounceIntermediatePrm) SetEpoch(epoch uint64) { + x.cliPrm.SetEpoch(epoch) +} + +// SetIteration sets number of the iteration of the trust calculation algorithm. +func (x *AnnounceIntermediatePrm) SetIteration(iter uint32) { + x.cliPrm.SetIteration(iter) +} + +// SetTrust sets current global trust value computed at the iteration. +func (x *AnnounceIntermediatePrm) SetTrust(t *reputation.PeerToPeerTrust) { + x.cliPrm.SetTrust(t) +} + +// AnnounceIntermediateRes groups resulting values of AnnounceIntermediate operation. +type AnnounceIntermediateRes struct{} + +// AnnounceIntermediate sends global trust value calculated at the specified iteration +// and epoch to to the remote node. +// +// Client, context and key must be set. +func AnnounceIntermediate(prm AnnounceIntermediatePrm) (res AnnounceIntermediateRes, err error) { + _, err = prm.cli.AnnounceIntermediateTrust(prm.ctx, prm.cliPrm, prm.opts...) + + return +} diff --git a/cmd/neofs-node/reputation/internal/client/doc.go b/cmd/neofs-node/reputation/internal/client/doc.go new file mode 100644 index 000000000..2c6365d7b --- /dev/null +++ b/cmd/neofs-node/reputation/internal/client/doc.go @@ -0,0 +1,11 @@ +// Package internal provides functionality for NeoFS Node Reputation system communication with NeoFS network. +// The base client for accessing remote nodes via NeoFS API is a NeoFS SDK Go API client. +// However, although it encapsulates a useful piece of business logic (e.g. the signature mechanism), +// the Reputation service does not fully use the client's flexible interface. +// +// In this regard, this package provides functions over base API client necessary for the application. +// This allows you to concentrate the entire spectrum of the client's use in one place (this will be convenient +// both when updating the base client and for evaluating the UX of SDK library). So it is expected that all +// Reputation service packages will be limited to this package for the development of functionality requiring +// NeoFS API communication. +package internal diff --git a/cmd/neofs-node/reputation/local/remote.go b/cmd/neofs-node/reputation/local/remote.go index 6d5df183a..e7397e0fe 100644 --- a/cmd/neofs-node/reputation/local/remote.go +++ b/cmd/neofs-node/reputation/local/remote.go @@ -6,6 +6,7 @@ import ( 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" + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/internal/client" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" ) @@ -84,16 +85,15 @@ func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error { } func (rtp *RemoteTrustWriter) Close() error { - prm := apiClient.AnnounceLocalTrustPrm{} + var prm internalclient.AnnounceLocalPrm + prm.SetContext(rtp.ctx) + prm.SetClient(rtp.client) + prm.SetPrivateKey(rtp.key) prm.SetEpoch(rtp.ctx.Epoch()) prm.SetTrusts(rtp.buf) - _, err := rtp.client.AnnounceLocalTrust( - rtp.ctx, - prm, - apiClient.WithKey(rtp.key), - ) + _, err := internalclient.AnnounceLocal(prm) return err }