From ff813e527145b41b6efef6ceaade1b99dfb82696 Mon Sep 17 00:00:00 2001 From: tilacog Date: Mon, 26 Jun 2023 18:29:34 -0300 Subject: [PATCH] agent,common: refactor reconcileActions function --- packages/indexer-agent/package.json | 2 - packages/indexer-agent/src/agent.ts | 81 +++++++------------ packages/indexer-common/src/multi-networks.ts | 13 +++ yarn.lock | 12 --- 4 files changed, 40 insertions(+), 68 deletions(-) diff --git a/packages/indexer-agent/package.json b/packages/indexer-agent/package.json index 55d9b222c..cacb7d79b 100644 --- a/packages/indexer-agent/package.json +++ b/packages/indexer-agent/package.json @@ -43,7 +43,6 @@ "graphql-tag": "2.12.6", "isomorphic-fetch": "3.0.0", "jayson": "3.6.6", - "lodash.isempty": "^4.4.0", "lodash.isequal": "^4.5.0", "lodash.mapvalues": "^4.6.0", "lodash.zip": "^4.2.0", @@ -64,7 +63,6 @@ "@types/isomorphic-fetch": "0.0.36", "@types/jest": "27.4.1", "@types/lodash.countby": "^4.6.7", - "@types/lodash.isempty": "^4.4.7", "@types/lodash.isequal": "^4.5.6", "@types/lodash.mapvalues": "^4.6.7", "@types/lodash.zip": "^4.2.7", diff --git a/packages/indexer-agent/src/agent.ts b/packages/indexer-agent/src/agent.ts index ac5df0662..c4196c82a 100644 --- a/packages/indexer-agent/src/agent.ts +++ b/packages/indexer-agent/src/agent.ts @@ -38,15 +38,9 @@ import pMap from 'p-map' import pFilter from 'p-filter' import isEqual from 'lodash.isequal' import mapValues from 'lodash.mapvalues' -import isEmpty from 'lodash.isempty' import zip from 'lodash.zip' -type ActionReconciliationContext = [ - AllocationDecision[], - Allocation[], - number, - number, -] +type ActionReconciliationContext = [AllocationDecision[], number, number] const deploymentInList = ( list: SubgraphDeploymentID[], @@ -608,7 +602,6 @@ export class Agent { // Reconcile allocation actions await this.reconcileActions( networkDeploymentAllocationDecisions, - activeAllocations, currentEpochNumber, maxAllocationEpochs, ) @@ -654,7 +647,7 @@ export class Agent { dispute => dispute.allocationID == allocation.id, ), ) - if (newDisputableAllocations.length == 0) { + if (newDisputableAllocations.length === 0) { this.logger.trace( 'No new disputable allocations to process for potential disputes', { protocolNetwork: network.specification.networkIdentifier }, @@ -991,61 +984,43 @@ export class Agent { // remove it from this function? async reconcileActions( networkDeploymentAllocationDecisions: NetworkMapped, - activeAllocations: NetworkMapped, epoch: NetworkMapped, maxAllocationEpochs: NetworkMapped, ): Promise { - // ---------------------------------------------------------------------------------------- - // Filter out networks set to `manual` allocation management mode - // ---------------------------------------------------------------------------------------- - const manualModeNetworks = this.multiNetworks.mapNetworkMapped( - networkDeploymentAllocationDecisions, - async ({ network }) => - network.specification.indexerOptions.allocationManagementMode == - AllocationManagementMode.MANUAL, - ) - - for (const [networkIdentifier, isOnManualMode] of Object.entries( - manualModeNetworks, - )) { - if (isOnManualMode) { - const allocationDecisions = - networkDeploymentAllocationDecisions[networkIdentifier] - this.logger.trace( - `Skipping allocation reconciliation since AllocationManagementMode = 'manual'`, - { - protocolNetwork: networkIdentifier, - activeAllocations, - targetDeployments: allocationDecisions - .filter(decision => decision.toAllocate) - .map(decision => decision.deployment.ipfsHash), - }, - ) - delete networkDeploymentAllocationDecisions[networkIdentifier] - } - } - - if (isEmpty(networkDeploymentAllocationDecisions)) { - return - } - - // ---------------------------------------------------------------------------------------- - // Ensure the network subgraph is NEVER allocated towards - // ---------------------------------------------------------------------------------------- - - const filteredNetworkDeploymentAllocationDecisions = + // -------------------------------------------------------------------------------- + // Filter out networks set to `manual` allocation management mode, and ensure the + // Network Subgraph is NEVER allocated towards + // -------------------------------------------------------------------------------- + const validatedAllocationDecisions = await this.multiNetworks.mapNetworkMapped( networkDeploymentAllocationDecisions, async ( { network }: NetworkAndOperator, allocationDecisions: AllocationDecision[], ) => { + if ( + network.specification.indexerOptions.allocationManagementMode === + AllocationManagementMode.MANUAL + ) { + this.logger.trace( + `Skipping allocation reconciliation since AllocationManagementMode = 'manual'`, + { + protocolNetwork: network.specification.networkIdentifier, + targetDeployments: allocationDecisions + .filter(decision => decision.toAllocate) + .map(decision => decision.deployment.ipfsHash), + }, + ) + allocationDecisions.forEach( + allocation => (allocation.toAllocate = false), + ) + return allocationDecisions + } const networkSubgraphDeployment = network.networkSubgraph.deployment if ( networkSubgraphDeployment && !network.specification.indexerOptions.allocateOnNetworkSubgraph ) { - // QUESTION: Could we just remove this allocation decision from the set? const networkSubgraphIndex = allocationDecisions.findIndex( decision => decision.deployment.bytes32 == @@ -1063,9 +1038,8 @@ export class Agent { // For every network, loop through all deployments and queue allocation actions if needed //---------------------------------------------------------------------------------------- await this.multiNetworks.mapNetworkMapped( - this.multiNetworks.zip4( - filteredNetworkDeploymentAllocationDecisions, - activeAllocations, + this.multiNetworks.zip3( + validatedAllocationDecisions, epoch, maxAllocationEpochs, ), @@ -1073,7 +1047,6 @@ export class Agent { { network, operator }: NetworkAndOperator, [ allocationDecisions, - _activeAllocations, epoch, maxAllocationEpochs, ]: ActionReconciliationContext, diff --git a/packages/indexer-common/src/multi-networks.ts b/packages/indexer-common/src/multi-networks.ts index 2ba7b7fa6..f305d2087 100644 --- a/packages/indexer-common/src/multi-networks.ts +++ b/packages/indexer-common/src/multi-networks.ts @@ -64,6 +64,19 @@ export class MultiNetworks { return result } + zip3( + a: NetworkMapped, + b: NetworkMapped, + c: NetworkMapped, + ): NetworkMapped<[U, V, W]> { + this.checkEqualKeys(a, b) + const result = {} as NetworkMapped<[U, V, W]> + for (const key in a) { + result[key] = [a[key], b[key], c[key]] + } + return result + } + zip4( a: NetworkMapped, b: NetworkMapped, diff --git a/yarn.lock b/yarn.lock index 8132298cc..cec112ecd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2842,13 +2842,6 @@ dependencies: "@types/lodash" "*" -"@types/lodash.isempty@^4.4.7": - version "4.4.7" - resolved "https://registry.npmjs.org/@types/lodash.isempty/-/lodash.isempty-4.4.7.tgz#b1015d1adba560daf560ad04f294848939e75317" - integrity sha512-YOzlpoIn9jrfHzjIukKnu9Le3tmi+0PhUdOt2rMpJW/4J6jX7s0HeBatXdh9QckLga8qt4EKBxVIEqtEq6pzLg== - dependencies: - "@types/lodash" "*" - "@types/lodash.isequal@^4.5.6": version "4.5.6" resolved "https://registry.npmjs.org/@types/lodash.isequal/-/lodash.isequal-4.5.6.tgz" @@ -7594,11 +7587,6 @@ lodash.groupby@^4.6.0: resolved "https://registry.npmjs.org/lodash.groupby/-/lodash.groupby-4.6.0.tgz" integrity sha512-5dcWxm23+VAoz+awKmBaiBvzox8+RqMgFhi7UvX9DHZr2HdxHXM/Wrf8cfKpsW37RNrvtPn6hSwNqurSILbmJw== -lodash.isempty@^4.4.0: - version "4.4.0" - resolved "https://registry.npmjs.org/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e" - integrity sha512-oKMuF3xEeqDltrGMfDxAPGIVMSSRv8tbRSODbrs4KGsRRLEhrW8N8Rd4DRgB2+621hY8A8XwwrTVhXWpxFvMzg== - lodash.isequal@^4.5.0: version "4.5.0" resolved "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz"