Skip to content

Commit

Permalink
fix: protocol version for old configs and ram calculation when config…
Browse files Browse the repository at this point in the history
… is migrated (#1099)

* fix: protocol version for old configs and ram calculation when config is migrated

* fix protocol version

* set min and max
  • Loading branch information
shamsartem authored Jan 28, 2025
1 parent dfb59a5 commit 0dd4c03
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import type { JSONSchemaType } from "ajv";
import mapValues from "lodash-es/mapValues.js";

import { versions } from "../../../../versions.js";
import { PT_SYMBOL } from "../../../const.js";
import { numToStr } from "../../../helpers/typesafeStringify.js";
import type { ConfigOptions } from "../../initConfigNewTypes.js";
Expand All @@ -38,6 +37,8 @@ import {
type Config as PrevConfig,
} from "./provider1.js";

export const PROTOCOL_VERSION_1 = 1;

type Offer = {
minPricePerCuPerEpoch: string;
computePeers: Array<string>;
Expand Down Expand Up @@ -65,19 +66,19 @@ const offerSchema = {
minProtocolVersion: {
type: "integer",
description: `Min protocol version. Must be less then or equal to maxProtocolVersion. Default: ${numToStr(
versions.protocolVersion,
PROTOCOL_VERSION_1,
)}`,
nullable: true,
default: versions.protocolVersion,
default: PROTOCOL_VERSION_1,
minimum: 1,
},
maxProtocolVersion: {
type: "integer",
description: `Max protocol version. Must be more then or equal to minProtocolVersion. Default: ${numToStr(
versions.protocolVersion,
PROTOCOL_VERSION_1,
)}`,
nullable: true,
default: versions.protocolVersion,
default: PROTOCOL_VERSION_1,
minimum: 1,
},
},
Expand Down
119 changes: 63 additions & 56 deletions packages/cli/package/src/lib/configs/project/provider/provider3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
} from "./provider1.js";
import {
offersSchema,
PROTOCOL_VERSION_1,
type Offers,
type Config as PrevConfig,
} from "./provider2.js";
Expand Down Expand Up @@ -172,69 +173,75 @@ export default {
validateCC(config),
validateMissingComputePeers(config),
validateNoDuplicatePeerNamesInOffers(config),
validateProtocolVersions(config),
getValidateProtocolVersions(PROTOCOL_VERSION_1)(config),
);
},
} satisfies ConfigOptions<PrevConfig, Config>;

export async function validateProtocolVersions(providerConfig: {
offers: Record<
string,
{ maxProtocolVersion?: number; minProtocolVersion?: number }
>;
}): Promise<ValidationResult> {
const errors = (
await Promise.all(
Object.entries(providerConfig.offers).flatMap(
([
offer,
{
maxProtocolVersion = versions.protocolVersion,
minProtocolVersion = versions.protocolVersion,
},
]) => {
return [
Promise.resolve({
offer,
property: "minProtocolVersion or maxProtocolVersion",
validity:
minProtocolVersion > maxProtocolVersion
? `minProtocolVersion must be less than or equal to maxProtocolVersion. Got: minProtocolVersion=${color.yellow(
minProtocolVersion,
)} maxProtocolVersion=${color.yellow(maxProtocolVersion)}`
: true,
}),
...(
[
["minProtocolVersion", minProtocolVersion],
["maxProtocolVersion", maxProtocolVersion],
] as const
).map(async ([property, v]) => {
return {
export function getValidateProtocolVersions(protocolVersion: number) {
return async function validateProtocolVersions(providerConfig: {
offers: Record<
string,
{ maxProtocolVersion?: number; minProtocolVersion?: number }
>;
}): Promise<ValidationResult> {
const shouldValidateOnChain = protocolVersion === versions.protocolVersion;

const errors = (
await Promise.all(
Object.entries(providerConfig.offers).flatMap(
([
offer,
{
maxProtocolVersion = protocolVersion,
minProtocolVersion = protocolVersion,
},
]) => {
return [
Promise.resolve({
offer,
property,
validity: await validateProtocolVersion(v),
};
}),
];
},
),
)
).filter((a): a is typeof a & { validity: string } => {
return a.validity !== true;
});
property: "minProtocolVersion or maxProtocolVersion",
validity:
minProtocolVersion > maxProtocolVersion
? `minProtocolVersion must be less than or equal to maxProtocolVersion. Got: minProtocolVersion=${color.yellow(
minProtocolVersion,
)} maxProtocolVersion=${color.yellow(maxProtocolVersion)}`
: true,
}),
...(shouldValidateOnChain
? (
[
["minProtocolVersion", minProtocolVersion],
["maxProtocolVersion", maxProtocolVersion],
] as const
).map(async ([property, v]) => {
return {
offer,
property,
validity: await validateProtocolVersion(v),
};
})
: []),
];
},
),
)
).filter((a): a is typeof a & { validity: string } => {
return a.validity !== true;
});

if (errors.length > 0) {
return errors
.map(({ offer, property, validity }) => {
return `Offer ${color.yellow(offer)} has invalid ${color.yellow(
property,
)} property: ${validity}`;
})
.join("\n");
}
if (errors.length > 0) {
return errors
.map(({ offer, property, validity }) => {
return `Offer ${color.yellow(offer)} has invalid ${color.yellow(
property,
)} property: ${validity}`;
})
.join("\n");
}

return true;
return true;
};
}

export function validateNoDuplicatePeerNamesInOffers(config: {
Expand Down
28 changes: 15 additions & 13 deletions packages/cli/package/src/lib/configs/project/provider/provider4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import isEmpty from "lodash-es/isEmpty.js";
import merge from "lodash-es/merge.js";
import { stringify } from "yaml";

import { versions } from "../../../../versions.js";
import { ajv, validationErrorToString } from "../../../ajvInstance.js";
import { ptParse } from "../../../chain/currencies.js";
import { commandObj } from "../../../commandObj.js";
Expand Down Expand Up @@ -51,12 +50,14 @@ import {
validateNoDuplicatePeerNamesInOffers,
type CapacityCommitments,
validateCC,
validateProtocolVersions,
getValidateProtocolVersions,
} from "./provider3.js";

const OPTIONAL_RESOURCE_DETAILS_STRING = "<optional>";
const OPTIONAL_RESOURCE_DETAILS_NUMBER = 1;
const OPTIONAL_RESOURCE_DETAILS_BOOLEAN = false;
const BYTES_PER_CORE = 4_000_000_000;
const PROTOCOL_VERSION_2 = 2;

type PeerCPUDetails = {
model?: string;
Expand Down Expand Up @@ -424,20 +425,22 @@ const offerSchema = {
minProtocolVersion: {
type: "integer",
description: `Min protocol version. Must be less then or equal to maxProtocolVersion. Default: ${numToStr(
versions.protocolVersion,
PROTOCOL_VERSION_2,
)}`,
nullable: true,
default: versions.protocolVersion,
minimum: 1,
default: PROTOCOL_VERSION_2,
minimum: PROTOCOL_VERSION_2,
maximum: PROTOCOL_VERSION_2,
},
maxProtocolVersion: {
type: "integer",
description: `Max protocol version. Must be more then or equal to minProtocolVersion. Default: ${numToStr(
versions.protocolVersion,
PROTOCOL_VERSION_2,
)}`,
nullable: true,
default: versions.protocolVersion,
minimum: 1,
default: PROTOCOL_VERSION_2,
minimum: PROTOCOL_VERSION_2,
maximum: PROTOCOL_VERSION_2,
},
},
required: ["computePeers", "dataCenterName", "resourcePrices"],
Expand Down Expand Up @@ -514,7 +517,6 @@ export default {
const dataCenterName = await getDefaultDataCenterName();

const newOffers = Object.fromEntries(
// TODO: protocol versions
await Promise.all(
Object.entries(offers).map(async ([name, { computePeers }]) => {
return [
Expand Down Expand Up @@ -542,7 +544,7 @@ export default {
validateEnoughRAMPerCPUCore(config),
validateCC(config),
validateNoDuplicatePeerNamesInOffers(config),
validateProtocolVersions(config),
getValidateProtocolVersions(PROTOCOL_VERSION_2)(config),
validateOfferHasComputePeerResources(config),
validateComputePeerIPs(config),
validateOfferPrices(config),
Expand Down Expand Up @@ -822,6 +824,7 @@ export async function getDefaultComputePeerConfig({
ip,
index,
}: DefaultComputePeerConfigArgs): Promise<ComputePeer> {
const xbytes = (await import("xbytes")).default;
const resources = await getDefaultChainResources();

return {
Expand All @@ -833,7 +836,8 @@ export async function getDefaultComputePeerConfig({
},
ram: {
name: resources.ram,
supply: "11 GiB",
supply:
computeUnits <= 1 ? "11 GiB" : xbytes(computeUnits * BYTES_PER_CORE),
},
storage: [
{
Expand Down Expand Up @@ -1263,8 +1267,6 @@ const onChainResourceTypeToResourceTypeMap: Record<
[OnChainResourceType.PUBLIC_IP]: "ip",
};

const BYTES_PER_CORE = 4_000_000_000;

async function validateEnoughRAMPerCPUCore({
computePeers,
}: {
Expand Down

0 comments on commit 0dd4c03

Please sign in to comment.