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

Commit ffe5260

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` - `getblockheader`
1 parent d4301fc commit ffe5260

File tree

8 files changed

+358
-9
lines changed

8 files changed

+358
-9
lines changed

client/src/client_sync/v17/blockchain.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,50 @@ 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+
97+
/// Implements bitcoind JSON-RPC API method `getblockheader`
98+
#[macro_export]
99+
macro_rules! impl_client_v17__getblockheader {
100+
() => {
101+
impl Client {
102+
pub fn get_block_header(&self, hash: &BlockHash) -> Result<GetBlockHeader> {
103+
self.call("getblockheader", &[into_json(hash)?, into_json(false)?])
104+
}
105+
106+
// This is the same as calling getblockheader with verbose==true.
107+
pub fn get_block_header_verbose(
108+
&self,
109+
hash: &BlockHash,
110+
) -> Result<GetBlockHeaderVerbose> {
111+
self.call("getblockheader", &[into_json(hash)?])
112+
}
113+
}
114+
};
115+
}
116+
73117
/// Implements bitcoind JSON-RPC API method `gettxout`
74118
#[macro_export]
75119
macro_rules! impl_client_v17__gettxout {

client/src/client_sync/v17/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ 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!();
30+
crate::impl_client_v17__getblockheader!();
2831

2932
// == Control ==
3033
crate::impl_client_v17__stop!();

integration_test/src/v17/blockchain.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,56 @@ 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+
110+
/// Requires `Client` to be in scope and to implement `getblockheader`.
111+
#[macro_export]
112+
macro_rules! impl_test_v17__getblockheader {
113+
() => {
114+
#[test]
115+
fn get_block_header() { // verbose = false
116+
let bitcoind = $crate::bitcoind_no_wallet();
117+
let json = bitcoind.client.get_block_hash(0).expect("getblockhash");
118+
let hash = json.block_hash().expect("failed to get block hash");
119+
let json = bitcoind.client.get_block_header(&hash).expect("getblockheader");
120+
assert!(json.into_model().is_ok());
121+
}
122+
123+
#[test]
124+
fn get_block_header_verbose() { // verbose = true
125+
let bitcoind = $crate::bitcoind_no_wallet();
126+
let json = bitcoind.client.get_block_hash(0).expect("getblockhash");
127+
let hash = json.block_hash().expect("failed to get block hash");
128+
let json = bitcoind.client.get_block_header_verbose(&hash).expect("getblockheader");
129+
assert!(json.into_model().is_ok());
130+
}
131+
};
132+
}
133+
84134
/// Requires `Client` to be in scope and to implement `get_tx_out`.
85135
#[macro_export]
86136
macro_rules! impl_test_v17__gettxout {

integration_test/tests/v17_api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ 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!();
17+
impl_test_v17__getblockheader!();
1518
}
1619

1720
// == Control ==

json/src/model/blockchain.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use std::collections::BTreeMap;
99

1010
use bitcoin::address::NetworkUnchecked;
1111
use bitcoin::{
12-
block, Address, Block, BlockHash, CompactTarget, Network, TxOut, Txid, Weight, Work,
12+
block, Address, Block, BlockHash, CompactTarget, Network, TxMerkleNode, TxOut, Txid, Weight,
13+
Work,
1314
};
1415
use serde::{Deserialize, Serialize};
1516

@@ -176,6 +177,51 @@ pub struct Bip9SoftforkStatistics {
176177
pub possible: Option<bool>,
177178
}
178179

180+
/// Models the result of JSON-RPC method `getblockcount`.
181+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
182+
pub struct GetBlockCount(pub u64);
183+
184+
/// Models the result of JSON-RPC method `getblockhash`.
185+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
186+
pub struct GetBlockHash(pub BlockHash);
187+
188+
/// Models the result of JSON-RPC method `getblockheader`.
189+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
190+
pub struct GetBlockHeader(pub block::Header);
191+
192+
/// Models the result of JSON-RPC method `getblockheader`.
193+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
194+
pub struct GetBlockHeaderVerbose {
195+
/// the block hash (same as provided).
196+
pub hash: BlockHash,
197+
/// The number of confirmations, or -1 if the block is not on the main chain.
198+
pub confirmations: i64,
199+
/// The block height or index.
200+
pub height: u64,
201+
/// Block version, now repurposed for soft fork signalling.
202+
pub version: block::Version,
203+
/// The root hash of the Merkle tree of transactions in the block.
204+
pub merkle_root: TxMerkleNode,
205+
/// The timestamp of the block, as claimed by the miner (seconds since epoch (Jan 1 1970 GMT).
206+
pub time: u64,
207+
/// The median block time in seconds since epoch (Jan 1 1970 GMT).
208+
pub median_time: u64,
209+
/// The nonce.
210+
pub nonce: u64,
211+
/// The target value below which the blockhash must lie.
212+
pub bits: CompactTarget,
213+
/// The difficulty.
214+
pub difficulty: f64,
215+
/// Expected number of hashes required to produce the current chain.
216+
pub chainwork: Work,
217+
/// The number of transactions in the block.
218+
pub n_tx: u32,
219+
/// The hash of the previous block (if available).
220+
pub previous_block_hash: Option<BlockHash>,
221+
/// The hash of the next block (if available).
222+
pub next_block_hash: Option<BlockHash>,
223+
}
224+
179225
/// Models the result of JSON-RPC method `gettxout`.
180226
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
181227
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, GetBlockHeader, GetBlockHeaderVerbose, GetBlockVerbosityOne,
31+
GetBlockVerbosityZero, GetBlockchainInfo, GetTxOut, Softfork, SoftforkType,
3232
},
3333
generating::GenerateToAddress,
3434
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},

0 commit comments

Comments
 (0)