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

Implement caching for parameters and identity storage contracts #2834

Merged
merged 11 commits into from
Dec 25, 2023
20 changes: 12 additions & 8 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,15 +589,19 @@ export const BLOCK_TIME_MILLIS = {

export const TRANSACTION_CONFIRMATIONS = 1;

export const CACHE_DATA_TYPES = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I get why would we need this

Copy link
Contributor

Choose a reason for hiding this comment

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

Still don't think it's needed, for the specific events we have predefined types in the ABI

e.g. for ParametersStorage it can't be anything else except Number, so I would just cast values based on the types in the ABI
image

NUMBER: 'number',
};

export const CACHED_FUNCTIONS = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also please move the cached values from the scoring function ?

ParametersStorage: [
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make it an object, with parameterName as key and type as value, then we don't need to iterate through all parameters every time.

If we remove types as I suggested in the comment above, we can just make it a Set with constant O(1) lookup

'r0',
'r1',
'r2',
'finalizationCommitsNumber',
'updateCommitWindowDuration',
'commitWindowDurationPerc',
'proofWindowDurationPerc',
'epochLength',
{ name: 'r0', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'r1', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'r2', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'finalizationCommitsNumber', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'updateCommitWindowDuration', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'commitWindowDurationPerc', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'proofWindowDurationPerc', type: CACHE_DATA_TYPES.NUMBER },
{ name: 'epochLength', type: CACHE_DATA_TYPES.NUMBER },
],
};
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 @@ -20,6 +20,7 @@ import {
FALLBACK_PROVIDER_QUORUM,
RPC_PROVIDER_STALL_TIMEOUT,
CACHED_FUNCTIONS,
CACHE_DATA_TYPES,
} from '../../../constants/constants.js';

const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -272,11 +273,28 @@ class Web3Service {
}

cacheParameter(parameterName, parameterValue) {
resultCache[parameterName] = parameterValue;
console.log('PARAMETER IME', parameterName);
const found = Object.values(CACHED_FUNCTIONS)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just add contractName as argument for the function?

.flat()
.find((item) => item.name === parameterName);
if (found) {
console.log('NADJENO?', found);
const { type } = found;

switch (type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Following the suggestion to remove types from constants, I would make it a separate function somewhere in the services or even utils for casting based on types of the event arguments in the ABI and cast it directly in the event handler instead of doing it here

case CACHE_DATA_TYPES.NUMBER:
resultCache[parameterName] = Number(parameterValue);
break;
default:
resultCache[parameterName] = parameterValue;
}
}
}

getCachedValue(parameterName) {
return resultCache[parameterName];
if (CACHED_FUNCTIONS.ParametersStorage.some((item) => item.name === parameterName)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just add contractName as argument for the function?

return resultCache[parameterName];
}
}

initializeContract(contractName, contractAddress) {
Expand Down Expand Up @@ -416,16 +434,13 @@ class Web3Service {

async callContractFunction(contractInstance, functionName, args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're using this function for identityId, old logic for getIdentityId function should be removed and we can add it to cached parameters since it's immutable

let result;
if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) {
result = this.getCachedValue(functionName);
}
result = this.getCachedValue(functionName);

while (!result) {
Copy link
Contributor

Choose a reason for hiding this comment

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

while loop here is redundant, we won't ever reiterate here

try {
// eslint-disable-next-line no-await-in-loop
result = await contractInstance[functionName](...args);
if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) {
this.cacheParameter(functionName, result);
}
this.cacheParameter(functionName, result);
} catch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why we don't call "handleError" in the catch block anymore ? This was previously used to switch RPCs in case the error was due to RPC being down.
If this is intended, then the while loop is not necessary and can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

We switched to a new provider implementation in the ethers.js that automatically hits different RPC after defined timeout, while loop probably can be removed as we're not retrying the call anyway (because of throwing new error)

const decodedErrorData = this._decodeErrorData(error, contractInstance.interface);

Expand Down