Skip to content

Commit

Permalink
Expose remaining get*txout* for v19+ clients
Browse files Browse the repository at this point in the history
.. as it was previously only available in `v17` and `v18`.
  • Loading branch information
tnull committed Mar 6, 2025
1 parent 0fef9fd commit 2b11188
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 13 deletions.
3 changes: 3 additions & 0 deletions client/src/client_sync/v19/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v22/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
22 changes: 22 additions & 0 deletions client/src/client_sync/v26/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: CC0-1.0

//! Macros for implementing JSON-RPC methods on a client.
//!
//! Specifically this is methods found under the `== Blockchain ==` section of the
//! API docs of Bitcoin Core `v0.26`.
//!
//! All macros require `Client` to be in scope.
//!
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
/// Implements Bitcoin Core JSON-RPC API method `gettxoutsetinfo`
#[macro_export]
macro_rules! impl_client_v26__gettxoutsetinfo {
() => {
impl Client {
pub fn get_tx_out_set_info(&self) -> Result<GetTxOutSetInfo> {
self.call("gettxoutsetinfo", &[])
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//!
//! We ignore option arguments unless they effect the shape of the returned JSON data.
pub mod blockchain;

use bitcoin::address::{Address, NetworkChecked};
use bitcoin::{Amount, Block, BlockHash, Txid};

Expand Down Expand Up @@ -32,6 +34,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v27.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
3 changes: 3 additions & 0 deletions client/src/client_sync/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ crate::impl_client_v19__getmempoolentry!();
crate::impl_client_v17__getmempoolinfo!();
crate::impl_client_v17__getrawmempool!();
crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
2 changes: 0 additions & 2 deletions integration_test/tests/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ fn get_tx_out() {
let _ = json.into_model().expect("into_model");
}

#[cfg(any(feature = "0_17_1", feature = "0_18_1"))]
#[test]
fn get_tx_out_set_info() {
let node = Node::new_with_default_wallet();
Expand All @@ -235,7 +234,6 @@ fn get_tx_out_set_info() {

}

#[cfg(any(feature = "0_17_1", feature = "0_18_1"))]
// Implicitly tests the omitted method `gettxoutproof` as well.
#[test]
fn verify_tx_out_proof() {
Expand Down
4 changes: 3 additions & 1 deletion types/src/model/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ pub struct GetTxOutSetInfo {
/// A meaningless metric for UTXO set size.
pub bogo_size: u32,
/// The serialized hash.
pub hash_serialized_2: String, // FIXME: What sort of hash is this?
///
/// This was removed in Bitcoin Core v26, and hence will be `None` for v26 and later.
pub hash_serialized_2: Option<String>, // FIXME: What sort of hash is this?
/// The estimated size of the chainstate on disk.
pub disk_size: u32,
/// The total amount.
Expand Down
2 changes: 1 addition & 1 deletion types/src/v17/blockchain/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl GetTxOutSetInfo {
let transactions = crate::to_u32(self.transactions, "transactions")?;
let tx_outs = crate::to_u32(self.tx_outs, "tx_outs")?;
let bogo_size = crate::to_u32(self.bogo_size, "bogo_size")?;
let hash_serialized_2 = self.hash_serialized_2; // TODO: Convert this to a hash type.
let hash_serialized_2 = Some(self.hash_serialized_2); // TODO: Convert this to a hash type.
let disk_size = crate::to_u32(self.disk_size, "disk_size")?;
let total_amount = Amount::from_btc(self.total_amount).map_err(E::TotalAmount)?;

Expand Down
107 changes: 107 additions & 0 deletions types/src/v26/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: CC0-1.0

//! The JSON-RPC API for Bitcoin Core `v0.26` - blockchain.
//!
//! Types for methods found under the `== Blockchain ==` section of the API docs.
use core::fmt;

use bitcoin::{amount, hex, Amount, BlockHash};
use serde::{Deserialize, Serialize};

use crate::error::write_err;
use crate::{model, NumericError};

/// Result of JSON-RPC method `gettxoutsetinfo`.
///
/// > gettxoutsetinfo
/// >
/// > Returns statistics about the unspent transaction output set.
/// > Note this call may take some time.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetTxOutSetInfo {
/// The current block height (index).
pub height: i64,
/// The hash of the block at the tip of the chain.
#[serde(rename = "bestblock")]
pub best_block: String,
/// The number of transactions with unspent outputs.
pub transactions: i64,
/// The number of unspent transaction outputs.
#[serde(rename = "txouts")]
pub tx_outs: i64,
/// A meaningless metric for UTXO set size.
#[serde(rename = "bogosize")]
pub bogo_size: i64,
/// The estimated size of the chainstate on disk.
pub disk_size: i64,
/// The total amount.
pub total_amount: f64,
}

impl GetTxOutSetInfo {
/// Converts version specific type to a version nonspecific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetTxOutSetInfo, GetTxOutSetInfoError> {
use GetTxOutSetInfoError as E;

let height = crate::to_u32(self.height, "height")?;
let best_block = self.best_block.parse::<BlockHash>().map_err(E::BestBlock)?;
let transactions = crate::to_u32(self.transactions, "transactions")?;
let tx_outs = crate::to_u32(self.tx_outs, "tx_outs")?;
let bogo_size = crate::to_u32(self.bogo_size, "bogo_size")?;
let hash_serialized_2 = None; // Removed in Core v26
let disk_size = crate::to_u32(self.disk_size, "disk_size")?;
let total_amount = Amount::from_btc(self.total_amount).map_err(E::TotalAmount)?;

Ok(model::GetTxOutSetInfo {
height,
best_block,
transactions,
tx_outs,
bogo_size,
hash_serialized_2,
disk_size,
total_amount,
})
}
}

/// Error when converting a `GetTxOut` type into the model type.
#[derive(Debug)]
pub enum GetTxOutSetInfoError {
/// Conversion of numeric type to expected type failed.
Numeric(NumericError),
/// Conversion of the transaction `best_block` field failed.
BestBlock(hex::HexToArrayError),
/// Conversion of the transaction `total_amount` field failed.
TotalAmount(amount::ParseAmountError),
}

impl fmt::Display for GetTxOutSetInfoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use GetTxOutSetInfoError::*;

match *self {
Numeric(ref e) => write_err!(f, "numeric"; e),
BestBlock(ref e) => write_err!(f, "conversion of the `beast_block` field failed"; e),
TotalAmount(ref e) => write_err!(f, "conversion of the `total_amount` field failed"; e),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetTxOutSetInfoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetTxOutSetInfoError::*;

match *self {
Numeric(ref e) => Some(e),
BestBlock(ref e) => Some(e),
TotalAmount(ref e) => Some(e),
}
}
}

impl From<NumericError> for GetTxOutSetInfoError {
fn from(e: NumericError) -> Self { Self::Numeric(e) }
}
10 changes: 7 additions & 3 deletions types/src/v26/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
//! - Method returns a simple type (e.g. bool or integer).
//! - Method is deprecated.
mod blockchain;

#[doc(inline)]
pub use self::blockchain::GetTxOutSetInfo;
#[doc(inline)]
pub use crate::{
v17::{
Expand All @@ -258,9 +262,9 @@ pub use crate::{
GetMempoolInfo, GetNetTotals, GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoError,
GetNetworkInfoNetwork, GetNewAddress, GetPeerInfo, GetRawChangeAddress, GetRawMempool,
GetRawMempoolVerbose, GetReceivedByAddress, GetTransaction, GetTransactionDetail,
GetTxOutSetInfo, GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications,
ListAddressGroupings, ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent,
ListLockUnspentItem, ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications, ListAddressGroupings,
ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent, ListLockUnspentItem,
ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
ListSinceBlockTransaction, ListTransactions, ListTransactionsItem, ListUnspent,
ListUnspentItem, ListWallets, Locked, Logging, PeerInfo, RescanBlockchain, SendMany,
SendRawTransaction, SendToAddress, SignErrorData, SignMessage,
Expand Down
7 changes: 4 additions & 3 deletions types/src/v27/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ pub use crate::{
GetMempoolInfo, GetNetTotals, GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoError,
GetNetworkInfoNetwork, GetNewAddress, GetPeerInfo, GetRawChangeAddress, GetRawMempool,
GetRawMempoolVerbose, GetReceivedByAddress, GetTransaction, GetTransactionDetail,
GetTxOutSetInfo, GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications,
ListAddressGroupings, ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent,
ListLockUnspentItem, ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications, ListAddressGroupings,
ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent, ListLockUnspentItem,
ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
ListSinceBlockTransaction, ListTransactions, ListTransactionsItem, ListUnspent,
ListUnspentItem, ListWallets, Locked, Logging, PeerInfo, RescanBlockchain, SendMany,
SendRawTransaction, SendToAddress, SignErrorData, SignMessage,
Expand All @@ -276,4 +276,5 @@ pub use crate::{
v21::UnloadWallet,
v22::{GetTxOut, ScriptPubkey},
v25::{CreateWallet, LoadWallet},
v26::GetTxOutSetInfo,
};
7 changes: 4 additions & 3 deletions types/src/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ pub use crate::{
GetMempoolInfo, GetNetTotals, GetNetworkInfoAddress, GetNetworkInfoError,
GetNetworkInfoNetwork, GetNewAddress, GetPeerInfo, GetRawChangeAddress, GetRawMempool,
GetRawMempoolVerbose, GetReceivedByAddress, GetTransaction, GetTransactionDetail,
GetTxOutSetInfo, GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications,
ListAddressGroupings, ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent,
ListLockUnspentItem, ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
GetUnconfirmedBalance, GetWalletInfo, GetZmqNotifications, ListAddressGroupings,
ListAddressGroupingsItem, ListBanned, ListLabels, ListLockUnspent, ListLockUnspentItem,
ListReceivedByAddress, ListReceivedByAddressItem, ListSinceBlock,
ListSinceBlockTransaction, ListTransactions, ListTransactionsItem, ListUnspent,
ListUnspentItem, ListWallets, Locked, Logging, PeerInfo, RescanBlockchain, SendMany,
SendRawTransaction, SendToAddress, SignErrorData, SignMessage,
Expand All @@ -288,4 +288,5 @@ pub use crate::{
v21::UnloadWallet,
v22::{GetTxOut, ScriptPubkey},
v25::{CreateWallet, LoadWallet},
v26::GetTxOutSetInfo,
};

0 comments on commit 2b11188

Please sign in to comment.