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:
parent
fd01736802
commit
a6c9c70e8e
@ -449,7 +449,7 @@ export class DataTokens {
|
|||||||
return trxReceipt
|
return trxReceipt
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to start order : ${e.message}`)
|
this.logger.error(`ERROR: Failed to start order : ${e.message}`)
|
||||||
return null
|
throw new Error(`Failed to start order: ${e.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -488,7 +488,10 @@ export class Assets extends Instantiable {
|
|||||||
serviceIndex,
|
serviceIndex,
|
||||||
service.serviceEndpoint
|
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) {
|
if (searchPreviousOrders) {
|
||||||
const previousOrder = await this.ocean.datatokens.getPreviousValidOrders(
|
const previousOrder = await this.ocean.datatokens.getPreviousValidOrders(
|
||||||
providerData.dataToken,
|
providerData.dataToken,
|
||||||
@ -510,7 +513,12 @@ export class Assets extends Instantiable {
|
|||||||
' but balance is ' +
|
' but balance is ' +
|
||||||
balance.toString()
|
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(
|
const txid = await this.ocean.datatokens.startOrder(
|
||||||
providerData.dataToken,
|
providerData.dataToken,
|
||||||
@ -522,9 +530,9 @@ export class Assets extends Instantiable {
|
|||||||
)
|
)
|
||||||
if (txid) return txid.transactionHash
|
if (txid) return txid.transactionHash
|
||||||
} catch (e) {
|
} 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
|
// marketplace flow
|
||||||
|
@ -516,21 +516,30 @@ export class Compute extends Instantiable {
|
|||||||
return new SubscribablePromise(async (observer) => {
|
return new SubscribablePromise(async (observer) => {
|
||||||
// first check if we can order this
|
// first check if we can order this
|
||||||
const allowed = await this.isOrderable(datasetDid, serviceIndex, algorithm)
|
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 ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
||||||
// const service: Service = ddo.findServiceByType('compute')
|
// const service: Service = ddo.findServiceByType('compute')
|
||||||
const service: Service = ddo.findServiceById(serviceIndex)
|
const service: Service = ddo.findServiceById(serviceIndex)
|
||||||
if (!service) return null
|
if (!service)
|
||||||
const order = await this.ocean.assets.order(
|
throw new Error(`Dataset order failed, Could not find service for the DDO`)
|
||||||
datasetDid,
|
try {
|
||||||
service.type,
|
const order = await this.ocean.assets.order(
|
||||||
consumerAccount,
|
datasetDid,
|
||||||
-1,
|
service.type,
|
||||||
mpAddress,
|
consumerAccount,
|
||||||
computeAddress,
|
-1,
|
||||||
searchPreviousOrders
|
mpAddress,
|
||||||
)
|
computeAddress,
|
||||||
return order
|
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
|
searchPreviousOrders = true
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// this is only a convienince function, which calls ocean.assets.order
|
// this is only a convienince function, which calls ocean.assets.order
|
||||||
return await this.ocean.assets.order(
|
try {
|
||||||
did,
|
return await this.ocean.assets.order(
|
||||||
serviceType,
|
did,
|
||||||
payerAddress,
|
serviceType,
|
||||||
serviceIndex,
|
payerAddress,
|
||||||
mpAddress,
|
serviceIndex,
|
||||||
consumerAddress,
|
mpAddress,
|
||||||
searchPreviousOrders
|
consumerAddress,
|
||||||
)
|
searchPreviousOrders
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`ERROR: Failed to orderAlgorithm: ${error.message}`)
|
||||||
|
throw new Error(`Failed to order algorithm: ${error.message}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -934,15 +934,19 @@ describe('Compute flow', () => {
|
|||||||
const algoDefinition: ComputeAlgorithm = {
|
const algoDefinition: ComputeAlgorithm = {
|
||||||
meta: algorithmMeta
|
meta: algorithmMeta
|
||||||
}
|
}
|
||||||
const order = await ocean.compute.orderAsset(
|
try {
|
||||||
bob.getId(),
|
const order = await ocean.compute.orderAsset(
|
||||||
datasetNoRawAlgo.id,
|
bob.getId(),
|
||||||
service1.index,
|
datasetNoRawAlgo.id,
|
||||||
algoDefinition,
|
service1.index,
|
||||||
null, // no marketplace fee
|
algoDefinition,
|
||||||
computeAddress // CtD is the consumer of the dataset
|
null, // no marketplace fee
|
||||||
)
|
computeAddress // CtD is the consumer of the dataset
|
||||||
assert(order === null, 'Order should be null')
|
)
|
||||||
|
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 () => {
|
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')
|
const service1 = datasetWithTrustedAlgo.findServiceByType('compute')
|
||||||
@ -965,15 +969,19 @@ describe('Compute flow', () => {
|
|||||||
assert(allowed === false)
|
assert(allowed === false)
|
||||||
|
|
||||||
// try even futher, since we now this should fail
|
// try even futher, since we now this should fail
|
||||||
const order = await ocean.compute.orderAsset(
|
try {
|
||||||
bob.getId(),
|
const order = await ocean.compute.orderAsset(
|
||||||
datasetWithTrustedAlgo.id,
|
bob.getId(),
|
||||||
service1.index,
|
datasetWithTrustedAlgo.id,
|
||||||
algoDefinition,
|
service1.index,
|
||||||
null, // no marketplace fee
|
algoDefinition,
|
||||||
computeAddress // CtD is the consumer of the dataset
|
null, // no marketplace fee
|
||||||
)
|
computeAddress // CtD is the consumer of the dataset
|
||||||
assert(order === null, 'Order should be null')
|
)
|
||||||
|
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 () => {
|
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
|
algoDefinition
|
||||||
)
|
)
|
||||||
assert(allowed === false)
|
assert(allowed === false)
|
||||||
const order = await ocean.compute.orderAsset(
|
|
||||||
bob.getId(),
|
try {
|
||||||
ddo.id,
|
const order = await ocean.compute.orderAsset(
|
||||||
computeService.index,
|
bob.getId(),
|
||||||
algoDefinition,
|
ddo.id,
|
||||||
null, // no marketplace fee
|
computeService.index,
|
||||||
computeAddress // CtD is the consumer of the dataset
|
algoDefinition,
|
||||||
)
|
null, // no marketplace fee
|
||||||
assert(order === null, 'Order should be null')
|
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 () => {
|
it('Alice updates Compute Privacy', async () => {
|
||||||
@ -1084,15 +1097,19 @@ describe('Compute flow', () => {
|
|||||||
algoDefinition
|
algoDefinition
|
||||||
)
|
)
|
||||||
assert(allowed === false)
|
assert(allowed === false)
|
||||||
const order = await ocean.compute.orderAsset(
|
try {
|
||||||
bob.getId(),
|
const order = await ocean.compute.orderAsset(
|
||||||
ddo.id,
|
bob.getId(),
|
||||||
computeService.index,
|
ddo.id,
|
||||||
algoDefinition,
|
computeService.index,
|
||||||
null, // no marketplace fee
|
algoDefinition,
|
||||||
computeAddress // CtD is the consumer of the dataset
|
null, // no marketplace fee
|
||||||
)
|
computeAddress // CtD is the consumer of the dataset
|
||||||
assert(order === null, 'Order should be null')
|
)
|
||||||
|
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 () => {
|
it('should start a compute job with a published algo that has a compute service', async () => {
|
||||||
const output = {}
|
const output = {}
|
||||||
@ -1543,15 +1560,21 @@ describe('Compute flow', () => {
|
|||||||
)
|
)
|
||||||
assert(allowed === false)
|
assert(allowed === false)
|
||||||
// we know that it is not Orderable, but we are trying to force it
|
// we know that it is not Orderable, but we are trying to force it
|
||||||
computeOrderId = await ocean.compute.orderAsset(
|
|
||||||
bob.getId(),
|
try {
|
||||||
datasetWithBogusProvider.id,
|
const order = await ocean.compute.orderAsset(
|
||||||
computeService.index,
|
bob.getId(),
|
||||||
algoDefinition,
|
datasetWithBogusProvider.id,
|
||||||
null, // no marketplace fee
|
computeService.index,
|
||||||
computeAddress // CtD is the consumer of the dataset
|
algoDefinition,
|
||||||
)
|
null, // no marketplace fee
|
||||||
assert(computeOrderId === null, 'computeOrderId !== null')
|
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
|
// we are forcing a bogus orderId
|
||||||
computeOrderId = '1234'
|
computeOrderId = '1234'
|
||||||
const response = await ocean.compute.start(
|
const response = await ocean.compute.start(
|
||||||
|
@ -540,12 +540,17 @@ describe('Marketplace flow', () => {
|
|||||||
})
|
})
|
||||||
it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => {
|
it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => {
|
||||||
const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId())
|
const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId())
|
||||||
const txid = await ocean.assets.order(
|
try {
|
||||||
ddoWithBadUrl.id,
|
const order = await ocean.assets.order(
|
||||||
accessService.type,
|
ddoWithBadUrl.id,
|
||||||
bob.getId()
|
accessService.type,
|
||||||
)
|
bob.getId()
|
||||||
assert(txid === null)
|
)
|
||||||
|
assert(order === null, 'Order should be null')
|
||||||
|
} catch (error) {
|
||||||
|
assert(error != null, 'Order should throw error')
|
||||||
|
}
|
||||||
|
|
||||||
const balanceAfterOrder = await datatoken.balance(
|
const balanceAfterOrder = await datatoken.balance(
|
||||||
tokenAddressForBadUrlAsset,
|
tokenAddressForBadUrlAsset,
|
||||||
bob.getId()
|
bob.getId()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user