@@ -10,12 +10,15 @@ pub mod status;
1010
1111pub use features:: { SubgraphFeature , SubgraphFeatureValidationError } ;
1212
13- use crate :: object;
13+ use crate :: { components :: store :: BLOCK_NUMBER_MAX , object} ;
1414use anyhow:: { anyhow, Context , Error } ;
1515use futures03:: { future:: try_join, stream:: FuturesOrdered , TryStreamExt as _} ;
1616use itertools:: Itertools ;
1717use semver:: Version ;
18- use serde:: { de, ser} ;
18+ use serde:: {
19+ de:: { self , Visitor } ,
20+ ser,
21+ } ;
1922use serde_yaml;
2023use slog:: Logger ;
2124use stable_hash:: { FieldAddress , StableHash } ;
@@ -548,7 +551,83 @@ pub struct BaseSubgraphManifest<C, S, D, T> {
548551#[ derive( Debug , Deserialize ) ]
549552#[ serde( rename_all = "camelCase" ) ]
550553pub struct IndexerHints {
551- pub history_blocks : Option < BlockNumber > ,
554+ prune : Option < Prune > ,
555+ }
556+
557+ impl IndexerHints {
558+ pub fn history_blocks ( & self ) -> BlockNumber {
559+ match self . prune {
560+ Some ( ref hb) => hb. history_blocks ( ) ,
561+ None => BLOCK_NUMBER_MAX ,
562+ }
563+ }
564+ }
565+
566+ #[ derive( Debug ) ]
567+ pub enum Prune {
568+ Auto ,
569+ Never ,
570+ Blocks ( BlockNumber ) ,
571+ }
572+
573+ impl Prune {
574+ pub fn history_blocks ( & self ) -> BlockNumber {
575+ match self {
576+ Prune :: Never => BLOCK_NUMBER_MAX ,
577+ Prune :: Auto => ENV_VARS . min_history_blocks ,
578+ Prune :: Blocks ( x) => * x,
579+ }
580+ }
581+ }
582+
583+ impl < ' de > de:: Deserialize < ' de > for Prune {
584+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
585+ where
586+ D : de:: Deserializer < ' de > ,
587+ {
588+ struct HistoryBlocksVisitor ;
589+
590+ const ERROR_MSG : & str = "expected 'all', 'min', or a number for history blocks" ;
591+
592+ impl < ' de > Visitor < ' de > for HistoryBlocksVisitor {
593+ type Value = Prune ;
594+
595+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
596+ formatter. write_str ( "a string or an integer for history blocks" )
597+ }
598+
599+ fn visit_str < E > ( self , value : & str ) -> Result < Prune , E >
600+ where
601+ E : de:: Error ,
602+ {
603+ match value {
604+ "never" => Ok ( Prune :: Never ) ,
605+ "auto" => Ok ( Prune :: Auto ) ,
606+ _ => value
607+ . parse :: < i32 > ( )
608+ . map ( Prune :: Blocks )
609+ . map_err ( |_| E :: custom ( ERROR_MSG ) ) ,
610+ }
611+ }
612+
613+ fn visit_i32 < E > ( self , value : i32 ) -> Result < Prune , E >
614+ where
615+ E : de:: Error ,
616+ {
617+ Ok ( Prune :: Blocks ( value) )
618+ }
619+
620+ fn visit_u64 < E > ( self , v : u64 ) -> Result < Self :: Value , E >
621+ where
622+ E : de:: Error ,
623+ {
624+ let i = v. try_into ( ) . map_err ( |_| E :: custom ( ERROR_MSG ) ) ?;
625+ Ok ( Prune :: Blocks ( i) )
626+ }
627+ }
628+
629+ deserializer. deserialize_any ( HistoryBlocksVisitor )
630+ }
552631}
553632
554633/// SubgraphManifest with IPFS links unresolved
@@ -681,10 +760,11 @@ impl<C: Blockchain> SubgraphManifest<C> {
681760 . collect ( )
682761 }
683762
684- pub fn history_blocks ( & self ) -> Option < BlockNumber > {
685- self . indexer_hints
686- . as_ref ( )
687- . and_then ( |hints| hints. history_blocks )
763+ pub fn history_blocks ( & self ) -> BlockNumber {
764+ match self . indexer_hints {
765+ Some ( ref hints) => hints. history_blocks ( ) ,
766+ None => BLOCK_NUMBER_MAX ,
767+ }
688768 }
689769
690770 pub fn api_versions ( & self ) -> impl Iterator < Item = semver:: Version > + ' _ {
@@ -872,10 +952,10 @@ impl<C: Blockchain> UnresolvedSubgraphManifest<C> {
872952 ) ;
873953 }
874954
875- if spec_version < SPEC_VERSION_0_1_0 && indexer_hints. is_some ( ) {
955+ if spec_version < SPEC_VERSION_1_0_0 && indexer_hints. is_some ( ) {
876956 bail ! (
877957 "`indexerHints` are not supported prior to {}" ,
878- SPEC_VERSION_0_1_0
958+ SPEC_VERSION_1_0_0
879959 ) ;
880960 }
881961
0 commit comments