@@ -669,7 +669,7 @@ mod data {
669669 let pointers_query = if overwrite {
670670 format ! (
671671 "insert into {pointers_table}(hash, number, parent_hash, timestamp) \
672- values ($1, $2, $3, $5 ) \
672+ values ($1, $2, $3, $4 ) \
673673 on conflict(hash) \
674674 do update set number = $2, parent_hash = $3, timestamp = $5;
675675 " ,
@@ -678,7 +678,7 @@ mod data {
678678 } else {
679679 format ! (
680680 "insert into {pointers_table}(hash, number, parent_hash, timestamp) \
681- values ($1, $2, $3, $5 ) \
681+ values ($1, $2, $3, $4 ) \
682682 on conflict(hash) do nothing;
683683 " ,
684684 pointers_table = block_pointers. qname,
@@ -688,15 +688,15 @@ mod data {
688688 let blocks_query = if overwrite {
689689 format ! (
690690 "insert into {blocks_table}(hash, data) \
691- values ($1, $4 ) \
691+ values ($1, $2 ) \
692692 on conflict(hash) \
693- do update set data = $4 ;",
693+ do update set data = $2 ;",
694694 blocks_table = blocks. qname,
695695 )
696696 } else {
697697 format ! (
698698 "insert into {blocks_table}(hash, data) \
699- values ($1, $4 ) \
699+ values ($1, $2 ) \
700700 on conflict(hash) do nothing;",
701701 blocks_table = blocks. qname,
702702 )
@@ -705,22 +705,18 @@ mod data {
705705 conn. transaction ( move |conn| {
706706 let data = data;
707707 sql_query ( blocks_query)
708+ . bind :: < Bytea , _ > ( hash. as_slice ( ) )
709+ . bind :: < Jsonb , _ > ( & data)
710+ . execute ( conn)
711+ . map_err ( StoreError :: from) ?;
712+
713+ sql_query ( pointers_query)
708714 . bind :: < Bytea , _ > ( hash. as_slice ( ) )
709715 . bind :: < BigInt , _ > ( number)
710716 . bind :: < Bytea , _ > ( parent_hash. as_slice ( ) )
711- . bind :: < Jsonb , _ > ( & data )
717+ . bind :: < Timestamptz , _ > ( & block . timestamp ( ) )
712718 . execute ( conn)
713719 . map_err ( StoreError :: from)
714- . and_then ( |_| {
715- sql_query ( pointers_query)
716- . bind :: < Bytea , _ > ( hash. as_slice ( ) )
717- . bind :: < BigInt , _ > ( number)
718- . bind :: < Bytea , _ > ( parent_hash. as_slice ( ) )
719- . bind :: < Jsonb , _ > ( data)
720- . bind :: < Timestamptz , _ > ( & block. timestamp ( ) )
721- . execute ( conn)
722- . map_err ( StoreError :: from)
723- } )
724720 } ) ?;
725721 }
726722 } ;
@@ -894,16 +890,41 @@ mod data {
894890 . execute ( conn)
895891 . map_err ( Error :: from)
896892 }
897- Storage :: Private ( Schema { block_pointers, .. } ) => {
898- let query = format ! (
893+ Storage :: Private ( Schema {
894+ block_pointers,
895+ blocks,
896+ ..
897+ } ) => {
898+ let blocks_query = format ! (
899+ "
900+ DELETE FROM {block_table}
901+ WHERE hash in (
902+ SELECT hash
903+ FROM {ptrs_table}
904+ WHERE number = $1 AND hash != $2
905+ )
906+ " ,
907+ block_table = blocks. qname,
908+ ptrs_table = block_pointers. qname
909+ ) ;
910+ let ptrs_query = format ! (
899911 "delete from {} where number = $1 and hash != $2" ,
900912 block_pointers. qname
901913 ) ;
902- sql_query ( query)
903- . bind :: < BigInt , _ > ( number)
904- . bind :: < Bytea , _ > ( hash. as_slice ( ) )
905- . execute ( conn)
906- . map_err ( Error :: from)
914+
915+ conn. transaction ( |conn| {
916+ sql_query ( blocks_query)
917+ . bind :: < BigInt , _ > ( number)
918+ . bind :: < Bytea , _ > ( hash. as_slice ( ) )
919+ . execute ( conn)
920+ . map_err ( Error :: from) ?;
921+
922+ sql_query ( ptrs_query)
923+ . bind :: < BigInt , _ > ( number)
924+ . bind :: < Bytea , _ > ( hash. as_slice ( ) )
925+ . execute ( conn)
926+ . map_err ( Error :: from)
927+ } )
907928 }
908929 }
909930 }
@@ -971,12 +992,7 @@ mod data {
971992 Some ( ( number, ts, parent_hash) ) => {
972993 let number = BlockNumber :: try_from ( number)
973994 . map_err ( |e| StoreError :: QueryExecutionError ( e. to_string ( ) ) ) ?;
974- Ok ( Some ( (
975- number,
976- ts,
977- // crate::chain_store::try_parse_timestamp(ts)?,
978- parent_hash. map ( |h| BlockHash :: from ( h) ) ,
979- ) ) )
995+ Ok ( Some ( ( number, ts, parent_hash. map ( |h| BlockHash :: from ( h) ) ) ) )
980996 }
981997 }
982998 }
@@ -1006,14 +1022,11 @@ mod data {
10061022 } )
10071023 . collect :: < Vec < _ > > ( )
10081024 }
1009- Storage :: Private ( Schema { block_pointers, .. } ) => {
1010- // let hashes: Vec<_> = hashes.into_iter().map(|hash| &hash.0).collect();
1011- block_pointers
1012- . table ( )
1013- . select ( ( block_pointers. hash ( ) , block_pointers. number ( ) ) )
1014- . filter ( block_pointers. hash ( ) . eq_any ( hashes) )
1015- . load :: < ( BlockHash , i64 ) > ( conn) ?
1016- }
1025+ Storage :: Private ( Schema { block_pointers, .. } ) => block_pointers
1026+ . table ( )
1027+ . select ( ( block_pointers. hash ( ) , block_pointers. number ( ) ) )
1028+ . filter ( block_pointers. hash ( ) . eq_any ( hashes) )
1029+ . load :: < ( BlockHash , i64 ) > ( conn) ?,
10171030 } ;
10181031
10191032 let pairs = pairs
@@ -1403,7 +1416,8 @@ mod data {
14031416 "
14041417 DELETE FROM {blocks}
14051418 WHERE hash in (
1406- SELECT FROM {block_ptrs}
1419+ SELECT hash
1420+ FROM {block_ptrs}
14071421 WHERE hash = any($1) AND number > 0;
14081422 );
14091423 DELETE FROM {block_ptrs} WHERE hash = any($1) AND number > 0;
@@ -1998,7 +2012,7 @@ impl ChainStore {
19982012 Ok ( ( ) )
19992013 }
20002014
2001- pub ( crate ) fn ensure_up_to_date ( & self , _ident : & ChainIdentifier ) -> Result < ( ) , Error > {
2015+ pub ( crate ) fn migrate ( & self , _ident : & ChainIdentifier ) -> Result < ( ) , Error > {
20022016 let mut conn = self . get_conn ( ) ?;
20032017 // no version (which implicitly is version 1) to version 2 upgrade.
20042018 self . storage . split_block_cache_update ( & mut conn) ?;
@@ -2683,7 +2697,7 @@ impl ChainStoreTrait for ChainStore {
26832697 . confirm_block_hash ( & mut conn, & self . chain , number, hash)
26842698 }
26852699
2686- async fn block_number (
2700+ async fn block_pointer (
26872701 & self ,
26882702 hash : & BlockHash ,
26892703 ) -> Result < Option < ( String , BlockNumber , Option < BlockTime > , Option < BlockHash > ) > , StoreError >
0 commit comments