Skip to content

Commit

Permalink
fix: treat prereleases as different versions (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler authored Aug 8, 2023
1 parent 964020e commit a16afab
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
16 changes: 15 additions & 1 deletion common/protocol/src/methods/checks/isValidVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Validator, standardizeJSON } from "../..";
import { valid, major, minor } from "semver";
import { valid, major, minor, prerelease } from "semver";

/**
* isValidVersion checks if the major and minor version of the pool matches
Expand Down Expand Up @@ -61,6 +61,20 @@ export function isValidVersion(this: Validator): boolean {
return false;
}

// exit if prerelease version does not match
if (
JSON.stringify(prerelease(remoteVersion)) !==
JSON.stringify(prerelease(localVersion))
) {
this.logger.fatal(`Running an invalid version. Exiting ...`);
this.logger.fatal(
`Found Runtime version = ${this.runtime.version} required = ${
this.pool.data!.protocol!.version
}`
);
return false;
}

// patch version can be different, continue in this case
this.logger.info(
`Validator running on valid runtime version = ${this.runtime.version}`
Expand Down
6 changes: 4 additions & 2 deletions common/protocol/src/methods/setups/setupValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
colors,
uniqueNamesGenerator,
} from "unique-names-generator";
import { major, minor } from "semver";
import { major, minor, prerelease } from "semver";

import { Validator, standardizeError } from "../..";

Expand All @@ -32,7 +32,9 @@ export async function setupValidator(this: Validator): Promise<void> {
// runtime, runtime version and valaddress
const valnameSeed = `${this.chainId}-${this.poolId}-${
this.runtime.name
}-${major(this.runtime.version)}-${minor(this.runtime.version)}-${
}-${major(this.runtime.version)}-${minor(
this.runtime.version
)}-${JSON.stringify(prerelease(this.runtime.version))}-${
this.client[0].account.address
}`;

Expand Down
70 changes: 69 additions & 1 deletion common/protocol/test/checks/is_valid_version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import { genesis_pool } from "../mocks/constants";
TEST CASES - isValidVersion
* assert equal remote and local version
* assert equal remote and local version with prerelease
* assert remote and local with higher patch version
* assert remote and local with lower patch version
* assert remote and local with higher minor version
* assert remote and local with lower minor version
* assert remote and local with higher major version
* assert remote and local with lower major version
* assert remote and local with higher prerelease version
* assert remote and local with lower prerelease version
* assert remote and local with different major and minor version
* assert remote and local with different major, minor and patch version
* assert remote and local with different major, minor, patch and prerelease version
* assert unexpected error
*/
Expand Down Expand Up @@ -65,6 +69,22 @@ describe("isValidVersion", () => {
expect(result).toBeTruthy();
});

test("assert equal remote and local version with prerelease", async () => {
// ARRANGE

// remote
v.pool!.data!.protocol!.version = "1.0.0-alpha.3";

// local
v["runtime"].version = "1.0.0-alpha.3";

// ACT
const result = isValidVersion.call(v);

// ASSERT
expect(result).toBeTruthy();
});

test("assert remote and local with higher patch version", async () => {
// ARRANGE

Expand Down Expand Up @@ -145,7 +165,7 @@ describe("isValidVersion", () => {
expect(result).toBeFalsy();
});

test("assert remote and local with higher major version", async () => {
test("assert remote and local with lower major version", async () => {
// ARRANGE

// remote
Expand All @@ -161,6 +181,38 @@ describe("isValidVersion", () => {
expect(result).toBeFalsy();
});

test("assert remote and local with higher prerelease version", async () => {
// ARRANGE

// remote
v.pool!.data!.protocol!.version = "1.0.0-alpha.5";

// local
v["runtime"].version = "1.0.0-alpha.8";

// ACT
const result = isValidVersion.call(v);

// ASSERT
expect(result).toBeFalsy();
});

test("assert remote and local with lower prerelease version", async () => {
// ARRANGE

// remote
v.pool!.data!.protocol!.version = "1.0.0-beta.5";

// local
v["runtime"].version = "1.0.0-beta.0";

// ACT
const result = isValidVersion.call(v);

// ASSERT
expect(result).toBeFalsy();
});

test("assert remote and local with different major and minor version", async () => {
// ARRANGE

Expand Down Expand Up @@ -193,6 +245,22 @@ describe("isValidVersion", () => {
expect(result).toBeFalsy();
});

test("assert remote and local with different major, minor, patch and prerelease version", async () => {
// ARRANGE

// remote
v.pool!.data!.protocol!.version = "1.0.0-alpha.3";

// local
v["runtime"].version = "2.1.1-alpha.5";

// ACT
const result = isValidVersion.call(v);

// ASSERT
expect(result).toBeFalsy();
});

test("assert unexpected error", async () => {
// ARRANGE

Expand Down

0 comments on commit a16afab

Please sign in to comment.