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

Fix/ Order method should throw error messages (#803)

* throw error when order methods fail, updated integration tests accordingly

* throw error for startOrder method & update error messages

* updated assert messages and added asserts in try block
This commit is contained in:
Bogdan Fazakas 2021-05-25 17:00:13 +03:00 committed by GitHub
parent fd01736802
commit a6c9c70e8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 77 deletions

View File

@ -449,7 +449,7 @@ export class DataTokens {
return trxReceipt
} catch (e) {
this.logger.error(`ERROR: Failed to start order : ${e.message}`)
return null
throw new Error(`Failed to start order: ${e.message}`)
}
}

View File

@ -488,7 +488,10 @@ export class Assets extends Instantiable {
serviceIndex,
service.serviceEndpoint
)
if (!providerData) return null
if (!providerData)
throw new Error(
`Order asset failed, Failed to initialize service to compute totalCost for ordering`
)
if (searchPreviousOrders) {
const previousOrder = await this.ocean.datatokens.getPreviousValidOrders(
providerData.dataToken,
@ -510,7 +513,12 @@ export class Assets extends Instantiable {
' but balance is ' +
balance.toString()
)
return null
throw new Error(
'ERROR: Not enough funds Needed ' +
totalCost.toString() +
' but balance is ' +
balance.toString()
)
}
const txid = await this.ocean.datatokens.startOrder(
providerData.dataToken,
@ -522,9 +530,9 @@ export class Assets extends Instantiable {
)
if (txid) return txid.transactionHash
} catch (e) {
this.logger.error(`ERROR: Failed to order: ${e.message}`)
this.logger.error(`ERROR: Failed to order a service : ${e.message}`)
throw new Error(`Failed to order a service: ${e.message}`)
}
return null
}
// marketplace flow

View File

@ -516,21 +516,30 @@ export class Compute extends Instantiable {
return new SubscribablePromise(async (observer) => {
// first check if we can order this
const allowed = await this.isOrderable(datasetDid, serviceIndex, algorithm)
if (!allowed) return null
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) return null
const order = await this.ocean.assets.order(
datasetDid,
service.type,
consumerAccount,
-1,
mpAddress,
computeAddress,
searchPreviousOrders
)
return order
if (!service)
throw new Error(`Dataset order failed, Could not find service for the DDO`)
try {
const order = await this.ocean.assets.order(
datasetDid,
service.type,
consumerAccount,
-1,
mpAddress,
computeAddress,
searchPreviousOrders
)
return order
} catch (error) {
this.logger.error(`ERROR: Failed to order: ${error.message}`)
throw new Error(`Failed to order dataset: ${error.message}`)
}
})
}
@ -554,15 +563,20 @@ export class Compute extends Instantiable {
searchPreviousOrders = true
): Promise<string> {
// this is only a convienince function, which calls ocean.assets.order
return await this.ocean.assets.order(
did,
serviceType,
payerAddress,
serviceIndex,
mpAddress,
consumerAddress,
searchPreviousOrders
)
try {
return await this.ocean.assets.order(
did,
serviceType,
payerAddress,
serviceIndex,
mpAddress,
consumerAddress,
searchPreviousOrders
)
} catch (error) {
this.logger.error(`ERROR: Failed to orderAlgorithm: ${error.message}`)
throw new Error(`Failed to order algorithm: ${error.message}`)
}
}
/**

View File

@ -934,15 +934,19 @@ describe('Compute flow', () => {
const algoDefinition: ComputeAlgorithm = {
meta: algorithmMeta
}
const order = await ocean.compute.orderAsset(
bob.getId(),
datasetNoRawAlgo.id,
service1.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
try {
const order = await ocean.compute.orderAsset(
bob.getId(),
datasetNoRawAlgo.id,
service1.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
})
it('should not allow order the compute service with algoDid != "did:op:1234" for dataset that allows only "did:op:1234" as algo', async () => {
const service1 = datasetWithTrustedAlgo.findServiceByType('compute')
@ -965,15 +969,19 @@ describe('Compute flow', () => {
assert(allowed === false)
// try even futher, since we now this should fail
const order = await ocean.compute.orderAsset(
bob.getId(),
datasetWithTrustedAlgo.id,
service1.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
try {
const order = await ocean.compute.orderAsset(
bob.getId(),
datasetWithTrustedAlgo.id,
service1.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
})
it('should not allow a compute job with a published algo because asset does not allow allowAllPublishedAlgorithms', async () => {
@ -990,15 +998,20 @@ describe('Compute flow', () => {
algoDefinition
)
assert(allowed === false)
const order = await ocean.compute.orderAsset(
bob.getId(),
ddo.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
try {
const order = await ocean.compute.orderAsset(
bob.getId(),
ddo.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
})
it('Alice updates Compute Privacy', async () => {
@ -1084,15 +1097,19 @@ describe('Compute flow', () => {
algoDefinition
)
assert(allowed === false)
const order = await ocean.compute.orderAsset(
bob.getId(),
ddo.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
try {
const order = await ocean.compute.orderAsset(
bob.getId(),
ddo.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
})
it('should start a compute job with a published algo that has a compute service', async () => {
const output = {}
@ -1543,15 +1560,21 @@ describe('Compute flow', () => {
)
assert(allowed === false)
// we know that it is not Orderable, but we are trying to force it
computeOrderId = await ocean.compute.orderAsset(
bob.getId(),
datasetWithBogusProvider.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(computeOrderId === null, 'computeOrderId !== null')
try {
const order = await ocean.compute.orderAsset(
bob.getId(),
datasetWithBogusProvider.id,
computeService.index,
algoDefinition,
null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
// we are forcing a bogus orderId
computeOrderId = '1234'
const response = await ocean.compute.start(

View File

@ -540,12 +540,17 @@ describe('Marketplace flow', () => {
})
it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => {
const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId())
const txid = await ocean.assets.order(
ddoWithBadUrl.id,
accessService.type,
bob.getId()
)
assert(txid === null)
try {
const order = await ocean.assets.order(
ddoWithBadUrl.id,
accessService.type,
bob.getId()
)
assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
const balanceAfterOrder = await datatoken.balance(
tokenAddressForBadUrlAsset,
bob.getId()