diff --git a/package.json b/package.json index c87405653..f23eeaf64 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover", "test:unit": "npm run build-tests && npm run mocha \"./dist/test/unit/**/*.test.js\"", "test:integration": "npm run build-tests && npm run mocha \"./dist/test/integration/**/*.test.js\"", - "test:integration:compute": "npm run build-tests && npm run mocha ./dist/test/integration/compute.test.js", "test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit", "test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration", "logs": "./scripts/logs.sh" diff --git a/src/components/core/downloadHandler.ts b/src/components/core/downloadHandler.ts index a1c2d33c2..c8338fdc4 100644 --- a/src/components/core/downloadHandler.ts +++ b/src/components/core/downloadHandler.ts @@ -316,7 +316,6 @@ export class DownloadHandler extends Handler { } } } - // 6. Call the validateOrderTransaction function to check order transaction const paymentValidation = await validateOrderTransaction( task.transferTxId, @@ -327,7 +326,6 @@ export class DownloadHandler extends Handler { AssetUtils.getServiceIndexById(ddo, task.serviceId), service.timeout ) - if (paymentValidation.isValid) { CORE_LOGGER.logMessage( `Valid payment transaction. Result: ${paymentValidation.message}`, diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index fb1ed427d..05bd36413 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -3,6 +3,7 @@ import { fetchEventFromTransaction } from '../../../utils/util.js' import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' assert { type: 'json' } import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' assert { type: 'json' } import { CORE_LOGGER } from '../../../utils/logging/common.js' +import { EVENTS } from '../../../utils/index.js' interface ValidateTransactionResponse { isValid: boolean @@ -46,7 +47,6 @@ export async function validateOrderTransaction( ): Promise { const contractInterface = new Interface(ERC20Template.abi) let txReceiptMined = await fetchTransactionReceipt(txId, provider) - if (!txReceiptMined) { const errorMsg = `Tx receipt cannot be processed, because tx id ${txId} was not mined.` CORE_LOGGER.logMessage(errorMsg) @@ -55,10 +55,17 @@ export async function validateOrderTransaction( message: errorMsg } } + const erc20Address = txReceiptMined.to + const datatokenContract = new Contract( + erc20Address, + ERC20Template.abi, + await provider.getSigner() + ) + const erc721Address = await datatokenContract.getERC721Address() const orderReusedEvent = fetchEventFromTransaction( txReceiptMined, - 'OrderReused', + EVENTS.ORDER_REUSED, contractInterface ) @@ -74,22 +81,32 @@ export async function validateOrderTransaction( } } } - const OrderStartedEvent = fetchEventFromTransaction( txReceiptMined, - 'OrderStarted', + EVENTS.ORDER_STARTED, contractInterface ) - if ( - userAddress.toLowerCase() !== OrderStartedEvent[0].args[0].toLowerCase() && - userAddress.toLowerCase() !== OrderStartedEvent[0].args[1].toLowerCase() - ) { + let orderEvent + for (const event of OrderStartedEvent) { + if ( + (userAddress.toLowerCase() === event.args[0].toLowerCase() || + userAddress.toLowerCase() === event.args[1].toLowerCase()) && + erc20Address.toLowerCase() === datatokenAddress.toLowerCase() && + erc721Address.toLowerCase() === dataNftAddress.toLowerCase() + ) { + orderEvent = event + break + } + } + + if (!orderEvent) { return { isValid: false, - message: 'User address does not match with consumer or payer of the transaction.' + message: + 'Tx id used not valid, one of the NFT addresses, Datatoken address or the User address contract address does not match.' } } - const eventServiceIndex = OrderStartedEvent[0].args[3] + const eventServiceIndex = orderEvent.args[3] if (BigInt(serviceIndex) !== eventServiceIndex) { return { diff --git a/src/test/integration/download.test.ts b/src/test/integration/download.test.ts index 5f6015509..eb4af3656 100644 --- a/src/test/integration/download.test.ts +++ b/src/test/integration/download.test.ts @@ -47,7 +47,6 @@ describe('Should run a complete node flow.', () => { let publisherAccount: Signer let consumerAccount: Signer let consumerAddress: string - let resolvedDDO: Record let orderTxId: string let assetDID: string let publishedDataset: any @@ -83,10 +82,10 @@ describe('Should run a complete node flow.', () => { ) config = await getConfiguration(true) // Force reload the configuration - const dbconn = await new Database(config.dbConfig) - oceanNode = await OceanNode.getInstance(dbconn) + database = await new Database(config.dbConfig) + oceanNode = await OceanNode.getInstance(database) // eslint-disable-next-line no-unused-vars - const indexer = new OceanIndexer(dbconn, mockSupportedNetworks) + const indexer = new OceanIndexer(database, mockSupportedNetworks) let network = getOceanArtifactsAdressesByChainId(DEVELOPMENT_CHAIN_ID) if (!network) { @@ -200,20 +199,15 @@ describe('Should run a complete node flow.', () => { assert(orderTxId, 'transaction id not found') }) - it('should download triger download file', function () { + it('should download triger download file', async function () { this.timeout(DEFAULT_TEST_TIMEOUT * 3) const doCheck = async () => { - const config = await getConfiguration(true) - database = await new Database(config.dbConfig) - const oceanNode = OceanNode.getInstance(database) - assert(oceanNode, 'Failed to instantiate OceanNode') - const wallet = new ethers.Wallet( '0xef4b441145c1d0f3b4bc6d61d29f5c6e502359481152f869247c7a4244d45209' ) const nonce = Date.now().toString() - const message = String(resolvedDDO.id + nonce) + const message = String(publishedDataset.ddo.id + nonce) const consumerMessage = ethers.solidityPackedKeccak256( ['bytes'], [ethers.hexlify(ethers.toUtf8Bytes(message))] @@ -223,8 +217,8 @@ describe('Should run a complete node flow.', () => { const downloadTask = { fileIndex: 0, - documentId: assetDID, - serviceId, + documentId: publishedDataset.ddo.id, + serviceId: publishedDataset.ddo.services[0].id, transferTxId: orderTxId, nonce, consumerAddress, @@ -243,7 +237,7 @@ describe('Should run a complete node flow.', () => { expect(expectedTimeoutFailure(this.test.title)).to.be.equal(true) }, DEFAULT_TEST_TIMEOUT * 3) - doCheck() + await doCheck() }) it('should not allow to download the asset with different consumer address', function () { this.timeout(DEFAULT_TEST_TIMEOUT * 3) diff --git a/src/test/integration/transactionValidation.test.ts b/src/test/integration/transactionValidation.test.ts index 6c02e08c8..2450abd63 100644 --- a/src/test/integration/transactionValidation.test.ts +++ b/src/test/integration/transactionValidation.test.ts @@ -213,8 +213,7 @@ describe('validateOrderTransaction Function with Orders', () => { assert(!validationResult.isValid, 'Reuse order transaction should not be valid.') assert( validationResult.message === - 'User address does not match with consumer or payer of the transaction.', - 'Wrong transaction rejection message' + 'Tx id used not valid, one of the NFT addresses, Datatoken address or the User address contract address does not match.' ) }) after(async () => {