@@ -3,14 +3,20 @@ use std::sync::Arc;
33use diesel:: sql_query;
44use diesel:: Connection ;
55use diesel:: RunQueryDsl ;
6+ use graph:: blockchain:: BlockHash ;
67use graph:: blockchain:: BlockPtr ;
8+ use graph:: blockchain:: ChainIdentifier ;
79use graph:: cheap_clone:: CheapClone ;
10+ use graph:: components:: adapter:: ChainId ;
11+ use graph:: components:: adapter:: IdentValidator ;
812use graph:: components:: store:: StoreError ;
913use graph:: prelude:: BlockNumber ;
1014use graph:: prelude:: ChainStore as _;
1115use graph:: prelude:: { anyhow, anyhow:: bail} ;
16+ use graph:: slog:: Logger ;
1217use graph:: { components:: store:: BlockStore as _, prelude:: anyhow:: Error } ;
1318use graph_store_postgres:: add_chain;
19+ use graph_store_postgres:: connection_pool:: PoolCoordinator ;
1420use graph_store_postgres:: find_chain;
1521use graph_store_postgres:: update_chain_name;
1622use graph_store_postgres:: BlockStore ;
@@ -21,6 +27,8 @@ use graph_store_postgres::{
2127 command_support:: catalog:: block_store, connection_pool:: ConnectionPool ,
2228} ;
2329
30+ use crate :: network_setup:: Networks ;
31+
2432pub async fn list ( primary : ConnectionPool , store : Arc < BlockStore > ) -> Result < ( ) , Error > {
2533 let mut chains = {
2634 let mut conn = primary. get ( ) ?;
@@ -148,6 +156,52 @@ pub fn remove(primary: ConnectionPool, store: Arc<BlockStore>, name: String) ->
148156 Ok ( ( ) )
149157}
150158
159+ pub async fn update_chain_genesis (
160+ networks : & Networks ,
161+ coord : Arc < PoolCoordinator > ,
162+ store : Arc < BlockStore > ,
163+ logger : & Logger ,
164+ chain_id : ChainId ,
165+ genesis_hash : BlockHash ,
166+ force : bool ,
167+ ) -> Result < ( ) , Error > {
168+ let ident = networks. chain_identifier ( logger, & chain_id) . await ?;
169+ if !genesis_hash. eq ( & ident. genesis_block_hash ) {
170+ println ! (
171+ "Expected adapter for chain {} to return genesis hash {} but got {}" ,
172+ chain_id, genesis_hash, ident. genesis_block_hash
173+ ) ;
174+ if !force {
175+ println ! ( "Not performing update" ) ;
176+ return Ok ( ( ) ) ;
177+ } else {
178+ println ! ( "--force used, updating anyway" ) ;
179+ }
180+ }
181+
182+ println ! ( "Updating shard..." ) ;
183+ // Update the local shard's genesis, whether or not it is the primary.
184+ // The chains table is replicated from the primary and keeps another genesis hash.
185+ // To keep those in sync we need to update the primary and then refresh the shard tables.
186+ store. update_ident (
187+ & chain_id,
188+ & ChainIdentifier {
189+ net_version : ident. net_version . clone ( ) ,
190+ genesis_block_hash : genesis_hash,
191+ } ,
192+ ) ?;
193+
194+ // Update the primary public.chains
195+ println ! ( "Updating primary public.chains" ) ;
196+ store. set_chain_identifier ( chain_id, & ident) ?;
197+
198+ // Refresh the new values
199+ println ! ( "Refresh mappings" ) ;
200+ crate :: manager:: commands:: database:: remap ( & coord, None , None , false ) . await ?;
201+
202+ Ok ( ( ) )
203+ }
204+
151205pub fn change_block_cache_shard (
152206 primary_store : ConnectionPool ,
153207 store : Arc < BlockStore > ,
0 commit comments