mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #828 from oceanprotocol/feature/issue777-add-ddo-param-when-did-exists
Add also DDO object param where DID param is available
This commit is contained in:
commit
666da8156f
@ -5,7 +5,7 @@ import { Service, ServiceAccess } from '../ddo/interfaces/Service'
|
||||
import { EditableMetadata } from '../ddo/interfaces/EditableMetadata'
|
||||
import Account from './Account'
|
||||
import DID from './DID'
|
||||
import { SubscribablePromise, didNoZeroX, didPrefixed } from '../utils'
|
||||
import { SubscribablePromise, didNoZeroX, didPrefixed, assetResolve } from '../utils'
|
||||
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
||||
import { WebServiceConnector } from './utils/WebServiceConnector'
|
||||
import BigNumber from 'bignumber.js'
|
||||
@ -406,18 +406,18 @@ export class Assets extends Instantiable {
|
||||
|
||||
/**
|
||||
* Returns the creator of a asset.
|
||||
* @param {string} did Decentralized ID.
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @return {Promise<string>} Returns eth address
|
||||
*/
|
||||
public async creator(did: string): Promise<string> {
|
||||
const ddo = await this.resolve(did)
|
||||
public async creator(asset: DDO | string): Promise<string> {
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const checksum = ddo.getChecksum()
|
||||
const { creator, signatureValue } = ddo.proof
|
||||
const signer = await this.ocean.utils.signature.verifyText(checksum, signatureValue)
|
||||
|
||||
if (signer.toLowerCase() !== creator.toLowerCase()) {
|
||||
this.logger.warn(
|
||||
`Owner of ${ddo.id} doesn't match. Expected ${creator} instead of ${signer}.`
|
||||
`Owner of ${did} doesn't match. Expected ${creator} instead of ${signer}.`
|
||||
)
|
||||
}
|
||||
|
||||
@ -453,9 +453,13 @@ export class Assets extends Instantiable {
|
||||
} as SearchQuery)
|
||||
}
|
||||
|
||||
public async getServiceByType(did: string, serviceType: string): Promise<Service> {
|
||||
public async getServiceByType(
|
||||
asset: DDO | string,
|
||||
serviceType: string
|
||||
): Promise<Service> {
|
||||
const { ddo } = await assetResolve(asset, this.ocean)
|
||||
let service: Service
|
||||
const services: Service[] = (await this.resolve(did)).service
|
||||
const services: Service[] = ddo.service
|
||||
|
||||
services.forEach((serv) => {
|
||||
if (serv.type.toString() === serviceType) {
|
||||
@ -465,9 +469,13 @@ export class Assets extends Instantiable {
|
||||
return service
|
||||
}
|
||||
|
||||
public async getServiceByIndex(did: string, serviceIndex: number): Promise<Service> {
|
||||
public async getServiceByIndex(
|
||||
asset: DDO | string,
|
||||
serviceIndex: number
|
||||
): Promise<Service> {
|
||||
const { ddo } = await assetResolve(asset, this.ocean)
|
||||
let service: Service
|
||||
const services: Service[] = (await this.resolve(did)).service
|
||||
const services: Service[] = ddo.service
|
||||
|
||||
services.forEach((serv) => {
|
||||
if (serv.index === serviceIndex) {
|
||||
@ -511,7 +519,7 @@ export class Assets extends Instantiable {
|
||||
/**
|
||||
* Initialize a service
|
||||
* Can be used to compute totalCost for ordering a service
|
||||
* @param {String} did
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {String} serviceType
|
||||
* @param {String} consumerAddress
|
||||
* @param {Number} serviceIndex
|
||||
@ -519,7 +527,7 @@ export class Assets extends Instantiable {
|
||||
* @return {Promise<any>} Order details
|
||||
*/
|
||||
public async initialize(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceType: string,
|
||||
consumerAddress: string,
|
||||
serviceIndex = -1,
|
||||
@ -527,7 +535,12 @@ export class Assets extends Instantiable {
|
||||
): Promise<any> {
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
await provider.setBaseUrl(serviceEndpoint)
|
||||
const res = await provider.initialize(did, serviceIndex, serviceType, consumerAddress)
|
||||
const res = await provider.initialize(
|
||||
asset,
|
||||
serviceIndex,
|
||||
serviceType,
|
||||
consumerAddress
|
||||
)
|
||||
if (res === null) return null
|
||||
const providerData = JSON.parse(res)
|
||||
return providerData
|
||||
@ -535,7 +548,7 @@ export class Assets extends Instantiable {
|
||||
|
||||
/**
|
||||
* Orders & pays for a service
|
||||
* @param {String} did
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {String} serviceType
|
||||
* @param {String} payerAddress
|
||||
* @param {Number} serviceIndex
|
||||
@ -544,7 +557,7 @@ export class Assets extends Instantiable {
|
||||
* @return {Promise<String>} transactionHash of the payment
|
||||
*/
|
||||
public async order(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceType: string,
|
||||
payerAddress: string,
|
||||
serviceIndex = -1,
|
||||
@ -553,8 +566,7 @@ export class Assets extends Instantiable {
|
||||
searchPreviousOrders = true
|
||||
): Promise<string> {
|
||||
let service: Service
|
||||
|
||||
const ddo = await this.resolve(did)
|
||||
const { ddo } = await assetResolve(asset, this.ocean)
|
||||
const consumable = await this.isConsumable(ddo, consumerAddress)
|
||||
if (consumable.status > 0) {
|
||||
throw new Error(`Order asset failed, ` + consumable.message)
|
||||
@ -562,15 +574,15 @@ export class Assets extends Instantiable {
|
||||
|
||||
if (!consumerAddress) consumerAddress = payerAddress
|
||||
if (serviceIndex === -1) {
|
||||
service = await this.getServiceByType(did, serviceType)
|
||||
service = await this.getServiceByType(ddo, serviceType)
|
||||
serviceIndex = service.index
|
||||
} else {
|
||||
service = await this.getServiceByIndex(did, serviceIndex)
|
||||
service = await this.getServiceByIndex(ddo, serviceIndex)
|
||||
serviceType = service.type
|
||||
}
|
||||
try {
|
||||
const providerData = await this.initialize(
|
||||
did,
|
||||
ddo,
|
||||
serviceType,
|
||||
payerAddress,
|
||||
serviceIndex,
|
||||
@ -625,13 +637,13 @@ export class Assets extends Instantiable {
|
||||
|
||||
// marketplace flow
|
||||
public async download(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
txId: string,
|
||||
tokenAddress: string,
|
||||
consumerAccount: Account,
|
||||
destination: string
|
||||
): Promise<string | true> {
|
||||
const ddo = await this.resolve(did)
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const { attributes } = ddo.findServiceByType('metadata')
|
||||
const service = ddo.findServiceByType('access')
|
||||
const { files } = attributes.main
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { DDO } from '../ddo/DDO'
|
||||
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
|
||||
import {
|
||||
Service,
|
||||
ServiceComputePrivacy,
|
||||
@ -7,7 +6,7 @@ import {
|
||||
publisherTrustedAlgorithm
|
||||
} from '../ddo/interfaces/Service'
|
||||
import Account from './Account'
|
||||
import { SubscribablePromise } from '../utils'
|
||||
import { SubscribablePromise, assetResolve, AssetResolved } from '../utils'
|
||||
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
||||
import {
|
||||
ComputeOutput,
|
||||
@ -98,7 +97,7 @@ export class Compute extends Instantiable {
|
||||
|
||||
/**
|
||||
* Start the execution of a compute job.
|
||||
* @param {string} did Decentralized identifer for the asset
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {string} txId
|
||||
* @param {string} tokenAddress
|
||||
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||
@ -108,7 +107,7 @@ export class Compute extends Instantiable {
|
||||
* @return {Promise<ComputeJob>} Returns compute job ID under status.jobId
|
||||
*/
|
||||
public async start(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
txId: string,
|
||||
tokenAddress: string,
|
||||
consumerAccount: Account,
|
||||
@ -119,7 +118,7 @@ export class Compute extends Instantiable {
|
||||
additionalInputs?: ComputeInput[]
|
||||
): Promise<ComputeJob> {
|
||||
output = this.checkOutput(consumerAccount, output)
|
||||
const ddo = await this.ocean.assets.resolve(did)
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
if (did && txId) {
|
||||
@ -145,16 +144,16 @@ export class Compute extends Instantiable {
|
||||
/**
|
||||
* Ends a running compute job.
|
||||
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||
* @param {string} did Decentralized identifier.
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {string} jobId The ID of the compute job to be stopped
|
||||
* @return {Promise<ComputeJob>} Returns the new status of a job
|
||||
*/
|
||||
public async stop(
|
||||
consumerAccount: Account,
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
const ddo = await this.ocean.assets.resolve(did)
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
@ -167,16 +166,17 @@ export class Compute extends Instantiable {
|
||||
/**
|
||||
* Deletes a compute job and all resources associated with the job. If job is running it will be stopped first.
|
||||
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||
* @param {string} did Decentralized identifier.
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {string} jobId The ID of the compute job to be stopped
|
||||
* @param {DDO} ddo If undefined then the ddo will be fetched by did
|
||||
* @return {Promise<ComputeJob>} Returns the new status of a job
|
||||
*/
|
||||
public async delete(
|
||||
consumerAccount: Account,
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
const ddo = await this.ocean.assets.resolve(did)
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
@ -241,16 +241,16 @@ export class Compute extends Instantiable {
|
||||
/**
|
||||
* Returns the final result of a specific compute job published as an asset.
|
||||
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||
* @param {string} did Decentralized identifier.
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {string} jobId The ID of the compute job to be stopped.
|
||||
* @return {Promise<ComputeJob>} Returns the DDO of the result asset.
|
||||
*/
|
||||
public async result(
|
||||
consumerAccount: Account,
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
const ddo = await this.ocean.assets.resolve(did)
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
@ -397,10 +397,10 @@ export class Compute extends Instantiable {
|
||||
|
||||
/**
|
||||
* Checks if an asset is orderable with a specific algorithm
|
||||
* @param {string} datasetDid The DID of the asset (of type `dataset`) to run the algorithm on.
|
||||
* @param {DDO|string} dataset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier of the asset (of type `dataset`) to run the algorithm on.
|
||||
* @param {string} serviceIndex The Service index
|
||||
* @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset.
|
||||
* @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified.
|
||||
* @param {ComputeAlgorithm} algorithm
|
||||
* @param {DDO} algorithmDDO Algorithm DDO object. If undefined then the ddo will be fetched by did
|
||||
* @return {Promise<boolean>} True is you can order this
|
||||
*
|
||||
* Note: algorithmDid and algorithmMeta are optional, but if they are not passed,
|
||||
@ -408,12 +408,13 @@ export class Compute extends Instantiable {
|
||||
* but provider will not allow the compute, due to privacy settings of the ddo
|
||||
*/
|
||||
public async isOrderable(
|
||||
datasetDid: string,
|
||||
dataset: DDO | string,
|
||||
serviceIndex: number,
|
||||
algorithm: ComputeAlgorithm
|
||||
algorithm: ComputeAlgorithm,
|
||||
algorithmDDO?: DDO
|
||||
): Promise<boolean> {
|
||||
const ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
||||
const service: Service = ddo.findServiceById(serviceIndex)
|
||||
const datasetResolved: AssetResolved = await assetResolve(dataset, this.ocean)
|
||||
const service: Service = datasetResolved.ddo.findServiceById(serviceIndex)
|
||||
if (!service) return false
|
||||
if (service.type === 'compute') {
|
||||
if (algorithm.meta) {
|
||||
@ -426,8 +427,14 @@ export class Compute extends Instantiable {
|
||||
if (algorithm.did) {
|
||||
// check if both have compute services and then if they are served by the same provider
|
||||
if (algorithm.serviceIndex) {
|
||||
const algoDDO: DDO = await this.ocean.assets.resolve(algorithm.did)
|
||||
const algoService: Service = algoDDO.findServiceById(algorithm.serviceIndex)
|
||||
if (!algorithmDDO) {
|
||||
algorithmDDO = await this.ocean.assets.resolve(algorithm.did)
|
||||
if (!algorithmDDO)
|
||||
throw new Error(`Couldn't resolve the did ${algorithm.did}`)
|
||||
}
|
||||
const algoService: Service = algorithmDDO.findServiceById(
|
||||
algorithm.serviceIndex
|
||||
)
|
||||
if (algoService && algoService.type === 'compute') {
|
||||
// since both dataset & algo services are compute, we need to check if they are served by the same provider
|
||||
const algoProvider = await Provider.getInstance(this.instanceConfig)
|
||||
@ -460,7 +467,7 @@ export class Compute extends Instantiable {
|
||||
) {
|
||||
this.logger.error(
|
||||
'ERROR: Algorithm container section was altered since it was added as trusted by ' +
|
||||
datasetDid
|
||||
datasetResolved.did
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -470,7 +477,7 @@ export class Compute extends Instantiable {
|
||||
) {
|
||||
this.logger.error(
|
||||
'ERROR: Algorithm files section was altered since it was added as trusted by ' +
|
||||
datasetDid
|
||||
datasetResolved.ddo
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -480,7 +487,10 @@ export class Compute extends Instantiable {
|
||||
}
|
||||
// algorithmDid was not found
|
||||
this.logger.error(
|
||||
'ERROR: Algorithm ' + algorithm.did + ' is not allowed by ' + datasetDid
|
||||
'ERROR: Algorithm ' +
|
||||
algorithm.did +
|
||||
' is not allowed by ' +
|
||||
datasetResolved.did
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -493,7 +503,7 @@ export class Compute extends Instantiable {
|
||||
/**
|
||||
* Starts an order of a compute or access service for a compute job
|
||||
* @param {String} consumerAccount The account of the consumer ordering the service.
|
||||
* @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on.
|
||||
* @param {DDO|string} dataset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier of the asset (of type `dataset`) to run the algorithm on.
|
||||
* @param {string} serviceIndex The Service index
|
||||
* @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset.
|
||||
* @param {string} algorithmServiceIndex The index of the service in the algorithm
|
||||
@ -506,7 +516,7 @@ export class Compute extends Instantiable {
|
||||
*/
|
||||
public orderAsset(
|
||||
consumerAccount: string,
|
||||
datasetDid: string,
|
||||
dataset: DDO | string,
|
||||
serviceIndex: number,
|
||||
algorithm: ComputeAlgorithm,
|
||||
mpAddress?: string,
|
||||
@ -514,20 +524,20 @@ export class Compute extends Instantiable {
|
||||
searchPreviousOrders = true
|
||||
): SubscribablePromise<OrderProgressStep, string> {
|
||||
return new SubscribablePromise(async (observer) => {
|
||||
const { ddo } = await assetResolve(dataset, this.ocean)
|
||||
// first check if we can order this
|
||||
const allowed = await this.isOrderable(datasetDid, serviceIndex, algorithm)
|
||||
const allowed = await this.isOrderable(ddo, serviceIndex, algorithm)
|
||||
if (!allowed)
|
||||
throw new Error(
|
||||
`Dataset order failed, dataset is not orderable with the specified algorithm`
|
||||
)
|
||||
const ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
||||
// const service: Service = ddo.findServiceByType('compute')
|
||||
const service: Service = ddo.findServiceById(serviceIndex)
|
||||
if (!service)
|
||||
throw new Error(`Dataset order failed, Could not find service for the DDO`)
|
||||
try {
|
||||
const order = await this.ocean.assets.order(
|
||||
datasetDid,
|
||||
ddo,
|
||||
service.type,
|
||||
consumerAccount,
|
||||
-1,
|
||||
@ -545,7 +555,7 @@ export class Compute extends Instantiable {
|
||||
|
||||
/**
|
||||
* Orders & pays for a algorithm
|
||||
* @param {String} did
|
||||
* @param {DDO|string} asset DID Descriptor Object containing all the data related to an asset or a Decentralized identifier.
|
||||
* @param {String} serviceType
|
||||
* @param {String} payerAddress
|
||||
* @param {Number} serviceIndex
|
||||
@ -554,7 +564,7 @@ export class Compute extends Instantiable {
|
||||
* @return {Promise<String>} transactionHash of the payment
|
||||
*/
|
||||
public async orderAlgorithm(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceType: string,
|
||||
payerAddress: string,
|
||||
serviceIndex = -1,
|
||||
@ -565,7 +575,7 @@ export class Compute extends Instantiable {
|
||||
// this is only a convienince function, which calls ocean.assets.order
|
||||
try {
|
||||
return await this.ocean.assets.order(
|
||||
did,
|
||||
asset,
|
||||
serviceType,
|
||||
payerAddress,
|
||||
serviceIndex,
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Account from '../ocean/Account'
|
||||
import { noZeroX } from '../utils'
|
||||
import { noZeroX, assetResolve } from '../utils'
|
||||
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
||||
import { File } from '../ddo/interfaces/File'
|
||||
import {
|
||||
@ -8,8 +8,6 @@ import {
|
||||
ComputeOutput,
|
||||
ComputeAlgorithm
|
||||
} from '../ocean/interfaces/Compute'
|
||||
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
|
||||
import { Versions } from '../ocean/Versions'
|
||||
import { DDO } from '../ddo/DDO'
|
||||
import DID from '../ocean/DID'
|
||||
import { Service } from '../ddo/interfaces'
|
||||
@ -19,6 +17,7 @@ export interface ServiceEndpoint {
|
||||
method: string
|
||||
urlPath: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides an interface for provider service.
|
||||
* Provider service is the technical component executed
|
||||
@ -186,19 +185,12 @@ export class Provider extends Instantiable {
|
||||
}
|
||||
|
||||
public async initialize(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceIndex: number,
|
||||
serviceType: string,
|
||||
consumerAddress: string
|
||||
): Promise<string> {
|
||||
let DDO: DDO
|
||||
|
||||
try {
|
||||
DDO = await this.ocean.assets.resolve(did)
|
||||
} catch (e) {
|
||||
this.logger.error(e)
|
||||
throw new Error('Failed to resolve DID')
|
||||
}
|
||||
const { did, ddo } = await assetResolve(asset, this.ocean)
|
||||
let initializeUrl = this.getInitializeEndpoint()
|
||||
? this.getInitializeEndpoint().urlPath
|
||||
: null
|
||||
@ -206,7 +198,7 @@ export class Provider extends Instantiable {
|
||||
initializeUrl += `?documentId=${did}`
|
||||
initializeUrl += `&serviceId=${serviceIndex}`
|
||||
initializeUrl += `&serviceType=${serviceType}`
|
||||
initializeUrl += `&dataToken=${DDO.dataToken}`
|
||||
initializeUrl += `&dataToken=${ddo.dataToken}`
|
||||
initializeUrl += `&consumerAddress=${consumerAddress}`
|
||||
try {
|
||||
const response = await this.ocean.utils.fetch.get(initializeUrl)
|
||||
|
26
src/utils/AssetResolverHelper.ts
Normal file
26
src/utils/AssetResolverHelper.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { DDO } from '../ddo/DDO'
|
||||
import { Ocean } from '../ocean/Ocean'
|
||||
|
||||
export interface AssetResolved {
|
||||
did: string
|
||||
ddo: DDO
|
||||
}
|
||||
|
||||
function isDdo(arg: any): arg is DDO {
|
||||
return arg.id !== undefined
|
||||
}
|
||||
|
||||
export async function assetResolve(
|
||||
asset: DDO | string,
|
||||
ocean: Ocean
|
||||
): Promise<AssetResolved> {
|
||||
if (isDdo(asset)) {
|
||||
const did = asset.id
|
||||
const ddo = asset
|
||||
return { did, ddo }
|
||||
} else {
|
||||
const ddo = await ocean.assets.resolve(asset)
|
||||
const did = ddo.id
|
||||
return { did, ddo }
|
||||
}
|
||||
}
|
@ -5,3 +5,4 @@ export * from './GeneratorHelpers'
|
||||
export * from './SubscribablePromise'
|
||||
export * from './SubscribableObserver'
|
||||
export * from './GasUtils'
|
||||
export * from './AssetResolverHelper'
|
||||
|
@ -4,11 +4,7 @@ import { DataTokens } from '../../src/datatokens/Datatokens'
|
||||
import { Ocean } from '../../src/ocean/Ocean'
|
||||
import { ConfigHelper } from '../../src/utils/ConfigHelper'
|
||||
import { assert } from 'chai'
|
||||
import {
|
||||
Service,
|
||||
ServiceComputePrivacy,
|
||||
publisherTrustedAlgorithm
|
||||
} from '../../src/ddo/interfaces/Service'
|
||||
import { ServiceComputePrivacy } from '../../src/ddo/interfaces/Service'
|
||||
import Web3 from 'web3'
|
||||
import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json'
|
||||
import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
|
||||
@ -838,14 +834,14 @@ describe('Compute flow', () => {
|
||||
meta: algorithmMeta
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
)
|
||||
assert(allowed === true)
|
||||
computeOrderId = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -853,7 +849,7 @@ describe('Compute flow', () => {
|
||||
)
|
||||
assert(computeOrderId != null, 'computeOrderId === null')
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeOrderId,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -918,7 +914,7 @@ describe('Compute flow', () => {
|
||||
})
|
||||
it('Bob should stop compute job', async () => {
|
||||
assert(jobId != null, 'Jobid is null')
|
||||
await ocean.compute.stop(bob, ddo.id, jobId)
|
||||
await ocean.compute.stop(bob, ddo, jobId)
|
||||
const response = await ocean.compute.status(bob, ddo.id, undefined, undefined, jobId)
|
||||
// TODO: typings say that `stopreq` does not exist
|
||||
assert((response[0] as any).stopreq === 1, 'Response.stopreq is invalid')
|
||||
@ -937,7 +933,7 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetNoRawAlgo.id,
|
||||
datasetNoRawAlgo,
|
||||
service1.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -962,7 +958,7 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
datasetWithTrustedAlgo.id,
|
||||
datasetWithTrustedAlgo,
|
||||
service1.index,
|
||||
algoDefinition
|
||||
)
|
||||
@ -972,7 +968,7 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetWithTrustedAlgo.id,
|
||||
datasetWithTrustedAlgo,
|
||||
service1.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -993,16 +989,17 @@ describe('Compute flow', () => {
|
||||
did: algorithmAsset.id
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === false)
|
||||
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1092,15 +1089,16 @@ describe('Compute flow', () => {
|
||||
serviceIndex: serviceAlgo.index
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAssetRemoteProviderWithCompute
|
||||
)
|
||||
assert(allowed === false)
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1125,14 +1123,15 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAssetwithCompute
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1141,7 +1140,7 @@ describe('Compute flow', () => {
|
||||
assert(order != null, 'Order should not be null')
|
||||
// order the algorithm
|
||||
const orderalgo = await ocean.compute.orderAlgorithm(
|
||||
algorithmAssetwithCompute.id,
|
||||
algorithmAssetwithCompute,
|
||||
serviceAlgo.type,
|
||||
bob.getId(),
|
||||
serviceAlgo.index,
|
||||
@ -1152,14 +1151,15 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAssetwithCompute.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
algoDefinition,
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type
|
||||
computeService.type,
|
||||
undefined
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
jobId = response.jobId
|
||||
@ -1181,14 +1181,15 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1197,7 +1198,7 @@ describe('Compute flow', () => {
|
||||
assert(order != null, 'Order should not be null')
|
||||
// order the algorithm
|
||||
const orderalgo = await ocean.compute.orderAlgorithm(
|
||||
algorithmAsset.id,
|
||||
algorithmAsset,
|
||||
serviceAlgo.type,
|
||||
bob.getId(),
|
||||
serviceAlgo.index,
|
||||
@ -1208,14 +1209,15 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAsset.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
algoDefinition,
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type
|
||||
computeService.type,
|
||||
undefined
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
jobId = response.jobId
|
||||
@ -1240,14 +1242,15 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAssetRemoteProvider
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1255,7 +1258,7 @@ describe('Compute flow', () => {
|
||||
)
|
||||
assert(order != null, 'Order should not be null')
|
||||
const orderalgo = await ocean.compute.orderAlgorithm(
|
||||
algorithmAssetRemoteProvider.id,
|
||||
algorithmAssetRemoteProvider,
|
||||
serviceAlgo.type,
|
||||
bob.getId(),
|
||||
serviceAlgo.index,
|
||||
@ -1266,7 +1269,7 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAssetRemoteProvider.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1294,14 +1297,15 @@ describe('Compute flow', () => {
|
||||
}
|
||||
let allowed
|
||||
allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1310,7 +1314,7 @@ describe('Compute flow', () => {
|
||||
assert(order != null, 'Order should not be null')
|
||||
// order the algo
|
||||
const orderalgo = await ocean.compute.orderAlgorithm(
|
||||
algorithmAsset.id,
|
||||
algorithmAsset,
|
||||
serviceAlgo.type,
|
||||
bob.getId(),
|
||||
serviceAlgo.index,
|
||||
@ -1322,14 +1326,15 @@ describe('Compute flow', () => {
|
||||
assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null')
|
||||
const inputOrder1Service = ddoAdditional1.findServiceByType('compute')
|
||||
allowed = await ocean.compute.isOrderable(
|
||||
ddoAdditional1.id,
|
||||
ddoAdditional1,
|
||||
inputOrder1Service.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
ddoAdditional1
|
||||
)
|
||||
assert(allowed === true)
|
||||
const inputOrder1 = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddoAdditional1.id,
|
||||
ddoAdditional1,
|
||||
inputOrder1Service.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1340,14 +1345,15 @@ describe('Compute flow', () => {
|
||||
// 2nd additional input
|
||||
const inputOrder2Service = ddoAdditional2.findServiceByType('access')
|
||||
allowed = await ocean.compute.isOrderable(
|
||||
ddoAdditional2.id,
|
||||
ddoAdditional2,
|
||||
inputOrder2Service.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
ddoAdditional2
|
||||
)
|
||||
assert(allowed === true)
|
||||
const inputOrder2 = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddoAdditional2.id,
|
||||
ddoAdditional2,
|
||||
inputOrder2Service.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1369,7 +1375,7 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAsset.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1462,9 +1468,10 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === false, 'This should fail, the algo container section was changed!')
|
||||
})
|
||||
@ -1498,9 +1505,10 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
algorithmAssetRemoteProvider
|
||||
)
|
||||
assert(allowed === false, 'This should fail, the algo files section was changed!')
|
||||
})
|
||||
@ -1554,9 +1562,10 @@ describe('Compute flow', () => {
|
||||
}
|
||||
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeService.index,
|
||||
algoDefinition
|
||||
algoDefinition,
|
||||
datasetWithBogusProvider
|
||||
)
|
||||
assert(allowed === false)
|
||||
// we know that it is not Orderable, but we are trying to force it
|
||||
@ -1564,7 +1573,7 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
@ -1578,7 +1587,7 @@ describe('Compute flow', () => {
|
||||
// we are forcing a bogus orderId
|
||||
computeOrderId = '1234'
|
||||
const response = await ocean.compute.start(
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeOrderId,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1604,7 +1613,7 @@ describe('Compute flow', () => {
|
||||
})
|
||||
it('Bob should fail to stop a fake compute job on a bogus provider', async () => {
|
||||
const jobid = '1234'
|
||||
const response = await ocean.compute.stop(bob, datasetWithBogusProvider.id, jobid)
|
||||
const response = await ocean.compute.stop(bob, datasetWithBogusProvider, jobid)
|
||||
assert(response === null || response === undefined, 'Invalid response')
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user