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

Backwards comp fixes #3610

Merged
merged 2 commits into from
Dec 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {

let assertionPromise;

if (isOperationV0) {
if (!assertionId) {
assertionId = await this.tripleStoreService.getLatestAssertionId(
TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT,
ual,
);

this.logger.info(
`Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`,
);
}
if (!assertionId) {
assertionId = await this.tripleStoreService.getLatestAssertionId(
TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT,
ual,
);

this.logger.info(
`Found assertion id: ${assertionId}, operation id ${operationId}, ual: ${ual}`,
);
}

if (assertionId) {
// DO NOT RUN THIS IF !assertionId
assertionPromise = this.tripleStoreService
.getV6Assertion(TRIPLE_STORE_REPOSITORIES.PUBLIC_CURRENT, assertionId)
Expand All @@ -145,10 +145,7 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
blockchain,
);

return [
...(fallbackResult.public ?? []),
...(fallbackResult.private ?? []),
];
return fallbackResult;
}

this.operationIdService.emitChangeEvent(
Expand All @@ -161,11 +158,21 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
});
} else {
if (!knowledgeAssetId) {
knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange(
blockchain,
contract,
knowledgeCollectionId,
);
try {
knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange(
blockchain,
contract,
knowledgeCollectionId,
);
} catch (error) {
// Asset created on old content asset storage contract
// TODO: actually it could be other error so we should check that, or add try catch to getKARange function
knowledgeAssetId = {
startTokenId: 1,
endTokenId: 1,
burned: [],
};
}
}
assertionPromise = this.tripleStoreService
.getAssertion(
Expand Down Expand Up @@ -208,7 +215,9 @@ class HandleGetRequestCommand extends HandleProtocolMessageCommand {
const [assertion, metadata] = await Promise.all(promises);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class GetFindShardCommand extends FindShardCommand {
getOperationCommandSequence(nodePartOfShard, commandData) {
const sequence = [];
if (nodePartOfShard) {
// If operationV0 and no assertionId found go directly to networkGet
if (!commandData.isOperationV0 || commandData.assertionId) {
sequence.push('localGetCommand');
}
sequence.push('localGetCommand');
}
sequence.push('networkGetCommand');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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(
Expand Down
25 changes: 19 additions & 6 deletions src/commands/protocols/get/sender/local-get-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LocalGetCommand extends Command {
knowledgeCollectionId,
contentType,
assertionId,
isOperationV0,
} = command.data;
let { knowledgeAssetId } = command.data;
await this.operationIdService.updateOperationIdStatus(
Expand Down Expand Up @@ -140,11 +141,20 @@ class LocalGetCommand extends Command {
} else {
// TODO: Do this in clean way
if (!knowledgeAssetId) {
knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange(
blockchain,
contract,
knowledgeCollectionId,
);
try {
knowledgeAssetId = await this.blockchainModuleManager.getKnowledgeAssetsRange(
blockchain,
contract,
knowledgeCollectionId,
);
} catch (error) {
// Asset created on old content asset storage contract
knowledgeAssetId = {
startTokenId: 1,
endTokenId: 1,
burned: [],
};
}
}
assertionPromise = this.tripleStoreService
.getAssertion(
Expand Down Expand Up @@ -188,9 +198,12 @@ class LocalGetCommand extends Command {
const [assertion, metadata] = await Promise.all(promises);

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

if (assertion?.public?.length || assertion?.private?.length || assertion?.length) {
await this.operationService.markOperationAsCompleted(
operationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -68,7 +69,13 @@ class GetRequestCommand extends ProtocolRequestCommand {
}

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

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

if (responseData?.assertion?.public) {
// Only whole collection can be validated not particular KA

Expand All @@ -92,31 +99,47 @@ class GetRequestCommand extends ProtocolRequestCommand {
publicKnowledgeAssetsTriplesGrouped.push(
...kcTools.groupNquadsBySubject(privateHashTriples, true),
);
try {
await this.validationService.validateDatasetOnBlockchain(
publicKnowledgeAssetsTriplesGrouped.map((t) => t.sort()).flat(),
blockchain,
contract,
knowledgeCollectionId,
);

// This is added as support when get starts supporting private for curated paranet
// TODO: This needs to be fixed when paranets are introduced
if (responseData.assertion?.private?.length)
await this.validationService.validatePrivateMerkleRoot(
responseData.assertion.public,
responseData.assertion.private,

if (!isOldContract) {
try {
await this.validationService.validateDatasetOnBlockchain(
publicKnowledgeAssetsTriplesGrouped.map((t) => t.sort()).flat(),
blockchain,
contract,
knowledgeCollectionId,
);
} catch (e) {
return this.handleNack(command, {
errorMessage: e.message,
});

// This is added as support when get starts supporting private for curated paranet
// TODO: This needs to be fixed when paranets are introduced
if (responseData.assertion?.private?.length)
await this.validationService.validatePrivateMerkleRoot(
responseData.assertion.public,
responseData.assertion.private,
);
} catch (e) {
return this.handleNack(command, {
errorMessage: e.message,
});
}
}
}

let updatedResponseData = responseData;

if (isOperationV0) {
// TODO: Extract converting assertion into one array from the object into 1 function since its used for v0
const assertion = [
...(responseData.assertion?.public ?? []),
...(responseData.assertion?.private ?? []),
];

updatedResponseData = { ...responseData, assertion };
}

await this.operationService.processResponse(
command,
OPERATION_REQUEST_STATUS.COMPLETED,
responseData,
updatedResponseData,
);

return ProtocolRequestCommand.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BidSuggestionController extends BaseController {

async handleRequest(req, res) {
try {
const { blockchain, epochsNumber, assertionSize } = req.body;
const { blockchain, epochsNumber, assertionSize } = req.query;
const promises = [
this.blockchainModuleManager.getTimeUntilNextEpoch(blockchain),
this.blockchainModuleManager.getEpochLength(blockchain),
Expand Down
Loading