Skip to content

Commit

Permalink
Merge pull request #4357 from oasisprotocol/matevz/feature/runtime_ve…
Browse files Browse the repository at this point in the history
…rsion

test-runner: Read runtime version from fixture
  • Loading branch information
matevz authored Nov 12, 2021
2 parents a3491a8 + f668a0f commit 28fb7a6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
Empty file added .changelog/4343.trivial.md
Empty file.
63 changes: 39 additions & 24 deletions go/common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (v Version) ToU64() uint64 {
// FromU64 returns the version from platform-dependent uint64.
func FromU64(v uint64) Version {
return Version{
Major: uint16((v >> 32) & 0xff),
Minor: uint16((v >> 16) & 0xff),
Patch: uint16(v & 0xff),
Major: uint16((v >> 32) & 0xffff),
Minor: uint16((v >> 16) & 0xffff),
Patch: uint16(v & 0xffff),
}
}

Expand All @@ -58,6 +58,41 @@ func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}

// FromString parses version in semver format. e.g. "1.0.0"
func FromString(s string) (Version, error) {
// Trim potential pre-release suffix.
s = strings.Split(s, "-")[0]
// Trim potential git commit.
s = strings.Split(s, "+")[0]
split := strings.SplitN(s, ".", 4)
if len(split) != 3 {
return Version{}, fmt.Errorf("version: failed to parse SemVer '%s': exactly three components are required", s)
}

var semVers []uint16 = []uint16{0, 0, 0}
for i, v := range split {
if i >= 3 {
break
}
ver, err := strconv.ParseUint(v, 10, 16)
if err != nil {
return Version{}, fmt.Errorf("version: failed to parse SemVer '%s': %w", s, err)
}
semVers[i] = uint16(ver)
}

return Version{Major: semVers[0], Minor: semVers[1], Patch: semVers[2]}, nil
}

// MustFromString parses version in semver format and panics, if there is an error.
func MustFromString(s string) Version {
ver, err := FromString(s)
if err != nil {
panic(err)
}
return ver
}

// MaskNonMajor masks all non-major version segments to 0 and returns a new
// protocol version.
//
Expand Down Expand Up @@ -111,7 +146,7 @@ var (
TendermintAppVersion = ConsensusProtocol.MaskNonMajor().ToU64()

// Toolchain is the version of the Go compiler/standard library.
Toolchain = parseSemVerStr(strings.TrimPrefix(runtime.Version(), "go"))
Toolchain = MustFromString(strings.TrimPrefix(runtime.Version(), "go"))
)

// ProtocolVersions are the protocol versions.
Expand Down Expand Up @@ -178,26 +213,6 @@ var Versions = ProtocolVersions{
RuntimeCommitteeProtocol,
}

func parseSemVerStr(s string) Version {
// Trim potential pre-release suffix.
s = strings.Split(s, "-")[0]
split := strings.SplitN(s, ".", 4)

var semVers []uint16 = []uint16{0, 0, 0}
for i, v := range split {
if i >= 3 {
break
}
ver, err := strconv.ParseUint(v, 10, 16)
if err != nil {
panic(fmt.Errorf("version: failed to parse SemVer '%s': %w", s, err))
}
semVers[i] = uint16(ver)
}

return Version{Major: semVers[0], Minor: semVers[1], Patch: semVers[2]}
}

var goModulesVersionRegex = regexp.MustCompile(`v0.(?P<year>[0-9]{2})(?P<minor>[0-9]{2}).(?P<micro>[0-9]+)`)

// Convert Go Modules compatible version to Oasis Core's canonical version.
Expand Down
34 changes: 27 additions & 7 deletions go/common/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,38 @@ func TestMaskNonMajor(t *testing.T) {
require.NotEqual(v1.MaskNonMajor(), v4.MaskNonMajor(), "version.MaskNonMajor() should not match")
}

func TestParseSemVer(t *testing.T) {
func TestFromString(t *testing.T) {
require := require.New(t)

for _, v := range []struct {
semver string
expected Version
}{
{"1.0.0", Version{1, 0, 0}},
{"2.1.3", Version{2, 1, 3}},
{"1.0.0-alpha", Version{1, 0, 0}},
{"1.0.0-alpha+1.2", Version{1, 0, 0}},
{"1.8.2-beta.1.13", Version{1, 8, 2}},
{"0.0.1", Version{0, 0, 1}},
{"0.1.2", Version{0, 1, 2}},
{"1.2.3", Version{1, 2, 3}},
{"1.2.3-alpha", Version{1, 2, 3}},
{"1.2.3-alpha+git0253df22", Version{1, 2, 3}},
{"1.2.3+git0253df22", Version{1, 2, 3}},
{"1.2.3-beta.1", Version{1, 2, 3}},
{"300.400.500", Version{300, 400, 500}},
{"30000.40000.50000", Version{30000, 40000, 50000}},
} {
require.Equal(parseSemVerStr(v.semver), v.expected, "parseSemVerStr()")
version, err := FromString(v.semver)
require.NoError(err)
require.Equal(v.expected, version, "FromString()")
}

// Invalid versions.
for _, v := range []string{
"",
"100000.0.0", "0.100000.0", "0.0.100000",
"-1.0.0", "0.-1.0", "0.0.-1",
"1.0", "1",
"a.b.c",
} {
_, err := FromString(v)
require.Error(err, v)
}
}

Expand All @@ -86,6 +104,8 @@ func TestFromToU64(t *testing.T) {
{0, 0, 0},
{1, 1, 1},
{10, 20, 30},
{300, 400, 500},
{30000, 40000, 50000},
} {
require.Equal(FromU64(v.ToU64()), v, "FromU64(version.ToU64())")
}
Expand Down
3 changes: 3 additions & 0 deletions go/oasis-test-runner/oasis/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/sgx"
"github.com/oasisprotocol/oasis-core/go/common/version"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/log"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
Expand Down Expand Up @@ -225,6 +226,7 @@ type RuntimeFixture struct { // nolint: maligned
Kind registry.RuntimeKind `json:"kind"`
Entity int `json:"entity"`
Keymanager int `json:"keymanager"`
Version version.Version `json:"version"`

Binaries map[node.TEEHardware][]string `json:"binaries"`
GenesisState storage.WriteLog `json:"genesis_state,omitempty"`
Expand Down Expand Up @@ -272,6 +274,7 @@ func (f *RuntimeFixture) Create(netFixture *NetworkFixture, net *Network) (*Runt
Keymanager: km,
TEEHardware: netFixture.TEE.Hardware,
MrSigner: netFixture.TEE.MrSigner,
Version: f.Version,
Executor: f.Executor,
TxnScheduler: f.TxnScheduler,
Storage: f.Storage,
Expand Down
3 changes: 3 additions & 0 deletions go/oasis-test-runner/oasis/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/sgx"
"github.com/oasisprotocol/oasis-core/go/common/version"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
Expand Down Expand Up @@ -52,6 +53,7 @@ type RuntimeCfg struct { // nolint: maligned
Keymanager *Runtime
TEEHardware node.TEEHardware
MrSigner *sgx.MrSigner
Version version.Version

Binaries map[node.TEEHardware][]string
GenesisState storage.WriteLog
Expand Down Expand Up @@ -152,6 +154,7 @@ func (net *Network) NewRuntime(cfg *RuntimeCfg) (*Runtime, error) {
EntityID: cfg.Entity.entity.ID,
Kind: cfg.Kind,
TEEHardware: cfg.TEEHardware,
Version: registry.VersionInfo{Version: cfg.Version},
Executor: cfg.Executor,
TxnScheduler: cfg.TxnScheduler,
Storage: cfg.Storage,
Expand Down

0 comments on commit 28fb7a6

Please sign in to comment.