Skip to content

Commit

Permalink
Fix get on migrated assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Zvonimir authored and Zvonimir committed Dec 27, 2024
1 parent d76f875 commit 727f5e2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
ual,
includeMetadata,
isOperationV0,
isOldContract,
} = commandData;

let { assertionId, knowledgeAssetId } = commandData;
Expand Down Expand Up @@ -109,6 +110,7 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
);

let assertionPromise;
let notMigrated = false;

if (!assertionId) {
assertionId = await this.tripleStoreService.getLatestAssertionId(
Expand Down Expand Up @@ -148,6 +150,10 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
return fallbackResult;
}

if (!isOperationV0) {
notMigrated = true;
}

this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.GET.GET_REMOTE_GET_ASSERTION_END,
operationId,
Expand Down Expand Up @@ -215,9 +221,10 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
const [assertion, metadata] = await Promise.all(promises);

const responseData = {
assertion: isOperationV0
? [...(assertion.public ?? []), ...(assertion.private ?? [])]
: assertion,
assertion:
(isOperationV0 || notMigrated) && isOldContract
? [...(assertion.public ?? []), ...(assertion.private ?? [])]
: assertion,
...(includeMetadata && metadata && { metadata }),
};

Expand Down
25 changes: 11 additions & 14 deletions src/commands/protocols/get/sender/get-validate-asset-command.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import ValidateAssetCommand from '../../../common/validate-asset-command.js';
import Command from '../../../command.js';
import {
OPERATION_ID_STATUS,
ERROR_TYPE,
OLD_CONTENT_STORAGE_MAP,
} from '../../../../constants/constants.js';
import { OPERATION_ID_STATUS, ERROR_TYPE } from '../../../../constants/constants.js';

class GetValidateAssetCommand extends ValidateAssetCommand {
constructor(ctx) {
Expand All @@ -27,8 +23,15 @@ class GetValidateAssetCommand extends ValidateAssetCommand {
* @param command
*/
async execute(command) {
const { operationId, blockchain, contract, knowledgeCollectionId, ual, isOperationV0 } =
command.data;
const {
operationId,
blockchain,
contract,
knowledgeCollectionId,
ual,
isOperationV0,
isOldContract,
} = command.data;
await this.operationIdService.updateOperationIdStatus(
operationId,
blockchain,
Expand All @@ -53,13 +56,7 @@ class GetValidateAssetCommand extends ValidateAssetCommand {
blockchain,
);
// TODO: Update to validate knowledge asset index
// TODO: Use isOldContract as variable and pass it through with command.data since it's used
if (
!isOperationV0 &&
Object.values(OLD_CONTENT_STORAGE_MAP).every(
(ca) => !ca.toLowerCase().includes(contract.toLowerCase()),
)
) {
if (!isOperationV0 && !isOldContract) {
const isValidUal = await this.validationService.validateUal(
blockchain,
contract,
Expand Down
51 changes: 36 additions & 15 deletions src/commands/protocols/get/sender/local-get-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class LocalGetCommand extends Command {
contentType,
assertionId,
isOperationV0,
isOldContract,
} = command.data;
let { knowledgeAssetId } = command.data;
await this.operationIdService.updateOperationIdStatus(
Expand Down Expand Up @@ -103,23 +104,13 @@ class LocalGetCommand extends Command {
);

let assertionPromise;
let notMigrated = false;

if (assertionId) {
assertionPromise = (async () => {
let result = null;

for (const repository of [
TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT,
TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT,
]) {
// eslint-disable-next-line no-await-in-loop
result = await this.tripleStoreService.getV6Assertion(repository, assertionId);
if (result?.length) {
break;
}
}

if (!result?.length) {
if (!isOperationV0) {
result = await this.tripleStoreService.getAssertion(
blockchain,
contract,
Expand All @@ -128,6 +119,35 @@ class LocalGetCommand extends Command {
contentType,
);
}

if (!result?.length) {
for (const repository of [
TRIPLE_STORE_REPOSITORIES.PRIVATE_CURRENT,
TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT,
]) {
// eslint-disable-next-line no-await-in-loop
result = await this.tripleStoreService.getV6Assertion(
repository,
assertionId,
);
if (result?.length) {
if (!isOperationV0) {
notMigrated = true;
}
break;
}
}
if (!result?.length && isOperationV0) {
result = await this.tripleStoreService.getAssertion(
blockchain,
contract,
knowledgeCollectionId,
knowledgeAssetId,
contentType,
);
}
}

this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.GET.GET_LOCAL_GET_ASSERTION_END,
operationId,
Expand Down Expand Up @@ -198,9 +218,10 @@ class LocalGetCommand extends Command {
const [assertion, metadata] = await Promise.all(promises);

const responseData = {
assertion: isOperationV0
? [...(assertion?.public ?? []), ...(assertion?.private ?? [])]
: assertion,
assertion:
(isOperationV0 || notMigrated) && isOldContract
? [...(assertion?.public ?? []), ...(assertion?.private ?? [])]
: assertion,
...(includeMetadata && metadata && { metadata }),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
OPERATION_STATUS,
OPERATION_ID_STATUS,
PRIVATE_HASH_SUBJECT_PREFIX,
OLD_CONTENT_STORAGE_MAP,
} from '../../../../../constants/constants.js';

class GetRequestCommand extends ProtocolRequestCommand {
Expand Down Expand Up @@ -52,6 +51,7 @@ class GetRequestCommand extends ProtocolRequestCommand {
paranetId,
isOperationV0,
assertionId,
isOldContract,
} = command.data;

return {
Expand All @@ -65,16 +65,19 @@ class GetRequestCommand extends ProtocolRequestCommand {
paranetId,
isOperationV0,
assertionId,
isOldContract,
};
}

async handleAck(command, responseData) {
const { blockchain, contract, knowledgeCollectionId, knowledgeAssetId, isOperationV0 } =
command.data;

const isOldContract = Object.values(OLD_CONTENT_STORAGE_MAP).some((ca) =>
ca.toLowerCase().includes(contract.toLowerCase()),
);
const {
blockchain,
contract,
knowledgeCollectionId,
knowledgeAssetId,
isOperationV0,
isOldContract,
} = command.data;

if (responseData?.assertion?.public) {
// Only whole collection can be validated not particular KA
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/http-api/v0/get-http-api-controller-v0.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ class GetController extends BaseController {
// Get assertionId - datasetRoot
//

const isOldContract = Object.values(OLD_CONTENT_STORAGE_MAP).some((ca) =>
ca.toLowerCase().includes(contract.toLowerCase()),
);

const commandSequence = [];

if (
!tripleStoreMigrationAlreadyExecuted &&
Object.values(OLD_CONTENT_STORAGE_MAP)
.map((ca) => ca.toLowerCase())
.includes(contract.toLowerCase())
) {
if (!tripleStoreMigrationAlreadyExecuted && isOldContract) {
commandSequence.push('getAssertionMerkleRootCommand');
}

Expand All @@ -91,6 +90,7 @@ class GetController extends BaseController {
knowledgeAssetId,
operationId,
paranetUAL,
isOldContract,
contentType: contentType ?? TRIPLES_VISIBILITY.ALL,
isOperationV0: true,
},
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/http-api/v1/get-http-api-controller-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ class GetController extends BaseController {
// Get assertionId - datasetRoot
//

const isOldContract = Object.values(OLD_CONTENT_STORAGE_MAP).some((ca) =>
ca.toLowerCase().includes(contract.toLowerCase()),
);

const commandSequence = [];
commandSequence.push('getValidateAssetCommand');

if (
!tripleStoreMigrationAlreadyExecuted &&
Object.values(OLD_CONTENT_STORAGE_MAP)
.map((ca) => ca.toLowerCase())
.includes(contract.toLowerCase())
) {
if (!tripleStoreMigrationAlreadyExecuted && isOldContract) {
commandSequence.push('getAssertionMerkleRootCommand');
}

Expand All @@ -93,6 +92,7 @@ class GetController extends BaseController {
knowledgeAssetId,
operationId,
paranetUAL,
isOldContract,
contentType: contentType ?? TRIPLES_VISIBILITY.ALL,
},
transactional: false,
Expand Down
1 change: 1 addition & 0 deletions src/controllers/rpc/get-rpc-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GetController extends BaseController {
paranetId: message.data.paranetId,
isOperationV0: message.data.isOperationV0,
assertionId: message.data.assertionId,
isOldContract: message.data.isOldContract,
},
transactional: false,
});
Expand Down

0 comments on commit 727f5e2

Please sign in to comment.