Skip to content

Commit f052e08

Browse files
author
Ludo Galabru
committed
feat: update chainhook-sdk
1 parent f1a3c85 commit f052e08

File tree

4 files changed

+15
-146
lines changed

4 files changed

+15
-146
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/hord-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ redis = "0.21.5"
1414
serde-redis = "0.12.0"
1515
hex = "0.4.3"
1616
rand = "0.8.5"
17-
chainhook-sdk = { version = "=0.7.4", default-features = false, features = ["zeromq"] }
17+
chainhook-sdk = { version = "=0.7.5", default-features = false, features = ["zeromq"] }
1818
hiro-system-kit = "0.1.0"
1919
clap = { version = "3.2.23", features = ["derive"], optional = true }
2020
clap_generate = { version = "3.0.3", optional = true }

components/hord-cli/src/db/mod.rs

+6-140
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::{
88
};
99

1010
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+
},
1215
types::{
1316
BitcoinBlockData, BlockIdentifier, OrdinalInscriptionRevealData,
1417
OrdinalInscriptionTransferData, TransactionIdentifier,
@@ -917,10 +920,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
917920
let (block_compressed_tx, block_compressed_rx) = crossbeam_channel::bounded(block_process_lim);
918921

919922
// 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();
924924

925925
for block_cursor in start_block..=end_block {
926926
let block_height = block_cursor.clone();
@@ -1878,11 +1878,7 @@ pub async fn rebuild_rocks_db(
18781878
let (block_data_tx, block_data_rx) = crossbeam_channel::bounded(block_req_lim);
18791879
let compress_block_data_pool = ThreadPool::new(hord_config.ingestion_thread_max);
18801880
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();
18861882

18871883
// Thread pool #1: given a block height, retrieve the block hash
18881884
for block_cursor in start_block..=end_block {
@@ -2030,133 +2026,3 @@ pub async fn rebuild_rocks_db(
20302026

20312027
Ok(())
20322028
}
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-
}

components/hord-cli/src/scan/bitcoin.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use chainhook_sdk::chainhooks::bitcoin::{
2020
};
2121
use chainhook_sdk::chainhooks::types::{BitcoinChainhookSpecification, BitcoinPredicateType};
2222
use chainhook_sdk::indexer::bitcoin::{
23-
download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
23+
build_http_client, download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
2424
};
2525
use chainhook_sdk::observer::{gather_proofs, EventObserverConfig};
2626
use chainhook_sdk::types::{
@@ -95,6 +95,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
9595
let event_observer_config = config.get_event_observer_config();
9696
let bitcoin_config = event_observer_config.get_bitcoin_config();
9797
let mut traversals = HashMap::new();
98+
let http_client = build_http_client();
9899

99100
let mut cursor = start_block.saturating_sub(1);
100101

@@ -108,9 +109,11 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
108109
}
109110
}
110111

111-
let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
112+
let block_hash =
113+
retrieve_block_hash_with_retry(&http_client, &cursor, &bitcoin_config, ctx).await?;
112114
let block_breakdown =
113-
download_and_parse_block_with_retry(&block_hash, &bitcoin_config, ctx).await?;
115+
download_and_parse_block_with_retry(&http_client, &block_hash, &bitcoin_config, ctx)
116+
.await?;
114117
let mut block = match hord::parse_ordinals_and_standardize_block(
115118
block_breakdown,
116119
&event_observer_config.bitcoin_network,

0 commit comments

Comments
 (0)