From b9a1aaec2399aff7a594c975fce4570ec413ca30 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 31 Mar 2021 17:59:07 +0300 Subject: [PATCH] [#452] morph/client: Add reputation contract client wrapper Signed-off-by: Alex Vanin --- pkg/morph/client/reputation/wrapper/get.go | 84 +++++++++++++++++++ pkg/morph/client/reputation/wrapper/list.go | 55 ++++++++++++ pkg/morph/client/reputation/wrapper/put.go | 51 +++++++++++ .../client/reputation/wrapper/wrapper.go | 14 ++++ 4 files changed, 204 insertions(+) create mode 100644 pkg/morph/client/reputation/wrapper/get.go create mode 100644 pkg/morph/client/reputation/wrapper/list.go create mode 100644 pkg/morph/client/reputation/wrapper/put.go create mode 100644 pkg/morph/client/reputation/wrapper/wrapper.go diff --git a/pkg/morph/client/reputation/wrapper/get.go b/pkg/morph/client/reputation/wrapper/get.go new file mode 100644 index 000000000..4924b47a2 --- /dev/null +++ b/pkg/morph/client/reputation/wrapper/get.go @@ -0,0 +1,84 @@ +package wrapper + +import ( + reputationClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/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 + } + + // GetResults groups the result of "get reputation value" and + // "get reputation value by reputation id" test invocations. + GetResult struct { + reputations [][]byte // todo: replace with the slice of structures + } +) + +// 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() [][]byte { + 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.Bytes()) + + data, err := (*reputationClient.Client)(w).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 := (*reputationClient.Client)(w).GetByID(args) + if err != nil { + return nil, err + } + + return parseGetResult(data) +} + +func parseGetResult(data *reputationClient.GetResult) (*GetResult, error) { + rawReputations := data.Reputations() + + // todo: unmarshal all reputation values into structure + + return &GetResult{ + reputations: rawReputations, + }, nil +} diff --git a/pkg/morph/client/reputation/wrapper/list.go b/pkg/morph/client/reputation/wrapper/list.go new file mode 100644 index 000000000..1392de2e0 --- /dev/null +++ b/pkg/morph/client/reputation/wrapper/list.go @@ -0,0 +1,55 @@ +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 := (*reputationClient.Client)(w).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 +} diff --git a/pkg/morph/client/reputation/wrapper/put.go b/pkg/morph/client/reputation/wrapper/put.go new file mode 100644 index 000000000..b181d97a8 --- /dev/null +++ b/pkg/morph/client/reputation/wrapper/put.go @@ -0,0 +1,51 @@ +package wrapper + +import ( + reputationClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" +) + +type ( + // PutArgs groups the arguments of "put reputation value" invocation call. + PutArgs struct { + epoch uint64 + peerID reputation.PeerID + value []byte // todo: replace with struct + } +) + +// 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 []byte) { + p.value = v +} + +// Put invokes direct call of "put reputation value" method of reputation contract. +func (w *ClientWrapper) Put(v PutArgs) error { + args := reputationClient.PutArgs{} + args.SetEpoch(v.epoch) + args.SetPeerID(v.peerID.Bytes()) + args.SetValue(v.value) // todo: marshal reputation value to `value` bytes there + + return (*reputationClient.Client)(w).Put(args) +} + +// PutViaNotary invokes notary call of "put reputation value" method of +// reputation contract. +func (w *ClientWrapper) PutViaNotary(v PutArgs) error { + args := reputationClient.PutArgs{} + args.SetEpoch(v.epoch) + args.SetPeerID(v.peerID.Bytes()) + args.SetValue(v.value) // todo: marshal reputation value to `value` bytes there + + return (*reputationClient.Client)(w).PutViaNotary(args) +} diff --git a/pkg/morph/client/reputation/wrapper/wrapper.go b/pkg/morph/client/reputation/wrapper/wrapper.go new file mode 100644 index 000000000..2a790ce84 --- /dev/null +++ b/pkg/morph/client/reputation/wrapper/wrapper.go @@ -0,0 +1,14 @@ +package wrapper + +import ( + "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 reputation.Client + +// WrapClient wraps reputation contract client and returns ClientWrapper instance. +func WrapClient(c *reputation.Client) *ClientWrapper { + return (*ClientWrapper)(c) +}