From c0bda32082959955d7472bf06df13bf84f7a80cc Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Wed, 20 Mar 2024 12:19:32 +0200 Subject: [PATCH 1/9] debug --- package.json | 2 +- src/components/core/utils/validateOrders.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index c62c916b0..224af99d7 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "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:complete": "npm run build-tests && npm run mocha ./dist/test/integration/completeFlow.test.js", + "test:integration:download": "npm run build-tests && npm run mocha ./dist/test/integration/download.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/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index fb1ed427d..14871d98f 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 @@ -74,12 +75,13 @@ export async function validateOrderTransaction( } } } - + console.log('txReceiptMined == ', txReceiptMined) const OrderStartedEvent = fetchEventFromTransaction( txReceiptMined, - 'OrderStarted', + EVENTS.ORDER_STARTED, contractInterface ) + console.log('txReceiptMined == ', OrderStartedEvent) if ( userAddress.toLowerCase() !== OrderStartedEvent[0].args[0].toLowerCase() && userAddress.toLowerCase() !== OrderStartedEvent[0].args[1].toLowerCase() From 49f78c1e8ff94d0e62d5b3561a67ffd8bcc1a901 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Thu, 21 Mar 2024 12:42:49 +0200 Subject: [PATCH 2/9] test fixes --- src/components/core/downloadHandler.ts | 2 -- src/components/core/utils/validateOrders.ts | 27 +++++++++++++------- src/test/integration/download.test.ts | 28 ++++++++++----------- src/utils/address.ts | 4 +++ 4 files changed, 35 insertions(+), 26 deletions(-) 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 14871d98f..2b0b111ff 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -45,9 +45,11 @@ export async function validateOrderTransaction( serviceIndex: number, serviceTimeout: number ): Promise { + console.log(dataNftAddress, datatokenAddress) const contractInterface = new Interface(ERC20Template.abi) let txReceiptMined = await fetchTransactionReceipt(txId, provider) - + console.log('txReceiptMined ', txReceiptMined) + console.log('txReceiptMined contractAddress: ', txReceiptMined.contractAddress) if (!txReceiptMined) { const errorMsg = `Tx receipt cannot be processed, because tx id ${txId} was not mined.` CORE_LOGGER.logMessage(errorMsg) @@ -59,7 +61,7 @@ export async function validateOrderTransaction( const orderReusedEvent = fetchEventFromTransaction( txReceiptMined, - 'OrderReused', + EVENTS.ORDER_REUSED, contractInterface ) @@ -75,23 +77,30 @@ export async function validateOrderTransaction( } } } - console.log('txReceiptMined == ', txReceiptMined) const OrderStartedEvent = fetchEventFromTransaction( txReceiptMined, EVENTS.ORDER_STARTED, contractInterface ) - console.log('txReceiptMined == ', OrderStartedEvent) - 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() + ) { + continue + } + orderEvent = event + break + } + + if (!orderEvent) { return { isValid: false, message: 'User address does not match with consumer or payer of the transaction.' } } - 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..3c2de45a1 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,14 +82,18 @@ 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) + console.log('network ', network) + console.log('DEVELOPMENT_CHAIN_ID ', DEVELOPMENT_CHAIN_ID) + if (!network) { network = getOceanArtifactsAdresses().development + console.log('network 2', network) } provider = new JsonRpcProvider('http://127.0.0.1:8545') @@ -195,25 +198,20 @@ describe('Should run a complete node flow.', () => { publisherAccount, oceanNode ) - assert(orderTxReceipt, 'order transaction failed') + console.log('orderTxReceipt', orderTxReceipt) orderTxId = orderTxReceipt.hash 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 +221,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 +241,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/utils/address.ts b/src/utils/address.ts index 7441f8505..3f61356ea 100644 --- a/src/utils/address.ts +++ b/src/utils/address.ts @@ -10,6 +10,10 @@ import { ENVIRONMENT_VARIABLES, existsEnvironmentVariable } from './index.js' */ export function getOceanArtifactsAdresses(): any { try { + console.log( + 'Getting ocean artifacts addresses', + ENVIRONMENT_VARIABLES.ADDRESS_FILE.value + ) if (existsEnvironmentVariable(ENVIRONMENT_VARIABLES.ADDRESS_FILE)) { // eslint-disable-next-line security/detect-non-literal-fs-filename const data = fs.readFileSync(ENVIRONMENT_VARIABLES.ADDRESS_FILE.value, 'utf8') From b464338edb0cb5c2aebfe1942915cf8f7aa79275 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Thu, 21 Mar 2024 12:47:52 +0200 Subject: [PATCH 3/9] check datatoken adress as well --- src/components/core/utils/validateOrders.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index 2b0b111ff..2cb4e6ea0 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -48,8 +48,6 @@ export async function validateOrderTransaction( console.log(dataNftAddress, datatokenAddress) const contractInterface = new Interface(ERC20Template.abi) let txReceiptMined = await fetchTransactionReceipt(txId, provider) - console.log('txReceiptMined ', txReceiptMined) - console.log('txReceiptMined contractAddress: ', txReceiptMined.contractAddress) if (!txReceiptMined) { const errorMsg = `Tx receipt cannot be processed, because tx id ${txId} was not mined.` CORE_LOGGER.logMessage(errorMsg) @@ -58,6 +56,7 @@ export async function validateOrderTransaction( message: errorMsg } } + const contractAddress = txReceiptMined.to const orderReusedEvent = fetchEventFromTransaction( txReceiptMined, @@ -85,8 +84,9 @@ export async function validateOrderTransaction( let orderEvent for (const event of OrderStartedEvent) { if ( - userAddress.toLowerCase() !== event.args[0].toLowerCase() && - userAddress.toLowerCase() !== event.args[1].toLowerCase() + (userAddress.toLowerCase() !== event.args[0].toLowerCase() && + userAddress.toLowerCase() !== event.args[1].toLowerCase()) || + contractAddress.toLowerCase() !== datatokenAddress.toLowerCase() ) { continue } @@ -97,7 +97,8 @@ export async function validateOrderTransaction( if (!orderEvent) { return { isValid: false, - message: 'User address does not match with consumer or payer of the transaction.' + message: + 'Tx id used not valid, Datatoken adreess does not match or User address does not match with consumer or payer of the transaction.' } } const eventServiceIndex = orderEvent.args[3] From ecd276e8f0ed0f77a1335e82954d3c98ca394fdf Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Thu, 21 Mar 2024 12:51:30 +0200 Subject: [PATCH 4/9] cleanup --- src/components/core/utils/validateOrders.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index 2cb4e6ea0..99ef0daa7 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -45,7 +45,6 @@ export async function validateOrderTransaction( serviceIndex: number, serviceTimeout: number ): Promise { - console.log(dataNftAddress, datatokenAddress) const contractInterface = new Interface(ERC20Template.abi) let txReceiptMined = await fetchTransactionReceipt(txId, provider) if (!txReceiptMined) { From 8865ba31201f7fb2ae5398aef151acbb273d0936 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 22 Mar 2024 15:35:34 +0200 Subject: [PATCH 5/9] update validation test --- src/test/integration/transactionValidation.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/integration/transactionValidation.test.ts b/src/test/integration/transactionValidation.test.ts index 6c02e08c8..d3c7a7981 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, Datatoken adreess does not match or User address does not match with consumer or payer of the transaction.' ) }) after(async () => { From 107e932e6eb5cf287f6e1cf7cb92011c878db551 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Mon, 25 Mar 2024 15:14:36 +0200 Subject: [PATCH 6/9] add nft address check as well --- src/components/core/utils/validateOrders.ts | 19 +++++++++++++++---- .../integration/transactionValidation.test.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index 99ef0daa7..c19aea4d9 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -55,8 +55,18 @@ export async function validateOrderTransaction( message: errorMsg } } - const contractAddress = txReceiptMined.to - + console.log('datanftAddress ', dataNftAddress) + console.log('datatokenAddress ', datatokenAddress) + + const erc20Address = txReceiptMined.to + const datatokenContract = new Contract( + erc20Address, + ERC20Template.abi, + await provider.getSigner() + ) + const erc721Address = await datatokenContract.getERC721Address() + console.log('erc721Address ', erc721Address) + console.log('erc20Address ', erc20Address) const orderReusedEvent = fetchEventFromTransaction( txReceiptMined, EVENTS.ORDER_REUSED, @@ -85,7 +95,8 @@ export async function validateOrderTransaction( if ( (userAddress.toLowerCase() !== event.args[0].toLowerCase() && userAddress.toLowerCase() !== event.args[1].toLowerCase()) || - contractAddress.toLowerCase() !== datatokenAddress.toLowerCase() + erc20Address.toLowerCase() !== datatokenAddress.toLowerCase() || + erc721Address.toLowerCase() !== dataNftAddress.toLowerCase() ) { continue } @@ -97,7 +108,7 @@ export async function validateOrderTransaction( return { isValid: false, message: - 'Tx id used not valid, Datatoken adreess does not match or User address does not match with consumer or payer of the transaction.' + 'Tx id used not valid, one of the NFT addresses, Datatoken address or the User address contract address does not match.' } } const eventServiceIndex = orderEvent.args[3] diff --git a/src/test/integration/transactionValidation.test.ts b/src/test/integration/transactionValidation.test.ts index d3c7a7981..2450abd63 100644 --- a/src/test/integration/transactionValidation.test.ts +++ b/src/test/integration/transactionValidation.test.ts @@ -213,7 +213,7 @@ describe('validateOrderTransaction Function with Orders', () => { assert(!validationResult.isValid, 'Reuse order transaction should not be valid.') assert( validationResult.message === - 'Tx id used not valid, Datatoken adreess does not match or User address does not match with consumer or payer of the transaction.' + 'Tx id used not valid, one of the NFT addresses, Datatoken address or the User address contract address does not match.' ) }) after(async () => { From 4f4463f4de52b984ffaa495c8be467301227036b Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Mon, 25 Mar 2024 15:40:41 +0200 Subject: [PATCH 7/9] remove debug logs --- src/components/core/utils/validateOrders.ts | 6 +----- src/test/integration/download.test.ts | 4 ---- src/utils/address.ts | 4 ---- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index c19aea4d9..49821b70b 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -55,9 +55,6 @@ export async function validateOrderTransaction( message: errorMsg } } - console.log('datanftAddress ', dataNftAddress) - console.log('datatokenAddress ', datatokenAddress) - const erc20Address = txReceiptMined.to const datatokenContract = new Contract( erc20Address, @@ -65,8 +62,7 @@ export async function validateOrderTransaction( await provider.getSigner() ) const erc721Address = await datatokenContract.getERC721Address() - console.log('erc721Address ', erc721Address) - console.log('erc20Address ', erc20Address) + const orderReusedEvent = fetchEventFromTransaction( txReceiptMined, EVENTS.ORDER_REUSED, diff --git a/src/test/integration/download.test.ts b/src/test/integration/download.test.ts index 3c2de45a1..ddbf96a15 100644 --- a/src/test/integration/download.test.ts +++ b/src/test/integration/download.test.ts @@ -88,12 +88,8 @@ describe('Should run a complete node flow.', () => { const indexer = new OceanIndexer(database, mockSupportedNetworks) let network = getOceanArtifactsAdressesByChainId(DEVELOPMENT_CHAIN_ID) - console.log('network ', network) - console.log('DEVELOPMENT_CHAIN_ID ', DEVELOPMENT_CHAIN_ID) - if (!network) { network = getOceanArtifactsAdresses().development - console.log('network 2', network) } provider = new JsonRpcProvider('http://127.0.0.1:8545') diff --git a/src/utils/address.ts b/src/utils/address.ts index 3f61356ea..7441f8505 100644 --- a/src/utils/address.ts +++ b/src/utils/address.ts @@ -10,10 +10,6 @@ import { ENVIRONMENT_VARIABLES, existsEnvironmentVariable } from './index.js' */ export function getOceanArtifactsAdresses(): any { try { - console.log( - 'Getting ocean artifacts addresses', - ENVIRONMENT_VARIABLES.ADDRESS_FILE.value - ) if (existsEnvironmentVariable(ENVIRONMENT_VARIABLES.ADDRESS_FILE)) { // eslint-disable-next-line security/detect-non-literal-fs-filename const data = fs.readFileSync(ENVIRONMENT_VARIABLES.ADDRESS_FILE.value, 'utf8') From e8cf9801d0e821f85634a291493268dc4e960d21 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Tue, 2 Apr 2024 10:28:45 +0300 Subject: [PATCH 8/9] update loop --- src/components/core/utils/validateOrders.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/components/core/utils/validateOrders.ts b/src/components/core/utils/validateOrders.ts index 49821b70b..05bd36413 100644 --- a/src/components/core/utils/validateOrders.ts +++ b/src/components/core/utils/validateOrders.ts @@ -89,15 +89,14 @@ export async function validateOrderTransaction( 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() + (userAddress.toLowerCase() === event.args[0].toLowerCase() || + userAddress.toLowerCase() === event.args[1].toLowerCase()) && + erc20Address.toLowerCase() === datatokenAddress.toLowerCase() && + erc721Address.toLowerCase() === dataNftAddress.toLowerCase() ) { - continue + orderEvent = event + break } - orderEvent = event - break } if (!orderEvent) { From 731fee710a2468de944e7b17c6a233d07f9fe691 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Wed, 3 Apr 2024 18:43:04 +0300 Subject: [PATCH 9/9] small tweaks --- package.json | 1 - src/test/integration/download.test.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 05193f979..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:download": "npm run build-tests && npm run mocha ./dist/test/integration/download.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/test/integration/download.test.ts b/src/test/integration/download.test.ts index ddbf96a15..eb4af3656 100644 --- a/src/test/integration/download.test.ts +++ b/src/test/integration/download.test.ts @@ -194,7 +194,7 @@ describe('Should run a complete node flow.', () => { publisherAccount, oceanNode ) - console.log('orderTxReceipt', orderTxReceipt) + assert(orderTxReceipt, 'order transaction failed') orderTxId = orderTxReceipt.hash assert(orderTxId, 'transaction id not found') })