Skip to content

Commit

Permalink
feat(sdl): move denom validation from cloudmos to sdl (#133)
Browse files Browse the repository at this point in the history
As part of this:
- run validation on the class init
- add deprecation warning to static validation method
- move along necessary deps types and deps from cloudmos
  • Loading branch information
ygrishajev committed May 3, 2024
1 parent 7de1540 commit 245f7a9
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"scope-enum": [2, "always", ["certificates", "network", "wallet", "api", "stargate"]]
"scope-enum": [2, "always", ["certificates", "network", "wallet", "api", "stargate", "sdl"]]
}
}
11 changes: 11 additions & 0 deletions src/config/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MainnetNetworkId, NetworkId, SandboxNetworkId, TestnetNetworkId } from "../types/network";

export const MAINNET_ID: MainnetNetworkId = "mainnet";
export const SANDBOX_ID: SandboxNetworkId = "sandbox";
export const TESTNET_ID: TestnetNetworkId = "testnet";

export const USDC_IBC_DENOMS: Record<NetworkId, string> = {
[MAINNET_ID]: "ibc/170C677610AC31DF0904FFE09CD3B5C657492170E7E52372E48756B71E56F2F1",
[SANDBOX_ID]: "ibc/12C6A0C374171B595A0A9E18B83FA09D295FB1F2D8C6DAA3AC28683471752D84",
[TESTNET_ID]: ""
};
12 changes: 12 additions & 0 deletions src/error/CustomValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class CustomValidationError extends Error {
static assert(condition: boolean, message: string): asserts condition {
if (!condition) {
throw new CustomValidationError(message);
}
}

constructor(message: string) {
super(message);
this.name = "CustomValidationError";
}
}
1 change: 1 addition & 0 deletions src/error/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./CustomValidationError";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * as rpc from "./rpc";

export * as protoclient from "./pbclient/pbclient";
export * as sdl from "./sdl";
export * from "./error";
55 changes: 41 additions & 14 deletions src/sdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import {
import { convertCpuResourceString, convertResourceString } from "./sizes";
import { default as stableStringify } from "json-stable-stringify";
import crypto from "node:crypto";
import { MAINNET_ID, USDC_IBC_DENOMS } from "../config/network";
import { NetworkId } from "../types/network";
import { CustomValidationError } from "../error/CustomValidationError";

const Endpoint_SHARED_HTTP = 0;
const Endpoint_RANDOM_PORT = 1;
Expand All @@ -51,28 +54,20 @@ function isString(str: any): str is string {
type NetworkVersion = "beta2" | "beta3";

export class SDL {
data: v2Sdl;
version: NetworkVersion;

constructor(data: v2Sdl, version: NetworkVersion = "beta2") {
this.data = data;
this.version = version;
}

static fromString(yaml: string, version: NetworkVersion = "beta2") {
const data = SDL.validate(yaml) as v2Sdl;

return new SDL(data, version);
static fromString(yaml: string, version: NetworkVersion = "beta2", networkId: NetworkId = MAINNET_ID) {
const data = YAML.load(yaml) as v3Sdl;
return new SDL(data, version, networkId);
}

static validate(yaml: string) {
console.warn("SDL.validate is deprecated. Use SDL.constructor directly.");
// TODO: this should really be cast to unknown, then assigned
// to v2 or v3 SDL only after being validated
const data = YAML.load(yaml) as v3Sdl;

for (const [name, profile] of Object.entries(data.profiles.compute)) {
SDL.validateGPU(name, profile.resources.gpu);
SDL.validateStorage(name, profile.resources.storage);
this.validateGPU(name, profile.resources.gpu);
this.validateStorage(name, profile.resources.storage);
}

return data;
Expand Down Expand Up @@ -142,6 +137,38 @@ export class SDL {
}
}

constructor(
public readonly data: v2Sdl,
public readonly version: NetworkVersion = "beta2",
private readonly networkId: NetworkId = MAINNET_ID
) {
this.validate();
}

private validate() {
// TODO: this should really be cast to unknown, then assigned
// to v2 or v3 SDL only after being validated
const v3data = this.data as v3Sdl;
Object.entries(v3data.profiles.compute).forEach(([name, { resources }]) => {
if ("gpu" in resources) {
SDL.validateGPU(name, resources.gpu);
}
SDL.validateStorage(name, resources.storage);
});

this.validateDenom();
}

private validateDenom() {
const usdcDenom = USDC_IBC_DENOMS[this.networkId];
const denoms = this.groups()
.flatMap(g => g.resources)
.map(resource => resource.price.denom);
const invalidDenom = denoms.find(denom => denom !== "uakt" && denom !== usdcDenom);

CustomValidationError.assert(!invalidDenom, `Invalid denom: "${invalidDenom}". Only uakt and ${usdcDenom} are supported.`);
}

services() {
if (this.data) {
return this.data.services;
Expand Down
4 changes: 4 additions & 0 deletions src/types/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type MainnetNetworkId = "mainnet";
export type TestnetNetworkId = "testnet";
export type SandboxNetworkId = "sandbox";
export type NetworkId = MainnetNetworkId | TestnetNetworkId | SandboxNetworkId;

0 comments on commit 245f7a9

Please sign in to comment.