diff --git a/.github/workflows/grid_client_nightly.yml b/.github/workflows/grid_client_nightly.yml index 2fb4ea34c8..1cd38c2d4e 100644 --- a/.github/workflows/grid_client_nightly.yml +++ b/.github/workflows/grid_client_nightly.yml @@ -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: diff --git a/packages/grid_client/README.md b/packages/grid_client/README.md index d8856dc1f4..e5c12911c7 100644 --- a/packages/grid_client/README.md +++ b/packages/grid_client/README.md @@ -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 diff --git a/packages/grid_client/src/client.ts b/packages/grid_client/src/client.ts index f10a37eac5..8e0641c3fc 100644 --- a/packages/grid_client/src/client.ts +++ b/packages/grid_client/src/client.ts @@ -14,9 +14,9 @@ import { BackendStorage, BackendStorageType } from "./storage/backend"; import { KeypairType } from "./zos/deployment"; class GridClient { - static config: GridClientConfig; - static rmbClients: Record = {}; + static rmbClients: Map = new Map(); static connecting = new Set(); + config: GridClientConfig; rmbClient: RMBClient; tfclient: TFClient; machines: modules.machines; @@ -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, @@ -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, @@ -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); } } diff --git a/packages/grid_client/src/config.ts b/packages/grid_client/src/config.ts index 0346e3138d..1da919263d 100644 --- a/packages/grid_client/src/config.ts +++ b/packages/grid_client/src/config.ts @@ -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"; @@ -17,6 +18,7 @@ class GridClientConfig { mnemonic: string; storeSecret: string | Uint8Array; rmbClient: RMBClient; + tfclient: TFClient; projectName: string; backendStorageType: BackendStorageType; backendStorage: BackendStorageInterface; diff --git a/packages/grid_client/src/high_level/base.ts b/packages/grid_client/src/high_level/base.ts index 33862e6eaa..715233a513 100644 --- a/packages/grid_client/src/high_level/base.ts +++ b/packages/grid_client/src/high_level/base.ts @@ -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; diff --git a/packages/grid_client/src/high_level/farmerbot.ts b/packages/grid_client/src/high_level/farmerbot.ts index 97ff256527..a1f2029949 100644 --- a/packages/grid_client/src/high_level/farmerbot.ts +++ b/packages/grid_client/src/high_level/farmerbot.ts @@ -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); } diff --git a/packages/grid_client/src/high_level/twinDeploymentHandler.ts b/packages/grid_client/src/high_level/twinDeploymentHandler.ts index b7ea5e5785..27c72a39d7 100644 --- a/packages/grid_client/src/high_level/twinDeploymentHandler.ts +++ b/packages/grid_client/src/high_level/twinDeploymentHandler.ts @@ -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); @@ -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 }); @@ -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) { diff --git a/packages/grid_client/src/modules/algorand.ts b/packages/grid_client/src/modules/algorand.ts index 068ba20670..fba1d54e87 100644 --- a/packages/grid_client/src/modules/algorand.ts +++ b/packages/grid_client/src/modules/algorand.ts @@ -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) { diff --git a/packages/grid_client/src/modules/balance.ts b/packages/grid_client/src/modules/balance.ts index f586474c3a..1dd8befe85 100644 --- a/packages/grid_client/src/modules/balance.ts +++ b/packages/grid_client/src/modules/balance.ts @@ -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 diff --git a/packages/grid_client/src/modules/base.ts b/packages/grid_client/src/modules/base.ts index ec1544026e..3a956c373c 100644 --- a/packages/grid_client/src/modules/base.ts +++ b/packages/grid_client/src/modules/base.ts @@ -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; } newGetDeploymentPath(...paths: string[]): string { diff --git a/packages/grid_client/src/modules/calculator.ts b/packages/grid_client/src/modules/calculator.ts index 9c4668a5bd..b6a975ad92 100644 --- a/packages/grid_client/src/modules/calculator.ts +++ b/packages/grid_client/src/modules/calculator.ts @@ -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 diff --git a/packages/grid_client/src/modules/capacity.ts b/packages/grid_client/src/modules/capacity.ts index 5960a84a6a..1c3e590e0d 100644 --- a/packages/grid_client/src/modules/capacity.ts +++ b/packages/grid_client/src/modules/capacity.ts @@ -11,6 +11,7 @@ import { FarmsGetModel, FilterOptions, NodeFreeResourcesModel, + NodeIdFromContractIdModel, NodesByFarmIdModel, NodesGetModel, } from "./models"; @@ -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 }; diff --git a/packages/grid_client/src/modules/contracts.ts b/packages/grid_client/src/modules/contracts.ts index db4914af40..3d1118d68f 100644 --- a/packages/grid_client/src/modules/contracts.ts +++ b/packages/grid_client/src/modules/contracts.ts @@ -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); } diff --git a/packages/grid_client/src/modules/farmerbot.ts b/packages/grid_client/src/modules/farmerbot.ts index 96cf27c98e..9ab126e4db 100644 --- a/packages/grid_client/src/modules/farmerbot.ts +++ b/packages/grid_client/src/modules/farmerbot.ts @@ -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); } diff --git a/packages/grid_client/src/modules/farms.ts b/packages/grid_client/src/modules/farms.ts index b358cdd388..5aeb3cf743 100644 --- a/packages/grid_client/src/modules/farms.ts +++ b/packages/grid_client/src/modules/farms.ts @@ -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 diff --git a/packages/grid_client/src/modules/kvstore.ts b/packages/grid_client/src/modules/kvstore.ts index 082090c016..aae63cd9ad 100644 --- a/packages/grid_client/src/modules/kvstore.ts +++ b/packages/grid_client/src/modules/kvstore.ts @@ -7,8 +7,8 @@ import { checkBalance } from "./utils"; class KVStore { client: TFClient; - constructor(config: GridClientConfig) { - this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType); + constructor(public config: GridClientConfig) { + this.client = config.tfclient; } @expose @validateInput diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 04a3d0749a..a8c02acdda 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -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 @@ -722,6 +726,7 @@ export { NodesByFarmIdModel, NodeFreeResourcesModel, FarmIdFromFarmNameModel, + NodeIdFromContractIdModel, CapacityPoolCheckModel, FilterOptions, FarmFilterOptions, diff --git a/packages/grid_client/src/modules/nodes.ts b/packages/grid_client/src/modules/nodes.ts index c4edfa1c31..fd5a9a2887 100644 --- a/packages/grid_client/src/modules/nodes.ts +++ b/packages/grid_client/src/modules/nodes.ts @@ -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 diff --git a/packages/grid_client/src/modules/stellar.ts b/packages/grid_client/src/modules/stellar.ts index 0f071a51c2..69b77559fd 100644 --- a/packages/grid_client/src/modules/stellar.ts +++ b/packages/grid_client/src/modules/stellar.ts @@ -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) { diff --git a/packages/grid_client/src/modules/tfchain.ts b/packages/grid_client/src/modules/tfchain.ts index 1535b01035..5d7a2b1400 100644 --- a/packages/grid_client/src/modules/tfchain.ts +++ b/packages/grid_client/src/modules/tfchain.ts @@ -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() { diff --git a/packages/grid_client/src/modules/twins.ts b/packages/grid_client/src/modules/twins.ts index ce4efbd0e8..b0ef1805ef 100644 --- a/packages/grid_client/src/modules/twins.ts +++ b/packages/grid_client/src/modules/twins.ts @@ -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 diff --git a/packages/grid_client/src/modules/utility.ts b/packages/grid_client/src/modules/utility.ts index 5b5fd0bb92..33979b6294 100644 --- a/packages/grid_client/src/modules/utility.ts +++ b/packages/grid_client/src/modules/utility.ts @@ -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 diff --git a/packages/grid_client/src/modules/utils.ts b/packages/grid_client/src/modules/utils.ts index 240fe7b8ee..08c0a5965d 100644 --- a/packages/grid_client/src/modules/utils.ts +++ b/packages/grid_client/src/modules/utils.ts @@ -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(); if (balances["free"] < 0.001) { throw Error("Balance is not enough to apply an extrinsic"); } diff --git a/packages/grid_client/src/modules/zos.ts b/packages/grid_client/src/modules/zos.ts index 8d66b16941..4521b03106 100644 --- a/packages/grid_client/src/modules/zos.ts +++ b/packages/grid_client/src/modules/zos.ts @@ -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); diff --git a/packages/grid_client/src/primitives/deployment.ts b/packages/grid_client/src/primitives/deployment.ts index 9693aa0b07..b3ffff21cf 100644 --- a/packages/grid_client/src/primitives/deployment.ts +++ b/packages/grid_client/src/primitives/deployment.ts @@ -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) { diff --git a/packages/grid_client/src/primitives/network.ts b/packages/grid_client/src/primitives/network.ts index bd30e85746..f30b61edbe 100644 --- a/packages/grid_client/src/primitives/network.ts +++ b/packages/grid_client/src/primitives/network.ts @@ -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) { diff --git a/packages/grid_client/src/primitives/nodes.ts b/packages/grid_client/src/primitives/nodes.ts index eec2300530..d20eeac6e6 100644 --- a/packages/grid_client/src/primitives/nodes.ts +++ b/packages/grid_client/src/primitives/nodes.ts @@ -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"; @@ -129,13 +130,8 @@ class Nodes { return accessNodes; } - async getNodeIdFromContractId(contractId: number, mnemonic: string): Promise { - const tfclient = new TFClient( - GridClient.config.substrateURL, - mnemonic, - GridClient.config.storeSecret, - GridClient.config.keypairType, - ); + async getNodeIdFromContractId(contractId: number, substrateURL: string): Promise { + const tfclient = new QueryClient(substrateURL); return tfclient.contracts .get({ id: contractId }) .then(contract => { diff --git a/packages/playground/src/components/deployment_data_dialog.vue b/packages/playground/src/components/deployment_data_dialog.vue index 442b3d83f6..927cc9519f 100644 --- a/packages/playground/src/components/deployment_data_dialog.vue +++ b/packages/playground/src/components/deployment_data_dialog.vue @@ -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);