[#265] v2/reputation: Support PeerToPeerTrust message

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-04-09 11:28:06 +03:00 committed by Leonard Lyubich
parent 771f395d9d
commit 269288119d
10 changed files with 172 additions and 6 deletions

View file

@ -81,6 +81,61 @@ func (x *Trust) FromGRPCMessage(m grpc.Message) error {
return nil
}
// ToGRPCMessage converts PeerToPeerTrust to gRPC-generated
// reputation.PeerToPeerTrust message.
func (x *PeerToPeerTrust) ToGRPCMessage() grpc.Message {
var m *reputation.PeerToPeerTrust
if x != nil {
m = new(reputation.PeerToPeerTrust)
m.SetTrustingPeer(x.trusting.ToGRPCMessage().(*reputation.PeerID))
m.SetTrust(x.trust.ToGRPCMessage().(*reputation.Trust))
}
return m
}
// FromGRPCMessage tries to restore PeerToPeerTrust from grpc.Message.
//
// Returns message.ErrUnexpectedMessageType if m is not
// a gRPC-generated reputation.PeerToPeerTrust message.
func (x *PeerToPeerTrust) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*reputation.PeerToPeerTrust)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
trusting := v.GetTrustingPeer()
if trusting == nil {
x.trusting = nil
} else {
if x.trusting == nil {
x.trusting = new(PeerID)
}
err = x.trusting.FromGRPCMessage(trusting)
if err != nil {
return err
}
}
trust := v.GetTrust()
if trust == nil {
x.trust = nil
} else {
if x.trust == nil {
x.trust = new(Trust)
}
err = x.trust.FromGRPCMessage(trust)
}
return err
}
// TrustsToGRPC converts slice of Trust structures
// to slice of gRPC-generated Trust messages.
func TrustsToGRPC(xs []*Trust) (res []*reputation.Trust) {
@ -404,7 +459,7 @@ func (x *SendIntermediateResultRequestBody) ToGRPCMessage() grpc.Message {
m = new(reputation.SendIntermediateResultRequest_Body)
m.SetIteration(x.iter)
m.SetTrust(x.trust.ToGRPCMessage().(*reputation.Trust))
m.SetTrust(x.trust.ToGRPCMessage().(*reputation.PeerToPeerTrust))
}
return m

View file

@ -68,7 +68,7 @@ func (x *SendIntermediateResultRequest_Body) SetIteration(v uint32) {
}
// SetTrust sets current global trust value.
func (x *SendIntermediateResultRequest_Body) SetTrust(v *Trust) {
func (x *SendIntermediateResultRequest_Body) SetTrust(v *PeerToPeerTrust) {
if x != nil {
x.Trust = v
}

Binary file not shown.

View file

@ -25,6 +25,20 @@ func (x *Trust) SetValue(v float64) {
}
}
// SetTrustingPeer sets trusting peer ID.
func (x *PeerToPeerTrust) SetTrustingPeer(v *PeerID) {
if x != nil {
x.TrustingPeer = v
}
}
// SetTrust sets trust value of trusting peer to the trusted one.
func (x *PeerToPeerTrust) SetTrust(v *Trust) {
if x != nil {
x.Trust = v
}
}
// SetManager sets manager ID.
func (x *GlobalTrust_Body) SetManager(v *PeerID) {
if x != nil {

Binary file not shown.

View file

@ -21,6 +21,14 @@ func (x *Trust) UnmarshalJSON(data []byte) error {
return message.UnmarshalJSON(x, data, new(reputation.Trust))
}
func (x *PeerToPeerTrust) MarshalJSON() ([]byte, error) {
return message.MarshalJSON(x)
}
func (x *PeerToPeerTrust) UnmarshalJSON(data []byte) error {
return message.UnmarshalJSON(x, data, new(reputation.PeerToPeerTrust))
}
func (x *GlobalTrust) MarshalJSON() ([]byte, error) {
return message.MarshalJSON(x)
}

View file

@ -84,6 +84,45 @@ func (x *Trust) Unmarshal(data []byte) error {
return message.Unmarshal(x, data, new(reputation.Trust))
}
const (
_ = iota
p2pTrustTrustingFNum
p2pTrustValueFNum
)
func (x *PeerToPeerTrust) StableMarshal(buf []byte) ([]byte, error) {
if x == nil {
return []byte{}, nil
}
if buf == nil {
buf = make([]byte, x.StableSize())
}
offset, err := protoutil.NestedStructureMarshal(p2pTrustTrustingFNum, buf, x.trusting)
if err != nil {
return nil, err
}
_, err = protoutil.NestedStructureMarshal(p2pTrustValueFNum, buf[offset:], x.trust)
if err != nil {
return nil, err
}
return buf, nil
}
func (x *PeerToPeerTrust) StableSize() (size int) {
size += protoutil.NestedStructureSize(p2pTrustTrustingFNum, x.trusting)
size += protoutil.NestedStructureSize(p2pTrustValueFNum, x.trust)
return
}
func (x *PeerToPeerTrust) Unmarshal(data []byte) error {
return message.Unmarshal(x, data, new(reputation.PeerToPeerTrust))
}
const (
_ = iota
globalTrustBodyManagerFNum

View file

@ -25,5 +25,6 @@ func TestMessageConvert(t *testing.T) {
func(empty bool) message.Message { return reputationtest.GenerateSendIntermediateResultResponse(empty) },
func(empty bool) message.Message { return reputationtest.GenerateGlobalTrustBody(empty) },
func(empty bool) message.Message { return reputationtest.GenerateGlobalTrust(empty) },
func(empty bool) message.Message { return reputationtest.GeneratePeerToPeerTrust(empty) },
)
}

View file

@ -28,6 +28,15 @@ func GenerateTrust(empty bool) *reputation.Trust {
return m
}
func GeneratePeerToPeerTrust(empty bool) *reputation.PeerToPeerTrust {
m := new(reputation.PeerToPeerTrust)
m.SetTrustingPeer(GeneratePeerID(empty))
m.SetTrust(GenerateTrust(empty))
return m
}
func GenerateGlobalTrustBody(empty bool) *reputation.GlobalTrustBody {
m := new(reputation.GlobalTrustBody)
@ -103,7 +112,7 @@ func GenerateSendIntermediateResultRequestBody(empty bool) *reputation.SendInter
m.SetIteration(564)
}
m.SetTrust(GenerateTrust(empty))
m.SetTrust(GeneratePeerToPeerTrust(empty))
return m
}

View file

@ -67,6 +67,46 @@ func (x *Trust) SetValue(v float64) {
}
}
// PeerToPeerTrust represents reputation.PeerToPeerTrust message
// from NeoFS API v2.
type PeerToPeerTrust struct {
trusting *PeerID
trust *Trust
}
// GetTrustingPeer returns trusting peer ID.
func (x *PeerToPeerTrust) GetTrustingPeer() *PeerID {
if x != nil {
return x.trusting
}
return nil
}
// SetTrustingPeer sets trusting peer ID.
func (x *PeerToPeerTrust) SetTrustingPeer(v *PeerID) {
if x != nil {
x.trusting = v
}
}
// GetTrust returns trust value of trusting peer to the trusted one.
func (x *PeerToPeerTrust) GetTrust() *Trust {
if x != nil {
return x.trust
}
return nil
}
// SetTrust sets trust value of trusting peer to the trusted one.
func (x *PeerToPeerTrust) SetTrust(v *Trust) {
if x != nil {
x.trust = v
}
}
// GlobalTrustBody represents reputation.GlobalTrust.Body message
// from NeoFS API v2.
type GlobalTrustBody struct {
@ -259,7 +299,7 @@ func (x *SendLocalTrustResponse) SetBody(v *SendLocalTrustResponseBody) {
type SendIntermediateResultRequestBody struct {
iter uint32
trust *Trust
trust *PeerToPeerTrust
}
// GetIteration returns sequence number of the iteration.
@ -279,7 +319,7 @@ func (x *SendIntermediateResultRequestBody) SetIteration(v uint32) {
}
// GetTrust returns current global trust value.
func (x *SendIntermediateResultRequestBody) GetTrust() *Trust {
func (x *SendIntermediateResultRequestBody) GetTrust() *PeerToPeerTrust {
if x != nil {
return x.trust
}
@ -288,7 +328,7 @@ func (x *SendIntermediateResultRequestBody) GetTrust() *Trust {
}
// SetTrust sets current global trust value.
func (x *SendIntermediateResultRequestBody) SetTrust(v *Trust) {
func (x *SendIntermediateResultRequestBody) SetTrust(v *PeerToPeerTrust) {
if x != nil {
x.trust = v
}