From a8c7806e63d09c39eb6c08ebfeaf9913fd865939 Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Mon, 11 Dec 2023 14:12:01 +0100 Subject: [PATCH 01/10] Implementation of caching and parameter storage handler --- src/constants/constants.js | 2 + .../blockchain/blockchain-module-manager.js | 8 +++ .../blockchain/implementation/web3-service.js | 71 +++++++++++++------ .../blockchain-event-listener-service.js | 19 +++++ 4 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/constants/constants.js b/src/constants/constants.js index ea9f47e221..198965622a 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -554,6 +554,7 @@ export const CONTRACTS = { HUB_CONTRACT: 'HubContract', COMMIT_MANAGER_V1_U1_CONTRACT: 'CommitManagerV1U1Contract', SERVICE_AGREEMENT_V1_CONTRACT: 'ServiceAgreementV1Contract', + PARAMETERS_STORAGE_CONTRACT: 'ParametersStorageContract', }; export const CONTRACT_EVENTS = { @@ -563,6 +564,7 @@ export const CONTRACT_EVENTS = { PROFILE: ['AskUpdated'], COMMIT_MANAGER_V1: ['StateFinalized'], SERVICE_AGREEMENT_V1: ['ServiceAgreementV1Extended', 'ServiceAgreementV1Terminated'], + PARAMETERS_STORAGE: ['ParameterChanged'], }; export const NODE_ENVIRONMENTS = { diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index d7b8b98c19..1eddda3c16 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -26,6 +26,14 @@ class BlockchainModuleManager extends BaseModuleManager { ]); } + cacheParameter(blockchain, parameterName, parameterValue) { + console.log('CacheParametar module manager'); + return this.callImplementationFunction(blockchain, 'cacheParameter', [ + parameterName, + parameterValue, + ]); + } + getPrivateKey(blockchain) { return this.callImplementationFunction(blockchain, 'getPrivateKey'); } diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index dd0472aee6..e1bc8ef4b2 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -51,6 +51,17 @@ const ABIs = { const SCORING_FUNCTIONS = { 1: 'Log2PLDSF', }; +const cachedFunctionsList = [ + 'r0', + 'r1', + 'r2', + 'finalizationCommitsNumber', + 'updateCommitWindowDuration', + 'commitWindowDurationPerc', + 'proofWindowDurationPerc', + 'epochLength', +]; +const resultCache = {}; class Web3Service { async initialize(config, logger) { @@ -269,6 +280,11 @@ class Web3Service { } } + cacheParameter(parameterName, parameterValue) { + console.log('cache web3 service'); + resultCache[parameterName] = parameterValue; + } + initializeContract(contractName, contractAddress) { if (ABIs[contractName] != null) { this[`${contractName}Contract`] = new ethers.Contract( @@ -406,30 +422,42 @@ class Web3Service { async callContractFunction(contractInstance, functionName, args) { let result; - while (result === undefined) { - try { - // eslint-disable-next-line no-await-in-loop - result = await contractInstance[functionName](...args); - } catch (error) { - const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); - - const functionFragment = contractInstance.interface.getFunction( - error.transaction.data.slice(0, 10), - ); - const inputs = functionFragment.inputs - .map((input, i) => { - const argName = input.name; - const argValue = this._formatArgument(args[i]); - return `${argName}=${argValue}`; - }) - .join(', '); + if (cachedFunctionsList.includes(functionName) && resultCache[functionName] !== undefined) { + console.log('cached!'); + result = resultCache[functionName]; + console.log(result); + } else { + while (result === undefined) { + try { + // eslint-disable-next-line no-await-in-loop + result = await contractInstance[functionName](...args); + if (cachedFunctionsList.includes(functionName)) { + resultCache[functionName] = result; + console.log(`Cached result for ${functionName}:`, result); + } + } catch (error) { + const decodedErrorData = this._decodeErrorData( + error, + contractInstance.interface, + ); - throw new Error( - `Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, - ); + const functionFragment = contractInstance.interface.getFunction( + error.transaction.data.slice(0, 10), + ); + const inputs = functionFragment.inputs + .map((input, i) => { + const argName = input.name; + const argValue = this._formatArgument(args[i]); + return `${argName}=${argValue}`; + }) + .join(', '); + + throw new Error( + `Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, + ); + } } } - return result; } @@ -904,6 +932,7 @@ class Web3Service { score: commit.score, })); } + // OVO OVDE TREBA DODATI MEMORY CACHE NAJPAMETNIJE URADITI U CALL CONTRACT FFFF async getR2() { const r2 = await this.callContractFunction(this.ParametersStorageContract, 'r2', []); diff --git a/src/service/blockchain-event-listener-service.js b/src/service/blockchain-event-listener-service.js index ea8f9611e4..5b4fb68b08 100644 --- a/src/service/blockchain-event-listener-service.js +++ b/src/service/blockchain-event-listener-service.js @@ -79,6 +79,12 @@ class BlockchainEventListenerService { currentBlock, CONTRACT_EVENTS.SERVICE_AGREEMENT_V1, ), + this.getContractEvents( + blockchainId, + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, + currentBlock, + CONTRACT_EVENTS.PARAMETERS_STORAGE, + ), ]; if (!devEnvironment) { @@ -227,6 +233,19 @@ class BlockchainEventListenerService { } } + async handleParameterChangedEvents(blockEvents) { + console.log('handler'); + for (const event of blockEvents) { + const { parameterName, parameterValue } = JSON.parse(event.data); + console.log('Handler', parameterName, parameterValue); + this.blockchainModuleManager.cacheParameter( + event.blockchainId, + parameterName, + parameterValue, + ); + } + } + handleNewContractEvents(blockEvents) { for (const event of blockEvents) { const { contractName, newContractAddress } = JSON.parse(event.data); From d0870dbeff7b9cb1d546e3416ffcf5199f7b615e Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Mon, 11 Dec 2023 15:02:38 +0100 Subject: [PATCH 02/10] caching code improvement and removing unnecessary logs --- src/constants/constants.js | 13 +++++++++++ .../blockchain/blockchain-module-manager.js | 1 - .../blockchain/implementation/web3-service.js | 23 +++++-------------- .../blockchain-event-listener-service.js | 2 -- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/constants/constants.js b/src/constants/constants.js index 198965622a..d2791b5fdc 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -588,3 +588,16 @@ export const BLOCK_TIME_MILLIS = { }; export const TRANSACTION_CONFIRMATIONS = 1; + +export const CACHED_FUNCTIONS = { + ParametersStorage: [ + 'r0', + 'r1', + 'r2', + 'finalizationCommitsNumber', + 'updateCommitWindowDuration', + 'commitWindowDurationPerc', + 'proofWindowDurationPerc', + 'epochLength', + ], +}; diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index 1eddda3c16..4398c9f5b9 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -27,7 +27,6 @@ class BlockchainModuleManager extends BaseModuleManager { } cacheParameter(blockchain, parameterName, parameterValue) { - console.log('CacheParametar module manager'); return this.callImplementationFunction(blockchain, 'cacheParameter', [ parameterName, parameterValue, diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index e1bc8ef4b2..747daa1b75 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -19,6 +19,7 @@ import { HTTP_RPC_PROVIDER_PRIORITY, FALLBACK_PROVIDER_QUORUM, RPC_PROVIDER_STALL_TIMEOUT, + CACHED_FUNCTIONS, } from '../../../constants/constants.js'; const require = createRequire(import.meta.url); @@ -51,16 +52,6 @@ const ABIs = { const SCORING_FUNCTIONS = { 1: 'Log2PLDSF', }; -const cachedFunctionsList = [ - 'r0', - 'r1', - 'r2', - 'finalizationCommitsNumber', - 'updateCommitWindowDuration', - 'commitWindowDurationPerc', - 'proofWindowDurationPerc', - 'epochLength', -]; const resultCache = {}; class Web3Service { @@ -281,7 +272,6 @@ class Web3Service { } cacheParameter(parameterName, parameterValue) { - console.log('cache web3 service'); resultCache[parameterName] = parameterValue; } @@ -422,18 +412,18 @@ class Web3Service { async callContractFunction(contractInstance, functionName, args) { let result; - if (cachedFunctionsList.includes(functionName) && resultCache[functionName] !== undefined) { - console.log('cached!'); + if ( + CACHED_FUNCTIONS.ParametersStorage.includes(functionName) && + resultCache[functionName] !== undefined + ) { result = resultCache[functionName]; - console.log(result); } else { while (result === undefined) { try { // eslint-disable-next-line no-await-in-loop result = await contractInstance[functionName](...args); - if (cachedFunctionsList.includes(functionName)) { + if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { resultCache[functionName] = result; - console.log(`Cached result for ${functionName}:`, result); } } catch (error) { const decodedErrorData = this._decodeErrorData( @@ -932,7 +922,6 @@ class Web3Service { score: commit.score, })); } - // OVO OVDE TREBA DODATI MEMORY CACHE NAJPAMETNIJE URADITI U CALL CONTRACT FFFF async getR2() { const r2 = await this.callContractFunction(this.ParametersStorageContract, 'r2', []); diff --git a/src/service/blockchain-event-listener-service.js b/src/service/blockchain-event-listener-service.js index 5b4fb68b08..1f7e65911b 100644 --- a/src/service/blockchain-event-listener-service.js +++ b/src/service/blockchain-event-listener-service.js @@ -234,10 +234,8 @@ class BlockchainEventListenerService { } async handleParameterChangedEvents(blockEvents) { - console.log('handler'); for (const event of blockEvents) { const { parameterName, parameterValue } = JSON.parse(event.data); - console.log('Handler', parameterName, parameterValue); this.blockchainModuleManager.cacheParameter( event.blockchainId, parameterName, From ade10907f8c13120decd0307d169442122618b8d Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Mon, 11 Dec 2023 15:39:48 +0100 Subject: [PATCH 03/10] code improvement --- src/modules/blockchain/implementation/web3-service.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 747daa1b75..5bcd4edd05 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -275,6 +275,10 @@ class Web3Service { resultCache[parameterName] = parameterValue; } + getCachedValue(parameterName) { + return resultCache[parameterName]; + } + initializeContract(contractName, contractAddress) { if (ABIs[contractName] != null) { this[`${contractName}Contract`] = new ethers.Contract( @@ -412,11 +416,8 @@ class Web3Service { async callContractFunction(contractInstance, functionName, args) { let result; - if ( - CACHED_FUNCTIONS.ParametersStorage.includes(functionName) && - resultCache[functionName] !== undefined - ) { - result = resultCache[functionName]; + if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { + result = this.getCachedValue(functionName); } else { while (result === undefined) { try { From 317684504b015f29fe04a474c98b650f44920a8d Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Mon, 11 Dec 2023 16:00:19 +0100 Subject: [PATCH 04/10] undefined bugfix --- .../blockchain/implementation/web3-service.js | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 5bcd4edd05..58adbd9b9d 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -418,37 +418,34 @@ class Web3Service { let result; if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { result = this.getCachedValue(functionName); - } else { - while (result === undefined) { - try { - // eslint-disable-next-line no-await-in-loop - result = await contractInstance[functionName](...args); - if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { - resultCache[functionName] = result; - } - } catch (error) { - const decodedErrorData = this._decodeErrorData( - error, - contractInstance.interface, - ); - - const functionFragment = contractInstance.interface.getFunction( - error.transaction.data.slice(0, 10), - ); - const inputs = functionFragment.inputs - .map((input, i) => { - const argName = input.name; - const argValue = this._formatArgument(args[i]); - return `${argName}=${argValue}`; - }) - .join(', '); - - throw new Error( - `Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, - ); + } + while (!result) { + try { + // eslint-disable-next-line no-await-in-loop + result = await contractInstance[functionName](...args); + if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { + this.cacheParameter(functionName, result); } + } catch (error) { + const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); + + const functionFragment = contractInstance.interface.getFunction( + error.transaction.data.slice(0, 10), + ); + const inputs = functionFragment.inputs + .map((input, i) => { + const argName = input.name; + const argValue = this._formatArgument(args[i]); + return `${argName}=${argValue}`; + }) + .join(', '); + + throw new Error( + `Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, + ); } } + return result; } From 829599f9fc74352f783a85ccc12d4158762e6855 Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Wed, 13 Dec 2023 12:27:31 +0100 Subject: [PATCH 05/10] updated cache to be scalable --- src/constants/constants.js | 20 +++++++----- .../blockchain/implementation/web3-service.js | 31 ++++++++++++++----- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/constants/constants.js b/src/constants/constants.js index d2791b5fdc..95882baaac 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -589,15 +589,19 @@ export const BLOCK_TIME_MILLIS = { export const TRANSACTION_CONFIRMATIONS = 1; +export const CACHE_DATA_TYPES = { + NUMBER: 'number', +}; + export const CACHED_FUNCTIONS = { ParametersStorage: [ - '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 }, ], }; diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 58adbd9b9d..ed442d068e 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -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); @@ -272,11 +273,28 @@ class Web3Service { } cacheParameter(parameterName, parameterValue) { - resultCache[parameterName] = parameterValue; + console.log('PARAMETER IME', parameterName); + const found = Object.values(CACHED_FUNCTIONS) + .flat() + .find((item) => item.name === parameterName); + if (found) { + console.log('NADJENO?', found); + const { type } = found; + + switch (type) { + 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)) { + return resultCache[parameterName]; + } } initializeContract(contractName, contractAddress) { @@ -416,16 +434,13 @@ class Web3Service { async callContractFunction(contractInstance, functionName, args) { let result; - if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { - result = this.getCachedValue(functionName); - } + result = this.getCachedValue(functionName); + while (!result) { 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) { const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); From 4ac849d34573d840d6dd6ceb22b282bbc76b5623 Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Wed, 13 Dec 2023 12:28:14 +0100 Subject: [PATCH 06/10] updated cache to be scalable --- src/modules/blockchain/implementation/web3-service.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index ed442d068e..7f74401d31 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -273,12 +273,10 @@ class Web3Service { } cacheParameter(parameterName, parameterValue) { - console.log('PARAMETER IME', parameterName); const found = Object.values(CACHED_FUNCTIONS) .flat() .find((item) => item.name === parameterName); if (found) { - console.log('NADJENO?', found); const { type } = found; switch (type) { From d44ed6dad8d0020ef4ec8e213401165cde532b68 Mon Sep 17 00:00:00 2001 From: Milos Stanisavljevic Date: Thu, 14 Dec 2023 14:08:34 +0100 Subject: [PATCH 07/10] added contractName as parameter in caching --- .../blockchain/implementation/web3-service.js | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 7f74401d31..703ae3a68e 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -272,13 +272,12 @@ class Web3Service { } } - cacheParameter(parameterName, parameterValue) { - const found = Object.values(CACHED_FUNCTIONS) - .flat() - .find((item) => item.name === parameterName); + cacheParameter(contractName, parameterName, parameterValue) { + const found = + CACHED_FUNCTIONS[contractName] && + CACHED_FUNCTIONS[contractName].find((item) => item.name === parameterName); if (found) { const { type } = found; - switch (type) { case CACHE_DATA_TYPES.NUMBER: resultCache[parameterName] = Number(parameterValue); @@ -289,8 +288,11 @@ class Web3Service { } } - getCachedValue(parameterName) { - if (CACHED_FUNCTIONS.ParametersStorage.some((item) => item.name === parameterName)) { + getCachedValue(contractName, parameterName) { + if ( + CACHED_FUNCTIONS[contractName] && + CACHED_FUNCTIONS[contractName].some((item) => item.name === parameterName) + ) { return resultCache[parameterName]; } } @@ -430,15 +432,15 @@ class Web3Service { } } - async callContractFunction(contractInstance, functionName, args) { + async callContractFunction(contractInstance, functionName, args, contractName = null) { let result; - result = this.getCachedValue(functionName); + result = this.getCachedValue(contractName, functionName); while (!result) { try { // eslint-disable-next-line no-await-in-loop result = await contractInstance[functionName](...args); - this.cacheParameter(functionName, result); + this.cacheParameter(contractName, functionName, result); } catch (error) { const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); @@ -935,17 +937,32 @@ class Web3Service { } async getR2() { - const r2 = await this.callContractFunction(this.ParametersStorageContract, 'r2', []); + const r2 = await this.callContractFunction( + this.ParametersStorageContract, + 'r2', + [], + 'ParametersStorage', + ); return r2; } async getR1() { - const r1 = await this.callContractFunction(this.ParametersStorageContract, 'r1', []); + const r1 = await this.callContractFunction( + this.ParametersStorageContract, + 'r1', + [], + 'ParametersStorage', + ); return r1; } async getR0() { - const r0 = await this.callContractFunction(this.ParametersStorageContract, 'r0', []); + const r0 = await this.callContractFunction( + this.ParametersStorageContract, + 'r0', + [], + 'ParametersStorage', + ); return r0; } @@ -954,6 +971,7 @@ class Web3Service { this.ParametersStorageContract, 'finalizationCommitsNumber', [], + 'ParametersStorage', ); return finalizationCommitsNumber; } @@ -1125,6 +1143,7 @@ class Web3Service { this.ParametersStorageContract, 'updateCommitWindowDuration', [], + 'ParametersStorage', ); return Number(commitWindowDurationPerc); } @@ -1134,6 +1153,7 @@ class Web3Service { this.ParametersStorageContract, 'commitWindowDurationPerc', [], + 'ParametersStorage', ); return Number(commitWindowDurationPerc); } @@ -1143,6 +1163,7 @@ class Web3Service { this.ParametersStorageContract, 'proofWindowDurationPerc', [], + 'ParametersStorage', ); } @@ -1151,6 +1172,7 @@ class Web3Service { this.ParametersStorageContract, 'epochLength', [], + 'ParametersStorage', ); return Number(epochLength); } From 76ca35209794b323b3c16ee09e55741a837d0599 Mon Sep 17 00:00:00 2001 From: Djordje Kovacevic Date: Mon, 25 Dec 2023 12:10:40 +0100 Subject: [PATCH 08/10] Updated Caching of contract calls --- src/constants/constants.js | 31 ++-- .../blockchain/blockchain-module-manager.js | 42 +----- .../blockchain/implementation/web3-service.js | 133 ++++++------------ .../blockchain-event-listener-service.js | 3 +- 4 files changed, 68 insertions(+), 141 deletions(-) diff --git a/src/constants/constants.js b/src/constants/constants.js index 95882baaac..e624863323 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -555,6 +555,7 @@ export const CONTRACTS = { COMMIT_MANAGER_V1_U1_CONTRACT: 'CommitManagerV1U1Contract', SERVICE_AGREEMENT_V1_CONTRACT: 'ServiceAgreementV1Contract', PARAMETERS_STORAGE_CONTRACT: 'ParametersStorageContract', + IDENTITY_STORAGE_CONTRACT: 'IdentityStorageContract', }; export const CONTRACT_EVENTS = { @@ -593,15 +594,25 @@ export const CACHE_DATA_TYPES = { NUMBER: 'number', }; +/** + * CACHED_FUNCTIONS: + * ContractName: { + * functionName: returnType + * } + * @type {{IdentityStorageContract: [{name: string, type: string}], ParametersStorageContract: *}} + */ export const CACHED_FUNCTIONS = { - ParametersStorage: [ - { 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 }, - ], + ParametersStorageContract: { + r0: CACHE_DATA_TYPES.NUMBER, + r1: CACHE_DATA_TYPES.NUMBER, + r2: CACHE_DATA_TYPES.NUMBER, + finalizationCommitsNumber: CACHE_DATA_TYPES.NUMBER, + updateCommitWindowDuration: CACHE_DATA_TYPES.NUMBER, + commitWindowDurationPerc: CACHE_DATA_TYPES.NUMBER, + proofWindowDurationPerc: CACHE_DATA_TYPES.NUMBER, + epochLength: CACHE_DATA_TYPES.NUMBER, + }, + IdentityStorageContract: { + getIndentityId: CACHE_DATA_TYPES.NUMBER, + }, }; diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index 4398c9f5b9..dfce41cee5 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -26,10 +26,11 @@ class BlockchainModuleManager extends BaseModuleManager { ]); } - cacheParameter(blockchain, parameterName, parameterValue) { - return this.callImplementationFunction(blockchain, 'cacheParameter', [ - parameterName, - parameterValue, + setContractCallCache(blockchain, contractName, functionName, value) { + return this.callImplementationFunction(blockchain, 'setContractCallCache', [ + contractName, + functionName, + value, ]); } @@ -45,20 +46,12 @@ class BlockchainModuleManager extends BaseModuleManager { return this.callImplementationFunction(blockchain, 'getManagementKey'); } - async isHubContract(blockchain, contractAddress) { - return this.callImplementationFunction(blockchain, 'isHubContract', [contractAddress]); - } - async isAssetStorageContract(blockchain, contractAddress) { return this.callImplementationFunction(blockchain, 'isAssetStorageContract', [ contractAddress, ]); } - async getNodeStake(blockchain, identityId) { - return this.callImplementationFunction(blockchain, 'getNodeStake', [identityId]); - } - async getBlockNumber(blockchain) { return this.callImplementationFunction(blockchain, 'getBlockNumber'); } @@ -119,13 +112,6 @@ class BlockchainModuleManager extends BaseModuleManager { ]); } - async getAssertionIdsLength(blockchain, assetContractAddress, tokenId) { - return this.callImplementationFunction(blockchain, 'getAssertionIdsLength', [ - assetContractAddress, - tokenId, - ]); - } - async getKnowledgeAssetOwner(blockchain, assetContractAddress, tokenId) { return this.callImplementationFunction(blockchain, 'getKnowledgeAssetOwner', [ assetContractAddress, @@ -137,10 +123,6 @@ class BlockchainModuleManager extends BaseModuleManager { return this.callImplementationFunction(blockchain, 'getUnfinalizedState', [tokenId]); } - async getAssertionIssuer(blockchain, assertionId) { - return this.callImplementationFunction(blockchain, 'getAssertionIssuer', [assertionId]); - } - async getShardingTableHead(blockchain) { return this.callImplementationFunction(blockchain, 'getShardingTableHead'); } @@ -380,20 +362,6 @@ class BlockchainModuleManager extends BaseModuleManager { return this.callImplementationFunction(blockchain, 'isHashFunction', [hashFunctionId]); } - async isScoreFunction(blockchain, scoreFunctionId) { - return this.callImplementationFunction(blockchain, 'isScoreFunction', [scoreFunctionId]); - } - - async callScoreFunction(blockchain, scoreFunctionId, hashFunctionId, peerId, keyword, stake) { - return this.callImplementationFunction(blockchain, 'callScoreFunction', [ - scoreFunctionId, - hashFunctionId, - peerId, - keyword, - stake, - ]); - } - async getLog2PLDSFParams(blockchain) { return this.callImplementationFunction(blockchain, 'getLog2PLDSFParams'); } diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 703ae3a68e..d9eaf63616 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -21,6 +21,7 @@ import { RPC_PROVIDER_STALL_TIMEOUT, CACHED_FUNCTIONS, CACHE_DATA_TYPES, + CONTRACTS, } from '../../../constants/constants.js'; const require = createRequire(import.meta.url); @@ -53,7 +54,7 @@ const ABIs = { const SCORING_FUNCTIONS = { 1: 'Log2PLDSF', }; -const resultCache = {}; +const contractCallCache = {}; class Web3Service { async initialize(config, logger) { @@ -272,28 +273,25 @@ class Web3Service { } } - cacheParameter(contractName, parameterName, parameterValue) { - const found = - CACHED_FUNCTIONS[contractName] && - CACHED_FUNCTIONS[contractName].find((item) => item.name === parameterName); - if (found) { - const { type } = found; + setContractCallCache(contractName, functionName, value) { + if (CACHED_FUNCTIONS[contractName]?.[functionName]) { + const type = CACHED_FUNCTIONS[contractName][functionName]; + if (!contractCallCache[contractName]) { + contractCallCache[contractName] = {}; + } switch (type) { case CACHE_DATA_TYPES.NUMBER: - resultCache[parameterName] = Number(parameterValue); + contractCallCache[contractName][functionName] = Number(value); break; default: - resultCache[parameterName] = parameterValue; + contractCallCache[contractName][functionName] = value; } } } - getCachedValue(contractName, parameterName) { - if ( - CACHED_FUNCTIONS[contractName] && - CACHED_FUNCTIONS[contractName].some((item) => item.name === parameterName) - ) { - return resultCache[parameterName]; + getContractCallCache(contractName, parameterName) { + if (CACHED_FUNCTIONS[contractName]?.[parameterName]) { + return contractCallCache[contractName][parameterName]; } } @@ -360,16 +358,13 @@ class Web3Service { } async getIdentityId() { - if (this.config.identityId) { - return this.config.identityId; - } const identityId = await this.callContractFunction( this.IdentityStorageContract, 'getIdentityId', [this.getPublicKey()], + CONTRACTS.IDENTITY_STORAGE_CONTRACT, ); - this.config.identityId = Number(identityId); - return this.config.identityId; + return Number(identityId); } async identityIdExists() { @@ -433,34 +428,29 @@ class Web3Service { } async callContractFunction(contractInstance, functionName, args, contractName = null) { - let result; - result = this.getCachedValue(contractName, functionName); - - while (!result) { - try { + let result = this.getContractCallCache(contractName, functionName); + try { + if (!result) { // eslint-disable-next-line no-await-in-loop result = await contractInstance[functionName](...args); - this.cacheParameter(contractName, functionName, result); - } catch (error) { - const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); + this.setContractCallCache(contractName, functionName, result); + } + } catch (error) { + const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); - const functionFragment = contractInstance.interface.getFunction( - error.transaction.data.slice(0, 10), - ); - const inputs = functionFragment.inputs - .map((input, i) => { - const argName = input.name; - const argValue = this._formatArgument(args[i]); - return `${argName}=${argValue}`; - }) - .join(', '); + const functionFragment = contractInstance.interface.getFunction( + error.transaction.data.slice(0, 10), + ); + const inputs = functionFragment.inputs + .map((input, i) => { + const argName = input.name; + const argValue = this._formatArgument(args[i]); + return `${argName}=${argValue}`; + }) + .join(', '); - throw new Error( - `Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, - ); - } + throw new Error(`Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`); } - return result; } @@ -757,22 +747,12 @@ class Web3Service { return timestamp < timestampThirtyDaysInPast; } - async isHubContract(contractAddress) { - return this.callContractFunction(this.HubContract, 'isContract(address)', [ - contractAddress, - ]); - } - async isAssetStorageContract(contractAddress) { return this.callContractFunction(this.HubContract, 'isAssetStorage(address)', [ contractAddress, ]); } - async getNodeStake(identityId) { - return this.callContractFunction(this.StakingStorageContract, 'totalStakes', [identityId]); - } - async getAssertionIdByIndex(assetContractAddress, tokenId, index) { const assetStorageContractInstance = this.assetStorageContracts[assetContractAddress.toLowerCase()]; @@ -815,17 +795,6 @@ class Web3Service { ]); } - async getAssertionIdsLength(assetContractAddress, tokenId) { - const assetStorageContractInstance = - this.assetStorageContracts[assetContractAddress.toString().toLowerCase()]; - if (!assetStorageContractInstance) - throw new Error('Unknown asset storage contract address'); - - return this.callContractFunction(assetStorageContractInstance, 'getAssertionIdsLength', [ - tokenId, - ]); - } - async getKnowledgeAssetOwner(assetContractAddress, tokenId) { const assetStorageContractInstance = this.assetStorageContracts[assetContractAddress.toString().toLowerCase()]; @@ -843,12 +812,6 @@ class Web3Service { ); } - async getAssertionIssuer(assertionId) { - return this.callContractFunction(this.AssertionStorageContract, 'getAssertionIssuer', [ - assertionId, - ]); - } - async getAgreementData(agreementId) { const result = await this.callContractFunction( this.ServiceAgreementStorageProxyContract, @@ -941,7 +904,7 @@ class Web3Service { this.ParametersStorageContract, 'r2', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return r2; } @@ -951,7 +914,7 @@ class Web3Service { this.ParametersStorageContract, 'r1', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return r1; } @@ -961,7 +924,7 @@ class Web3Service { this.ParametersStorageContract, 'r0', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return r0; } @@ -971,7 +934,7 @@ class Web3Service { this.ParametersStorageContract, 'finalizationCommitsNumber', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return finalizationCommitsNumber; } @@ -1153,7 +1116,7 @@ class Web3Service { this.ParametersStorageContract, 'commitWindowDurationPerc', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return Number(commitWindowDurationPerc); } @@ -1163,7 +1126,7 @@ class Web3Service { this.ParametersStorageContract, 'proofWindowDurationPerc', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); } @@ -1172,7 +1135,7 @@ class Web3Service { this.ParametersStorageContract, 'epochLength', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return Number(epochLength); } @@ -1183,22 +1146,6 @@ class Web3Service { ]); } - async isScoreFunction(scoreFunctionId) { - return this.callContractFunction(this.ScoringProxyContract, 'isScoreFunction(uint8)', [ - scoreFunctionId, - ]); - } - - async callScoreFunction(scoreFunctionId, hashFunctionId, peerId, keyword, stake) { - return this.callContractFunction(this.ScoringProxyContract, 'callScoreFunction', [ - scoreFunctionId, - hashFunctionId, - this.convertAsciiToHex(peerId), - keyword, - stake, - ]); - } - async getLog2PLDSFParams() { const log2pldsfParams = await this.callContractFunction( this.scoringFunctionsContracts[1], diff --git a/src/service/blockchain-event-listener-service.js b/src/service/blockchain-event-listener-service.js index 1f7e65911b..04d52f3661 100644 --- a/src/service/blockchain-event-listener-service.js +++ b/src/service/blockchain-event-listener-service.js @@ -236,8 +236,9 @@ class BlockchainEventListenerService { async handleParameterChangedEvents(blockEvents) { for (const event of blockEvents) { const { parameterName, parameterValue } = JSON.parse(event.data); - this.blockchainModuleManager.cacheParameter( + this.blockchainModuleManager.setContractCallCache( event.blockchainId, + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, parameterName, parameterValue, ); From 04fbd45744291eb83080e2054953083e1ed3e4c8 Mon Sep 17 00:00:00 2001 From: Djordje Kovacevic Date: Mon, 25 Dec 2023 12:24:51 +0100 Subject: [PATCH 09/10] Updated caching --- src/constants/constants.js | 2 +- src/modules/blockchain/implementation/web3-service.js | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/constants/constants.js b/src/constants/constants.js index e624863323..052f17df4c 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -613,6 +613,6 @@ export const CACHED_FUNCTIONS = { epochLength: CACHE_DATA_TYPES.NUMBER, }, IdentityStorageContract: { - getIndentityId: CACHE_DATA_TYPES.NUMBER, + getIdentityId: CACHE_DATA_TYPES.NUMBER, }, }; diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index d9eaf63616..486d25acd5 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -289,10 +289,14 @@ class Web3Service { } } - getContractCallCache(contractName, parameterName) { - if (CACHED_FUNCTIONS[contractName]?.[parameterName]) { - return contractCallCache[contractName][parameterName]; + getContractCallCache(contractName, functionName) { + if ( + CACHED_FUNCTIONS[contractName]?.[functionName] && + contractCallCache[contractName]?.[functionName] + ) { + return contractCallCache[contractName][functionName]; } + return null; } initializeContract(contractName, contractAddress) { From 4810571b7433c778e5484c6c71c56abd9ca8b0e5 Mon Sep 17 00:00:00 2001 From: djordjekovac Date: Mon, 25 Dec 2023 12:36:37 +0100 Subject: [PATCH 10/10] Update src/modules/blockchain/implementation/web3-service.js Co-authored-by: Nikola Todorovic --- src/modules/blockchain/implementation/web3-service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index df5d7775f5..78c257f741 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -1121,7 +1121,7 @@ class Web3Service { this.ParametersStorageContract, 'updateCommitWindowDuration', [], - 'ParametersStorage', + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, ); return Number(commitWindowDurationPerc); }