From d2c93f7fa3d5420b322e661afdc637f4c7f4e455 Mon Sep 17 00:00:00 2001 From: waynewyang Date: Tue, 2 Jan 2024 14:16:11 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20basic=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 9 +++ package-lock.json | 12 +++ package.json | 2 + src/backgroundTask/context/index.ts | 30 ++++---- src/backgroundTask/decoder/index.ts | 102 +++++++++++++------------- src/backgroundTask/interface/index.ts | 52 +++++++------ src/config/backgroundTask.ts | 7 +- src/config/evm.ts | 29 ++++++++ test/backgroundTask/index.spec.ts | 17 +++++ tsconfig.json | 23 +++--- 10 files changed, 180 insertions(+), 103 deletions(-) create mode 100644 .env create mode 100644 src/config/evm.ts create mode 100644 test/backgroundTask/index.spec.ts diff --git a/.env b/.env new file mode 100644 index 0000000..38d0465 --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +CALIBRATION_LOTUS_PROVIDER_URL = "https://api.calibration.node.glif.io/rpc/v1" +CALIBRATION_LOTUS_API_ENDPOINT = "https://api.calibration.node.glif.io/rpc/v1" +CALIBRATION_LOTUS_TOKEN = "" +CALIBRATION_MONGO_URL = "mongodb://127.0.0.1:27017/messagehub_calibration" + +MAIN_LOTUS_PROVIDER_URL = "https://api.node.glif.io/rpc/v1" +MAIN_LOTUS_API_ENDPOINT = "https://api.node.glif.io/rpc/v1" +MAIN_LOTUS_TOKEN = "" +MAIN_MONGO_URL = "mongodb://127.0.0.1:27017/messagehub_main" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 02547f3..a9a36b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,12 +23,14 @@ "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", "@release-it/conventional-changelog": "^8.0.1", + "@types/dotenv": "^8.2.0", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/supertest": "^2.0.12", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", + "dotenv": "^16.3.1", "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", @@ -6191,6 +6193,16 @@ "integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==", "dev": true }, + "node_modules/@types/dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==", + "deprecated": "This is a stub types definition. dotenv provides its own type definitions, so you do not need this installed.", + "dev": true, + "dependencies": { + "dotenv": "*" + } + }, "node_modules/@types/eslint": { "version": "8.56.0", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.0.tgz", diff --git a/package.json b/package.json index afac7d1..a6c2ff3 100644 --- a/package.json +++ b/package.json @@ -52,12 +52,14 @@ "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", "@release-it/conventional-changelog": "^8.0.1", + "@types/dotenv": "^8.2.0", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/supertest": "^2.0.12", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", + "dotenv": "^16.3.1", "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", diff --git a/src/backgroundTask/context/index.ts b/src/backgroundTask/context/index.ts index cb8658b..20a430f 100644 --- a/src/backgroundTask/context/index.ts +++ b/src/backgroundTask/context/index.ts @@ -52,9 +52,9 @@ export interface Config { * Represents a connection to a Filecoin network. */ export class Context implements IContext { - chain: ChainContext; - evm: EvmContext; - datastore: DatastoreContext; + chain: ChainContext = {} as ChainContext; + evm: EvmContext = {} as EvmContext; + datastore: DatastoreContext = {} as DatastoreContext; /** * Creates an instance of ChainNetwork. @@ -87,17 +87,17 @@ export class Context implements IContext { }, }); - this.evm.roles = config.evm.roles; - this.evm.filplus = config.evm.filplus; - this.evm.escrow = config.evm.escrow; - this.evm.dataset.metadata = config.evm.dataset.metadata; - this.evm.dataset.requirement = config.evm.dataset.requirement; - this.evm.dataset.proof = config.evm.dataset.proof; - this.evm.dataset.challenge = config.evm.dataset.challenge; - this.evm.matching.metadata = config.evm.matching.metadata; - this.evm.matching.target = config.evm.matching.target; - this.evm.matching.bids = config.evm.matching.bids; - this.evm.storages = config.evm.storages; - this.evm.datacaps = config.evm.datacaps; + // this.evm.roles = config.evm.roles; + // this.evm.filplus = config.evm.filplus; + // this.evm.escrow = config.evm.escrow; + this.evm.datasetMetadata = config.evm.datasetMetadata; + // this.evm.dataset.requirement = config.evm.dataset.requirement; + // this.evm.dataset.proof = config.evm.dataset.proof; + // this.evm.dataset.challenge = config.evm.dataset.challenge; + // this.evm.matching.metadata = config.evm.matching.metadata; + // this.evm.matching.target = config.evm.matching.target; + // this.evm.matching.bids = config.evm.matching.bids; + // this.evm.storages = config.evm.storages; + // this.evm.datacaps = config.evm.datacaps; } } diff --git a/src/backgroundTask/decoder/index.ts b/src/backgroundTask/decoder/index.ts index 032b74e..e4b63ee 100644 --- a/src/backgroundTask/decoder/index.ts +++ b/src/backgroundTask/decoder/index.ts @@ -47,59 +47,59 @@ export class Decoder implements IDecoder { pendingChainInfo.messages.forEach((msg) => { //TODO:To check address format ,add toEthaddress function switch (msg.Msg.To) { - case this.context.evm.dataset.metadata.getContractAddress(): - res.push(this.context.evm.dataset.metadata.decodeMessage(msg).data); + case this.context.evm.datasetMetadata.getContractAddress(): + res.push(this.context.evm.datasetMetadata.decodeMessage(msg).data); break; - case this.context.evm.dataset.requirement.getContractAddress(): - res.push( - this.context.evm.dataset.requirement.decodeMessage(msg).data, - ); - break; - - case this.context.evm.dataset.proof.getContractAddress(): - res.push(this.context.evm.dataset.proof.decodeMessage(msg).data); - break; - - case this.context.evm.dataset.challenge.getContractAddress(): - res.push( - this.context.evm.dataset.challenge.decodeMessage(msg).data, - ); - break; - - case this.context.evm.matching.target.getContractAddress(): - res.push(this.context.evm.matching.target.decodeMessage(msg).data); - break; - - case this.context.evm.matching.metadata.getContractAddress(): - res.push( - this.context.evm.matching.metadata.decodeMessage(msg).data, - ); - break; - - case this.context.evm.matching.bids.getContractAddress(): - res.push(this.context.evm.matching.bids.decodeMessage(msg).data); - break; - - case this.context.evm.storages.getContractAddress(): - res.push(this.context.evm.storages.decodeMessage(msg).data); - break; - - case this.context.evm.datacaps.getContractAddress(): - res.push(this.context.evm.datacaps.decodeMessage(msg).data); - break; - - case this.context.evm.escrow.getContractAddress(): - res.push(this.context.evm.escrow.decodeMessage(msg).data); - break; - - case this.context.evm.filplus.getContractAddress(): - res.push(this.context.evm.filplus.decodeMessage(msg).data); - break; - - case this.context.evm.roles.getContractAddress(): - res.push(this.context.evm.roles.decodeMessage(msg).data); - break; + // case this.context.evm.dataset.requirement.getContractAddress(): + // res.push( + // this.context.evm.dataset.requirement.decodeMessage(msg).data, + // ); + // break; + + // case this.context.evm.dataset.proof.getContractAddress(): + // res.push(this.context.evm.dataset.proof.decodeMessage(msg).data); + // break; + + // case this.context.evm.dataset.challenge.getContractAddress(): + // res.push( + // this.context.evm.dataset.challenge.decodeMessage(msg).data, + // ); + // break; + + // case this.context.evm.matching.target.getContractAddress(): + // res.push(this.context.evm.matching.target.decodeMessage(msg).data); + // break; + + // case this.context.evm.matching.metadata.getContractAddress(): + // res.push( + // this.context.evm.matching.metadata.decodeMessage(msg).data, + // ); + // break; + + // case this.context.evm.matching.bids.getContractAddress(): + // res.push(this.context.evm.matching.bids.decodeMessage(msg).data); + // break; + + // case this.context.evm.storages.getContractAddress(): + // res.push(this.context.evm.storages.decodeMessage(msg).data); + // break; + + // case this.context.evm.datacaps.getContractAddress(): + // res.push(this.context.evm.datacaps.decodeMessage(msg).data); + // break; + + // case this.context.evm.escrow.getContractAddress(): + // res.push(this.context.evm.escrow.decodeMessage(msg).data); + // break; + + // case this.context.evm.filplus.getContractAddress(): + // res.push(this.context.evm.filplus.decodeMessage(msg).data); + // break; + + // case this.context.evm.roles.getContractAddress(): + // res.push(this.context.evm.roles.decodeMessage(msg).data); + // break; default: break; diff --git a/src/backgroundTask/interface/index.ts b/src/backgroundTask/interface/index.ts index e8006c5..8ae6833 100644 --- a/src/backgroundTask/interface/index.ts +++ b/src/backgroundTask/interface/index.ts @@ -28,17 +28,17 @@ import { } from '@unipackage/filecoin'; import { DatasetMetadataEvm, - DatasetProofEvm, - DatasetRequirementEvm, - DatasetChallengeEvm, - MatchingBidsEvm, - MatchingMetadataEvm, - MatchingTargetEvm, - StoragesEvm, - DatacapsEvm, - EscrowEvm, - RolesEvm, - FilplusEvm, + // DatasetProofEvm, + // DatasetRequirementEvm, + // DatasetChallengeEvm, + // MatchingBidsEvm, + // MatchingMetadataEvm, + // MatchingTargetEvm, + // StoragesEvm, + // DatacapsEvm, + // EscrowEvm, + // RolesEvm, + // FilplusEvm, DataswapMessage, DataswapMessageMongoDatastore, DatasetMetadataMongoDatastore, @@ -58,22 +58,20 @@ export interface ChainContext { * Configuration for Evm context. */ export interface EvmContext { - escrow: EscrowEvm; - filplus: FilplusEvm; - roles: RolesEvm; - dataset: { - metadata: DatasetMetadataEvm; - requirement: DatasetRequirementEvm; - proof: DatasetProofEvm; - challenge: DatasetChallengeEvm; - }; - matching: { - target: MatchingTargetEvm; - metadata: MatchingMetadataEvm; - bids: MatchingBidsEvm; - }; - storages: StoragesEvm; - datacaps: DatacapsEvm; + // escrow: EscrowEvm; + // filplus: FilplusEvm; + // roles: RolesEvm; + datasetMetadata: DatasetMetadataEvm; + // requirement: DatasetRequirementEvm; + // proof: DatasetProofEvm; + // challenge: DatasetChallengeEvm; + // matching: { + // target: MatchingTargetEvm; + // metadata: MatchingMetadataEvm; + // bids: MatchingBidsEvm; + // }; + // storages: StoragesEvm; + // datacaps: DatacapsEvm; } /** diff --git a/src/config/backgroundTask.ts b/src/config/backgroundTask.ts index 18433aa..e041b27 100644 --- a/src/config/backgroundTask.ts +++ b/src/config/backgroundTask.ts @@ -24,6 +24,9 @@ import { EvmContext } from '../backgroundTask/interface'; import { Syncer } from '../backgroundTask/syncer'; import { Decoder } from '../backgroundTask/decoder'; import { Storager } from '../backgroundTask/storager'; +import { datasetMetadataEvm_Calibration } from './evm'; +import * as dotenv from 'dotenv'; +dotenv.config(); /** * Calibration network configuration @@ -33,7 +36,9 @@ export const calibrationConfig: Config = { token: process.env.CALIBRATION_LOTUS_TOKEN as string, mongoUrl: process.env.CALIBRATION_MONGO_URL as string, startHeight: 1210900, - evm: {} as EvmContext, + evm: { + datasetMetadata: datasetMetadataEvm_Calibration, + }, }; /** diff --git a/src/config/evm.ts b/src/config/evm.ts new file mode 100644 index 0000000..af5afb4 --- /dev/null +++ b/src/config/evm.ts @@ -0,0 +1,29 @@ +/******************************************************************************* + * (c) 2024 dataswap + * + * Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0 + * (the "Apache License"). You may not use this file except in compliance with one of these + * licenses. You may obtain a copy of the MIT License at + * + * https://opensource.org/licenses/MIT + * + * Or the Apache License, Version 2.0 at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the MIT License or the Apache License for the specific language governing permissions and + * limitations under the respective licenses. + ********************************************************************************/ +import { DatasetMetadataEvm } from '@dataswapjs/dataswapjs'; +import datasetMetaAbi from '@dataswapcore/contracts/abi/v0.8/Datasets.json'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +export const datasetMetadataEvm_Calibration = new DatasetMetadataEvm( + datasetMetaAbi, + '0x023e4b966b943c830b7d4cae84fe761641b29003', + process.env.CALIBRATION_LOTUS_PROVIDER_URL, +); diff --git a/test/backgroundTask/index.spec.ts b/test/backgroundTask/index.spec.ts new file mode 100644 index 0000000..67989ac --- /dev/null +++ b/test/backgroundTask/index.spec.ts @@ -0,0 +1,17 @@ +import { calibrationBgTask } from '../../src/config/backgroundTask'; + +describe('BackgroundTask', () => { + beforeAll(() => { + calibrationBgTask.start(); + }); + + afterAll(() => { + calibrationBgTask.stop(); + }); + + it('should stop the background task', () => { + expect(calibrationBgTask.isRunning()).toBe(true); + calibrationBgTask.stop(); + expect(calibrationBgTask.isRunning()).toBe(false); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 95f5641..b7bfe60 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,26 @@ { "compilerOptions": { - "module": "commonjs", - "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, - "experimentalDecorators": true, "allowSyntheticDefaultImports": true, - "target": "ES2021", "sourceMap": true, - "outDir": "./dist", "baseUrl": "./", - "incremental": true, - "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, - "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "outDir": "./dist-test", + "declaration": true, + "target": "ES2015", + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true, + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": false, + "experimentalDecorators": true, + "jsx": "react", + "skipLibCheck": true, + "incremental": false, } }