From 5a1a660d49d6ce37573a2fdb4240ed9059a0f54c Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Mon, 15 Jun 2020 16:37:11 +0200 Subject: [PATCH 1/8] add test:integration --- package.json | 3 +- test/{unit => }/TestContractHandler.ts | 0 test/integration/Simpleflow.test.ts | 43 ++++++++++++++++++++++++++ test/integration/mocha.opts | 7 +++++ test/unit/Datatokens.test.ts | 4 +-- 5 files changed, 53 insertions(+), 4 deletions(-) rename test/{unit => }/TestContractHandler.ts (100%) create mode 100644 test/integration/Simpleflow.test.ts create mode 100644 test/integration/mocha.opts diff --git a/package.json b/package.json index 52166316..9e444b0c 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "release": "release-it --non-interactive", "changelog": "auto-changelog -p", "prepublishOnly": "npm run build", - "test": "mocha --opts test/unit/mocha.opts" + "test": "mocha --opts test/unit/mocha.opts", + "test:integration": "mocha --opts test/integration/mocha.opts" }, "repository": { "type": "git", diff --git a/test/unit/TestContractHandler.ts b/test/TestContractHandler.ts similarity index 100% rename from test/unit/TestContractHandler.ts rename to test/TestContractHandler.ts diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts new file mode 100644 index 00000000..027cc421 --- /dev/null +++ b/test/integration/Simpleflow.test.ts @@ -0,0 +1,43 @@ +import { assert } from 'chai' +import { TestContractHandler } from '../TestContractHandler' +import { DataTokens } from '../../src/datatokens/Datatokens' + +const Web3 = require('web3') +const web3 = new Web3("http://127.0.0.1:8545") + +const factoryABI = require('../../src/datatokens/FactoryABI.json') +const datatokensABI = require('../../src/datatokens/DatatokensABI.json') + +describe('Simple flow', () => { + + let minter + let spender + let balance + let contracts + let datatoken + let tokenAddress + + let tokenAmount = 100 + let blob = 'https://example.com/dataset-1' + + describe('#test', () => { + it('should deploy contracts', async () => { + contracts = new TestContractHandler(factoryABI,datatokensABI) + await contracts.getAccounts() + minter = contracts.accounts[0] + spender = contracts.accounts[1] + await contracts.deployContracts(minter) + }) + + it('should create Datatoken object', async () => { + datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) + assert(datatoken !== null) + }) + + it('should create Datatoken contract', async () => { + tokenAddress = await datatoken.create(blob, minter) + assert(tokenAddress !== null) + }) + + }) +}) \ No newline at end of file diff --git a/test/integration/mocha.opts b/test/integration/mocha.opts new file mode 100644 index 00000000..6f57adae --- /dev/null +++ b/test/integration/mocha.opts @@ -0,0 +1,7 @@ +--require ts-node/register +--require source-map-support/register +--require mock-local-storage +--full-trace +--exit +--timeout 300000 +test/integration/**/*.test.ts \ No newline at end of file diff --git a/test/unit/Datatokens.test.ts b/test/unit/Datatokens.test.ts index e362cfa9..f910bb54 100644 --- a/test/unit/Datatokens.test.ts +++ b/test/unit/Datatokens.test.ts @@ -1,5 +1,5 @@ import { assert } from 'chai' -import { TestContractHandler } from './TestContractHandler' +import { TestContractHandler } from '../TestContractHandler' import { DataTokens } from '../../src/datatokens/Datatokens' const Web3 = require('web3') @@ -49,7 +49,6 @@ describe('DataTokens', () => { await datatoken.transfer(tokenAddress, spender, tokenAmount, minter) balance = await datatoken.balance(tokenAddress, spender) assert(balance.toString() === tokenAmount.toString()) - }) it('should approve Datatokens to spend', async () => { @@ -61,6 +60,5 @@ describe('DataTokens', () => { minter = await datatoken.balance(tokenAddress, spender) assert(balance.toString() === tokenAmount.toString()) }) - }) }) \ No newline at end of file From 13fe64395ac9e3cb07bc7bb74c13ec865384723d Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Mon, 15 Jun 2020 17:41:36 +0200 Subject: [PATCH 2/8] add publish dataset step --- src/datatokens/Datatokens.ts | 2 +- test/integration/Simpleflow.test.ts | 33 +++++++++++++++++------------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 3024db66..c7c5dda8 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -123,7 +123,7 @@ export class DataTokens { const trxReceipt = await datatoken.methods.mint(address, amount) .send({ from:account, - gas: estGas*2, + gas: estGas+1, gasPrice: '3000000000' }) diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 027cc421..b76b21b4 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -10,8 +10,10 @@ const datatokensABI = require('../../src/datatokens/DatatokensABI.json') describe('Simple flow', () => { - let minter - let spender + let owner + let alice + let bob + let marketplace let balance let contracts let datatoken @@ -21,23 +23,28 @@ describe('Simple flow', () => { let blob = 'https://example.com/dataset-1' describe('#test', () => { - it('should deploy contracts', async () => { + it('Initialize Ocean contracts v3', async () => { contracts = new TestContractHandler(factoryABI,datatokensABI) await contracts.getAccounts() - minter = contracts.accounts[0] - spender = contracts.accounts[1] - await contracts.deployContracts(minter) + owner = contracts.accounts[0] + alice = contracts.accounts[1] + bob = contracts.accounts[2] + marketplace = contracts.accounts[3] + await contracts.deployContracts(owner) }) - it('should create Datatoken object', async () => { + it('Alice publishes a dataset', async () => { + //Alice's config + const config={ + network: 'ganache', + providerUri: 'localhost:8030' + } + datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) - assert(datatoken !== null) - }) + tokenAddress = await datatoken.create(config.providerUri, alice) - it('should create Datatoken contract', async () => { - tokenAddress = await datatoken.create(blob, minter) - assert(tokenAddress !== null) + //Alice allows MarketPlace to transfer tokenAmount of DT + await datatoken.approve(tokenAddress, alice, tokenAmount, marketplace) }) - }) }) \ No newline at end of file From b8f2092f7417680cb98242c81da804d146a35336 Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Tue, 16 Jun 2020 14:39:21 +0200 Subject: [PATCH 3/8] add simpleflow structure --- test/integration/Simpleflow.test.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index b76b21b4..75177e71 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -13,14 +13,14 @@ describe('Simple flow', () => { let owner let alice let bob - let marketplace let balance let contracts let datatoken let tokenAddress let tokenAmount = 100 - let blob = 'https://example.com/dataset-1' + let transferAmount = 1 + let blob = 'localhost:8030/api/v1/services' describe('#test', () => { it('Initialize Ocean contracts v3', async () => { @@ -29,7 +29,6 @@ describe('Simple flow', () => { owner = contracts.accounts[0] alice = contracts.accounts[1] bob = contracts.accounts[2] - marketplace = contracts.accounts[3] await contracts.deployContracts(owner) }) @@ -39,12 +38,21 @@ describe('Simple flow', () => { network: 'ganache', providerUri: 'localhost:8030' } - + // Alice creates a Datatoken datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) tokenAddress = await datatoken.create(config.providerUri, alice) + }) - //Alice allows MarketPlace to transfer tokenAmount of DT - await datatoken.approve(tokenAddress, alice, tokenAmount, marketplace) + it('Alice mints 100 tokens', async () => { + datatoken.mint(tokenAddress, alice, tokenAmount) + }) + + it('Alice transfers 1 token to Bob', async () => { + datatoken.transfer(tokenAddress, bob, tokenAmount, alice) + }) + + it('Bob consumes dataset', async () => { + datatoken.transfer(tokenAddress, bob, tokenAmount, alice) }) }) }) \ No newline at end of file From 84ba2144fd8e9a243be38aca6810a8a1a0e69668 Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Tue, 16 Jun 2020 17:05:15 +0200 Subject: [PATCH 4/8] wip add download function --- src/ocean/Assets.ts | 31 +++++++++++++++++++++++++++++ test/integration/Simpleflow.test.ts | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 1f2d083a..3337f875 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -170,4 +170,35 @@ export class Assets extends Instantiable { return storedDdo }) } + + public async download( + agreementId: string, + serviceEndpoint: string, + account: Account, + files: File[], + destination: string, + index: number = -1 + ): Promise { + // const signature = await this.createSignature(account, agreementId) + const filesPromises = files + .filter((_, i) => index === -1 || i === index) + .map(async ({ index: i }) => { + let consumeUrl = serviceEndpoint + consumeUrl += `?index=${i}` + consumeUrl += `&serviceAgreementId=${noZeroX(agreementId)}` + consumeUrl += `&consumerAddress=${account.getId()}` + // consumeUrl += `&signature=${signature}` + try { + // TODO: change to WebServiceConnector.ts + await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i) + } catch (e) { + this.logger.error('Error consuming assets') + this.logger.error(e) + throw e + } + }) + await Promise.all(filesPromises) + return destination + } + } diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 75177e71..71a3b82f 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -11,8 +11,8 @@ const datatokensABI = require('../../src/datatokens/DatatokensABI.json') describe('Simple flow', () => { let owner - let alice let bob + let alice let balance let contracts let datatoken From a6bccad3f17b4f5529e9026590b8f1903fd71baa Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Wed, 17 Jun 2020 10:52:40 +0200 Subject: [PATCH 5/8] update assets.download --- src/ocean/Assets.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 3337f875..e24a07b9 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -1,5 +1,6 @@ import { TransactionReceipt } from 'web3-core' import { SearchQuery } from '../aquarius/Aquarius' +import { File, MetaDataAlgorithm } from '../ddo/MetaData' import { DDO } from '../ddo/DDO' import { MetaData, EditableMetaData } from '../ddo/MetaData' import { Service, ServiceAccess, ServiceComputePrivacy } from '../ddo/Service' @@ -8,6 +9,7 @@ import DID from './DID' import { SubscribablePromise, didZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' +import { WebServiceConnector } from './utils/WebServiceConnector' import { DataTokens } from '../lib' export enum CreateProgressStep { @@ -172,7 +174,7 @@ export class Assets extends Instantiable { } public async download( - agreementId: string, + dtAddress: string, serviceEndpoint: string, account: Account, files: File[], @@ -185,12 +187,15 @@ export class Assets extends Instantiable { .map(async ({ index: i }) => { let consumeUrl = serviceEndpoint consumeUrl += `?index=${i}` - consumeUrl += `&serviceAgreementId=${noZeroX(agreementId)}` - consumeUrl += `&consumerAddress=${account.getId()}` + consumeUrl += `&serviceAgreementId=${dtAddress}` + // consumeUrl += `&consumerAddress=${account.getId()}` // consumeUrl += `&signature=${signature}` + + let serviceConnector = new WebServiceConnector(this.logger) + try { // TODO: change to WebServiceConnector.ts - await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i) + await serviceConnector.downloadFile(consumeUrl, destination, i) } catch (e) { this.logger.error('Error consuming assets') this.logger.error(e) From 653dcf5ddaf43c316438e0fcebe5937069d8147e Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Wed, 17 Jun 2020 12:18:02 +0200 Subject: [PATCH 6/8] add bob consumes --- src/ocean/Assets.ts | 32 ++++++++++------------------- test/integration/Simpleflow.test.ts | 10 +++++++-- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index e24a07b9..cf21d458 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -176,34 +176,24 @@ export class Assets extends Instantiable { public async download( dtAddress: string, serviceEndpoint: string, - account: Account, - files: File[], - destination: string, - index: number = -1 + account: string ): Promise { - // const signature = await this.createSignature(account, agreementId) - const filesPromises = files - .filter((_, i) => index === -1 || i === index) - .map(async ({ index: i }) => { - let consumeUrl = serviceEndpoint - consumeUrl += `?index=${i}` - consumeUrl += `&serviceAgreementId=${dtAddress}` - // consumeUrl += `&consumerAddress=${account.getId()}` - // consumeUrl += `&signature=${signature}` + + let consumeUrl = serviceEndpoint + consumeUrl += `&consumerAddress=${account}` + consumeUrl += `&serviceAgreementId=${dtAddress}` - let serviceConnector = new WebServiceConnector(this.logger) + let serviceConnector = new WebServiceConnector(this.logger) - try { - // TODO: change to WebServiceConnector.ts - await serviceConnector.downloadFile(consumeUrl, destination, i) + try { + await serviceConnector.downloadFile(consumeUrl) } catch (e) { this.logger.error('Error consuming assets') this.logger.error(e) throw e - } - }) - await Promise.all(filesPromises) - return destination + } + + return serviceEndpoint } } diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 71a3b82f..47956792 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -1,6 +1,9 @@ import { assert } from 'chai' import { TestContractHandler } from '../TestContractHandler' import { DataTokens } from '../../src/datatokens/Datatokens' +import { Ocean } from '../../src/ocean/Ocean' +import { Config } from '../../src/models/Config' + const Web3 = require('web3') const web3 = new Web3("http://127.0.0.1:8545") @@ -20,7 +23,7 @@ describe('Simple flow', () => { let tokenAmount = 100 let transferAmount = 1 - let blob = 'localhost:8030/api/v1/services' + let blob = 'https://localhost:8030/api/v1/services' describe('#test', () => { it('Initialize Ocean contracts v3', async () => { @@ -52,7 +55,10 @@ describe('Simple flow', () => { }) it('Bob consumes dataset', async () => { - datatoken.transfer(tokenAddress, bob, tokenAmount, alice) + const config = new Config() + + let ocean = await Ocean.getInstance(config) + ocean.assets.download(tokenAddress, blob, bob) }) }) }) \ No newline at end of file From b29dbf1fd9de7986c7e6bf9555d70a285a005a41 Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Wed, 17 Jun 2020 15:43:14 +0200 Subject: [PATCH 7/8] fix url --- test/integration/Simpleflow.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 47956792..9b77cb9b 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -23,7 +23,7 @@ describe('Simple flow', () => { let tokenAmount = 100 let transferAmount = 1 - let blob = 'https://localhost:8030/api/v1/services' + let blob = 'http://localhost:8030/api/v1/provider/services' describe('#test', () => { it('Initialize Ocean contracts v3', async () => { @@ -56,7 +56,6 @@ describe('Simple flow', () => { it('Bob consumes dataset', async () => { const config = new Config() - let ocean = await Ocean.getInstance(config) ocean.assets.download(tokenAddress, blob, bob) }) From b1c134df203eae6c57e1c59222e7dc8df3ff1d72 Mon Sep 17 00:00:00 2001 From: arsenyjin Date: Wed, 17 Jun 2020 16:01:49 +0200 Subject: [PATCH 8/8] add clean up --- src/ocean/Assets.ts | 4 ++-- test/integration/Simpleflow.test.ts | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index cf21d458..9f56ccb1 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -180,8 +180,8 @@ export class Assets extends Instantiable { ): Promise { let consumeUrl = serviceEndpoint - consumeUrl += `&consumerAddress=${account}` - consumeUrl += `&serviceAgreementId=${dtAddress}` + // consumeUrl += `&consumerAddress=${account}` + // consumeUrl += `&serviceAgreementId=${dtAddress}` let serviceConnector = new WebServiceConnector(this.logger) diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 9b77cb9b..82ebe88d 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -36,11 +36,6 @@ describe('Simple flow', () => { }) it('Alice publishes a dataset', async () => { - //Alice's config - const config={ - network: 'ganache', - providerUri: 'localhost:8030' - } // Alice creates a Datatoken datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) tokenAddress = await datatoken.create(config.providerUri, alice)