@@ -10,13 +10,16 @@ use diesel::{
1010 r2d2:: { ConnectionManager , PooledConnection } ,
1111 sql_query, ExpressionMethods as _, PgConnection , RunQueryDsl ,
1212} ;
13- use graph:: components:: network_provider:: ChainName ;
1413use graph:: {
1514 blockchain:: ChainIdentifier ,
1615 components:: store:: { BlockStore as BlockStoreTrait , QueryPermit } ,
1716 prelude:: { error, info, BlockNumber , BlockPtr , Logger , ENV_VARS } ,
1817 slog:: o,
1918} ;
19+ use graph:: {
20+ components:: { network_provider:: ChainName , store:: ChainIdStore } ,
21+ prelude:: ChainStore as _,
22+ } ;
2023use graph:: { internal_error, prelude:: CheapClone } ;
2124use graph:: { prelude:: StoreError , util:: timed_cache:: TimedCache } ;
2225
@@ -605,3 +608,41 @@ impl BlockStoreTrait for BlockStore {
605608 . map_err ( anyhow:: Error :: from)
606609 }
607610}
611+
612+ impl ChainIdStore for BlockStore {
613+ fn chain_identifier ( & self , chain_name : & ChainName ) -> Result < ChainIdentifier , anyhow:: Error > {
614+ let chain_store = self
615+ . chain_store ( & chain_name)
616+ . ok_or_else ( || anyhow ! ( "unable to get store for chain '{chain_name}'" ) ) ?;
617+
618+ chain_store. chain_identifier ( )
619+ }
620+
621+ fn set_chain_identifier (
622+ & self ,
623+ chain_name : & ChainName ,
624+ ident : & ChainIdentifier ,
625+ ) -> Result < ( ) , anyhow:: Error > {
626+ use primary:: chains as c;
627+
628+ // Update the block shard first since that contains a copy from the primary
629+ let chain_store = self
630+ . chain_store ( & chain_name)
631+ . ok_or_else ( || anyhow ! ( "unable to get store for chain '{chain_name}'" ) ) ?;
632+
633+ chain_store. set_chain_identifier ( ident) ?;
634+
635+ // Update the master copy in the primary
636+ let primary_pool = self . pools . get ( & * PRIMARY_SHARD ) . unwrap ( ) ;
637+ let mut conn = primary_pool. get ( ) ?;
638+
639+ diesel:: update ( c:: table. filter ( c:: name. eq ( chain_name. as_str ( ) ) ) )
640+ . set ( (
641+ c:: genesis_block_hash. eq ( ident. genesis_block_hash . hash_hex ( ) ) ,
642+ c:: net_version. eq ( & ident. net_version ) ,
643+ ) )
644+ . execute ( & mut conn) ?;
645+
646+ Ok ( ( ) )
647+ }
648+ }
0 commit comments