From 8c5c3ac9e8808308b4acc901fb2301bc610e2d24 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 31 Jan 2022 14:04:59 +0300 Subject: [PATCH] [#625] client/neofsid: remove intermediate wrapper Signed-off-by: Evgenii Stratonikov --- pkg/innerring/innerring.go | 4 +- pkg/innerring/processors/container/common.go | 2 +- .../processors/container/process_container.go | 2 +- .../processors/container/processor.go | 6 +- .../processors/neofs/process_bind.go | 27 ++--- pkg/innerring/processors/neofs/processor.go | 6 +- pkg/morph/client/neofsid/addrm_keys.go | 34 ++---- pkg/morph/client/neofsid/client.go | 50 ++++++++- pkg/morph/client/neofsid/keys.go | 57 ++++------ pkg/morph/client/neofsid/wrapper/client.go | 70 ------------ pkg/morph/client/neofsid/wrapper/keys.go | 105 ------------------ 11 files changed, 101 insertions(+), 262 deletions(-) delete mode 100644 pkg/morph/client/neofsid/wrapper/client.go delete mode 100644 pkg/morph/client/neofsid/wrapper/keys.go diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 739ce6129..3b2e689ce 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -33,7 +33,7 @@ import ( balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" cntWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" neofsClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" - neofsidWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" repWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper" morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet" @@ -503,7 +503,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } - neofsIDClient, err := neofsidWrapper.NewFromMorph(server.morphClient, server.contracts.neofsID, fee, neofsidWrapper.TryNotary(), neofsidWrapper.AsAlphabet()) + neofsIDClient, err := neofsid.NewFromMorph(server.morphClient, server.contracts.neofsID, fee, neofsid.TryNotary(), neofsid.AsAlphabet()) if err != nil { return nil, err } diff --git a/pkg/innerring/processors/container/common.go b/pkg/innerring/processors/container/common.go index e709e9ba8..fe656e001 100644 --- a/pkg/innerring/processors/container/common.go +++ b/pkg/innerring/processors/container/common.go @@ -8,7 +8,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/session" diff --git a/pkg/innerring/processors/container/process_container.go b/pkg/innerring/processors/container/process_container.go index 42b4a9335..91ae4facd 100644 --- a/pkg/innerring/processors/container/process_container.go +++ b/pkg/innerring/processors/container/process_container.go @@ -11,7 +11,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-node/pkg/core/container" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" - neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet" "github.com/nspcc-dev/neofs-node/pkg/morph/event" containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" diff --git a/pkg/innerring/processors/container/processor.go b/pkg/innerring/processors/container/processor.go index 3edaf6bde..c5f4afa69 100644 --- a/pkg/innerring/processors/container/processor.go +++ b/pkg/innerring/processors/container/processor.go @@ -6,7 +6,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" - neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet" "github.com/nspcc-dev/neofs-node/pkg/morph/event" containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" @@ -26,7 +26,7 @@ type ( pool *ants.Pool alphabetState AlphabetState cnrClient *wrapper.Wrapper // notary must be enabled - idClient *neofsid.ClientWrapper + idClient *neofsid.Client subnetClient *morphsubnet.Client netState NetworkState notaryDisabled bool @@ -38,7 +38,7 @@ type ( PoolSize int AlphabetState AlphabetState ContainerClient *wrapper.Wrapper - NeoFSIDClient *neofsid.ClientWrapper + NeoFSIDClient *neofsid.Client SubnetClient *morphsubnet.Client NetworkState NetworkState NotaryDisabled bool diff --git a/pkg/innerring/processors/neofs/process_bind.go b/pkg/innerring/processors/neofs/process_bind.go index 154007a2a..6ac771a24 100644 --- a/pkg/innerring/processors/neofs/process_bind.go +++ b/pkg/innerring/processors/neofs/process_bind.go @@ -8,7 +8,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/util" - neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" "github.com/nspcc-dev/neofs-node/pkg/morph/event/neofs" "go.uber.org/zap" ) @@ -95,25 +95,22 @@ func (np *Processor) approveBindCommon(e *bindCommonContext) { return } - prm := neofsid.ManageKeysPrm{} - + prm := neofsid.CommonBindPrm{} prm.SetOwnerID(wallet) prm.SetKeys(e.Keys()) - prm.SetAdd(e.bind) prm.SetHash(e.bindCommon.TxHash()) - err = np.neofsIDClient.ManageKeys(prm) + var typ string + if e.bind { + typ = "bind" + err = np.neofsIDClient.AddKeys(prm) + } else { + typ = "unbind" + err = np.neofsIDClient.RemoveKeys(prm) + } + if err != nil { - var typ string - - if e.bind { - typ = "bind" - } else { - typ = "unbind" - } - np.log.Error(fmt.Sprintf("could not approve %s", typ), - zap.String("error", err.Error()), - ) + zap.String("error", err.Error())) } } diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index 1491256b6..c77447537 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -10,7 +10,7 @@ import ( "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/balance" - neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" neofsEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/neofs" @@ -51,7 +51,7 @@ type ( mintEmitValue fixedn.Fixed8 gasBalanceThreshold int64 - neofsIDClient *neofsid.ClientWrapper + neofsIDClient *neofsid.Client } // Params of the processor constructor. @@ -59,7 +59,7 @@ type ( Log *zap.Logger PoolSize int NeoFSContract util.Uint160 - NeoFSIDClient *neofsid.ClientWrapper + NeoFSIDClient *neofsid.Client BalanceClient *balance.Client NetmapClient *nmWrapper.Wrapper MorphClient *client.Client diff --git a/pkg/morph/client/neofsid/addrm_keys.go b/pkg/morph/client/neofsid/addrm_keys.go index 4e99d5a4e..cd0deb239 100644 --- a/pkg/morph/client/neofsid/addrm_keys.go +++ b/pkg/morph/client/neofsid/addrm_keys.go @@ -6,19 +6,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/morph/client" ) -// AddKeysArgs groups the arguments -// of key binding call. -type AddKeysArgs struct { - commonBindArgs -} - -// RemoveKeysArgs groups the arguments -// of key unbinding call. -type RemoveKeysArgs struct { - commonBindArgs -} - -type commonBindArgs struct { +type CommonBindPrm struct { ownerID []byte // NeoFS account identifier keys [][]byte // list of serialized public keys @@ -26,28 +14,27 @@ type commonBindArgs struct { client.InvokePrmOptional } -func (x *commonBindArgs) SetOptionalPrm(prm client.InvokePrmOptional) { +func (x *CommonBindPrm) SetOptionalPrm(prm client.InvokePrmOptional) { x.InvokePrmOptional = prm } // SetOwnerID sets NeoFS account identifier. -func (x *commonBindArgs) SetOwnerID(v []byte) { +func (x *CommonBindPrm) SetOwnerID(v []byte) { x.ownerID = v } // SetKeys sets list of public keys in a binary format. -func (x *commonBindArgs) SetKeys(v [][]byte) { +func (x *CommonBindPrm) SetKeys(v [][]byte) { x.keys = v } -// AddKeys invokes the call of key adding method -// of NeoFS contract. -func (x *Client) AddKeys(args AddKeysArgs) error { +// AddKeys adds a list of public keys to/from NeoFS account. +func (x *Client) AddKeys(p CommonBindPrm) error { prm := client.InvokePrm{} prm.SetMethod(addKeysMethod) - prm.SetArgs(args.ownerID, args.keys) - prm.InvokePrmOptional = args.InvokePrmOptional + prm.SetArgs(p.ownerID, p.keys) + prm.InvokePrmOptional = p.InvokePrmOptional err := x.client.Invoke(prm) if err != nil { @@ -57,9 +44,8 @@ func (x *Client) AddKeys(args AddKeysArgs) error { return nil } -// RemoveKeys invokes the call of key removing method -// of NeoFS contract. -func (x *Client) RemoveKeys(args RemoveKeysArgs) error { +// RemoveKeys removes a list of public keys to/from NeoFS account. +func (x *Client) RemoveKeys(args CommonBindPrm) error { prm := client.InvokePrm{} prm.SetMethod(removeKeysMethod) diff --git a/pkg/morph/client/neofsid/client.go b/pkg/morph/client/neofsid/client.go index 82d42d94b..3799abe48 100644 --- a/pkg/morph/client/neofsid/client.go +++ b/pkg/morph/client/neofsid/client.go @@ -1,6 +1,10 @@ package neofsid 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" ) @@ -22,7 +26,47 @@ const ( removeKeysMethod = "removeKey" ) -// New creates, initializes and returns the Client instance. -func New(c *client.StaticClient) *Client { - return &Client{client: c} +// NewFromMorph wraps client to work with NeoFS ID contract. +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 client of NeoFS ID contract: %w", err) + } + + return &Client{client: sc}, nil +} + +// 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()) + } } diff --git a/pkg/morph/client/neofsid/keys.go b/pkg/morph/client/neofsid/keys.go index 415c3fef0..17926acbb 100644 --- a/pkg/morph/client/neofsid/keys.go +++ b/pkg/morph/client/neofsid/keys.go @@ -1,44 +1,31 @@ package neofsid import ( + "crypto/elliptic" "fmt" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-sdk-go/owner" ) -// KeyListingArgs groups the arguments -// of key listing call. -type KeyListingArgs struct { - ownerID []byte // account identifier +// AccountKeysPrm groups parameters of AccountKeys operation. +type AccountKeysPrm struct { + id *owner.ID } -// KeyListingValues groups the stack parameters -// returned by key listing call. -type KeyListingValues struct { - keys [][]byte // list of user public keys in binary format +// SetID sets owner ID. +func (a *AccountKeysPrm) SetID(id *owner.ID) { + a.id = id } -// SetOwnerID sets the NeoFS account identifier -// in a binary format. -func (l *KeyListingArgs) SetOwnerID(v []byte) { - l.ownerID = v -} +// AccountKeys requests public keys of NeoFS account from NeoFS ID contract. +func (x *Client) AccountKeys(p AccountKeysPrm) (keys.PublicKeys, error) { + prm := client.TestInvokePrm{} + prm.SetMethod(keyListingMethod) + prm.SetArgs(p.id.ToV2().GetValue()) -// Keys returns the list of account keys -// in a binary format. -func (l *KeyListingValues) Keys() [][]byte { - return l.keys -} - -// AccountKeys requests public keys of NeoFS account -// through method of NeoFS ID contract. -func (x *Client) AccountKeys(args KeyListingArgs) (*KeyListingValues, error) { - invokePrm := client.TestInvokePrm{} - - invokePrm.SetMethod(keyListingMethod) - invokePrm.SetArgs(args.ownerID) - - items, err := x.client.TestInvoke(invokePrm) + items, err := x.client.TestInvoke(prm) if err != nil { return nil, fmt.Errorf("could not perform test invocation (%s): %w", keyListingMethod, err) } else if ln := len(items); ln != 1 { @@ -50,18 +37,18 @@ func (x *Client) AccountKeys(args KeyListingArgs) (*KeyListingValues, error) { return nil, fmt.Errorf("1st stack item must be an array (%s)", keyListingMethod) } - keys := make([][]byte, 0, len(items)) - + pubs := make(keys.PublicKeys, len(items)) for i := range items { - key, err := client.BytesFromStackItem(items[i]) + rawPub, err := client.BytesFromStackItem(items[i]) if err != nil { return nil, fmt.Errorf("invalid stack item, expected byte array (%s)", keyListingMethod) } - keys = append(keys, key) + pubs[i], err = keys.NewPublicKeyFromBytes(rawPub, elliptic.P256()) + if err != nil { + return nil, fmt.Errorf("received invalid key (%s): %w", keyListingMethod, err) + } } - return &KeyListingValues{ - keys: keys, - }, nil + return pubs, nil } diff --git a/pkg/morph/client/neofsid/wrapper/client.go b/pkg/morph/client/neofsid/wrapper/client.go deleted file mode 100644 index 35aa33a95..000000000 --- a/pkg/morph/client/neofsid/wrapper/client.go +++ /dev/null @@ -1,70 +0,0 @@ -package neofsid - -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/neofsid" -) - -// ClientWrapper is a wrapper over NeoFS ID contract -// client which provides convenient methods for -// working with a contract. -// -// Working ClientWrapper must be created via Wrap. -type ClientWrapper struct { - internal.StaticClient - - client *neofsid.Client -} - -// 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()) - } -} - -// NewFromMorph wraps client to work with NeoFS ID contract. -func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...Option) (*ClientWrapper, 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 client of NeoFS ID contract: %w", err) - } - - return &ClientWrapper{ - StaticClient: sc, - client: neofsid.New(sc), - }, nil -} diff --git a/pkg/morph/client/neofsid/wrapper/keys.go b/pkg/morph/client/neofsid/wrapper/keys.go deleted file mode 100644 index 42d7b10fb..000000000 --- a/pkg/morph/client/neofsid/wrapper/keys.go +++ /dev/null @@ -1,105 +0,0 @@ -package neofsid - -import ( - "crypto/elliptic" - "fmt" - - "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - "github.com/nspcc-dev/neofs-node/pkg/morph/client" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" - "github.com/nspcc-dev/neofs-sdk-go/owner" -) - -// AccountKeysPrm groups parameters of AccountKeys operation. -type AccountKeysPrm struct { - id *owner.ID -} - -// SetID sets owner ID. -func (a *AccountKeysPrm) SetID(id *owner.ID) { - a.id = id -} - -// AccountKeys requests public keys of NeoFS account from NeoFS ID contract. -func (x *ClientWrapper) AccountKeys(prm AccountKeysPrm) (keys.PublicKeys, error) { - var args neofsid.KeyListingArgs - - args.SetOwnerID(prm.id.ToV2().GetValue()) - - res, err := x.client.AccountKeys(args) - if err != nil { - return nil, err - } - - binKeys := res.Keys() - ks := make(keys.PublicKeys, 0, len(binKeys)) - - curve := elliptic.P256() - - for i := range binKeys { - k, err := keys.NewPublicKeyFromBytes(binKeys[i], curve) - if err != nil { - return nil, fmt.Errorf("received invalid key: %w", err) - } - - ks = append(ks, k) - } - - return ks, nil -} - -// ManageKeysPrm groups parameters of ManageKeys operation. -type ManageKeysPrm struct { - ownerID []byte - ks [][]byte - add bool - - client.InvokePrmOptional -} - -// SetOwnerID sets Owner ID. -func (m *ManageKeysPrm) SetOwnerID(ownerID []byte) { - m.ownerID = ownerID -} - -// SetKeys sets keys to add/remove. -func (m *ManageKeysPrm) SetKeys(ks [][]byte) { - m.ks = ks -} - -// SetAdd sets operation type. -func (m *ManageKeysPrm) SetAdd(add bool) { - m.add = add -} - -// ManageKeys adds/removes list of public keys to/from NeoFS account. -func (x *ClientWrapper) ManageKeys(prm ManageKeysPrm) error { - type args interface { - SetOwnerID([]byte) - SetKeys([][]byte) - SetOptionalPrm(optional client.InvokePrmOptional) - } - - var ( - a args - call func(args) error - ) - - if prm.add { - a = new(neofsid.AddKeysArgs) - call = func(a args) error { - return x.client.AddKeys(*a.(*neofsid.AddKeysArgs)) - } - } else { - a = new(neofsid.RemoveKeysArgs) - call = func(a args) error { - return x.client.RemoveKeys(*a.(*neofsid.RemoveKeysArgs)) - } - } - - a.SetOwnerID(prm.ownerID) - a.SetKeys(prm.ks) - a.SetOptionalPrm(prm.InvokePrmOptional) - - return call(a) -}