From 0750d98b986bad063dff0dd1e14458010665fcca Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Thu, 23 Mar 2023 11:39:55 +0200 Subject: [PATCH] fix compute flow integration test --- package.json | 2 +- src/services/Provider.ts | 47 +- test/integration/ComputeFlow.test.ts | 1615 +++++++++---------- test/integration/PublishEditConsume.test.ts | 3 +- test/integration/helpers.ts | 96 +- 5 files changed, 879 insertions(+), 884 deletions(-) diff --git a/package.json b/package.json index 514274c6..f3c32b6a 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "mocha": "TS_NODE_PROJECT='./test/tsconfig.json' mocha --config=test/.mocharc.json --node-env=test --exit", "test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover", "test:unit": "npm run mocha -- 'test/unit/**/*.test.ts'", - "test:integration": "npm run mocha -- 'test/integration/Provider.test.ts' 'test/integration/PublishFlow.test.ts' 'test/integration/PublishEditConsume.test.ts'", + "test:integration": "npm run mocha -- 'test/integration/ComputeFlow.test.ts'", "test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit", "test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration", "create:guide": "./scripts/createCodeExamples.sh test/integration/CodeExamples.test.ts", diff --git a/src/services/Provider.ts b/src/services/Provider.ts index 852f5bc0..426ed5d3 100644 --- a/src/services/Provider.ts +++ b/src/services/Provider.ts @@ -18,7 +18,6 @@ import { Smartcontract, GraphqlQuery } from '../@types' -import Web3 from 'web3' export class Provider { /** @@ -119,7 +118,7 @@ export class Provider { /** Encrypt data using the Provider's own symmetric key * @param {string} data data in json format that needs to be sent , it can either be a DDO or a File array - * @param {number} chainId network's id so provider can choose the corresponding web3 object + * @param {number} chainId network's id so provider can choose the corresponding Signer object * @param {string} providerUri provider uri address * @param {AbortSignal} signal abort signal * @return {Promise} urlDetails @@ -414,20 +413,20 @@ export class Provider { } /** Instruct the provider to start a compute job - * @param {string} did - * @param {string} consumerAddress - * @param {string} computeEnv - * @param {ComputeAlgorithm} algorithm * @param {string} providerUri * @param {Signer} signer + * @param {string} consumerAddress + * @param {string} computeEnv + * @param {ComputeAsset} dataset + * @param {ComputeAlgorithm} algorithm * @param {AbortSignal} signal abort signal + * @param {ComputeAsset[]} additionalDatasets * @param {ComputeOutput} output * @return {Promise} */ public async computeStart( providerUri: string, - signer: Signer, - consumerAddress: string, + consumer: Signer, computeEnv: string, dataset: ComputeAsset, algorithm: ComputeAlgorithm, @@ -445,12 +444,12 @@ export class Provider { : null const nonce = Date.now() - let signatureMessage = consumerAddress + let signatureMessage = await consumer.getAddress() signatureMessage += dataset.documentId signatureMessage += nonce - const signature = await this.signProviderRequest(signer, signatureMessage) + const signature = await this.signProviderRequest(consumer, signatureMessage) const payload = Object() - payload.consumerAddress = consumerAddress + payload.consumerAddress = await consumer.getAddress() payload.signature = signature payload.nonce = nonce payload.environment = computeEnv @@ -610,16 +609,14 @@ export class Provider { /** Get compute result url * @param {string} providerUri The URI of the provider we want to query - * @param {Signer} signer Web3 instance - * @param {string} consumerAddress The consumer ethereum address + * @param {Signer} consumer ether.Signer instance * @param {string} jobId The ID of a compute job. * @param {number} index Result index * @return {Promise} */ public async getComputeResultUrl( providerUri: string, - signer: Signer, - consumerAddress: string, + consumer: Signer, jobId: string, index: number ): Promise { @@ -633,14 +630,14 @@ export class Provider { : null const nonce = Date.now() - let signatureMessage = consumerAddress + let signatureMessage = await consumer.getAddress() signatureMessage += jobId signatureMessage += index.toString() signatureMessage += nonce - const signature = await this.signProviderRequest(signer, signatureMessage) + const signature = await this.signProviderRequest(consumer, signatureMessage) if (!computeResultUrl) return null let resultUrl = computeResultUrl - resultUrl += `?consumerAddress=${consumerAddress}` + resultUrl += `?consumerAddress=${await consumer.getAddress()}` resultUrl += `&jobId=${jobId}` resultUrl += `&index=${index.toString()}` resultUrl += `&nonce=${nonce}` @@ -650,19 +647,17 @@ export class Provider { /** Deletes a compute job. * @param {string} did - * @param {string} consumerAddress + * @param {Signer} consumer * @param {string} jobId * @param {string} providerUri - * @param {Web3} web3 * @param {AbortSignal} signal abort signal * @return {Promise} */ public async computeDelete( did: string, - consumerAddress: string, + consumer: Signer, jobId: string, providerUri: string, - signer: Signer, signal?: AbortSignal ): Promise { const providerEndpoints = await this.getEndpoints(providerUri) @@ -676,20 +671,20 @@ export class Provider { const nonce = await this.getNonce( providerUri, - consumerAddress, + await consumer.getAddress(), signal, providerEndpoints, serviceEndpoints ) - let signatureMessage = consumerAddress + let signatureMessage = await consumer.getAddress() signatureMessage += jobId || '' signatureMessage += (did && `${this.noZeroX(did)}`) || '' signatureMessage += nonce - const signature = await this.signProviderRequest(signer, signatureMessage) + const signature = await this.signProviderRequest(consumer, signatureMessage) const payload = Object() payload.documentId = this.noZeroX(did) - payload.consumerAddress = consumerAddress + payload.consumerAddress = await consumer.getAddress() payload.jobId = jobId if (signature) payload.signature = signature diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index a096ef4f..3230f9c4 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -1,855 +1,854 @@ -// import { assert } from 'chai' -// import { AbiItem } from 'web3-utils' -// import { web3, getTestConfig, getAddresses } from '../config' -// import { -// Config, -// ProviderInstance, -// Aquarius, -// Datatoken, -// calculateEstimatedGas, -// sendTx -// } from '../../src' -// import { ComputeJob, ComputeAsset, ComputeAlgorithm, Files } from '../../src/@types' -// import { createAsset, handleComputeOrder } from './helpers' +import { assert } from 'chai' +import { AbiItem } from 'web3-utils' +import { ethers, Signer } from 'ethers' +import { getTestConfig, getAddresses, provider } from '../config' +import { + Config, + ProviderInstance, + Aquarius, + Datatoken, + sendTx, + amountToUnits +} from '../../src' +import { ComputeJob, ComputeAsset, ComputeAlgorithm, Files } from '../../src/@types' +import { createAsset, handleComputeOrder } from './helpers' -// let config: Config +let config: Config -// let aquarius: Aquarius -// let datatoken: Datatoken -// let providerUrl: string -// let consumerAccount: string -// let publisherAccount: string -// let providerInitializeComputeResults -// let computeEnvs -// let addresses: any -// let ddoWith5mTimeoutId -// let ddoWithNoTimeoutId -// let algoDdoWith5mTimeoutId -// let algoDdoWithNoTimeoutId +let aquarius: Aquarius +let datatoken: Datatoken +let providerUrl: string +let consumerAccount: Signer +let publisherAccount: Signer +let providerInitializeComputeResults +let computeEnvs +let addresses: any +let ddoWith5mTimeoutId +let ddoWithNoTimeoutId +let algoDdoWith5mTimeoutId +let algoDdoWithNoTimeoutId -// let freeComputeJobId: string -// let paidComputeJobId: string +let freeComputeJobId: string +let paidComputeJobId: string -// let resolvedDdoWith5mTimeout -// let resolvedDdoWithNoTimeout -// let resolvedAlgoDdoWith5mTimeout -// let resolvedAlgoDdoWithNoTimeout +let resolvedDdoWith5mTimeout +let resolvedDdoWithNoTimeout +let resolvedAlgoDdoWith5mTimeout +let resolvedAlgoDdoWithNoTimeout -// let freeEnvDatasetTxId -// let freeEnvAlgoTxId -// let paidEnvDatasetTxId -// let paidEnvAlgoTxId -// let computeValidUntil +let freeEnvDatasetTxId +let freeEnvAlgoTxId +let paidEnvDatasetTxId +let paidEnvAlgoTxId +let computeValidUntil -// const assetUrl: Files = { -// datatokenAddress: '0x0', -// nftAddress: '0x0', -// files: [ -// { -// type: 'url', -// url: 'https://raw.githubusercontent.com/oceanprotocol/testdatasets/main/shs_dataset_test.txt', -// method: 'GET' -// } -// ] -// } -// const ddoWithNoTimeout = { -// '@context': ['https://w3id.org/did/v1'], -// id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', -// version: '4.1.0', -// chainId: 4, -// nftAddress: '0x0', -// metadata: { -// created: '2021-12-20T14:35:20Z', -// updated: '2021-12-20T14:35:20Z', -// type: 'dataset', -// name: 'dfgdfgdg', -// description: 'd dfgd fgd dfg dfgdfgd dfgdf', -// tags: [''], -// author: 'dd', -// license: 'https://market.oceanprotocol.com/terms', -// additionalInformation: { -// termsAndConditions: true -// } -// }, -// services: [ -// { -// id: 'notAnId', -// type: 'compute', -// files: '', -// datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', -// serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com', -// timeout: 0, -// compute: { -// publisherTrustedAlgorithmPublishers: [], -// publisherTrustedAlgorithms: [], -// allowRawAlgorithm: true, -// allowNetworkAccess: true -// } -// } -// ] -// } +const assetUrl: Files = { + datatokenAddress: '0x0', + nftAddress: '0x0', + files: [ + { + type: 'url', + url: 'https://raw.githubusercontent.com/oceanprotocol/testdatasets/main/shs_dataset_test.txt', + method: 'GET' + } + ] +} +const ddoWithNoTimeout = { + '@context': ['https://w3id.org/did/v1'], + id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', + version: '4.1.0', + chainId: 4, + nftAddress: '0x0', + metadata: { + created: '2021-12-20T14:35:20Z', + updated: '2021-12-20T14:35:20Z', + type: 'dataset', + name: 'dfgdfgdg', + description: 'd dfgd fgd dfg dfgdfgd dfgdf', + tags: [''], + author: 'dd', + license: 'https://market.oceanprotocol.com/terms', + additionalInformation: { + termsAndConditions: true + } + }, + services: [ + { + id: 'notAnId', + type: 'compute', + files: '', + datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', + serviceEndpoint: 'http://172.15.0.4:8030', + timeout: 0, + compute: { + publisherTrustedAlgorithmPublishers: [], + publisherTrustedAlgorithms: [], + allowRawAlgorithm: true, + allowNetworkAccess: true + } + } + ] +} -// const ddoWith5mTimeout = { -// '@context': ['https://w3id.org/did/v1'], -// id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', -// version: '4.1.0', -// chainId: 4, -// nftAddress: '0x0', -// metadata: { -// created: '2021-12-20T14:35:20Z', -// updated: '2021-12-20T14:35:20Z', -// type: 'dataset', -// name: 'dfgdfgdg', -// description: 'd dfgd fgd dfg dfgdfgd dfgdf', -// tags: [''], -// author: 'dd', -// license: 'https://market.oceanprotocol.com/terms', -// additionalInformation: { -// termsAndConditions: true -// } -// }, -// services: [ -// { -// id: 'notAnId', -// type: 'compute', -// files: '', -// datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', -// serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com', -// timeout: 300, -// compute: { -// publisherTrustedAlgorithmPublishers: [], -// publisherTrustedAlgorithms: [], -// allowRawAlgorithm: true, -// allowNetworkAccess: true -// } -// } -// ] -// } -// const algoAssetUrl: Files = { -// datatokenAddress: '0x0', -// nftAddress: '0x0', -// files: [ -// { -// type: 'url', -// url: 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', -// method: 'GET' -// } -// ] -// } -// const algoDdoWithNoTimeout = { -// '@context': ['https://w3id.org/did/v1'], -// id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', -// version: '4.1.0', -// chainId: 4, -// nftAddress: '0x0', -// metadata: { -// created: '2021-12-20T14:35:20Z', -// updated: '2021-12-20T14:35:20Z', -// type: 'algorithm', -// name: 'dfgdfgdg', -// description: 'd dfgd fgd dfg dfgdfgd dfgdf', -// tags: [''], -// author: 'dd', -// license: 'https://market.oceanprotocol.com/terms', -// additionalInformation: { -// termsAndConditions: true -// }, -// algorithm: { -// language: 'Node.js', -// version: '1.0.0', -// container: { -// entrypoint: 'node $ALGO', -// image: 'ubuntu', -// tag: 'latest', -// checksum: -// 'sha256:2d7ecc9c5e08953d586a6e50c29b91479a48f69ac1ba1f9dc0420d18a728dfc5' -// } -// } -// }, -// services: [ -// { -// id: 'notAnId', -// type: 'access', -// files: '', -// datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', -// serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com', -// timeout: 0 -// } -// ] -// } +const ddoWith5mTimeout = { + '@context': ['https://w3id.org/did/v1'], + id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', + version: '4.1.0', + chainId: 4, + nftAddress: '0x0', + metadata: { + created: '2021-12-20T14:35:20Z', + updated: '2021-12-20T14:35:20Z', + type: 'dataset', + name: 'dfgdfgdg', + description: 'd dfgd fgd dfg dfgdfgd dfgdf', + tags: [''], + author: 'dd', + license: 'https://market.oceanprotocol.com/terms', + additionalInformation: { + termsAndConditions: true + } + }, + services: [ + { + id: 'notAnId', + type: 'compute', + files: '', + datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', + serviceEndpoint: 'http://172.15.0.4:8030', + timeout: 300, + compute: { + publisherTrustedAlgorithmPublishers: [], + publisherTrustedAlgorithms: [], + allowRawAlgorithm: true, + allowNetworkAccess: true + } + } + ] +} +const algoAssetUrl: Files = { + datatokenAddress: '0x0', + nftAddress: '0x0', + files: [ + { + type: 'url', + url: 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', + method: 'GET' + } + ] +} +const algoDdoWithNoTimeout = { + '@context': ['https://w3id.org/did/v1'], + id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', + version: '4.1.0', + chainId: 4, + nftAddress: '0x0', + metadata: { + created: '2021-12-20T14:35:20Z', + updated: '2021-12-20T14:35:20Z', + type: 'algorithm', + name: 'dfgdfgdg', + description: 'd dfgd fgd dfg dfgdfgd dfgdf', + tags: [''], + author: 'dd', + license: 'https://market.oceanprotocol.com/terms', + additionalInformation: { + termsAndConditions: true + }, + algorithm: { + language: 'Node.js', + version: '1.0.0', + container: { + entrypoint: 'node $ALGO', + image: 'ubuntu', + tag: 'latest', + checksum: + 'sha256:2d7ecc9c5e08953d586a6e50c29b91479a48f69ac1ba1f9dc0420d18a728dfc5' + } + } + }, + services: [ + { + id: 'notAnId', + type: 'access', + files: '', + datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', + serviceEndpoint: 'http://172.15.0.4:8030', + timeout: 0 + } + ] +} -// const algoDdoWith5mTimeout = { -// '@context': ['https://w3id.org/did/v1'], -// id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', -// version: '4.1.0', -// chainId: 4, -// nftAddress: '0x0', -// metadata: { -// created: '2021-12-20T14:35:20Z', -// updated: '2021-12-20T14:35:20Z', -// type: 'algorithm', -// name: 'dfgdfgdg', -// description: 'd dfgd fgd dfg dfgdfgd dfgdf', -// tags: [''], -// author: 'dd', -// license: 'https://market.oceanprotocol.com/terms', -// additionalInformation: { -// termsAndConditions: true -// }, -// algorithm: { -// language: 'Node.js', -// version: '1.0.0', -// container: { -// entrypoint: 'node $ALGO', -// image: 'ubuntu', -// tag: 'latest', -// checksum: -// 'sha256:2d7ecc9c5e08953d586a6e50c29b91479a48f69ac1ba1f9dc0420d18a728dfc5' -// } -// } -// }, -// services: [ -// { -// id: 'notAnId', -// type: 'access', -// files: '', -// datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', -// serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com', -// timeout: 300 -// } -// ] -// } +const algoDdoWith5mTimeout = { + '@context': ['https://w3id.org/did/v1'], + id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c', + version: '4.1.0', + chainId: 4, + nftAddress: '0x0', + metadata: { + created: '2021-12-20T14:35:20Z', + updated: '2021-12-20T14:35:20Z', + type: 'algorithm', + name: 'dfgdfgdg', + description: 'd dfgd fgd dfg dfgdfgd dfgdf', + tags: [''], + author: 'dd', + license: 'https://market.oceanprotocol.com/terms', + additionalInformation: { + termsAndConditions: true + }, + algorithm: { + language: 'Node.js', + version: '1.0.0', + container: { + entrypoint: 'node $ALGO', + image: 'ubuntu', + tag: 'latest', + checksum: + 'sha256:2d7ecc9c5e08953d586a6e50c29b91479a48f69ac1ba1f9dc0420d18a728dfc5' + } + } + }, + services: [ + { + id: 'notAnId', + type: 'access', + files: '', + datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3', + serviceEndpoint: 'http://172.15.0.4:8030', + timeout: 300 + } + ] +} -// function delay(interval: number) { -// return it('should delay', (done) => { -// setTimeout(() => done(), interval) -// }).timeout(interval + 100) -// } +function delay(interval: number) { + return it('should delay', (done) => { + setTimeout(() => done(), interval) + }).timeout(interval + 100) +} -// describe('Simple compute tests', async () => { -// before(async () => { -// config = await getTestConfig(web3) -// addresses = getAddresses() -// aquarius = new Aquarius(config.metadataCacheUri) -// providerUrl = config.providerUri -// datatoken = new Datatoken(web3) -// }) +describe('Compute flow tests', async () => { + before(async () => { + publisherAccount = (await provider.getSigner(0)) as Signer + consumerAccount = (await provider.getSigner(1)) as Signer + config = await getTestConfig(publisherAccount) + aquarius = new Aquarius(config?.metadataCacheUri) + providerUrl = config?.providerUri + addresses = getAddresses() + }) -// it('should publish datasets and algorithms', async () => { -// const accounts = await web3.eth.getAccounts() -// publisherAccount = accounts[0] -// consumerAccount = accounts[1] -// // mint Ocean -// ///