Skip to content

Commit

Permalink
feat(sdl): move validation to akashjs
Browse files Browse the repository at this point in the history
refs #133
  • Loading branch information
ygrishajev committed May 6, 2024
1 parent 78dbf35 commit 700049a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 168 deletions.
6 changes: 4 additions & 2 deletions deploy-web/src/types/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NetworkId } from "@akashnetwork/akashjs/build/types/network";

export type Network = {
id: string;
id: NetworkId;
title: string;
description: string;
nodesUrl: string;
Expand All @@ -9,4 +11,4 @@ export type Network = {
rpcEndpoint?: string;
version: string;
enabled: boolean;
};
};
21 changes: 11 additions & 10 deletions deploy-web/src/utils/deploymentData/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import { SDL } from "@akashnetwork/akashjs/build/sdl";
import { v2Sdl } from "@akashnetwork/akashjs/build/sdl/types";
import { NetworkId } from "@akashnetwork/akashjs/build/types/network";

export class CustomValidationError extends Error {
constructor(message) {
Expand Down Expand Up @@ -61,26 +62,26 @@ function isValidString(value: unknown): value is string {
return typeof value === "string" && !!value;
}

export function getSdl(yamlJson: string | v2Sdl, networkType: NetworkType) {
return isValidString(yamlJson) ? SDL.fromString(yamlJson, networkType) : new SDL(yamlJson, networkType);
export function getSdl(yamlJson: string | v2Sdl, networkType: NetworkType, networkId: NetworkId) {
return isValidString(yamlJson) ? SDL.fromString(yamlJson, networkType, networkId) : new SDL(yamlJson, networkType, networkId);
}

export function DeploymentGroups(yamlJson: string | v2Sdl, networkType: NetworkType) {
const sdl = getSdl(yamlJson, networkType);
export function DeploymentGroups(yamlJson: string | v2Sdl, networkType: NetworkType, networkId: NetworkId) {
const sdl = getSdl(yamlJson, networkType, networkId);
return sdl.groups();
}

export function Manifest(yamlJson: string | v2Sdl, networkType: NetworkType, asString = false) {
const sdl = getSdl(yamlJson, networkType);
export function Manifest(yamlJson: string | v2Sdl, networkType: NetworkType, networkId: NetworkId, asString = false) {
const sdl = getSdl(yamlJson, networkType, networkId);
return sdl.manifest(asString);
}

export async function ManifestVersion(yamlJson: string | v2Sdl, networkType: NetworkType) {
const sdl = getSdl(yamlJson, networkType);
export async function ManifestVersion(yamlJson: string | v2Sdl, networkType: NetworkType, networkId: NetworkId) {
const sdl = getSdl(yamlJson, networkType, networkId);
return sdl.manifestVersion();
}

export function ManifestYaml(sdlConfig: v2Sdl, networkType: NetworkType) {
const sdl = getSdl(sdlConfig, networkType);
export function ManifestYaml(sdlConfig: v2Sdl, networkType: NetworkType, networkId: NetworkId) {
const sdl = getSdl(sdlConfig, networkType, networkId);
return sdl.manifestSortedJSON();
}
78 changes: 34 additions & 44 deletions deploy-web/src/utils/deploymentData/v1beta2.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
import { CustomValidationError, ManifestVersion, getCurrentHeight, Manifest, DeploymentGroups, getSdl, parseSizeStr } from "./helpers";
import { CustomValidationError, DeploymentGroups, getCurrentHeight, getSdl, Manifest, ManifestVersion, parseSizeStr } from "./helpers";
import { defaultInitialDeposit } from "../constants";
import { stringToBoolean } from "../stringUtils";
import path from "path";
import yaml from "js-yaml";
import { SDL } from "@akashnetwork/akashjs/build/sdl";
import { getSelectedNetwork } from "@src/hooks/useSelectedNetwork";
import { NetworkId } from "@akashnetwork/akashjs/build/types/network";
import { nanoid } from "nanoid";

const endpointNameValidationRegex = /^[a-z]+[-_\da-z]+$/;
const endpointKindIP = "ip";

function validate(yamlStr: string, yamlJson) {
function validate(yamlStr: string, yamlJson, networkId: NetworkId) {
let sdl: SDL;
const pid = nanoid();
try {
SDL.validate(yamlStr);
sdl = getSdl(yamlJson, "beta3", networkId);
} catch (e) {
console.log("DEBUG e", pid, e);
throw new CustomValidationError(e.message);
}

const sdl = getSdl(yamlJson, "beta2");

// ENDPOINT VALIDATION
if (yamlJson.endpoints) {
Object.keys(yamlJson.endpoints).forEach(endpoint => {
const _endpoint = yamlJson.endpoints[endpoint];
if (!endpointNameValidationRegex.test(endpoint)) {
throw new CustomValidationError(`Endpoint named "${endpoint}" is not a valid name.`);
}

if (!_endpoint.kind) {
throw new CustomValidationError(`Endpoint named "${endpoint}" has no kind.`);
}

if (_endpoint.kind !== endpointKindIP) {
throw new CustomValidationError(`Endpoint named "${endpoint}" has an unknown kind "${_endpoint.kind}".`);
}
});
}

const endpointsUsed = {};
const portsUsed = {};
Object.keys(yamlJson.services).forEach(svcName => {
Expand All @@ -54,15 +37,15 @@ function validate(yamlStr: string, yamlJson) {
throw new CustomValidationError(`The placement "${placementName}" is not defined in the "placement" section.`);
}

const price = infra.pricing[svcdepl.profile];

if (!price) {
throw new CustomValidationError(`The pricing for the "${svcdepl.profile}" profile is not defined in the "${placementName}" "placement" definition.`);
}

if (!compute) {
throw new CustomValidationError(`The compute requirements for the "${svcdepl.profile}" profile are not defined in the "compute" section.`);
}
// const price = infra.pricing[svcdepl.profile];
//
// if (!price) {
// throw new CustomValidationError(`The pricing for the "${svcdepl.profile}" profile is not defined in the "${placementName}" "placement" definition.`);
// }
//
// if (!compute) {
// throw new CustomValidationError(`The compute requirements for the "${svcdepl.profile}" profile are not defined in the "compute" section.`);
// }

// LEASE IP VALIDATION
svc.expose?.forEach(expose => {
Expand Down Expand Up @@ -175,16 +158,17 @@ function validate(yamlStr: string, yamlJson) {
}
});
}
console.log("DEBUG done", pid);
}

export function getManifest(yamlJson, asString = false) {
const manifest = Manifest(yamlJson, "beta2", asString);

return manifest;
const { id: networkId } = getSelectedNetwork();
return Manifest(yamlJson, "beta2", networkId, asString);
}

export async function getManifestVersion(yamlJson, asString = false) {
const version = await ManifestVersion(yamlJson, "beta2");
const { id: networkId } = getSelectedNetwork();
const version = await ManifestVersion(yamlJson, "beta2", networkId);

if (asString) {
return Buffer.from(version).toString("base64");
Expand All @@ -201,14 +185,20 @@ export async function NewDeploymentData(
deposit = defaultInitialDeposit,
depositorAddress = null
) {
const { id: networkId } = getSelectedNetwork();
const yamlJson = yaml.load(yamlStr) as any;

// Validate the integrity of the yaml
validate(yamlStr, yamlJson);
try {
// Validate the integrity of the yaml
validate(yamlStr, yamlJson, networkId);
} catch (e) {
console.error(e);
throw e;
}

const groups = DeploymentGroups(yamlJson, "beta2");
const mani = Manifest(yamlJson, "beta2");
const ver = await ManifestVersion(yamlJson, "beta2");
const groups = DeploymentGroups(yamlJson, "beta2", networkId);
const mani = Manifest(yamlJson, "beta2", networkId);
const ver = await ManifestVersion(yamlJson, "beta2", networkId);
const id = {
owner: fromAddress,
dseq: dseq
Expand Down
Loading

0 comments on commit 700049a

Please sign in to comment.