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,