Skip to content

Commit

Permalink
[nspcc-dev#339] netmap: Support protocol changes related to NetworkInfo
Browse files Browse the repository at this point in the history
Recompile NeoFS API protobuf files. Implement `NetworkParameter` and
`NetworkConfig` types. Expand `NetworkInfo` type with MillisecondsPerBlock
and `NetworkConfig`.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich committed Sep 28, 2021
1 parent 427b8f1 commit 2d94eed
Show file tree
Hide file tree
Showing 12 changed files with 908 additions and 63 deletions.
139 changes: 138 additions & 1 deletion pkg/netmap/network_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func NewNetworkInfoFromV2(iV2 *netmap.NetworkInfo) *NetworkInfo {
//
// Defaults:
// - curEpoch: 0;
// - magicNum: 0.
// - magicNum: 0;
// - msPerBlock: 0;
// - network config: nil.
func NewNetworkInfo() *NetworkInfo {
return NewNetworkInfoFromV2(new(netmap.NetworkInfo))
}
Expand Down Expand Up @@ -55,6 +57,32 @@ func (i *NetworkInfo) SetMagicNumber(epoch uint64) {
SetMagicNumber(epoch)
}

// MsPerBlock returns MillisecondsPerBlock network parameter.
func (i *NetworkInfo) MsPerBlock() int64 {
return (*netmap.NetworkInfo)(i).
GetMsPerBlock()
}

// SetMsPerBlock sets MillisecondsPerBlock network parameter.
func (i *NetworkInfo) SetMsPerBlock(v int64) {
(*netmap.NetworkInfo)(i).
SetMsPerBlock(v)
}

// NetworkConfig returns NeoFS network configuration.
func (i *NetworkInfo) NetworkConfig() *NetworkConfig {
return NewNetworkConfigFromV2(
(*netmap.NetworkInfo)(i).
GetNetworkConfig(),
)
}

// SetNetworkConfig sets NeoFS network configuration.
func (i *NetworkInfo) SetNetworkConfig(v *NetworkConfig) {
(*netmap.NetworkInfo)(i).
SetNetworkConfig(v.ToV2())
}

// Marshal marshals NetworkInfo into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
Expand Down Expand Up @@ -86,3 +114,112 @@ func (i *NetworkInfo) UnmarshalJSON(data []byte) error {
return (*netmap.NetworkInfo)(i).
UnmarshalJSON(data)
}

// NetworkParameter represents v2-compatible NeoFS network parameter.
type NetworkParameter netmap.NetworkParameter

// NewNetworkParameterFromV2 wraps v2 NetworkParameter message to NetworkParameter.
//
// Nil netmap.NetworkParameter converts to nil.
func NewNetworkParameterFromV2(pv2 *netmap.NetworkParameter) *NetworkParameter {
return (*NetworkParameter)(pv2)
}

// NewNetworkParameter creates and initializes blank NetworkParameter.
//
// Defaults:
// - key: nil;
// - value: nil.
func NewNetworkParameter() *NetworkParameter {
return NewNetworkParameterFromV2(new(netmap.NetworkParameter))
}

// ToV2 converts NetworkParameter to v2 NetworkParameter.
//
// Nil NetworkParameter converts to nil.
func (x *NetworkParameter) ToV2() *netmap.NetworkParameter {
return (*netmap.NetworkParameter)(x)
}

// Key returns key to network parameter.
func (x *NetworkParameter) Key() []byte {
return (*netmap.NetworkParameter)(x).
GetKey()
}

// SetKey sets key to the network parameter.
func (x *NetworkParameter) SetKey(key []byte) {
(*netmap.NetworkParameter)(x).
SetKey(key)
}

// Value returns value of the network parameter.
func (x *NetworkParameter) Value() []byte {
return (*netmap.NetworkParameter)(x).
GetValue()
}

// SetValue sets value of the network parameter.
func (x *NetworkParameter) SetValue(val []byte) {
(*netmap.NetworkParameter)(x).
SetValue(val)
}

// NetworkConfig represents v2-compatible NeoFS network configuration.
type NetworkConfig netmap.NetworkConfig

// NewNetworkConfigFromV2 wraps v2 NetworkConfig message to NetworkConfig.
//
// Nil netmap.NetworkConfig converts to nil.
func NewNetworkConfigFromV2(cv2 *netmap.NetworkConfig) *NetworkConfig {
return (*NetworkConfig)(cv2)
}

// NewNetworkConfig creates and initializes blank NetworkConfig.
//
// Defaults:
// - parameters num: 0.
func NewNetworkConfig() *NetworkConfig {
return NewNetworkConfigFromV2(new(netmap.NetworkConfig))
}

// ToV2 converts NetworkConfig to v2 NetworkConfig.
//
// Nil NetworkConfig converts to nil.
func (x *NetworkConfig) ToV2() *netmap.NetworkConfig {
return (*netmap.NetworkConfig)(x)
}

// NumberOfParameters returns number of network parameters.
func (x *NetworkConfig) NumberOfParameters() int {
return (*netmap.NetworkConfig)(x).NumberOfParameters()
}

// IterateAddresses iterates over network parameters.
// Breaks iteration on f's true return.
//
// Handler should not be nil.
func (x *NetworkConfig) IterateParameters(f func(*NetworkParameter) bool) {
(*netmap.NetworkConfig)(x).
IterateParameters(func(p *netmap.NetworkParameter) bool {
return f(NewNetworkParameterFromV2(p))
})
}

// Value returns value of the network parameter.
func (x *NetworkConfig) SetParameters(ps ...*NetworkParameter) {
var psV2 []*netmap.NetworkParameter

if ps != nil {
ln := len(ps)

psV2 = make([]*netmap.NetworkParameter, 0, ln)

for i := 0; i < ln; i++ {
psV2 = append(psV2, ps[i].ToV2())
}
}

(*netmap.NetworkConfig)(x).
SetParameters(psV2...)
}
142 changes: 138 additions & 4 deletions pkg/netmap/network_info_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,124 @@
package netmap
package netmap_test

import (
"testing"

. "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmaptest "github.com/nspcc-dev/neofs-api-go/pkg/netmap/test"
"github.com/stretchr/testify/require"
)

func TestNetworkParameter_Key(t *testing.T) {
i := NewNetworkParameter()

k := []byte("key")

i.SetKey(k)

require.Equal(t, k, i.Key())
require.Equal(t, k, i.ToV2().GetKey())
}

func TestNetworkParameter_Value(t *testing.T) {
i := NewNetworkParameter()

v := []byte("value")

i.SetValue(v)

require.Equal(t, v, i.Value())
require.Equal(t, v, i.ToV2().GetValue())
}

func TestNewNetworkParameterFromV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
require.Nil(t, NewNetworkParameterFromV2(nil))
})
}

func TestNetworkParameter_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *NetworkParameter

require.Nil(t, x.ToV2())
})
}

func TestNewNetworkParameter(t *testing.T) {
x := NewNetworkParameter()

// check initial values
require.Nil(t, x.Key())
require.Nil(t, x.Value())

// convert to v2 message
xV2 := x.ToV2()

require.Nil(t, xV2.GetKey())
require.Nil(t, xV2.GetValue())
}

func TestNetworkConfig_SetParameters(t *testing.T) {
x := NewNetworkConfig()

require.Zero(t, x.NumberOfParameters())

called := 0

x.IterateParameters(func(p *NetworkParameter) bool {
called++
return false
})

require.Zero(t, called)

pps := []*NetworkParameter{
netmaptest.NetworkParameter(),
netmaptest.NetworkParameter(),
}

x.SetParameters(pps...)

require.EqualValues(t, len(pps), x.NumberOfParameters())

var dst []*NetworkParameter

x.IterateParameters(func(p *NetworkParameter) bool {
dst = append(dst, p)
called++
return false
})

require.Equal(t, pps, dst)
require.Equal(t, len(pps), called)
}

func TestNewNetworkConfigFromV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
require.Nil(t, NewNetworkConfigFromV2(nil))
})
}

func TestNetworkConfig_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {

var x *NetworkConfig
require.Nil(t, x.ToV2())
})
}

func TestNewNetworkConfig(t *testing.T) {
x := NewNetworkConfig()

// check initial values
require.Zero(t, x.NumberOfParameters())

// convert to v2 message
xV2 := x.ToV2()

require.Zero(t, xV2.NumberOfParameters())
}

func TestNetworkInfo_CurrentEpoch(t *testing.T) {
i := NewNetworkInfo()
e := uint64(13)
Expand All @@ -26,10 +139,29 @@ func TestNetworkInfo_MagicNumber(t *testing.T) {
require.Equal(t, m, i.ToV2().GetMagicNumber())
}

func TestNetworkInfoEncoding(t *testing.T) {
func TestNetworkInfo_MsPerBlock(t *testing.T) {
i := NewNetworkInfo()
i.SetCurrentEpoch(13)
i.SetMagicNumber(666)

const ms = 987

i.SetMsPerBlock(ms)

require.EqualValues(t, ms, i.MsPerBlock())
require.EqualValues(t, ms, i.ToV2().GetMsPerBlock())
}

func TestNetworkInfo_Config(t *testing.T) {
i := NewNetworkInfo()

c := netmaptest.NetworkConfig()

i.SetNetworkConfig(c)

require.Equal(t, c, i.NetworkConfig())
}

func TestNetworkInfoEncoding(t *testing.T) {
i := netmaptest.NetworkInfo()

t.Run("binary", func(t *testing.T) {
data, err := i.Marshal()
Expand Down Expand Up @@ -72,10 +204,12 @@ func TestNewNetworkInfo(t *testing.T) {
// check initial values
require.Zero(t, ni.CurrentEpoch())
require.Zero(t, ni.MagicNumber())
require.Zero(t, ni.MsPerBlock())

// convert to v2 message
niV2 := ni.ToV2()

require.Zero(t, niV2.GetCurrentEpoch())
require.Zero(t, niV2.GetMagicNumber())
require.Zero(t, niV2.GetMsPerBlock())
}
24 changes: 24 additions & 0 deletions pkg/netmap/test/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,36 @@ func PlacementPolicy() *netmap.PlacementPolicy {
return x
}

// NetworkParameter returns random netmap.NetworkParameter.
func NetworkParameter() *netmap.NetworkParameter {
x := netmap.NewNetworkParameter()

x.SetKey([]byte("key"))
x.SetValue([]byte("value"))

return x
}

// NetworkConfig returns random netmap.NetworkConfig.
func NetworkConfig() *netmap.NetworkConfig {
x := netmap.NewNetworkConfig()

x.SetParameters(
NetworkParameter(),
NetworkParameter(),
)

return x
}

// NetworkInfo returns random netmap.NetworkInfo.
func NetworkInfo() *netmap.NetworkInfo {
x := netmap.NewNetworkInfo()

x.SetCurrentEpoch(21)
x.SetMagicNumber(32)
x.SetMsPerBlock(43)
x.SetNetworkConfig(NetworkConfig())

return x
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/owner/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ func NEO3WalletFromPublicKey(key *ecdsa.PublicKey) (*NEO3Wallet, error) {
}

neoPublicKey := keys.PublicKey{
X: key.X,
Y: key.Y,
Curve: key.Curve,
X: key.X,
Y: key.Y,
}

d, err := base58.Decode(neoPublicKey.Address())
Expand Down
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Version represents v2-compatible version.
type Version refs.Version

const sdkMjr, sdkMnr = 2, 9
const sdkMjr, sdkMnr = 2, 10

// NewVersionFromV2 wraps v2 Version message to Version.
//
Expand Down
Loading

0 comments on commit 2d94eed

Please sign in to comment.