@@ -8,13 +8,9 @@ use crate::error::Error;
88use crate :: jsonrpc:: minreq_http:: Builder ;
99use 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} ;
1915use 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`
135131impl 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}
0 commit comments