[#55] reputation: move package from neofs-api-go

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-08 11:41:39 +03:00 committed by Alex Vanin
parent ff54fb7647
commit bdb99877f6
5 changed files with 794 additions and 0 deletions

84
reputation/peer.go Normal file
View file

@ -0,0 +1,84 @@
package reputation
import (
"bytes"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/reputation"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
)
// PeerID represents peer ID compatible with NeoFS API v2.
type PeerID reputation.PeerID
// NewPeerID creates and returns blank PeerID.
//
// Defaults:
// - publicKey: nil.
func NewPeerID() *PeerID {
return PeerIDFromV2(new(reputation.PeerID))
}
// PeerIDFromV2 converts NeoFS API v2 reputation.PeerID message to PeerID.
//
// Nil reputation.PeerID converts to nil.
func PeerIDFromV2(id *reputation.PeerID) *PeerID {
return (*PeerID)(id)
}
// SetPublicKey sets peer ID as a compressed public key.
func (x *PeerID) SetPublicKey(v [signature.PublicKeyCompressedSize]byte) {
(*reputation.PeerID)(x).SetPublicKey(v[:])
}
// ToV2 converts PeerID to NeoFS API v2 reputation.PeerID message.
//
// Nil PeerID converts to nil.
func (x *PeerID) ToV2() *reputation.PeerID {
return (*reputation.PeerID)(x)
}
// Equal returns true if identifiers are identical.
func (x *PeerID) Equal(x2 *PeerID) bool {
return bytes.Equal(
(*reputation.PeerID)(x).GetPublicKey(),
(*reputation.PeerID)(x2).GetPublicKey(),
)
}
// Parse parses PeerID from base58 string.
func (x *PeerID) Parse(s string) error {
data, err := base58.Decode(s)
if err != nil {
return err
}
(*reputation.PeerID)(x).SetPublicKey(data)
return nil
}
// String returns base58 string representation of PeerID.
func (x *PeerID) String() string {
return base58.Encode((*reputation.PeerID)(x).GetPublicKey())
}
// Marshal marshals PeerID into a protobuf binary form.
func (x *PeerID) Marshal() ([]byte, error) {
return (*reputation.PeerID)(x).StableMarshal(nil)
}
// Unmarshal unmarshals protobuf binary representation of PeerID.
func (x *PeerID) Unmarshal(data []byte) error {
return (*reputation.PeerID)(x).Unmarshal(data)
}
// MarshalJSON encodes PeerID to protobuf JSON format.
func (x *PeerID) MarshalJSON() ([]byte, error) {
return (*reputation.PeerID)(x).MarshalJSON()
}
// UnmarshalJSON decodes PeerID from protobuf JSON format.
func (x *PeerID) UnmarshalJSON(data []byte) error {
return (*reputation.PeerID)(x).UnmarshalJSON(data)
}

88
reputation/peer_test.go Normal file
View file

@ -0,0 +1,88 @@
package reputation_test
import (
"testing"
reputationV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
reputationtest "github.com/nspcc-dev/neofs-sdk-go/reputation/test"
"github.com/stretchr/testify/require"
)
func TestPeerID_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *reputation.PeerID
require.Nil(t, x.ToV2())
})
t.Run("nil", func(t *testing.T) {
peerID := reputationtest.GeneratePeerID()
require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2()))
})
}
func TestPeerID_String(t *testing.T) {
t.Run("Parse/String", func(t *testing.T) {
id := reputationtest.GeneratePeerID()
strID := id.String()
id2 := reputation.NewPeerID()
err := id2.Parse(strID)
require.NoError(t, err)
require.Equal(t, id, id2)
})
t.Run("nil", func(t *testing.T) {
id := reputation.NewPeerID()
require.Empty(t, id.String())
})
}
func TestPeerIDEncoding(t *testing.T) {
id := reputationtest.GeneratePeerID()
t.Run("binary", func(t *testing.T) {
data, err := id.Marshal()
require.NoError(t, err)
id2 := reputation.NewPeerID()
require.NoError(t, id2.Unmarshal(data))
require.Equal(t, id, id2)
})
t.Run("json", func(t *testing.T) {
data, err := id.MarshalJSON()
require.NoError(t, err)
id2 := reputation.NewPeerID()
require.NoError(t, id2.UnmarshalJSON(data))
require.Equal(t, id, id2)
})
}
func TestPeerIDFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.PeerID
require.Nil(t, reputation.PeerIDFromV2(x))
})
}
func TestNewPeerID(t *testing.T) {
t.Run("default values", func(t *testing.T) {
id := reputation.NewPeerID()
// convert to v2 message
idV2 := id.ToV2()
require.Nil(t, idV2.GetPublicKey())
})
}

View file

@ -0,0 +1,59 @@
package reputationtest
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
"github.com/stretchr/testify/require"
)
func GeneratePeerID() *reputation.PeerID {
v := reputation.NewPeerID()
p, err := keys.NewPrivateKey()
if err != nil {
panic(err)
}
key := [signature.PublicKeyCompressedSize]byte{}
copy(key[:], p.Bytes())
v.SetPublicKey(key)
return v
}
func GenerateTrust() *reputation.Trust {
v := reputation.NewTrust()
v.SetPeer(GeneratePeerID())
v.SetValue(1.5)
return v
}
func GeneratePeerToPeerTrust() *reputation.PeerToPeerTrust {
v := reputation.NewPeerToPeerTrust()
v.SetTrustingPeer(GeneratePeerID())
v.SetTrust(GenerateTrust())
return v
}
func GenerateGlobalTrust() *reputation.GlobalTrust {
v := reputation.NewGlobalTrust()
v.SetManager(GeneratePeerID())
v.SetTrust(GenerateTrust())
return v
}
func GenerateSignedGlobalTrust(t testing.TB) *reputation.GlobalTrust {
gt := GenerateGlobalTrust()
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
require.NoError(t, gt.Sign(&priv.PrivateKey))
return gt
}

313
reputation/trust.go Normal file
View file

@ -0,0 +1,313 @@
package reputation
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-api-go/v2/reputation"
signatureV2 "github.com/nspcc-dev/neofs-api-go/v2/signature"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
"github.com/nspcc-dev/neofs-sdk-go/version"
)
// Trust represents peer's trust compatible with NeoFS API v2.
type Trust reputation.Trust
// NewTrust creates and returns blank Trust.
//
// Defaults:
// - value: 0;
// - PeerID: nil.
func NewTrust() *Trust {
return TrustFromV2(new(reputation.Trust))
}
// TrustFromV2 converts NeoFS API v2
// reputation.Trust message structure to Trust.
//
// Nil reputation.Trust converts to nil.
func TrustFromV2(t *reputation.Trust) *Trust {
return (*Trust)(t)
}
// ToV2 converts Trust to NeoFS API v2
// reputation.Trust message structure.
//
// Nil Trust converts to nil.
func (x *Trust) ToV2() *reputation.Trust {
return (*reputation.Trust)(x)
}
// TrustsToV2 converts slice of Trust's to slice of
// NeoFS API v2 reputation.Trust message structures.
func TrustsToV2(xs []*Trust) (res []*reputation.Trust) {
if xs != nil {
res = make([]*reputation.Trust, 0, len(xs))
for i := range xs {
res = append(res, xs[i].ToV2())
}
}
return
}
// SetPeer sets trusted peer ID.
func (x *Trust) SetPeer(id *PeerID) {
(*reputation.Trust)(x).SetPeer(id.ToV2())
}
// Peer returns trusted peer ID.
func (x *Trust) Peer() *PeerID {
return PeerIDFromV2(
(*reputation.Trust)(x).GetPeer())
}
// SetValue sets trust value.
func (x *Trust) SetValue(val float64) {
(*reputation.Trust)(x).SetValue(val)
}
// Value returns trust value.
func (x *Trust) Value() float64 {
return (*reputation.Trust)(x).GetValue()
}
// Marshal marshals Trust into a protobuf binary form.
func (x *Trust) Marshal() ([]byte, error) {
return (*reputation.Trust)(x).StableMarshal(nil)
}
// Unmarshal unmarshals protobuf binary representation of Trust.
func (x *Trust) Unmarshal(data []byte) error {
return (*reputation.Trust)(x).Unmarshal(data)
}
// MarshalJSON encodes Trust to protobuf JSON format.
func (x *Trust) MarshalJSON() ([]byte, error) {
return (*reputation.Trust)(x).MarshalJSON()
}
// UnmarshalJSON decodes Trust from protobuf JSON format.
func (x *Trust) UnmarshalJSON(data []byte) error {
return (*reputation.Trust)(x).UnmarshalJSON(data)
}
// PeerToPeerTrust represents directed peer-to-peer trust
// compatible with NeoFS API v2.
type PeerToPeerTrust reputation.PeerToPeerTrust
// NewPeerToPeerTrust creates and returns blank PeerToPeerTrust.
//
// Defaults:
// - trusting: nil;
// - trust: nil.
func NewPeerToPeerTrust() *PeerToPeerTrust {
return PeerToPeerTrustFromV2(new(reputation.PeerToPeerTrust))
}
// PeerToPeerTrustFromV2 converts NeoFS API v2
// reputation.PeerToPeerTrust message structure to PeerToPeerTrust.
//
// Nil reputation.PeerToPeerTrust converts to nil.
func PeerToPeerTrustFromV2(t *reputation.PeerToPeerTrust) *PeerToPeerTrust {
return (*PeerToPeerTrust)(t)
}
// ToV2 converts PeerToPeerTrust to NeoFS API v2
// reputation.PeerToPeerTrust message structure.
//
// Nil PeerToPeerTrust converts to nil.
func (x *PeerToPeerTrust) ToV2() *reputation.PeerToPeerTrust {
return (*reputation.PeerToPeerTrust)(x)
}
// SetTrustingPeer sets trusting peer ID.
func (x *PeerToPeerTrust) SetTrustingPeer(id *PeerID) {
(*reputation.PeerToPeerTrust)(x).SetTrustingPeer(id.ToV2())
}
// TrustingPeer returns trusting peer ID.
func (x *PeerToPeerTrust) TrustingPeer() *PeerID {
return PeerIDFromV2(
(*reputation.PeerToPeerTrust)(x).
GetTrustingPeer(),
)
}
// SetTrust sets trust value of the trusting peer to the trusted one.
func (x *PeerToPeerTrust) SetTrust(t *Trust) {
(*reputation.PeerToPeerTrust)(x).SetTrust(t.ToV2())
}
// Trust returns trust value of the trusting peer to the trusted one.
func (x *PeerToPeerTrust) Trust() *Trust {
return TrustFromV2(
(*reputation.PeerToPeerTrust)(x).GetTrust())
}
// Marshal marshals PeerToPeerTrust into a protobuf binary form.
func (x *PeerToPeerTrust) Marshal() ([]byte, error) {
return (*reputation.PeerToPeerTrust)(x).StableMarshal(nil)
}
// Unmarshal unmarshals protobuf binary representation of PeerToPeerTrust.
func (x *PeerToPeerTrust) Unmarshal(data []byte) error {
return (*reputation.PeerToPeerTrust)(x).Unmarshal(data)
}
// MarshalJSON encodes PeerToPeerTrust to protobuf JSON format.
func (x *PeerToPeerTrust) MarshalJSON() ([]byte, error) {
return (*reputation.PeerToPeerTrust)(x).MarshalJSON()
}
// UnmarshalJSON decodes PeerToPeerTrust from protobuf JSON format.
func (x *PeerToPeerTrust) UnmarshalJSON(data []byte) error {
return (*reputation.PeerToPeerTrust)(x).UnmarshalJSON(data)
}
// GlobalTrust represents peer's global trust compatible with NeoFS API v2.
type GlobalTrust reputation.GlobalTrust
// NewGlobalTrust creates and returns blank GlobalTrust.
//
// Defaults:
// - version: version.Current();
// - manager: nil;
// - trust: nil.
func NewGlobalTrust() *GlobalTrust {
gt := GlobalTrustFromV2(new(reputation.GlobalTrust))
gt.SetVersion(version.Current())
return gt
}
// GlobalTrustFromV2 converts NeoFS API v2
// reputation.GlobalTrust message structure to GlobalTrust.
//
// Nil reputation.GlobalTrust converts to nil.
func GlobalTrustFromV2(t *reputation.GlobalTrust) *GlobalTrust {
return (*GlobalTrust)(t)
}
// ToV2 converts GlobalTrust to NeoFS API v2
// reputation.GlobalTrust message structure.
//
// Nil GlobalTrust converts to nil.
func (x *GlobalTrust) ToV2() *reputation.GlobalTrust {
return (*reputation.GlobalTrust)(x)
}
// SetVersion sets GlobalTrust's protocol version.
func (x *GlobalTrust) SetVersion(version *version.Version) {
(*reputation.GlobalTrust)(x).SetVersion(version.ToV2())
}
// Version returns GlobalTrust's protocol version.
func (x *GlobalTrust) Version() *version.Version {
return version.NewFromV2(
(*reputation.GlobalTrust)(x).GetVersion())
}
func (x *GlobalTrust) setBodyField(setter func(*reputation.GlobalTrustBody)) {
if x != nil {
v2 := (*reputation.GlobalTrust)(x)
body := v2.GetBody()
if body == nil {
body = new(reputation.GlobalTrustBody)
v2.SetBody(body)
}
setter(body)
}
}
// SetManager sets node manager ID.
func (x *GlobalTrust) SetManager(id *PeerID) {
x.setBodyField(func(body *reputation.GlobalTrustBody) {
body.SetManager(id.ToV2())
})
}
// Manager returns node manager ID.
func (x *GlobalTrust) Manager() *PeerID {
return PeerIDFromV2(
(*reputation.GlobalTrust)(x).
GetBody().
GetManager(),
)
}
// SetTrust sets global trust value.
func (x *GlobalTrust) SetTrust(trust *Trust) {
x.setBodyField(func(body *reputation.GlobalTrustBody) {
body.SetTrust(trust.ToV2())
})
}
// Trust returns global trust value.
func (x *GlobalTrust) Trust() *Trust {
return TrustFromV2(
(*reputation.GlobalTrust)(x).
GetBody().
GetTrust(),
)
}
// Sign signs global trust value with key.
func (x *GlobalTrust) Sign(key *ecdsa.PrivateKey) error {
v2 := (*reputation.GlobalTrust)(x)
sigV2 := v2.GetSignature()
if sigV2 == nil {
sigV2 = new(refs.Signature)
v2.SetSignature(sigV2)
}
return signature.SignDataWithHandler(
key,
signatureV2.StableMarshalerWrapper{SM: v2.GetBody()},
func(key, sig []byte) {
sigV2.SetKey(key)
sigV2.SetSign(sig)
},
)
}
// VerifySignature verifies global trust signature.
func (x *GlobalTrust) VerifySignature() error {
v2 := (*reputation.GlobalTrust)(x)
sigV2 := v2.GetSignature()
if sigV2 == nil {
sigV2 = new(refs.Signature)
}
return signature.VerifyDataWithSource(
signatureV2.StableMarshalerWrapper{SM: v2.GetBody()},
func() ([]byte, []byte) {
return sigV2.GetKey(), sigV2.GetSign()
},
)
}
// Marshal marshals GlobalTrust into a protobuf binary form.
func (x *GlobalTrust) Marshal() ([]byte, error) {
return (*reputation.GlobalTrust)(x).StableMarshal(nil)
}
// Unmarshal unmarshals protobuf binary representation of GlobalTrust.
func (x *GlobalTrust) Unmarshal(data []byte) error {
return (*reputation.GlobalTrust)(x).Unmarshal(data)
}
// MarshalJSON encodes GlobalTrust to protobuf JSON format.
func (x *GlobalTrust) MarshalJSON() ([]byte, error) {
return (*reputation.GlobalTrust)(x).MarshalJSON()
}
// UnmarshalJSON decodes GlobalTrust from protobuf JSON format.
func (x *GlobalTrust) UnmarshalJSON(data []byte) error {
return (*reputation.GlobalTrust)(x).UnmarshalJSON(data)
}

250
reputation/trust_test.go Normal file
View file

@ -0,0 +1,250 @@
package reputation_test
import (
"testing"
reputationV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation"
reputationtestV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation/test"
"github.com/nspcc-dev/neofs-sdk-go/reputation"
reputationtest "github.com/nspcc-dev/neofs-sdk-go/reputation/test"
"github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/stretchr/testify/require"
)
func TestTrust(t *testing.T) {
trust := reputation.NewTrust()
id := reputationtest.GeneratePeerID()
trust.SetPeer(id)
require.Equal(t, id, trust.Peer())
val := 1.5
trust.SetValue(val)
require.Equal(t, val, trust.Value())
t.Run("binary encoding", func(t *testing.T) {
trust := reputationtest.GenerateTrust()
data, err := trust.Marshal()
require.NoError(t, err)
trust2 := reputation.NewTrust()
require.NoError(t, trust2.Unmarshal(data))
require.Equal(t, trust, trust2)
})
t.Run("JSON encoding", func(t *testing.T) {
trust := reputationtest.GenerateTrust()
data, err := trust.MarshalJSON()
require.NoError(t, err)
trust2 := reputation.NewTrust()
require.NoError(t, trust2.UnmarshalJSON(data))
require.Equal(t, trust, trust2)
})
}
func TestPeerToPeerTrust(t *testing.T) {
t.Run("v2", func(t *testing.T) {
p2ptV2 := reputationtestV2.GeneratePeerToPeerTrust(false)
p2pt := reputation.PeerToPeerTrustFromV2(p2ptV2)
require.Equal(t, p2ptV2, p2pt.ToV2())
})
t.Run("getters+setters", func(t *testing.T) {
p2pt := reputation.NewPeerToPeerTrust()
require.Nil(t, p2pt.TrustingPeer())
require.Nil(t, p2pt.Trust())
trusting := reputationtest.GeneratePeerID()
p2pt.SetTrustingPeer(trusting)
require.Equal(t, trusting, p2pt.TrustingPeer())
trust := reputationtest.GenerateTrust()
p2pt.SetTrust(trust)
require.Equal(t, trust, p2pt.Trust())
})
t.Run("encoding", func(t *testing.T) {
p2pt := reputationtest.GeneratePeerToPeerTrust()
t.Run("binary", func(t *testing.T) {
data, err := p2pt.Marshal()
require.NoError(t, err)
p2pt2 := reputation.NewPeerToPeerTrust()
require.NoError(t, p2pt2.Unmarshal(data))
require.Equal(t, p2pt, p2pt2)
})
t.Run("JSON", func(t *testing.T) {
data, err := p2pt.MarshalJSON()
require.NoError(t, err)
p2pt2 := reputation.NewPeerToPeerTrust()
require.NoError(t, p2pt2.UnmarshalJSON(data))
require.Equal(t, p2pt, p2pt2)
})
})
}
func TestGlobalTrust(t *testing.T) {
t.Run("v2", func(t *testing.T) {
gtV2 := reputationtestV2.GenerateGlobalTrust(false)
gt := reputation.GlobalTrustFromV2(gtV2)
require.Equal(t, gtV2, gt.ToV2())
})
t.Run("getters+setters", func(t *testing.T) {
gt := reputation.NewGlobalTrust()
require.Equal(t, version.Current(), gt.Version())
require.Nil(t, gt.Manager())
require.Nil(t, gt.Trust())
version := version.New()
version.SetMajor(13)
version.SetMinor(31)
gt.SetVersion(version)
require.Equal(t, version, gt.Version())
mngr := reputationtest.GeneratePeerID()
gt.SetManager(mngr)
require.Equal(t, mngr, gt.Manager())
trust := reputationtest.GenerateTrust()
gt.SetTrust(trust)
require.Equal(t, trust, gt.Trust())
})
t.Run("sign+verify", func(t *testing.T) {
gt := reputationtest.GenerateSignedGlobalTrust(t)
err := gt.VerifySignature()
require.NoError(t, err)
})
t.Run("encoding", func(t *testing.T) {
t.Run("binary", func(t *testing.T) {
gt := reputationtest.GenerateSignedGlobalTrust(t)
data, err := gt.Marshal()
require.NoError(t, err)
gt2 := reputation.NewGlobalTrust()
require.NoError(t, gt2.Unmarshal(data))
require.Equal(t, gt, gt2)
})
t.Run("JSON", func(t *testing.T) {
gt := reputationtest.GenerateSignedGlobalTrust(t)
data, err := gt.MarshalJSON()
require.NoError(t, err)
gt2 := reputation.NewGlobalTrust()
require.NoError(t, gt2.UnmarshalJSON(data))
require.Equal(t, gt, gt2)
})
})
}
func TestTrustFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.Trust
require.Nil(t, reputation.TrustFromV2(x))
})
}
func TestPeerToPeerTrustFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.PeerToPeerTrust
require.Nil(t, reputation.PeerToPeerTrustFromV2(x))
})
}
func TestGlobalTrustFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.GlobalTrust
require.Nil(t, reputation.GlobalTrustFromV2(x))
})
}
func TestTrust_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *reputation.Trust
require.Nil(t, x.ToV2())
})
}
func TestPeerToPeerTrust_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *reputation.PeerToPeerTrust
require.Nil(t, x.ToV2())
})
}
func TestGlobalTrust_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *reputation.GlobalTrust
require.Nil(t, x.ToV2())
})
}
func TestNewTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewTrust()
// check initial values
require.Zero(t, trust.Value())
require.Nil(t, trust.Peer())
// convert to v2 message
trustV2 := trust.ToV2()
require.Zero(t, trustV2.GetValue())
require.Nil(t, trustV2.GetPeer())
})
}
func TestNewPeerToPeerTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewPeerToPeerTrust()
// check initial values
require.Nil(t, trust.Trust())
require.Nil(t, trust.TrustingPeer())
// convert to v2 message
trustV2 := trust.ToV2()
require.Nil(t, trustV2.GetTrust())
require.Nil(t, trustV2.GetTrustingPeer())
})
}
func TestNewGlobalTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewGlobalTrust()
// check initial values
require.Nil(t, trust.Manager())
require.Nil(t, trust.Trust())
require.Equal(t, version.Current().String(), trust.Version().String())
// convert to v2 message
trustV2 := trust.ToV2()
require.Nil(t, trustV2.GetBody())
})
}