2020-01-21 22:09:32 +01:00
|
|
|
import { assert } from 'chai'
|
|
|
|
|
|
|
|
import { config } from '../config'
|
2020-03-17 12:07:54 +01:00
|
|
|
import {
|
|
|
|
Ocean,
|
|
|
|
Account,
|
|
|
|
DDO,
|
|
|
|
MetaData,
|
|
|
|
ComputeJobStatus,
|
|
|
|
Config,
|
|
|
|
MetaDataAlgorithm
|
|
|
|
} from '../../../src'
|
2020-03-17 12:41:49 +01:00
|
|
|
import { getMetadata } from '../utils'
|
2020-05-10 09:49:13 +02:00
|
|
|
import { ServiceCompute, ServiceComputePrivacy } from '../../../src/ddo/Service'
|
2020-03-10 12:30:06 +01:00
|
|
|
|
2020-01-29 20:24:28 +01:00
|
|
|
const metadataAsset = getMetadata()
|
|
|
|
const metadataAlgorithm = getMetadata(0, 'algorithm')
|
|
|
|
|
2020-02-01 18:33:17 +01:00
|
|
|
const customConfig: Config = {
|
|
|
|
...config,
|
2020-02-03 19:02:42 +01:00
|
|
|
// nodeUri: 'https://nile.dev-ocean.com',
|
|
|
|
// aquariusUri: 'https://aquarius.nile.dev-ocean.com',
|
|
|
|
// brizoUri: 'http://89.46.156.10:8030',
|
|
|
|
// secretStoreUri: 'https://secret-store.nile.dev-ocean.com',
|
|
|
|
// brizoAddress: '0x413c9ba0a05b8a600899b41b0c62dd661e689354',
|
2020-02-01 18:33:17 +01:00
|
|
|
verbose: true
|
|
|
|
}
|
|
|
|
|
2020-05-10 09:49:13 +02:00
|
|
|
export const rawAlgoMeta = {
|
|
|
|
rawcode: `console.log('Hello world'!)`,
|
|
|
|
format: 'docker-image',
|
|
|
|
version: '0.1',
|
|
|
|
container: {
|
|
|
|
entrypoint: 'node $ALGO',
|
|
|
|
image: 'node',
|
|
|
|
tag: '10'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:32 +01:00
|
|
|
describe('Compute', () => {
|
|
|
|
let ocean: Ocean
|
|
|
|
let account: Account
|
2020-01-27 15:59:26 +01:00
|
|
|
let agreementId: string
|
2020-01-29 20:24:28 +01:00
|
|
|
let dataset: DDO
|
2020-05-10 09:49:13 +02:00
|
|
|
let datasetNoRawAlgo: DDO
|
|
|
|
let datasetWithTrustedAlgo: DDO
|
2020-01-29 20:24:28 +01:00
|
|
|
let algorithm: DDO
|
2020-01-31 13:09:37 +01:00
|
|
|
let computeService: ServiceCompute
|
2020-04-08 09:44:36 +02:00
|
|
|
let jobId: string
|
2020-01-21 22:09:32 +01:00
|
|
|
|
|
|
|
before(async () => {
|
2020-02-01 18:33:17 +01:00
|
|
|
ocean = await Ocean.getInstance(customConfig)
|
2020-01-21 22:09:32 +01:00
|
|
|
;[account] = await ocean.accounts.list()
|
2020-01-27 19:14:12 +01:00
|
|
|
})
|
|
|
|
|
2020-04-08 11:26:48 +02:00
|
|
|
/* it('should authenticate the consumer account', async () => {
|
2020-01-27 19:14:12 +01:00
|
|
|
await account.authenticate()
|
|
|
|
})
|
2020-04-08 11:26:48 +02:00
|
|
|
*/
|
2020-01-29 20:24:28 +01:00
|
|
|
it('should publish a dataset with a compute service object', async () => {
|
2020-01-27 19:14:12 +01:00
|
|
|
const stepsAsset = []
|
2020-03-17 11:57:36 +01:00
|
|
|
computeService = await ocean.compute.createComputeServiceAttributes(
|
|
|
|
account,
|
|
|
|
'1000',
|
|
|
|
metadataAsset.main.datePublished
|
|
|
|
)
|
2020-01-29 20:24:28 +01:00
|
|
|
dataset = await ocean.assets
|
2020-01-31 13:09:37 +01:00
|
|
|
.create(metadataAsset as MetaData, account, [computeService])
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => stepsAsset.push(step))
|
2020-01-27 19:14:12 +01:00
|
|
|
|
2020-01-29 20:24:28 +01:00
|
|
|
assert.instanceOf(dataset, DDO)
|
2020-01-31 13:09:37 +01:00
|
|
|
assert.isDefined(
|
|
|
|
dataset.findServiceByType('compute'),
|
|
|
|
`DDO compute service doesn't exist`
|
|
|
|
)
|
2020-01-27 19:14:12 +01:00
|
|
|
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
|
2020-01-29 20:24:28 +01:00
|
|
|
})
|
2020-01-27 19:14:12 +01:00
|
|
|
|
2020-05-10 09:49:13 +02:00
|
|
|
it('should publish a dataset with a compute service object that does not allow rawAlgo', async () => {
|
|
|
|
const stepsAsset = []
|
|
|
|
const origComputePrivacy = {
|
|
|
|
allowRawAlgorithm: false,
|
|
|
|
allowNetworkAccess: false,
|
|
|
|
trustedAlgorithms: []
|
|
|
|
}
|
|
|
|
|
|
|
|
computeService = await ocean.compute.createComputeServiceAttributes(
|
|
|
|
account,
|
|
|
|
'1000',
|
|
|
|
metadataAsset.main.datePublished,
|
|
|
|
origComputePrivacy as ServiceComputePrivacy
|
|
|
|
)
|
|
|
|
datasetNoRawAlgo = await ocean.assets
|
|
|
|
.create(metadataAsset as MetaData, account, [computeService])
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => stepsAsset.push(step))
|
2020-05-10 09:49:13 +02:00
|
|
|
|
|
|
|
assert.instanceOf(datasetNoRawAlgo, DDO)
|
|
|
|
assert.isDefined(
|
|
|
|
dataset.findServiceByType('compute'),
|
|
|
|
`DDO compute service doesn't exist`
|
|
|
|
)
|
|
|
|
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should publish a dataset with a compute service object that allows only algo with did:op:1234', async () => {
|
|
|
|
const stepsAsset = []
|
|
|
|
const origComputePrivacy = {
|
|
|
|
allowRawAlgorithm: false,
|
|
|
|
allowNetworkAccess: false,
|
|
|
|
trustedAlgorithms: ['did:op:1234']
|
|
|
|
}
|
|
|
|
|
|
|
|
computeService = await ocean.compute.createComputeServiceAttributes(
|
|
|
|
account,
|
|
|
|
'1000',
|
|
|
|
metadataAsset.main.datePublished,
|
|
|
|
origComputePrivacy as ServiceComputePrivacy
|
|
|
|
)
|
|
|
|
datasetWithTrustedAlgo = await ocean.assets
|
|
|
|
.create(metadataAsset as MetaData, account, [computeService])
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => stepsAsset.push(step))
|
2020-05-10 09:49:13 +02:00
|
|
|
|
|
|
|
assert.instanceOf(datasetWithTrustedAlgo, DDO)
|
|
|
|
assert.isDefined(
|
|
|
|
dataset.findServiceByType('compute'),
|
|
|
|
`DDO compute service doesn't exist`
|
|
|
|
)
|
|
|
|
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
|
|
|
|
})
|
|
|
|
|
2020-01-29 20:24:28 +01:00
|
|
|
it('should publish an algorithm', async () => {
|
2020-01-27 19:14:12 +01:00
|
|
|
const stepsAlgorithm = []
|
2020-01-29 20:24:28 +01:00
|
|
|
algorithm = await ocean.assets
|
2020-01-27 19:14:12 +01:00
|
|
|
.create(metadataAlgorithm as MetaData, account)
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => stepsAlgorithm.push(step))
|
2020-01-27 19:14:12 +01:00
|
|
|
|
2020-01-29 20:24:28 +01:00
|
|
|
assert.instanceOf(algorithm, DDO)
|
2020-01-27 19:14:12 +01:00
|
|
|
assert.deepEqual(stepsAlgorithm, [0, 1, 2, 3, 4, 5, 6, 7])
|
|
|
|
})
|
|
|
|
|
2020-05-10 09:49:13 +02:00
|
|
|
it('should not allow order the compute service with raw algo for dataset that does not allow raw algo', async () => {
|
|
|
|
const steps = []
|
|
|
|
|
|
|
|
agreementId = await ocean.compute
|
|
|
|
.order(account, datasetNoRawAlgo.id, null, rawAlgoMeta)
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => steps.push(step))
|
2020-05-10 09:49:13 +02:00
|
|
|
|
|
|
|
assert.equal(agreementId, null)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not allow order the compute service with did != did:op:1234 for dataset that allows only did:op:1234 as algo', async () => {
|
|
|
|
const steps = []
|
|
|
|
|
|
|
|
agreementId = await ocean.compute
|
|
|
|
.order(account, datasetWithTrustedAlgo.id, 'did:op:233454', null)
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => steps.push(step))
|
2020-05-10 09:49:13 +02:00
|
|
|
|
|
|
|
assert.equal(agreementId, null)
|
|
|
|
})
|
|
|
|
|
2020-01-29 20:24:28 +01:00
|
|
|
it('should order the compute service of the dataset', async () => {
|
2020-01-27 19:14:12 +01:00
|
|
|
const steps = []
|
|
|
|
try {
|
|
|
|
await account.requestTokens(
|
2020-03-06 11:35:31 +01:00
|
|
|
parseInt(
|
|
|
|
(
|
|
|
|
+computeService.attributes.main.price *
|
|
|
|
10 ** -(await ocean.keeper.token.decimals())
|
|
|
|
).toString()
|
|
|
|
)
|
2020-01-27 19:14:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
agreementId = await ocean.compute
|
2020-01-29 20:24:28 +01:00
|
|
|
.order(account, dataset.id)
|
2020-05-19 16:15:56 +02:00
|
|
|
.next((step) => steps.push(step))
|
2020-01-27 15:59:26 +01:00
|
|
|
|
2020-02-03 19:02:42 +01:00
|
|
|
console.log(agreementId)
|
2020-01-31 13:09:37 +01:00
|
|
|
assert.isDefined(agreementId)
|
|
|
|
assert.deepEqual(steps, [0, 1, 2, 3])
|
|
|
|
} catch {}
|
2020-01-21 22:09:32 +01:00
|
|
|
})
|
|
|
|
|
2020-03-16 13:40:43 +01:00
|
|
|
it('should start a compute job with a published algo', async () => {
|
2020-01-29 20:24:28 +01:00
|
|
|
const response = await ocean.compute.start(account, agreementId, algorithm.id)
|
2020-01-21 22:09:32 +01:00
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
assert.equal(response.status, ComputeJobStatus.Started)
|
2020-01-21 22:09:32 +01:00
|
|
|
})
|
2020-03-16 13:40:43 +01:00
|
|
|
|
|
|
|
it('should start a compute job with a rawcode algo', async () => {
|
|
|
|
const algoMeta: MetaDataAlgorithm = {
|
|
|
|
rawcode: `console.log('Hello world!')`,
|
|
|
|
container: {
|
|
|
|
entrypoint: 'node $ALGO',
|
|
|
|
image: 'node',
|
|
|
|
tag: '10'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const response = await ocean.compute.start(
|
|
|
|
account,
|
|
|
|
agreementId,
|
|
|
|
undefined,
|
|
|
|
algoMeta
|
|
|
|
)
|
|
|
|
|
2020-04-14 15:19:30 +02:00
|
|
|
assert.isAtLeast(response.status, ComputeJobStatus.Started)
|
2020-04-08 09:44:36 +02:00
|
|
|
jobId = response.jobId
|
2020-04-08 11:26:48 +02:00
|
|
|
})
|
|
|
|
|
2020-04-08 09:44:36 +02:00
|
|
|
it('should get status of a compute job', async () => {
|
2020-04-08 11:26:48 +02:00
|
|
|
const response = await ocean.compute.status(account, agreementId, jobId)
|
|
|
|
|
|
|
|
assert.equal(response[0].jobId, jobId)
|
2020-04-08 09:44:36 +02:00
|
|
|
})
|
2020-04-08 11:26:48 +02:00
|
|
|
|
2020-04-08 09:44:36 +02:00
|
|
|
it('should get status of all compute jobs for an address', async () => {
|
2020-04-08 11:26:48 +02:00
|
|
|
const response = await ocean.compute.status(account, undefined, undefined)
|
|
|
|
|
|
|
|
assert.isAbove(response.length, 0)
|
2020-03-16 13:40:43 +01:00
|
|
|
})
|
2020-01-21 22:09:32 +01:00
|
|
|
})
|