From 919f4364f1447104e0c037b3dfc8b8206fad565c Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 21 Dec 2020 16:47:19 +0300 Subject: [PATCH] [#269] morph/audit: Implement wrapper over Audit contract client Signed-off-by: Leonard Lyubich --- pkg/morph/client/audit/wrapper/result.go | 50 ++++++++++++++++ pkg/morph/client/audit/wrapper/result_test.go | 59 +++++++++++++++++++ pkg/morph/client/audit/wrapper/wrapper.go | 14 +++++ 3 files changed, 123 insertions(+) create mode 100644 pkg/morph/client/audit/wrapper/result.go create mode 100644 pkg/morph/client/audit/wrapper/result_test.go create mode 100644 pkg/morph/client/audit/wrapper/wrapper.go diff --git a/pkg/morph/client/audit/wrapper/result.go b/pkg/morph/client/audit/wrapper/result.go new file mode 100644 index 000000000..743c17151 --- /dev/null +++ b/pkg/morph/client/audit/wrapper/result.go @@ -0,0 +1,50 @@ +package audit + +import ( + auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit" + "github.com/pkg/errors" +) + +// PutAuditResult saves passed audit result structure in NeoFS system +// through Audit contract call. +// +// Returns calculated container identifier and any error +// encountered that caused the saving to interrupt. +func (w *ClientWrapper) PutAuditResult(result *auditAPI.Result) error { + rawResult, err := result.Marshal() + if err != nil { + return errors.Wrap(err, "could not marshal audit result") + } + + args := audit.PutAuditResultArgs{} + args.SetRawResult(rawResult) + + return (*audit.Client)(w). + PutAuditResult(args) +} + +// ListAuditResults returns a list of all audit results in NeoFS system. +// The list is composed through Audit contract call. +func (w *ClientWrapper) ListAuditResults() ([]*auditAPI.Result, error) { + args := audit.ListResultsArgs{} + + values, err := (*audit.Client)(w).ListAuditResults(args) + if err != nil { + return nil, err + } + + rawResults := values.RawResults() + result := make([]*auditAPI.Result, 0, len(rawResults)) + + for i := range rawResults { + auditRes := auditAPI.NewResult() + if err := auditRes.Unmarshal(rawResults[i]); err != nil { + return nil, errors.Wrap(err, "could not unmarshal audit result structure") + } + + result = append(result, auditRes) + } + + return result, nil +} diff --git a/pkg/morph/client/audit/wrapper/result_test.go b/pkg/morph/client/audit/wrapper/result_test.go new file mode 100644 index 000000000..752e5f200 --- /dev/null +++ b/pkg/morph/client/audit/wrapper/result_test.go @@ -0,0 +1,59 @@ +package audit_test + +import ( + "crypto/sha256" + "testing" + "time" + + "github.com/nspcc-dev/neo-go/pkg/util" + auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit" + auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" + "github.com/stretchr/testify/require" +) + +func TestAuditResults(t *testing.T) { + t.Skip() + + endpoint := "http://morph_chain.neofs.devenv:30333" + sAuditHash := "96a746aa7186f775e5744a6e2c6566dc5c4a57a2" + irKeyWIF := "L3o221BojgcCPYgdbXsm6jn7ayTZ72xwREvBHXKknR8VJ3G4WmjB" + + key, err := crypto.WIFDecode(irKeyWIF) + require.NoError(t, err) + + pubKey := crypto.MarshalPublicKey(&key.PublicKey) + + auditHash, err := util.Uint160DecodeStringLE(sAuditHash) + require.NoError(t, err) + + morphClient, err := client.New(key, endpoint) + require.NoError(t, err) + + auditContractClient, err := client.NewStatic(morphClient, auditHash, 0) + require.NoError(t, err) + + auditClient := audit.New(auditContractClient) + + auditClientWrapper := auditWrapper.WrapClient(auditClient) + + cid := container.NewID() + cid.SetSHA256([sha256.Size]byte{1, 2, 3}) + + auditRes := auditAPI.NewResult() + auditRes.SetAuditEpoch(11) + auditRes.SetPublicKey(pubKey) + auditRes.SetContainerID(cid) + + require.NoError(t, auditClientWrapper.PutAuditResult(auditRes)) + + time.Sleep(5 * time.Second) + + list, err := auditClientWrapper.ListAuditResults() + require.NoError(t, err) + require.Len(t, list, 1) + require.Contains(t, list, auditRes) +} diff --git a/pkg/morph/client/audit/wrapper/wrapper.go b/pkg/morph/client/audit/wrapper/wrapper.go new file mode 100644 index 000000000..3236a1f51 --- /dev/null +++ b/pkg/morph/client/audit/wrapper/wrapper.go @@ -0,0 +1,14 @@ +package audit + +import ( + "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit" +) + +// ClientWrapper is a wrapper over Audit contract +// client which implements storage of audit results. +type ClientWrapper audit.Client + +// WrapClient wraps Audit contract client and returns ClientWrapper instance. +func WrapClient(c *audit.Client) *ClientWrapper { + return (*ClientWrapper)(c) +}