@@ -29,6 +29,7 @@ use shared::block::Block;
2929use shared:: block_result:: BlockResult ;
3030use shared:: checksums:: Checksums ;
3131use shared:: client:: Client ;
32+ use shared:: cometbft:: CometbftBlock ;
3233use shared:: crawler:: crawl;
3334use shared:: crawler_state:: ChainCrawlerState ;
3435use shared:: error:: {
@@ -277,8 +278,12 @@ async fn crawling_fn(
277278 let native_token_address: namada_sdk:: address:: Address =
278279 native_token. clone ( ) . into ( ) ;
279280
281+ let cometbft_block =
282+ get_cometbft_block_with_fallback ( & conn, & client, block_height)
283+ . await
284+ . into_db_error ( ) ?;
280285 let ( block, tm_block_response, epoch) =
281- get_block ( block_height , & client, & checksums, & native_token_address)
286+ get_block ( cometbft_block , & client, & checksums, & native_token_address)
282287 . await ?;
283288
284289 let rate_limits = new_epoch. then ( || {
@@ -654,8 +659,14 @@ async fn try_initial_query(
654659 . await
655660 . into_rpc_error ( ) ?
656661 . into ( ) ;
662+
663+ let cometbft_block =
664+ get_cometbft_block_with_fallback ( conn, client, block_height)
665+ . await
666+ . into_db_error ( ) ?;
667+
657668 let ( block, tm_block_response, epoch) =
658- get_block ( block_height , client, & checksums, & native_token) . await ?;
669+ get_block ( cometbft_block , client, & checksums, & native_token) . await ?;
659670
660671 let tokens = query_tokens ( client) . await . into_rpc_error ( ) ?;
661672
@@ -846,36 +857,18 @@ async fn update_crawler_timestamp(
846857}
847858
848859async fn get_block (
849- block_height : u32 ,
860+ block : CometbftBlock ,
850861 client : & HttpClient ,
851862 checksums : & Checksums ,
852863 native_token : & namada_sdk:: address:: Address ,
853864) -> Result < ( Block , TendermintBlockResponse , u32 ) , MainError > {
854- tracing:: debug!( block = block_height, "Query block..." ) ;
855- let tm_block_response =
856- tendermint_service:: query_raw_block_at_height ( client, block_height)
857- . await
858- . into_rpc_error ( ) ?;
859- tracing:: debug!(
860- block = block_height,
861- "Raw block contains {} txs..." ,
862- tm_block_response. block. data. len( )
863- ) ;
865+ let block_height = block. block_height ;
864866
865- tracing:: debug!( block = block_height, "Query block results..." ) ;
866- let tm_block_results_response =
867- tendermint_service:: query_raw_block_results_at_height (
868- client,
869- block_height,
870- )
871- . await
872- . into_rpc_error ( ) ?;
873- let block_results = BlockResult :: from ( tm_block_results_response) ;
867+ let tm_block_response = block. block ;
868+ let tm_block_results_response = block. events ;
869+ let epoch = block. epoch ;
874870
875- tracing:: debug!( block = block_height, "Query epoch..." ) ;
876- let epoch = namada_service:: get_epoch_at_block_height ( client, block_height)
877- . await
878- . into_rpc_error ( ) ?;
871+ let block_results = BlockResult :: from ( tm_block_results_response) ;
879872
880873 let proposer_address_namada = namada_service:: get_validator_namada_address (
881874 client,
@@ -939,3 +932,46 @@ async fn query_token_supplies(
939932
940933 Ok ( buffer)
941934}
935+
936+ pub async fn get_cometbft_block_with_fallback (
937+ conn : & Object ,
938+ client : & HttpClient ,
939+ block_height : u32 ,
940+ ) -> anyhow:: Result < CometbftBlock > {
941+ let block = repository:: cometbft:: get_block ( conn, block_height)
942+ . await
943+ . context ( "Failed to get block" ) ?;
944+
945+ let block = match block {
946+ Some ( block) => block,
947+ None => {
948+ let block = tendermint_service:: query_raw_block_at_height (
949+ client,
950+ block_height,
951+ )
952+ . await
953+ . context ( "Failed to query block" ) ?;
954+
955+ let events = tendermint_service:: query_raw_block_results_at_height (
956+ client,
957+ block_height,
958+ )
959+ . await
960+ . context ( "Failed to query block results" ) ?;
961+
962+ let epoch =
963+ namada_service:: get_epoch_at_block_height ( client, block_height)
964+ . await
965+ . context ( "Failed to query epoch" ) ?;
966+
967+ CometbftBlock {
968+ block_height,
969+ block,
970+ events,
971+ epoch,
972+ }
973+ }
974+ } ;
975+
976+ Ok ( block)
977+ }
0 commit comments