[#259] pkg/netmap: Implement v2-compatible NetworkInfo type

Define NetworkInfo structure. Implement constructors, fields getters and
setters, binary and JSON encoding methods.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-17 20:55:02 +03:00 committed by Alex Vanin
parent 88d6857a3c
commit f6268339d0
2 changed files with 133 additions and 0 deletions

View file

@ -0,0 +1,80 @@
package netmap
import (
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)
// NetworkInfo represents v2-compatible structure
// with information about NeoFS network.
type NetworkInfo netmap.NetworkInfo
// NewNetworkInfoFromV2 wraps v2 NetworkInfo message to NetworkInfo.
func NewNetworkInfoFromV2(iV2 *netmap.NetworkInfo) *NetworkInfo {
return (*NetworkInfo)(iV2)
}
// NewNetworkInfo creates and initializes blank NetworkInfo.
func NewNetworkInfo() *NetworkInfo {
return NewNetworkInfoFromV2(new(netmap.NetworkInfo))
}
// ToV2 converts NetworkInfo to v2 NetworkInfo.
func (i *NetworkInfo) ToV2() *netmap.NetworkInfo {
return (*netmap.NetworkInfo)(i)
}
// CurrentEpoch returns current epoch of the NeoFS network.
func (i *NetworkInfo) CurrentEpoch() uint64 {
return (*netmap.NetworkInfo)(i).
GetCurrentEpoch()
}
// SetCurrentEpoch sets current epoch of the NeoFS network.
func (i *NetworkInfo) SetCurrentEpoch(epoch uint64) {
(*netmap.NetworkInfo)(i).
SetCurrentEpoch(epoch)
}
// MagicNumber returns magic number of the sidechain.
func (i *NetworkInfo) MagicNumber() uint64 {
return (*netmap.NetworkInfo)(i).
GetMagicNumber()
}
// SetMagicNumber sets magic number of the sidechain.
func (i *NetworkInfo) SetMagicNumber(epoch uint64) {
(*netmap.NetworkInfo)(i).
SetMagicNumber(epoch)
}
// Marshal marshals NetworkInfo into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (i *NetworkInfo) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*netmap.NetworkInfo)(i).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of NetworkInfo.
func (i *NetworkInfo) Unmarshal(data []byte) error {
return (*netmap.NetworkInfo)(i).
Unmarshal(data)
}
// MarshalJSON encodes NetworkInfo to protobuf JSON format.
func (i *NetworkInfo) MarshalJSON() ([]byte, error) {
return (*netmap.NetworkInfo)(i).
MarshalJSON()
}
// UnmarshalJSON decodes NetworkInfo from protobuf JSON format.
func (i *NetworkInfo) UnmarshalJSON(data []byte) error {
return (*netmap.NetworkInfo)(i).
UnmarshalJSON(data)
}

View file

@ -0,0 +1,53 @@
package netmap
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestNetworkInfo_CurrentEpoch(t *testing.T) {
i := NewNetworkInfo()
e := uint64(13)
i.SetCurrentEpoch(e)
require.Equal(t, e, i.CurrentEpoch())
require.Equal(t, e, i.ToV2().GetCurrentEpoch())
}
func TestNetworkInfo_MagicNumber(t *testing.T) {
i := NewNetworkInfo()
m := uint64(666)
i.SetMagicNumber(m)
require.Equal(t, m, i.MagicNumber())
require.Equal(t, m, i.ToV2().GetMagicNumber())
}
func TestNetworkInfoEncoding(t *testing.T) {
i := NewNetworkInfo()
i.SetCurrentEpoch(13)
i.SetMagicNumber(666)
t.Run("binary", func(t *testing.T) {
data, err := i.Marshal()
require.NoError(t, err)
i2 := NewNetworkInfo()
require.NoError(t, i2.Unmarshal(data))
require.Equal(t, i, i2)
})
t.Run("json", func(t *testing.T) {
data, err := i.MarshalJSON()
require.NoError(t, err)
i2 := NewNetworkInfo()
require.NoError(t, i2.UnmarshalJSON(data))
require.Equal(t, i, i2)
})
}