@@ -3,7 +3,7 @@ use diesel::pg::Pg;
33use diesel:: pg:: PgConnection ;
44use diesel:: prelude:: * ;
55use diesel:: sql_types:: Text ;
6- use diesel:: { debug_query, delete, insert_into, result, select} ;
6+ use diesel:: { debug_query, delete, insert_into, result, select, update } ;
77use filter:: store_filter;
88use futures:: sync:: mpsc:: { channel, Sender } ;
99use std:: collections:: HashMap ;
@@ -330,17 +330,68 @@ impl BasicStore for Store {
330330}
331331
332332impl BlockStore for Store {
333- fn add_network_if_missing ( & self , network_name : & str ) -> Result < ( ) , Error > {
333+ fn add_network_if_missing (
334+ & self ,
335+ new_network_name : & str ,
336+ new_net_version : & str ,
337+ new_genesis_block_hash : H256 ,
338+ ) -> Result < ( ) , Error > {
334339 use db_schema:: ethereum_networks:: dsl:: * ;
335340
336- insert_into ( ethereum_networks)
337- . values ( (
338- name. eq ( network_name) ,
339- head_block_hash. eq :: < Option < String > > ( None ) ,
340- head_block_number. eq :: < Option < i64 > > ( None ) ,
341- ) ) . on_conflict ( name)
342- . do_nothing ( )
343- . execute ( & * self . conn . lock ( ) . unwrap ( ) ) ?;
341+ let network_identifiers_opt = ethereum_networks
342+ . select ( ( net_version, genesis_block_hash) )
343+ . filter ( name. eq ( new_network_name) )
344+ . first :: < ( Option < String > , Option < String > ) > ( & * self . conn . lock ( ) . unwrap ( ) )
345+ . optional ( ) ?;
346+
347+ match network_identifiers_opt {
348+ // Network is missing in database
349+ None => {
350+ insert_into ( ethereum_networks)
351+ . values ( (
352+ name. eq ( new_network_name) ,
353+ head_block_hash. eq :: < Option < String > > ( None ) ,
354+ head_block_number. eq :: < Option < i64 > > ( None ) ,
355+ net_version. eq :: < Option < String > > ( Some ( new_net_version. to_owned ( ) ) ) ,
356+ genesis_block_hash
357+ . eq :: < Option < String > > ( Some ( format ! ( "{:x}" , new_genesis_block_hash) ) ) ,
358+ ) ) . on_conflict ( name)
359+ . do_nothing ( )
360+ . execute ( & * self . conn . lock ( ) . unwrap ( ) ) ?;
361+ }
362+
363+ // Network is in database and has identifiers
364+ Some ( ( Some ( last_net_version) , Some ( last_genesis_block_hash) ) ) => {
365+ if last_net_version != new_net_version {
366+ panic ! (
367+ "Ethereum node provided net_version {}, \
368+ but we expected {}. Did you change networks \
369+ without changing the network name?",
370+ new_net_version, last_net_version
371+ ) ;
372+ }
373+
374+ if last_genesis_block_hash. parse ( ) . ok ( ) != Some ( new_genesis_block_hash) {
375+ panic ! (
376+ "Ethereum node provided genesis block hash {}, \
377+ but we expected {}. Did you change networks \
378+ without changing the network name?",
379+ new_genesis_block_hash, last_genesis_block_hash
380+ ) ;
381+ }
382+ }
383+
384+ // Network is in database but is missing identifiers
385+ Some ( _) => {
386+ update ( ethereum_networks)
387+ . set ( (
388+ net_version. eq :: < Option < String > > ( Some ( new_net_version. to_owned ( ) ) ) ,
389+ genesis_block_hash
390+ . eq :: < Option < String > > ( Some ( format ! ( "{:x}" , new_genesis_block_hash) ) ) ,
391+ ) ) . filter ( name. eq ( new_network_name) )
392+ . execute ( & * self . conn . lock ( ) . unwrap ( ) ) ?;
393+ }
394+ }
344395
345396 Ok ( ( ) )
346397 }
0 commit comments