[#625] client/audit: remove intermediate wrapper

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

View file

@ -29,7 +29,7 @@ import (
timerEvent "github.com/nspcc-dev/neofs-node/pkg/innerring/timers"
"github.com/nspcc-dev/neofs-node/pkg/metrics"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
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"
@ -74,7 +74,7 @@ type (
epochDuration atomic.Uint64
statusIndex *innerRingIndexer
precision precision.Fixed8Converter
auditClient *auditWrapper.ClientWrapper
auditClient *auditClient.Client
healthStatus atomic.Value
balanceClient *balanceClient.Client
netmapClient *nmClient.Client
@ -463,7 +463,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
// do not use TryNotary() in audit wrapper
// audit operations do not require multisignatures
server.auditClient, err = auditWrapper.NewFromMorph(server.morphClient, server.contracts.audit, fee)
server.auditClient, err = auditClient.NewFromMorph(server.morphClient, server.contracts.audit, fee)
if err != nil {
return nil, err
}

View file

@ -14,7 +14,7 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/basic"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
@ -40,7 +40,7 @@ type settlementDeps struct {
cnrSrc container.Source
auditClient *auditClient.ClientWrapper
auditClient *auditClient.Client
nmSrc netmap.Source

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance"
auditwrp "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
control "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
"github.com/nspcc-dev/neofs-node/pkg/util/state"
@ -151,7 +151,7 @@ func (s *Server) WriteReport(r *audit.Report) error {
res := r.Result()
res.SetPublicKey(s.pubKey)
prm := auditwrp.PutPrm{}
prm := auditClient.PutPrm{}
prm.SetResult(res)
return s.auditClient.PutAuditResult(prm)

View file

@ -1,6 +1,10 @@
package audit
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"
)
@ -25,7 +29,12 @@ const (
listByNodeResultsMethod = "listByNode"
)
// 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 ...client.StaticClientOption) (*Client, error) {
sc, err := client.NewStatic(cli, contract, fee, opts...)
if err != nil {
return nil, fmt.Errorf("could not create static client of audit contract: %w", err)
}
return &Client{client: sc}, nil
}

View file

@ -4,51 +4,31 @@ import (
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
auditAPI "github.com/nspcc-dev/neofs-sdk-go/audit"
)
// GetAuditResultArgs groups the arguments
// of "get audit result" invocation call.
type GetAuditResultArgs struct {
id []byte
}
// GetAuditResult returns audit result structure stored in audit contract.
func (c *Client) GetAuditResult(id ResultID) (*auditAPI.Result, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(getResultMethod)
prm.SetArgs([]byte(id))
// GetAuditResultValue groups the stack parameters
// returned by "get audit result" test invoke.
type GetAuditResultValue struct {
rawResult []byte
}
// SetID sets audit result ID generated by audit contract.
func (g *GetAuditResultArgs) SetID(id []byte) {
g.id = id
}
// Result returns audit result structure in binary format.
func (v *GetAuditResultValue) Result() []byte {
return v.rawResult
}
// GetAuditResult invokes the call of "get audit result" method
// of NeoFS Audit contract.
func (c *Client) GetAuditResult(args GetAuditResultArgs) (*GetAuditResultValue, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(getResultMethod)
invokePrm.SetArgs(args.id)
prms, err := c.client.TestInvoke(invokePrm)
prms, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getResultMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getResultMethod, ln)
}
resultBytes, err := client.BytesFromStackItem(prms[0])
value, err := client.BytesFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", getResultMethod, err)
}
return &GetAuditResultValue{
rawResult: resultBytes,
}, nil
auditRes := auditAPI.NewResult()
if err := auditRes.Unmarshal(value); err != nil {
return nil, fmt.Errorf("could not unmarshal audit result structure: %w", err)
}
return auditRes, nil
}

View file

@ -5,125 +5,74 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
)
// ListResultsArgs groups the arguments
// of "list all audit result IDs" test invoke call.
type ListResultsArgs struct{}
// ListResultsByEpochArgs groups the arguments
// of "list audit result IDs by epoch" test invoke call.
type ListResultsByEpochArgs struct {
epoch int64
}
// ListResultsByCIDArgs groups the arguments
// of "list audit result IDs by epoch and CID" test invoke call.
type ListResultsByCIDArgs struct {
ListResultsByEpochArgs
cid []byte
}
// ListResultsByNodeArgs groups the arguments
// of "list audit result IDs by epoch, CID, and node key" test invoke call.
type ListResultsByNodeArgs struct {
ListResultsByCIDArgs
nodeKey []byte
}
// ListResultsValues groups the stack parameters
// returned by "list audit results" test invoke.
type ListResultsValues struct {
rawResults [][]byte // audit results in a binary format
}
// RawResults returns list of audit result IDs
// in a binary format.
func (v *ListResultsValues) RawResults() [][]byte {
return v.rawResults
}
// SetEpoch sets epoch of listing audit results.
func (v *ListResultsByEpochArgs) SetEpoch(epoch int64) {
v.epoch = epoch
}
// SetCID sets container ID of listing audit results.
func (v *ListResultsByCIDArgs) SetCID(cid []byte) {
v.cid = cid
}
// SetNodeKey sets public key of node that produced listing audit results.
func (v *ListResultsByNodeArgs) SetNodeKey(key []byte) {
v.nodeKey = key
}
// ListAuditResults performs the test invoke of "list all audit result IDs"
// method of NeoFS Audit contract.
func (c *Client) ListAuditResults(args ListResultsArgs) (*ListResultsValues, error) {
// ListAllAuditResultID returns a list of all audit result IDs inside audit contract.
func (c *Client) ListAllAuditResultID() ([]ResultID, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(listResultsMethod)
items, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listResultsMethod, err)
}
return parseAuditResults(items, listResultsMethod)
}
// ListAuditResultsByEpoch performs the test invoke of "list audit result IDs
// by epoch" method of NeoFS Audit contract.
func (c *Client) ListAuditResultsByEpoch(args ListResultsByEpochArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
// ListAuditResultIDByEpoch returns a list of audit result IDs inside audit
// contract for specific epoch number.
func (c *Client) ListAuditResultIDByEpoch(epoch uint64) ([]ResultID, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(listByEpochResultsMethod)
prm.SetArgs(int64(epoch))
invokePrm.SetMethod(listByEpochResultsMethod)
invokePrm.SetArgs(args.epoch)
items, err := c.client.TestInvoke(invokePrm)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByEpochResultsMethod, err)
}
return parseAuditResults(items, listByEpochResultsMethod)
}
// ListAuditResultsByCID performs the test invoke of "list audit result IDs
// by epoch and CID" method of NeoFS Audit contract.
func (c *Client) ListAuditResultsByCID(args ListResultsByCIDArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
// ListAuditResultIDByCID returns a list of audit result IDs inside audit
// contract for specific epoch number and container ID.
func (c *Client) ListAuditResultIDByCID(epoch uint64, cid *cid.ID) ([]ResultID, error) {
v2 := cid.ToV2()
if v2 == nil {
return nil, errUnsupported // use other major version if there any
}
invokePrm.SetMethod(listByCIDResultsMethod)
invokePrm.SetArgs(args.epoch, args.cid)
prm := client.TestInvokePrm{}
prm.SetMethod(listByCIDResultsMethod)
prm.SetArgs(int64(epoch), v2.GetValue())
items, err := c.client.TestInvoke(invokePrm)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByCIDResultsMethod, err)
}
return parseAuditResults(items, listByCIDResultsMethod)
}
// ListAuditResultsByNode performs the test invoke of "list audit result IDs
// by epoch, CID, and node key" method of NeoFS Audit contract.
func (c *Client) ListAuditResultsByNode(args ListResultsByNodeArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
// ListAuditResultIDByNode returns a list of audit result IDs inside audit
// contract for specific epoch number, container ID and inner ring public key.
func (c *Client) ListAuditResultIDByNode(epoch uint64, cid *cid.ID, nodeKey []byte) ([]ResultID, error) {
v2 := cid.ToV2()
if v2 == nil {
return nil, errUnsupported // use other major version if there any
}
invokePrm.SetMethod(listByNodeResultsMethod)
invokePrm.SetArgs(args.epoch, args.cid, args.nodeKey)
prm := client.TestInvokePrm{}
prm.SetMethod(listByNodeResultsMethod)
prm.SetArgs(int64(epoch), v2.GetValue(), nodeKey)
items, err := c.client.TestInvoke(invokePrm)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByNodeResultsMethod, err)
}
return parseAuditResults(items, listByNodeResultsMethod)
}
func parseAuditResults(items []stackitem.Item, method string) (*ListResultsValues, error) {
func parseAuditResults(items []stackitem.Item, method string) ([]ResultID, error) {
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
@ -133,17 +82,14 @@ func parseAuditResults(items []stackitem.Item, method string) (*ListResultsValue
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
}
res := &ListResultsValues{
rawResults: make([][]byte, 0, len(items)),
}
res := make([]ResultID, 0, len(items))
for i := range items {
rawRes, err := client.BytesFromStackItem(items[i])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", method, err)
}
res.rawResults = append(res.rawResults, rawRes)
res = append(res, rawRes)
}
return res, nil

View file

@ -1,36 +1,46 @@
package audit
import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
auditAPI "github.com/nspcc-dev/neofs-sdk-go/audit"
)
// PutAuditResultArgs groups the arguments
// of "put audit result" invocation call.
type PutAuditResultArgs struct {
rawResult []byte // audit result in NeoFS API-compatible binary representation
// ResultID is an identity of audit result inside audit contract.
type ResultID []byte
var errUnsupported = errors.New("unsupported structure version")
// PutPrm groups parameters of PutAuditResult operation.
type PutPrm struct {
result *auditAPI.Result
client.InvokePrmOptional
}
// SetRawResult sets audit result structure
// in NeoFS API-compatible binary representation.
func (g *PutAuditResultArgs) SetRawResult(v []byte) {
g.rawResult = v
// SetResult sets audit result.
func (p *PutPrm) SetResult(result *auditAPI.Result) {
p.result = result
}
// PutAuditResult invokes the call of "put audit result" method
// of NeoFS Audit contract.
func (c *Client) PutAuditResult(args PutAuditResultArgs) error {
// PutAuditResult saves passed audit result structure in NeoFS system
// through Audit contract call.
//
// Returns encountered error that caused the saving to interrupt.
func (c *Client) PutAuditResult(p PutPrm) error {
rawResult, err := p.result.Marshal()
if err != nil {
return fmt.Errorf("could not marshal audit result: %w", err)
}
prm := client.InvokePrm{}
prm.SetMethod(putResultMethod)
prm.SetArgs(args.rawResult)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
prm.SetArgs(rawResult)
prm.InvokePrmOptional = p.InvokePrmOptional
err = c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", putResultMethod, err)
}

View file

@ -1,4 +1,4 @@
package audit_test
package audit
import (
"testing"
@ -7,7 +7,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
auditAPI "github.com/nspcc-dev/neofs-sdk-go/audit"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/stretchr/testify/require"
@ -30,7 +29,7 @@ func TestAuditResults(t *testing.T) {
morphClient, err := client.New(key, endpoint)
require.NoError(t, err)
auditClientWrapper, err := auditWrapper.NewFromMorph(morphClient, auditHash, 0)
auditClientWrapper, err := NewFromMorph(morphClient, auditHash, 0)
require.NoError(t, err)
id := cidtest.ID()
@ -40,7 +39,7 @@ func TestAuditResults(t *testing.T) {
auditRes.SetPublicKey(key.PublicKey().Bytes())
auditRes.SetContainerID(id)
prm := auditWrapper.PutPrm{}
prm := PutPrm{}
prm.SetResult(auditRes)
require.NoError(t, auditClientWrapper.PutAuditResult(prm))

View file

@ -1,144 +0,0 @@
package audit
import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
auditAPI "github.com/nspcc-dev/neofs-sdk-go/audit"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
)
// ResultID is an identity of audit result inside audit contract.
type ResultID []byte
var errUnsupported = errors.New("unsupported structure version")
// PutPrm groups parameters of PutAuditResult operation.
type PutPrm struct {
result *auditAPI.Result
client.InvokePrmOptional
}
// SetResult sets audit result.
func (p *PutPrm) SetResult(result *auditAPI.Result) {
p.result = result
}
// PutAuditResult saves passed audit result structure in NeoFS system
// through Audit contract call.
//
// Returns encountered error that caused the saving to interrupt.
func (w *ClientWrapper) PutAuditResult(prm PutPrm) error {
rawResult, err := prm.result.Marshal()
if err != nil {
return fmt.Errorf("could not marshal audit result: %w", err)
}
args := audit.PutAuditResultArgs{}
args.SetRawResult(rawResult)
args.InvokePrmOptional = prm.InvokePrmOptional
return w.client.
PutAuditResult(args)
}
// ListAllAuditResultID returns a list of all audit result IDs inside audit contract.
func (w *ClientWrapper) ListAllAuditResultID() ([]ResultID, error) {
args := audit.ListResultsArgs{}
values, err := w.client.ListAuditResults(args)
if err != nil {
return nil, err
}
return parseRawResult(values), nil
}
// ListAuditResultIDByEpoch returns a list of audit result IDs inside audit
// contract for specific epoch number.
func (w *ClientWrapper) ListAuditResultIDByEpoch(epoch uint64) ([]ResultID, error) {
args := audit.ListResultsByEpochArgs{}
args.SetEpoch(int64(epoch))
values, err := w.client.ListAuditResultsByEpoch(args)
if err != nil {
return nil, err
}
return parseRawResult(values), nil
}
// ListAuditResultIDByCID returns a list of audit result IDs inside audit
// contract for specific epoch number and container ID.
func (w *ClientWrapper) ListAuditResultIDByCID(epoch uint64, cid *cid.ID) ([]ResultID, error) {
args := audit.ListResultsByCIDArgs{}
args.SetEpoch(int64(epoch))
v2 := cid.ToV2()
if v2 == nil {
return nil, errUnsupported // use other major version if there any
}
args.SetCID(v2.GetValue())
values, err := w.client.ListAuditResultsByCID(args)
if err != nil {
return nil, err
}
return parseRawResult(values), nil
}
// ListAuditResultIDByNode returns a list of audit result IDs inside audit
// contract for specific epoch number, container ID and inner ring public key.
func (w *ClientWrapper) ListAuditResultIDByNode(epoch uint64, cid *cid.ID, key []byte) ([]ResultID, error) {
args := audit.ListResultsByNodeArgs{}
args.SetEpoch(int64(epoch))
args.SetNodeKey(key)
v2 := cid.ToV2()
if v2 == nil {
return nil, errUnsupported // use other major version if there any
}
args.SetCID(v2.GetValue())
values, err := w.client.ListAuditResultsByNode(args)
if err != nil {
return nil, err
}
return parseRawResult(values), nil
}
func parseRawResult(values *audit.ListResultsValues) []ResultID {
rawResults := values.RawResults()
result := make([]ResultID, 0, len(rawResults))
for i := range rawResults {
result = append(result, rawResults[i])
}
return result
}
// GetAuditResult returns audit result structure stored in audit contract.
func (w *ClientWrapper) GetAuditResult(id ResultID) (*auditAPI.Result, error) {
args := audit.GetAuditResultArgs{}
args.SetID(id)
value, err := w.client.GetAuditResult(args)
if err != nil {
return nil, err
}
auditRes := auditAPI.NewResult()
if err := auditRes.Unmarshal(value.Result()); err != nil {
return nil, fmt.Errorf("could not unmarshal audit result structure: %w", err)
}
return auditRes, nil
}

View file

@ -1,30 +0,0 @@
package audit
import (
"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/audit"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/internal"
)
// ClientWrapper is a wrapper over Audit contract
// client which implements storage of audit results.
type ClientWrapper struct {
internal.StaticClient
client *audit.Client
}
// NewFromMorph returns the wrapper instance from the raw morph client.
func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...client.StaticClientOption) (*ClientWrapper, error) {
staticClient, err := client.NewStatic(cli, contract, fee, opts...)
if err != nil {
return nil, err
}
return &ClientWrapper{
StaticClient: staticClient,
client: audit.New(staticClient),
}, nil
}