From b3da277d74872932d0ca8841a522755ee6e08274 Mon Sep 17 00:00:00 2001 From: Djordje Kovacevic Date: Thu, 1 Feb 2024 11:53:30 +0100 Subject: [PATCH] Updated epoch check command, added listening on scoring contract events --- .../protocols/common/epoch-check-command.js | 50 ++++++++++++------ .../v1-0-0-handle-update-request-command.js | 4 +- src/constants/constants.js | 6 ++- .../blockchain/implementation/web3-service.js | 9 ++-- .../blockchain-event-listener-service.js | 51 ++++++++++++++++--- 5 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/commands/protocols/common/epoch-check-command.js b/src/commands/protocols/common/epoch-check-command.js index 477a3b9747..0b144995a5 100644 --- a/src/commands/protocols/common/epoch-check-command.js +++ b/src/commands/protocols/common/epoch-check-command.js @@ -37,11 +37,11 @@ class EpochCheckCommand extends Command { !migrationExecuted ) { this.logger.info( - 'Epoch check command will be postponed until ual extension triple store migration is completed', + 'Epoch check: command will be postponed until ual extension triple store migration is completed', ); return Command.repeat(); } - this.logger.info('Starting epoch check command'); + this.logger.info('Epoch check: Starting epoch check command'); const operationId = this.operationIdService.generateId(); await Promise.all( @@ -77,7 +77,7 @@ class EpochCheckCommand extends Command { const [r0, r2, totalNodesNumber, minStake, maxStake] = await Promise.all([ this.blockchainModuleManager.getR0(blockchain), this.blockchainModuleManager.getR2(blockchain), - this.blockchainModuleManager.getShardingTableLength(blockchain), + this.repositoryModuleManager.getPeersCount(blockchain), this.blockchainModuleManager.getMinimumStake(blockchain), this.blockchainModuleManager.getMaximumStake(blockchain), ]); @@ -129,11 +129,18 @@ class EpochCheckCommand extends Command { blockchain, commitWindowDurationPerc, ); - + this.logger.info( + `Epoch check: Found ${eligibleAgreementForSubmitCommit.length} eligible agreements for submit commit for blockchain: ${blockchain}`, + ); const scheduleSubmitCommitCommands = []; const updateServiceAgreementsLastCommitEpoch = []; for (const serviceAgreement of eligibleAgreementForSubmitCommit) { - if (scheduleSubmitCommitCommands.length >= maxTransactions) break; + if (scheduleSubmitCommitCommands.length >= maxTransactions) { + this.logger.warn( + `Epoch check: not scheduling new commits. Submit commit command length: ${scheduleSubmitCommitCommands.length}, max number of transactions: ${maxTransactions} for blockchain: ${blockchain}`, + ); + break; + } const neighbourhood = await this.shardingTableService.findNeighbourhood( blockchain, @@ -156,7 +163,10 @@ class EpochCheckCommand extends Command { } if (!neighbourhoodEdges && serviceAgreement.scoreFunctionId === 2) { - throw Error('Unable to find neighbourhood edges for asset'); + this.logger.warn( + `Epoch check: unable to find neighbourhood edges for agreement id: ${serviceAgreement.agreementId} for blockchain: ${blockchain}`, + ); + continue; } try { @@ -182,28 +192,28 @@ class EpochCheckCommand extends Command { if (rank == null) { this.logger.trace( - `Node not in R2: ${r2} for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Skipping scheduling submitCommitCommand.`, + `Epoch check: Node not in R2: ${r2} for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Skipping scheduling submitCommitCommand for blockchain: ${blockchain}`, ); continue; } if (rank >= r0) { this.logger.trace( - `Calculated rank: ${ + `Epoch check: Calculated rank: ${ rank + 1 }. Node not in R0: ${r0} for the Service Agreement with the ID: ${ serviceAgreement.agreementId - }. Skipping scheduling submitCommitCommand.`, + }. Skipping scheduling submitCommitCommand for blockchain: ${blockchain}`, ); continue; } this.logger.trace( - `Calculated rank: ${ + `Epoch check: Calculated rank: ${ rank + 1 }. Node in R0: ${r0} for the Service Agreement with the ID: ${ serviceAgreement.agreementId - }. Scheduling submitCommitCommand.`, + }. Scheduling submitCommitCommand for blockchain: ${blockchain}`, ); const closestNode = neighbourhood[0]; scheduleSubmitCommitCommands.push( @@ -215,7 +225,7 @@ class EpochCheckCommand extends Command { ); } catch (error) { this.logger.warn( - `Failed to schedule submitCommitCommand for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Error message: ${error.message}.`, + `Epoch check: Failed to schedule submitCommitCommand for the Service Agreement with the ID: ${serviceAgreement.agreementId} for blockchain: ${blockchain}. Error message: ${error.message}.`, ); continue; } @@ -239,10 +249,18 @@ class EpochCheckCommand extends Command { blockchain, proofWindowDurationPerc, ); + this.logger.info( + `Epoch check: Found ${eligibleAgreementsForSubmitProofs.length} eligible agreements for submit proof for blockchain: ${blockchain}`, + ); const scheduleSubmitProofCommands = []; const updateServiceAgreementsLastProofEpoch = []; for (const serviceAgreement of eligibleAgreementsForSubmitProofs) { - if (scheduleSubmitProofCommands.length >= maxTransactions) break; + if (scheduleSubmitProofCommands.length >= maxTransactions) { + this.logger.warn( + `Epoch check: not scheduling new proofs. Submit proofs command length: ${scheduleSubmitProofCommands.length}, max number of transactions: ${maxTransactions} for blockchain: ${blockchain}`, + ); + break; + } try { const eligibleForReward = await this.isEligibleForRewards( @@ -254,7 +272,7 @@ class EpochCheckCommand extends Command { ); if (eligibleForReward) { this.logger.trace( - `Node is eligible for rewards for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Scheduling submitProofsCommand.`, + `Epoch check: Node is eligible for rewards for the Service Agreement with the ID: ${serviceAgreement.agreementId} for blockchain: ${blockchain}. Scheduling submitProofsCommand.`, ); scheduleSubmitProofCommands.push( @@ -262,7 +280,7 @@ class EpochCheckCommand extends Command { ); } else { this.logger.trace( - `Node is not eligible for rewards for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Skipping scheduling submitProofsCommand.`, + `Epoch check: Node is not eligible for rewards for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Skipping scheduling submitProofsCommand for blockchain: ${blockchain}`, ); } updateServiceAgreementsLastProofEpoch.push( @@ -273,7 +291,7 @@ class EpochCheckCommand extends Command { ); } catch (error) { this.logger.warn( - `Failed to schedule submitProofsCommand for the Service Agreement with the ID: ${serviceAgreement.agreementId}. Error message: ${error.message}.`, + `Epoch check: Failed to schedule submitProofsCommand for the Service Agreement with the ID: ${serviceAgreement.agreementId} for blockchain: ${blockchain}. Error message: ${error.message}.`, ); continue; } diff --git a/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js b/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js index daf2576398..4a2ab745c1 100644 --- a/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js +++ b/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js @@ -96,9 +96,7 @@ class HandleUpdateRequestCommand extends HandleProtocolMessageCommand { throw Error('Unable to find neighbourhood edges for asset'); } - const totalNodesNumber = await this.blockchainModuleManager.getShardingTableLength( - blockchain, - ); + const totalNodesNumber = await this.repositoryModuleManager.getPeersCount(blockchain); const minStake = await this.blockchainModuleManager.getMinimumStake(blockchain); const maxStake = await this.blockchainModuleManager.getMaximumStake(blockchain); diff --git a/src/constants/constants.js b/src/constants/constants.js index 47af51d9c5..ddb44ab5bc 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -576,8 +576,8 @@ export const CONTRACTS = { SERVICE_AGREEMENT_V1_CONTRACT: 'ServiceAgreementV1Contract', PARAMETERS_STORAGE_CONTRACT: 'ParametersStorageContract', IDENTITY_STORAGE_CONTRACT: 'IdentityStorageContract', - Log2PLDSF: 'Log2PLDSF', - LINEAR_SUM: 'LinearSum', + Log2PLDSF_CONTRACT: 'Log2PLDSFContract', + LINEAR_SUM_CONTRACT: 'LinearSumContract', }; export const CONTRACT_EVENTS = { @@ -588,6 +588,8 @@ export const CONTRACT_EVENTS = { COMMIT_MANAGER_V1: ['StateFinalized'], SERVICE_AGREEMENT_V1: ['ServiceAgreementV1Extended', 'ServiceAgreementV1Terminated'], PARAMETERS_STORAGE: ['ParameterChanged'], + Log2PLDSF: ['ParameterChanged'], + LINEAR_SUM: ['ParameterChanged'], }; export const NODE_ENVIRONMENTS = { diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index db44d6ce73..9db13b47de 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -710,9 +710,10 @@ class Web3Service { ) { const contract = this[contractName]; if (!contract) { - throw new Error( - `Error while getting all past events. Unknown contract: ${contractName}`, - ); + // this will happen when we have different set of contracts on different blockchains + // eg LinearSum contract is available on gnosis but not on NeuroWeb, so the node should not fetch events + // from LinearSum contract on NeuroWeb blockchain + return []; } let fromBlock; @@ -1294,7 +1295,7 @@ class Web3Service { this.scoringFunctionsContracts[2], 'getParameters', [], - CONTRACTS.LINEAR_SUM, + CONTRACTS.LINEAR_SUM_CONTRACT, ); return { distanceScaleFactor: BigNumber.from(linearSumParams[0]), diff --git a/src/service/blockchain-event-listener-service.js b/src/service/blockchain-event-listener-service.js index afb4b889d9..aca8db13f2 100644 --- a/src/service/blockchain-event-listener-service.js +++ b/src/service/blockchain-event-listener-service.js @@ -88,6 +88,18 @@ class BlockchainEventListenerService { currentBlock, CONTRACT_EVENTS.PARAMETERS_STORAGE, ), + this.getContractEvents( + blockchainId, + CONTRACTS.Log2PLDSF_CONTRACT, + currentBlock, + CONTRACT_EVENTS.Log2PLDSF, + ), + this.getContractEvents( + blockchainId, + CONTRACTS.LINEAR_SUM_CONTRACT, + currentBlock, + CONTRACT_EVENTS.LINEAR_SUM, + ), ]; if (!devEnvironment) { @@ -239,13 +251,38 @@ class BlockchainEventListenerService { async handleParameterChangedEvents(blockEvents) { for (const event of blockEvents) { - const { parameterName, parameterValue } = JSON.parse(event.data); - this.blockchainModuleManager.setContractCallCache( - event.blockchainId, - CONTRACTS.PARAMETERS_STORAGE_CONTRACT, - parameterName, - parameterValue, - ); + const { blockchainId, contract, data } = event; + const { parameterName, parameterValue } = JSON.parse(data); + switch (contract) { + case CONTRACTS.Log2PLDSF_CONTRACT: + this.blockchainModuleManager.setContractCallCache( + blockchainId, + CONTRACTS.Log2PLDSF_CONTRACT, + parameterName, + null, + ); + break; + case CONTRACTS.LINEAR_SUM_CONTRACT: + this.blockchainModuleManager.setContractCallCache( + blockchainId, + CONTRACTS.LINEAR_SUM_CONTRACT, + parameterName, + null, + ); + break; + case CONTRACTS.PARAMETERS_STORAGE_CONTRACT: + this.blockchainModuleManager.setContractCallCache( + blockchainId, + CONTRACTS.PARAMETERS_STORAGE_CONTRACT, + parameterName, + parameterValue, + ); + break; + default: + this.logger.warn( + `Unable to handle parameter changed event. Unknown contract name ${event.contract}`, + ); + } } }