From f6268339d033fd21e74ccba3b59f716031a3cdae Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 17 Feb 2021 20:55:02 +0300 Subject: [PATCH] [#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 --- pkg/netmap/network_info.go | 80 +++++++++++++++++++++++++++++++++ pkg/netmap/network_info_test.go | 53 ++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 pkg/netmap/network_info.go create mode 100644 pkg/netmap/network_info_test.go diff --git a/pkg/netmap/network_info.go b/pkg/netmap/network_info.go new file mode 100644 index 0000000..ba3f204 --- /dev/null +++ b/pkg/netmap/network_info.go @@ -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) +} diff --git a/pkg/netmap/network_info_test.go b/pkg/netmap/network_info_test.go new file mode 100644 index 0000000..bb8643f --- /dev/null +++ b/pkg/netmap/network_info_test.go @@ -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) + }) +}