diff --git a/v2/reputation/convert.go b/v2/reputation/convert.go index 19be9f03..52df2fa1 100644 --- a/v2/reputation/convert.go +++ b/v2/reputation/convert.go @@ -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 diff --git a/v2/reputation/grpc/service.go b/v2/reputation/grpc/service.go index bf8626e6..2104d94c 100644 --- a/v2/reputation/grpc/service.go +++ b/v2/reputation/grpc/service.go @@ -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 } diff --git a/v2/reputation/grpc/service.pb.go b/v2/reputation/grpc/service.pb.go index fea43eaf..01161891 100644 Binary files a/v2/reputation/grpc/service.pb.go and b/v2/reputation/grpc/service.pb.go differ diff --git a/v2/reputation/grpc/types.go b/v2/reputation/grpc/types.go index 0a469bd8..07d1d8b4 100644 --- a/v2/reputation/grpc/types.go +++ b/v2/reputation/grpc/types.go @@ -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 { diff --git a/v2/reputation/grpc/types.pb.go b/v2/reputation/grpc/types.pb.go index c2adca51..7c4ec047 100644 Binary files a/v2/reputation/grpc/types.pb.go and b/v2/reputation/grpc/types.pb.go differ diff --git a/v2/reputation/json.go b/v2/reputation/json.go index 637035a1..5ee1cc29 100644 --- a/v2/reputation/json.go +++ b/v2/reputation/json.go @@ -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) } diff --git a/v2/reputation/marshal.go b/v2/reputation/marshal.go index 76b5e9d3..20fa6776 100644 --- a/v2/reputation/marshal.go +++ b/v2/reputation/marshal.go @@ -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 diff --git a/v2/reputation/message_test.go b/v2/reputation/message_test.go index ecbec7b5..2d5899a1 100644 --- a/v2/reputation/message_test.go +++ b/v2/reputation/message_test.go @@ -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) }, ) } diff --git a/v2/reputation/test/generate.go b/v2/reputation/test/generate.go index 4cb525f2..65e50e86 100644 --- a/v2/reputation/test/generate.go +++ b/v2/reputation/test/generate.go @@ -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 } diff --git a/v2/reputation/types.go b/v2/reputation/types.go index 3b67c139..b7ef5453 100644 --- a/v2/reputation/types.go +++ b/v2/reputation/types.go @@ -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 }