|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +//! The JSON-RPC API for Bitcoin Core v28.0 - blockchain. |
| 4 | +//! |
| 5 | +//! Types for methods found under the `== Blockchain ==` section of the API docs. |
| 6 | +
|
| 7 | +use std::collections::BTreeMap; |
| 8 | + |
| 9 | +use bitcoin::{BlockHash, Network, Work}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | + |
| 12 | +use super::{GetBlockchainInfoError, Softfork}; |
| 13 | +use crate::model; |
| 14 | + |
| 15 | +#[rustfmt::skip] // Keep public re-exports separate. |
| 16 | + |
| 17 | +/// Result of JSON-RPC method `getblockchaininfo`. |
| 18 | +/// |
| 19 | +/// Method call: `getblockchaininfo` |
| 20 | +/// |
| 21 | +/// > Returns an object containing various state info regarding blockchain processing. |
| 22 | +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] |
| 23 | +pub struct GetBlockchainInfo { |
| 24 | + /// Current network name as defined in BIP70 (main, test, signet, regtest). |
| 25 | + pub chain: String, |
| 26 | + /// The current number of blocks processed in the server. |
| 27 | + pub blocks: i64, |
| 28 | + /// The current number of headers we have validated. |
| 29 | + pub headers: i64, |
| 30 | + /// The hash of the currently best block. |
| 31 | + #[serde(rename = "bestblockhash")] |
| 32 | + pub best_block_hash: String, |
| 33 | + /// The current difficulty. |
| 34 | + pub difficulty: f64, |
| 35 | + /// Median time for the current best block. |
| 36 | + #[serde(rename = "mediantime")] |
| 37 | + pub median_time: i64, |
| 38 | + /// Estimate of verification progress (between 0 and 1). |
| 39 | + #[serde(rename = "verificationprogress")] |
| 40 | + pub verification_progress: f64, |
| 41 | + /// Estimate of whether this node is in Initial Block Download (IBD) mode. |
| 42 | + #[serde(rename = "initialblockdownload")] |
| 43 | + pub initial_block_download: bool, |
| 44 | + /// Total amount of work in active chain, in hexadecimal. |
| 45 | + #[serde(rename = "chainwork")] |
| 46 | + pub chain_work: String, |
| 47 | + /// The estimated size of the block and undo files on disk. |
| 48 | + pub size_on_disk: u64, |
| 49 | + /// If the blocks are subject to pruning. |
| 50 | + pub pruned: bool, |
| 51 | + /// Lowest-height complete block stored (only present if pruning is enabled). |
| 52 | + #[serde(rename = "pruneheight")] |
| 53 | + pub prune_height: Option<i64>, |
| 54 | + /// Whether automatic pruning is enabled (only present if pruning is enabled). |
| 55 | + pub automatic_pruning: Option<bool>, |
| 56 | + /// The target size used by pruning (only present if automatic pruning is enabled). |
| 57 | + pub prune_target_size: Option<i64>, |
| 58 | + /// Status of softforks in progress, maps softfork name -> [`Softfork`]. |
| 59 | + #[serde(default)] |
| 60 | + pub softforks: BTreeMap<String, Softfork>, |
| 61 | + /// Any network and blockchain warnings. |
| 62 | + pub warnings: Vec<String>, |
| 63 | +} |
| 64 | + |
| 65 | +impl GetBlockchainInfo { |
| 66 | + /// Converts version specific type to a version in-specific, more strongly typed type. |
| 67 | + pub fn into_model(self) -> Result<model::GetBlockchainInfo, GetBlockchainInfoError> { |
| 68 | + use GetBlockchainInfoError as E; |
| 69 | + |
| 70 | + let chain = Network::from_core_arg(&self.chain).map_err(E::Chain)?; |
| 71 | + let best_block_hash = |
| 72 | + self.best_block_hash.parse::<BlockHash>().map_err(E::BestBlockHash)?; |
| 73 | + let chain_work = Work::from_unprefixed_hex(&self.chain_work).map_err(E::ChainWork)?; |
| 74 | + let prune_height = |
| 75 | + self.prune_height.map(|h| crate::to_u32(h, "prune_height")).transpose()?; |
| 76 | + let prune_target_size = |
| 77 | + self.prune_target_size.map(|h| crate::to_u32(h, "prune_target_size")).transpose()?; |
| 78 | + let softforks = BTreeMap::new(); // TODO: Handle softforks stuff. |
| 79 | + |
| 80 | + Ok(model::GetBlockchainInfo { |
| 81 | + chain, |
| 82 | + blocks: crate::to_u32(self.blocks, "blocks")?, |
| 83 | + headers: crate::to_u32(self.headers, "headers")?, |
| 84 | + best_block_hash, |
| 85 | + difficulty: self.difficulty, |
| 86 | + median_time: crate::to_u32(self.median_time, "median_time")?, |
| 87 | + verification_progress: self.verification_progress, |
| 88 | + initial_block_download: self.initial_block_download, |
| 89 | + chain_work, |
| 90 | + size_on_disk: self.size_on_disk, |
| 91 | + pruned: self.pruned, |
| 92 | + prune_height, |
| 93 | + automatic_pruning: self.automatic_pruning, |
| 94 | + prune_target_size, |
| 95 | + softforks, |
| 96 | + warnings: self.warnings, |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments