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:
parent
f0404f3204
commit
ec2e67db35
@ -40,6 +40,10 @@ export interface Order {
|
||||
serviceType?: string
|
||||
}
|
||||
|
||||
function isDdo(arg: any): arg is DDO {
|
||||
return arg.id !== undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Assets submodule of Ocean Protocol.
|
||||
*/
|
||||
@ -335,11 +339,11 @@ 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 ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const checksum = ddo.getChecksum()
|
||||
const { creator, signatureValue } = ddo.proof
|
||||
const signer = await this.ocean.utils.signature.verifyText(checksum, signatureValue)
|
||||
@ -382,9 +386,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 = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
let service: Service
|
||||
const services: Service[] = (await this.resolve(did)).service
|
||||
const services: Service[] = ddo.service
|
||||
|
||||
services.forEach((serv) => {
|
||||
if (serv.type.toString() === serviceType) {
|
||||
@ -394,9 +402,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 = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
let service: Service
|
||||
const services: Service[] = (await this.resolve(did)).service
|
||||
const services: Service[] = ddo.service
|
||||
|
||||
services.forEach((serv) => {
|
||||
if (serv.index === serviceIndex) {
|
||||
@ -440,7 +452,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
|
||||
@ -448,21 +460,19 @@ export class Assets extends Instantiable {
|
||||
* @return {Promise<any>} Order details
|
||||
*/
|
||||
public async initialize(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceType: string,
|
||||
consumerAddress: string,
|
||||
serviceIndex = -1,
|
||||
serviceEndpoint: string,
|
||||
ddo?: DDO
|
||||
serviceEndpoint: string
|
||||
): Promise<any> {
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
await provider.setBaseUrl(serviceEndpoint)
|
||||
const res = await provider.initialize(
|
||||
did,
|
||||
asset,
|
||||
serviceIndex,
|
||||
serviceType,
|
||||
consumerAddress,
|
||||
ddo
|
||||
consumerAddress
|
||||
)
|
||||
if (res === null) return null
|
||||
const providerData = JSON.parse(res)
|
||||
@ -471,7 +481,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
|
||||
@ -480,7 +490,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,
|
||||
@ -489,27 +499,25 @@ export class Assets extends Instantiable {
|
||||
searchPreviousOrders = true
|
||||
): Promise<string> {
|
||||
let service: Service
|
||||
|
||||
const ddo = await this.resolve(did)
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const consumable = await this.isConsumable(ddo)
|
||||
if (consumable.status > 0) return null
|
||||
|
||||
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,
|
||||
service.serviceEndpoint,
|
||||
ddo
|
||||
service.serviceEndpoint
|
||||
)
|
||||
if (!providerData)
|
||||
throw new Error(
|
||||
@ -560,13 +568,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 ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const { attributes } = ddo.findServiceByType('metadata')
|
||||
const service = ddo.findServiceByType('access')
|
||||
const { files } = attributes.main
|
||||
@ -586,7 +594,7 @@ export class Assets extends Instantiable {
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
await provider.setBaseUrl(serviceEndpoint)
|
||||
await provider.download(
|
||||
did,
|
||||
ddo.id,
|
||||
txId,
|
||||
tokenAddress,
|
||||
service.type,
|
||||
|
@ -59,6 +59,10 @@ export const ComputeJobStatus = Object.freeze({
|
||||
Deleted: 90
|
||||
})
|
||||
|
||||
function isDdo(arg: any): arg is DDO {
|
||||
return arg.id !== undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute submodule of Ocean Protocol.
|
||||
*/
|
||||
@ -98,18 +102,17 @@ 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.
|
||||
* @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 {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
|
||||
*/
|
||||
public async start(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
txId: string,
|
||||
tokenAddress: string,
|
||||
consumerAccount: Account,
|
||||
@ -117,21 +120,17 @@ export class Compute extends Instantiable {
|
||||
output?: ComputeOutput,
|
||||
serviceIndex?: string,
|
||||
serviceType?: string,
|
||||
additionalInputs?: ComputeInput[],
|
||||
ddo?: DDO
|
||||
additionalInputs?: ComputeInput[]
|
||||
): Promise<ComputeJob> {
|
||||
output = this.checkOutput(consumerAccount, output)
|
||||
if (!ddo) {
|
||||
ddo = await this.ocean.assets.resolve(did)
|
||||
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
|
||||
}
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
if (did && txId) {
|
||||
if (ddo.id && txId) {
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
await provider.setBaseUrl(serviceEndpoint)
|
||||
const computeJobsList = await provider.computeStart(
|
||||
did,
|
||||
ddo.id,
|
||||
consumerAccount,
|
||||
algorithm,
|
||||
output,
|
||||
@ -150,26 +149,21 @@ 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
|
||||
* @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 stop(
|
||||
consumerAccount: Account,
|
||||
did: string,
|
||||
jobId: string,
|
||||
ddo?: DDO
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
if (!ddo) {
|
||||
ddo = await this.ocean.assets.resolve(did)
|
||||
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
|
||||
}
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
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
|
||||
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.
|
||||
* @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,
|
||||
jobId: string,
|
||||
ddo?: DDO
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
if (!ddo) {
|
||||
ddo = await this.ocean.assets.resolve(did)
|
||||
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
|
||||
}
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
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
|
||||
return null
|
||||
}
|
||||
@ -256,27 +246,22 @@ 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.
|
||||
* @param {DDO} ddo If undefined then the ddo will be fetched by did
|
||||
* @return {Promise<ComputeJob>} Returns the DDO of the result asset.
|
||||
*/
|
||||
public async result(
|
||||
consumerAccount: Account,
|
||||
did: string,
|
||||
jobId: string,
|
||||
ddo?: DDO
|
||||
asset: DDO | string,
|
||||
jobId: string
|
||||
): Promise<ComputeJob> {
|
||||
if (!ddo) {
|
||||
ddo = await this.ocean.assets.resolve(did)
|
||||
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
|
||||
}
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
const service = ddo.findServiceByType('compute')
|
||||
const { serviceEndpoint } = service
|
||||
const provider = await Provider.getInstance(this.instanceConfig)
|
||||
await provider.setBaseUrl(serviceEndpoint)
|
||||
const computeJobsList = await provider.computeStatus(
|
||||
did,
|
||||
ddo.id,
|
||||
consumerAccount,
|
||||
jobId,
|
||||
undefined,
|
||||
@ -417,11 +402,9 @@ 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 {DDO} datasetDdo Dataset DDO object. If undefined then the ddo will be fetched by did
|
||||
* @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
|
||||
*
|
||||
@ -430,16 +413,12 @@ 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,
|
||||
datasetDdo?: DDO,
|
||||
algorithmDDO?: DDO
|
||||
): Promise<boolean> {
|
||||
if (!datasetDdo) {
|
||||
datasetDdo = await this.ocean.assets.resolve(datasetDid)
|
||||
if (!datasetDdo) throw new Error(`Couldn't resolve the did ${datasetDid}`)
|
||||
}
|
||||
const datasetDdo = isDdo(dataset) ? dataset : await this.ocean.assets.resolve(dataset)
|
||||
const service: Service = datasetDdo.findServiceById(serviceIndex)
|
||||
if (!service) return false
|
||||
if (service.type === 'compute') {
|
||||
@ -493,7 +472,7 @@ export class Compute extends Instantiable {
|
||||
) {
|
||||
this.logger.error(
|
||||
'ERROR: Algorithm container section was altered since it was added as trusted by ' +
|
||||
datasetDid
|
||||
datasetDdo.id
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -503,7 +482,7 @@ export class Compute extends Instantiable {
|
||||
) {
|
||||
this.logger.error(
|
||||
'ERROR: Algorithm files section was altered since it was added as trusted by ' +
|
||||
datasetDid
|
||||
datasetDdo.id
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -513,7 +492,7 @@ 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 ' + datasetDdo.id
|
||||
)
|
||||
return false
|
||||
}
|
||||
@ -526,7 +505,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
|
||||
@ -539,38 +518,30 @@ export class Compute extends Instantiable {
|
||||
*/
|
||||
public orderAsset(
|
||||
consumerAccount: string,
|
||||
datasetDid: string,
|
||||
dataset: DDO | string,
|
||||
serviceIndex: number,
|
||||
algorithm: ComputeAlgorithm,
|
||||
mpAddress?: string,
|
||||
computeAddress?: string,
|
||||
datasetDdo?: DDO,
|
||||
searchPreviousOrders = true
|
||||
): SubscribablePromise<OrderProgressStep, string> {
|
||||
return new SubscribablePromise(async (observer) => {
|
||||
const datasetDdo = isDdo(dataset)
|
||||
? dataset
|
||||
: await this.ocean.assets.resolve(dataset)
|
||||
// first check if we can order this
|
||||
const allowed = await this.isOrderable(
|
||||
datasetDid,
|
||||
serviceIndex,
|
||||
algorithm,
|
||||
datasetDdo
|
||||
)
|
||||
const allowed = await this.isOrderable(datasetDdo, serviceIndex, algorithm)
|
||||
if (!allowed)
|
||||
throw new Error(
|
||||
`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 = datasetDdo.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,
|
||||
datasetDdo,
|
||||
service.type,
|
||||
consumerAccount,
|
||||
-1,
|
||||
@ -588,7 +559,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
|
||||
@ -597,7 +568,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,
|
||||
@ -608,7 +579,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,
|
||||
|
@ -19,6 +19,9 @@ export interface ServiceEndpoint {
|
||||
method: string
|
||||
urlPath: string
|
||||
}
|
||||
function isDdo(arg: any): arg is DDO {
|
||||
return arg.id !== undefined
|
||||
}
|
||||
/**
|
||||
* Provides an interface for provider service.
|
||||
* Provider service is the technical component executed
|
||||
@ -186,21 +189,17 @@ export class Provider extends Instantiable {
|
||||
}
|
||||
|
||||
public async initialize(
|
||||
did: string,
|
||||
asset: DDO | string,
|
||||
serviceIndex: number,
|
||||
serviceType: string,
|
||||
consumerAddress: string,
|
||||
ddo?: DDO
|
||||
consumerAddress: string
|
||||
): Promise<string> {
|
||||
if (!ddo) {
|
||||
ddo = await this.ocean.assets.resolve(did)
|
||||
if (!ddo) throw new Error(`Couldn't resolve the did ${did}`)
|
||||
}
|
||||
const ddo = isDdo(asset) ? asset : await this.ocean.assets.resolve(asset)
|
||||
let initializeUrl = this.getInitializeEndpoint()
|
||||
? this.getInitializeEndpoint().urlPath
|
||||
: null
|
||||
if (!initializeUrl) return null
|
||||
initializeUrl += `?documentId=${did}`
|
||||
initializeUrl += `?documentId=${ddo.id}`
|
||||
initializeUrl += `&serviceId=${serviceIndex}`
|
||||
initializeUrl += `&serviceType=${serviceType}`
|
||||
initializeUrl += `&dataToken=${ddo.dataToken}`
|
||||
|
@ -844,24 +844,22 @@ describe('Compute flow', () => {
|
||||
meta: algorithmMeta
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo
|
||||
algoDefinition
|
||||
)
|
||||
assert(allowed === true)
|
||||
computeOrderId = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(computeOrderId != null, 'computeOrderId === null')
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeOrderId,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -869,8 +867,7 @@ describe('Compute flow', () => {
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
undefined,
|
||||
ddo
|
||||
undefined
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
jobId = response.jobId
|
||||
@ -928,7 +925,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, ddo)
|
||||
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')
|
||||
@ -947,12 +944,11 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetNoRawAlgo.id,
|
||||
datasetNoRawAlgo,
|
||||
service1.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
datasetNoRawAlgo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order === null, 'Order should be null')
|
||||
} 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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
datasetWithTrustedAlgo.id,
|
||||
datasetWithTrustedAlgo,
|
||||
service1.index,
|
||||
algoDefinition,
|
||||
datasetWithTrustedAlgo
|
||||
algoDefinition
|
||||
)
|
||||
assert(allowed === false)
|
||||
|
||||
@ -984,12 +979,11 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetWithTrustedAlgo.id,
|
||||
datasetWithTrustedAlgo,
|
||||
service1.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
datasetWithTrustedAlgo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order === null, 'Order should be null')
|
||||
} catch (error) {
|
||||
@ -1006,10 +1000,9 @@ describe('Compute flow', () => {
|
||||
did: algorithmAsset.id
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === false)
|
||||
@ -1017,12 +1010,11 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order === null, 'Order should be null')
|
||||
} catch (error) {
|
||||
@ -1109,22 +1101,20 @@ describe('Compute flow', () => {
|
||||
serviceIndex: serviceAlgo.index
|
||||
}
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAssetRemoteProviderWithCompute
|
||||
)
|
||||
assert(allowed === false)
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order === null, 'Order should be null')
|
||||
} 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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAssetwithCompute
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
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,
|
||||
@ -1175,7 +1163,7 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAssetwithCompute.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1183,8 +1171,7 @@ describe('Compute flow', () => {
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
undefined,
|
||||
ddo
|
||||
undefined
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
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,
|
||||
@ -1236,7 +1221,7 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAsset.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1244,8 +1229,7 @@ describe('Compute flow', () => {
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
undefined,
|
||||
ddo
|
||||
undefined
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAssetRemoteProvider
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order != null, 'Order should not be null')
|
||||
const orderalgo = await ocean.compute.orderAlgorithm(
|
||||
algorithmAssetRemoteProvider.id,
|
||||
algorithmAssetRemoteProvider,
|
||||
serviceAlgo.type,
|
||||
bob.getId(),
|
||||
serviceAlgo.index,
|
||||
@ -1299,16 +1281,14 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAssetRemoteProvider.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
algoDefinition,
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
undefined,
|
||||
ddo
|
||||
computeService.type
|
||||
)
|
||||
assert(response, 'Compute error')
|
||||
assert(response.status >= 1, 'Invalid response status')
|
||||
@ -1329,26 +1309,24 @@ describe('Compute flow', () => {
|
||||
}
|
||||
let allowed
|
||||
allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAsset
|
||||
)
|
||||
assert(allowed === true)
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddo
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
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,
|
||||
@ -1360,7 +1338,7 @@ 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,
|
||||
ddoAdditional1
|
||||
@ -1368,19 +1346,18 @@ describe('Compute flow', () => {
|
||||
assert(allowed === true)
|
||||
const inputOrder1 = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddoAdditional1.id,
|
||||
ddoAdditional1,
|
||||
inputOrder1Service.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddoAdditional1
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(inputOrder1 != null, 'inputOrder1 should not be null')
|
||||
assert(ddoAdditional1 != null, 'ddoAdditional1 should not be null')
|
||||
// 2nd additional input
|
||||
const inputOrder2Service = ddoAdditional2.findServiceByType('access')
|
||||
allowed = await ocean.compute.isOrderable(
|
||||
ddoAdditional2.id,
|
||||
ddoAdditional2,
|
||||
inputOrder2Service.index,
|
||||
algoDefinition,
|
||||
ddoAdditional2
|
||||
@ -1388,12 +1365,11 @@ describe('Compute flow', () => {
|
||||
assert(allowed === true)
|
||||
const inputOrder2 = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
ddoAdditional2.id,
|
||||
ddoAdditional2,
|
||||
inputOrder2Service.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
ddoAdditional2
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(inputOrder2 != null, 'inputOrder2 should not be null')
|
||||
const additionalInputs: ComputeInput[] = [
|
||||
@ -1411,7 +1387,7 @@ describe('Compute flow', () => {
|
||||
algoDefinition.transferTxId = orderalgo
|
||||
algoDefinition.dataToken = algorithmAsset.dataToken
|
||||
const response = await ocean.compute.start(
|
||||
ddo.id,
|
||||
ddo,
|
||||
order,
|
||||
tokenAddress,
|
||||
bob,
|
||||
@ -1419,8 +1395,7 @@ describe('Compute flow', () => {
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
additionalInputs,
|
||||
ddo
|
||||
additionalInputs
|
||||
)
|
||||
assert(response.status >= 1, 'Invalid response.status')
|
||||
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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAsset
|
||||
)
|
||||
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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
ddo.id,
|
||||
ddo,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
ddo,
|
||||
algorithmAssetRemoteProvider
|
||||
)
|
||||
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
|
||||
const allowed = await ocean.compute.isOrderable(
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
datasetWithBogusProvider
|
||||
@ -1612,12 +1585,11 @@ describe('Compute flow', () => {
|
||||
try {
|
||||
const order = await ocean.compute.orderAsset(
|
||||
bob.getId(),
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeService.index,
|
||||
algoDefinition,
|
||||
null, // no marketplace fee
|
||||
computeAddress, // CtD is the consumer of the dataset
|
||||
datasetWithBogusProvider
|
||||
computeAddress // CtD is the consumer of the dataset
|
||||
)
|
||||
assert(order === null, 'Order should be null')
|
||||
} catch (error) {
|
||||
@ -1627,16 +1599,14 @@ describe('Compute flow', () => {
|
||||
// we are forcing a bogus orderId
|
||||
computeOrderId = '1234'
|
||||
const response = await ocean.compute.start(
|
||||
datasetWithBogusProvider.id,
|
||||
datasetWithBogusProvider,
|
||||
computeOrderId,
|
||||
tokenAddress,
|
||||
bob,
|
||||
algoDefinition,
|
||||
output,
|
||||
`${computeService.index}`,
|
||||
computeService.type,
|
||||
undefined,
|
||||
ddo
|
||||
computeService.type
|
||||
)
|
||||
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 () => {
|
||||
const jobid = '1234'
|
||||
const response = await ocean.compute.stop(
|
||||
bob,
|
||||
datasetWithBogusProvider.id,
|
||||
jobid,
|
||||
ddo
|
||||
)
|
||||
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