From 41484513190827b9d31e7a20bd9950191874a759 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Thu, 19 Dec 2024 19:38:18 +0100 Subject: [PATCH 1/6] Use query to get assertion id of ual instead of blockchain --- .../get-assertion-merkle-root-command.js | 28 ++++++++++++------- .../http-api/v0/get-http-api-controller-v0.js | 1 - .../implementation/ot-triple-store.js | 15 ++++++++++ .../triple-store-module-manager.js | 9 ++++++ src/service/triple-store-service.js | 10 +++++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js index 7ffc0d321..510ff0878 100644 --- a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js +++ b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js @@ -1,11 +1,12 @@ import Command from '../../../command.js'; -import { ERROR_TYPE } from '../../../../constants/constants.js'; +import { ERROR_TYPE, TRIPLE_STORE_REPOSITORIES } from '../../../../constants/constants.js'; class GetAssertionMerkleRootCommand extends Command { constructor(ctx) { super(ctx); this.operationService = ctx.getService; this.blockchainModuleManager = ctx.blockchainModuleManager; + this.tripleStoreService = ctx.tripleStoreService; this.errorType = ERROR_TYPE.GET.GET_ASSERTION_ID_ERROR; } @@ -15,19 +16,26 @@ class GetAssertionMerkleRootCommand extends Command { * @param command */ async execute(command) { - const { operationId, blockchain, contract, knowledgeCollectionId } = command.data; - this.logger.info( - `Getting assertion id for token id: ${knowledgeCollectionId}, contract: ${contract}, operation id: ${operationId} on blockchain: ${blockchain}`, - ); + const { operationId, ual } = command.data; + this.logger.info(`Getting assertion id and operation id ${operationId} for ual: ${ual}`); - const assertionId = await this.blockchainModuleManager.getLatestAssertionId( - blockchain, - contract, - knowledgeCollectionId, + let assertionId = await this.tripleStoreService.getLatestAssertionId( + TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, + ual, ); + if (!assertionId) { + assertionId = await this.tripleStoreService.getLatestAssertionId( + TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT, + ual, + ); + } + + if (!assertionId) { + throw new Error(`No assertionId found for UAL: ${ual} in either repository.`); + } this.logger.info( - `Found assertion id: ${assertionId} for token id: ${knowledgeCollectionId}, contract: ${contract} on blockchain: ${blockchain} for operation id: ${operationId}`, + `Found assertion id: ${assertionId} and operation id ${operationId} ual: ${ual}`, ); return this.continueSequence({ ...command.data, assertionId }, command.sequence); } diff --git a/src/controllers/http-api/v0/get-http-api-controller-v0.js b/src/controllers/http-api/v0/get-http-api-controller-v0.js index 02698057c..02b66bb6f 100644 --- a/src/controllers/http-api/v0/get-http-api-controller-v0.js +++ b/src/controllers/http-api/v0/get-http-api-controller-v0.js @@ -64,7 +64,6 @@ class GetController extends BaseController { // const commandSequence = []; - commandSequence.push('getValidateAssetCommand'); if ( !tripleStoreMigrationAlreadyExecuted && diff --git a/src/modules/triple-store/implementation/ot-triple-store.js b/src/modules/triple-store/implementation/ot-triple-store.js index 208495b53..ae9b64c41 100644 --- a/src/modules/triple-store/implementation/ot-triple-store.js +++ b/src/modules/triple-store/implementation/ot-triple-store.js @@ -512,6 +512,21 @@ class OtTripleStore { this.select(repository, query); } + async getLatestAssertionId(repository, ual) { + const query = `SELECT ?assertionId + WHERE { + GRAPH { + <${ual}> ?p ?assertionId + } + }`; + + const data = await this.select(repository, query); + + const latestAssertionId = data?.[0]?.assertionId; + + return latestAssertionId; + } + async construct(repository, query) { return this._executeQuery(repository, query, MEDIA_TYPES.N_QUADS); } diff --git a/src/modules/triple-store/triple-store-module-manager.js b/src/modules/triple-store/triple-store-module-manager.js index 5a39daf25..e3694103a 100644 --- a/src/modules/triple-store/triple-store-module-manager.js +++ b/src/modules/triple-store/triple-store-module-manager.js @@ -223,6 +223,15 @@ class TripleStoreModuleManager extends BaseModuleManager { } } + async getLatestAssertionId(implementationName, repository, ual) { + if (this.getImplementation(implementationName)) { + return this.getImplementation(implementationName).module.getLatestAssertionId( + repository, + ual, + ); + } + } + async construct(implementationName, repository, query) { if (this.getImplementation(implementationName)) { return this.getImplementation(implementationName).module.construct(repository, query); diff --git a/src/service/triple-store-service.js b/src/service/triple-store-service.js index f0a0ae6bc..d0a002cc5 100644 --- a/src/service/triple-store-service.js +++ b/src/service/triple-store-service.js @@ -460,6 +460,16 @@ class TripleStoreService { return nquads; } + async getLatestAssertionId(repository, ual) { + const nquads = await this.tripleStoreModuleManager.getLatestAssertionId( + this.repositoryImplementations[repository], + repository, + ual, + ); + + return nquads; + } + async construct(query, repository = TRIPLE_STORE_REPOSITORY.DKG) { return this.tripleStoreModuleManager.construct( this.repositoryImplementations[repository] ?? From af4739d08231c149b311b5531004d0f895d89cb5 Mon Sep 17 00:00:00 2001 From: Marko Brkic Date: Thu, 19 Dec 2024 19:50:46 +0100 Subject: [PATCH 2/6] Update src/commands/protocols/get/sender/get-assertion-merkle-root-command.js Co-authored-by: Mihajlo Pavlovic --- .../protocols/get/sender/get-assertion-merkle-root-command.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js index 510ff0878..db71205e3 100644 --- a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js +++ b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js @@ -35,7 +35,7 @@ class GetAssertionMerkleRootCommand extends Command { throw new Error(`No assertionId found for UAL: ${ual} in either repository.`); } this.logger.info( - `Found assertion id: ${assertionId} and operation id ${operationId} ual: ${ual}`, + `Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`, ); return this.continueSequence({ ...command.data, assertionId }, command.sequence); } From 5bb23862cf8ae00a96181db362cbf045c9c86f1e Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Thu, 19 Dec 2024 20:03:56 +0100 Subject: [PATCH 3/6] Add some of the network logic --- .../v1-0-0-handle-get-request-command.js | 27 +++++++++++-------- .../get/sender/get-find-shard-command.js | 5 +++- .../sender/get-schedule-messages-command.js | 2 ++ .../v1.0.0/v1-0-0-get-request-command.js | 4 +++ src/controllers/rpc/get-rpc-controller.js | 2 ++ 5 files changed, 28 insertions(+), 12 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 b01b1e377..3ee3337b4 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 @@ -38,6 +38,7 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { ual, includeMetadata, assertionId, + isOperationV0, } = commandData; // if (paranetUAL) { @@ -109,17 +110,21 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { let assertionPromise; - if (assertionId) { - assertionPromise = this.tripleStoreService - .getV6Assertion(TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, assertionId) - .then((result) => { - this.operationIdService.emitChangeEvent( - OPERATION_ID_STATUS.GET.GET_REMOTE_GET_ASSERTION_END, - operationId, - blockchain, - ); - return result; - }); + if (isOperationV0) { + if (!assertionId) { + // Find assertion id + } else { + assertionPromise = this.tripleStoreService + .getV6Assertion(TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, assertionId) + .then((result) => { + this.operationIdService.emitChangeEvent( + OPERATION_ID_STATUS.GET.GET_REMOTE_GET_ASSERTION_END, + operationId, + blockchain, + ); + return result; + }); + } } else { assertionPromise = this.tripleStoreService .getAssertion( diff --git a/src/commands/protocols/get/sender/get-find-shard-command.js b/src/commands/protocols/get/sender/get-find-shard-command.js index f3f0e5842..409f50452 100644 --- a/src/commands/protocols/get/sender/get-find-shard-command.js +++ b/src/commands/protocols/get/sender/get-find-shard-command.js @@ -22,7 +22,10 @@ class GetFindShardCommand extends FindShardCommand { getOperationCommandSequence(nodePartOfShard, commandData) { const sequence = []; if (nodePartOfShard) { - sequence.push('localGetCommand'); + // If operationV0 and no assertionId found go directly to networkGet + if (!commandData.isOperationV0 || commandData.assertionId) { + sequence.push('localGetCommand'); + } } sequence.push('networkGetCommand'); diff --git a/src/commands/protocols/get/sender/get-schedule-messages-command.js b/src/commands/protocols/get/sender/get-schedule-messages-command.js index d59bb6897..9ec9eb44b 100644 --- a/src/commands/protocols/get/sender/get-schedule-messages-command.js +++ b/src/commands/protocols/get/sender/get-schedule-messages-command.js @@ -26,6 +26,8 @@ class GetScheduleMessagesCommand extends ProtocolScheduleMessagesCommand { paranetUAL: command.data.paranetUAL, paranetId: command.data.paranetId, paranetMetadata: command.data.paranetMetadata, + assertionId: command.data.isOperationV0, + isOperationV0: command.data.assertionId, }; } diff --git a/src/commands/protocols/get/sender/v1.0.0/v1-0-0-get-request-command.js b/src/commands/protocols/get/sender/v1.0.0/v1-0-0-get-request-command.js index 786001ab3..84a727a52 100644 --- a/src/commands/protocols/get/sender/v1.0.0/v1-0-0-get-request-command.js +++ b/src/commands/protocols/get/sender/v1.0.0/v1-0-0-get-request-command.js @@ -47,6 +47,8 @@ class GetRequestCommand extends ProtocolRequestCommand { ual, paranetUAL, paranetId, + isOperationV0, + assertionId, } = command.data; return { @@ -58,6 +60,8 @@ class GetRequestCommand extends ProtocolRequestCommand { ual, paranetUAL, paranetId, + isOperationV0, + assertionId, }; } diff --git a/src/controllers/rpc/get-rpc-controller.js b/src/controllers/rpc/get-rpc-controller.js index 571d1511e..32b242b84 100644 --- a/src/controllers/rpc/get-rpc-controller.js +++ b/src/controllers/rpc/get-rpc-controller.js @@ -37,6 +37,8 @@ class GetController extends BaseController { state: message.data.state ?? DEFAULT_GET_STATE, paranetUAL: message.data.paranetUAL, paranetId: message.data.paranetId, + isOperationV0: message.data.isOperationV0, + assertionId: message.data.assertionId, }, transactional: false, }); From b4bac2d66c9b7d870476170e59866156deb49629 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Thu, 19 Dec 2024 20:09:12 +0100 Subject: [PATCH 4/6] Better comment + logic on receiving nodes --- .../v1-0-0-handle-get-request-command.js | 41 +++++++++++++------ .../get-assertion-merkle-root-command.js | 3 -- 2 files changed, 28 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 3ee3337b4..5a6fc0856 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 @@ -37,10 +37,12 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { knowledgeAssetId, ual, includeMetadata, - assertionId, + isOperationV0, } = commandData; + let { assertionId } = commandData; + // if (paranetUAL) { // const paranetNodeAccessPolicy = await this.blockchainModuleManager.getNodesAccessPolicy( // blockchain, @@ -112,19 +114,32 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { if (isOperationV0) { if (!assertionId) { - // Find assertion id - } else { - assertionPromise = this.tripleStoreService - .getV6Assertion(TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, assertionId) - .then((result) => { - this.operationIdService.emitChangeEvent( - OPERATION_ID_STATUS.GET.GET_REMOTE_GET_ASSERTION_END, - operationId, - blockchain, - ); - return result; - }); + assertionId = await this.tripleStoreService.getLatestAssertionId( + TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, + ual, + ); + + if (!assertionId) { + assertionId = await this.tripleStoreService.getLatestAssertionId( + TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT, + ual, + ); + } + + this.logger.info( + `Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`, + ); } + assertionPromise = this.tripleStoreService + .getV6Assertion(TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, assertionId) + .then((result) => { + this.operationIdService.emitChangeEvent( + OPERATION_ID_STATUS.GET.GET_REMOTE_GET_ASSERTION_END, + operationId, + blockchain, + ); + return result; + }); } else { assertionPromise = this.tripleStoreService .getAssertion( diff --git a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js index db71205e3..e5346592a 100644 --- a/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js +++ b/src/commands/protocols/get/sender/get-assertion-merkle-root-command.js @@ -31,9 +31,6 @@ class GetAssertionMerkleRootCommand extends Command { ); } - if (!assertionId) { - throw new Error(`No assertionId found for UAL: ${ual} in either repository.`); - } this.logger.info( `Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`, ); From e71a25ff4238978ecb497c6efc819eacdd5c04f8 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Thu, 19 Dec 2024 20:13:02 +0100 Subject: [PATCH 5/6] Return nack immediately if assertionId is not found in receiving nodes --- .../receiver/v1.0.0/v1-0-0-handle-get-request-command.js | 7 +++++++ 1 file changed, 7 insertions(+) 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 5a6fc0856..5f44e753a 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 @@ -126,6 +126,13 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { ); } + if (!assertionId) { + return { + messageType: NETWORK_MESSAGE_TYPES.RESPONSES.NACK, + messageData: { errorMessage: `Unable to find assertion ${ual}` }, + }; + } + this.logger.info( `Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`, ); From c2ff35e9b8dd510176f162f233a8657414b684f5 Mon Sep 17 00:00:00 2001 From: brkagithub Date: Thu, 19 Dec 2024 20:15:02 +0100 Subject: [PATCH 6/6] remove extra line --- .../get/receiver/v1.0.0/v1-0-0-handle-get-request-command.js | 1 - 1 file changed, 1 deletion(-) 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 5f44e753a..1f8474e9a 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 @@ -37,7 +37,6 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand { knowledgeAssetId, ual, includeMetadata, - isOperationV0, } = commandData;