@@ -8,7 +8,10 @@ use std::{
8
8
} ;
9
9
10
10
use chainhook_sdk:: {
11
- indexer:: bitcoin:: parse_downloaded_block,
11
+ indexer:: bitcoin:: {
12
+ build_http_client, download_block_with_retry, parse_downloaded_block,
13
+ retrieve_block_hash_with_retry,
14
+ } ,
12
15
types:: {
13
16
BitcoinBlockData , BlockIdentifier , OrdinalInscriptionRevealData ,
14
17
OrdinalInscriptionTransferData , TransactionIdentifier ,
@@ -917,10 +920,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
917
920
let ( block_compressed_tx, block_compressed_rx) = crossbeam_channel:: bounded ( block_process_lim) ;
918
921
919
922
// Thread pool #1: given a block height, retrieve the block hash
920
- let http_client: HttpClient = HttpClient :: builder ( )
921
- . timeout ( Duration :: from_secs ( 20 ) )
922
- . build ( )
923
- . expect ( "Unable to build http client" ) ;
923
+ let http_client = build_http_client ( ) ;
924
924
925
925
for block_cursor in start_block..=end_block {
926
926
let block_height = block_cursor. clone ( ) ;
@@ -1878,11 +1878,7 @@ pub async fn rebuild_rocks_db(
1878
1878
let ( block_data_tx, block_data_rx) = crossbeam_channel:: bounded ( block_req_lim) ;
1879
1879
let compress_block_data_pool = ThreadPool :: new ( hord_config. ingestion_thread_max ) ;
1880
1880
let ( block_compressed_tx, block_compressed_rx) = crossbeam_channel:: bounded ( block_process_lim) ;
1881
- let http_client: HttpClient = HttpClient :: builder ( )
1882
- . timeout ( Duration :: from_secs ( 20 ) )
1883
- . danger_accept_invalid_certs ( true )
1884
- . build ( )
1885
- . expect ( "Unable to build http client" ) ;
1881
+ let http_client = build_http_client ( ) ;
1886
1882
1887
1883
// Thread pool #1: given a block height, retrieve the block hash
1888
1884
for block_cursor in start_block..=end_block {
@@ -2030,133 +2026,3 @@ pub async fn rebuild_rocks_db(
2030
2026
2031
2027
Ok ( ( ) )
2032
2028
}
2033
-
2034
- use reqwest:: Client as HttpClient ;
2035
-
2036
- pub async fn retrieve_block_hash_with_retry (
2037
- http_client : & HttpClient ,
2038
- block_height : & u64 ,
2039
- bitcoin_config : & BitcoinConfig ,
2040
- ctx : & Context ,
2041
- ) -> Result < String , String > {
2042
- let mut errors_count = 0 ;
2043
- let block_hash = loop {
2044
- match retrieve_block_hash ( http_client, block_height, bitcoin_config, ctx) . await {
2045
- Ok ( result) => break result,
2046
- Err ( _e) => {
2047
- errors_count += 1 ;
2048
- if errors_count > 3 {
2049
- ctx. try_log ( |logger| {
2050
- slog:: warn!(
2051
- logger,
2052
- "unable to retrieve block hash #{block_height}: will retry in a few seconds (attempt #{errors_count})." ,
2053
- )
2054
- } ) ;
2055
- }
2056
- std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ;
2057
- }
2058
- }
2059
- } ;
2060
- Ok ( block_hash)
2061
- }
2062
-
2063
- pub async fn retrieve_block_hash (
2064
- http_client : & HttpClient ,
2065
- block_height : & u64 ,
2066
- bitcoin_config : & BitcoinConfig ,
2067
- _ctx : & Context ,
2068
- ) -> Result < String , String > {
2069
- let body = json ! ( {
2070
- "jsonrpc" : "1.0" ,
2071
- "id" : "chainhook-cli" ,
2072
- "method" : "getblockhash" ,
2073
- "params" : [ block_height]
2074
- } ) ;
2075
- let block_hash = http_client
2076
- . post ( & bitcoin_config. rpc_url )
2077
- . basic_auth ( & bitcoin_config. username , Some ( & bitcoin_config. password ) )
2078
- . header ( "Content-Type" , "application/json" )
2079
- . header ( "Host" , & bitcoin_config. rpc_url [ 7 ..] )
2080
- . json ( & body)
2081
- . send ( )
2082
- . await
2083
- . map_err ( |e| format ! ( "unable to send request ({})" , e) ) ?
2084
- . json :: < chainhook_sdk:: bitcoincore_rpc:: jsonrpc:: Response > ( )
2085
- . await
2086
- . map_err ( |e| format ! ( "unable to parse response ({})" , e) ) ?
2087
- . result :: < String > ( )
2088
- . map_err ( |e| format ! ( "unable to parse response ({})" , e) ) ?;
2089
-
2090
- Ok ( block_hash)
2091
- }
2092
-
2093
- pub async fn download_block_with_retry (
2094
- http_client : & HttpClient ,
2095
- block_hash : & str ,
2096
- bitcoin_config : & BitcoinConfig ,
2097
- ctx : & Context ,
2098
- ) -> Result < BitcoinBlockFullBreakdown , String > {
2099
- let mut errors_count = 0 ;
2100
- let block = loop {
2101
- let response = {
2102
- match download_block ( http_client, block_hash, bitcoin_config, ctx) . await {
2103
- Ok ( result) => result,
2104
- Err ( _e) => {
2105
- errors_count += 1 ;
2106
- ctx. try_log ( |logger| {
2107
- slog:: warn!(
2108
- logger,
2109
- "unable to fetch block #{block_hash}: will retry in a few seconds (attempt #{errors_count})." ,
2110
- )
2111
- } ) ;
2112
- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 500 ) ) ;
2113
- continue ;
2114
- }
2115
- }
2116
- } ;
2117
-
2118
- match parse_downloaded_block ( response) {
2119
- Ok ( result) => break result,
2120
- Err ( e) => {
2121
- errors_count += 1 ;
2122
- ctx. try_log ( |logger| {
2123
- slog:: warn!(
2124
- logger,
2125
- "unable to parse fetched block #{block_hash}: will retry in a few seconds (attempt #{errors_count}) ({e})" ,
2126
- )
2127
- } ) ;
2128
- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 500 ) ) ;
2129
- continue ;
2130
- }
2131
- } ;
2132
- } ;
2133
- Ok ( block)
2134
- }
2135
-
2136
- pub async fn download_block (
2137
- http_client : & HttpClient ,
2138
- block_hash : & str ,
2139
- bitcoin_config : & BitcoinConfig ,
2140
- _ctx : & Context ,
2141
- ) -> Result < Vec < u8 > , String > {
2142
- let body = json ! ( {
2143
- "jsonrpc" : "1.0" ,
2144
- "id" : "chainhook-cli" ,
2145
- "method" : "getblock" ,
2146
- "params" : [ block_hash, 3 ]
2147
- } ) ;
2148
- let block = http_client
2149
- . post ( & bitcoin_config. rpc_url )
2150
- . basic_auth ( & bitcoin_config. username , Some ( & bitcoin_config. password ) )
2151
- . header ( "Content-Type" , "application/json" )
2152
- . header ( "Host" , & bitcoin_config. rpc_url [ 7 ..] )
2153
- . json ( & body)
2154
- . send ( )
2155
- . await
2156
- . map_err ( |e| format ! ( "unable to send request ({})" , e) ) ?
2157
- . bytes ( )
2158
- . await
2159
- . map_err ( |e| format ! ( "unable to get bytes ({})" , e) ) ?
2160
- . to_vec ( ) ;
2161
- Ok ( block)
2162
- }
0 commit comments