Skip to content

Commit

Permalink
Merge pull request #3598 from OriginTrail/v8/develop
Browse files Browse the repository at this point in the history
New query for get
  • Loading branch information
brkagithub authored Dec 24, 2024
2 parents c07884b + 0e53242 commit b940d84
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 117 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 @@ -33,7 +33,7 @@ class ValidateAssertionMetadataCommand extends Command {
assertion.public ?? assertion,
);

if (byteSize !== calculatedAssertionSize) {
if (byteSize.toString() !== calculatedAssertionSize.toString()) {
await this.handleError(
operationId,
blockchain,
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
6 changes: 4 additions & 2 deletions v8-data-migration/blockchain-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ function maskRpcUrl(url) {
export async function initializeRpc(rpcEndpoint) {
// Validation
if (!rpcEndpoint || typeof rpcEndpoint !== 'string') {
throw new Error(
logger.error(
`RPC endpoint is not defined or it is not a string. RPC endpoint: ${rpcEndpoint}`,
);
process.exit(1);
}
// initialize all possible providers
const Provider = ethers.providers.JsonRpcProvider;
Expand All @@ -39,7 +40,8 @@ export async function initializeRpc(rpcEndpoint) {
logger.info(`Connected to the blockchain RPC: ${maskRpcUrl(rpcEndpoint)}.`);
return provider;
} catch (e) {
throw new Error(`Unable to connect to the blockchain RPC: ${maskRpcUrl(rpcEndpoint)}.`);
logger.error(`Unable to connect to the blockchain RPC: ${maskRpcUrl(rpcEndpoint)}.`);
process.exit(1);
}
}

Expand Down
8 changes: 6 additions & 2 deletions v8-data-migration/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ export const VISIBILITY = {
PRIVATE: 'private',
};
export const BATCH_SIZE = 50;

export const DEFAULT_CONFIG_PATH = '/root/ot-node/current/config/config.json';
export const NODERC_CONFIG_PATH = '/root/ot-node/.origintrail_noderc';
export const DATA_MIGRATION_DIR = '/root/ot-node/data/data-migration';
export const LOG_DIR = '/root/ot-node/data/data-migration/logs';
export const ENV_PATH = '/root/ot-node/current/.env';
export const MIGRATION_PROGRESS_FILE = '/root/ot-node/data/migrations/v8DataMigration';
export const MIGRATION_DIR = '/root/ot-node/data/migrations/';
export const MIGRATION_PROGRESS_FILE = 'v8DataMigration';

export const DB_URLS = {
testnet: 'https://hosting.origin-trail.network/csv/testnet.db',
mainnet: '',
mainnet: 'https://hosting.origin-trail.network/csv/mainnet.db',
};

const require = createRequire(import.meta.url);
Expand Down
10 changes: 8 additions & 2 deletions v8-data-migration/logger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import pino from 'pino';
import fs from 'fs';
import { LOG_DIR } from './constants.js';

// Ensure logs directory exists
const LOG_DIR = './logs';
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
fs.mkdirSync(LOG_DIR, { recursive: true });

if (!fs.existsSync(LOG_DIR)) {
throw new Error(
`Something went wrong. Directory: ${LOG_DIR} does not exist after creation.`,
);
}
}

const timers = new Map();
Expand Down
2 changes: 1 addition & 1 deletion v8-data-migration/run-data-migration.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd /root/ot-node/current/v8-data-migration/ &&
npm rebuild sqlite3 &&
nohup node v8-data-migration.js > /root/ot-node/current/v8-data-migration/nohup.out 2>&1 &
nohup node v8-data-migration.js > /root/ot-node/data/nohup.out 2>&1 &
9 changes: 6 additions & 3 deletions v8-data-migration/sqlite-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class SqliteDatabase {
});

if (!this.db) {
throw new Error('Failed to initialize SQLite database');
logger.error('Failed to initialize SQLite database');
process.exit(1);
}
}

Expand Down Expand Up @@ -124,13 +125,15 @@ export class SqliteDatabase {

_validateConnection() {
if (!this.db) {
throw new Error('Database not initialized. Call initialize() first.');
logger.error('Database not initialized. Call initialize() first.');
process.exit(1);
}
}

_validateBlockchainName(blockchainName) {
if (!blockchainName) {
throw new Error('Blockchain name is required');
logger.error('Blockchain name is required');
process.exit(1);
}
}
}
Expand Down
Loading

0 comments on commit b940d84

Please sign in to comment.