Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ impl Client {
}
}

/// GET /rest/chaininfo.json
/// Returns chain information when connecting to a bitcoin node, None for esplora
pub async fn chain_info(&self) -> Result<Option<ChainInfo>> {
if self.use_esplora {
return Ok(None);
}

let base = &self.base_url;
let url = format!("{base}/rest/chaininfo.json");

let response = self
.client
.get(&url)
.send()
.await
.with_context(|| format!("failing for {url}"))?;

let status = response.status();
if status == 200 {
let text = response
.text()
.await
.with_context(|| format!("failing converting body to text for {url}"))?;
let chain_info: ChainInfo = serde_json::from_str(&text)
.with_context(|| format!("failing converting {text} to ChainInfo"))?;
Ok(Some(chain_info))
} else {
return Err(Error::UnexpectedStatus(url, status).into());
}
}

/// GET /rest/block/<BLOCK-HASH>.<bin|hex|json>
/// GET /block/:hash/raw
pub async fn block(&self, hash: BlockHash, family: Family) -> Result<be::Block> {
Expand Down Expand Up @@ -476,6 +507,14 @@ pub struct HeaderJson {
pub nextblockhash: Option<BlockHash>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct ChainInfo {
pub chain: String,
pub blocks: u32,
pub headers: u32,
pub bestblockhash: BlockHash,
}

#[cfg(test)]
mod test {
use std::str::FromStr;
Expand Down
12 changes: 12 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ pub async fn inner_main(
let state = state.clone();
let client: Client =
Client::new(&args).unwrap_or_else(|e| error_panic!("Failed to create client: {e}"));
log::info!(
"Client for blocks task created, chain info: {:?}",
client.chain_info().await
);
let shutdown_rx = shutdown_tx.subscribe();
tokio::spawn(async move {
let shutdown_future = async {
Expand All @@ -338,6 +342,10 @@ pub async fn inner_main(
let state = state.clone();
let client =
Client::new(&args).unwrap_or_else(|e| error_panic!("Failed to create client: {e}"));
log::info!(
"Client for mempool task created, chain info: {:?}",
client.chain_info().await
);
let shutdown_rx = shutdown_tx.subscribe();
tokio::spawn(async move {
let shutdown_future = async {
Expand All @@ -363,6 +371,10 @@ pub async fn inner_main(

let listener = TcpListener::bind(addr).await?;
let client = Client::new(&args)?;
log::info!(
"Client for server tasks created, chain info: {:?}",
client.chain_info().await
);
let client = Arc::new(Mutex::new(client));
let mut signal = std::pin::pin!(shutdown_signal);

Expand Down