[#378] netmap: Add binary encoding for NetworkInfo type

Define `Marshal` / `Unmarshal` methods of the `NetworkInfo` type.

Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
This commit is contained in:
Leonard Lyubich 2023-01-31 12:15:29 +04:00
parent 4191e5f13e
commit 8fdbf6950a
2 changed files with 37 additions and 0 deletions

View file

@ -112,6 +112,33 @@ func (x NetworkInfo) WriteToV2(m *netmap.NetworkInfo) {
*m = x.m
}
// Marshal encodes NetworkInfo into a binary format of the NeoFS API protocol
// (Protocol Buffers with direct field order).
//
// See also Unmarshal.
func (x NetworkInfo) Marshal() []byte {
var m netmap.NetworkInfo
x.WriteToV2(&m)
return m.StableMarshal(nil)
}
// Unmarshal decodes NeoFS API protocol binary format into the NetworkInfo
// (Protocol Buffers with direct field order). Returns an error describing
// a format violation.
//
// See also Marshal.
func (x *NetworkInfo) Unmarshal(data []byte) error {
var m netmap.NetworkInfo
err := m.Unmarshal(data)
if err != nil {
return err
}
return x.readFromV2(m, false)
}
// CurrentEpoch returns epoch set using SetCurrentEpoch.
//
// Zero NetworkInfo has zero current epoch.

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
. "github.com/nspcc-dev/neofs-sdk-go/netmap"
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
"github.com/stretchr/testify/require"
)
@ -251,3 +252,12 @@ func TestNetworkInfo_MaintenanceModeAllowed(t *testing.T) {
},
)
}
func TestNetworkInfo_Marshal(t *testing.T) {
v := netmaptest.NetworkInfo()
var v2 NetworkInfo
require.NoError(t, v2.Unmarshal(v.Marshal()))
require.Equal(t, v, v2)
}