Skip to content

Commit cbbedbe

Browse files
authored
Merge pull request #1 from ValuedMammal/tvpeter-feat/blockchain_rpc_methods_v28
feat(client): Add `v28` module
2 parents b0731c1 + df84fd8 commit cbbedbe

4 files changed

Lines changed: 113 additions & 22 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ corepc-types = { version = "0.11.0", features = ["default"]}
1616
jsonrpc = { version = "0.18.0", features = ["simple_http", "simple_tcp", "minreq_http", "simple_uds", "proxy"] }
1717

1818
[features]
19+
default = ["30_0"]
1920
30_0 = []
21+
28_0 = []

src/client.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ use corepc_types::{
1010
bitcoin::{
1111
block::Header, consensus::encode::deserialize_hex, Block, BlockHash, Transaction, Txid,
1212
},
13-
model::{GetBlockCount, GetBlockFilter, GetBlockVerboseOne, GetRawMempool},
13+
model::{GetBlockCount, GetBlockFilter, GetRawMempool},
1414
};
1515
use jsonrpc::{
1616
serde,
1717
serde_json::{self, json},
1818
Transport,
1919
};
2020

21+
#[cfg(feature = "28_0")]
22+
pub mod v28;
23+
2124
/// Client authentication methods for the Bitcoin Core JSON-RPC server
2225
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
2326
pub enum Auth {
@@ -142,21 +145,6 @@ impl Client {
142145
Ok(block)
143146
}
144147

145-
/// Retrieves the verbose JSON representation of a block (verbosity 1)
146-
///
147-
/// # Arguments
148-
/// * `block_hash`: The hash of the block to retrieve.
149-
///
150-
/// # Returns
151-
/// The verbose block data as a `GetBlockVerboseOne` struct.
152-
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
153-
let block: corepc_types::v30::GetBlockVerboseOne =
154-
self.call("getblock", &[json!(block_hash), json!(1)])?;
155-
let block_model = block.into_model()?;
156-
157-
Ok(block_model)
158-
}
159-
160148
/// Retrieves the hash of the tip of the best block chain.
161149
///
162150
/// # Returns
@@ -238,6 +226,52 @@ impl Client {
238226
}
239227
}
240228

229+
#[cfg(not(feature = "28_0"))]
230+
use corepc_types::{
231+
model::{GetBlockHeaderVerbose, GetBlockVerboseOne},
232+
v30,
233+
};
234+
235+
#[cfg(not(feature = "28_0"))]
236+
impl Client {
237+
/// Retrieves the verbose JSON representation of a block header (verbosity 1).
238+
///
239+
/// # Arguments
240+
///
241+
/// * `block_hash`: The hash of the block to retrieve.
242+
///
243+
/// # Returns
244+
///
245+
/// The verbose header as a `GetBlockHeaderVerbose` struct.
246+
pub fn get_block_header_verbose(
247+
&self,
248+
hash: &BlockHash,
249+
) -> Result<GetBlockHeaderVerbose, Error> {
250+
let header_info: v30::GetBlockHeaderVerbose =
251+
self.call("getblockheader", &[json!(hash)])?;
252+
header_info
253+
.into_model()
254+
.map_err(Error::GetBlockHeaderVerboseError)
255+
}
256+
257+
/// Retrieves the verbose JSON representation of a block (verbosity 1).
258+
///
259+
/// # Arguments
260+
///
261+
/// * `block_hash`: The hash of the block to retrieve.
262+
///
263+
/// # Returns
264+
///
265+
/// The verbose block data as a `GetBlockVerboseOne` struct.
266+
pub fn get_block_verbose(&self, hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
267+
let block_info: v30::GetBlockVerboseOne =
268+
self.call("getblock", &[json!(hash), json!(1)])?;
269+
block_info
270+
.into_model()
271+
.map_err(Error::GetBlockVerboseOneError)
272+
}
273+
}
274+
241275
#[cfg(test)]
242276
mod test_auth {
243277
use super::*;

src/client/v28.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use bitcoin::BlockHash;
2+
use corepc_types::{
3+
bitcoin,
4+
model::{GetBlockHeaderVerbose, GetBlockVerboseOne},
5+
v28,
6+
};
7+
8+
use jsonrpc::serde_json::json;
9+
10+
use crate::{Client, Error};
11+
12+
impl Client {
13+
/// Retrieves the verbose JSON representation of a block header (verbosity 1).
14+
///
15+
/// # Arguments
16+
///
17+
/// * `block_hash`: The hash of the block to retrieve.
18+
///
19+
/// # Returns
20+
///
21+
/// The verbose header as a `GetBlockHeaderVerbose` struct.
22+
pub fn get_block_header_verbose(
23+
&self,
24+
hash: &BlockHash,
25+
) -> Result<GetBlockHeaderVerbose, Error> {
26+
let header_info: v28::GetBlockHeaderVerbose =
27+
self.call("getblockheader", &[json!(hash)])?;
28+
header_info
29+
.into_model()
30+
.map_err(Error::GetBlockHeaderVerboseError)
31+
}
32+
33+
/// Retrieves the verbose JSON representation of a block (verbosity 1).
34+
///
35+
/// # Arguments
36+
///
37+
/// * `block_hash`: The hash of the block to retrieve.
38+
///
39+
/// # Returns
40+
///
41+
/// The verbose block data as a `GetBlockVerboseOne` struct.
42+
pub fn get_block_verbose(&self, hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
43+
let block_info: v28::GetBlockVerboseOne =
44+
self.call("getblock", &[json!(hash), json!(1)])?;
45+
block_info
46+
.into_model()
47+
.map_err(Error::GetBlockVerboseOneError)
48+
}
49+
}

src/error.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
33
use std::{fmt, io, num::TryFromIntError};
44

5-
use corepc_types::{
6-
bitcoin::{
7-
consensus::{self, encode::FromHexError},
8-
hex::{HexToArrayError, HexToBytesError},
9-
},
10-
v30::GetBlockVerboseOneError,
5+
use bitcoin::{
6+
consensus::{self, encode::FromHexError},
7+
hex::{HexToArrayError, HexToBytesError},
118
};
9+
use corepc_types::bitcoin;
10+
#[cfg(feature = "28_0")]
11+
use corepc_types::v17::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
12+
#[cfg(not(feature = "28_0"))]
13+
use corepc_types::v30::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
1214
use jsonrpc::serde_json;
1315

1416
/// Result type alias for the RPC client.
@@ -20,6 +22,9 @@ pub enum Error {
2022
/// Hex deserialization error
2123
DecodeHex(consensus::encode::FromHexError),
2224

25+
/// Error modeling [`GetBlockHeaderVerbose`](corepc_types::model::GetBlockHeaderVerbose).
26+
GetBlockHeaderVerboseError(GetBlockHeaderVerboseError),
27+
2328
/// Error converting `GetBlockVersboseOne` type into the model type
2429
GetBlockVerboseOneError(GetBlockVerboseOneError),
2530

@@ -65,6 +70,7 @@ impl fmt::Display for Error {
6570
Error::Json(e) => write!(f, "JSON error: {e}"),
6671
Error::Io(e) => write!(f, "I/O error: {e}"),
6772
Error::DecodeHex(e) => write!(f, "Hex deserialization error: {e}"),
73+
Error::GetBlockHeaderVerboseError(e) => write!(f, "{e}"),
6874
Error::GetBlockVerboseOneError(e) => {
6975
write!(f, "Error converting getblockverboseone: {e}")
7076
}

0 commit comments

Comments
 (0)