[#625] client/reputation: remove intermediate wrapper

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-31 15:19:10 +03:00 committed by Alex Vanin
parent 97d18bc515
commit 767ee5c0cd
13 changed files with 145 additions and 375 deletions

View file

@ -10,7 +10,7 @@ import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/common"
intermediatereputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/intermediate"
localreputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/local"
rptwrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
grpcreputation "github.com/nspcc-dev/neofs-node/pkg/network/transport/reputation/grpc"
@ -32,7 +32,7 @@ import (
)
func initReputationService(c *cfg) {
wrap, err := rptwrapper.NewFromMorph(c.cfgMorph.client, c.cfgReputation.scriptHash, 0, rptwrapper.TryNotary())
wrap, err := repClient.NewFromMorph(c.cfgMorph.client, c.cfgReputation.scriptHash, 0, repClient.TryNotary())
fatalOnErr(err)
localKey := c.key.PublicKey().Bytes()

View file

@ -4,7 +4,7 @@ import (
"crypto/ecdsa"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust"
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
@ -20,7 +20,7 @@ import (
type FinalWriterProviderPrm struct {
PrivatKey *ecdsa.PrivateKey
PubKey []byte
Client *wrapper.ClientWrapper
Client *repClient.Client
}
// NewFinalWriterProvider creates a new instance of the FinalWriterProvider.
@ -64,13 +64,13 @@ func (fwp FinalWriterProvider) InitIntermediateWriter(
type FinalWriter struct {
privatKey *ecdsa.PrivateKey
pubKey []byte
client *wrapper.ClientWrapper
client *repClient.Client
l *logger.Logger
}
func (fw FinalWriter) WriteIntermediateTrust(t eigentrust.IterationTrust) error {
args := wrapper.PutArgs{}
args := repClient.PutPrm{}
var trustedPublicKey [33]byte
copy(trustedPublicKey[:], t.Peer().Bytes())

View file

@ -35,7 +35,7 @@ import (
neofsClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
repWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/subscriber"
@ -498,7 +498,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
return nil, err
}
repClient, err := repWrapper.NewFromMorph(server.morphClient, server.contracts.reputation, fee, repWrapper.TryNotary(), repWrapper.AsAlphabet())
repClient, err := repClient.NewFromMorph(server.morphClient, server.contracts.reputation, fee, repClient.TryNotary(), repClient.AsAlphabet())
if err != nil {
return nil, err
}

View file

@ -6,7 +6,7 @@ import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
reputationEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/reputation"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
apireputation "github.com/nspcc-dev/neofs-sdk-go/reputation"
@ -83,7 +83,7 @@ func (rp *Processor) approvePutReputation(e *reputationEvent.Put) {
// put event was received via Notary service
err = rp.reputationWrp.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
} else {
args := wrapper.PutArgs{}
args := repClient.PutPrm{}
args.SetEpoch(e.Epoch())
args.SetPeerID(id)
args.SetValue(e.Value())

View file

@ -5,7 +5,7 @@ import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
reputationWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
repClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
reputationEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/reputation"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
@ -32,7 +32,7 @@ type (
epochState EpochState
alphabetState AlphabetState
reputationWrp *reputationWrapper.ClientWrapper
reputationWrp *repClient.Client
mngBuilder common.ManagerBuilder
@ -45,7 +45,7 @@ type (
PoolSize int
EpochState EpochState
AlphabetState AlphabetState
ReputationWrapper *reputationWrapper.ClientWrapper
ReputationWrapper *repClient.Client
ManagerBuilder common.ManagerBuilder
NotaryDisabled bool
}

View file

@ -1,6 +1,10 @@
package reputation
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
@ -23,12 +27,57 @@ const (
listByEpochMethod = "listByEpoch"
)
// New creates, initializes and returns the Client instance.
func New(c *client.StaticClient) *Client {
return &Client{client: c}
// NewFromMorph returns the wrapper instance from the raw morph client.
func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...Option) (*Client, error) {
o := defaultOpts()
for i := range opts {
opts[i](o)
}
sc, err := client.NewStatic(cli, contract, fee, ([]client.StaticClientOption)(*o)...)
if err != nil {
return nil, fmt.Errorf("could not create static client of reputation contract: %w", err)
}
return &Client{client: sc}, nil
}
// Morph returns raw morph client.
func (c Client) Morph() *client.Client {
return c.client.Morph()
}
// ContractAddress returns the address of the associated contract.
func (c Client) ContractAddress() util.Uint160 {
return c.client.ContractAddress()
}
// Option allows to set an optional
// parameter of ClientWrapper.
type Option func(*opts)
type opts []client.StaticClientOption
func defaultOpts() *opts {
return new(opts)
}
// TryNotary returns option to enable
// notary invocation tries.
func TryNotary() Option {
return func(o *opts) {
*o = append(*o, client.TryNotary())
}
}
// AsAlphabet returns option to sign main TX
// of notary requests with client's private
// key.
//
// Considered to be used by IR nodes only.
func AsAlphabet() Option {
return func(o *opts) {
*o = append(*o, client.AsAlphabet())
}
}

View file

@ -5,68 +5,58 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
)
// GetArgs groups the arguments of "get reputation value" test invocation.
type GetArgs struct {
type (
// GetPrm groups the arguments of "get reputation value" test invocation.
GetPrm struct {
epoch uint64
peerID []byte // object of reputation evaluation
}
peerID reputation.PeerID
}
// GetByIDArgs groups the arguments of "get reputation value by reputation id"
// test invocation.
type GetByIDArgs struct {
id []byte // id of reputation value in reputation contract
}
// GetResult groups the stack parameters returned by
// "get" and "get by id" test invocations.
type GetResult struct {
reputations [][]byte
}
// GetByIDPrm groups the arguments of "get reputation value by
// reputation id" test invocation.
GetByIDPrm struct {
id ID
}
)
// SetEpoch sets epoch of expected reputation value.
func (g *GetArgs) SetEpoch(v uint64) {
func (g *GetPrm) SetEpoch(v uint64) {
g.epoch = v
}
// SetPeerID sets peer id of expected reputation value.
func (g *GetArgs) SetPeerID(v []byte) {
func (g *GetPrm) SetPeerID(v reputation.PeerID) {
g.peerID = v
}
// SetID sets id of expected reputation value in reputation contract.
func (g *GetByIDArgs) SetID(v []byte) {
func (g *GetByIDPrm) SetID(v ID) {
g.id = v
}
// Reputations returns slice of marshalled reputation values.
func (g GetResult) Reputations() [][]byte {
return g.reputations
}
// Get invokes the call of "get reputation value" method of reputation contract.
func (c *Client) Get(args GetArgs) (*GetResult, error) {
func (c *Client) Get(p GetPrm) ([]reputation.GlobalTrust, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(getMethod)
invokePrm.SetArgs(int64(args.epoch), args.peerID)
invokePrm.SetArgs(int64(p.epoch), p.peerID.ToV2().GetPublicKey())
prms, err := c.client.TestInvoke(invokePrm)
res, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getMethod, err)
}
return parseReputations(prms, getMethod)
return parseReputations(res, getMethod)
}
// GetByID invokes the call of "get reputation value by reputation id" method
// of reputation contract.
func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
func (c *Client) GetByID(p GetByIDPrm) ([]reputation.GlobalTrust, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(getByIDMethod)
invokePrm.SetArgs(args.id)
invokePrm.SetArgs([]byte(p.id))
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
@ -76,7 +66,24 @@ func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
return parseReputations(prms, getByIDMethod)
}
func parseReputations(items []stackitem.Item, method string) (*GetResult, error) {
func parseGetResult(rawReputations [][]byte, method string) ([]reputation.GlobalTrust, error) {
reputations := make([]reputation.GlobalTrust, 0, len(rawReputations))
for i := range rawReputations {
r := reputation.GlobalTrust{}
err := r.Unmarshal(rawReputations[i])
if err != nil {
return nil, fmt.Errorf("can't unmarshal global trust value (%s): %w", method, err)
}
reputations = append(reputations, r)
}
return reputations, nil
}
func parseReputations(items []stackitem.Item, method string) ([]reputation.GlobalTrust, error) {
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
@ -86,9 +93,7 @@ func parseReputations(items []stackitem.Item, method string) (*GetResult, error)
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
}
res := &GetResult{
reputations: make([][]byte, 0, len(items)),
}
res := make([][]byte, 0, len(items))
for i := range items {
rawReputation, err := client.BytesFromStackItem(items[i])
@ -96,8 +101,8 @@ func parseReputations(items []stackitem.Item, method string) (*GetResult, error)
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", method, err)
}
res.reputations = append(res.reputations, rawReputation)
res = append(res, rawReputation)
}
return res, nil
return parseGetResult(res, method)
}

View file

@ -6,35 +6,28 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
// ListByEpochArgs groups the arguments of
// "list reputation ids by epoch" test invoke call.
type ListByEpochArgs struct {
epoch uint64
}
type (
// ID is an ID of the reputation record in reputation contract.
ID []byte
// ListByEpochResult groups the stack parameters
// returned by "list reputation ids by epoch" test invoke.
type ListByEpochResult struct {
ids [][]byte
}
// ListByEpochArgs groups the arguments of
// "list reputation ids by epoch" test invoke call.
ListByEpochArgs struct {
epoch uint64
}
)
// SetEpoch sets epoch of expected reputation ids.
func (l *ListByEpochArgs) SetEpoch(v uint64) {
l.epoch = v
}
// IDs returns slice of reputation id values.
func (l ListByEpochResult) IDs() [][]byte {
return l.ids
}
// ListByEpoch invokes the call of "list reputation ids by epoch" method of
// reputation contract.
func (c *Client) ListByEpoch(args ListByEpochArgs) (*ListByEpochResult, error) {
func (c *Client) ListByEpoch(p ListByEpochArgs) ([]ID, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(listByEpochMethod)
invokePrm.SetArgs(int64(args.epoch))
invokePrm.SetArgs(int64(p.epoch))
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
@ -48,18 +41,15 @@ func (c *Client) ListByEpoch(args ListByEpochArgs) (*ListByEpochResult, error) {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listByEpochMethod, err)
}
res := &ListByEpochResult{
ids: make([][]byte, 0, len(items)),
}
result := make([]ID, 0, len(items))
for i := range items {
rawReputation, err := client.BytesFromStackItem(items[i])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", listByEpochMethod, err)
}
res.ids = append(res.ids, rawReputation)
result = append(result, rawReputation)
}
return res, nil
return result, nil
}

View file

@ -4,39 +4,47 @@ import (
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
)
// PutArgs groups the arguments of "put reputation value" invocation call.
type PutArgs struct {
type (
// PutPrm groups the arguments of "put reputation value" invocation call.
PutPrm struct {
epoch uint64
peerID []byte
value []byte
}
peerID reputation.PeerID
value reputation.GlobalTrust
}
)
// SetEpoch sets epoch of reputation value.
func (p *PutArgs) SetEpoch(v uint64) {
func (p *PutPrm) SetEpoch(v uint64) {
p.epoch = v
}
// SetPeerID sets peer id of reputation value.
func (p *PutArgs) SetPeerID(v []byte) {
func (p *PutPrm) SetPeerID(v reputation.PeerID) {
p.peerID = v
}
// SetValue sets marshaled reputation value.
func (p *PutArgs) SetValue(v []byte) {
// SetValue sets reputation value.
func (p *PutPrm) SetValue(v reputation.GlobalTrust) {
p.value = v
}
// Put invokes direct call of "put reputation value" method of reputation contract.
func (c *Client) Put(args PutArgs) error {
//
// If TryNotary is provided, calls notary contract.
func (c *Client) Put(p PutPrm) error {
data, err := p.value.Marshal()
if err != nil {
return fmt.Errorf("can't marshal global trust value: %w", err)
}
prm := client.InvokePrm{}
prm.SetMethod(putMethod)
prm.SetArgs(int64(args.epoch), args.peerID, args.value)
err := c.client.Invoke(prm)
prm.SetArgs(int64(p.epoch), p.peerID.ToV2().GetPublicKey(), data)
err = c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", putMethod, err)
}

View file

@ -1,96 +0,0 @@
package wrapper
import (
"fmt"
reputationClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
)
type (
// GetArgs groups the arguments of "get reputation value" test invocation.
GetArgs struct {
epoch uint64
peerID reputation.PeerID
}
// GetByIDArgs groups the arguments of "get reputation value by
// reputation id" test invocation.
GetByIDArgs struct {
id ReputationID
}
// GetResult groups the result of "get reputation value" and
// "get reputation value by reputation id" test invocations.
GetResult struct {
reputations []reputation.GlobalTrust
}
)
// SetEpoch sets epoch of expected reputation value.
func (g *GetArgs) SetEpoch(v uint64) {
g.epoch = v
}
// SetPeerID sets peer id of expected reputation value.
func (g *GetArgs) SetPeerID(v reputation.PeerID) {
g.peerID = v
}
// SetID sets id of expected reputation value in reputation contract.
func (g *GetByIDArgs) SetID(v ReputationID) {
g.id = v
}
// Reputations returns slice of reputation values.
func (g GetResult) Reputations() []reputation.GlobalTrust {
return g.reputations
}
// Get invokes the call of "get reputation value" method of reputation contract.
func (w *ClientWrapper) Get(v GetArgs) (*GetResult, error) {
args := reputationClient.GetArgs{}
args.SetEpoch(v.epoch)
args.SetPeerID(v.peerID.ToV2().GetPublicKey())
data, err := w.client.Get(args)
if err != nil {
return nil, err
}
return parseGetResult(data)
}
// GetByID invokes the call of "get reputation value by reputation id" method
// of reputation contract.
func (w *ClientWrapper) GetByID(v GetByIDArgs) (*GetResult, error) {
args := reputationClient.GetByIDArgs{}
args.SetID(v.id)
data, err := w.client.GetByID(args)
if err != nil {
return nil, err
}
return parseGetResult(data)
}
func parseGetResult(data *reputationClient.GetResult) (*GetResult, error) {
rawReputations := data.Reputations()
reputations := make([]reputation.GlobalTrust, 0, len(rawReputations))
for i := range rawReputations {
r := reputation.GlobalTrust{}
err := r.Unmarshal(rawReputations[i])
if err != nil {
return nil, fmt.Errorf("can't unmarshal global trust value: %w", err)
}
reputations = append(reputations, r)
}
return &GetResult{
reputations: reputations,
}, nil
}

View file

@ -1,55 +0,0 @@
package wrapper
import (
reputationClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
)
type (
// ReputationID is an ID of the reputation record in reputation contract.
ReputationID []byte
// ListByEpochArgs groups the arguments of
// "list reputation ids by epoch" test invoke call.
ListByEpochArgs struct {
epoch uint64
}
// ListByEpochResult groups the result of "list reputation ids by epoch"
// test invoke.
ListByEpochResult struct {
ids []ReputationID
}
)
// SetEpoch sets epoch of expected reputation ids.
func (l *ListByEpochArgs) SetEpoch(v uint64) {
l.epoch = v
}
// IDs returns slice of reputation id values.
func (l ListByEpochResult) IDs() []ReputationID {
return l.ids
}
// ListByEpoch invokes the call of "list reputation ids by epoch" method of
// reputation contract.
func (w *ClientWrapper) ListByEpoch(v ListByEpochArgs) (*ListByEpochResult, error) {
args := reputationClient.ListByEpochArgs{}
args.SetEpoch(v.epoch)
data, err := w.client.ListByEpoch(args)
if err != nil {
return nil, err
}
ids := data.IDs()
result := make([]ReputationID, 0, len(ids))
for i := range ids {
result = append(result, ids[i])
}
return &ListByEpochResult{
ids: result,
}, nil
}

View file

@ -1,59 +0,0 @@
package wrapper
import (
"fmt"
reputationClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
)
type (
// PutArgs groups the arguments of "put reputation value" invocation call.
PutArgs struct {
epoch uint64
peerID reputation.PeerID
value reputation.GlobalTrust
}
)
// SetEpoch sets epoch of reputation value.
func (p *PutArgs) SetEpoch(v uint64) {
p.epoch = v
}
// SetPeerID sets peer id of reputation value.
func (p *PutArgs) SetPeerID(v reputation.PeerID) {
p.peerID = v
}
// SetValue sets reputation value.
func (p *PutArgs) SetValue(v reputation.GlobalTrust) {
p.value = v
}
// Put invokes direct call of "put reputation value" method of reputation contract.
//
// If TryNotary is provided, calls notary contract.
func (w *ClientWrapper) Put(v PutArgs) error {
args, err := preparePutArgs(v)
if err != nil {
return err
}
return w.client.Put(args)
}
func preparePutArgs(v PutArgs) (reputationClient.PutArgs, error) {
args := reputationClient.PutArgs{}
data, err := v.value.Marshal()
if err != nil {
return args, fmt.Errorf("can't marshal global trust value: %w", err)
}
args.SetEpoch(v.epoch)
args.SetPeerID(v.peerID.ToV2().GetPublicKey())
args.SetValue(data)
return args, nil
}

View file

@ -1,72 +0,0 @@
package wrapper
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/internal"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation"
)
// ClientWrapper is a wrapper over reputation contract
// client which implements storage of reputation values.
type ClientWrapper struct {
internal.StaticClient
client *reputation.Client
}
// Option allows to set an optional
// parameter of ClientWrapper.
type Option func(*opts)
type opts []client.StaticClientOption
func defaultOpts() *opts {
return new(opts)
}
// Morph returns raw morph client.
func (w ClientWrapper) Morph() *client.Client {
return w.client.Morph()
}
// TryNotary returns option to enable
// notary invocation tries.
func TryNotary() Option {
return func(o *opts) {
*o = append(*o, client.TryNotary())
}
}
// AsAlphabet returns option to sign main TX
// of notary requests with client's private
// key.
//
// Considered to be used by IR nodes only.
func AsAlphabet() Option {
return func(o *opts) {
*o = append(*o, client.AsAlphabet())
}
}
// NewFromMorph returns the wrapper instance from the raw morph client.
func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...Option) (*ClientWrapper, error) {
o := defaultOpts()
for i := range opts {
opts[i](o)
}
staticClient, err := client.NewStatic(cli, contract, fee, ([]client.StaticClientOption)(*o)...)
if err != nil {
return nil, fmt.Errorf("could not create static client of reputation contract: %w", err)
}
return &ClientWrapper{
StaticClient: staticClient,
client: reputation.New(staticClient),
}, nil
}