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

Commit bf50d4b

Browse files
committed
start removing duplicated code
1 parent a4da8ee commit bf50d4b

File tree

3 files changed

+79
-217
lines changed

3 files changed

+79
-217
lines changed

packages/common/src/constraints/ValidityChecks.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export const checkValidateIntention = async (
5252
!validators?.length ||
5353
validators.includes(Util.formatAddress(candidate?.stash, config))
5454
) {
55-
await setValidateIntentionValidity(candidate.stash, true);
55+
await setValidateIntentionValidity(candidate, true);
5656
return true;
5757
}
5858

59-
await setValidateIntentionValidity(candidate.stash, false);
59+
await setValidateIntentionValidity(candidate, false);
6060
return false;
6161
} catch (e) {
6262
logger.error(`Error checking validate intention: ${e}`, constraintsLabel);
@@ -74,9 +74,9 @@ export const checkAllValidateIntentions = async (
7474
const validators = await chaindata.getValidators();
7575
for (const candidate of candidates) {
7676
if (!validators.includes(Util.formatAddress(candidate.stash, config))) {
77-
await setValidateIntentionValidity(candidate.stash, false);
77+
await setValidateIntentionValidity(candidate, false);
7878
} else {
79-
await setValidateIntentionValidity(candidate.stash, true);
79+
await setValidateIntentionValidity(candidate, true);
8080
}
8181
}
8282
return true;
@@ -95,7 +95,7 @@ export const checkLatestClientVersion = async (
9595
const skipClientUpgrade = config.constraints?.skipClientUpgrade || false;
9696
if (skipClientUpgrade || candidate?.implementation === "Kagome Node") {
9797
// Skip the check if the node is a Kagome Client or if skipping client upgrade is enabled
98-
await setLatestClientReleaseValidity(candidate.stash, true);
98+
await setLatestClientReleaseValidity(candidate, true);
9999
return true;
100100
}
101101

@@ -145,34 +145,34 @@ export const checkLatestClientVersion = async (
145145

146146
// If cannot parse the version, set the release as invalid
147147
if (!nodeVersion || !latestVersion) {
148-
await setLatestClientReleaseValidity(candidate.stash, false);
148+
await setLatestClientReleaseValidity(candidate, false);
149149
return false;
150150
}
151151

152152
const isUpgraded = semver.gte(nodeVersion, latestVersion);
153153

154154
// If they are not upgraded, set the validity as invalid
155155
if (!isUpgraded) {
156-
await setLatestClientReleaseValidity(candidate.stash, false);
156+
await setLatestClientReleaseValidity(candidate, false);
157157
return false;
158158
}
159159

160160
// If the current version is the latest release, set the release as valid
161-
await setLatestClientReleaseValidity(candidate.stash, true);
161+
await setLatestClientReleaseValidity(candidate, true);
162162
return true;
163163
} else {
164164
logger.info(`Still in grace window of latest release`, constraintsLabel);
165165

166166
// If not past the grace window, set the release as invalid
167-
await setLatestClientReleaseValidity(candidate.stash, true);
167+
await setLatestClientReleaseValidity(candidate, true);
168168
return true;
169169
}
170170
} catch (e) {
171171
logger.error(
172172
`Error checking latest client version: ${e}`,
173173
constraintsLabel,
174174
);
175-
await setLatestClientReleaseValidity(candidate.stash, false);
175+
await setLatestClientReleaseValidity(candidate, false);
176176
throw new Error("could not make validity check");
177177
}
178178
};
@@ -185,14 +185,14 @@ export const checkConnectionTime = async (
185185
if (!config?.constraints?.skipConnectionTime) {
186186
const now = new Date().getTime();
187187
if (now - candidate.discoveredAt < Constants.WEEK) {
188-
await setConnectionTimeInvalidity(candidate.stash, false);
188+
await setConnectionTimeInvalidity(candidate, false);
189189
return false;
190190
} else {
191-
await setConnectionTimeInvalidity(candidate.stash, true);
191+
await setConnectionTimeInvalidity(candidate, true);
192192
return true;
193193
}
194194
} else {
195-
await setConnectionTimeInvalidity(candidate.stash, true);
195+
await setConnectionTimeInvalidity(candidate, true);
196196
return true;
197197
}
198198
} catch (e) {
@@ -211,15 +211,15 @@ export const checkIdentity = async (
211211
);
212212
if (!hasIdentity) {
213213
const invalidityString = `${candidate.name} does not have an identity set.`;
214-
await setIdentityInvalidity(candidate.stash, false, invalidityString);
214+
await setIdentityInvalidity(candidate, false, invalidityString);
215215
return false;
216216
}
217217
if (!verified) {
218218
const invalidityString = `${candidate.name} has an identity but is not verified by the registrar.`;
219-
await setIdentityInvalidity(candidate.stash, false, invalidityString);
219+
await setIdentityInvalidity(candidate, false, invalidityString);
220220
return false;
221221
}
222-
await setIdentityInvalidity(candidate.stash, true);
222+
await setIdentityInvalidity(candidate, true);
223223
return true;
224224
} catch (e) {
225225
logger.error(`Error checking identity: ${e}`, constraintsLabel);
@@ -231,10 +231,10 @@ export const checkOffline = async (candidate: Candidate): Promise<boolean> => {
231231
try {
232232
const totalOffline = candidate.offlineAccumulated / Constants.WEEK;
233233
if (totalOffline > 0.02) {
234-
await setOfflineAccumulatedInvalidity(candidate.stash, false);
234+
await setOfflineAccumulatedInvalidity(candidate, false);
235235
return false;
236236
} else {
237-
await setOfflineAccumulatedInvalidity(candidate.stash, true);
237+
await setOfflineAccumulatedInvalidity(candidate, true);
238238
return true;
239239
}
240240
return true;

0 commit comments

Comments
 (0)