Skip to content

Commit

Permalink
Merge pull request #3597 from OriginTrail/rework-get-query
Browse files Browse the repository at this point in the history
Update queries
  • Loading branch information
brkagithub authored Dec 24, 2024
2 parents 3219cc3 + 185c1ca commit 3e794b6
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion src/commands/protocols/get/sender/local-get-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down Expand Up @@ -137,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,
Expand Down
7 changes: 7 additions & 0 deletions src/modules/blockchain/blockchain-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class BlockchainModuleManager extends BaseModuleManager {
]);
}

async getKnowledgeAssetsRange(blockchain, assetStorageContractAddress, knowledgeCollectionId) {
return this.callImplementationFunction(blockchain, 'getKnowledgeAssetsRange', [
assetStorageContractAddress,
knowledgeCollectionId,
]);
}

async getParanetKnowledgeAssetsCount(blockchain, paranetId) {
return this.callImplementationFunction(blockchain, 'getParanetKnowledgeAssetsCount', [
paranetId,
Expand Down
31 changes: 31 additions & 0 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,37 @@ 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: 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(),
),
),
};
}

async getMinimumStake() {
const minimumStake = await this.callContractFunction(
this.contracts.ParametersStorage,
Expand Down
64 changes: 30 additions & 34 deletions src/modules/triple-store/implementation/ot-triple-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,52 +291,48 @@ class OtTripleStore {
await this.queryVoid(repository, query);
}

async getKnowledgeCollectionNamedGraphs(repository, ual, visibility) {
async getKnowledgeCollectionNamedGraphs(repository, tokenIds, ual, 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: <http://schema.org/>
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: <http://schema.org/>
CONSTRUCT {
?s ?p ?o .
PREFIX schema: <http://schema.org/>
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);
}

Expand Down
11 changes: 9 additions & 2 deletions src/modules/triple-store/triple-store-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/service/triple-store-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,27 @@ 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,
// TODO: Add state with implemented update
`${ual}`,
knowledgeAssetId,
visibility,
);
} else {
this.logger.debug(`Getting Assertion with the UAL: ${ual}.`);
nquads = await this.tripleStoreModuleManager.getKnowledgeCollectionNamedGraphs(
this.repositoryImplementations[repository],
repository,
ual,
knowledgeAssetId,
visibility,
);
}
Expand Down

0 comments on commit 3e794b6

Please sign in to comment.