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

Commit 5facaf0

Browse files
committed
v17: Add support for blockchain methods
Add support to the `v17` client and integration tests for the following methods (all from the `blockchain` section): - `getblockcount` - `getblockhash`
1 parent d4301fc commit 5facaf0

File tree

8 files changed

+101
-5
lines changed

8 files changed

+101
-5
lines changed

client/src/client_sync/v17/blockchain.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ macro_rules! impl_client_v17__getblockchaininfo {
7070
};
7171
}
7272

73+
/// Implements bitcoind JSON-RPC API method `getblockcount`
74+
#[macro_export]
75+
macro_rules! impl_client_v17__getblockcount {
76+
() => {
77+
impl Client {
78+
pub fn get_block_count(&self) -> Result<GetBlockCount> {
79+
self.call("getblockcount", &[])
80+
}
81+
}
82+
};
83+
}
84+
85+
/// Implements bitcoind JSON-RPC API method `getblockhash`
86+
#[macro_export]
87+
macro_rules! impl_client_v17__getblockhash {
88+
() => {
89+
impl Client {
90+
pub fn get_block_hash(&self, height: u64) -> Result<GetBlockHash> {
91+
self.call("getblockhash", &[into_json(height)?])
92+
}
93+
}
94+
};
95+
}
96+
7397
/// Implements bitcoind JSON-RPC API method `gettxout`
7498
#[macro_export]
7599
macro_rules! impl_client_v17__gettxout {

client/src/client_sync/v17/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ crate::impl_client_v17__getblockchaininfo!();
2525
crate::impl_client_v17__getbestblockhash!();
2626
crate::impl_client_v17__getblock!();
2727
crate::impl_client_v17__gettxout!();
28+
crate::impl_client_v17__getblockcount!();
29+
crate::impl_client_v17__getblockhash!();
2830

2931
// == Control ==
3032
crate::impl_client_v17__stop!();

integration_test/src/v17/blockchain.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ macro_rules! impl_test_v17__getblockchaininfo {
8181
};
8282
}
8383

84+
/// Requires `Client` to be in scope and to implement `getblockcount`.
85+
#[macro_export]
86+
macro_rules! impl_test_v17__getblockcount {
87+
() => {
88+
#[test]
89+
fn get_block_count() {
90+
let bitcoind = $crate::bitcoind_no_wallet();
91+
let json = bitcoind.client.get_block_count().expect("getblockcount");
92+
let _ = json.into_model();
93+
}
94+
};
95+
}
96+
97+
/// Requires `Client` to be in scope and to implement `getblockhash`.
98+
#[macro_export]
99+
macro_rules! impl_test_v17__getblockhash {
100+
() => {
101+
#[test]
102+
fn get_block_hash() {
103+
let bitcoind = $crate::bitcoind_no_wallet();
104+
let json = bitcoind.client.get_block_hash(0).expect("getblockhash");
105+
assert!(json.into_model().is_ok());
106+
}
107+
};
108+
}
109+
84110
/// Requires `Client` to be in scope and to implement `get_tx_out`.
85111
#[macro_export]
86112
macro_rules! impl_test_v17__gettxout {

integration_test/tests/v17_api.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod blockchain {
1212
impl_test_v17__getblock_verbosity_0!();
1313
impl_test_v17__getblock_verbosity_1!();
1414
impl_test_v17__getblockchaininfo!();
15+
impl_test_v17__getblockcount!();
16+
impl_test_v17__getblockhash!();
1517
}
1618

1719
// == Control ==

json/src/model/blockchain.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ pub struct Bip9SoftforkStatistics {
176176
pub possible: Option<bool>,
177177
}
178178

179+
/// Models the result of JSON-RPC method `getblockcount`.
180+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
181+
pub struct GetBlockCount(pub u64);
182+
183+
/// Models the result of JSON-RPC method `getblockhash`.
184+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
185+
pub struct GetBlockHash(pub BlockHash);
186+
179187
/// Models the result of JSON-RPC method `gettxout`.
180188
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
181189
pub struct GetTxOut {

json/src/model/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ mod zmq;
2727
pub use self::{
2828
blockchain::{
2929
Bip9SoftforkInfo, Bip9SoftforkStatistics, Bip9SoftforkStatus, GetBestBlockHash,
30-
GetBlockVerbosityOne, GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, Softfork,
31-
SoftforkType,
30+
GetBlockCount, GetBlockHash, GetBlockVerbosityOne, GetBlockVerbosityZero,
31+
GetBlockchainInfo, GetTxOut, Softfork, SoftforkType,
3232
},
3333
generating::GenerateToAddress,
3434
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},

json/src/v17/blockchain.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,36 @@ impl std::error::Error for GetTxOutError {
500500
}
501501
}
502502
}
503+
504+
/// Result of JSON-RPC method `getblockcount`.
505+
///
506+
/// > getblockcount
507+
///
508+
/// > Returns the number of blocks in the longest blockchain.
509+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
510+
pub struct GetBlockCount(pub u64);
511+
512+
impl GetBlockCount {
513+
/// Converts version specific type to a version in-specific, more strongly typed type.
514+
pub fn into_model(self) -> model::GetBlockCount { model::GetBlockCount(self.0) }
515+
}
516+
517+
/// Result of JSON-RPC method `getblockhash`.
518+
///
519+
/// > Returns hash of block in best-block-chain at height provided.
520+
///
521+
/// > Arguments:
522+
/// > 1. height (numeric, required) The height index
523+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
524+
pub struct GetBlockHash(pub String);
525+
526+
impl GetBlockHash {
527+
/// Converts version specific type to a version in-specific, more strongly typed type.
528+
pub fn into_model(self) -> Result<model::GetBlockHash, hex::HexToArrayError> {
529+
let hash = self.0.parse::<BlockHash>()?;
530+
Ok(model::GetBlockHash(hash))
531+
}
532+
533+
/// Converts json straight to a `bitcoin::BlockHash`.
534+
pub fn block_hash(self) -> Result<BlockHash, hex::HexToArrayError> { Ok(self.into_model()?.0) }
535+
}

json/src/v17/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! - [x] `getbestblockhash`
1212
//! - [x] `getblock "blockhash" ( verbosity ) `
1313
//! - [x] `getblockchaininfo`
14-
//! - [ ] `getblockcount`
15-
//! - [ ] `getblockhash height`
14+
//! - [x] `getblockcount`
15+
//! - [x] `getblockhash height`
1616
//! - [ ] `getblockheader "hash" ( verbose )`
1717
//! - [ ] `getblockstats hash_or_height ( stats )`
1818
//! - [ ] `getchaintips`
@@ -166,8 +166,9 @@ mod zmq;
166166
#[doc(inline)]
167167
pub use self::{
168168
blockchain::{
169-
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockVerbosityOne,
169+
Bip9Softfork, Bip9SoftforkStatus, GetBestBlockHash, GetBlockCount, GetBlockVerbosityOne,
170170
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, ScriptPubkey, Softfork, SoftforkReject,
171+
GetBlockHash,
171172
},
172173
generating::GenerateToAddress,
173174
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},

0 commit comments

Comments
 (0)