Skip to content

Commit

Permalink
Merge pull request #1161 from threefoldtech/development_gridclient_ad…
Browse files Browse the repository at this point in the history
…justments

Some adjustments in grid client
  • Loading branch information
AhmedHanafy725 authored Sep 28, 2023
2 parents 10b3dca + 712bdc5 commit 60f6903
Show file tree
Hide file tree
Showing 28 changed files with 54 additions and 71 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/grid_client_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ jobs:
- uses: actions/checkout@v2
if: ${{ env.NETWORK == 'qa' }}
with:
ref: refs/tags/v2.0.0
ref: refs/tags/v2.1.1
- uses: actions/checkout@v2
if: ${{ env.NETWORK == 'test' }}
with:
ref: refs/tags/v2.0.0
ref: refs/tags/v2.1.1
- uses: actions/checkout@v2
if: ${{ env.NETWORK == 'main' }}
with:
ref: refs/tags/v2.0.0
ref: refs/tags/v2.1.1
- name: Set up node 18
uses: actions/setup-node@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions packages/grid_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ grid_client is a client used for deploying workloads (VMs, ZDBs, k8s, etc.) on g

## Installation

> **Warning**: For **Qanet**, Please use @2.0.0 version
> **Warning**: For **Qanet**, Please use @2.1.1 version
> **Warning**: For **Testnet**, Please use @2.0.0 version
> **Warning**: For **Testnet**, Please use @2.1.1 version
> **Warning**: For **Mainnet**, Please use @2.0.0 version
> **Warning**: For **Mainnet**, Please use @2.1.1 version
### External package

Expand Down
11 changes: 6 additions & 5 deletions packages/grid_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { BackendStorage, BackendStorageType } from "./storage/backend";
import { KeypairType } from "./zos/deployment";

class GridClient {
static config: GridClientConfig;
static rmbClients: Record<string, RMBClient> = {};
static rmbClients: Map<string, RMBClient> = new Map();
static connecting = new Set<string>();
config: GridClientConfig;
rmbClient: RMBClient;
tfclient: TFClient;
machines: modules.machines;
Expand All @@ -43,7 +43,7 @@ class GridClient {
networks: modules.networks;
modules: string[] = [];

constructor(public clientOptions?: ClientOptions) {
constructor(public clientOptions: ClientOptions) {
this.clientOptions = {
mnemonic: clientOptions.mnemonic,
network: clientOptions.network,
Expand Down Expand Up @@ -135,11 +135,12 @@ class GridClient {
_connect(): void {
const urls = this.getDefaultUrls(this.clientOptions.network);
const storePath = PATH.join(appPath, this.clientOptions.network, String(this.twinId));
GridClient.config = {
this.config = {
network: this.clientOptions.network,
mnemonic: this.clientOptions.mnemonic,
storeSecret: this.clientOptions.storeSecret,
rmbClient: this.rmbClient,
tfclient: this.tfclient,
projectName: this.clientOptions.projectName,
backendStorageType: this.clientOptions.backendStorageType,
backendStorage: this.clientOptions.backendStorage,
Expand All @@ -157,7 +158,7 @@ class GridClient {
if (module.includes("Model")) {
continue;
}
this[module] = new modules[module](GridClient.config);
this[module] = new modules[module](this.config);
this.modules.push(module);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/grid_client/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client as RMBClient } from "@threefold/rmb_direct_client";

import { TFClient } from "./clients/tf-grid/client";
import { BackendStorageType } from "./storage/backend";
import BackendStorageInterface from "./storage/BackendStorageInterface";
import { KeypairType } from "./zos/deployment";
Expand All @@ -17,6 +18,7 @@ class GridClientConfig {
mnemonic: string;
storeSecret: string | Uint8Array;
rmbClient: RMBClient;
tfclient: TFClient;
projectName: string;
backendStorageType: BackendStorageType;
backendStorage: BackendStorageInterface;
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/high_level/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class HighLevelBase {
throw Error("Network workload can't be deleted");
}
let twinDeployments: TwinDeployment[] = [];
const node_id = await this.nodes.getNodeIdFromContractId(deployment.contract_id, this.config.mnemonic);
const node_id = await this.nodes.getNodeIdFromContractId(deployment.contract_id, this.config.substrateURL);
const deploymentFactory = new DeploymentFactory(this.config);

const numberOfWorkloads = deployment.workloads.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/high_level/farmerbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FarmerBot {
client: TFClient;

constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
this.rmb = new RMB(config.rmbClient);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/grid_client/src/high_level/twinDeploymentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TwinDeploymentHandler {
nodes: Nodes;

constructor(public config: GridClientConfig) {
this.tfclient = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.tfclient = config.tfclient;
this.deploymentFactory = new DeploymentFactory(this.config);
this.rmb = new RMB(config.rmbClient);
this.nodes = new Nodes(this.config.graphqlURL, this.config.proxyURL, this.config.rmbClient);
Expand Down Expand Up @@ -59,7 +59,7 @@ class TwinDeploymentHandler {
}

async getDeployment(contract_id: number) {
const node_id = await this.nodes.getNodeIdFromContractId(contract_id, this.config.mnemonic);
const node_id = await this.nodes.getNodeIdFromContractId(contract_id, this.config.substrateURL);
const node_twin_id = await this.nodes.getNodeTwinId(node_id);

const payload = JSON.stringify({ contract_id: contract_id });
Expand All @@ -83,7 +83,7 @@ class TwinDeploymentHandler {

async waitForDeployment(twinDeployment: TwinDeployment, timeout = this.config.deploymentTimeoutMinutes) {
const contract_id = twinDeployment.deployment.contract_id;
const node_id = await this.nodes.getNodeIdFromContractId(contract_id, this.config.mnemonic);
const node_id = await this.nodes.getNodeIdFromContractId(contract_id, this.config.substrateURL);

const now = new Date().getTime();
while (new Date().getTime() < now + timeout * 1000 * 60) {
Expand Down
7 changes: 1 addition & 6 deletions packages/grid_client/src/modules/algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ class Algorand implements blockchainInterface {
config.backendStorage,
config.seed,
);
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
this.tfClient = config.tfclient;
}

private async saveIfKVStoreBackend(extrinsics) {
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { checkBalance } from "./utils";
class Balance {
client: TFClient;
constructor(config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}
@expose
@validateInput
Expand Down
7 changes: 1 addition & 6 deletions packages/grid_client/src/modules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ class BaseModule {
config.backendStorage,
config.seed,
);
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
this.tfClient = config.tfclient;
}

getDeploymentPath(name: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Calculator {
client: TFClient;

constructor(config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}
@expose
@validateInput
Expand Down
7 changes: 7 additions & 0 deletions packages/grid_client/src/modules/capacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
FarmsGetModel,
FilterOptions,
NodeFreeResourcesModel,
NodeIdFromContractIdModel,
NodesByFarmIdModel,
NodesGetModel,
} from "./models";
Expand Down Expand Up @@ -138,6 +139,12 @@ class Capacity {
options.nodeId,
);
}

@expose
@validateInput
async getNodeIdFromContractId(options: NodeIdFromContractIdModel) {
return await this.nodes.getNodeIdFromContractId(options.contractId, this.config.substrateURL);
}
}

export { Capacity as capacity };
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Contracts {
client: TFClient;
nodes: Nodes;
constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
this.nodes = new Nodes(config.graphqlURL, config.proxyURL, config.rmbClient);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/farmerbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Farmerbot {
rmb: RMB;

constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
this.farmerBot = new FarmerBot(this.config);
this.rmb = new RMB(config.rmbClient);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Farms {
client: TFClient;

constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}

@expose
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/kvstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { checkBalance } from "./utils";
class KVStore {
client: TFClient;
constructor(config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}
@expose
@validateInput
Expand Down
5 changes: 5 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ class NodeFreeResourcesModel {
class FarmIdFromFarmNameModel {
@Expose() @IsString() @IsNotEmpty() farmName: string;
}

class NodeIdFromContractIdModel {
@Expose() @IsInt() @Min(1) contractId: number;
}
class CapacityPoolCheckModel {
@Expose() @IsInt() @Min(1) nodeId: number;
@Expose() @IsInt({ each: true }) @Min(250 * 1024 ** 2, { each: true }) rootfsDisks: number[]; //Byte
Expand Down Expand Up @@ -722,6 +726,7 @@ export {
NodesByFarmIdModel,
NodeFreeResourcesModel,
FarmIdFromFarmNameModel,
NodeIdFromContractIdModel,
CapacityPoolCheckModel,
FilterOptions,
FarmFilterOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { checkBalance } from "./utils";
class Nodes {
client: TFClient;
constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}

@expose
Expand Down
7 changes: 1 addition & 6 deletions packages/grid_client/src/modules/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ class Stellar implements blockchainInterface {
config.backendStorage,
config.seed,
);
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
this.tfClient = config.tfclient;
}

private async saveIfKVStoreBackend(extrinsics) {
Expand Down
7 changes: 1 addition & 6 deletions packages/grid_client/src/modules/tfchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ class TFChain implements blockchainInterface {
this.storeSecret = config.storeSecret as string;
this.keypairType = config.keypairType;
this.network = config.network;
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
this.tfClient = config.tfclient;
}

getPath() {
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/twins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { checkBalance } from "./utils";
class Twins {
client: TFClient;
constructor(config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}

@expose
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Utility {
client: TFClient;

constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.client = config.tfclient;
}

@expose
Expand Down
4 changes: 1 addition & 3 deletions packages/grid_client/src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { TFClient } from "../clients/tf-grid/client";
function checkBalance(target, propertyKey: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function (...args) {
const { substrateURL, mnemonic, storeSecret, keypairType } = GridClient.config;
const tfclient = new TFClient(substrateURL, mnemonic, storeSecret, keypairType);
const balances = await tfclient.balances.getMyBalance();
const balances = await this.config.tfclient.balances.getMyBalance();

Check failure on line 8 in packages/grid_client/src/modules/utils.ts

View workflow job for this annotation

GitHub Actions / JEST Tests

kvstore.test.ts ► TC1238 - KVStore: Use TFChain KVStore ► TC1238 - KVStore: Use TFChain KVStore

Failed test found in: packages/grid_client/tests/test-reports/report.xml Error: TypeError: Cannot read properties of undefined (reading 'tfclient')
Raw output
TypeError: Cannot read properties of undefined (reading 'tfclient')
    at KVStore.tfclient (/home/runner/work/tfgrid-sdk-ts/tfgrid-sdk-ts/packages/grid_client/src/modules/utils.ts:8:40)
    at KVStore.apply (/home/runner/work/tfgrid-sdk-ts/tfgrid-sdk-ts/packages/grid_client/src/helpers/validator.ts:23:25)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Object.<anonymous> (/home/runner/work/tfgrid-sdk-ts/tfgrid-sdk-ts/packages/grid_client/tests/modules/kvstore.test.ts:40:18)
if (balances["free"] < 0.001) {
throw Error("Balance is not enough to apply an extrinsic");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/modules/zos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Zos {
@expose
@validateInput
async getDeployment(options: ZOSGetDeploymentModel) {
const nodeId = await this.capacity.getNodeIdFromContractId(options.contractId, this.config.mnemonic);
const nodeId = await this.capacity.getNodeIdFromContractId(options.contractId, this.config.substrateURL);
const nodeTwinId = await this.capacity.getNodeTwinId(nodeId);
const payload = JSON.stringify({ contract_id: options.contractId });
return await this.rmb.request([nodeTwinId], "zos.deployment.get", payload);
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/src/primitives/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DeploymentFactory {
// Don't change the machine ip
if (w.type === WorkloadTypes.zmachine) {
const nodes = new Nodes(this.config.graphqlURL, this.config.proxyURL, this.config.rmbClient);
const node_id = await nodes.getNodeIdFromContractId(oldDeployment.contract_id, this.config.mnemonic);
const node_id = await nodes.getNodeIdFromContractId(oldDeployment.contract_id, this.config.substrateURL);
const oldIp = workload.data["network"]["interfaces"][0]["ip"];
const newIp = w.data["network"]["interfaces"][0]["ip"];
if (newIp !== oldIp) {
Expand Down
7 changes: 1 addition & 6 deletions packages/grid_client/src/primitives/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ class Network {
);
this.rmb = new RMB(config.rmbClient);
this.capacity = new Nodes(this.config.graphqlURL, this.config.proxyURL, this.config.rmbClient);
this.tfClient = new TFClient(
this.config.substrateURL,
this.config.mnemonic,
this.config.storeSecret,
this.config.keypairType,
);
this.tfClient = config.tfclient;
}

private async saveIfKVStoreBackend(extrinsics) {
Expand Down
10 changes: 3 additions & 7 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client as RMBClient } from "@threefold/rmb_direct_client";
import { QueryClient } from "@threefold/tfchain_client";
import { default as PrivateIp } from "private-ip";
import urlJoin from "url-join";

Expand Down Expand Up @@ -129,13 +130,8 @@ class Nodes {
return accessNodes;
}

async getNodeIdFromContractId(contractId: number, mnemonic: string): Promise<number> {
const tfclient = new TFClient(
GridClient.config.substrateURL,
mnemonic,
GridClient.config.storeSecret,
GridClient.config.keypairType,
);
async getNodeIdFromContractId(contractId: number, substrateURL: string): Promise<number> {
const tfclient = new QueryClient(substrateURL);
return tfclient.contracts
.get({ id: contractId })
.then(contract => {
Expand Down
7 changes: 3 additions & 4 deletions packages/playground/src/components/deployment_data_dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ async function getGPUInfo() {
const grid = await getGrid(profileManager.profile!);
if (grid) {
const nodeId = await grid.zos.capacity.getNodeIdFromContractId(
contract.value.contractId,
profileManager.profile!.mnemonic,
);
const nodeId = await grid.capacity.getNodeIdFromContractId({
contractId: contract.value.contractId,
});
const gpuCards = await grid.zos.getNodeGPUInfo({ nodeId });
const usedCards = gpuCards?.filter((card: any) => card.contract == contract.value.contractId);
Expand Down

0 comments on commit 60f6903

Please sign in to comment.