[#265] pkg/reputation: Implement PeerToPeerTrust structure

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-04-09 11:39:25 +03:00 committed by Leonard Lyubich
parent 269288119d
commit a61c15b990
3 changed files with 135 additions and 0 deletions

View file

@ -28,6 +28,14 @@ func GenerateTrust() *reputation.Trust {
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())

View file

@ -100,6 +100,86 @@ func (x *Trust) UnmarshalJSON(data []byte) error {
UnmarshalJSON(data)
}
// PeerToPeerTrust represents directed peer-to-peer trust
// compatible with NeoFS API v2.
type PeerToPeerTrust reputation.PeerToPeerTrust
// NewPeerToPeerTrust creates and returns blank PeerToPeerTrust.
func NewPeerToPeerTrust() *PeerToPeerTrust {
return PeerToPeerTrustFromV2(new(reputation.PeerToPeerTrust))
}
// PeerToPeerTrustFromV2 converts NeoFS API v2
// reputation.PeerToPeerTrust message structure to PeerToPeerTrust.
func PeerToPeerTrustFromV2(t *reputation.PeerToPeerTrust) *PeerToPeerTrust {
return (*PeerToPeerTrust)(t)
}
// ToV2 converts PeerToPeerTrust to NeoFS API v2
// reputation.PeerToPeerTrust message structure.
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.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (x *PeerToPeerTrust) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*reputation.PeerToPeerTrust)(x).StableMarshal(buf)
}
// 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

View file

@ -42,6 +42,53 @@ func TestTrust(t *testing.T) {
})
}
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)