Skip to content

Commit 0dd4c03

Browse files
authored
fix: protocol version for old configs and ram calculation when config is migrated (#1099)
* fix: protocol version for old configs and ram calculation when config is migrated * fix protocol version * set min and max
1 parent dfb59a5 commit 0dd4c03

File tree

3 files changed

+84
-74
lines changed

3 files changed

+84
-74
lines changed

packages/cli/package/src/lib/configs/project/provider/provider2.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import type { JSONSchemaType } from "ajv";
1919
import mapValues from "lodash-es/mapValues.js";
2020

21-
import { versions } from "../../../../versions.js";
2221
import { PT_SYMBOL } from "../../../const.js";
2322
import { numToStr } from "../../../helpers/typesafeStringify.js";
2423
import type { ConfigOptions } from "../../initConfigNewTypes.js";
@@ -38,6 +37,8 @@ import {
3837
type Config as PrevConfig,
3938
} from "./provider1.js";
4039

40+
export const PROTOCOL_VERSION_1 = 1;
41+
4142
type Offer = {
4243
minPricePerCuPerEpoch: string;
4344
computePeers: Array<string>;
@@ -65,19 +66,19 @@ const offerSchema = {
6566
minProtocolVersion: {
6667
type: "integer",
6768
description: `Min protocol version. Must be less then or equal to maxProtocolVersion. Default: ${numToStr(
68-
versions.protocolVersion,
69+
PROTOCOL_VERSION_1,
6970
)}`,
7071
nullable: true,
71-
default: versions.protocolVersion,
72+
default: PROTOCOL_VERSION_1,
7273
minimum: 1,
7374
},
7475
maxProtocolVersion: {
7576
type: "integer",
7677
description: `Max protocol version. Must be more then or equal to minProtocolVersion. Default: ${numToStr(
77-
versions.protocolVersion,
78+
PROTOCOL_VERSION_1,
7879
)}`,
7980
nullable: true,
80-
default: versions.protocolVersion,
81+
default: PROTOCOL_VERSION_1,
8182
minimum: 1,
8283
},
8384
},

packages/cli/package/src/lib/configs/project/provider/provider3.ts

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
} from "./provider1.js";
5151
import {
5252
offersSchema,
53+
PROTOCOL_VERSION_1,
5354
type Offers,
5455
type Config as PrevConfig,
5556
} from "./provider2.js";
@@ -172,69 +173,75 @@ export default {
172173
validateCC(config),
173174
validateMissingComputePeers(config),
174175
validateNoDuplicatePeerNamesInOffers(config),
175-
validateProtocolVersions(config),
176+
getValidateProtocolVersions(PROTOCOL_VERSION_1)(config),
176177
);
177178
},
178179
} satisfies ConfigOptions<PrevConfig, Config>;
179180

180-
export async function validateProtocolVersions(providerConfig: {
181-
offers: Record<
182-
string,
183-
{ maxProtocolVersion?: number; minProtocolVersion?: number }
184-
>;
185-
}): Promise<ValidationResult> {
186-
const errors = (
187-
await Promise.all(
188-
Object.entries(providerConfig.offers).flatMap(
189-
([
190-
offer,
191-
{
192-
maxProtocolVersion = versions.protocolVersion,
193-
minProtocolVersion = versions.protocolVersion,
194-
},
195-
]) => {
196-
return [
197-
Promise.resolve({
198-
offer,
199-
property: "minProtocolVersion or maxProtocolVersion",
200-
validity:
201-
minProtocolVersion > maxProtocolVersion
202-
? `minProtocolVersion must be less than or equal to maxProtocolVersion. Got: minProtocolVersion=${color.yellow(
203-
minProtocolVersion,
204-
)} maxProtocolVersion=${color.yellow(maxProtocolVersion)}`
205-
: true,
206-
}),
207-
...(
208-
[
209-
["minProtocolVersion", minProtocolVersion],
210-
["maxProtocolVersion", maxProtocolVersion],
211-
] as const
212-
).map(async ([property, v]) => {
213-
return {
181+
export function getValidateProtocolVersions(protocolVersion: number) {
182+
return async function validateProtocolVersions(providerConfig: {
183+
offers: Record<
184+
string,
185+
{ maxProtocolVersion?: number; minProtocolVersion?: number }
186+
>;
187+
}): Promise<ValidationResult> {
188+
const shouldValidateOnChain = protocolVersion === versions.protocolVersion;
189+
190+
const errors = (
191+
await Promise.all(
192+
Object.entries(providerConfig.offers).flatMap(
193+
([
194+
offer,
195+
{
196+
maxProtocolVersion = protocolVersion,
197+
minProtocolVersion = protocolVersion,
198+
},
199+
]) => {
200+
return [
201+
Promise.resolve({
214202
offer,
215-
property,
216-
validity: await validateProtocolVersion(v),
217-
};
218-
}),
219-
];
220-
},
221-
),
222-
)
223-
).filter((a): a is typeof a & { validity: string } => {
224-
return a.validity !== true;
225-
});
203+
property: "minProtocolVersion or maxProtocolVersion",
204+
validity:
205+
minProtocolVersion > maxProtocolVersion
206+
? `minProtocolVersion must be less than or equal to maxProtocolVersion. Got: minProtocolVersion=${color.yellow(
207+
minProtocolVersion,
208+
)} maxProtocolVersion=${color.yellow(maxProtocolVersion)}`
209+
: true,
210+
}),
211+
...(shouldValidateOnChain
212+
? (
213+
[
214+
["minProtocolVersion", minProtocolVersion],
215+
["maxProtocolVersion", maxProtocolVersion],
216+
] as const
217+
).map(async ([property, v]) => {
218+
return {
219+
offer,
220+
property,
221+
validity: await validateProtocolVersion(v),
222+
};
223+
})
224+
: []),
225+
];
226+
},
227+
),
228+
)
229+
).filter((a): a is typeof a & { validity: string } => {
230+
return a.validity !== true;
231+
});
226232

227-
if (errors.length > 0) {
228-
return errors
229-
.map(({ offer, property, validity }) => {
230-
return `Offer ${color.yellow(offer)} has invalid ${color.yellow(
231-
property,
232-
)} property: ${validity}`;
233-
})
234-
.join("\n");
235-
}
233+
if (errors.length > 0) {
234+
return errors
235+
.map(({ offer, property, validity }) => {
236+
return `Offer ${color.yellow(offer)} has invalid ${color.yellow(
237+
property,
238+
)} property: ${validity}`;
239+
})
240+
.join("\n");
241+
}
236242

237-
return true;
243+
return true;
244+
};
238245
}
239246

240247
export function validateNoDuplicatePeerNamesInOffers(config: {

packages/cli/package/src/lib/configs/project/provider/provider4.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import isEmpty from "lodash-es/isEmpty.js";
2323
import merge from "lodash-es/merge.js";
2424
import { stringify } from "yaml";
2525

26-
import { versions } from "../../../../versions.js";
2726
import { ajv, validationErrorToString } from "../../../ajvInstance.js";
2827
import { ptParse } from "../../../chain/currencies.js";
2928
import { commandObj } from "../../../commandObj.js";
@@ -51,12 +50,14 @@ import {
5150
validateNoDuplicatePeerNamesInOffers,
5251
type CapacityCommitments,
5352
validateCC,
54-
validateProtocolVersions,
53+
getValidateProtocolVersions,
5554
} from "./provider3.js";
5655

5756
const OPTIONAL_RESOURCE_DETAILS_STRING = "<optional>";
5857
const OPTIONAL_RESOURCE_DETAILS_NUMBER = 1;
5958
const OPTIONAL_RESOURCE_DETAILS_BOOLEAN = false;
59+
const BYTES_PER_CORE = 4_000_000_000;
60+
const PROTOCOL_VERSION_2 = 2;
6061

6162
type PeerCPUDetails = {
6263
model?: string;
@@ -424,20 +425,22 @@ const offerSchema = {
424425
minProtocolVersion: {
425426
type: "integer",
426427
description: `Min protocol version. Must be less then or equal to maxProtocolVersion. Default: ${numToStr(
427-
versions.protocolVersion,
428+
PROTOCOL_VERSION_2,
428429
)}`,
429430
nullable: true,
430-
default: versions.protocolVersion,
431-
minimum: 1,
431+
default: PROTOCOL_VERSION_2,
432+
minimum: PROTOCOL_VERSION_2,
433+
maximum: PROTOCOL_VERSION_2,
432434
},
433435
maxProtocolVersion: {
434436
type: "integer",
435437
description: `Max protocol version. Must be more then or equal to minProtocolVersion. Default: ${numToStr(
436-
versions.protocolVersion,
438+
PROTOCOL_VERSION_2,
437439
)}`,
438440
nullable: true,
439-
default: versions.protocolVersion,
440-
minimum: 1,
441+
default: PROTOCOL_VERSION_2,
442+
minimum: PROTOCOL_VERSION_2,
443+
maximum: PROTOCOL_VERSION_2,
441444
},
442445
},
443446
required: ["computePeers", "dataCenterName", "resourcePrices"],
@@ -514,7 +517,6 @@ export default {
514517
const dataCenterName = await getDefaultDataCenterName();
515518

516519
const newOffers = Object.fromEntries(
517-
// TODO: protocol versions
518520
await Promise.all(
519521
Object.entries(offers).map(async ([name, { computePeers }]) => {
520522
return [
@@ -542,7 +544,7 @@ export default {
542544
validateEnoughRAMPerCPUCore(config),
543545
validateCC(config),
544546
validateNoDuplicatePeerNamesInOffers(config),
545-
validateProtocolVersions(config),
547+
getValidateProtocolVersions(PROTOCOL_VERSION_2)(config),
546548
validateOfferHasComputePeerResources(config),
547549
validateComputePeerIPs(config),
548550
validateOfferPrices(config),
@@ -822,6 +824,7 @@ export async function getDefaultComputePeerConfig({
822824
ip,
823825
index,
824826
}: DefaultComputePeerConfigArgs): Promise<ComputePeer> {
827+
const xbytes = (await import("xbytes")).default;
825828
const resources = await getDefaultChainResources();
826829

827830
return {
@@ -833,7 +836,8 @@ export async function getDefaultComputePeerConfig({
833836
},
834837
ram: {
835838
name: resources.ram,
836-
supply: "11 GiB",
839+
supply:
840+
computeUnits <= 1 ? "11 GiB" : xbytes(computeUnits * BYTES_PER_CORE),
837841
},
838842
storage: [
839843
{
@@ -1263,8 +1267,6 @@ const onChainResourceTypeToResourceTypeMap: Record<
12631267
[OnChainResourceType.PUBLIC_IP]: "ip",
12641268
};
12651269

1266-
const BYTES_PER_CORE = 4_000_000_000;
1267-
12681270
async function validateEnoughRAMPerCPUCore({
12691271
computePeers,
12701272
}: {

0 commit comments

Comments
 (0)