Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Can't list deployments #2701

Merged
merged 9 commits into from
May 21, 2024
1 change: 1 addition & 0 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface GqlBaseContract {
twinID: string;
state: string;
createdAt: string;
updatedAt: string;
solutionProviderID: string;
}

Expand Down
23 changes: 19 additions & 4 deletions packages/grid_client/src/modules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class BaseModule {
BaseModule.newContracts.push({
contractID: String(contract.contractId),
createdAt: Date.now().toString(),
updatedAt: Date.now().toString(),
deploymentData: contract.contractType.nodeContract.deploymentData,
deploymentHash: contract.contractType.nodeContract.deploymentHash,
gridVersion: "4",
Expand Down Expand Up @@ -179,15 +180,29 @@ class BaseModule {
if (fetch || !this.contracts) {
let contracts = await this.tfClient.contracts.listMyNodeContracts({
graphqlURL: this.config.graphqlURL,
type: modulesNames[this.moduleName],
type: modulesNames[this.moduleName] ?? this.moduleName,
projectName: this.projectName,
});

const alreadyFetchedContracts: GqlNodeContract[] = [];

for (const contract of BaseModule.newContracts) {
if (contract.parsedDeploymentData?.projectName !== this.projectName) continue;
if (!contract.parsedDeploymentData?.projectName.includes(this.projectName)) continue;
if (contract.parsedDeploymentData.type !== modulesNames[this.moduleName]) continue;
const c = contracts.filter(c => +c.contractID === +contract.contractID);
if (c.length > 0) {

const filteredContract = contracts.filter(c => c.contractID === contract.contractID);

const now = new Date();
const contractUpdatedAt = new Date(+contract.updatedAt);
const beforeOneMin = now.getTime() - contractUpdatedAt.getTime() < 1000 * 60;

if (filteredContract.length > 0) {
if (beforeOneMin) {
const idx = contracts.indexOf(filteredContract[0]);
contracts[idx] = contract;
continue;
}

alreadyFetchedContracts.push(contract);
continue;
}
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from "./farmerbot";
export * from "./farms";
export * from "./networks";
export * from "./bridge";
export * from "./base";
1 change: 1 addition & 0 deletions packages/grid_client/src/primitives/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ PersistentKeepalive = 25\nEndpoint = ${endpoint}`;
Network.newContracts.push({
contractID: String(AddedContract.contractId),
createdAt: Date.now().toString(),
updatedAt: Date.now().toString(),
deploymentData: AddedContract.contractType.nodeContract.deploymentData,
deploymentHash: AddedContract.contractType.nodeContract.deploymentHash,
gridVersion: "4",
Expand Down
59 changes: 56 additions & 3 deletions packages/playground/src/utils/migration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
import type { BaseModule } from "@threefold/grid_client/dist/es6/modules/base";
import type { Contract } from "@threefold/tfchain_client";
import { BaseModule } from "@threefold/grid_client";
import type { Contract, ExtrinsicResult } from "@threefold/tfchain_client";

/**
* `_migrateOldFullVMs` is a function is implemented to list all old `FullVM` that listed in the old `Playground` with type `Fullvm`
* @param `module` takes the `module` which is an instance of the `BaseModule`.
* @returns Applying the extrinsic in the chain with the batch request.
*/
async function _migrateOldFullVMs(module: BaseModule) {
const moduleName = module.moduleName;
module.moduleName = "Fullvm"; // Load old deployments that deployed with `Fullvm` type.
const deploymentNames = await module._list();
const contracts = await Promise.all(
deploymentNames.map(deploymentName => module.getDeploymentContracts(deploymentName)),
);

const loadUpdateExtrinsics = () => {
return Promise.all(
contracts.flat().map(contract => {
const oldData = JSON.parse(contract.deploymentData || "{}") as unknown as {
type: string;
name: string;
projectName: string;
};

if (oldData.type === "vm") {
return;
}

oldData.type = "vm";
contract.parsedDeploymentData = oldData;
contract.deploymentData = JSON.stringify(oldData);
contract.updatedAt = Date.now().toString();

BaseModule.newContracts.push(contract);

return module.tfClient.contracts.updateNode({
id: +contract.contractID,
data: contract.deploymentData,
hash: contract.deploymentHash,
});
}),
);
};

module.moduleName = moduleName;
if (deploymentNames.length) {
const extrinsics = await loadUpdateExtrinsics().then(exts => exts.filter(Boolean) as ExtrinsicResult<Contract>[]);
return module.tfClient.applyAllExtrinsics<Contract>(extrinsics);
}
}

export async function migrateModule(module: BaseModule) {
await module._list(); // force grid_client migration
// To list only `Full` and `Micro` VMs.
if (module.moduleName === "machines") {
await _migrateOldFullVMs(module);
}

await module._list(); // force grid_client migration
const path = module.getNewDeploymentPath();
const keys = await module.backendStorage.list(path);

Expand Down
Loading