From a7398d991b64e40ca916360045ed97976b87b940 Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Tue, 24 Dec 2024 16:18:29 +0100 Subject: [PATCH 1/4] Update queries --- .../protocols/get/sender/local-get-command.js | 10 ++- .../blockchain/blockchain-module-manager.js | 6 ++ .../blockchain/implementation/web3-service.js | 17 +++++ .../implementation/ot-triple-store.js | 64 +++++++++---------- src/service/triple-store-service.js | 11 ++-- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/commands/protocols/get/sender/local-get-command.js b/src/commands/protocols/get/sender/local-get-command.js index 0054c3344..fdf8d5082 100644 --- a/src/commands/protocols/get/sender/local-get-command.js +++ b/src/commands/protocols/get/sender/local-get-command.js @@ -16,6 +16,7 @@ class LocalGetCommand extends Command { this.paranetService = ctx.paranetService; this.ualService = ctx.ualService; this.repositoryModuleManager = ctx.repositoryModuleManager; + this.blockchainModuleManager = ctx.blockchainModuleManager; this.errorType = ERROR_TYPE.GET.GET_LOCAL_ERROR; } @@ -31,10 +32,10 @@ class LocalGetCommand extends Command { includeMetadata, contract, knowledgeCollectionId, - knowledgeAssetId, contentType, assertionId, } = command.data; + let { knowledgeAssetId } = command.data; await this.operationIdService.updateOperationIdStatus( operationId, blockchain, @@ -116,6 +117,13 @@ class LocalGetCommand extends Command { break; } } + // TODO: Do this in clean way + if (!knowledgeAssetId) { + knowledgeAssetId = this.blockchainModuleManager.getKnowledgeAssetsRange( + blockchain, + knowledgeCollectionId, + ); + } if (!result?.length) { result = await this.tripleStoreService.getAssertion( diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index 82e24561c..dde4b2836 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -181,6 +181,12 @@ class BlockchainModuleManager extends BaseModuleManager { ]); } + async getKnowledgeAssetsRange(blockchain, knowledgeCollectionId) { + return this.callImplementationFunction(blockchain, 'getKnowledgeAssetsRange', [ + knowledgeCollectionId, + ]); + } + async getParanetKnowledgeAssetsCount(blockchain, paranetId) { return this.callImplementationFunction(blockchain, 'getParanetKnowledgeAssetsCount', [ paranetId, diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index b45969757..47b4c9f63 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -932,6 +932,23 @@ class Web3Service { return Number(knowledgeCollectionSize); } + async getKnowledgeAssetsRange(assetStorageContractAddress, knowledgeCollectionId) { + const assetStorageContractInstance = + this.assetStorageContracts[assetStorageContractAddress.toString().toLowerCase()]; + if (!assetStorageContractInstance) + throw new Error('Unknown asset storage contract address'); + const knowledgeAssetsRange = await this.callContractFunction( + assetStorageContractInstance, + 'getKnowledgeAssetsRange', + [knowledgeCollectionId], + ); + return { + startTokenId: knowledgeAssetsRange[0], + endTokenId: knowledgeAssetsRange[1], + burned: knowledgeAssetsRange[2], + }; + } + async getMinimumStake() { const minimumStake = await this.callContractFunction( this.contracts.ParametersStorage, diff --git a/src/modules/triple-store/implementation/ot-triple-store.js b/src/modules/triple-store/implementation/ot-triple-store.js index b82d2ecc9..f275b937b 100644 --- a/src/modules/triple-store/implementation/ot-triple-store.js +++ b/src/modules/triple-store/implementation/ot-triple-store.js @@ -291,52 +291,48 @@ class OtTripleStore { await this.queryVoid(repository, query); } - async getKnowledgeCollectionNamedGraphs(repository, ual, visibility) { + async getKnowledgeCollectionNamedGraphs(repository, ual, tokenIds, visibility) { + const namedGraphs = Array.from( + { length: tokenIds.endTokenId - tokenIds.startTokenId + 1 }, + (_, i) => tokenIds.startTokenId + i, + ) + .filter((id) => !tokenIds.burned.includes(id)) + .map((id) => `${ual}/${id}`); const assertion = {}; if (visibility === TRIPLES_VISIBILITY.PUBLIC || visibility === TRIPLES_VISIBILITY.ALL) { const query = ` PREFIX schema: CONSTRUCT { ?s ?p ?o . - } - WHERE { - { - SELECT ?s ?p ?o ?g - WHERE { - GRAPH ?g { - ?s ?p ?o . - } - FILTER ( - STRSTARTS(STR(?g), "${ual}") - && STRENDS(STR(?g), "${TRIPLES_VISIBILITY.PUBLIC}") - ) - } - ORDER BY ?g ?s ?p ?o + } + WHERE { + GRAPH ?g { + ?s ?p ?o . } - }`; + VALUES ?g { + ${namedGraphs + .map((graph) => `${graph}/${TRIPLES_VISIBILITY.PUBLIC}`) + .join('\n')} + } + }`; assertion.public = await this.construct(repository, query); } if (visibility === TRIPLES_VISIBILITY.PRIVATE || visibility === TRIPLES_VISIBILITY.ALL) { const query = ` - PREFIX schema: - CONSTRUCT { - ?s ?p ?o . + PREFIX schema: + CONSTRUCT { + ?s ?p ?o . + } + WHERE { + GRAPH ?g { + ?s ?p ?o . } - WHERE { - { - SELECT ?s ?p ?o ?g - WHERE { - GRAPH ?g { - ?s ?p ?o . - } - FILTER ( - STRSTARTS(STR(?g), "${ual}") - && STRENDS(STR(?g), "${TRIPLES_VISIBILITY.PRIVATE}") - ) - } - ORDER BY ?g ?s ?p ?o - } - }`; + VALUES ?g { + ${namedGraphs + .map((graph) => `${graph}/${TRIPLES_VISIBILITY.PRIVATE}`) + .join('\n')} + } + }`; assertion.private = await this.construct(repository, query); } diff --git a/src/service/triple-store-service.js b/src/service/triple-store-service.js index f37280856..f0ea24f27 100644 --- a/src/service/triple-store-service.js +++ b/src/service/triple-store-service.js @@ -380,14 +380,12 @@ class TripleStoreService { repository = TRIPLE_STORE_REPOSITORY.DKG, ) { // TODO: Use stateId - const ual = `did:dkg:${blockchain}/${contract}/${knowledgeCollectionId}${ - knowledgeAssetId ? `/${knowledgeAssetId}` : '' - }`; - - this.logger.debug(`Getting Assertion with the UAL: ${ual}.`); + let ual = `did:dkg:${blockchain}/${contract}/${knowledgeCollectionId}`; let nquads; - if (knowledgeAssetId) { + if (typeof knowledgeAssetId === 'string') { + ual = `${ual}/${knowledgeAssetId}`; + this.logger.debug(`Getting Assertion with the UAL: ${ual}.`); nquads = await this.tripleStoreModuleManager.getKnowledgeAssetNamedGraph( this.repositoryImplementations[repository], repository, @@ -396,6 +394,7 @@ class TripleStoreService { visibility, ); } else { + this.logger.debug(`Getting Assertion with the UAL: ${ual}.`); nquads = await this.tripleStoreModuleManager.getKnowledgeCollectionNamedGraphs( this.repositoryImplementations[repository], repository, From dda8c95b977d587bcab63810d689f011b4e46ce3 Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Tue, 24 Dec 2024 16:22:06 +0100 Subject: [PATCH 2/4] Add correct argument propagation --- .../triple-store/triple-store-module-manager.js | 11 +++++++++-- src/service/triple-store-service.js | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/modules/triple-store/triple-store-module-manager.js b/src/modules/triple-store/triple-store-module-manager.js index e3694103a..cde1972f0 100644 --- a/src/modules/triple-store/triple-store-module-manager.js +++ b/src/modules/triple-store/triple-store-module-manager.js @@ -137,11 +137,18 @@ class TripleStoreModuleManager extends BaseModuleManager { } } - async getKnowledgeCollectionNamedGraphs(implementationName, repository, ual, visibility, sort) { + async getKnowledgeCollectionNamedGraphs( + implementationName, + repository, + ual, + tokenIds, + visibility, + sort, + ) { if (this.getImplementation(implementationName)) { return this.getImplementation( implementationName, - ).module.getKnowledgeCollectionNamedGraphs(repository, ual, visibility, sort); + ).module.getKnowledgeCollectionNamedGraphs(repository, tokenIds, ual, visibility, sort); } } diff --git a/src/service/triple-store-service.js b/src/service/triple-store-service.js index f0ea24f27..8ab9214ac 100644 --- a/src/service/triple-store-service.js +++ b/src/service/triple-store-service.js @@ -391,6 +391,7 @@ class TripleStoreService { repository, // TODO: Add state with implemented update `${ual}`, + knowledgeAssetId, visibility, ); } else { @@ -399,6 +400,7 @@ class TripleStoreService { this.repositoryImplementations[repository], repository, ual, + knowledgeAssetId, visibility, ); } From bb9b9d472ffe2ef0c1eeb8dac622c6ee4eb3b834 Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Tue, 24 Dec 2024 17:17:07 +0100 Subject: [PATCH 3/4] New get working --- .../v1-0-0-handle-get-request-command.js | 10 ++++++++-- .../protocols/get/sender/local-get-command.js | 15 +++++++------- .../blockchain/blockchain-module-manager.js | 3 ++- .../blockchain/implementation/web3-service.js | 20 ++++++++++++++++--- .../implementation/ot-triple-store.js | 6 +++--- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/commands/protocols/get/receiver/v1.0.0/v1-0-0-handle-get-request-command.js b/src/commands/protocols/get/receiver/v1.0.0/v1-0-0-handle-get-request-command.js index a5058454a..09189febe 100644 --- a/src/commands/protocols/get/receiver/v1.0.0/v1-0-0-handle-get-request-command.js +++ b/src/commands/protocols/get/receiver/v1.0.0/v1-0-0-handle-get-request-command.js @@ -34,13 +34,12 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { blockchain, contract, knowledgeCollectionId, - knowledgeAssetId, ual, includeMetadata, isOperationV0, } = commandData; - let { assertionId } = commandData; + let { assertionId, knowledgeAssetId } = commandData; // if (paranetUAL) { // const paranetNodeAccessPolicy = await this.blockchainModuleManager.getNodesAccessPolicy( @@ -161,6 +160,13 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { return result.split('\n').filter((res) => res.length > 0); }); } else { + if (!knowledgeAssetId) { + knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange( + blockchain, + contract, + knowledgeCollectionId, + ); + } assertionPromise = this.tripleStoreService .getAssertion( blockchain, diff --git a/src/commands/protocols/get/sender/local-get-command.js b/src/commands/protocols/get/sender/local-get-command.js index fdf8d5082..39165d0e5 100644 --- a/src/commands/protocols/get/sender/local-get-command.js +++ b/src/commands/protocols/get/sender/local-get-command.js @@ -117,13 +117,6 @@ class LocalGetCommand extends Command { break; } } - // TODO: Do this in clean way - if (!knowledgeAssetId) { - knowledgeAssetId = this.blockchainModuleManager.getKnowledgeAssetsRange( - blockchain, - knowledgeCollectionId, - ); - } if (!result?.length) { result = await this.tripleStoreService.getAssertion( @@ -145,6 +138,14 @@ class LocalGetCommand extends Command { : result; })(); } else { + // TODO: Do this in clean way + if (!knowledgeAssetId) { + knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange( + blockchain, + contract, + knowledgeCollectionId, + ); + } assertionPromise = this.tripleStoreService .getAssertion( blockchain, diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index dde4b2836..9e6f691ea 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -181,8 +181,9 @@ class BlockchainModuleManager extends BaseModuleManager { ]); } - async getKnowledgeAssetsRange(blockchain, knowledgeCollectionId) { + async getKnowledgeAssetsRange(blockchain, assetStorageContractAddress, knowledgeCollectionId) { return this.callImplementationFunction(blockchain, 'getKnowledgeAssetsRange', [ + assetStorageContractAddress, knowledgeCollectionId, ]); } diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 47b4c9f63..34149490e 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -943,9 +943,23 @@ class Web3Service { [knowledgeCollectionId], ); return { - startTokenId: knowledgeAssetsRange[0], - endTokenId: knowledgeAssetsRange[1], - burned: knowledgeAssetsRange[2], + startTokenId: Number( + knowledgeAssetsRange[0] + .sub(BigNumber.from(knowledgeCollectionId - 1).mul('0x0f4240')) + .toString(), + ), + endTokenId: Number( + knowledgeAssetsRange[1] + .sub(BigNumber.from(knowledgeCollectionId - 1).mul('0x0f4240')) + .toString(), + ), + burned: knowledgeAssetsRange[2].map((burned) => + Number( + burned + .sub(BigNumber.from(knowledgeCollectionId - 1).mul('0x0f4240')) + .toString(), + ), + ), }; } diff --git a/src/modules/triple-store/implementation/ot-triple-store.js b/src/modules/triple-store/implementation/ot-triple-store.js index f275b937b..8a986c41c 100644 --- a/src/modules/triple-store/implementation/ot-triple-store.js +++ b/src/modules/triple-store/implementation/ot-triple-store.js @@ -291,7 +291,7 @@ class OtTripleStore { await this.queryVoid(repository, query); } - async getKnowledgeCollectionNamedGraphs(repository, ual, tokenIds, visibility) { + async getKnowledgeCollectionNamedGraphs(repository, tokenIds, ual, visibility) { const namedGraphs = Array.from( { length: tokenIds.endTokenId - tokenIds.startTokenId + 1 }, (_, i) => tokenIds.startTokenId + i, @@ -311,7 +311,7 @@ class OtTripleStore { } VALUES ?g { ${namedGraphs - .map((graph) => `${graph}/${TRIPLES_VISIBILITY.PUBLIC}`) + .map((graph) => `<${graph}/${TRIPLES_VISIBILITY.PUBLIC}>`) .join('\n')} } }`; @@ -329,7 +329,7 @@ class OtTripleStore { } VALUES ?g { ${namedGraphs - .map((graph) => `${graph}/${TRIPLES_VISIBILITY.PRIVATE}`) + .map((graph) => `<${graph}/${TRIPLES_VISIBILITY.PRIVATE}>`) .join('\n')} } }`; From 185c1cae34b7fb1d4fbff571f6c55a736348c67c Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Tue, 24 Dec 2024 17:18:04 +0100 Subject: [PATCH 4/4] version bump --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6f6de7da0..7bc0de04c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "origintrail_node", - "version": "8.0.0-sigma.14", + "version": "8.0.0-sigma.15", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "origintrail_node", - "version": "8.0.0-sigma.14", + "version": "8.0.0-sigma.15", "license": "ISC", "dependencies": { "@comunica/query-sparql": "^2.4.3", diff --git a/package.json b/package.json index ce65fce55..4ccd2d9d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "origintrail_node", - "version": "8.0.0-sigma.14", + "version": "8.0.0-sigma.15", "description": "OTNode V8", "main": "index.js", "type": "module",