Skip to content

Commit b0731c1

Browse files
committed
feat: Update corepc-types to v0.11.0
- update `corepc-types` to v0.11.0 to support Bitcoind v30
1 parent 291f42a commit b0731c1

File tree

5 files changed

+71
-48
lines changed

5 files changed

+71
-48
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ authors = ["Bitcoin Dev Kit Developers"]
1212
readme = "README.md"
1313

1414
[dependencies]
15-
corepc-types = { version = "0.10.1", features = ["default"]}
15+
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]

src/client.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ use crate::error::Error;
88
use crate::jsonrpc::minreq_http::Builder;
99
use corepc_types::{
1010
bitcoin::{
11-
block::Header,
12-
consensus::{deserialize, encode::deserialize_hex},
13-
hex::FromHex,
14-
Block, BlockHash, Transaction, Txid,
11+
block::Header, consensus::encode::deserialize_hex, Block, BlockHash, Transaction, Txid,
1512
},
16-
model::{GetBlockCount, GetBlockFilter, GetRawMempool},
17-
v29::GetBlockVerboseOne,
13+
model::{GetBlockCount, GetBlockFilter, GetBlockVerboseOne, GetRawMempool},
1814
};
1915
use jsonrpc::{
2016
serde,
@@ -131,7 +127,7 @@ impl Client {
131127
}
132128
}
133129

134-
// `bitcoind` RPC methods implementation for `Client`
130+
/// `Bitcoind` RPC methods implementation for `Client`
135131
impl Client {
136132
/// Retrieves the raw block data for a given block hash (verbosity 0)
137133
///
@@ -141,39 +137,44 @@ impl Client {
141137
/// # Returns
142138
/// The deserialized `Block` struct.
143139
pub fn get_block(&self, block_hash: &BlockHash) -> Result<Block, Error> {
144-
let hex_string: String = self.call("getblock", &[json!(block_hash), json!(0)])?;
145-
let block = deserialize_hex(&hex_string).map_err(Error::DecodeHex)?;
140+
let block_string: String = self.call("getblock", &[json!(block_hash), json!(0)])?;
141+
let block = deserialize_hex(&block_string)?;
146142
Ok(block)
147143
}
148144

149145
/// Retrieves the verbose JSON representation of a block (verbosity 1)
146+
///
150147
/// # Arguments
151148
/// * `block_hash`: The hash of the block to retrieve.
152149
///
153150
/// # Returns
154151
/// The verbose block data as a `GetBlockVerboseOne` struct.
155152
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
156-
let block_verbose_one: GetBlockVerboseOne =
153+
let block: corepc_types::v30::GetBlockVerboseOne =
157154
self.call("getblock", &[json!(block_hash), json!(1)])?;
158-
Ok(block_verbose_one)
155+
let block_model = block.into_model()?;
156+
157+
Ok(block_model)
159158
}
160159

161160
/// Retrieves the hash of the tip of the best block chain.
162161
///
163162
/// # Returns
164163
/// The `BlockHash` of the chain tip.
165164
pub fn get_best_block_hash(&self) -> Result<BlockHash, Error> {
166-
let res: String = self.call("getbestblockhash", &[])?;
167-
Ok(res.parse()?)
165+
let best_block_hash: String = self.call("getbestblockhash", &[])?;
166+
Ok(best_block_hash.parse()?)
168167
}
169168

170169
/// Retrieves the number of blocks in the longest chain
171170
///
172171
/// # Returns
173-
/// The block count as a `u64`
174-
pub fn get_block_count(&self) -> Result<u64, Error> {
175-
let res: GetBlockCount = self.call("getblockcount", &[])?;
176-
Ok(res.0)
172+
/// The block count as a `u32`
173+
pub fn get_block_count(&self) -> Result<u32, Error> {
174+
let block_count: GetBlockCount = self.call("getblockcount", &[])?;
175+
let block_count_u64 = block_count.0;
176+
let block_count_u32 = block_count_u64.try_into()?;
177+
Ok(block_count_u32)
177178
}
178179

179180
/// Retrieves the block hash at a given height
@@ -184,8 +185,8 @@ impl Client {
184185
/// # Returns
185186
/// The `BlockHash` for the given height
186187
pub fn get_block_hash(&self, height: u32) -> Result<BlockHash, Error> {
187-
let hex: String = self.call("getblockhash", &[json!(height)])?;
188-
Ok(hex.parse()?)
188+
let block_hash: String = self.call("getblockhash", &[json!(height)])?;
189+
Ok(block_hash.parse()?)
189190
}
190191

191192
/// Retrieves the compact block filter for a given block
@@ -196,8 +197,8 @@ impl Client {
196197
/// # Returns
197198
/// The `GetBlockFilter` structure containing the filter data
198199
pub fn get_block_filter(&self, block_hash: &BlockHash) -> Result<GetBlockFilter, Error> {
199-
let res: GetBlockFilter = self.call("getblockfilter", &[json!(block_hash)])?;
200-
Ok(res)
200+
let block_filter: GetBlockFilter = self.call("getblockfilter", &[json!(block_hash)])?;
201+
Ok(block_filter)
201202
}
202203

203204
/// Retrieves the raw block header for a given block hash.
@@ -208,14 +209,9 @@ impl Client {
208209
/// # Returns
209210
/// The deserialized `Header` struct
210211
pub fn get_block_header(&self, block_hash: &BlockHash) -> Result<Header, Error> {
211-
let hex_string: String = self.call("getblockheader", &[json!(block_hash), json!(false)])?;
212-
213-
let bytes = Vec::<u8>::from_hex(&hex_string).map_err(Error::HexToBytes)?;
214-
215-
let header = deserialize(&bytes).map_err(|e| {
216-
Error::InvalidResponse(format!("failed to deserialize block header: {e}"))
217-
})?;
218-
212+
let header_string: String =
213+
self.call("getblockheader", &[json!(block_hash), json!(false)])?;
214+
let header = deserialize_hex(&header_string)?;
219215
Ok(header)
220216
}
221217

@@ -224,11 +220,11 @@ impl Client {
224220
/// # Returns
225221
/// A vector of `Txid`s in the raw mempool
226222
pub fn get_raw_mempool(&self) -> Result<Vec<Txid>, Error> {
227-
let res: GetRawMempool = self.call("getrawmempool", &[])?;
228-
Ok(res.0)
223+
let txids: GetRawMempool = self.call("getrawmempool", &[])?;
224+
Ok(txids.0)
229225
}
230226

231-
/// Retrieves the raw transaction data for a given transaction ID.
227+
/// Retrieves the raw transaction data for a given transaction ID
232228
///
233229
/// # Arguments
234230
/// * `txid`: The transaction ID to retrieve.
@@ -237,13 +233,7 @@ impl Client {
237233
/// The deserialized `Transaction` struct
238234
pub fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction, Error> {
239235
let hex_string: String = self.call("getrawtransaction", &[json!(txid)])?;
240-
241-
let bytes = Vec::<u8>::from_hex(&hex_string).map_err(Error::HexToBytes)?;
242-
243-
let transaction = deserialize(&bytes).map_err(|e| {
244-
Error::InvalidResponse(format!("transaction deserialization failed: {e}"))
245-
})?;
246-
236+
let transaction = deserialize_hex(&hex_string)?;
247237
Ok(transaction)
248238
}
249239
}

src/error.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Error types for the Bitcoin RPC client.
22
3-
use std::{fmt, io};
4-
5-
use corepc_types::bitcoin::{
6-
consensus,
7-
hex::{HexToArrayError, HexToBytesError},
3+
use std::{fmt, io, num::TryFromIntError};
4+
5+
use corepc_types::{
6+
bitcoin::{
7+
consensus::{self, encode::FromHexError},
8+
hex::{HexToArrayError, HexToBytesError},
9+
},
10+
v30::GetBlockVerboseOneError,
811
};
912
use jsonrpc::serde_json;
1013

@@ -17,6 +20,9 @@ pub enum Error {
1720
/// Hex deserialization error
1821
DecodeHex(consensus::encode::FromHexError),
1922

23+
/// Error converting `GetBlockVersboseOne` type into the model type
24+
GetBlockVerboseOneError(GetBlockVerboseOneError),
25+
2026
/// Missing authentication credentials.
2127
MissingAuthentication,
2228

@@ -40,6 +46,9 @@ pub enum Error {
4046

4147
/// I/O error (e.g., reading cookie file, network issues).
4248
Io(io::Error),
49+
50+
/// Error when converting an integer type to a smaller type due to overflow.
51+
Overflow(TryFromIntError),
4352
}
4453

4554
impl fmt::Display for Error {
@@ -56,6 +65,10 @@ impl fmt::Display for Error {
5665
Error::Json(e) => write!(f, "JSON error: {e}"),
5766
Error::Io(e) => write!(f, "I/O error: {e}"),
5867
Error::DecodeHex(e) => write!(f, "Hex deserialization error: {e}"),
68+
Error::GetBlockVerboseOneError(e) => {
69+
write!(f, "Error converting getblockverboseone: {e}")
70+
}
71+
Error::Overflow(e) => write!(f, "Integer conversion overflow error: {e}"),
5972
}
6073
}
6174
}
@@ -69,6 +82,8 @@ impl std::error::Error for Error {
6982
Error::HexToBytes(e) => Some(e),
7083
Error::HexToArray(e) => Some(e),
7184
Error::DecodeHex(e) => Some(e),
85+
Error::GetBlockVerboseOneError(e) => Some(e),
86+
Error::Overflow(e) => Some(e),
7287
_ => None,
7388
}
7489
}
@@ -98,3 +113,21 @@ impl From<io::Error> for Error {
98113
Error::Io(e)
99114
}
100115
}
116+
117+
impl From<TryFromIntError> for Error {
118+
fn from(e: TryFromIntError) -> Self {
119+
Error::Overflow(e)
120+
}
121+
}
122+
123+
impl From<GetBlockVerboseOneError> for Error {
124+
fn from(e: GetBlockVerboseOneError) -> Self {
125+
Error::GetBlockVerboseOneError(e)
126+
}
127+
}
128+
129+
impl From<FromHexError> for Error {
130+
fn from(e: FromHexError) -> Self {
131+
Error::DecodeHex(e)
132+
}
133+
}

tests/test_rpc_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn test_get_block_hash_for_current_height() {
148148
let block_count = client.get_block_count().expect("failed to get block count");
149149

150150
let block_hash = client
151-
.get_block_hash(block_count.try_into().unwrap())
151+
.get_block_hash(block_count)
152152
.expect("failed to get block hash");
153153

154154
assert_eq!(block_hash.to_string().len(), 64);

0 commit comments

Comments
 (0)