Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 087f1af

Browse files
will pankiewiczwill pankiewicz
authored andcommitted
fix endpoint + make window 48 hours
1 parent 7b8fe97 commit 087f1af

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

packages/common/src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const WEEK = 7 * 24 * 60 * 60 * 1000;
1212
/// The time a node has to make an upgrade to the latest release.
1313
export const SIXTEEN_HOURS = 16 * 60 * 60 * 1000;
1414

15+
export const TWENTY_FOUR_HOURS = 24 * 60 * 60 * 1000;
16+
17+
export const FORTY_EIGHT_HOURS = 48 * 60 * 60 * 1000;
18+
1519
export const GATEWAY_CACHE_TTL = 18 * 1000;
1620

1721
/// Number of Eras in 4 days that a validator should have claimed all previous rewards except

packages/common/src/constraints/ValidityChecks.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,20 @@ export const checkLatestClientVersion = async (
116116
);
117117
}
118118

119+
// Ensure latestRelease contains a valid name
120+
if (!latestRelease || !latestRelease.name) {
121+
logger.error(
122+
`Latest release name is null or undefined: ${latestRelease}`,
123+
constraintsLabel,
124+
);
125+
return false;
126+
}
127+
119128
// Check if there is a latest release and if the current time is past the grace window
120129
const isPastGraceWindow =
121-
latestRelease &&
122-
Date.now() > latestRelease.publishedAt + Constants.SIXTEEN_HOURS;
130+
Date.now() > latestRelease.publishedAt + Constants.FORTY_EIGHT_HOURS;
123131

124-
if (latestRelease && isPastGraceWindow) {
132+
if (isPastGraceWindow) {
125133
const nodeVersion = semver.coerce(candidate.version);
126134
const latestVersion = forceLatestRelease
127135
? semver.clean(forceLatestRelease)
@@ -140,7 +148,7 @@ export const checkLatestClientVersion = async (
140148

141149
const isUpgraded = semver.gte(nodeVersion, latestVersion);
142150

143-
// If they are not upgrade, set the validity as invalid
151+
// If they are not upgraded, set the validity as invalid
144152
if (!isUpgraded) {
145153
await setLatestClientReleaseValidity(candidate.stash, false);
146154
return false;
@@ -150,7 +158,7 @@ export const checkLatestClientVersion = async (
150158
await setLatestClientReleaseValidity(candidate.stash, true);
151159
return true;
152160
} else {
153-
// If there is no latest release or if not past the grace window, set the release as invalid
161+
// If not past the grace window, set the release as invalid
154162
await setLatestClientReleaseValidity(candidate.stash, false);
155163
return false;
156164
}
@@ -182,7 +190,6 @@ export const checkConnectionTime = async (
182190
await setConnectionTimeInvalidity(candidate.stash, true);
183191
return true;
184192
}
185-
return true;
186193
} catch (e) {
187194
logger.error(`Error checking connection time: ${e}`, constraintsLabel);
188195
return false;
@@ -254,7 +261,6 @@ export const checkCommission = async (
254261
await setCommissionInvalidity(candidate.stash, true);
255262
return true;
256263
}
257-
return true;
258264
} catch (e) {
259265
logger.error(`Error checking commission: ${e}`, constraintsLabel);
260266
return false;
@@ -317,7 +323,6 @@ export const checkUnclaimed = async (
317323
await setUnclaimedInvalidity(candidate.stash, true);
318324
return true;
319325
}
320-
return true;
321326
} catch (e) {
322327
logger.error(`Error checking unclaimed: ${e}`, constraintsLabel);
323328
return false;
@@ -339,7 +344,6 @@ export const checkBlocked = async (
339344
await setBlockedInvalidity(candidate.stash, true);
340345
return true;
341346
}
342-
return true;
343347
} catch (e) {
344348
logger.error(`Error checking blocked: ${e}`, constraintsLabel);
345349
return false;
@@ -376,7 +380,6 @@ export const checkProvider = async (
376380
await setProviderInvalidity(candidate.stash, true);
377381
return true;
378382
}
379-
return true;
380383
} catch (e) {
381384
logger.error(`Error checking provider: ${e}`, constraintsLabel);
382385
return false;

packages/common/src/db/queries/Release.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,14 @@ export const setRelease = async (
1717
};
1818

1919
export const getLatestRelease = async (): Promise<any> => {
20-
return (await ReleaseModel.find({}).lean().sort("-publishedAt").limit(1))[0];
20+
try {
21+
const latestRelease = await ReleaseModel.findOne({})
22+
.sort("-publishedAt")
23+
.lean()
24+
.limit(1);
25+
return latestRelease;
26+
} catch (error) {
27+
console.error("Error while fetching latest release:", error);
28+
throw error;
29+
}
2130
};

packages/common/src/monitor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ export default class Monitor {
3939
const { tag_name, published_at } = latestRelease.data;
4040
const publishedAt = new Date(published_at).getTime();
4141

42-
const tagParts = tag_name.split("-");
43-
const version = tagParts[tagParts.length - 1];
42+
// Extract version number from the tag name
43+
const versionMatch = tag_name.match(/v?(\d+\.\d+\.\d+)/);
44+
if (!versionMatch) {
45+
logger.warn(`Unable to extract version from tag name: ${tag_name}`);
46+
return null;
47+
}
48+
const version = versionMatch[1]; // Extracted version number
4449

4550
await queries.setRelease(version, publishedAt);
4651

packages/common/src/scorekeeper/jobs/specificJobs/ReleaseMonitorJob.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const getLatestTaggedRelease = async () => {
2222
const ghApi = new Octokit();
2323

2424
try {
25-
// Assign the result of ghApi.repos.getLatestRelease() to latestRelease
2625
const release = await ghApi.repos.getLatestRelease({
2726
owner: "paritytech",
2827
repo: "polkadot-sdk",
@@ -32,7 +31,6 @@ export const getLatestTaggedRelease = async () => {
3231
logger.warn("Could not get latest release.", monitorLabel);
3332
}
3433

35-
// Check if latestRelease is null or if tag_name and published_at are not present
3634
if (
3735
!latestRelease ||
3836
!latestRelease.tag_name ||
@@ -44,20 +42,22 @@ export const getLatestTaggedRelease = async () => {
4442
const { tag_name, published_at } = latestRelease;
4543
const publishedAt = new Date(published_at).getTime();
4644

47-
await queries.setRelease(tag_name, publishedAt);
45+
const version = tag_name.split("-")[1];
4846

49-
const taggedReleaseName = latestRelease ? latestRelease?.name : "";
50-
if (latestRelease && tag_name === taggedReleaseName) {
47+
await queries.setRelease(version, publishedAt);
48+
49+
const taggedReleaseName = latestRelease ? latestRelease.name : "";
50+
if (latestRelease && version === taggedReleaseName) {
5151
logger.info("No new release found", monitorLabel);
5252
} else {
5353
latestRelease = {
54-
name: tag_name.split(`-`)[0],
54+
name: version,
5555
publishedAt,
5656
};
5757
}
5858

5959
logger.info(
60-
`Latest release updated: ${taggedReleaseName} | Published at: ${publishedAt}`,
60+
`Latest release updated: ${version} | Published at: ${publishedAt}`,
6161
monitorLabel,
6262
);
6363

@@ -76,6 +76,7 @@ export const getLatestTaggedRelease = async () => {
7676
jobStatusEmitter.emit("jobErrored", errorStatus);
7777
}
7878
};
79+
7980
// Called by worker to process Job
8081
export const processReleaseMonitorJob = async (job: any) => {
8182
await getLatestTaggedRelease();

packages/gateway/src/routes/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Location from "../controllers/Location";
99
import Validator from "../controllers/Validators";
1010
import Rewards from "../controllers/Rewards";
1111
import Block from "../controllers/Block";
12+
import { response } from "../controllers";
13+
import { queries } from "@1kv/common";
1214

1315
const router: any = new Router();
1416

@@ -127,4 +129,8 @@ router.get(API.BlockIndex, Block.getBlockIndex);
127129
router.get(API.StatsTotalReqeusts, Stats.getTotalRequests);
128130
router.get(API.StatsEndpointCounts, Stats.getEndpointCounts);
129131

132+
router.get(API.Release, async (ctx: any) => {
133+
response(ctx, 200, await queries.getLatestRelease());
134+
});
135+
130136
export default router;

0 commit comments

Comments
 (0)