[#259] netmap: Implement binary encoding for NetworkInfo RPC messages

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-17 20:28:46 +03:00 committed by Alex Vanin
parent c3be9dc270
commit 276d863fd5
2 changed files with 130 additions and 0 deletions

View file

@ -500,3 +500,100 @@ func (l *LocalNodeInfoResponseBody) StableSize() (size int) {
return size return size
} }
const (
_ = iota
netInfoCurEpochFNum
netInfoMagicNumFNum
)
func (i *NetworkInfo) StableMarshal(buf []byte) ([]byte, error) {
if i == nil {
return []byte{}, nil
}
if buf == nil {
buf = make([]byte, i.StableSize())
}
var (
offset, n int
err error
)
n, err = protoutil.UInt64Marshal(netInfoCurEpochFNum, buf[offset:], i.curEpoch)
if err != nil {
return nil, err
}
offset += n
_, err = protoutil.UInt64Marshal(netInfoMagicNumFNum, buf[offset:], i.magicNum)
if err != nil {
return nil, err
}
return buf, nil
}
func (i *NetworkInfo) StableSize() (size int) {
if i == nil {
return 0
}
size += protoutil.UInt64Size(netInfoCurEpochFNum, i.curEpoch)
size += protoutil.UInt64Size(netInfoMagicNumFNum, i.magicNum)
return size
}
func (i *NetworkInfo) Unmarshal(data []byte) error {
m := new(netmap.NetworkInfo)
if err := proto.Unmarshal(data, m); err != nil {
return err
}
*i = *NetworkInfoFromGRPCMessage(m)
return nil
}
func (l *NetworkInfoRequestBody) StableMarshal(buf []byte) ([]byte, error) {
return nil, nil
}
func (l *NetworkInfoRequestBody) StableSize() (size int) {
return 0
}
const (
_ = iota
netInfoRespBodyNetInfoFNum
)
func (i *NetworkInfoResponseBody) StableMarshal(buf []byte) ([]byte, error) {
if i == nil {
return []byte{}, nil
}
if buf == nil {
buf = make([]byte, i.StableSize())
}
_, err := protoutil.NestedStructureMarshal(netInfoRespBodyNetInfoFNum, buf, i.netInfo)
if err != nil {
return nil, err
}
return buf, nil
}
func (i *NetworkInfoResponseBody) StableSize() (size int) {
if i == nil {
return 0
}
size += protoutil.NestedStructureSize(netInfoRespBodyNetInfoFNum, i.netInfo)
return size
}

View file

@ -111,6 +111,22 @@ func TestLocalNodeInfoResponseBody_StableMarshal(t *testing.T) {
}) })
} }
func TestNetworkInfoResponseBody_StableMarshal(t *testing.T) {
from := generateNetworkInfoResponseBody()
transport := new(grpc.NetworkInfoResponse_Body)
t.Run("non empty", func(t *testing.T) {
wire, err := from.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
to := netmap.NetworkInfoResponseBodyFromGRPCMessage(transport)
require.Equal(t, from, to)
})
}
func generateAttribute(k, v string) *netmap.Attribute { func generateAttribute(k, v string) *netmap.Attribute {
attr := new(netmap.Attribute) attr := new(netmap.Attribute)
attr.SetKey(k) attr.SetKey(k)
@ -213,3 +229,20 @@ func generateVersion(maj, min uint32) *refs.Version {
return version return version
} }
func generateNetworkInfo() *netmap.NetworkInfo {
ni := new(netmap.NetworkInfo)
ni.SetCurrentEpoch(13)
ni.SetMagicNumber(666)
return ni
}
func generateNetworkInfoResponseBody() *netmap.NetworkInfoResponseBody {
ni := generateNetworkInfo()
r := new(netmap.NetworkInfoResponseBody)
r.SetNetworkInfo(ni)
return r
}