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

used type guard to detect if ddo or did and resolve just when needed

This commit is contained in:
Bogdan Fazakas 2021-06-04 16:46:12 +03:00
parent f0404f3204
commit ec2e67db35
4 changed files with 140 additions and 197 deletions

View File

@ -40,6 +40,10 @@ export interface Order {
serviceType?: string serviceType?: string
} }
function isDdo(arg: any): arg is DDO {
return arg.id !== undefined
}
/** /**
* Assets submodule of Ocean Protocol. * Assets submodule of Ocean Protocol.
*/ */
@ -335,11 +339,11 @@ export class Assets extends Instantiable {
/** /**
* Returns the creator of a asset. * 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 * @return {Promise<string>} Returns eth address
*/ */
public async creator(did: string): Promise<string> { public async creator(asset: DDO | string): Promise<string> {
const ddo = await this.resolve(did) const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
const checksum = ddo.getChecksum() const checksum = ddo.getChecksum()
const { creator, signatureValue } = ddo.proof const { creator, signatureValue } = ddo.proof
const signer = await this.ocean.utils.signature.verifyText(checksum, signatureValue) const signer = await this.ocean.utils.signature.verifyText(checksum, signatureValue)
@ -382,9 +386,13 @@ export class Assets extends Instantiable {
} as SearchQuery) } as SearchQuery)
} }
public async getServiceByType(did: string, serviceType: string): Promise<Service> { public async getServiceByType(
asset: DDO | string,
serviceType: string
): Promise<Service> {
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
let service: Service let service: Service
const services: Service[] = (await this.resolve(did)).service const services: Service[] = ddo.service
services.forEach((serv) => { services.forEach((serv) => {
if (serv.type.toString() === serviceType) { if (serv.type.toString() === serviceType) {
@ -394,9 +402,13 @@ export class Assets extends Instantiable {
return service return service
} }
public async getServiceByIndex(did: string, serviceIndex: number): Promise<Service> { public async getServiceByIndex(
asset: DDO | string,
serviceIndex: number
): Promise<Service> {
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
let service: Service let service: Service
const services: Service[] = (await this.resolve(did)).service const services: Service[] = ddo.service
services.forEach((serv) => { services.forEach((serv) => {
if (serv.index === serviceIndex) { if (serv.index === serviceIndex) {
@ -440,7 +452,7 @@ export class Assets extends Instantiable {
/** /**
* Initialize a service * Initialize a service
* Can be used to compute totalCost for ordering 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} serviceType
* @param {String} consumerAddress * @param {String} consumerAddress
* @param {Number} serviceIndex * @param {Number} serviceIndex
@ -448,21 +460,19 @@ export class Assets extends Instantiable {
* @return {Promise<any>} Order details * @return {Promise<any>} Order details
*/ */
public async initialize( public async initialize(
did: string, asset: DDO | string,
serviceType: string, serviceType: string,
consumerAddress: string, consumerAddress: string,
serviceIndex = -1, serviceIndex = -1,
serviceEndpoint: string, serviceEndpoint: string
ddo?: DDO
): Promise<any> { ): Promise<any> {
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
const res = await provider.initialize( const res = await provider.initialize(
did, asset,
serviceIndex, serviceIndex,
serviceType, serviceType,
consumerAddress, consumerAddress
ddo
) )
if (res === null) return null if (res === null) return null
const providerData = JSON.parse(res) const providerData = JSON.parse(res)
@ -471,7 +481,7 @@ export class Assets extends Instantiable {
/** /**
* Orders & pays for a service * 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} serviceType
* @param {String} payerAddress * @param {String} payerAddress
* @param {Number} serviceIndex * @param {Number} serviceIndex
@ -480,7 +490,7 @@ export class Assets extends Instantiable {
* @return {Promise<String>} transactionHash of the payment * @return {Promise<String>} transactionHash of the payment
*/ */
public async order( public async order(
did: string, asset: DDO | string,
serviceType: string, serviceType: string,
payerAddress: string, payerAddress: string,
serviceIndex = -1, serviceIndex = -1,
@ -489,27 +499,25 @@ export class Assets extends Instantiable {
searchPreviousOrders = true searchPreviousOrders = true
): Promise<string> { ): Promise<string> {
let service: Service let service: Service
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
const ddo = await this.resolve(did)
const consumable = await this.isConsumable(ddo) const consumable = await this.isConsumable(ddo)
if (consumable.status > 0) return null if (consumable.status > 0) return null
if (!consumerAddress) consumerAddress = payerAddress if (!consumerAddress) consumerAddress = payerAddress
if (serviceIndex === -1) { if (serviceIndex === -1) {
service = await this.getServiceByType(did, serviceType) service = await this.getServiceByType(ddo, serviceType)
serviceIndex = service.index serviceIndex = service.index
} else { } else {
service = await this.getServiceByIndex(did, serviceIndex) service = await this.getServiceByIndex(ddo, serviceIndex)
serviceType = service.type serviceType = service.type
} }
try { try {
const providerData = await this.initialize( const providerData = await this.initialize(
did, ddo,
serviceType, serviceType,
payerAddress, payerAddress,
serviceIndex, serviceIndex,
service.serviceEndpoint, service.serviceEndpoint
ddo
) )
if (!providerData) if (!providerData)
throw new Error( throw new Error(
@ -560,13 +568,13 @@ export class Assets extends Instantiable {
// marketplace flow // marketplace flow
public async download( public async download(
did: string, asset: DDO | string,
txId: string, txId: string,
tokenAddress: string, tokenAddress: string,
consumerAccount: Account, consumerAccount: Account,
destination: string destination: string
): Promise<string | true> { ): Promise<string | true> {
const ddo = await this.resolve(did) const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
const { attributes } = ddo.findServiceByType('metadata') const { attributes } = ddo.findServiceByType('metadata')
const service = ddo.findServiceByType('access') const service = ddo.findServiceByType('access')
const { files } = attributes.main const { files } = attributes.main
@ -586,7 +594,7 @@ export class Assets extends Instantiable {
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
await provider.download( await provider.download(
did, ddo.id,
txId, txId,
tokenAddress, tokenAddress,
service.type, service.type,

View File

@ -59,6 +59,10 @@ export const ComputeJobStatus = Object.freeze({
Deleted: 90 Deleted: 90
}) })
function isDdo(arg: any): arg is DDO {
return arg.id !== undefined
}
/** /**
* Compute submodule of Ocean Protocol. * Compute submodule of Ocean Protocol.
*/ */
@ -98,18 +102,17 @@ export class Compute extends Instantiable {
/** /**
* Start the execution of a compute job. * 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} txId
* @param {string} tokenAddress * @param {string} tokenAddress
* @param {Account} consumerAccount The account of the consumer ordering the service. * @param {Account} consumerAccount The account of the consumer ordering the service.
* @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset. * @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 {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified.
* @param {Output} output Define algorithm output publishing. Publishing the result of a compute job is turned off by default. * @param {Output} output Define algorithm output publishing. Publishing the result of a compute job is turned off by default.
* @param {DDO} ddo If undefined then the ddo will be fetched by did
* @return {Promise<ComputeJob>} Returns compute job ID under status.jobId * @return {Promise<ComputeJob>} Returns compute job ID under status.jobId
*/ */
public async start( public async start(
did: string, asset: DDO | string,
txId: string, txId: string,
tokenAddress: string, tokenAddress: string,
consumerAccount: Account, consumerAccount: Account,
@ -117,21 +120,17 @@ export class Compute extends Instantiable {
output?: ComputeOutput, output?: ComputeOutput,
serviceIndex?: string, serviceIndex?: string,
serviceType?: string, serviceType?: string,
additionalInputs?: ComputeInput[], additionalInputs?: ComputeInput[]
ddo?: DDO
): Promise<ComputeJob> { ): Promise<ComputeJob> {
output = this.checkOutput(consumerAccount, output) output = this.checkOutput(consumerAccount, output)
if (!ddo) { const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
ddo = await this.ocean.assets.resolve(did)
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
}
const service = ddo.findServiceByType('compute') const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service const { serviceEndpoint } = service
if (did && txId) { if (ddo.id && txId) {
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.computeStart( const computeJobsList = await provider.computeStart(
did, ddo.id,
consumerAccount, consumerAccount,
algorithm, algorithm,
output, output,
@ -150,26 +149,21 @@ export class Compute extends Instantiable {
/** /**
* Ends a running compute job. * Ends a running compute job.
* @param {Account} consumerAccount The account of the consumer ordering the service. * @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 {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 * @return {Promise<ComputeJob>} Returns the new status of a job
*/ */
public async stop( public async stop(
consumerAccount: Account, consumerAccount: Account,
did: string, asset: DDO | string,
jobId: string, jobId: string
ddo?: DDO
): Promise<ComputeJob> { ): Promise<ComputeJob> {
if (!ddo) { const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
ddo = await this.ocean.assets.resolve(did)
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
}
const service = ddo.findServiceByType('compute') const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service const { serviceEndpoint } = service
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.computeStop(did, consumerAccount, jobId) const computeJobsList = await provider.computeStop(ddo.id, consumerAccount, jobId)
if (computeJobsList) return computeJobsList[0] as ComputeJob if (computeJobsList) return computeJobsList[0] as ComputeJob
return null return null
} }
@ -177,26 +171,22 @@ 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. * 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 {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 {string} jobId The ID of the compute job to be stopped
* @param {DDO} ddo If undefined then the ddo will be fetched by did * @param {DDO} ddo If undefined then the ddo will be fetched by did
* @return {Promise<ComputeJob>} Returns the new status of a job * @return {Promise<ComputeJob>} Returns the new status of a job
*/ */
public async delete( public async delete(
consumerAccount: Account, consumerAccount: Account,
did: string, asset: DDO | string,
jobId: string, jobId: string
ddo?: DDO
): Promise<ComputeJob> { ): Promise<ComputeJob> {
if (!ddo) { const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
ddo = await this.ocean.assets.resolve(did)
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
}
const service = ddo.findServiceByType('compute') const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service const { serviceEndpoint } = service
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.computeDelete(did, consumerAccount, jobId) const computeJobsList = await provider.computeDelete(ddo.id, consumerAccount, jobId)
if (computeJobsList) return computeJobsList[0] as ComputeJob if (computeJobsList) return computeJobsList[0] as ComputeJob
return null return null
} }
@ -256,27 +246,22 @@ export class Compute extends Instantiable {
/** /**
* Returns the final result of a specific compute job published as an asset. * 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 {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 {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 DDO of the result asset. * @return {Promise<ComputeJob>} Returns the DDO of the result asset.
*/ */
public async result( public async result(
consumerAccount: Account, consumerAccount: Account,
did: string, asset: DDO | string,
jobId: string, jobId: string
ddo?: DDO
): Promise<ComputeJob> { ): Promise<ComputeJob> {
if (!ddo) { const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
ddo = await this.ocean.assets.resolve(did)
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
}
const service = ddo.findServiceByType('compute') const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service const { serviceEndpoint } = service
const provider = await Provider.getInstance(this.instanceConfig) const provider = await Provider.getInstance(this.instanceConfig)
await provider.setBaseUrl(serviceEndpoint) await provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.computeStatus( const computeJobsList = await provider.computeStatus(
did, ddo.id,
consumerAccount, consumerAccount,
jobId, jobId,
undefined, undefined,
@ -417,11 +402,9 @@ export class Compute extends Instantiable {
/** /**
* Checks if an asset is orderable with a specific algorithm * 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} serviceIndex The Service index
* @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset. * @param {ComputeAlgorithm} algorithm
* @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified.
* @param {DDO} datasetDdo Dataset DDO object. If undefined then the ddo will be fetched by did
* @param {DDO} algorithmDDO Algorithm DDO object. If undefined then the ddo will be fetched by did * @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 * @return {Promise<boolean>} True is you can order this
* *
@ -430,16 +413,12 @@ export class Compute extends Instantiable {
* but provider will not allow the compute, due to privacy settings of the ddo * but provider will not allow the compute, due to privacy settings of the ddo
*/ */
public async isOrderable( public async isOrderable(
datasetDid: string, dataset: DDO | string,
serviceIndex: number, serviceIndex: number,
algorithm: ComputeAlgorithm, algorithm: ComputeAlgorithm,
datasetDdo?: DDO,
algorithmDDO?: DDO algorithmDDO?: DDO
): Promise<boolean> { ): Promise<boolean> {
if (!datasetDdo) { const datasetDdo = isDdo(dataset) ? dataset : await this.ocean.assets.resolve(dataset)
datasetDdo = await this.ocean.assets.resolve(datasetDid)
if (!datasetDdo) throw new Error(`Couldn't resolve the did ${datasetDid}`)
}
const service: Service = datasetDdo.findServiceById(serviceIndex) const service: Service = datasetDdo.findServiceById(serviceIndex)
if (!service) return false if (!service) return false
if (service.type === 'compute') { if (service.type === 'compute') {
@ -493,7 +472,7 @@ export class Compute extends Instantiable {
) { ) {
this.logger.error( this.logger.error(
'ERROR: Algorithm container section was altered since it was added as trusted by ' + 'ERROR: Algorithm container section was altered since it was added as trusted by ' +
datasetDid datasetDdo.id
) )
return false return false
} }
@ -503,7 +482,7 @@ export class Compute extends Instantiable {
) { ) {
this.logger.error( this.logger.error(
'ERROR: Algorithm files section was altered since it was added as trusted by ' + 'ERROR: Algorithm files section was altered since it was added as trusted by ' +
datasetDid datasetDdo.id
) )
return false return false
} }
@ -513,7 +492,7 @@ export class Compute extends Instantiable {
} }
// algorithmDid was not found // algorithmDid was not found
this.logger.error( this.logger.error(
'ERROR: Algorithm ' + algorithm.did + ' is not allowed by ' + datasetDid 'ERROR: Algorithm ' + algorithm.did + ' is not allowed by ' + datasetDdo.id
) )
return false return false
} }
@ -526,7 +505,7 @@ export class Compute extends Instantiable {
/** /**
* Starts an order of a compute or access service for a compute job * 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} 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} serviceIndex The Service index
* @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset. * @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 * @param {string} algorithmServiceIndex The index of the service in the algorithm
@ -539,38 +518,30 @@ export class Compute extends Instantiable {
*/ */
public orderAsset( public orderAsset(
consumerAccount: string, consumerAccount: string,
datasetDid: string, dataset: DDO | string,
serviceIndex: number, serviceIndex: number,
algorithm: ComputeAlgorithm, algorithm: ComputeAlgorithm,
mpAddress?: string, mpAddress?: string,
computeAddress?: string, computeAddress?: string,
datasetDdo?: DDO,
searchPreviousOrders = true searchPreviousOrders = true
): SubscribablePromise<OrderProgressStep, string> { ): SubscribablePromise<OrderProgressStep, string> {
return new SubscribablePromise(async (observer) => { return new SubscribablePromise(async (observer) => {
const datasetDdo = isDdo(dataset)
? dataset
: await this.ocean.assets.resolve(dataset)
// first check if we can order this // first check if we can order this
const allowed = await this.isOrderable( const allowed = await this.isOrderable(datasetDdo, serviceIndex, algorithm)
datasetDid,
serviceIndex,
algorithm,
datasetDdo
)
if (!allowed) if (!allowed)
throw new Error( throw new Error(
`Dataset order failed, dataset is not orderable with the specified algorithm` `Dataset order failed, dataset is not orderable with the specified algorithm`
) )
if (!datasetDdo) {
datasetDdo = await this.ocean.assets.resolve(datasetDid)
if (!datasetDdo) throw new Error(`Couldn't resolve the did ${datasetDid}`)
}
// const service: Service = ddo.findServiceByType('compute') // const service: Service = ddo.findServiceByType('compute')
const service: Service = datasetDdo.findServiceById(serviceIndex) const service: Service = datasetDdo.findServiceById(serviceIndex)
if (!service) if (!service)
throw new Error(`Dataset order failed, Could not find service for the DDO`) throw new Error(`Dataset order failed, Could not find service for the DDO`)
try { try {
const order = await this.ocean.assets.order( const order = await this.ocean.assets.order(
datasetDid, datasetDdo,
service.type, service.type,
consumerAccount, consumerAccount,
-1, -1,
@ -588,7 +559,7 @@ export class Compute extends Instantiable {
/** /**
* Orders & pays for a algorithm * 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} serviceType
* @param {String} payerAddress * @param {String} payerAddress
* @param {Number} serviceIndex * @param {Number} serviceIndex
@ -597,7 +568,7 @@ export class Compute extends Instantiable {
* @return {Promise<String>} transactionHash of the payment * @return {Promise<String>} transactionHash of the payment
*/ */
public async orderAlgorithm( public async orderAlgorithm(
did: string, asset: DDO | string,
serviceType: string, serviceType: string,
payerAddress: string, payerAddress: string,
serviceIndex = -1, serviceIndex = -1,
@ -608,7 +579,7 @@ export class Compute extends Instantiable {
// this is only a convienince function, which calls ocean.assets.order // this is only a convienince function, which calls ocean.assets.order
try { try {
return await this.ocean.assets.order( return await this.ocean.assets.order(
did, asset,
serviceType, serviceType,
payerAddress, payerAddress,
serviceIndex, serviceIndex,

View File

@ -19,6 +19,9 @@ export interface ServiceEndpoint {
method: string method: string
urlPath: string urlPath: string
} }
function isDdo(arg: any): arg is DDO {
return arg.id !== undefined
}
/** /**
* Provides an interface for provider service. * Provides an interface for provider service.
* Provider service is the technical component executed * Provider service is the technical component executed
@ -186,21 +189,17 @@ export class Provider extends Instantiable {
} }
public async initialize( public async initialize(
did: string, asset: DDO | string,
serviceIndex: number, serviceIndex: number,
serviceType: string, serviceType: string,
consumerAddress: string, consumerAddress: string
ddo?: DDO
): Promise<string> { ): Promise<string> {
if (!ddo) { const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
ddo = await this.ocean.assets.resolve(did)
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
}
let initializeUrl = this.getInitializeEndpoint() let initializeUrl = this.getInitializeEndpoint()
? this.getInitializeEndpoint().urlPath ? this.getInitializeEndpoint().urlPath
: null : null
if (!initializeUrl) return null if (!initializeUrl) return null
initializeUrl += `?documentId=${did}` initializeUrl += `?documentId=${ddo.id}`
initializeUrl += `&serviceId=${serviceIndex}` initializeUrl += `&serviceId=${serviceIndex}`
initializeUrl += `&serviceType=${serviceType}` initializeUrl += `&serviceType=${serviceType}`
initializeUrl += `&dataToken=${ddo.dataToken}` initializeUrl += `&dataToken=${ddo.dataToken}`

View File

@ -844,24 +844,22 @@ describe('Compute flow', () => {
meta: algorithmMeta meta: algorithmMeta
} }
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition
ddo
) )
assert(allowed === true) assert(allowed === true)
computeOrderId = await ocean.compute.orderAsset( computeOrderId = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(computeOrderId != null, 'computeOrderId === null') assert(computeOrderId != null, 'computeOrderId === null')
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo,
computeOrderId, computeOrderId,
tokenAddress, tokenAddress,
bob, bob,
@ -869,8 +867,7 @@ describe('Compute flow', () => {
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type,
undefined, undefined
ddo
) )
assert(response, 'Compute error') assert(response, 'Compute error')
jobId = response.jobId jobId = response.jobId
@ -928,7 +925,7 @@ describe('Compute flow', () => {
}) })
it('Bob should stop compute job', async () => { it('Bob should stop compute job', async () => {
assert(jobId != null, 'Jobid is null') assert(jobId != null, 'Jobid is null')
await ocean.compute.stop(bob, ddo.id, jobId, ddo) await ocean.compute.stop(bob, ddo, jobId)
const response = await ocean.compute.status(bob, ddo.id, undefined, undefined, jobId) const response = await ocean.compute.status(bob, ddo.id, undefined, undefined, jobId)
// TODO: typings say that `stopreq` does not exist // TODO: typings say that `stopreq` does not exist
assert((response[0] as any).stopreq === 1, 'Response.stopreq is invalid') assert((response[0] as any).stopreq === 1, 'Response.stopreq is invalid')
@ -947,12 +944,11 @@ describe('Compute flow', () => {
try { try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetNoRawAlgo.id, datasetNoRawAlgo,
service1.index, service1.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
datasetNoRawAlgo
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) { } catch (error) {
@ -973,10 +969,9 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
datasetWithTrustedAlgo.id, datasetWithTrustedAlgo,
service1.index, service1.index,
algoDefinition, algoDefinition
datasetWithTrustedAlgo
) )
assert(allowed === false) assert(allowed === false)
@ -984,12 +979,11 @@ describe('Compute flow', () => {
try { try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetWithTrustedAlgo.id, datasetWithTrustedAlgo,
service1.index, service1.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
datasetWithTrustedAlgo
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) { } catch (error) {
@ -1006,10 +1000,9 @@ describe('Compute flow', () => {
did: algorithmAsset.id did: algorithmAsset.id
} }
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAsset algorithmAsset
) )
assert(allowed === false) assert(allowed === false)
@ -1017,12 +1010,11 @@ describe('Compute flow', () => {
try { try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) { } catch (error) {
@ -1109,22 +1101,20 @@ describe('Compute flow', () => {
serviceIndex: serviceAlgo.index serviceIndex: serviceAlgo.index
} }
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAssetRemoteProviderWithCompute algorithmAssetRemoteProviderWithCompute
) )
assert(allowed === false) assert(allowed === false)
try { try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) { } catch (error) {
@ -1145,26 +1135,24 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAssetwithCompute algorithmAssetwithCompute
) )
assert(allowed === true) assert(allowed === true)
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order != null, 'Order should not be null') assert(order != null, 'Order should not be null')
// order the algorithm // order the algorithm
const orderalgo = await ocean.compute.orderAlgorithm( const orderalgo = await ocean.compute.orderAlgorithm(
algorithmAssetwithCompute.id, algorithmAssetwithCompute,
serviceAlgo.type, serviceAlgo.type,
bob.getId(), bob.getId(),
serviceAlgo.index, serviceAlgo.index,
@ -1175,7 +1163,7 @@ describe('Compute flow', () => {
algoDefinition.transferTxId = orderalgo algoDefinition.transferTxId = orderalgo
algoDefinition.dataToken = algorithmAssetwithCompute.dataToken algoDefinition.dataToken = algorithmAssetwithCompute.dataToken
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo,
order, order,
tokenAddress, tokenAddress,
bob, bob,
@ -1183,8 +1171,7 @@ describe('Compute flow', () => {
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type,
undefined, undefined
ddo
) )
assert(response, 'Compute error') assert(response, 'Compute error')
jobId = response.jobId jobId = response.jobId
@ -1206,26 +1193,24 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAsset algorithmAsset
) )
assert(allowed === true) assert(allowed === true)
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order != null, 'Order should not be null') assert(order != null, 'Order should not be null')
// order the algorithm // order the algorithm
const orderalgo = await ocean.compute.orderAlgorithm( const orderalgo = await ocean.compute.orderAlgorithm(
algorithmAsset.id, algorithmAsset,
serviceAlgo.type, serviceAlgo.type,
bob.getId(), bob.getId(),
serviceAlgo.index, serviceAlgo.index,
@ -1236,7 +1221,7 @@ describe('Compute flow', () => {
algoDefinition.transferTxId = orderalgo algoDefinition.transferTxId = orderalgo
algoDefinition.dataToken = algorithmAsset.dataToken algoDefinition.dataToken = algorithmAsset.dataToken
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo,
order, order,
tokenAddress, tokenAddress,
bob, bob,
@ -1244,8 +1229,7 @@ describe('Compute flow', () => {
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type,
undefined, undefined
ddo
) )
assert(response, 'Compute error') assert(response, 'Compute error')
jobId = response.jobId jobId = response.jobId
@ -1270,25 +1254,23 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAssetRemoteProvider algorithmAssetRemoteProvider
) )
assert(allowed === true) assert(allowed === true)
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order != null, 'Order should not be null') assert(order != null, 'Order should not be null')
const orderalgo = await ocean.compute.orderAlgorithm( const orderalgo = await ocean.compute.orderAlgorithm(
algorithmAssetRemoteProvider.id, algorithmAssetRemoteProvider,
serviceAlgo.type, serviceAlgo.type,
bob.getId(), bob.getId(),
serviceAlgo.index, serviceAlgo.index,
@ -1299,16 +1281,14 @@ describe('Compute flow', () => {
algoDefinition.transferTxId = orderalgo algoDefinition.transferTxId = orderalgo
algoDefinition.dataToken = algorithmAssetRemoteProvider.dataToken algoDefinition.dataToken = algorithmAssetRemoteProvider.dataToken
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo,
order, order,
tokenAddress, tokenAddress,
bob, bob,
algoDefinition, algoDefinition,
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type
undefined,
ddo
) )
assert(response, 'Compute error') assert(response, 'Compute error')
assert(response.status >= 1, 'Invalid response status') assert(response.status >= 1, 'Invalid response status')
@ -1329,26 +1309,24 @@ describe('Compute flow', () => {
} }
let allowed let allowed
allowed = await ocean.compute.isOrderable( allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAsset algorithmAsset
) )
assert(allowed === true) assert(allowed === true)
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddo
) )
assert(order != null, 'Order should not be null') assert(order != null, 'Order should not be null')
// order the algo // order the algo
const orderalgo = await ocean.compute.orderAlgorithm( const orderalgo = await ocean.compute.orderAlgorithm(
algorithmAsset.id, algorithmAsset,
serviceAlgo.type, serviceAlgo.type,
bob.getId(), bob.getId(),
serviceAlgo.index, serviceAlgo.index,
@ -1360,7 +1338,7 @@ describe('Compute flow', () => {
assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null') assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null')
const inputOrder1Service = ddoAdditional1.findServiceByType('compute') const inputOrder1Service = ddoAdditional1.findServiceByType('compute')
allowed = await ocean.compute.isOrderable( allowed = await ocean.compute.isOrderable(
ddoAdditional1.id, ddoAdditional1,
inputOrder1Service.index, inputOrder1Service.index,
algoDefinition, algoDefinition,
ddoAdditional1 ddoAdditional1
@ -1368,19 +1346,18 @@ describe('Compute flow', () => {
assert(allowed === true) assert(allowed === true)
const inputOrder1 = await ocean.compute.orderAsset( const inputOrder1 = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddoAdditional1.id, ddoAdditional1,
inputOrder1Service.index, inputOrder1Service.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddoAdditional1
) )
assert(inputOrder1 != null, 'inputOrder1 should not be null') assert(inputOrder1 != null, 'inputOrder1 should not be null')
assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null') assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null')
// 2nd additional input // 2nd additional input
const inputOrder2Service = ddoAdditional2.findServiceByType('access') const inputOrder2Service = ddoAdditional2.findServiceByType('access')
allowed = await ocean.compute.isOrderable( allowed = await ocean.compute.isOrderable(
ddoAdditional2.id, ddoAdditional2,
inputOrder2Service.index, inputOrder2Service.index,
algoDefinition, algoDefinition,
ddoAdditional2 ddoAdditional2
@ -1388,12 +1365,11 @@ describe('Compute flow', () => {
assert(allowed === true) assert(allowed === true)
const inputOrder2 = await ocean.compute.orderAsset( const inputOrder2 = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddoAdditional2.id, ddoAdditional2,
inputOrder2Service.index, inputOrder2Service.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
ddoAdditional2
) )
assert(inputOrder2 != null, 'inputOrder2 should not be null') assert(inputOrder2 != null, 'inputOrder2 should not be null')
const additionalInputs: ComputeInput[] = [ const additionalInputs: ComputeInput[] = [
@ -1411,7 +1387,7 @@ describe('Compute flow', () => {
algoDefinition.transferTxId = orderalgo algoDefinition.transferTxId = orderalgo
algoDefinition.dataToken = algorithmAsset.dataToken algoDefinition.dataToken = algorithmAsset.dataToken
const response = await ocean.compute.start( const response = await ocean.compute.start(
ddo.id, ddo,
order, order,
tokenAddress, tokenAddress,
bob, bob,
@ -1419,8 +1395,7 @@ describe('Compute flow', () => {
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type,
additionalInputs, additionalInputs
ddo
) )
assert(response.status >= 1, 'Invalid response.status') assert(response.status >= 1, 'Invalid response.status')
assert(response.jobId, 'Invalid jobId') assert(response.jobId, 'Invalid jobId')
@ -1505,10 +1480,9 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAsset algorithmAsset
) )
assert(allowed === false, 'This should fail, the algo container section was changed!') assert(allowed === false, 'This should fail, the algo container section was changed!')
@ -1543,10 +1517,9 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
ddo.id, ddo,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
ddo,
algorithmAssetRemoteProvider algorithmAssetRemoteProvider
) )
assert(allowed === false, 'This should fail, the algo files section was changed!') assert(allowed === false, 'This should fail, the algo files section was changed!')
@ -1601,7 +1574,7 @@ describe('Compute flow', () => {
} }
// check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions // check if asset is orderable. otherwise, you might pay for it, but it has some algo restrictions
const allowed = await ocean.compute.isOrderable( const allowed = await ocean.compute.isOrderable(
datasetWithBogusProvider.id, datasetWithBogusProvider,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
datasetWithBogusProvider datasetWithBogusProvider
@ -1612,12 +1585,11 @@ describe('Compute flow', () => {
try { try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetWithBogusProvider.id, datasetWithBogusProvider,
computeService.index, computeService.index,
algoDefinition, algoDefinition,
null, // no marketplace fee null, // no marketplace fee
computeAddress, // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
datasetWithBogusProvider
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) { } catch (error) {
@ -1627,16 +1599,14 @@ describe('Compute flow', () => {
// we are forcing a bogus orderId // we are forcing a bogus orderId
computeOrderId = '1234' computeOrderId = '1234'
const response = await ocean.compute.start( const response = await ocean.compute.start(
datasetWithBogusProvider.id, datasetWithBogusProvider,
computeOrderId, computeOrderId,
tokenAddress, tokenAddress,
bob, bob,
algoDefinition, algoDefinition,
output, output,
`${computeService.index}`, `${computeService.index}`,
computeService.type, computeService.type
undefined,
ddo
) )
assert(response === null || response === undefined, 'Compute error') assert(response === null || response === undefined, 'Compute error')
}) })
@ -1655,12 +1625,7 @@ describe('Compute flow', () => {
}) })
it('Bob should fail to stop a fake compute job on a bogus provider', async () => { it('Bob should fail to stop a fake compute job on a bogus provider', async () => {
const jobid = '1234' const jobid = '1234'
const response = await ocean.compute.stop( const response = await ocean.compute.stop(bob, datasetWithBogusProvider, jobid)
bob,
datasetWithBogusProvider.id,
jobid,
ddo
)
assert(response === null || response === undefined, 'Invalid response') assert(response === null || response === undefined, 'Invalid response')
}) })
}) })