Skip to content

Commit 0307e86

Browse files
committed
Add missing fields to getblockstats
There are missing return fields in v25 `getblockstats`. Redefine the type for v25 with the missing fields. Update the model and into functions.
1 parent f097907 commit 0307e86

File tree

9 files changed

+197
-5
lines changed

9 files changed

+197
-5
lines changed

types/src/model/blockchain.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ pub struct GetBlockStats {
306306
pub utxo_increase: i32,
307307
/// The increase/decrease in size for the utxo index (not discounting op_return and similar).
308308
pub utxo_size_increase: i32,
309+
/// The increase/decrease in the number of unspent outputs, not counting unspendables.
310+
/// v25 and later only.
311+
pub utxo_increase_actual: Option<i32>,
312+
/// The increase/decrease in size for the utxo index, not counting unspendables.
313+
/// v25 and later only.
314+
pub utxo_size_increase_actual: Option<i32>,
309315
}
310316

311317
/// Models the result of JSON-RPC method `getchaintips`.

types/src/v17/blockchain/into.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ impl GetBlockStats {
261261
txs: crate::to_u32(self.txs, "txs")?,
262262
utxo_increase: self.utxo_increase,
263263
utxo_size_increase: self.utxo_size_increase,
264+
utxo_increase_actual: None, // v25 and later only.
265+
utxo_size_increase_actual: None, // v25 and later only.
264266
})
265267
}
266268
}

types/src/v25/blockchain/into.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use bitcoin::{Amount, BlockHash, FeeRate, Weight};
4+
5+
use super::{GetBlockStats, GetBlockStatsError};
6+
use crate::model;
7+
8+
impl GetBlockStats {
9+
/// Converts version specific type to a version nonspecific, more strongly typed type.
10+
pub fn into_model(self) -> Result<model::GetBlockStats, GetBlockStatsError> {
11+
use GetBlockStatsError as E;
12+
13+
// `FeeRate::sat_per_vb` returns an option if value overflows.
14+
let average_fee_rate = FeeRate::from_sat_per_vb(self.average_fee_rate);
15+
let block_hash = self.block_hash.parse::<BlockHash>().map_err(E::BlockHash)?;
16+
let fee_rate_percentiles = self
17+
.fee_rate_percentiles
18+
.iter()
19+
.map(|vb| FeeRate::from_sat_per_vb(*vb))
20+
.collect::<Vec<Option<FeeRate>>>();
21+
let max_fee_rate = FeeRate::from_sat_per_vb(self.max_fee_rate);
22+
let minimum_fee_rate = FeeRate::from_sat_per_vb(self.minimum_fee_rate);
23+
24+
// FIXME: Double check that these values are virtual bytes and not weight units.
25+
let segwit_total_weight = Weight::from_vb(self.segwit_total_weight);
26+
let total_weight = Weight::from_vb(self.total_weight);
27+
28+
Ok(model::GetBlockStats {
29+
average_fee: Amount::from_sat(self.average_fee),
30+
average_fee_rate,
31+
average_tx_size: crate::to_u32(self.average_tx_size, "average_tx_size")?,
32+
block_hash,
33+
fee_rate_percentiles,
34+
height: crate::to_u32(self.height, "height")?,
35+
inputs: crate::to_u32(self.inputs, "inputs")?,
36+
max_fee: Amount::from_sat(self.max_fee),
37+
max_fee_rate,
38+
max_tx_size: crate::to_u32(self.max_tx_size, "max_tx_size")?,
39+
median_fee: Amount::from_sat(self.median_fee),
40+
median_time: crate::to_u32(self.median_time, "median_time")?,
41+
median_tx_size: crate::to_u32(self.median_tx_size, "median_tx_size")?,
42+
minimum_fee: Amount::from_sat(self.minimum_fee),
43+
minimum_fee_rate,
44+
minimum_tx_size: crate::to_u32(self.minimum_tx_size, "minimum_tx_size")?,
45+
outputs: crate::to_u32(self.outputs, "outputs")?,
46+
subsidy: Amount::from_sat(self.subsidy),
47+
segwit_total_size: crate::to_u32(self.segwit_total_size, "segwit_total_size")?,
48+
segwit_total_weight,
49+
segwit_txs: crate::to_u32(self.segwit_txs, "segwit_txs")?,
50+
time: crate::to_u32(self.time, "time")?,
51+
total_out: Amount::from_sat(self.total_out),
52+
total_size: crate::to_u32(self.total_size, "total_size")?,
53+
total_weight,
54+
total_fee: Amount::from_sat(self.total_fee),
55+
txs: crate::to_u32(self.txs, "txs")?,
56+
utxo_increase: self.utxo_increase,
57+
utxo_size_increase: self.utxo_size_increase,
58+
utxo_increase_actual: self.utxo_increase_actual,
59+
utxo_size_increase_actual: self.utxo_size_increase_actual,
60+
})
61+
}
62+
}

types/src/v25/blockchain/mod.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! The JSON-RPC API for Bitcoin Core `v25` - blockchain.
4+
//!
5+
//! Types for methods found under the `== Blockchain ==` section of the API docs.
6+
7+
mod into;
8+
9+
use serde::{Deserialize, Serialize};
10+
11+
pub use super::GetBlockStatsError;
12+
13+
/// Result of JSON-RPC method `getblockstats`.
14+
///
15+
/// > getblockstats hash_or_height ( stats )
16+
///
17+
/// > Returns the number of blocks in the longest blockchain.
18+
/// > getblockstats hash_or_height ( stats )
19+
/// >
20+
/// > Compute per block statistics for a given window. All amounts are in satoshis.
21+
/// > It won't work for some heights with pruning.
22+
/// > It won't work without -txindex for utxo_size_inc, *fee or *feerate stats.
23+
/// >
24+
/// > Arguments:
25+
/// > 1. "hash_or_height" (string or numeric, required) The block hash or height of the target block
26+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
27+
pub struct GetBlockStats {
28+
/// Average fee in the block.
29+
#[serde(rename = "avgfee")]
30+
pub average_fee: u64,
31+
// FIXME: Remember these docs will become silently stale when unit changes in a later version of Core.
32+
/// Average feerate (in satoshis per virtual byte).
33+
#[serde(rename = "avgfeerate")]
34+
pub average_fee_rate: u64,
35+
/// Average transaction size.
36+
#[serde(rename = "avgtxsize")]
37+
pub average_tx_size: i64,
38+
/// The block hash (to check for potential reorgs).
39+
#[serde(rename = "blockhash")]
40+
pub block_hash: String,
41+
/// Feerates at the 10th, 25th, 50th, 75th, and 90th percentile weight unit (in satoshis per
42+
/// virtual byte).
43+
#[serde(rename = "feerate_percentiles")]
44+
pub fee_rate_percentiles: [u64; 5],
45+
/// The height of the block.
46+
pub height: i64,
47+
/// The number of inputs (excluding coinbase).
48+
#[serde(rename = "ins")]
49+
pub inputs: i64,
50+
/// Maximum fee in the block.
51+
#[serde(rename = "maxfee")]
52+
pub max_fee: u64,
53+
/// Maximum feerate (in satoshis per virtual byte).
54+
#[serde(rename = "maxfeerate")]
55+
pub max_fee_rate: u64,
56+
/// Maximum transaction size.
57+
#[serde(rename = "maxtxsize")]
58+
pub max_tx_size: i64,
59+
/// Truncated median fee in the block.
60+
#[serde(rename = "medianfee")]
61+
pub median_fee: u64,
62+
/// The block median time past.
63+
#[serde(rename = "mediantime")]
64+
pub median_time: i64,
65+
/// Truncated median transaction size
66+
#[serde(rename = "mediantxsize")]
67+
pub median_tx_size: i64,
68+
/// Minimum fee in the block.
69+
#[serde(rename = "minfee")]
70+
pub minimum_fee: u64,
71+
/// Minimum feerate (in satoshis per virtual byte).
72+
#[serde(rename = "minfeerate")]
73+
pub minimum_fee_rate: u64,
74+
/// Minimum transaction size.
75+
#[serde(rename = "mintxsize")]
76+
pub minimum_tx_size: i64,
77+
/// The number of outputs.
78+
#[serde(rename = "outs")]
79+
pub outputs: i64,
80+
/// The block subsidy.
81+
pub subsidy: u64,
82+
/// Total size of all segwit transactions.
83+
#[serde(rename = "swtotal_size")]
84+
pub segwit_total_size: i64,
85+
/// Total weight of all segwit transactions divided by segwit scale factor (4).
86+
#[serde(rename = "swtotal_weight")]
87+
pub segwit_total_weight: u64,
88+
/// The number of segwit transactions.
89+
#[serde(rename = "swtxs")]
90+
pub segwit_txs: i64,
91+
/// The block time.
92+
pub time: i64,
93+
/// Total amount in all outputs (excluding coinbase and thus reward [ie subsidy + totalfee]).
94+
pub total_out: u64,
95+
/// Total size of all non-coinbase transactions.
96+
pub total_size: i64,
97+
/// Total weight of all non-coinbase transactions divided by segwit scale factor (4).
98+
pub total_weight: u64,
99+
/// The fee total.
100+
#[serde(rename = "totalfee")]
101+
pub total_fee: u64,
102+
/// The number of transactions (excluding coinbase).
103+
pub txs: i64,
104+
/// The increase/decrease in the number of unspent outputs.
105+
pub utxo_increase: i32,
106+
/// The increase/decrease in size for the utxo index (not discounting op_return and similar).
107+
#[serde(rename = "utxo_size_inc")]
108+
pub utxo_size_increase: i32,
109+
/// The increase/decrease in the number of unspent outputs, not counting unspendables.
110+
/// v25 and later only.
111+
pub utxo_increase_actual: Option<i32>,
112+
/// The increase/decrease in size for the utxo index, not counting unspendables.
113+
/// v25 and later only.
114+
#[serde(rename = "utxo_size_inc_actual")]
115+
pub utxo_size_increase_actual: Option<i32>,
116+
}

types/src/v25/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,13 @@
240240
//!
241241
//! </details>
242242
243+
mod blockchain;
243244
mod control;
244245
mod wallet;
245246

246247
#[doc(inline)]
247248
pub use self::{
249+
blockchain::GetBlockStats,
248250
control::Logging,
249251
wallet::{CreateWallet, LoadWallet, UnloadWallet},
250252
};
@@ -260,7 +262,7 @@ pub use crate::{
260262
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
261263
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
262264
GetBlockHash, GetBlockHeader, GetBlockHeaderError, GetBlockHeaderVerbose,
263-
GetBlockHeaderVerboseError, GetBlockStats, GetBlockStatsError, GetBlockTemplate,
265+
GetBlockHeaderVerboseError, GetBlockStatsError, GetBlockTemplate,
264266
GetBlockTemplateError, GetBlockVerboseOne, GetBlockVerboseOneError, GetBlockVerboseZero,
265267
GetChainTips, GetChainTxStatsError, GetConnectionCount, GetDifficulty, GetMemoryInfoStats,
266268
GetMempoolInfoError, GetMiningInfo, GetNetTotals, GetNetworkInfoAddress,

types/src/v26/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub use crate::{
280280
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
281281
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
282282
GetBlockHash, GetBlockHeader, GetBlockHeaderError, GetBlockHeaderVerbose,
283-
GetBlockHeaderVerboseError, GetBlockStats, GetBlockStatsError, GetBlockTemplate,
283+
GetBlockHeaderVerboseError, GetBlockStatsError, GetBlockTemplate,
284284
GetBlockTemplateError, GetBlockVerboseOne, GetBlockVerboseOneError, GetBlockVerboseZero,
285285
GetChainTips, GetChainTxStatsError, GetConnectionCount, GetDifficulty, GetMemoryInfoStats,
286286
GetMempoolInfoError, GetMiningInfo, GetNetTotals, GetNetworkInfoAddress,
@@ -323,4 +323,5 @@ pub use crate::{
323323
PsbtInput, PsbtOutput, TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
324324
GetTransactionDetail,
325325
},
326+
v25::GetBlockStats,
326327
};

types/src/v27/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub use crate::{
260260
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
261261
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
262262
GetBlockHash, GetBlockHeader, GetBlockHeaderError, GetBlockHeaderVerbose,
263-
GetBlockHeaderVerboseError, GetBlockStats, GetBlockStatsError, GetBlockTemplate,
263+
GetBlockHeaderVerboseError, GetBlockStatsError, GetBlockTemplate,
264264
GetBlockTemplateError, GetBlockVerboseOne, GetBlockVerboseOneError, GetBlockVerboseZero,
265265
GetChainTips, GetChainTxStatsError, GetConnectionCount, GetDifficulty, GetMemoryInfoStats,
266266
GetMempoolInfoError, GetMiningInfo, GetNetTotals, GetNetworkInfoAddress,
@@ -303,6 +303,7 @@ pub use crate::{
303303
PsbtInput, PsbtOutput, TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
304304
GetTransactionDetail,
305305
},
306+
v25::GetBlockStats,
306307
v26::{
307308
CreateWallet, DescriptorProcessPsbt, DescriptorProcessPsbtError, GetPeerInfo,
308309
GetPrioritisedTransactions, GetTxOutSetInfo, GetTxOutSetInfoError, LoadWallet,

types/src/v28/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub use crate::{
281281
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
282282
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
283283
GetBlockHash, GetBlockHeader, GetBlockHeaderError, GetBlockHeaderVerbose,
284-
GetBlockHeaderVerboseError, GetBlockStats, GetBlockStatsError, GetBlockTemplate,
284+
GetBlockHeaderVerboseError, GetBlockStatsError, GetBlockTemplate,
285285
GetBlockTemplateError, GetBlockVerboseOne, GetBlockVerboseOneError, GetBlockVerboseZero,
286286
GetChainTips, GetChainTxStatsError, GetConnectionCount, GetDifficulty, GetMemoryInfoStats,
287287
GetMempoolInfoError, GetNetTotals, GetNetworkInfoAddress, GetNetworkInfoError,
@@ -323,6 +323,7 @@ pub use crate::{
323323
PsbtInput, PsbtOutput, TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
324324
GetTransactionDetail,
325325
},
326+
v25::GetBlockStats,
326327
v26::{
327328
CreateWallet, DescriptorProcessPsbt, DescriptorProcessPsbtError, GetPeerInfo,
328329
GetPrioritisedTransactions, GetTxOutSetInfo, GetTxOutSetInfoError, LoadWallet,

types/src/v29/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub use crate::{
280280
FundRawTransaction, FundRawTransactionError, Generate, GenerateToAddress, GetAddedNodeInfo,
281281
GetAddressInfo, GetAddressInfoEmbedded, GetAddressInfoEmbeddedError, GetAddressInfoError,
282282
GetAddressInfoLabel, GetAddressesByLabel, GetBalance, GetBestBlockHash, GetBlockCount,
283-
GetBlockHash, GetBlockStats, GetBlockStatsError, GetBlockTemplate, GetBlockTemplateError,
283+
GetBlockHash, GetBlockStatsError, GetBlockTemplate, GetBlockTemplateError,
284284
GetBlockVerboseZero, GetChainTips, GetChainTxStatsError, GetConnectionCount, GetDifficulty,
285285
GetMemoryInfoStats, GetMempoolInfoError, GetNetTotals, GetNetworkInfoAddress,
286286
GetNetworkInfoError, GetNetworkInfoNetwork, GetNewAddress, GetRawChangeAddress,
@@ -321,6 +321,7 @@ pub use crate::{
321321
PsbtInput, PsbtOutput, TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
322322
GetTransactionDetail,
323323
},
324+
v25::GetBlockStats,
324325
v26::{
325326
CreateWallet, DescriptorProcessPsbt, DescriptorProcessPsbtError,
326327
GetPrioritisedTransactions, GetTxOutSetInfo, GetTxOutSetInfoError, LoadWallet,

0 commit comments

Comments
 (0)