Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local store #3708

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1+hotfix.3",
"version": "8.0.1+hotfix.4",
"description": "OTNode V8",
"main": "index.js",
"type": "module",
Expand Down
5 changes: 5 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,11 @@ export const HTTP_API_ROUTES = {
path: '/ask',
options: {},
},
'local-store': {
method: 'post',
path: '/local-store',
options: {},
},
},
};

Expand Down
157 changes: 157 additions & 0 deletions src/controllers/http-api/v1/local-store-http-api-controller-v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { kcTools } from 'assertion-tools';
import BaseController from '../base-http-api-controller.js';
import {
PRIVATE_HASH_SUBJECT_PREFIX,
TRIPLE_STORE_REPOSITORIES,
} from '../../../constants/constants.js';

class LocalStoreController extends BaseController {
constructor(ctx) {
super(ctx);
this.validationService = ctx.validationService;
this.ualService = ctx.ualService;
this.tripleStoreService = ctx.tripleStoreService;
this.cryptoService = ctx.cryptoService;
}

async handleRequest(req, res) {
const { dataset, blockchain, datasetRoot, UAL } = req.body;
let contract;
let knowledgeCollectionId;
try {
({ contract, knowledgeCollectionId } = this.ualService.resolveUAL(UAL));
const { publicKnowledgeAssetsUALs, privateKnowledgeAssetsUALs } = this.getKAUALs(
dataset,
UAL,
);

const alreadyInserted = await this.tripleStoreService.ask(`
ASK {
FILTER (
${[...publicKnowledgeAssetsUALs, ...privateKnowledgeAssetsUALs]
.map((ual) => `EXISTS { GRAPH <${ual}> { ?s ?p ?o } }`)
.join(' && ')}
)
}
`);

if (alreadyInserted) {
return this.returnResponse(res, 200, {
status: true,
});
}
} catch (error) {
return this.returnResponse(res, 500, {
status: false,
error,
});
}

try {
const validations = [
this.validationService.validateDatasetRoot(dataset.public, datasetRoot),
this.validationService.validateDatasetRootOnBlockchain(
datasetRoot,
blockchain,
contract,
knowledgeCollectionId,
),
];

if (dataset?.private?.length) {
validations.push(
this.validationService.validatePrivateMerkleRoot(
dataset.public,
dataset.private,
),
);
}

await Promise.all(validations);
} catch (error) {
return this.returnResponse(res, 200, {
status: false,
error,
});
}
try {
await this.tripleStoreService.insertKnowledgeCollection(
TRIPLE_STORE_REPOSITORIES.DKG,
UAL,
dataset,
);

return this.returnResponse(res, 200, {
status: true,
});
} catch (error) {
return this.returnResponse(res, 200, {
status: false,
error,
});
}
}

getKAUALs(dataset, UAL) {
const privateHashTriples = [];
const filteredPublic = [];
let privateKnowledgeAssetsUALs = [];
// Check if already inserted
dataset.public.forEach((triple) => {
if (triple.startsWith(`<${PRIVATE_HASH_SUBJECT_PREFIX}`)) {
privateHashTriples.push(triple);
} else {
filteredPublic.push(triple);
}
});

const publicKnowledgeAssetsTriplesGrouped = kcTools.groupNquadsBySubject(
filteredPublic,
true,
);
publicKnowledgeAssetsTriplesGrouped.push(
...kcTools.groupNquadsBySubject(privateHashTriples, true),
);

let publicKnowledgeAssetsUALs = publicKnowledgeAssetsTriplesGrouped.map(
(_, index) => `${UAL}/${index + 1}`,
);

if (dataset.private?.length) {
const privateKnowledgeAssetsTriplesGrouped = kcTools.groupNquadsBySubject(
dataset.private,
true,
);

const publicSubjectMap = publicKnowledgeAssetsTriplesGrouped.reduce(
(map, group, index) => {
const [publicSubject] = group[0].split(' ');
map.set(publicSubject, index);
return map;
},
new Map(),
);

for (const privateTriple of privateKnowledgeAssetsTriplesGrouped) {
const [privateSubject] = privateTriple[0].split(' ');
if (publicSubjectMap.has(privateSubject)) {
const ualIndex = publicSubjectMap.get(privateSubject);
privateKnowledgeAssetsUALs.push(publicKnowledgeAssetsUALs[ualIndex]);
} else {
const privateSubjectHashed = `<${PRIVATE_HASH_SUBJECT_PREFIX}${this.cryptoService.sha256(
privateSubject.slice(1, -1),
)}>`;
if (publicSubjectMap.has(privateSubjectHashed)) {
const ualIndex = publicSubjectMap.get(privateSubjectHashed);
privateKnowledgeAssetsUALs.push(publicKnowledgeAssetsUALs[ualIndex]);
}
}
}
}
publicKnowledgeAssetsUALs = publicKnowledgeAssetsUALs.map((ual) => `${ual}/public`);
privateKnowledgeAssetsUALs = privateKnowledgeAssetsUALs.map((ual) => `${ual}/private`);
return { publicKnowledgeAssetsUALs, privateKnowledgeAssetsUALs };
}
}

export default LocalStoreController;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export default (argumentsObject) => ({
type: 'object',
required: ['datasetRoot', 'dataset', 'blockchain'],
properties: {
datasetRoot: {
type: 'string',
minLength: 66,
maxLength: 66,
},
dataset: {
type: 'object',
properties: {
public: {
type: 'array',
items: {
type: 'string',
},
minItems: 1,
},
private: {
type: 'array',
items: {
type: 'string',
},
},
},
required: ['public'],
additionalProperties: false,
},
blockchain: {
enum: argumentsObject.blockchainImplementationNames,
},
UAL: {
type: 'string',
},
},
});
6 changes: 6 additions & 0 deletions src/modules/triple-store/triple-store-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ class TripleStoreModuleManager extends BaseModuleManager {
}
}

async ask(implementationName, repository, query) {
if (this.getImplementation(implementationName)) {
return this.getImplementation(implementationName).module.ask(repository, query);
}
}

getName() {
return 'tripleStore';
}
Expand Down
9 changes: 9 additions & 0 deletions src/service/triple-store-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ class TripleStoreService {
);
}

async ask(query, repository = TRIPLE_STORE_REPOSITORY.DKG) {
return this.tripleStoreModuleManager.ask(
this.repositoryImplementations[repository] ??
this.repositoryImplementations[TRIPLE_STORE_REPOSITORY.DKG],
repository,
query,
);
}

async queryVoid(repository, query, namedGraphs = null, labels = null) {
return this.tripleStoreModuleManager.queryVoid(
this.repositoryImplementations[repository],
Expand Down
Loading