[#452] morph/client: Add reputation contract client wrapper

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-03-31 17:59:07 +03:00 committed by Alex Vanin
parent 9f1a49a562
commit b9a1aaec23
4 changed files with 204 additions and 0 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)
}