Skip to content

Commit

Permalink
agent,common: refactor reconcileActions function
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Jun 26, 2023
1 parent 9e56aae commit ff813e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 68 deletions.
2 changes: 0 additions & 2 deletions packages/indexer-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
81 changes: 27 additions & 54 deletions packages/indexer-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -608,7 +602,6 @@ export class Agent {
// Reconcile allocation actions
await this.reconcileActions(
networkDeploymentAllocationDecisions,
activeAllocations,
currentEpochNumber,
maxAllocationEpochs,
)
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -991,61 +984,43 @@ export class Agent {
// remove it from this function?
async reconcileActions(
networkDeploymentAllocationDecisions: NetworkMapped<AllocationDecision[]>,
activeAllocations: NetworkMapped<Allocation[]>,
epoch: NetworkMapped<number>,
maxAllocationEpochs: NetworkMapped<number>,
): Promise<void> {
// ----------------------------------------------------------------------------------------
// 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 ==
Expand All @@ -1063,17 +1038,15 @@ 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,
),
async (
{ network, operator }: NetworkAndOperator,
[
allocationDecisions,
_activeAllocations,
epoch,
maxAllocationEpochs,
]: ActionReconciliationContext,
Expand Down
13 changes: 13 additions & 0 deletions packages/indexer-common/src/multi-networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ export class MultiNetworks<T> {
return result
}

zip3<U, V, W>(
a: NetworkMapped<U>,
b: NetworkMapped<V>,
c: NetworkMapped<W>,
): 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<U, V, W, Y>(
a: NetworkMapped<U>,
b: NetworkMapped<V>,
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ff813e5

Please sign in to comment.