diff --git a/package-lock.json b/package-lock.json index 555ae52..953be40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "license": "MIT", "dependencies": { "fastify": "^4.26.2", + "i18n-iso-countries": "^7.11.0", "ts-node": "^10.9.2" }, "bin": { @@ -7260,6 +7261,11 @@ "node": ">=8" } }, + "node_modules/diacritics": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/diacritics/-/diacritics-1.3.0.tgz", + "integrity": "sha512-wlwEkqcsaxvPJML+rDh/2iS824jbREk6DUMUKkEaSlxdYHeS43cClJtsWglvw2RfeXGm6ohKDqsXteJ5sP5enA==" + }, "node_modules/didyoumean": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.2.tgz", @@ -10221,6 +10227,17 @@ "node": ">=16.17.0" } }, + "node_modules/i18n-iso-countries": { + "version": "7.11.0", + "resolved": "https://registry.npmjs.org/i18n-iso-countries/-/i18n-iso-countries-7.11.0.tgz", + "integrity": "sha512-MP2+aAJwvBTuruaMEj+mPEhu4D9rn03GkkbuP8/xkvNzhVwNe2cAg1ivkL5Oj+vwqEwvIcf5C7Q+5Y/UZNLBHw==", + "dependencies": { + "diacritics": "1.3.0" + }, + "engines": { + "node": ">= 12" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index 6fd0228..b281fb8 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ }, "dependencies": { "fastify": "^4.26.2", + "i18n-iso-countries": "^7.11.0", "ts-node": "^10.9.2" } } diff --git a/src/cmd.ts b/src/cmd.ts index 9a22471..bb3d1a1 100644 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -196,6 +196,7 @@ function datasetCommand(yargs: yargs.Argv<{}>): { }, } ) + .command("getAllCountriesCallingCode", "Get all countries calling code") .command("isWinner", "The account is the dataset auditor winner", { datasetId: { description: "Dataset Id", @@ -553,6 +554,15 @@ async function dataset( path: String(argv.path), }) break + case "getDatasetState": + await new DatasetMetadatas().getDatasetState({ + context, + datasetId: Number(argv.datasetId), + }) + break + case "getAllCountriesCallingCode": + await new DatasetMetadatas().getAllCountriesCallingCode() + break case "submitDatasetProof": await new DatasetProofs().submitDatasetProof({ context, @@ -601,12 +611,6 @@ async function dataset( path: String(argv.path), }) break - case "getDatasetState": - await new DatasetMetadatas().getDatasetState({ - context, - datasetId: Number(argv.datasetId), - }) - break default: console.log("Unknown command.") } diff --git a/src/dataset/metadata/repo/index.ts b/src/dataset/metadata/repo/index.ts index f2c8b68..45a299a 100644 --- a/src/dataset/metadata/repo/index.ts +++ b/src/dataset/metadata/repo/index.ts @@ -19,6 +19,7 @@ ********************************************************************************/ import fs from "fs" +import * as countries from "i18n-iso-countries" import { DatasetState } from "@dataswapjs/dataswapjs" import { handleEvmError, logMethodCall } from "../../../shared/utils/utils" import { chainSuccessInterval, blockPeriod } from "../../../shared/constant" @@ -192,14 +193,13 @@ export class DatasetMetadatas { fs.readFileSync(options.path).toString() ) as DatasetReplicaRequirements - const state = await handleEvmError( - options.context.evm.datasetMetadata.getDatasetState( - datasetReplicaRequirements.datasetId - ) - ) - if (state != DatasetState.MetadataSubmitted) { - console.log("Dataset state is not MetadataSubmitted, do nothing~") - return true + if ( + !(await this.checkSubmissionRequirementsCriteria({ + context: options.context, + datasetReplicaRequirements, + })) + ) { + return false } options.context.evm.datasetRequirement @@ -211,8 +211,8 @@ export class DatasetMetadatas { datasetReplicaRequirements.dataPreparers, datasetReplicaRequirements.storageProviders, datasetReplicaRequirements.regions, - datasetReplicaRequirements.countrys, - datasetReplicaRequirements.citys, + datasetReplicaRequirements.countries, + datasetReplicaRequirements.cities, datasetReplicaRequirements.amount ) ) @@ -236,4 +236,37 @@ export class DatasetMetadatas { ) ) } + + @logMethodCall(["context"]) + async getAllCountriesCallingCode(): Promise<{ + [numericKey: string]: string + }> { + return countries.getNumericCodes() + } + + private async checkSubmissionRequirementsCriteria(options: { + context: Context + datasetReplicaRequirements: DatasetReplicaRequirements + }): Promise { + const state = await handleEvmError( + options.context.evm.datasetMetadata.getDatasetState( + options.datasetReplicaRequirements.datasetId + ) + ) + if (state != DatasetState.MetadataSubmitted) { + console.log("Dataset state is not MetadataSubmitted, do nothing~") + return false + } + for (const country of options.datasetReplicaRequirements.countries) { + if (!countries.isValid(Number(country))) { + console.log( + "Dataset replic requirements countries invalid:", + country + ) + return false + } + } + + return true + } } diff --git a/src/dataset/metadata/types/index.ts b/src/dataset/metadata/types/index.ts index 83c959d..bb5b3e7 100644 --- a/src/dataset/metadata/types/index.ts +++ b/src/dataset/metadata/types/index.ts @@ -46,7 +46,7 @@ export interface DatasetReplicaRequirements { dataPreparers: string[][] storageProviders: string[][] regions: bigint[] - countrys: bigint[] - citys: bigint[][] + countries: bigint[] + cities: bigint[][] amount: bigint } diff --git a/testdata/datasetReplicaRequirements.json b/testdata/datasetReplicaRequirements.json index c0c4c12..82b0ab1 100644 --- a/testdata/datasetReplicaRequirements.json +++ b/testdata/datasetReplicaRequirements.json @@ -60,14 +60,14 @@ 3474, 4211 ], - "countrys": [ - 517, - 1300, - 2040, - 3063, - 4687 + "countries": [ + 840, + 528, + 508, + 504, + 533 ], - "citys": [ + "cities": [ [ 202186, 1302254, @@ -95,5 +95,5 @@ ] ], "amount": 0, - "datasetId": 16 + "datasetId": 17 }