1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add more tests

This commit is contained in:
alexcos20 2020-07-13 00:10:47 -07:00
parent 3071dc1de7
commit 8174709bbf
5 changed files with 172 additions and 129 deletions

View File

@ -40,9 +40,9 @@ export class DataTokens {
*/ */
public async create(metaDataStoreURI: string, account: Account): Promise<string> { public async create(metaDataStoreURI: string, account: Account): Promise<string> {
// Create factory contract object // Create factory contract object
const factory = new this.web3.eth.Contract( const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
this.factoryABI, this.factoryAddress, {from: account} from: account
) })
const estGas = await factory.methods const estGas = await factory.methods
.createToken(metaDataStoreURI) .createToken(metaDataStoreURI)
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {

View File

@ -2,18 +2,14 @@ import { SearchQuery } from '../metadatastore/MetadataStore'
import { DDO } from '../ddo/DDO' import { DDO } from '../ddo/DDO'
import { Metadata } from '../ddo/interfaces/Metadata' import { Metadata } from '../ddo/interfaces/Metadata'
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm' import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
import { import { Service, ServiceComputePrivacy, ServiceCompute } from '../ddo/interfaces/Service'
Service,
ServiceComputePrivacy,
ServiceCompute
} from '../ddo/interfaces/Service'
import { EditableMetadata } from '../ddo/interfaces/EditableMetadata' import { EditableMetadata } from '../ddo/interfaces/EditableMetadata'
import Account from './Account' import Account from './Account'
import DID from './DID' import DID from './DID'
import { SubscribablePromise } from '../utils' import { SubscribablePromise } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import {Output} from "./interfaces/ComputeOutput"; import { Output } from './interfaces/ComputeOutput'
import {ComputeJob} from "./interfaces/ComputeJob"; import { ComputeJob } from './interfaces/ComputeJob'
// import { WebServiceConnector } from './utils/WebServiceConnector' // import { WebServiceConnector } from './utils/WebServiceConnector'
// import { Output } from './interfaces/ComputeOutput' // import { Output } from './interfaces/ComputeOutput'
// import { ComputeJob } from './interfaces/ComputeJob' // import { ComputeJob } from './interfaces/ComputeJob'
@ -72,7 +68,7 @@ export class Compute extends Instantiable {
algorithmMeta?: MetadataAlgorithm, algorithmMeta?: MetadataAlgorithm,
output?: Output, output?: Output,
serviceIndex?: string, serviceIndex?: string,
serviceType?: string serviceType?: string
): Promise<ComputeJob> { ): Promise<ComputeJob> {
output = this.checkOutput(consumerAccount, output) output = this.checkOutput(consumerAccount, output)
if (did) { if (did) {
@ -88,7 +84,6 @@ export class Compute extends Instantiable {
serviceIndex, serviceIndex,
serviceType, serviceType,
tokenAddress tokenAddress
) )
return computeJobsList[0] as ComputeJob return computeJobsList[0] as ComputeJob
} else return null } else return null
@ -167,25 +162,45 @@ export class Compute extends Instantiable {
} }
public createServerAttributes( public createServerAttributes(
serverId: string, serverType: string, cost: string, serverId: string,
cpu: string, gpu: string, memory: string, serverType: string,
disk: string, maxExecutionTime: number cost: string,
cpu: string,
gpu: string,
memory: string,
disk: string,
maxExecutionTime: number
): object { ): object {
return { return {
serverId, serverType, cost, cpu, gpu, memory, disk, maxExecutionTime serverId,
serverType,
cost,
cpu,
gpu,
memory,
disk,
maxExecutionTime
} }
} }
public createContainerAttributes(image: string, tag: string, checksum: string): object { public createContainerAttributes(
return {image, tag, checksum} image: string,
tag: string,
checksum: string
): object {
return { image, tag, checksum }
} }
public createClusterAttributes(type: string, url: string): object { public createClusterAttributes(type: string, url: string): object {
return {type, url} return { type, url }
} }
public createProviderAttributes( public createProviderAttributes(
type: string, description: string, cluster: object, containers: object[], servers: object[] type: string,
description: string,
cluster: object,
containers: object[],
servers: object[]
): object { ): object {
return { return {
type, type,
@ -204,7 +219,7 @@ export class Compute extends Instantiable {
datePublished: string, datePublished: string,
providerAttributes: object, providerAttributes: object,
computePrivacy?: ServiceComputePrivacy, computePrivacy?: ServiceComputePrivacy,
timeout?: number, timeout?: number
): ServiceCompute { ): ServiceCompute {
const name = 'dataAssetComputingService' const name = 'dataAssetComputingService'
if (!timeout) timeout = 3600 if (!timeout) timeout = 3600
@ -226,13 +241,12 @@ export class Compute extends Instantiable {
} }
} }
if (computePrivacy) if (computePrivacy) service.attributes.main.privacy = computePrivacy
service.attributes.main.privacy = computePrivacy
return service as ServiceCompute return service as ServiceCompute
} }
/** /**
* Check the output object and add default properties if needed * Check the output object and add default properties if needed
* @param {Account} consumerAccount The account of the consumer ordering the service. * @param {Account} consumerAccount The account of the consumer ordering the service.
* @param {Output} output Output section used for publishing the result. * @param {Output} output Output section used for publishing the result.
@ -270,52 +284,51 @@ export class Compute extends Instantiable {
owner: output.owner || consumerAccount.getId() owner: output.owner || consumerAccount.getId()
} }
} }
} }
// "creator": "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", // "creator": "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e",
// "datePublished": "2019-04-09T19:02:11Z", // "datePublished": "2019-04-09T19:02:11Z",
// "cost": "10", // "cost": "10",
// "timeout": 86400, // "timeout": 86400,
// "provider": { // "provider": {
// "type": "Azure", // "type": "Azure",
// "description": "", // "description": "",
// "environment": { // "environment": {
// "cluster": { // "cluster": {
// "type": "Kubernetes", // "type": "Kubernetes",
// "url": "http://10.0.0.17/xxx" // "url": "http://10.0.0.17/xxx"
// }, // },
// "supportedContainers": [ // "supportedContainers": [
// { // {
// "image": "tensorflow/tensorflow", // "image": "tensorflow/tensorflow",
// "tag": "latest", // "tag": "latest",
// "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" // "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc"
// }, // },
// { // {
// "image": "tensorflow/tensorflow", // "image": "tensorflow/tensorflow",
// "tag": "latest", // "tag": "latest",
// "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc" // "checksum": "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc"
// } // }
// ], // ],
// "supportedServers": [ // "supportedServers": [
// { // {
// "serverId": "1", // "serverId": "1",
// "serverType": "xlsize", // "serverType": "xlsize",
// "cost": "50", // "cost": "50",
// "cpu": "16", // "cpu": "16",
// "gpu": "0", // "gpu": "0",
// "memory": "128gb", // "memory": "128gb",
// "disk": "160gb", // "disk": "160gb",
// "maxExecutionTime": 86400 // "maxExecutionTime": 86400
// }, // },
// { // {
// "serverId": "2", // "serverId": "2",
// "serverType": "medium", // "serverType": "medium",
// "cost": "10", // "cost": "10",
// "cpu": "2", // "cpu": "2",
// "gpu": "0", // "gpu": "0",
// "memory": "8gb", // "memory": "8gb",
// "disk": "80gb", // "disk": "80gb",
// "maxExecutionTime": 86400 // "maxExecutionTime": 86400
// } // }
// ] // ]
// } // }

View File

@ -17,7 +17,7 @@ import {
Instantiable, Instantiable,
generateIntantiableConfigFromConfig generateIntantiableConfigFromConfig
} from '../Instantiable.abstract' } from '../Instantiable.abstract'
import {Compute} from "./Compute"; import { Compute } from './Compute'
/** /**
* Main interface for Ocean Protocol. * Main interface for Ocean Protocol.

View File

@ -186,7 +186,7 @@ export class Provider extends Instantiable {
// 'algorithmDataToken': alg_data_token // 'algorithmDataToken': alg_data_token
// switch fetch method // switch fetch method
let fetch let fetch
switch (method) { switch (method) {

View File

@ -3,7 +3,7 @@ import { DataTokens } from '../../src/datatokens/Datatokens'
import { Ocean } from '../../src/ocean/Ocean' import { Ocean } from '../../src/ocean/Ocean'
import config from './config' import config from './config'
import { assert } from 'console' import { assert } from 'console'
import {ComputeJob} from "../../src/ocean/interfaces/ComputeJob"; import { ComputeJob } from '../../src/ocean/interfaces/ComputeJob'
import { import {
Service, Service,
ServiceComputePrivacy, ServiceComputePrivacy,
@ -38,7 +38,7 @@ describe('Marketplace flow', () => {
let cluster let cluster
let servers let servers
let containers let containers
let provider let provider
const dateCreated = new Date(Date.now()).toISOString().split('.')[0] + 'Z' // remove milliseconds const dateCreated = new Date(Date.now()).toISOString().split('.')[0] + 'Z' // remove milliseconds
@ -88,7 +88,8 @@ describe('Marketplace flow', () => {
license: 'CC-BY', license: 'CC-BY',
files: [ files: [
{ {
url:'https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt', url:
'https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt',
checksum: 'efb2c764274b745f5fc37f97c6b0e764', checksum: 'efb2c764274b745f5fc37f97c6b0e764',
contentLength: '4535431', contentLength: '4535431',
contentType: 'text/csv', contentType: 'text/csv',
@ -102,9 +103,21 @@ describe('Marketplace flow', () => {
it('Alice publishes dataset with a compute service', async () => { it('Alice publishes dataset with a compute service', async () => {
price = 10 // in datatoken price = 10 // in datatoken
cluster = ocean.compute.createClusterAttributes('Kubernetes', 'http://10.0.0.17/xxx') cluster = ocean.compute.createClusterAttributes(
'Kubernetes',
'http://10.0.0.17/xxx'
)
servers = [ servers = [
ocean.compute.createServerAttributes('1', 'xlsize', '50', '16', '0', '128gb', '160gb', timeout) ocean.compute.createServerAttributes(
'1',
'xlsize',
'50',
'16',
'0',
'128gb',
'160gb',
timeout
)
] ]
containers = [ containers = [
ocean.compute.createContainerAttributes( ocean.compute.createContainerAttributes(
@ -120,23 +133,25 @@ describe('Marketplace flow', () => {
containers, containers,
servers servers
) )
const computeService = ocean.compute.createComputeService( const computeService = ocean.compute.createComputeService(
alice, price, dateCreated, provider alice,
price,
dateCreated,
provider
) )
ddo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) ddo = await ocean.assets.create(asset, alice, [computeService], tokenAddress)
assert(ddo.dataToken === tokenAddress) assert(ddo.dataToken === tokenAddress)
}) })
//alex // alex
it('should publish a dataset with a compute service object that does not allow rawAlgo', async () => { it('should publish a dataset with a compute service object that does not allow rawAlgo', async () => {
const origComputePrivacy = { const origComputePrivacy = {
allowRawAlgorithm: false, allowRawAlgorithm: false,
allowNetworkAccess: false, allowNetworkAccess: false,
trustedAlgorithms: [] trustedAlgorithms: []
} }
const computeService = ocean.compute.createComputeService( const computeService = ocean.compute.createComputeService(
alice, alice,
'1000', '1000',
@ -144,17 +159,22 @@ describe('Marketplace flow', () => {
provider, provider,
origComputePrivacy as ServiceComputePrivacy origComputePrivacy as ServiceComputePrivacy
) )
datasetNoRawAlgo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) datasetNoRawAlgo = await ocean.assets.create(
asset,
alice,
[computeService],
tokenAddress
)
assert(datasetNoRawAlgo.dataToken === tokenAddress) assert(datasetNoRawAlgo.dataToken === tokenAddress)
}) })
it('should publish a dataset with a compute service object that allows only algo with did:op:1234', async () => { it('should publish a dataset with a compute service object that allows only algo with did:op:1234', async () => {
const origComputePrivacy = { const origComputePrivacy = {
allowRawAlgorithm: false, allowRawAlgorithm: false,
allowNetworkAccess: false, allowNetworkAccess: false,
trustedAlgorithms: ['did:op:1234'] trustedAlgorithms: ['did:op:1234']
} }
const computeService = ocean.compute.createComputeService( const computeService = ocean.compute.createComputeService(
alice, alice,
'1000', '1000',
@ -162,10 +182,15 @@ describe('Marketplace flow', () => {
provider, provider,
origComputePrivacy as ServiceComputePrivacy origComputePrivacy as ServiceComputePrivacy
) )
datasetWithTrustedAlgo = await ocean.assets.create(asset, alice, [computeService], tokenAddress) datasetWithTrustedAlgo = await ocean.assets.create(
asset,
alice,
[computeService],
tokenAddress
)
assert(datasetWithTrustedAlgo.dataToken === tokenAddress) assert(datasetWithTrustedAlgo.dataToken === tokenAddress)
}) })
it('should publish an algorithm', async () => { it('should publish an algorithm', async () => {
const algoAsset = { const algoAsset = {
main: { main: {
@ -176,22 +201,22 @@ describe('Marketplace flow', () => {
license: 'CC-BY', license: 'CC-BY',
files: [ files: [
{ {
url:'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', url:
'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js',
contentType: 'text/js', contentType: 'text/js',
encoding: 'UTF-8' encoding: 'UTF-8'
} }
], ],
"algorithm": { algorithm: {
"language": "js", language: 'js',
"format": "docker-image", format: 'docker-image',
"version": "0.1", version: '0.1',
"container": { container: {
"entrypoint": "node $ALGO", entrypoint: 'node $ALGO',
"image": "node", image: 'node',
"tag": "10" tag: '10'
} }
}, }
} }
} }
const service1 = await ocean.assets.createAccessServiceAttributes( const service1 = await ocean.assets.createAccessServiceAttributes(
@ -200,12 +225,15 @@ describe('Marketplace flow', () => {
dateCreated, dateCreated,
0 0
) )
algorithmAsset = await ocean.assets.create(algoAsset, alice,[service1],tokenAddress) algorithmAsset = await ocean.assets.create(
algoAsset,
alice,
[service1],
tokenAddress
)
assert(algorithmAsset.dataToken === tokenAddress) assert(algorithmAsset.dataToken === tokenAddress)
}) })
it('Alice mints 100 DTs and tranfers them to the compute marketplace', async () => { it('Alice mints 100 DTs and tranfers them to the compute marketplace', async () => {
await datatoken.mint(tokenAddress, alice.getId(), tokenAmount) await datatoken.mint(tokenAddress, alice.getId(), tokenAmount)
}) })
@ -225,29 +253,32 @@ describe('Marketplace flow', () => {
}) })
}) })
it('Bob starts compute job', async () => { it('Bob starts compute job with a raw Algo', async () => {
const algorithmMeta = { const algorithmMeta = {
'language': 'scala', language: 'js',
'format': 'docker-image', format: 'docker-image',
'version': '0.1', version: '0.1',
'url': 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js', url:
'container': { 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js',
'entrypoint': 'node $ALGO', container: {
'image': 'node', entrypoint: 'node $ALGO',
'tag': '10' image: 'node',
tag: '10'
} }
} }
const output = { const output = {}
const order = await ocean.assets.order(
} ddo.id,
let order = await ocean.assets.order(ddo.id, computeService.type, bob.getId()) computeService.type,
let computeOrder=JSON.parse(order) bob.getId()
let tx=await datatoken.transfer( )
computeOrder["dataToken"], const computeOrder = JSON.parse(order)
computeOrder["to"], const tx = await datatoken.transfer(
computeOrder["numTokens"], computeOrder['dataToken'],
computeOrder["from"] computeOrder['to'],
computeOrder['numTokens'],
computeOrder['from']
) )
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo.id,
@ -262,24 +293,23 @@ describe('Marketplace flow', () => {
computeService.type computeService.type
) )
jobId = response.jobId jobId = response.jobId
assert(response.status>=10) assert(response.status >= 10)
}) })
it('Bob should get status of a compute job', async () => { it('Bob should get status of a compute job', async () => {
const response = await ocean.compute.status(bob, ddo.id, jobId) const response = await ocean.compute.status(bob, ddo.id, jobId)
assert(response[0].jobId==jobId) assert(response[0].jobId === jobId)
}) })
it('should get status of all compute jobs for an address', async () => { it('should get status of all compute jobs for an address', async () => {
const response = await ocean.compute.status(bob, undefined, undefined) const response = await ocean.compute.status(bob, undefined, undefined)
assert(response.length>0) assert(response.length > 0)
}) })
// it('should not allow order the compute service with raw algo for dataset that does not allow raw algo', async () => {})
// it('should not allow order the compute service with did != did:op:1234 for dataset that allows only did:op:1234 as algo', async () => {})
// it('should start a compute job with a published algo', async () => {
// it('Bob restarts compute job', async () => {}) // it('Bob restarts compute job', async () => {})
// it('Bob gets outputs', async () => {}) // it('Bob gets outputs', async () => {})
}) })
}) })