[#452] Use API structures for reputation PeerID and GlobalTrust

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-04-05 12:29:33 +03:00 committed by Alex Vanin
parent 7cf48d4d91
commit 27cf6f5112
6 changed files with 116 additions and 36 deletions

View file

@ -2,9 +2,9 @@ package reputation
import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-api-go/pkg/reputation"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
"github.com/pkg/errors"
)
@ -13,9 +13,11 @@ import (
type Put struct {
epoch uint64
peerID reputation.PeerID
value []byte
value reputation.GlobalTrust
}
const peerIDLength = 33 // compressed public key
// MorphEvent implements Neo:Morph Event interface.
func (Put) MorphEvent() {}
@ -30,8 +32,8 @@ func (p Put) PeerID() reputation.PeerID {
}
// Value returns reputation structure.
func (p Put) Value() []byte {
return p.value // consider returning parsed structure
func (p Put) Value() reputation.GlobalTrust {
return p.value
}
// ParsePut from notification into reputation event structure.
@ -45,6 +47,7 @@ func ParsePut(prms []stackitem.Item) (event.Event, error) {
return nil, event.WrongNumberOfParameters(3, ln)
}
// parse epoch number
epoch, err := client.IntFromStackItem(prms[0])
if err != nil {
return nil, errors.Wrap(err, "could not get integer epoch number")
@ -52,16 +55,29 @@ func ParsePut(prms []stackitem.Item) (event.Event, error) {
ev.epoch = uint64(epoch)
// parse peer ID value
peerID, err := client.BytesFromStackItem(prms[1])
if err != nil {
return nil, errors.Wrap(err, "could not get peer ID value")
}
ev.peerID = reputation.PeerIDFromBytes(peerID)
if ln := len(peerID); ln != peerIDLength {
return nil, errors.Errorf("peer ID is %d byte long, expected %d", ln, peerIDLength)
}
ev.value, err = client.BytesFromStackItem(prms[2])
var publicKey [33]byte
copy(publicKey[:], peerID)
ev.peerID.SetPublicKey(publicKey)
// parse global trust value
rawValue, err := client.BytesFromStackItem(prms[2])
if err != nil {
return nil, errors.Wrap(err, "could not get reputation value")
return nil, errors.Wrap(err, "could not get global trust value")
}
err = ev.value.Unmarshal(rawValue)
if err != nil {
return nil, errors.Wrap(err, "could not parse global trust value")
}
return ev, nil