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

Fetching only finalized SAs #3403

Open
wants to merge 2 commits into
base: release/v6.5.2
Choose a base branch
from
Open
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 @@ -3,11 +3,11 @@ import { setTimeout as sleep } from 'timers/promises';
import Command from '../../command.js';
import {
CONTENT_ASSET_HASH_FUNCTION_ID,
ETHERS_BLOCK_TAGS,
EXPECTED_TRANSACTION_ERRORS,
GET_ASSERTION_IDS_MAX_RETRY_COUNT,
GET_ASSERTION_IDS_RETRY_DELAY_IN_SECONDS,
GET_LATEST_SERVICE_AGREEMENT_BATCH_SIZE,
GET_LATEST_SERVICE_AGREEMENT_EXCLUDE_LATEST_TOKEN_ID,
GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS,
SERVICE_AGREEMENT_SOURCES,
} from '../../../constants/constants.js';
Expand Down Expand Up @@ -60,9 +60,13 @@ class BlockchainGetLatestServiceAgreement extends Command {
);
let latestBlockchainTokenId;
try {
latestBlockchainTokenId =
Number(await this.blockchainModuleManager.getLatestTokenId(blockchain, contract)) -
GET_LATEST_SERVICE_AGREEMENT_EXCLUDE_LATEST_TOKEN_ID;
latestBlockchainTokenId = Number(
await this.blockchainModuleManager.getLatestTokenId(
blockchain,
contract,
ETHERS_BLOCK_TAGS.FINALIZED,
),
);
} catch (error) {
if (error.message.includes(EXPECTED_TRANSACTION_ERRORS.NO_MINTED_ASSETS)) {
this.logger.info(
Expand All @@ -81,7 +85,7 @@ class BlockchainGetLatestServiceAgreement extends Command {
(await this.repositoryModuleManager.getLatestServiceAgreementTokenId(blockchain)) ??
latestBlockchainTokenId;

if (latestBlockchainTokenId < latestDbTokenId) {
if (latestBlockchainTokenId <= latestDbTokenId) {
this.logger.debug(
`Get latest service agreement: No new agreements found on blockchain: ${blockchain}.`,
);
Expand All @@ -91,13 +95,6 @@ class BlockchainGetLatestServiceAgreement extends Command {
};
}

if (latestBlockchainTokenId < latestDbTokenId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate condition there is the same one right before that

this.logger.debug(
`Get latest service agreement: No new agreements found on blockchain: ${blockchain}.`,
);
return;
}

this.logger.debug(
`Get latest service agreement: Latest token id on chain: ${latestBlockchainTokenId}, latest token id in database: ${latestDbTokenId} on blockchain: ${blockchain}`,
);
Expand All @@ -121,17 +118,17 @@ class BlockchainGetLatestServiceAgreement extends Command {
await this.repositoryModuleManager.bulkCreateServiceAgreementRecords(
missingAgreements.filter((agreement) => agreement != null),
);
tokenIdDifference -= getAgreementDataPromise.length;
getAgreementDataPromise = [];
tokenIdDifference -= GET_LATEST_SERVICE_AGREEMENT_BATCH_SIZE;
}
}
if (latestBlockchainTokenId - latestDbTokenId > 0) {
this.logger.debug(
`Get latest service agreement: Successfully fetched ${
latestBlockchainTokenId - latestDbTokenId
} on blockchain: ${blockchain}`,
);
}

this.logger.debug(
`Get latest service agreement: Successfully fetched ${
latestBlockchainTokenId - latestDbTokenId
} on blockchain: ${blockchain}`,
);

return {
contract,
lastProcessedTokenId: latestBlockchainTokenId,
Expand Down Expand Up @@ -164,6 +161,8 @@ class BlockchainGetLatestServiceAgreement extends Command {
blockchain,
contract,
tokenId,
0,
ETHERS_BLOCK_TAGS.FINALIZED,
);
retryCount += 1;
await sleep(GET_ASSERTION_IDS_RETRY_DELAY_IN_SECONDS * 1000);
Expand All @@ -185,6 +184,7 @@ class BlockchainGetLatestServiceAgreement extends Command {
const agreementData = await this.blockchainModuleManager.getAgreementData(
blockchain,
agreementId,
ETHERS_BLOCK_TAGS.FINALIZED,
);

if (!agreementData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class HandleStoreRequestCommand extends HandleProtocolMessageCommand {
tokenId,
);
const stateIndex = assertionIds.length - 1;

if (assertionId !== assertionIds[stateIndex]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already do this in validation command

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't

throw new Error(
`Provided assertionId: ${assertionId} doesn't match latest assertion id from the blockchain: ${assertionIds[stateIndex]} for KA with blockchain: ${blockchain}; contract: ${contract}, token id: ${tokenId}.`,
);
}

const { assertion } = await this.operationIdService.getCachedOperationIdData(operationId);
await this.validationService.validateAssertion(assertionId, blockchain, assertion);

Expand Down
10 changes: 8 additions & 2 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export const COMMITS_DELAY_BETWEEN_NODES_IN_BLOCKS = 5;

export const TRANSACTION_POLLING_TIMEOUT_MILLIS = 300 * 1000;

export const ETHERS_BLOCK_TAGS = {
EARLIEST: 'earliest',
FINALIZED: 'finalized',
LATEST: 'latest',
PENDING: 'pending',
SAFE: 'safe',
};

export const SOLIDITY_ERROR_STRING_PREFIX = '0x08c379a0';

export const SOLIDITY_PANIC_CODE_PREFIX = '0x4e487b71';
Expand Down Expand Up @@ -566,8 +574,6 @@ export const GET_ASSERTION_IDS_MAX_RETRY_COUNT = 5;

export const GET_ASSERTION_IDS_RETRY_DELAY_IN_SECONDS = 2;

export const GET_LATEST_SERVICE_AGREEMENT_EXCLUDE_LATEST_TOKEN_ID = 1;

/**
* @constant {object} HTTP_API_ROUTES -
* HTTP API Routes with parameters
Expand Down
19 changes: 15 additions & 4 deletions src/modules/blockchain/blockchain-module-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ETHERS_BLOCK_TAGS } from '../../constants/constants.js';
import BaseModuleManager from '../base-module-manager.js';

class BlockchainModuleManager extends BaseModuleManager {
Expand Down Expand Up @@ -114,20 +115,27 @@ class BlockchainModuleManager extends BaseModuleManager {
]);
}

async getLatestTokenId(blockchain, assetContractAddress) {
async getLatestTokenId(blockchain, assetContractAddress, blockTag = ETHERS_BLOCK_TAGS.LATEST) {
return this.callImplementationFunction(blockchain, 'getLatestTokenId', [
assetContractAddress,
blockTag,
]);
}

getAssetStorageContractAddresses(blockchain) {
return this.callImplementationFunction(blockchain, 'getAssetStorageContractAddresses');
}

async getAssertionIds(blockchain, assetContractAddress, tokenId) {
async getAssertionIds(
blockchain,
assetContractAddress,
tokenId,
blockTag = ETHERS_BLOCK_TAGS.LATEST,
) {
return this.callImplementationFunction(blockchain, 'getAssertionIds', [
assetContractAddress,
tokenId,
blockTag,
]);
}

Expand Down Expand Up @@ -241,8 +249,11 @@ class BlockchainModuleManager extends BaseModuleManager {
]);
}

async getAgreementData(blockchain, agreementId) {
return this.callImplementationFunction(blockchain, 'getAgreementData', [agreementId]);
async getAgreementData(blockchain, agreementId, blockTag = ETHERS_BLOCK_TAGS.LATEST) {
return this.callImplementationFunction(blockchain, 'getAgreementData', [
agreementId,
blockTag,
]);
}

async getAssertionSize(blockchain, assertionid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ class OtParachainService extends Web3Service {
if (!isRpcError) throw error;
}

async getLatestTokenId(assetContractAddress) {
return this.provider.getStorageAt(assetContractAddress.toString().toLowerCase(), 7);
async getLatestTokenId(assetContractAddress, blockTag) {
return this.provider.getStorageAt(
assetContractAddress.toString().toLowerCase(),
7,
blockTag,
);
}

async restartParachainProvider() {
Expand Down
31 changes: 23 additions & 8 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { setTimeout as sleep } from 'timers/promises';
import { createRequire } from 'module';

import {
ETHERS_BLOCK_TAGS,
SOLIDITY_ERROR_STRING_PREFIX,
SOLIDITY_PANIC_CODE_PREFIX,
SOLIDITY_PANIC_REASONS,
Expand Down Expand Up @@ -602,15 +603,21 @@ class Web3Service {
}
}

async callContractFunction(contractInstance, functionName, args, contractName = null) {
async callContractFunction(
contractInstance,
functionName,
args,
contractName = null,
blockTag = ETHERS_BLOCK_TAGS.LATEST,
) {
const maxNumberOfRetries = 3;
const retryDelayInSec = 12;
let retryCount = 0;
let result = this.getContractCallCache(contractName, functionName);
try {
if (!result) {
while (retryCount < maxNumberOfRetries) {
result = await contractInstance[functionName](...args);
result = await contractInstance[functionName](...args, { blockTag });
const resultIsValid = Web3ServiceValidator.validateResult(
functionName,
contractName,
Expand Down Expand Up @@ -1079,7 +1086,7 @@ class Web3Service {
]);
}

async getLatestTokenId(assetContractAddress) {
async getLatestTokenId(assetContractAddress, blockTag) {
const assetStorageContractInstance =
this.assetStorageContracts[assetContractAddress.toString().toLowerCase()];
if (!assetStorageContractInstance)
Expand All @@ -1089,6 +1096,8 @@ class Web3Service {
assetStorageContractInstance,
'lastTokenId',
[],
null,
blockTag,
);
return lastTokenId;
}
Expand All @@ -1097,15 +1106,19 @@ class Web3Service {
return Object.keys(this.assetStorageContracts);
}

async getAssertionIds(assetContractAddress, tokenId) {
async getAssertionIds(assetContractAddress, tokenId, blockTag) {
const assetStorageContractInstance =
this.assetStorageContracts[assetContractAddress.toString().toLowerCase()];
if (!assetStorageContractInstance)
throw new Error('Unknown asset storage contract address');

return this.callContractFunction(assetStorageContractInstance, 'getAssertionIds', [
tokenId,
]);
return this.callContractFunction(
assetStorageContractInstance,
'getAssertionIds',
[tokenId],
null,
blockTag,
);
}

async getKnowledgeAssetOwner(assetContractAddress, tokenId) {
Expand All @@ -1125,11 +1138,13 @@ class Web3Service {
);
}

async getAgreementData(agreementId) {
async getAgreementData(agreementId, blockTag) {
const result = await this.callContractFunction(
this.ServiceAgreementStorageProxyContract,
'getAgreementData',
[agreementId],
null,
blockTag,
);
if (!result) {
return null;
Expand Down
Loading