1
1
import gql from 'graphql-tag'
2
2
import jayson , { Client as RpcClient } from 'jayson/promise'
3
- import { Logger , SubgraphDeploymentID } from '@graphprotocol/common-ts'
3
+ import { Logger , SubgraphDeploymentID , NetworkContracts } from '@graphprotocol/common-ts'
4
4
import { Client , createClient } from '@urql/core'
5
5
import {
6
6
INDEXER_ERROR_MESSAGES ,
@@ -665,6 +665,7 @@ export class GraphNode {
665
665
name : string ,
666
666
deployment : SubgraphDeploymentID ,
667
667
currentAssignments ?: SubgraphDeploymentAssignment [ ] ,
668
+ contracts ?: NetworkContracts ,
668
669
) : Promise < void > {
669
670
this . logger . debug ( 'Ensure subgraph deployment is syncing' , {
670
671
name,
@@ -693,7 +694,12 @@ export class GraphNode {
693
694
await this . resume ( deployment )
694
695
} else {
695
696
// Subgraph deployment not found
696
- await this . autoGraftDeployDependencies ( deployment , deploymentAssignments , name )
697
+ await this . autoGraftDeployDependencies (
698
+ deployment ,
699
+ deploymentAssignments ,
700
+ name ,
701
+ contracts ,
702
+ )
697
703
698
704
// Create and deploy the subgraph
699
705
this . logger . debug (
@@ -732,6 +738,7 @@ export class GraphNode {
732
738
deployment : SubgraphDeploymentID ,
733
739
deploymentAssignments : SubgraphDeploymentAssignment [ ] ,
734
740
name : string ,
741
+ contracts ?: NetworkContracts ,
735
742
) {
736
743
this . logger . debug ( 'Auto graft deploy subgraph dependencies' )
737
744
const { network : subgraphChainName } = await this . subgraphFeatures ( deployment )
@@ -805,6 +812,7 @@ export class GraphNode {
805
812
dependency . block ,
806
813
dependency . base ,
807
814
subgraphChainName ,
815
+ contracts ,
808
816
)
809
817
}
810
818
}
@@ -820,6 +828,7 @@ export class GraphNode {
820
828
blockHeight : number ,
821
829
subgraphDeployment : SubgraphDeploymentID ,
822
830
chainName : string | null ,
831
+ contracts ?: NetworkContracts ,
823
832
) : Promise < void > {
824
833
async function waitForMs ( ms : number ) {
825
834
return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
@@ -912,12 +921,71 @@ export class GraphNode {
912
921
// Is the graftBaseBlock within the range of the earliest and head of the chain?
913
922
if ( chain . latestBlock && chain . latestBlock . number >= blockHeight ) {
914
923
if ( ! deployed [ 0 ] . paused ) {
915
- this . logger . debug ( `Subgraph synced to block! Pausing as requirement is met.` , {
916
- subgraph : subgraphDeployment . ipfsHash ,
917
- indexingStatus,
918
- } )
919
- // pause the subgraph to prevent further indexing
920
- await this . pause ( subgraphDeployment )
924
+ // Check if there's an active allocation before pausing
925
+ let hasActiveAllocation = false
926
+ let checkFailed = false
927
+
928
+ // If contracts are provided, check for active allocations
929
+ if ( contracts ) {
930
+ try {
931
+ // Check if any allocations exist for this subgraph deployment
932
+ const filter = contracts . staking . filters . AllocationCreated (
933
+ null , // indexer (any)
934
+ subgraphDeployment . bytes32 , // subgraphDeploymentID
935
+ )
936
+ const events = await contracts . staking . queryFilter ( filter )
937
+
938
+ // Check each allocation to see if it's still active
939
+ for ( const event of events ) {
940
+ const allocationId = event . args ?. allocationID
941
+ if ( allocationId ) {
942
+ const state = await contracts . staking . getAllocationState ( allocationId )
943
+ // State 1 means Active allocation
944
+ if ( state === 1 ) {
945
+ hasActiveAllocation = true
946
+ break
947
+ }
948
+ }
949
+ }
950
+
951
+ if ( hasActiveAllocation ) {
952
+ this . logger . warn ( `Subgraph has active allocation, not going to pause` , {
953
+ subgraph : subgraphDeployment . ipfsHash ,
954
+ blockHeight,
955
+ indexingStatus,
956
+ } )
957
+ }
958
+ } catch ( error ) {
959
+ checkFailed = true
960
+ this . logger . error ( `Failed to check allocation state, not going to pause` , {
961
+ subgraph : subgraphDeployment . ipfsHash ,
962
+ error,
963
+ } )
964
+ }
965
+ } else {
966
+ // No contracts provided, skip pause for safety
967
+ this . logger . warn (
968
+ `No contracts provided to check allocation state, not going to pause` ,
969
+ {
970
+ subgraph : subgraphDeployment . ipfsHash ,
971
+ blockHeight,
972
+ } ,
973
+ )
974
+ checkFailed = true
975
+ }
976
+
977
+ // Only pause if we successfully checked and found no active allocations
978
+ if ( ! hasActiveAllocation && ! checkFailed ) {
979
+ this . logger . debug (
980
+ `Subgraph synced to block! Pausing as requirement is met.` ,
981
+ {
982
+ subgraph : subgraphDeployment . ipfsHash ,
983
+ indexingStatus,
984
+ } ,
985
+ )
986
+ // pause the subgraph to prevent further indexing
987
+ await this . pause ( subgraphDeployment )
988
+ }
921
989
} else {
922
990
this . logger . debug ( `Subgraph already paused and synced to block.` , {
923
991
subgraph : subgraphDeployment . ipfsHash ,
0 commit comments