Skip to content

Commit e732a5b

Browse files
committed
chore(tests): use specific methods from corepc-client
- use specific methods such as: `into_model()`, and `block_hash()` to improve the usage of corepc client and it's types.
1 parent 0e02d79 commit e732a5b

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/lib.rs

+33-29
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ mod test {
267267
use tokio::sync::Mutex;
268268
#[cfg(all(feature = "blocking", feature = "async"))]
269269
use {
270-
bitcoin::{consensus, hashes::Hash, Amount},
270+
bitcoin::{hashes::Hash, Amount},
271271
corepc_node::AddressType,
272272
electrsd::electrum_client::ElectrumApi,
273273
std::time::Duration,
@@ -535,9 +535,20 @@ mod test {
535535
let _miner = MINER.lock().await;
536536
generate_blocks_and_wait(1);
537537

538-
let height: Option<u32> = BITCOIND.client.get_block_count().unwrap().0.try_into().ok();
539-
let tx_res = BITCOIND.client.get_transaction(txid).unwrap();
540-
let tx_exp: Transaction = consensus::encode::deserialize_hex(&tx_res.hex).unwrap();
538+
let tx_res = BITCOIND
539+
.client
540+
.get_transaction(txid)
541+
.unwrap()
542+
.into_model()
543+
.unwrap();
544+
let tx_exp: Transaction = tx_res.tx;
545+
let tx_block_height = BITCOIND
546+
.client
547+
.get_block_header_verbose(&tx_res.block_hash.unwrap())
548+
.unwrap()
549+
.into_model()
550+
.unwrap()
551+
.height;
541552

542553
let tx_info = blocking_client
543554
.get_tx_info(&txid)
@@ -553,21 +564,15 @@ mod test {
553564
assert_eq!(tx_info.to_tx(), tx_exp);
554565
assert_eq!(tx_info.size, tx_exp.total_size());
555566
assert_eq!(tx_info.weight(), tx_exp.weight());
556-
assert_eq!(
557-
tx_info.fee(),
558-
tx_res
559-
.fee
560-
.map(|fee| Amount::from_btc(fee.abs()).unwrap())
561-
.unwrap()
562-
);
567+
assert_eq!(tx_info.fee(), tx_res.fee.unwrap().unsigned_abs());
563568
assert!(tx_info.status.confirmed);
564569
// TODO(corepc): No .block_height field on GetTransaction ?
565-
assert_eq!(tx_info.status.block_height, height);
570+
assert_eq!(tx_info.status.block_height, Some(tx_block_height));
571+
assert_eq!(tx_info.status.block_hash, tx_res.block_hash);
566572
assert_eq!(
567-
tx_info.status.block_hash.map(|hash| hash.to_string()),
568-
tx_res.block_hash
573+
tx_info.status.block_time,
574+
tx_res.block_time.map(|bt| bt as u64)
569575
);
570-
assert!(tx_info.status.block_time >= Some(tx_res.time as u64));
571576

572577
let txid = Txid::hash(b"not exist");
573578
assert_eq!(blocking_client.get_tx_info(&txid).unwrap(), None);
@@ -583,8 +588,7 @@ mod test {
583588
.client
584589
.get_block_hash(23)
585590
.unwrap()
586-
.0
587-
.parse()
591+
.block_hash()
588592
.unwrap();
589593

590594
let block_header = blocking_client.get_header_by_hash(&block_hash).unwrap();
@@ -601,15 +605,13 @@ mod test {
601605
.client
602606
.get_block_hash(21)
603607
.unwrap()
604-
.0
605-
.parse()
608+
.block_hash()
606609
.unwrap();
607610
let next_block_hash = BITCOIND
608611
.client
609612
.get_block_hash(22)
610613
.unwrap()
611-
.0
612-
.parse()
614+
.block_hash()
613615
.unwrap();
614616

615617
let expected = BlockStatus {
@@ -658,8 +660,7 @@ mod test {
658660
.client
659661
.get_block_hash(21)
660662
.unwrap()
661-
.0
662-
.parse()
663+
.block_hash()
663664
.unwrap();
664665

665666
let expected = Some(BITCOIND.client.get_block(block_hash).unwrap());
@@ -835,8 +836,7 @@ mod test {
835836
.client
836837
.get_block_hash(21)
837838
.unwrap()
838-
.0
839-
.parse::<BlockHash>()
839+
.block_hash()
840840
.unwrap();
841841

842842
let block_hash_blocking = blocking_client.get_block_hash(21).unwrap();
@@ -854,8 +854,7 @@ mod test {
854854
.client
855855
.get_block_hash(23)
856856
.unwrap()
857-
.0
858-
.parse()
857+
.block_hash()
859858
.unwrap();
860859

861860
let txid_at_block_index = blocking_client
@@ -897,8 +896,13 @@ mod test {
897896
let _miner = MINER.lock().await;
898897
generate_blocks_and_wait(1);
899898

900-
let get_tx = BITCOIND.client.get_transaction(txid).unwrap();
901-
let expected_tx: Transaction = consensus::encode::deserialize_hex(&get_tx.hex).unwrap();
899+
let expected_tx = BITCOIND
900+
.client
901+
.get_transaction(txid)
902+
.unwrap()
903+
.into_model()
904+
.unwrap()
905+
.tx;
902906
let script = &expected_tx.output[0].script_pubkey;
903907
let scripthash_txs_txids: Vec<Txid> = blocking_client
904908
.scripthash_txs(script, None)

0 commit comments

Comments
 (0)