@@ -38,6 +38,7 @@ import {
38
38
DeploymentManagementMode ,
39
39
SubgraphStatus ,
40
40
sequentialTimerMap ,
41
+ HorizonTransitionValue ,
41
42
} from '@graphprotocol/indexer-common'
42
43
43
44
import PQueue from 'p-queue'
@@ -47,7 +48,11 @@ import mapValues from 'lodash.mapvalues'
47
48
import zip from 'lodash.zip'
48
49
import { AgentConfigs , NetworkAndOperator } from './types'
49
50
50
- type ActionReconciliationContext = [ AllocationDecision [ ] , number , bigint ]
51
+ type ActionReconciliationContext = [
52
+ AllocationDecision [ ] ,
53
+ number ,
54
+ HorizonTransitionValue ,
55
+ ]
51
56
52
57
const deploymentInList = (
53
58
list : SubgraphDeploymentID [ ] ,
@@ -271,21 +276,22 @@ export class Agent {
271
276
} ,
272
277
)
273
278
274
- const maxAllocationEpochs : Eventual < NetworkMapped < bigint > > =
275
- sequentialTimerMap (
276
- { logger, milliseconds : requestIntervalLarge } ,
277
- ( ) =>
278
- this . multiNetworks . map ( ( { network } ) => {
279
- logger . trace ( 'Fetching max allocation epochs' , {
280
- protocolNetwork : network . specification . networkIdentifier ,
281
- } )
282
- return network . contracts . LegacyStaking . maxAllocationEpochs ( )
283
- } ) ,
284
- {
285
- onError : error =>
286
- logger . warn ( `Failed to fetch max allocation epochs` , { error } ) ,
287
- } ,
288
- )
279
+ const maxAllocationDuration : Eventual <
280
+ NetworkMapped < HorizonTransitionValue >
281
+ > = sequentialTimerMap (
282
+ { logger, milliseconds : requestIntervalLarge } ,
283
+ ( ) =>
284
+ this . multiNetworks . map ( ( { network } ) => {
285
+ logger . trace ( 'Fetching max allocation duration' , {
286
+ protocolNetwork : network . specification . networkIdentifier ,
287
+ } )
288
+ return network . networkMonitor . maxAllocationDuration ( )
289
+ } ) ,
290
+ {
291
+ onError : error =>
292
+ logger . warn ( `Failed to fetch max allocation duration` , { error } ) ,
293
+ } ,
294
+ )
289
295
290
296
const indexingRules : Eventual < NetworkMapped < IndexingRuleAttributes [ ] > > =
291
297
sequentialTimerMap (
@@ -653,7 +659,7 @@ export class Agent {
653
659
join ( {
654
660
ticker : timer ( requestIntervalLarge ) ,
655
661
currentEpochNumber,
656
- maxAllocationEpochs ,
662
+ maxAllocationDuration ,
657
663
activeDeployments,
658
664
targetDeployments,
659
665
activeAllocations,
@@ -663,7 +669,7 @@ export class Agent {
663
669
} ) . pipe (
664
670
async ( {
665
671
currentEpochNumber,
666
- maxAllocationEpochs ,
672
+ maxAllocationDuration ,
667
673
activeDeployments,
668
674
targetDeployments,
669
675
activeAllocations,
@@ -746,7 +752,7 @@ export class Agent {
746
752
await this . reconcileActions (
747
753
networkDeploymentAllocationDecisions ,
748
754
currentEpochNumber ,
749
- maxAllocationEpochs ,
755
+ maxAllocationDuration ,
750
756
)
751
757
} catch ( err ) {
752
758
logger . warn ( `Exited early while reconciling actions` , {
@@ -1008,18 +1014,25 @@ export class Agent {
1008
1014
activeAllocations : Allocation [ ] ,
1009
1015
deploymentAllocationDecision : AllocationDecision ,
1010
1016
epoch : number ,
1011
- maxAllocationEpochs : bigint ,
1017
+ maxAllocationDuration : HorizonTransitionValue ,
1012
1018
network : Network ,
1013
1019
) : Promise < Allocation [ ] > {
1014
- const desiredAllocationLifetime = deploymentAllocationDecision . ruleMatch
1015
- . rule ?. allocationLifetime
1016
- ? deploymentAllocationDecision . ruleMatch . rule . allocationLifetime
1017
- : Math . max ( 1 , Number ( maxAllocationEpochs ) - 1 )
1018
-
1019
- // Identify expiring allocations
1020
1020
let expiredAllocations = activeAllocations . filter (
1021
- allocation =>
1022
- epoch >= allocation . createdAtEpoch + desiredAllocationLifetime ,
1021
+ async ( allocation : Allocation ) => {
1022
+ let desiredAllocationLifetime : number = 0
1023
+ if ( allocation . isLegacy ) {
1024
+ desiredAllocationLifetime = deploymentAllocationDecision . ruleMatch
1025
+ . rule ?. allocationLifetime
1026
+ ? deploymentAllocationDecision . ruleMatch . rule . allocationLifetime
1027
+ : Math . max ( 1 , maxAllocationDuration . legacy - 1 )
1028
+ } else {
1029
+ desiredAllocationLifetime = deploymentAllocationDecision . ruleMatch
1030
+ . rule ?. allocationLifetime
1031
+ ? deploymentAllocationDecision . ruleMatch . rule . allocationLifetime
1032
+ : maxAllocationDuration . horizon
1033
+ }
1034
+ return epoch >= allocation . createdAtEpoch + desiredAllocationLifetime
1035
+ } ,
1023
1036
)
1024
1037
// The allocations come from the network subgraph; due to short indexing
1025
1038
// latencies, this data may be slightly outdated. Cross-check with the
@@ -1029,9 +1042,17 @@ export class Agent {
1029
1042
expiredAllocations ,
1030
1043
async ( allocation : Allocation ) => {
1031
1044
try {
1032
- const onChainAllocation =
1033
- await network . contracts . LegacyStaking . getAllocation ( allocation . id )
1034
- return onChainAllocation . closedAtEpoch == 0n
1045
+ if ( allocation . isLegacy ) {
1046
+ const onChainAllocation =
1047
+ await network . contracts . LegacyStaking . getAllocation ( allocation . id )
1048
+ return onChainAllocation . closedAtEpoch == 0n
1049
+ } else {
1050
+ const onChainAllocation =
1051
+ await network . contracts . SubgraphService . getAllocation (
1052
+ allocation . id ,
1053
+ )
1054
+ return onChainAllocation . closedAt == 0n
1055
+ }
1035
1056
} catch ( err ) {
1036
1057
this . logger . warn (
1037
1058
`Failed to cross-check allocation state with contracts; assuming it needs to be closed` ,
@@ -1052,7 +1073,7 @@ export class Agent {
1052
1073
deploymentAllocationDecision : AllocationDecision ,
1053
1074
activeAllocations : Allocation [ ] ,
1054
1075
epoch : number ,
1055
- maxAllocationEpochs : bigint ,
1076
+ maxAllocationDuration : HorizonTransitionValue ,
1056
1077
network : Network ,
1057
1078
operator : Operator ,
1058
1079
) : Promise < void > {
@@ -1128,7 +1149,7 @@ export class Agent {
1128
1149
activeDeploymentAllocations ,
1129
1150
deploymentAllocationDecision ,
1130
1151
epoch ,
1131
- maxAllocationEpochs ,
1152
+ maxAllocationDuration ,
1132
1153
network ,
1133
1154
)
1134
1155
if ( expiringAllocations . length > 0 ) {
@@ -1147,7 +1168,7 @@ export class Agent {
1147
1168
async reconcileActions (
1148
1169
networkDeploymentAllocationDecisions : NetworkMapped < AllocationDecision [ ] > ,
1149
1170
epoch : NetworkMapped < number > ,
1150
- maxAllocationEpochs : NetworkMapped < bigint > ,
1171
+ maxAllocationDuration : NetworkMapped < HorizonTransitionValue > ,
1151
1172
) : Promise < void > {
1152
1173
// --------------------------------------------------------------------------------
1153
1174
// Filter out networks set to `manual` allocation management mode, and ensure the
@@ -1200,14 +1221,14 @@ export class Agent {
1200
1221
this . multiNetworks . zip3 (
1201
1222
validatedAllocationDecisions ,
1202
1223
epoch ,
1203
- maxAllocationEpochs ,
1224
+ maxAllocationDuration ,
1204
1225
) ,
1205
1226
async (
1206
1227
{ network, operator } : NetworkAndOperator ,
1207
1228
[
1208
1229
allocationDecisions ,
1209
1230
epoch ,
1210
- maxAllocationEpochs ,
1231
+ maxAllocationDuration ,
1211
1232
] : ActionReconciliationContext ,
1212
1233
) => {
1213
1234
// Do nothing if there are already approved actions in the queue awaiting execution
@@ -1232,7 +1253,7 @@ export class Agent {
1232
1253
this . logger . trace ( `Reconcile allocation actions` , {
1233
1254
protocolNetwork : network . specification . networkIdentifier ,
1234
1255
epoch,
1235
- maxAllocationEpochs ,
1256
+ maxAllocationDuration ,
1236
1257
targetDeployments : allocationDecisions
1237
1258
. filter ( decision => decision . toAllocate )
1238
1259
. map ( decision => decision . deployment . ipfsHash ) ,
@@ -1248,7 +1269,7 @@ export class Agent {
1248
1269
decision ,
1249
1270
activeAllocations ,
1250
1271
epoch ,
1251
- maxAllocationEpochs ,
1272
+ maxAllocationDuration ,
1252
1273
network ,
1253
1274
operator ,
1254
1275
) ,
0 commit comments