From 69826ebd905f999852b412425e0f3cdc1c207d30 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 2 Jul 2021 08:41:45 +0300 Subject: [PATCH] [#660] core: Implement function to check protocol version adequacy Create `version` package and implement `IsValid` function which checks if version is not earlier than the genesis one. Signed-off-by: Leonard Lyubich --- pkg/core/version/version.go | 17 +++++++++++++++++ pkg/core/version/version_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 pkg/core/version/version.go create mode 100644 pkg/core/version/version_test.go diff --git a/pkg/core/version/version.go b/pkg/core/version/version.go new file mode 100644 index 00000000..8371ab57 --- /dev/null +++ b/pkg/core/version/version.go @@ -0,0 +1,17 @@ +package version + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg" +) + +// IsValid checks if Version is not earlier than the genesis version of the NeoFS. +func IsValid(v pkg.Version) bool { + const ( + startMajor = 2 + startMinor = 7 + ) + + mjr := v.Major() + + return mjr > startMajor || mjr == startMajor && v.Minor() >= startMinor +} diff --git a/pkg/core/version/version_test.go b/pkg/core/version/version_test.go new file mode 100644 index 00000000..572448ac --- /dev/null +++ b/pkg/core/version/version_test.go @@ -0,0 +1,30 @@ +package version_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-node/pkg/core/version" + "github.com/stretchr/testify/require" +) + +func TestIsValid(t *testing.T) { + require.True(t, version.IsValid(*pkg.SDKVersion())) + + var v pkg.Version + + for _, item := range []struct { + mjr, mnr uint32 + valid bool + }{ + {mjr: 0, mnr: 0, valid: false}, + {mjr: 2, mnr: 6, valid: false}, + {mjr: 2, mnr: 7, valid: true}, + {mjr: 3, mnr: 0, valid: true}, + } { + v.SetMajor(item.mjr) + v.SetMinor(item.mnr) + + require.Equal(t, item.valid, version.IsValid(v), item) + } +}