Skip to content

Commit

Permalink
v17: Add support for blockchain methods
Browse files Browse the repository at this point in the history
Add support to the `v17` client and integration tests for the following
methods (all from the `blockchain` section):

- `getblockcount`
- `getblockhash`
  • Loading branch information
tcharding committed Sep 2, 2024
1 parent d4301fc commit 5facaf0
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 5 deletions.
24 changes: 24 additions & 0 deletions client/src/client_sync/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ macro_rules! impl_client_v17__getblockchaininfo {
};
}

/// Implements bitcoind JSON-RPC API method `getblockcount`
#[macro_export]
macro_rules! impl_client_v17__getblockcount {
() => {
impl Client {
pub fn get_block_count(&self) -> Result<GetBlockCount> {
self.call("getblockcount", &[])
}
}
};
}

/// Implements bitcoind JSON-RPC API method `getblockhash`
#[macro_export]
macro_rules! impl_client_v17__getblockhash {
() => {
impl Client {
pub fn get_block_hash(&self, height: u64) -> Result<GetBlockHash> {
self.call("getblockhash", &[into_json(height)?])
}
}
};
}

/// Implements bitcoind JSON-RPC API method `gettxout`
#[macro_export]
macro_rules! impl_client_v17__gettxout {
Expand Down
2 changes: 2 additions & 0 deletions client/src/client_sync/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ crate::impl_client_v17__getblockchaininfo!();
crate::impl_client_v17__getbestblockhash!();
crate::impl_client_v17__getblock!();
crate::impl_client_v17__gettxout!();
crate::impl_client_v17__getblockcount!();
crate::impl_client_v17__getblockhash!();

// == Control ==
crate::impl_client_v17__stop!();
Expand Down
26 changes: 26 additions & 0 deletions integration_test/src/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ macro_rules! impl_test_v17__getblockchaininfo {
};
}

/// Requires `Client` to be in scope and to implement `getblockcount`.
#[macro_export]
macro_rules! impl_test_v17__getblockcount {
() => {
#[test]
fn get_block_count() {
let bitcoind = $crate::bitcoind_no_wallet();
let json = bitcoind.client.get_block_count().expect("getblockcount");
let _ = json.into_model();
}
};
}

/// Requires `Client` to be in scope and to implement `getblockhash`.
#[macro_export]
macro_rules! impl_test_v17__getblockhash {
() => {
#[test]
fn get_block_hash() {
let bitcoind = $crate::bitcoind_no_wallet();
let json = bitcoind.client.get_block_hash(0).expect("getblockhash");
assert!(json.into_model().is_ok());
}
};
}

/// Requires `Client` to be in scope and to implement `get_tx_out`.
#[macro_export]
macro_rules! impl_test_v17__gettxout {
Expand Down
2 changes: 2 additions & 0 deletions integration_test/tests/v17_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod blockchain {
impl_test_v17__getblock_verbosity_0!();
impl_test_v17__getblock_verbosity_1!();
impl_test_v17__getblockchaininfo!();
impl_test_v17__getblockcount!();
impl_test_v17__getblockhash!();
}

// == Control ==
Expand Down
8 changes: 8 additions & 0 deletions json/src/model/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ pub struct Bip9SoftforkStatistics {
pub possible: Option<bool>,
}

/// Models the result of JSON-RPC method `getblockcount`.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GetBlockCount(pub u64);

/// Models the result of JSON-RPC method `getblockhash`.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GetBlockHash(pub BlockHash);

/// Models the result of JSON-RPC method `gettxout`.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct GetTxOut {
Expand Down
4 changes: 2 additions & 2 deletions json/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mod zmq;
pub use self::{
blockchain::{
Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBestBlockHash,
GetBlockVerbosityOne, GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, Softfork,
SoftforkType,
GetBlockCount, GetBlockHash, GetBlockVerbosityOne, GetBlockVerbosityZero,
GetBlockchainInfo, GetTxOut, Softfork, SoftforkType,
},
generating::GenerateToAddress,
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
Expand Down
33 changes: 33 additions & 0 deletions json/src/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,36 @@ impl std::error::Error for GetTxOutError {
}
}
}

/// Result of JSON-RPC method `getblockcount`.
///
/// > getblockcount
///
/// > Returns the number of blocks in the longest blockchain.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetBlockCount(pub u64);

impl GetBlockCount {
/// Converts version specific type to a version in-specific, more strongly typed type.
pub fn into_model(self) -> model::GetBlockCount { model::GetBlockCount(self.0) }
}

/// Result of JSON-RPC method `getblockhash`.
///
/// > Returns hash of block in best-block-chain at height provided.
///
/// > Arguments:
/// > 1. height (numeric, required) The height index
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetBlockHash(pub String);

impl GetBlockHash {
/// Converts version specific type to a version in-specific, more strongly typed type.
pub fn into_model(self) -> Result<model::GetBlockHash, hex::HexToArrayError> {
let hash = self.0.parse::<BlockHash>()?;
Ok(model::GetBlockHash(hash))
}

/// Converts json straight to a `bitcoin::BlockHash`.
pub fn block_hash(self) -> Result<BlockHash, hex::HexToArrayError> { Ok(self.into_model()?.0) }
}
7 changes: 4 additions & 3 deletions json/src/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//! - [x] `getbestblockhash`
//! - [x] `getblock "blockhash" ( verbosity ) `
//! - [x] `getblockchaininfo`
//! - [ ] `getblockcount`
//! - [ ] `getblockhash height`
//! - [x] `getblockcount`
//! - [x] `getblockhash height`
//! - [ ] `getblockheader "hash" ( verbose )`
//! - [ ] `getblockstats hash_or_height ( stats )`
//! - [ ] `getchaintips`
Expand Down Expand Up @@ -166,8 +166,9 @@ mod zmq;
#[doc(inline)]
pub use self::{
blockchain::{
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockVerbosityOne,
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockCount, GetBlockVerbosityOne,
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, ScriptPubkey, Softfork, SoftforkReject,
GetBlockHash,
},
generating::GenerateToAddress,
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
Expand Down

0 comments on commit 5facaf0

Please sign in to comment.