diff --git a/json/src/model/blockchain.rs b/json/src/model/blockchain.rs index a49b545..422531c 100644 --- a/json/src/model/blockchain.rs +++ b/json/src/model/blockchain.rs @@ -97,7 +97,7 @@ pub struct GetBlockchainInfo { /// Status of softforks in progress, maps softfork name -> [`Softfork`]. pub softforks: BTreeMap, /// Any network and blockchain warnings. - pub warnings: String, + pub warnings: Vec, } /// Status of softfork. diff --git a/json/src/v17/blockchain.rs b/json/src/v17/blockchain.rs index 91f6890..7c11c6e 100644 --- a/json/src/v17/blockchain.rs +++ b/json/src/v17/blockchain.rs @@ -346,7 +346,7 @@ impl GetBlockchainInfo { automatic_pruning: self.automatic_pruning, prune_target_size, softforks, - warnings: self.warnings, + warnings: vec![self.warnings], }) } } diff --git a/json/src/v19/blockchain.rs b/json/src/v19/blockchain.rs index 9be010f..b48888f 100644 --- a/json/src/v19/blockchain.rs +++ b/json/src/v19/blockchain.rs @@ -171,7 +171,7 @@ impl GetBlockchainInfo { automatic_pruning: self.automatic_pruning, prune_target_size, softforks, - warnings: self.warnings, + warnings: vec![self.warnings], }) } } diff --git a/json/src/v19/mod.rs b/json/src/v19/mod.rs index 7b1db75..8cdbd98 100644 --- a/json/src/v19/mod.rs +++ b/json/src/v19/mod.rs @@ -160,8 +160,8 @@ mod wallet; #[doc(inline)] pub use self::{ blockchain::{ - Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBlockchainInfo, Softfork, - SoftforkType, + Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBlockchainInfo, + GetBlockchainInfoError, Softfork, SoftforkType, }, wallet::{GetBalances, GetBalancesMine, GetBalancesWatchOnly}, }; diff --git a/json/src/v28/blockchain.rs b/json/src/v28/blockchain.rs new file mode 100644 index 0000000..4c27f82 --- /dev/null +++ b/json/src/v28/blockchain.rs @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! The JSON-RPC API for Bitcoin Core v28.0 - blockchain. +//! +//! Types for methods found under the `== Blockchain ==` section of the API docs. + +use std::collections::BTreeMap; + +use bitcoin::{BlockHash, Network, Work}; +use serde::{Deserialize, Serialize}; + +use super::{GetBlockchainInfoError, Softfork}; +use crate::model; + +#[rustfmt::skip] // Keep public re-exports separate. + +/// Result of JSON-RPC method `getblockchaininfo`. +/// +/// Method call: `getblockchaininfo` +/// +/// > Returns an object containing various state info regarding blockchain processing. +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct GetBlockchainInfo { + /// Current network name as defined in BIP70 (main, test, signet, regtest). + pub chain: String, + /// The current number of blocks processed in the server. + pub blocks: i64, + /// The current number of headers we have validated. + pub headers: i64, + /// The hash of the currently best block. + #[serde(rename = "bestblockhash")] + pub best_block_hash: String, + /// The current difficulty. + pub difficulty: f64, + /// Median time for the current best block. + #[serde(rename = "mediantime")] + pub median_time: i64, + /// Estimate of verification progress (between 0 and 1). + #[serde(rename = "verificationprogress")] + pub verification_progress: f64, + /// Estimate of whether this node is in Initial Block Download (IBD) mode. + #[serde(rename = "initialblockdownload")] + pub initial_block_download: bool, + /// Total amount of work in active chain, in hexadecimal. + #[serde(rename = "chainwork")] + pub chain_work: String, + /// The estimated size of the block and undo files on disk. + pub size_on_disk: u64, + /// If the blocks are subject to pruning. + pub pruned: bool, + /// Lowest-height complete block stored (only present if pruning is enabled). + #[serde(rename = "pruneheight")] + pub prune_height: Option, + /// Whether automatic pruning is enabled (only present if pruning is enabled). + pub automatic_pruning: Option, + /// The target size used by pruning (only present if automatic pruning is enabled). + pub prune_target_size: Option, + /// Status of softforks in progress, maps softfork name -> [`Softfork`]. + #[serde(default)] + pub softforks: BTreeMap, + /// Any network and blockchain warnings. + pub warnings: Vec, +} + +impl GetBlockchainInfo { + /// Converts version specific type to a version in-specific, more strongly typed type. + pub fn into_model(self) -> Result { + use GetBlockchainInfoError as E; + + let chain = Network::from_core_arg(&self.chain).map_err(E::Chain)?; + let best_block_hash = + self.best_block_hash.parse::().map_err(E::BestBlockHash)?; + let chain_work = Work::from_unprefixed_hex(&self.chain_work).map_err(E::ChainWork)?; + let prune_height = + self.prune_height.map(|h| crate::to_u32(h, "prune_height")).transpose()?; + let prune_target_size = + self.prune_target_size.map(|h| crate::to_u32(h, "prune_target_size")).transpose()?; + let softforks = BTreeMap::new(); // TODO: Handle softforks stuff. + + Ok(model::GetBlockchainInfo { + chain, + blocks: crate::to_u32(self.blocks, "blocks")?, + headers: crate::to_u32(self.headers, "headers")?, + best_block_hash, + difficulty: self.difficulty, + median_time: crate::to_u32(self.median_time, "median_time")?, + verification_progress: self.verification_progress, + initial_block_download: self.initial_block_download, + chain_work, + size_on_disk: self.size_on_disk, + pruned: self.pruned, + prune_height, + automatic_pruning: self.automatic_pruning, + prune_target_size, + softforks, + warnings: self.warnings, + }) + } +} diff --git a/json/src/v28/mod.rs b/json/src/v28/mod.rs index a2b414d..10101a6 100644 --- a/json/src/v28/mod.rs +++ b/json/src/v28/mod.rs @@ -180,8 +180,11 @@ //! **== Zmq ==** //! - [ ] `getzmqnotifications` +mod blockchain; mod network; +#[doc(inline)] +pub use self::blockchain::GetBlockchainInfo; #[doc(inline)] pub use self::network::GetNetworkInfo; #[doc(inline)] @@ -194,7 +197,7 @@ pub use crate::{ }, v19::{ Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBalances, GetBalancesMine, - GetBalancesWatchOnly, GetBlockchainInfo, Softfork, SoftforkType, + GetBalancesWatchOnly, GetBlockchainInfoError, Softfork, SoftforkType, }, v22::{SendToAddress, UnloadWallet}, v25::{CreateWallet, LoadWallet},