@@ -23,6 +23,14 @@ use crate::{subgraph_store::PRIMARY_SHARD, Shard};
2323#[ cfg( debug_assertions) ]
2424pub const FAKE_NETWORK_SHARED : & str = "fake_network_shared" ;
2525
26+ /// The status of a chain: whether we can only read from the chain, or
27+ /// whether it is ok to ingest from it, too
28+ #[ derive( Copy , Clone ) ]
29+ pub enum ChainStatus {
30+ ReadOnly ,
31+ Ingestible ,
32+ }
33+
2634mod primary {
2735 use std:: str:: FromStr ;
2836
@@ -214,57 +222,77 @@ impl BlockStore {
214222 }
215223 }
216224
225+ /// Check that the configuration for `chain` hasn't changed so that
226+ /// it is ok to ingest from it
227+ fn chain_ingestible (
228+ logger : & Logger ,
229+ chain : & primary:: Chain ,
230+ shard : & Shard ,
231+ ident : & Option < EthereumNetworkIdentifier > ,
232+ ) -> bool {
233+ if & chain. shard != shard {
234+ error ! (
235+ logger,
236+ "the chain {} is stored in shard {} but is configured for shard {}" ,
237+ chain. name,
238+ chain. shard,
239+ shard
240+ ) ;
241+ return false ;
242+ }
243+ if let Some ( ident) = ident {
244+ if chain. net_version != ident. net_version {
245+ error ! ( logger,
246+ "the net version for chain {} has changed from {} to {} since the last time we ran" ,
247+ chain. name,
248+ chain. net_version,
249+ ident. net_version
250+ ) ;
251+ return false ;
252+ }
253+ if & chain. genesis_block != & format ! ( "{:x}" , ident. genesis_block_hash) {
254+ error ! ( logger,
255+ "the genesis block hash for chain {} has changed from {} to {:x} since the last time we ran" ,
256+ chain. name,
257+ chain. genesis_block,
258+ ident. genesis_block_hash
259+ ) ;
260+ return false ;
261+ }
262+ }
263+ return true ;
264+ }
265+
217266 // For each configured chain, add a chain store
218267 for ( chain_name, idents, shard) in chains {
219268 let ident = reduce_idents ( & chain_name, idents) ?;
220- let ( chain , create ) = match (
269+ match (
221270 existing_chains
222271 . iter ( )
223272 . find ( |chain| chain. name == chain_name) ,
224273 ident,
225274 ) {
226275 ( Some ( chain) , ident) => {
227- if chain. shard != shard {
228- return Err ( StoreError :: Unknown ( anyhow ! (
229- "the chain {} is stored in shard {} but is configured for shard {}" ,
230- chain. name,
231- chain. shard,
232- shard
233- ) ) ) ;
234- }
235- if let Some ( ident) = ident {
236- if chain. net_version != ident. net_version {
237- return Err ( StoreError :: Unknown ( anyhow ! (
238- "the net version for chain {} has changed from {} to {} since the last time we ran" ,
239- chain. name,
240- chain. net_version,
241- ident. net_version
242- ) ) ) ;
243- }
244- if & chain. genesis_block != & format ! ( "{:x}" , ident. genesis_block_hash) {
245- return Err ( StoreError :: Unknown ( anyhow ! (
246- "the genesis block hash for chain {} has changed from {} to {:x} since the last time we ran" ,
247- chain. name,
248- chain. genesis_block,
249- ident. genesis_block_hash
250- ) ) ) ;
251- }
252- }
253- ( chain. clone ( ) , false )
276+ let status = if chain_ingestible ( & block_store. logger , chain, & shard, & ident) {
277+ ChainStatus :: Ingestible
278+ } else {
279+ ChainStatus :: ReadOnly
280+ } ;
281+ block_store. add_chain_store ( & chain, status, false ) ?;
282+ }
283+ ( None , Some ( ident) ) => {
284+ let chain =
285+ primary:: add_chain ( & block_store. primary , & chain_name, & ident, & shard) ?;
286+ block_store. add_chain_store ( & chain, ChainStatus :: Ingestible , true ) ?;
254287 }
255- ( None , Some ( ident) ) => (
256- primary:: add_chain ( & block_store. primary , & chain_name, & ident, & shard) ?,
257- true ,
258- ) ,
259288 ( None , None ) => {
260- return Err ( StoreError :: Unknown ( anyhow ! (
289+ error ! (
290+ & block_store. logger,
261291 " the chain {} is new but we could not get a network identifier for it" ,
262292 chain_name
263- ) ) ) ;
293+ ) ;
264294 }
265295 } ;
266-
267- block_store. add_chain_store ( & chain, create) ?;
268296 }
269297
270298 // There might be chains we have in the database that are not yet/
@@ -280,14 +308,15 @@ impl BlockStore {
280308 . iter ( )
281309 . filter ( |chain| !configured_chains. contains ( & chain. name ) )
282310 {
283- block_store. add_chain_store ( & chain, false ) ?;
311+ block_store. add_chain_store ( & chain, ChainStatus :: ReadOnly , false ) ?;
284312 }
285313 Ok ( block_store)
286314 }
287315
288316 fn add_chain_store (
289317 & self ,
290318 chain : & primary:: Chain ,
319+ status : ChainStatus ,
291320 create : bool ,
292321 ) -> Result < Arc < ChainStore > , StoreError > {
293322 let pool = self
@@ -301,6 +330,7 @@ impl BlockStore {
301330 chain. name . clone ( ) ,
302331 chain. storage . clone ( ) ,
303332 & ident,
333+ status,
304334 self . chain_head_update_listener . clone ( ) ,
305335 sender,
306336 pool,
@@ -343,7 +373,7 @@ impl BlockStore {
343373 // of the configured chains
344374 let conn = self . primary . get ( ) ?;
345375 primary:: find_chain ( & conn, chain) ?
346- . map ( |chain| self . add_chain_store ( & chain, false ) )
376+ . map ( |chain| self . add_chain_store ( & chain, ChainStatus :: ReadOnly , false ) )
347377 . transpose ( )
348378 }
349379
0 commit comments