Skip to content

Commit

Permalink
Add await to calculate root (#3416) (#3418)
Browse files Browse the repository at this point in the history
* Add await to calculate root (#3416)

* Add await

* version bump

* package update

* Add async for getMerkleProof

* Add await to tests

* more awaits
  • Loading branch information
Mihajlo-Pavlovic authored Nov 20, 2024
1 parent f36a7a5 commit 4ccdd1f
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 27 deletions.
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "origintrail_node",
"version": "8.1.3+beta.0",
"version": "8.1.3+beta.2",
"description": "OTNode V8",
"main": "index.js",
"type": "module",
Expand Down Expand Up @@ -73,7 +73,7 @@
"@polkadot/util": "^10.1.7",
"@polkadot/util-crypto": "^10.1.7",
"app-root-path": "^3.1.0",
"assertion-tools": "^2.2.0",
"assertion-tools": "^2.2.1",
"async": "^3.2.4",
"async-mutex": "^0.3.2",
"awilix": "^7.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/common/validate-asset-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ValidateAssetCommand extends Command {
);

try {
this.validationService.validateAssertionId(
await this.validationService.validateAssertionId(
cachedData.private.assertion,
cachedData.private.assertionId,
);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/protocols/common/submit-proofs-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SubmitProofsCommand extends SendTransactionCommand {
return Command.empty();
}

const { leaf, proof } = this.validationModuleManager.getMerkleProof(
const { leaf, proof } = await this.validationModuleManager.getMerkleProof(
assertion,
Number(challenge),
);
Expand Down
2 changes: 1 addition & 1 deletion src/migration/pending-storage-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PendingStorageMigration extends BaseMigration {
const cachedData = await this.fileService.readFile(newDirectoryPath, true);
await this.fileService.removeFile(newDirectoryPath);
if (cachedData?.public?.assertion) {
const newDocumentName = calculateRoot(cachedData.public.assertion);
const newDocumentName = await calculateRoot(cachedData.public.assertion);
await this.fileService.writeContentsToFile(
newDirectoryPath,
newDocumentName,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/validation/implementation/merkle-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class MerkleValidation {
};
}

calculateRoot(assertion) {
async calculateRoot(assertion) {
return calculateRoot(assertion);
}

getMerkleProof(nquadsArray, challenge) {
async getMerkleProof(nquadsArray, challenge) {
return getMerkleProof(nquadsArray, challenge);
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/validation/validation-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ValidationModuleManager extends BaseModuleManager {
return 'validation';
}

calculateRoot(assertion) {
async calculateRoot(assertion) {
if (this.initialized) {
if (!assertion) {
throw new Error('Calculation failed: Assertion cannot be null or undefined.');
Expand All @@ -15,7 +15,7 @@ class ValidationModuleManager extends BaseModuleManager {
throw new Error('Validation module is not initialized.');
}

getMerkleProof(assertion, index) {
async getMerkleProof(assertion, index) {
if (this.initialized) {
if (!assertion) {
throw new Error('Get merkle proof failed: Assertion cannot be null or undefined.');
Expand Down
6 changes: 3 additions & 3 deletions src/service/validation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ValidationService {
async validateAssertion(assertionId, blockchain, assertion) {
this.logger.info(`Validating assertionId: ${assertionId}`);

this.validateAssertionId(assertion, assertionId);
await this.validateAssertionId(assertion, assertionId);
const blockchainAssertionData = await this.blockchainModuleManager.getAssertionData(
blockchain,
assertionId,
Expand Down Expand Up @@ -80,8 +80,8 @@ class ValidationService {
}
}

validateAssertionId(assertion, assertionId) {
const calculatedAssertionId = this.validationModuleManager.calculateRoot(assertion);
async validateAssertionId(assertion, assertionId) {
const calculatedAssertionId = await this.validationModuleManager.calculateRoot(assertion);

if (assertionId !== calculatedAssertionId) {
// todo after corrective component is implemented, update this logic
Expand Down
19 changes: 10 additions & 9 deletions test/unit/modules/validation/validation-module-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe.only('Validation module manager', async () => {
});

it('validate successful root hash calculation, expect to be matched', async () => {
const expectedRootHash = calculateRoot(assertion);
const calculatedRootHash = validationManager.calculateRoot(assertion);
const expectedRootHash = await calculateRoot(assertion);
const calculatedRootHash = await validationManager.calculateRoot(assertion);

assert(expect(calculatedRootHash).to.exist);
expect(calculatedRootHash).to.equal(expectedRootHash);
Expand All @@ -60,7 +60,7 @@ describe.only('Validation module manager', async () => {
validationManager.initialized = false;

try {
validationManager.calculateRoot(assertion);
await validationManager.calculateRoot(assertion);
} catch (error) {
expect(error.message).to.equal('Validation module is not initialized.');
}
Expand All @@ -76,7 +76,7 @@ describe.only('Validation module manager', async () => {
});

it('successful getting merkle proof hash', async () => {
const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0);
const calculatedMerkleHash = await validationManager.getMerkleProof(assertion, 0);

assert(expect(calculatedMerkleHash).to.exist);
expect(calculatedMerkleHash).to.be.instanceof(Object);
Expand All @@ -88,19 +88,20 @@ describe.only('Validation module manager', async () => {
validationManager.initialized = false;

try {
validationManager.getMerkleProof(assertion, 0);
await validationManager.getMerkleProof(assertion, 0);
} catch (error) {
expect(error.message).to.equal('Validation module is not initialized.');
}
});

it('failed merkle prof hash calculation when assertion is null or undefined', async () => {
invalidValues.forEach((value) => {
expect(() => validationManager.getMerkleProof(value, 0)).to.throw(
it('failed merkle proof hash calculation when assertion is null or undefined', async () => {
for (const value of invalidValues) {
// eslint-disable-next-line no-await-in-loop
expect(await validationManager.getMerkleProof(value, 0)).to.be.rejectedWith(
Error,
'Get merkle proof failed: Assertion cannot be null or undefined.',
);
});
}
});

it('validate getting function name', async () => {
Expand Down

0 comments on commit 4ccdd1f

Please sign in to comment.