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

Commit

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


    //! **== Blockchain ==**
    //! - [x] `getbestblockhash`
    //! - [x] `getblock "blockhash" ( verbosity ) `
    //! - [x] `getblockchaininfo`
    //! - [x] `getblockcount`
    //! - [x] `getblockhash height`
    //! - [x] `getblockheader "hash" ( verbose )`
    //! - [x] `getblockstats hash_or_height ( stats )`
    //! - [x] `getchaintips`
    //! - [x] `getchaintxstats ( nblocks blockhash )`
    //! - [x] `getdifficulty`
    //! - [x] `getmempoolancestors txid (verbose)`
    //! - [x] `getmempooldescendants txid (verbose)`
    //! - [x] `getmempoolentry txid`
    //! - [x] `getmempoolinfo`
    //! - [x] `getrawmempool ( verbose )`
    //! - [x] `gettxout "txid" n ( include_mempool )`
    //! - [x] `gettxoutproof ["txid",...] ( blockhash )`
    //! - [x] `gettxoutsetinfo`
    //! - [x] `preciousblock "blockhash"`
    //! - [-] `pruneblockchain`
    //! - [-] `savemempool`
    //! - [-] `scantxoutset <action> ( <scanobjects> )`
    //! - [x] `verifychain ( checklevel nblocks )`
    //! - [-] `verifytxoutproof "proof"`
    //!
    //! Key:
    //! - `[ ]` means not yet done.
    //! - `[x]` marks means implemented _and_ tested.
    //! - `[-]` means it was considered and intentionally not done.
  • Loading branch information
tcharding authored Sep 11, 2024
2 parents 97ec8e8 + 4d906e9 commit e74992a
Show file tree
Hide file tree
Showing 8 changed files with 965 additions and 15 deletions.
106 changes: 106 additions & 0 deletions client/src/client_sync/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,112 @@ 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 `getblockheader`
#[macro_export]
macro_rules! impl_client_v17__getblockheader {
() => {
impl Client {
pub fn get_block_header(&self, hash: &BlockHash) -> Result<GetBlockHeader> {
self.call("getblockheader", &[into_json(hash)?, into_json(false)?])
}

// This is the same as calling getblockheader with verbose==true.
pub fn get_block_header_verbose(
&self,
hash: &BlockHash,
) -> Result<GetBlockHeaderVerbose> {
self.call("getblockheader", &[into_json(hash)?])
}
}
};
}

/// Implements bitcoind JSON-RPC API method `getblockstats`
#[macro_export]
macro_rules! impl_client_v17__getblockstats {
() => {
impl Client {
pub fn get_block_stats_by_height(&self, height: u32) -> Result<GetBlockStats> {
self.call("getblockstats", &[into_json(height)?])
}

pub fn get_block_stats_by_block_hash(&self, hash: &BlockHash) -> Result<GetBlockStats> {
self.call("getblockstats", &[into_json(hash)?])
}
}
};
}

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

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

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

/// Implements bitcoind JSON-RPC API method `getmempoolancestors`
#[macro_export]
macro_rules! impl_client_v17__getmempoolancestors {
() => {
impl Client {
pub fn get_mempool_ancestors(&self, txid: Txid) -> Result<GetMempoolAncestors> {
self.call("getmempoolancestors", &[into_json(txid)?])
}
}
};
}

/// Implements bitcoind JSON-RPC API method `gettxout`
#[macro_export]
macro_rules! impl_client_v17__gettxout {
Expand Down
8 changes: 8 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,14 @@ 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!();
crate::impl_client_v17__getblockheader!();
crate::impl_client_v17__getblockstats!();
crate::impl_client_v17__getchaintips!();
crate::impl_client_v17__getchaintxstats!();
crate::impl_client_v17__getdifficulty!();
crate::impl_client_v17__getmempoolancestors!();

// == Control ==
crate::impl_client_v17__getmemoryinfo!();
Expand Down
125 changes: 124 additions & 1 deletion integration_test/src/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,134 @@ 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 `getblockheader`.
#[macro_export]
macro_rules! impl_test_v17__getblockheader {
() => {
#[test]
fn get_block_header() { // verbose = false
let bitcoind = $crate::bitcoind_no_wallet();
let block_hash = best_block_hash();
let json = bitcoind.client.get_block_header(&block_hash).expect("getblockheader");
assert!(json.into_model().is_ok());
}

#[test]
fn get_block_header_verbose() { // verbose = true
let bitcoind = $crate::bitcoind_no_wallet();
let block_hash = best_block_hash();
let json = bitcoind.client.get_block_header_verbose(&block_hash).expect("getblockheader");
assert!(json.into_model().is_ok());
}
};
}

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

#[test]
fn get_block_stats_by_hash() { // verbose = true
let bitcoind = $crate::bitcoind_no_wallet();
let block_hash = best_block_hash();
let json = bitcoind.client.get_block_stats_by_block_hash(&block_hash).expect("getblockstats");
assert!(json.into_model().is_ok());
}
};
}

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

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

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

/// Requires `Client` to be in scope and to implement `getmempoolancestors`.
#[macro_export]
macro_rules! impl_test_v17__getmempoolancestors {
() => {
#[test]
fn get_mempool_ancestors() {
// FIXME: We need a valid txid to test this.
todo!()
}
}
}

/// Requires `Client` to be in scope and to implement `get_tx_out`.
#[macro_export]
macro_rules! impl_test_v17__gettxout {
() => {
#[test]
fn get_tx_out() { todo!() }
fn get_tx_out() {
// FIXME: We need a valid txid to test this.
todo!()
}
};
}
7 changes: 7 additions & 0 deletions integration_test/tests/v17_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ 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!();
impl_test_v17__getblockheader!();
impl_test_v17__getblockstats!();
impl_test_v17__getchaintips!();
impl_test_v17__getchaintxstats!();
impl_test_v17__getdifficulty!();
}

// == Control ==
Expand Down
Loading

0 comments on commit e74992a

Please sign in to comment.