Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions apps/api/src/bid-screening/http-schemas/bid-screening.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import { z } from "@hono/zod-openapi";
const UIntStringSchema = z.string().regex(/^\d+$/, "Must be an unsigned integer string");

const ResourceValueSchema = z.object({
val: UIntStringSchema.openapi({ description: "String-encoded integer value", example: "1000" })
val: z
.string()
.max(80)
.transform(str => {
if (/^\d+$/.test(str)) return BigInt(str);
const parsed = Buffer.from(str, "base64").toString("utf-8");
if (/^\d+$/.test(parsed)) return BigInt(parsed);
return NaN;
})
.refine(
val => !Number.isFinite(val) && typeof val === "bigint" && val >= 0n,
"Must be a non-negative integer or its protobuf base64-encoded representation"
)
});

// Mirrors AttributeNameRegexpStringWildcard in akash-network/chain-sdk
Expand All @@ -24,12 +36,12 @@ const StorageResourceSchema = z
.object({
name: z.string().openapi({ description: "Storage volume name", example: "default" }),
quantity: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
})
.superRefine((vol, ctx) => {
const isPersistent = vol.attributes.some(a => a.key === "persistent" && a.value === "true");
const isPersistent = vol.attributes?.some(a => a.key === "persistent" && a.value === "true");
if (!isPersistent) return;
const storageClass = vol.attributes.find(a => a.key === "class")?.value;
const storageClass = vol.attributes?.find(a => a.key === "class")?.value;
if (!storageClass || storageClass === "ram") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand All @@ -43,18 +55,18 @@ const ResourceSchema = z.object({
id: z.number().int().openapi({ description: "Resource unit ID", example: 1 }),
cpu: z.object({
units: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
memory: z.object({
quantity: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
gpu: z.object({
units: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
storage: z.array(StorageResourceSchema),
endpoints: z.array(z.unknown()).optional()
endpoints: z.array(z.unknown()).optional().optional()
});

const PriceSchema = z.object({
Expand Down
20 changes: 4 additions & 16 deletions apps/api/test/functional/__snapshots__/docs.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2859,9 +2859,7 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
"units": {
"properties": {
"val": {
"description": "String-encoded integer value",
"example": "1000",
"pattern": "^\\d+$",
"maxLength": 80,
"type": "string",
},
},
Expand All @@ -2873,7 +2871,6 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
},
"required": [
"units",
"attributes",
],
"type": "object",
},
Expand Down Expand Up @@ -2913,9 +2910,7 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
"units": {
"properties": {
"val": {
"description": "String-encoded integer value",
"example": "1000",
"pattern": "^\\d+$",
"maxLength": 80,
"type": "string",
},
},
Expand All @@ -2927,7 +2922,6 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
},
"required": [
"units",
"attributes",
],
"type": "object",
},
Expand Down Expand Up @@ -2966,9 +2960,7 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
"quantity": {
"properties": {
"val": {
"description": "String-encoded integer value",
"example": "1000",
"pattern": "^\\d+$",
"maxLength": 80,
"type": "string",
},
},
Expand All @@ -2980,7 +2972,6 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
},
"required": [
"quantity",
"attributes",
],
"type": "object",
},
Expand Down Expand Up @@ -3020,9 +3011,7 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
"quantity": {
"properties": {
"val": {
"description": "String-encoded integer value",
"example": "1000",
"pattern": "^\\d+$",
"maxLength": 80,
"type": "string",
},
},
Expand All @@ -3035,7 +3024,6 @@ exports[`API Docs > GET /v1/doc > returns docs with all routes expected 1`] = `
"required": [
"name",
"quantity",
"attributes",
],
"type": "object",
},
Expand Down
2 changes: 1 addition & 1 deletion apps/provider-inventory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Akash Network",
"main": "dist/server.js",
"scripts": {
"build": "NODE_ENV=production tsup",
"build": "NODE_ENV=production tsup && tsc --noEmit",
"build:container": "dc build provider-inventory",
"dev": "npm run migration:exec && npm run start",
"format": "prettier --write ./*.{ts,js,json} **/*.{ts,js,json}",
Expand Down
30 changes: 20 additions & 10 deletions apps/provider-inventory/src/http-schemas/bid-screening.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import { z } from "@hono/zod-openapi";
const UIntStringSchema = z.string().regex(/^\d+$/, "Must be an unsigned integer string");

const ResourceValueSchema = z.object({
val: UIntStringSchema.openapi({ description: "String-encoded integer value", example: "1000" })
val: z
.string()
.max(80)
.transform(str => {
if (/^\d+$/.test(str)) return BigInt(str);
const parsed = Buffer.from(str, "base64").toString("utf-8");
if (/^\d+$/.test(parsed)) return BigInt(parsed);
return NaN;
})
.refine(
(val): val is bigint => !Number.isFinite(val) && typeof val === "bigint" && val >= 0n,
"Must be a non-negative integer or its protobuf base64-encoded representation"
)
});

// Mirrors AttributeNameRegexpStringWildcard in akash-network/chain-sdk
Expand All @@ -23,12 +35,12 @@ const StorageResourceSchema = z
.object({
name: z.string().openapi({ description: "Storage volume name", example: "default" }),
quantity: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
})
.superRefine((vol, ctx) => {
const isPersistent = vol.attributes.some(a => a.key === "persistent" && a.value === "true");
const isPersistent = vol.attributes?.some(a => a.key === "persistent" && a.value === "true");
if (!isPersistent) return;
const storageClass = vol.attributes.find(a => a.key === "class")?.value;
const storageClass = vol.attributes?.find(a => a.key === "class")?.value;
if (!storageClass || storageClass === "ram") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand All @@ -42,18 +54,18 @@ const ResourceSchema = z.object({
id: z.number().int().openapi({ description: "Resource unit ID", example: 1 }),
cpu: z.object({
units: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
memory: z.object({
quantity: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
gpu: z.object({
units: ResourceValueSchema,
attributes: z.array(AttributeSchema)
attributes: z.array(AttributeSchema).optional()
}),
storage: z.array(StorageResourceSchema),
endpoints: z.array(z.unknown()).optional()
endpoints: z.array(z.unknown()).optional().optional()
});

const PriceSchema = z.object({
Expand Down Expand Up @@ -87,8 +99,6 @@ export type BidScreeningRequest = z.infer<typeof BidScreeningRequestSchema>;
const ProviderResultSchema = z.object({
owner: z.string().openapi({ description: "Provider address", example: "akash1q7spv2cw06yszgfp4f9ed59lkka6ytn8g4tkjf" }),
hostUri: z.string().openapi({ description: "Provider HTTPS endpoint", example: "https://provider.europlots.com:8443" }),
region: z.string().nullable().openapi({ description: "Geo-resolved region from IP" }),
uptime7d: z.number().nullable().openapi({ description: "7-day uptime as decimal (0.0-1.0)", example: 0.998 }),
isAudited: z.boolean().openapi({ description: "True if signed by a known auditor" })
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,23 @@ describe(mapGroupSpecToResourceUnits.name, () => {
expect(units[1].resources.gpu.units).toBe(1n);
});

it("throws for invalid resource value string", () => {
expect(() => setup({ invalidVal: true })).toThrow();
});

it("handles empty storage array", () => {
const { units } = setup({ emptyStorage: true });
expect(units[0].resources.storage).toEqual([]);
});

function setup(input: { count?: number; multiGroup?: boolean; invalidVal?: boolean; emptyStorage?: boolean }) {
function setup(input: { count?: number; multiGroup?: boolean; emptyStorage?: boolean }) {
const baseResource = {
id: 1,
cpu: { units: { val: input.invalidVal ? "notanumber" : "1000" }, attributes: [] },
memory: { quantity: { val: "1073741824" }, attributes: [] },
gpu: { units: { val: "0" }, attributes: [] },
cpu: { units: { val: 1000n }, attributes: [] },
memory: { quantity: { val: 1073741824n }, attributes: [] },
gpu: { units: { val: 0n }, attributes: [] },
storage: input.emptyStorage
? []
: [
{
name: "default",
quantity: { val: "5368709120" },
quantity: { val: 5368709120n },
attributes: [
{ key: "persistent", value: "false" },
{ key: "class", value: "ephemeral" }
Expand All @@ -86,7 +82,7 @@ describe(mapGroupSpecToResourceUnits.name, () => {
resource: {
...baseResource,
id: 2,
gpu: { units: { val: "1" }, attributes: [{ key: "vendor/nvidia/model/a100", value: "true" }] }
gpu: { units: { val: 1n }, attributes: [{ key: "vendor/nvidia/model/a100", value: "true" }] }
},
count: 1,
price: { denom: "uakt", amount: "5000" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,34 @@ import type { GroupSpec } from "@akashnetwork/chain-sdk/private-types/akash.v1be

import type { RequestedResourceUnit, RequestedStorage, ToJSON } from "../../types/inventory.types";

function parseResourceValue(val: string): bigint {
const parsed = BigInt(val);
if (parsed < 0n) {
throw new Error(`Invalid resource value: ${val} must be a non-negative integer`);
}
return parsed;
}

const EMPTY_ARRAY = Object.freeze([]);
export function mapGroupSpecToResourceUnits(request: GroupSpecJSON): RequestedResourceUnit[] {
return request.resources.map(unit => {
const resource = unit.resource;

const storage: RequestedStorage[] = resource.storage.map(vol => ({
name: vol.name,
quantity: parseResourceValue(vol.quantity.val),
attributes: vol.attributes
}));

return {
id: resource.id,
resources: {
cpu: {
units: parseResourceValue(resource.cpu.units.val),
attributes: resource.cpu.attributes
units: resource.cpu.units.val,
attributes: resource.cpu.attributes ?? EMPTY_ARRAY
},
gpu: {
units: parseResourceValue(resource.gpu.units.val),
attributes: resource.gpu.attributes
units: resource.gpu.units.val,
attributes: resource.gpu.attributes ?? EMPTY_ARRAY
},
memory: {
quantity: parseResourceValue(resource.memory.quantity.val),
attributes: resource.memory.attributes
quantity: resource.memory.quantity.val,
attributes: resource.memory.attributes ?? EMPTY_ARRAY
},
storage
storage: resource.storage.map(
(s): RequestedStorage => ({
name: s.name,
quantity: s.quantity.val,
attributes: s.attributes ?? EMPTY_ARRAY
})
),
endpoints: resource.endpoints ?? EMPTY_ARRAY
},
count: unit.count
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,34 @@ describe(parseQuantity.name, () => {
expect(parseQuantity({ string: unsafe.toString() })).toBe(unsafe);
});
});

describe("with multiplier (pre-truncation scaling)", () => {
it("scales 500m to 500 with multiplier 1000n", () => {
expect(parseQuantity({ string: "500m" }, 1000n)).toBe(500n);
});

it("scales 1500m to 1500 with multiplier 1000n", () => {
expect(parseQuantity({ string: "1500m" }, 1000n)).toBe(1500n);
});

it("scales integer cores to millicores with multiplier 1000n", () => {
expect(parseQuantity({ string: "2" }, 1000n)).toBe(2000n);
});

it("scales fractional cores to millicores with multiplier 1000n", () => {
expect(parseQuantity({ string: "0.5" }, 1000n)).toBe(500n);
});

it("scales binary fractional values before truncation", () => {
expect(parseQuantity({ string: "1.5Ki" }, 1000n)).toBe(1536000n);
});

it("scales negative millicore values", () => {
expect(parseQuantity({ string: "-500m" }, 1000n)).toBe(-500n);
});

it("scales decimal-exponent millicore equivalents", () => {
expect(parseQuantity({ string: "5e-3" }, 1000n)).toBe(5n);
});
});
});
Loading
Loading