From ccae5a40ba94fca7fba922f6fa8eb0b99aedebcd Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 1 Apr 2021 17:53:14 +0300 Subject: [PATCH] [#265] reputation: Implement converters and encoding methods on messages Signed-off-by: Leonard Lyubich --- v2/reputation/convert.go | 321 +++++++++++++++++++++++++++++++++- v2/reputation/json.go | 16 ++ v2/reputation/marshal.go | 174 +++++++++++++++++- v2/reputation/message_test.go | 10 ++ 4 files changed, 517 insertions(+), 4 deletions(-) diff --git a/v2/reputation/convert.go b/v2/reputation/convert.go index 51b37bc0..19be9f03 100644 --- a/v2/reputation/convert.go +++ b/v2/reputation/convert.go @@ -3,9 +3,40 @@ package reputation import ( "github.com/nspcc-dev/neofs-api-go/rpc/grpc" "github.com/nspcc-dev/neofs-api-go/rpc/message" + "github.com/nspcc-dev/neofs-api-go/v2/refs" + refsGRPC "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc" reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" ) +// ToGRPCMessage converts PeerID to gRPC-generated +// reputation.PeerID message. +func (x *PeerID) ToGRPCMessage() grpc.Message { + var m *reputation.PeerID + + if x != nil { + m = new(reputation.PeerID) + + m.SetValue(x.val) + } + + return m +} + +// FromGRPCMessage tries to restore PeerID from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.PeerID message. +func (x *PeerID) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.PeerID) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + x.val = v.GetValue() + + return nil +} + // ToGRPCMessage converts Trust to gRPC-generated // reputation.Trust message. func (x *Trust) ToGRPCMessage() grpc.Message { @@ -15,7 +46,7 @@ func (x *Trust) ToGRPCMessage() grpc.Message { m = new(reputation.Trust) m.SetValue(x.val) - m.SetPeer(x.peer) + m.SetPeer(x.peer.ToGRPCMessage().(*reputation.PeerID)) } return m @@ -31,8 +62,21 @@ func (x *Trust) FromGRPCMessage(m grpc.Message) error { return message.NewUnexpectedMessageType(m, v) } + peer := v.GetPeer() + if peer == nil { + x.peer = nil + } else { + if x.peer == nil { + x.peer = new(PeerID) + } + + err := x.peer.FromGRPCMessage(peer) + if err != nil { + return err + } + } + x.val = v.GetValue() - x.peer = v.GetPeer() return nil } @@ -76,6 +120,131 @@ func TrustsFromGRPC(xs []*reputation.Trust) (res []*Trust, err error) { return } +// ToGRPCMessage converts GlobalTrustBody to gRPC-generated +// reputation.GlobalTrust_Body message. +func (x *GlobalTrustBody) ToGRPCMessage() grpc.Message { + var m *reputation.GlobalTrust_Body + + if x != nil { + m = new(reputation.GlobalTrust_Body) + + m.SetManager(x.manager.ToGRPCMessage().(*reputation.PeerID)) + m.SetTrust(x.trust.ToGRPCMessage().(*reputation.Trust)) + } + + return m +} + +// FromGRPCMessage tries to restore GlobalTrustBody from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.GlobalTrust_Body message. +func (x *GlobalTrustBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.GlobalTrust_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + manager := v.GetManager() + if manager == nil { + x.manager = nil + } else { + if x.manager == nil { + x.manager = new(PeerID) + } + + err = x.manager.FromGRPCMessage(manager) + 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 +} + +// ToGRPCMessage converts GlobalTrust to gRPC-generated +// reputation.GlobalTrust message. +func (x *GlobalTrust) ToGRPCMessage() grpc.Message { + var m *reputation.GlobalTrust + + if x != nil { + m = new(reputation.GlobalTrust) + + m.SetVersion(x.version.ToGRPCMessage().(*refsGRPC.Version)) + m.SetBody(x.body.ToGRPCMessage().(*reputation.GlobalTrust_Body)) + m.SetSignature(x.sig.ToGRPCMessage().(*refsGRPC.Signature)) + } + + return m +} + +// FromGRPCMessage tries to restore GlobalTrust from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.GlobalTrust message. +func (x *GlobalTrust) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.GlobalTrust) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + version := v.GetVersion() + if version == nil { + x.version = nil + } else { + if x.version == nil { + x.version = new(refs.Version) + } + + err = x.version.FromGRPCMessage(version) + if err != nil { + return err + } + } + + body := v.GetBody() + if body == nil { + x.body = nil + } else { + if x.body == nil { + x.body = new(GlobalTrustBody) + } + + err = x.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + sig := v.GetSignature() + if sig == nil { + x.sig = nil + } else { + if x.sig == nil { + x.sig = new(refs.Signature) + } + + err = x.sig.FromGRPCMessage(sig) + } + + return err +} + // ToGRPCMessage converts SendLocalTrustRequestBody to gRPC-generated // reputation.SendLocalTrustRequest_Body message. func (x *SendLocalTrustRequestBody) ToGRPCMessage() grpc.Message { @@ -225,3 +394,151 @@ func (x *SendLocalTrustResponse) FromGRPCMessage(m grpc.Message) error { return x.ResponseHeaders.FromMessage(v) } + +// ToGRPCMessage converts SendIntermediateResultRequestBody to gRPC-generated +// reputation.SendIntermediateResultRequest_Body message. +func (x *SendIntermediateResultRequestBody) ToGRPCMessage() grpc.Message { + var m *reputation.SendIntermediateResultRequest_Body + + if x != nil { + m = new(reputation.SendIntermediateResultRequest_Body) + + m.SetIteration(x.iter) + m.SetTrust(x.trust.ToGRPCMessage().(*reputation.Trust)) + } + + return m +} + +// FromGRPCMessage tries to restore SendIntermediateResultRequestBody from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendIntermediateResultRequest_Body message. +func (x *SendIntermediateResultRequestBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendIntermediateResultRequest_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + err := x.trust.FromGRPCMessage(v.GetTrust()) + if err != nil { + return err + } + + x.iter = v.GetIteration() + + return nil +} + +// ToGRPCMessage converts SendIntermediateResultRequest to gRPC-generated +// reputation.SendIntermediateResultRequest message. +func (x *SendIntermediateResultRequest) ToGRPCMessage() grpc.Message { + var m *reputation.SendIntermediateResultRequest + + if x != nil { + m = new(reputation.SendIntermediateResultRequest) + + m.SetBody(x.body.ToGRPCMessage().(*reputation.SendIntermediateResultRequest_Body)) + x.RequestHeaders.ToMessage(m) + } + + return m +} + +// FromGRPCMessage tries to restore SendIntermediateResultRequest from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendIntermediateResultRequest message. +func (x *SendIntermediateResultRequest) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendIntermediateResultRequest) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + body := v.GetBody() + if body == nil { + x.body = nil + } else { + if x.body == nil { + x.body = new(SendIntermediateResultRequestBody) + } + + err = x.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + return x.RequestHeaders.FromMessage(v) +} + +// ToGRPCMessage converts SendIntermediateResultResponseBody to gRPC-generated +// reputation.SendIntermediateResultResponse_Body message. +func (x *SendIntermediateResultResponseBody) ToGRPCMessage() grpc.Message { + var m *reputation.SendIntermediateResultResponse_Body + + if x != nil { + m = new(reputation.SendIntermediateResultResponse_Body) + } + + return m +} + +// FromGRPCMessage tries to restore SendIntermediateResultResponseBody from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendIntermediateResultResponse_Body message. +func (x *SendIntermediateResultResponseBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendIntermediateResultResponse_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + return nil +} + +// ToGRPCMessage converts SendIntermediateResultResponse to gRPC-generated +// reputation.SendIntermediateResultResponse message. +func (x *SendIntermediateResultResponse) ToGRPCMessage() grpc.Message { + var m *reputation.SendIntermediateResultResponse + + if x != nil { + m = new(reputation.SendIntermediateResultResponse) + + m.SetBody(x.body.ToGRPCMessage().(*reputation.SendIntermediateResultResponse_Body)) + x.ResponseHeaders.ToMessage(m) + } + + return m +} + +// FromGRPCMessage tries to restore SendIntermediateResultResponse from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendIntermediateResultResponse message. +func (x *SendIntermediateResultResponse) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendIntermediateResultResponse) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + body := v.GetBody() + if body == nil { + x.body = nil + } else { + if x.body == nil { + x.body = new(SendIntermediateResultResponseBody) + } + + err = x.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + return x.ResponseHeaders.FromMessage(v) +} diff --git a/v2/reputation/json.go b/v2/reputation/json.go index 202bae20..637035a1 100644 --- a/v2/reputation/json.go +++ b/v2/reputation/json.go @@ -5,6 +5,14 @@ import ( reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" ) +func (x *PeerID) MarshalJSON() ([]byte, error) { + return message.MarshalJSON(x) +} + +func (x *PeerID) UnmarshalJSON(data []byte) error { + return message.UnmarshalJSON(x, data, new(reputation.PeerID)) +} + func (x *Trust) MarshalJSON() ([]byte, error) { return message.MarshalJSON(x) } @@ -12,3 +20,11 @@ func (x *Trust) MarshalJSON() ([]byte, error) { func (x *Trust) UnmarshalJSON(data []byte) error { return message.UnmarshalJSON(x, data, new(reputation.Trust)) } + +func (x *GlobalTrust) MarshalJSON() ([]byte, error) { + return message.MarshalJSON(x) +} + +func (x *GlobalTrust) UnmarshalJSON(data []byte) error { + return message.UnmarshalJSON(x, data, new(reputation.GlobalTrust)) +} diff --git a/v2/reputation/marshal.go b/v2/reputation/marshal.go index d627f813..76b5e9d3 100644 --- a/v2/reputation/marshal.go +++ b/v2/reputation/marshal.go @@ -6,6 +6,38 @@ import ( reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" ) +const ( + _ = iota + peerIDValFNum +) + +func (x *PeerID) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + _, err := protoutil.BytesMarshal(peerIDValFNum, buf, x.val) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (x *PeerID) StableSize() (size int) { + size += protoutil.BytesSize(peerIDValFNum, x.val) + + return +} + +func (x *PeerID) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.PeerID)) +} + const ( _ = iota trustPeerFNum @@ -26,7 +58,7 @@ func (x *Trust) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = protoutil.BytesMarshal(trustPeerFNum, buf[offset:], x.peer) + n, err = protoutil.NestedStructureMarshal(trustPeerFNum, buf[offset:], x.peer) if err != nil { return nil, err } @@ -42,7 +74,7 @@ func (x *Trust) StableMarshal(buf []byte) ([]byte, error) { } func (x *Trust) StableSize() (size int) { - size += protoutil.BytesSize(trustPeerFNum, x.peer) + size += protoutil.NestedStructureSize(trustPeerFNum, x.peer) size += protoutil.Float64Size(trustValueFNum, x.val) return @@ -52,6 +84,93 @@ func (x *Trust) Unmarshal(data []byte) error { return message.Unmarshal(x, data, new(reputation.Trust)) } +const ( + _ = iota + globalTrustBodyManagerFNum + globalTrustBodyValueFNum +) + +func (x *GlobalTrustBody) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + offset, err := protoutil.NestedStructureMarshal(globalTrustBodyManagerFNum, buf, x.manager) + if err != nil { + return nil, err + } + + _, err = protoutil.NestedStructureMarshal(globalTrustBodyValueFNum, buf[offset:], x.trust) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (x *GlobalTrustBody) StableSize() (size int) { + size += protoutil.NestedStructureSize(globalTrustBodyManagerFNum, x.manager) + size += protoutil.NestedStructureSize(globalTrustBodyValueFNum, x.trust) + + return +} + +func (x *GlobalTrustBody) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.GlobalTrust_Body)) +} + +const ( + _ = iota + globalTrustVersionFNum + globalTrustBodyFNum + globalTrustSigFNum +) + +func (x *GlobalTrust) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + offset, err := protoutil.NestedStructureMarshal(globalTrustVersionFNum, buf, x.version) + if err != nil { + return nil, err + } + + n, err := protoutil.NestedStructureMarshal(globalTrustBodyFNum, buf[offset:], x.body) + if err != nil { + return nil, err + } + + offset += n + + _, err = protoutil.NestedStructureMarshal(globalTrustSigFNum, buf[offset:], x.sig) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (x *GlobalTrust) StableSize() (size int) { + size += protoutil.NestedStructureSize(globalTrustVersionFNum, x.version) + size += protoutil.NestedStructureSize(globalTrustBodyFNum, x.body) + size += protoutil.NestedStructureSize(globalTrustSigFNum, x.sig) + + return +} + +func (x *GlobalTrust) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.GlobalTrust)) +} + const ( _ = iota sendLocalTrustBodyEpochFNum @@ -116,3 +235,54 @@ func (x *SendLocalTrustResponseBody) StableSize() int { func (x *SendLocalTrustResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(x, data, new(reputation.SendLocalTrustResponse_Body)) } + +const ( + _ = iota + sendInterResBodyIterFNum + sendInterResBodyTrustFNum +) + +func (x *SendIntermediateResultRequestBody) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + offset, err := protoutil.UInt32Marshal(sendInterResBodyIterFNum, buf, x.iter) + if err != nil { + return nil, err + } + + _, err = protoutil.NestedStructureMarshal(sendInterResBodyTrustFNum, buf[offset:], x.trust) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (x *SendIntermediateResultRequestBody) StableSize() (size int) { + size += protoutil.UInt32Size(sendInterResBodyIterFNum, x.iter) + size += protoutil.NestedStructureSize(sendInterResBodyTrustFNum, x.trust) + + return +} + +func (x *SendIntermediateResultRequestBody) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.SendIntermediateResultRequest_Body)) +} + +func (x *SendIntermediateResultResponseBody) StableMarshal(buf []byte) ([]byte, error) { + return buf, nil +} + +func (x *SendIntermediateResultResponseBody) StableSize() int { + return 0 +} + +func (x *SendIntermediateResultResponseBody) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.SendIntermediateResultResponse_Body)) +} diff --git a/v2/reputation/message_test.go b/v2/reputation/message_test.go index 07ca89f6..ecbec7b5 100644 --- a/v2/reputation/message_test.go +++ b/v2/reputation/message_test.go @@ -15,5 +15,15 @@ func TestMessageConvert(t *testing.T) { func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustRequest(empty) }, func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustResponseBody(empty) }, func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustResponse(empty) }, + func(empty bool) message.Message { + return reputationtest.GenerateSendIntermediateResultRequestBody(empty) + }, + func(empty bool) message.Message { return reputationtest.GenerateSendIntermediateResultRequest(empty) }, + func(empty bool) message.Message { + return reputationtest.GenerateSendIntermediateResultResponseBody(empty) + }, + 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) }, ) }