@acurast/cli
Version:
A cli to interact with the Acurast Cloud.
133 lines • 5.96 kB
JavaScript
import { AssignmentStrategyVariant, DeploymentRuntime, ScriptMutability, } from '../types.js';
import { deviceVersions } from './app-versions.js';
export const second = 1000;
export const minute = 60 * second;
export const hour = 60 * minute;
export const day = 24 * hour;
export const DEFAULT_DURATION_MS = 10000;
export const DEFAULT_REPLICAS = 1;
export const DEFAULT_REWARD = 100000000000;
export const DEFAULT_MAX_ALLOWED_START_DELAY_MS = 10000;
export const DEFAULT_PROCESSOR_REPUTATION = 0;
export const DEFAULT_START_DELAY = 5 * minute;
export function isStartAtMsFromNow(startAt) {
return startAt.msFromNow !== undefined;
}
export function isStartAtTimestamp(startAt) {
return startAt.timestamp !== undefined;
}
function getKeyFromValue(map, value) {
var _a;
if (!map) {
return undefined;
}
return (_a = Array.from(map.entries()).find(([_, version]) => version === value)) === null || _a === void 0 ? void 0 : _a[0];
}
function convertMinProcessorVersions(minProcessorVersions) {
const minAndroidVersion = minProcessorVersions === null || minProcessorVersions === void 0 ? void 0 : minProcessorVersions.android;
const minIosVersion = minProcessorVersions === null || minProcessorVersions === void 0 ? void 0 : minProcessorVersions.ios;
const versions = [];
function addVersionIfDefined(version, platform, platformName) {
if (version !== undefined) {
const buildNumber = typeof version === 'number'
? version
: getKeyFromValue(deviceVersions.get(platform), version);
if (buildNumber) {
versions.push({
platform,
buildNumber,
});
}
else {
throw new Error(`Cannot resolve min processor version for ${platformName} from version "${version}" to buildNumber. Please specify the build number directly.`);
}
}
}
addVersionIfDefined(minAndroidVersion, 0, 'Android');
addVersionIfDefined(minIosVersion, 1, 'iOS');
if (versions.length === 0) {
return undefined;
}
return {
min: versions,
};
}
export const convertConfigToJob = (config) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const slots = (_a = config.numberOfReplicas) !== null && _a !== void 0 ? _a : DEFAULT_REPLICAS;
const rewardPerExecution = (_b = config.maxCostPerExecution) !== null && _b !== void 0 ? _b : DEFAULT_REWARD;
const startDelay = (_c = config.maxAllowedStartDelayInMs) !== null && _c !== void 0 ? _c : DEFAULT_MAX_ALLOWED_START_DELAY_MS;
const processorReputation = (_d = config.minProcessorReputation) !== null && _d !== void 0 ? _d : DEFAULT_PROCESSOR_REPUTATION;
const minProcessorVersions = convertMinProcessorVersions(config.minProcessorVersions);
const mutability = (_e = config.mutability) !== null && _e !== void 0 ? _e : ScriptMutability.Immutable;
const now = Date.now();
let startTime = now + DEFAULT_START_DELAY;
if (config.startAt) {
if (isStartAtMsFromNow(config.startAt)) {
startTime = now + config.startAt.msFromNow;
}
if (isStartAtTimestamp(config.startAt)) {
startTime = new Date(config.startAt.timestamp).getTime();
}
}
let duration;
let endTime;
let interval;
if (config.execution.type === 'onetime') {
duration = (_f = config.execution.maxExecutionTimeInMs) !== null && _f !== void 0 ? _f : DEFAULT_DURATION_MS;
endTime = startTime + duration + 1; // +1 because end time needs to be greater than start time + duration
interval = endTime - startTime;
}
else if (config.execution.type === 'interval') {
interval = config.execution.intervalInMs;
endTime =
startTime +
config.execution.intervalInMs * config.execution.numberOfExecutions +
1;
duration =
(_g = config.execution.maxExecutionTimeInMs) !== null && _g !== void 0 ? _g : config.execution.intervalInMs - 1;
}
else {
throw new Error('Invalid execution type');
}
const assignmentStrategy = config.assignmentStrategy.type === AssignmentStrategyVariant.Single
? {
variant: AssignmentStrategyVariant.Single,
instantMatch: (_h = config.assignmentStrategy.instantMatch) === null || _h === void 0 ? void 0 : _h.map((item) => ({
source: item.processor,
startDelay: item.maxAllowedStartDelayInMs,
})),
}
: { variant: AssignmentStrategyVariant.Competing };
return {
script: config.fileUrl,
allowedSources: config.processorWhitelist && config.processorWhitelist.length > 0
? config.processorWhitelist
: undefined, // Array of processors, whitelist who can execute this job
allowOnlyVerifiedSources: config.onlyAttestedDevices, // Only allow processors that are attested to execute this job
schedule: {
duration, // How is this calculated? Is it cheaper if its shorter?
startTime,
endTime,
interval,
maxStartDelay: startDelay,
},
memory: config.usageLimit.maxMemory,
networkRequests: config.usageLimit.maxNetworkRequests,
storage: config.usageLimit.maxStorage,
requiredModules: config.requiredModules,
mutability,
reuseKeysFrom: config.reuseKeysFrom,
extra: {
requirements: {
assignmentStrategy,
slots: slots,
reward: rewardPerExecution,
minReputation: processorReputation,
processorVersion: minProcessorVersions,
runtime: (_j = config.runtime) !== null && _j !== void 0 ? _j : DeploymentRuntime.NodeJSWithBundle,
},
},
};
};
//# sourceMappingURL=convertConfigToJob.js.map