88//! ```
99
1010use bdk_bitcoind_client:: { Auth , Client , Error } ;
11- use corepc_types:: bitcoin:: BlockHash ;
12- use std:: path:: PathBuf ;
11+ use corepc_types:: bitcoin:: { BlockHash , Txid } ;
12+ use jsonrpc:: serde_json:: json;
13+ use std:: { path:: PathBuf , str:: FromStr } ;
1314
1415/// Helper to get the test RPC URL
1516fn test_url ( ) -> String {
@@ -23,6 +24,17 @@ fn test_auth() -> Auth {
2324 Auth :: UserPass ( user, pass)
2425}
2526
27+ /// Helper to create a test client
28+ fn test_client ( ) -> Client {
29+ Client :: with_auth ( & test_url ( ) , test_auth ( ) ) . expect ( "failed to create client" )
30+ }
31+
32+ /// Helper to mine blocks
33+ fn mine_blocks ( client : & Client , n : u64 ) -> Result < Vec < String > , Error > {
34+ let address: String = client. call ( "getnewaddress" , & [ ] ) ?;
35+ client. call ( "generatetoaddress" , & [ json ! ( n) , json ! ( address) ] )
36+ }
37+
2638#[ test]
2739#[ ignore]
2840fn test_client_with_user_pass ( ) {
@@ -105,3 +117,249 @@ fn test_client_with_custom_transport() {
105117 "block hash should be 64 characters"
106118 ) ;
107119}
120+
121+ #[ test]
122+ #[ ignore]
123+ fn test_get_block_count ( ) {
124+ let client = test_client ( ) ;
125+
126+ let block_count = client. get_block_count ( ) . expect ( "failed to get block count" ) ;
127+
128+ assert ! ( block_count >= 1 ) ;
129+ }
130+
131+ #[ test]
132+ #[ ignore]
133+ fn test_get_block_hash ( ) {
134+ let client = test_client ( ) ;
135+
136+ let genesis_hash = client
137+ . get_block_hash ( 0 )
138+ . expect ( "failed to get genesis block hash" ) ;
139+
140+ assert_eq ! ( genesis_hash. to_string( ) . len( ) , 64 ) ;
141+ }
142+
143+ #[ test]
144+ #[ ignore]
145+ fn test_get_block_hash_for_current_height ( ) {
146+ let client = test_client ( ) ;
147+
148+ let block_count = client. get_block_count ( ) . expect ( "failed to get block count" ) ;
149+
150+ let block_hash = client
151+ . get_block_hash ( block_count. try_into ( ) . unwrap ( ) )
152+ . expect ( "failed to get block hash" ) ;
153+
154+ assert_eq ! ( block_hash. to_string( ) . len( ) , 64 ) ;
155+ }
156+
157+ #[ test]
158+ #[ ignore]
159+ fn test_get_block_hash_invalid_height ( ) {
160+ let client = test_client ( ) ;
161+
162+ let result = client. get_block_hash ( 999999999 ) ;
163+
164+ assert ! ( result. is_err( ) ) ;
165+ }
166+
167+ #[ test]
168+ #[ ignore]
169+ fn test_get_best_block_hash ( ) {
170+ let client = test_client ( ) ;
171+
172+ let best_hash = client
173+ . get_best_block_hash ( )
174+ . expect ( "failed to get best block hash" ) ;
175+
176+ assert_eq ! ( best_hash. to_string( ) . len( ) , 64 ) ;
177+ }
178+
179+ #[ test]
180+ #[ ignore]
181+ fn test_get_best_block_hash_changes_after_mining ( ) {
182+ let client = test_client ( ) ;
183+
184+ let hash_before = client
185+ . get_best_block_hash ( )
186+ . expect ( "failed to get best block hash" ) ;
187+
188+ mine_blocks ( & client, 1 ) . expect ( "failed to mine block" ) ;
189+
190+ let hash_after = client
191+ . get_best_block_hash ( )
192+ . expect ( "failed to get best block hash" ) ;
193+
194+ assert_ne ! ( hash_before, hash_after) ;
195+ }
196+
197+ #[ test]
198+ #[ ignore]
199+ fn test_get_block ( ) {
200+ let client = test_client ( ) ;
201+
202+ let genesis_hash = client
203+ . get_block_hash ( 0 )
204+ . expect ( "failed to get genesis hash" ) ;
205+
206+ let block = client
207+ . get_block ( & genesis_hash)
208+ . expect ( "failed to get block" ) ;
209+
210+ assert_eq ! ( block. block_hash( ) , genesis_hash) ;
211+ assert ! ( !block. txdata. is_empty( ) ) ;
212+ }
213+
214+ #[ test]
215+ #[ ignore]
216+ fn test_get_block_after_mining ( ) {
217+ let client = test_client ( ) ;
218+
219+ let hashes = mine_blocks ( & client, 1 ) . expect ( "failed to mine block" ) ;
220+ let block_hash = BlockHash :: from_str ( & hashes[ 0 ] ) . expect ( "invalid hash" ) ;
221+
222+ let block = client. get_block ( & block_hash) . expect ( "failed to get block" ) ;
223+
224+ assert_eq ! ( block. block_hash( ) , block_hash) ;
225+ assert ! ( !block. txdata. is_empty( ) ) ;
226+ }
227+
228+ #[ test]
229+ #[ ignore]
230+ fn test_get_block_invalid_hash ( ) {
231+ let client = test_client ( ) ;
232+
233+ let invalid_hash =
234+ BlockHash :: from_str ( "0000000000000000000000000000000000000000000000000000000000000000" )
235+ . unwrap ( ) ;
236+
237+ let result = client. get_block ( & invalid_hash) ;
238+
239+ assert ! ( result. is_err( ) ) ;
240+ }
241+
242+ #[ test]
243+ #[ ignore]
244+ fn test_get_block_header ( ) {
245+ let client = test_client ( ) ;
246+
247+ let genesis_hash = client
248+ . get_block_hash ( 0 )
249+ . expect ( "failed to get genesis hash" ) ;
250+
251+ let header = client
252+ . get_block_header ( & genesis_hash)
253+ . expect ( "failed to get block header" ) ;
254+
255+ assert_eq ! ( header. block_hash( ) , genesis_hash) ;
256+ }
257+
258+ #[ test]
259+ #[ ignore]
260+ fn test_get_block_header_has_valid_fields ( ) {
261+ let client = test_client ( ) ;
262+
263+ let genesis_hash = client
264+ . get_block_hash ( 0 )
265+ . expect ( "failed to get genesis hash" ) ;
266+
267+ let header = client
268+ . get_block_header ( & genesis_hash)
269+ . expect ( "failed to get block header" ) ;
270+
271+ assert ! ( header. time > 0 ) ;
272+ assert ! ( header. nonce >= 1 ) ;
273+ }
274+
275+ #[ test]
276+ #[ ignore]
277+ fn test_get_raw_mempool_empty ( ) {
278+ let client = test_client ( ) ;
279+
280+ mine_blocks ( & client, 1 ) . expect ( "failed to mine block" ) ;
281+
282+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
283+
284+ let mempool = client. get_raw_mempool ( ) . expect ( "failed to get mempool" ) ;
285+
286+ assert ! ( mempool. is_empty( ) ) ;
287+ }
288+
289+ #[ test]
290+ #[ ignore]
291+ fn test_get_raw_mempool_with_transaction ( ) {
292+ let client = test_client ( ) ;
293+
294+ mine_blocks ( & client, 101 ) . expect ( "failed to mine blocks" ) ;
295+
296+ let address: String = client
297+ . call ( "getnewaddress" , & [ ] )
298+ . expect ( "failed to get address" ) ;
299+ let txid: String = client
300+ . call ( "sendtoaddress" , & [ json ! ( address) , json ! ( 0.001 ) ] )
301+ . expect ( "failed to send transaction" ) ;
302+
303+ let mempool = client. get_raw_mempool ( ) . expect ( "failed to get mempool" ) ;
304+
305+ let txid_parsed = Txid :: from_str ( & txid) . unwrap ( ) ;
306+ assert ! ( mempool. contains( & txid_parsed) ) ;
307+ }
308+
309+ #[ test]
310+ #[ ignore]
311+ fn test_get_raw_transaction ( ) {
312+ let client = test_client ( ) ;
313+
314+ mine_blocks ( & client, 1 ) . expect ( "failed to mine block" ) ;
315+
316+ let best_hash = client
317+ . get_best_block_hash ( )
318+ . expect ( "failed to get best block hash" ) ;
319+
320+ let block = client. get_block ( & best_hash) . expect ( "failed to get block" ) ;
321+
322+ let txid = & block. txdata [ 0 ] . compute_txid ( ) ;
323+
324+ let tx = client
325+ . get_raw_transaction ( txid)
326+ . expect ( "failed to get raw transaction" ) ;
327+
328+ assert_eq ! ( tx. compute_txid( ) , * txid) ;
329+ assert ! ( !tx. input. is_empty( ) ) ;
330+ assert ! ( !tx. output. is_empty( ) ) ;
331+ }
332+
333+ #[ test]
334+ #[ ignore]
335+ fn test_get_raw_transaction_invalid_txid ( ) {
336+ let client = test_client ( ) ;
337+
338+ let fake_txid =
339+ Txid :: from_str ( "0000000000000000000000000000000000000000000000000000000000000000" ) . unwrap ( ) ;
340+
341+ let result = client. get_raw_transaction ( & fake_txid) ;
342+
343+ assert ! ( result. is_err( ) ) ;
344+ }
345+
346+ #[ test]
347+ #[ ignore]
348+ fn test_get_block_filter ( ) {
349+ let client = test_client ( ) ;
350+
351+ let genesis_hash = client
352+ . get_block_hash ( 0 )
353+ . expect ( "failed to get genesis hash" ) ;
354+
355+ let result = client. get_block_filter ( genesis_hash) ;
356+
357+ match result {
358+ Ok ( filter) => {
359+ assert ! ( !filter. filter. is_empty( ) ) ;
360+ }
361+ Err ( _) => {
362+ println ! ( "Block filters not enabled (requires -blockfilterindex=1)" ) ;
363+ }
364+ }
365+ }
0 commit comments