Skip to content

Commit

Permalink
feat: detect http / rpc errors as early as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Mar 1, 2024
1 parent 384f94a commit e515116
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions components/chainhook-sdk/src/indexer/bitcoin/mod.rs
Expand Up @@ -8,6 +8,7 @@ use crate::observer::BitcoinConfig;
use crate::utils::Context;
use bitcoincore_rpc::bitcoin::hashes::Hash;
use bitcoincore_rpc::bitcoin::{self, Address, Amount, BlockHash};
use bitcoincore_rpc::jsonrpc::error::RpcError;
use bitcoincore_rpc_json::GetRawTransactionResultVoutScriptPubKey;
use chainhook_types::bitcoin::{OutPoint, TxIn, TxOut};
use chainhook_types::{
Expand Down Expand Up @@ -264,6 +265,11 @@ pub async fn try_download_block_bytes_with_retry(
Ok(response)
}

#[derive(Debug, Clone, Deserialize)]
pub struct RpcErrorResponse {
pub error: RpcError,
}

pub async fn download_block(
http_client: &HttpClient,
block_hash: &str,
Expand All @@ -276,20 +282,39 @@ pub async fn download_block(
"method": "getblock",
"params": [block_hash, 3]
});
let block = http_client
let res = http_client
.post(&bitcoin_config.rpc_url)
.basic_auth(&bitcoin_config.username, Some(&bitcoin_config.password))
.header("Content-Type", "application/json")
.header("Host", &bitcoin_config.rpc_url[7..])
.json(&body)
.send()
.await
.map_err(|e| format!("unable to send request ({})", e))?
.map_err(|e| format!("unable to send request ({})", e))?;

// Check status code
if !res.status().is_success() {
return Err(format!(
"http request unsuccessful ({:?})",
res.error_for_status()
));
}

let rpc_response_bytes = res
.bytes()
.await
.map_err(|e| format!("unable to get bytes ({})", e))?
.to_vec();
Ok(block)

// Check rpc error presence
if let Ok(rpc_error) = serde_json::from_slice::<RpcErrorResponse>(&rpc_response_bytes[..]) {
return Err(format!(
"rpc request unsuccessful ({})",
rpc_error.error.message
));
}

Ok(rpc_response_bytes)
}

pub fn parse_downloaded_block(
Expand Down

0 comments on commit e515116

Please sign in to comment.