Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
f Account for getblockchaininfo returning array of warnings now
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Nov 1, 2024
1 parent 77deaf3 commit bd1e13a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion json/src/model/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct GetBlockchainInfo {
/// Status of softforks in progress, maps softfork name -> [`Softfork`].
pub softforks: BTreeMap<String, Softfork>,
/// Any network and blockchain warnings.
pub warnings: String,
pub warnings: Vec<String>,
}

/// Status of softfork.
Expand Down
2 changes: 1 addition & 1 deletion json/src/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl GetBlockchainInfo {
automatic_pruning: self.automatic_pruning,
prune_target_size,
softforks,
warnings: self.warnings,
warnings: vec![self.warnings],
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion json/src/v19/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl GetBlockchainInfo {
automatic_pruning: self.automatic_pruning,
prune_target_size,
softforks,
warnings: self.warnings,
warnings: vec![self.warnings],
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions json/src/v19/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down
99 changes: 99 additions & 0 deletions json/src/v28/blockchain.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
/// Whether automatic pruning is enabled (only present if pruning is enabled).
pub automatic_pruning: Option<bool>,
/// The target size used by pruning (only present if automatic pruning is enabled).
pub prune_target_size: Option<i64>,
/// Status of softforks in progress, maps softfork name -> [`Softfork`].
#[serde(default)]
pub softforks: BTreeMap<String, Softfork>,
/// Any network and blockchain warnings.
pub warnings: Vec<String>,
}

impl GetBlockchainInfo {
/// Converts version specific type to a version in-specific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetBlockchainInfo, GetBlockchainInfoError> {
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::<BlockHash>().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,
})
}
}
5 changes: 4 additions & 1 deletion json/src/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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},
Expand Down

0 comments on commit bd1e13a

Please sign in to comment.