1
1
use crate :: http:: { BinaryResponse , JsonResponse } ;
2
- use crate :: utils:: hex_to_uint256 ;
2
+ use crate :: utils:: hex_to_work ;
3
3
use crate :: { BlockHeaderData , BlockSourceError } ;
4
4
5
- use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
5
+ use bitcoin:: blockdata:: block:: { Block , Header } ;
6
6
use bitcoin:: consensus:: encode;
7
7
use bitcoin:: hash_types:: { BlockHash , TxMerkleNode , Txid } ;
8
8
use bitcoin:: hashes:: hex:: FromHex ;
@@ -88,17 +88,21 @@ impl TryFrom<serde_json::Value> for BlockHeaderData {
88
88
} }
89
89
90
90
Ok ( BlockHeaderData {
91
- header : BlockHeader {
92
- version : get_field ! ( "version" , as_i64) . try_into ( ) . map_err ( |_| ( ) ) ?,
91
+ header : Header {
92
+ version : bitcoin:: blockdata:: block:: Version :: from_consensus (
93
+ get_field ! ( "version" , as_i64) . try_into ( ) . map_err ( |_| ( ) ) ?
94
+ ) ,
93
95
prev_blockhash : if let Some ( hash_str) = response. get ( "previousblockhash" ) {
94
- BlockHash :: from_hex ( hash_str. as_str ( ) . ok_or ( ( ) ) ?) . map_err ( |_| ( ) ) ?
96
+ BlockHash :: from_str ( hash_str. as_str ( ) . ok_or ( ( ) ) ?) . map_err ( |_| ( ) ) ?
95
97
} else { BlockHash :: all_zeros ( ) } ,
96
- merkle_root : TxMerkleNode :: from_hex ( get_field ! ( "merkleroot" , as_str) ) . map_err ( |_| ( ) ) ?,
98
+ merkle_root : TxMerkleNode :: from_str ( get_field ! ( "merkleroot" , as_str) ) . map_err ( |_| ( ) ) ?,
97
99
time : get_field ! ( "time" , as_u64) . try_into ( ) . map_err ( |_| ( ) ) ?,
98
- bits : u32:: from_be_bytes ( <[ u8 ; 4 ] >:: from_hex ( get_field ! ( "bits" , as_str) ) . map_err ( |_| ( ) ) ?) ,
100
+ bits : bitcoin:: CompactTarget :: from_consensus (
101
+ u32:: from_be_bytes ( <[ u8 ; 4 ] >:: from_hex ( get_field ! ( "bits" , as_str) ) . map_err ( |_| ( ) ) ?)
102
+ ) ,
99
103
nonce : get_field ! ( "nonce" , as_u64) . try_into ( ) . map_err ( |_| ( ) ) ?,
100
104
} ,
101
- chainwork : hex_to_uint256 ( get_field ! ( "chainwork" , as_str) ) . map_err ( |_| ( ) ) ?,
105
+ chainwork : hex_to_work ( get_field ! ( "chainwork" , as_str) ) . map_err ( |_| ( ) ) ?,
102
106
height : get_field ! ( "height" , as_u64) . try_into ( ) . map_err ( |_| ( ) ) ?,
103
107
} )
104
108
}
@@ -132,7 +136,7 @@ impl TryInto<(BlockHash, Option<u32>)> for JsonResponse {
132
136
}
133
137
134
138
let hash = match & self . 0 [ "bestblockhash" ] {
135
- serde_json:: Value :: String ( hex_data) => match BlockHash :: from_hex ( & hex_data) {
139
+ serde_json:: Value :: String ( hex_data) => match BlockHash :: from_str ( & hex_data) {
136
140
Err ( _) => return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , "invalid hex data" ) ) ,
137
141
Ok ( block_hash) => block_hash,
138
142
} ,
@@ -288,8 +292,8 @@ pub(crate) mod tests {
288
292
use super :: * ;
289
293
use bitcoin:: blockdata:: constants:: genesis_block;
290
294
use bitcoin:: hashes:: Hash ;
291
- use bitcoin:: hashes:: hex:: ToHex ;
292
295
use bitcoin:: network:: constants:: Network ;
296
+ use hex:: DisplayHex ;
293
297
use serde_json:: value:: Number ;
294
298
use serde_json:: Value ;
295
299
@@ -298,14 +302,14 @@ pub(crate) mod tests {
298
302
fn from ( data : BlockHeaderData ) -> Self {
299
303
let BlockHeaderData { chainwork, height, header } = data;
300
304
serde_json:: json!( {
301
- "chainwork" : chainwork. to_string ( ) [ "0x" . len ( ) .. ] ,
305
+ "chainwork" : chainwork. to_be_bytes ( ) . as_hex ( ) . to_string ( ) ,
302
306
"height" : height,
303
- "version" : header. version,
304
- "merkleroot" : header. merkle_root. to_hex ( ) ,
307
+ "version" : header. version. to_consensus ( ) ,
308
+ "merkleroot" : header. merkle_root. to_string ( ) ,
305
309
"time" : header. time,
306
310
"nonce" : header. nonce,
307
- "bits" : header. bits. to_hex ( ) ,
308
- "previousblockhash" : header. prev_blockhash. to_hex ( ) ,
311
+ "bits" : header. bits. to_consensus ( ) . to_be_bytes ( ) . as_hex ( ) . to_string ( ) ,
312
+ "previousblockhash" : header. prev_blockhash. to_string ( ) ,
309
313
} )
310
314
}
311
315
}
@@ -394,7 +398,7 @@ pub(crate) mod tests {
394
398
#[ test]
395
399
fn into_block_header_from_json_response_with_valid_header_array ( ) {
396
400
let genesis_block = genesis_block ( Network :: Bitcoin ) ;
397
- let best_block_header = BlockHeader {
401
+ let best_block_header = Header {
398
402
prev_blockhash : genesis_block. block_hash ( ) ,
399
403
..genesis_block. header
400
404
} ;
@@ -541,7 +545,7 @@ pub(crate) mod tests {
541
545
fn into_block_hash_from_json_response_without_height ( ) {
542
546
let block = genesis_block ( Network :: Bitcoin ) ;
543
547
let response = JsonResponse ( serde_json:: json!( {
544
- "bestblockhash" : block. block_hash( ) . to_hex ( ) ,
548
+ "bestblockhash" : block. block_hash( ) . to_string ( ) ,
545
549
} ) ) ;
546
550
match TryInto :: < ( BlockHash , Option < u32 > ) > :: try_into ( response) {
547
551
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
@@ -556,7 +560,7 @@ pub(crate) mod tests {
556
560
fn into_block_hash_from_json_response_with_unexpected_blocks_type ( ) {
557
561
let block = genesis_block ( Network :: Bitcoin ) ;
558
562
let response = JsonResponse ( serde_json:: json!( {
559
- "bestblockhash" : block. block_hash( ) . to_hex ( ) ,
563
+ "bestblockhash" : block. block_hash( ) . to_string ( ) ,
560
564
"blocks" : "foo" ,
561
565
} ) ) ;
562
566
match TryInto :: < ( BlockHash , Option < u32 > ) > :: try_into ( response) {
@@ -572,7 +576,7 @@ pub(crate) mod tests {
572
576
fn into_block_hash_from_json_response_with_invalid_height ( ) {
573
577
let block = genesis_block ( Network :: Bitcoin ) ;
574
578
let response = JsonResponse ( serde_json:: json!( {
575
- "bestblockhash" : block. block_hash( ) . to_hex ( ) ,
579
+ "bestblockhash" : block. block_hash( ) . to_string ( ) ,
576
580
"blocks" : std:: u64 :: MAX ,
577
581
} ) ) ;
578
582
match TryInto :: < ( BlockHash , Option < u32 > ) > :: try_into ( response) {
@@ -588,7 +592,7 @@ pub(crate) mod tests {
588
592
fn into_block_hash_from_json_response_with_height ( ) {
589
593
let block = genesis_block ( Network :: Bitcoin ) ;
590
594
let response = JsonResponse ( serde_json:: json!( {
591
- "bestblockhash" : block. block_hash( ) . to_hex ( ) ,
595
+ "bestblockhash" : block. block_hash( ) . to_string ( ) ,
592
596
"blocks" : 1 ,
593
597
} ) ) ;
594
598
match TryInto :: < ( BlockHash , Option < u32 > ) > :: try_into ( response) {
0 commit comments