diff --git a/client/src/client_sync/v17/blockchain.rs b/client/src/client_sync/v17/blockchain.rs index 0d3de1d..49773e6 100644 --- a/client/src/client_sync/v17/blockchain.rs +++ b/client/src/client_sync/v17/blockchain.rs @@ -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 { + 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 { + self.call("getblockhash", &[into_json(height)?]) + } + } + }; +} + /// Implements bitcoind JSON-RPC API method `gettxout` #[macro_export] macro_rules! impl_client_v17__gettxout { diff --git a/client/src/client_sync/v17/mod.rs b/client/src/client_sync/v17/mod.rs index 8d8438c..83699c2 100644 --- a/client/src/client_sync/v17/mod.rs +++ b/client/src/client_sync/v17/mod.rs @@ -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!(); diff --git a/integration_test/src/v17/blockchain.rs b/integration_test/src/v17/blockchain.rs index 4a8c56a..1373a30 100644 --- a/integration_test/src/v17/blockchain.rs +++ b/integration_test/src/v17/blockchain.rs @@ -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 { diff --git a/integration_test/tests/v17_api.rs b/integration_test/tests/v17_api.rs index 2ee2106..dbc8a14 100644 --- a/integration_test/tests/v17_api.rs +++ b/integration_test/tests/v17_api.rs @@ -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 == diff --git a/json/src/model/blockchain.rs b/json/src/model/blockchain.rs index d841cfb..eae18d7 100644 --- a/json/src/model/blockchain.rs +++ b/json/src/model/blockchain.rs @@ -176,6 +176,14 @@ pub struct Bip9SoftforkStatistics { pub possible: Option, } +/// 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 { diff --git a/json/src/model/mod.rs b/json/src/model/mod.rs index f724434..8c28806 100644 --- a/json/src/model/mod.rs +++ b/json/src/model/mod.rs @@ -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}, diff --git a/json/src/v17/blockchain.rs b/json/src/v17/blockchain.rs index 46ee53e..de8019f 100644 --- a/json/src/v17/blockchain.rs +++ b/json/src/v17/blockchain.rs @@ -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 { + let hash = self.0.parse::()?; + Ok(model::GetBlockHash(hash)) + } + + /// Converts json straight to a `bitcoin::BlockHash`. + pub fn block_hash(self) -> Result { Ok(self.into_model()?.0) } +} diff --git a/json/src/v17/mod.rs b/json/src/v17/mod.rs index 0f600cf..42ee18a 100644 --- a/json/src/v17/mod.rs +++ b/json/src/v17/mod.rs @@ -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` @@ -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},