44//!
55//! Implementation of compact blocks data structure and algorithms.
66
7+ use alloc:: vec:: Vec ;
78use core:: convert:: Infallible ;
89use core:: { convert, fmt, mem} ;
910#[ cfg( feature = "std" ) ]
@@ -16,11 +17,9 @@ use internals::array::ArrayExt as _;
1617use internals:: ToU64 as _;
1718use io:: { BufRead , Write } ;
1819
19- use crate :: consensus:: encode:: { self , Decodable , Encodable , ReadExt , WriteExt } ;
20- use crate :: internal_macros:: { self , impl_array_newtype_stringify} ;
21- use crate :: prelude:: Vec ;
22- use crate :: transaction:: TxIdentifier ;
23- use crate :: { block, consensus, Block , BlockChecked , BlockHash , Transaction } ;
20+ use bitcoin:: consensus:: encode:: { self , Decodable , Encodable , ReadExt , WriteExt } ;
21+ use bitcoin:: transaction:: TxIdentifier ;
22+ use bitcoin:: { block, Block , BlockChecked , BlockHash , Transaction } ;
2423
2524/// A BIP-0152 error
2625#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -89,7 +88,7 @@ impl Decodable for PrefilledTransaction {
8988 fn consensus_decode < R : BufRead + ?Sized > ( r : & mut R ) -> Result < Self , encode:: Error > {
9089 let idx = r. read_compact_size ( ) ?;
9190 let idx = u16:: try_from ( idx) . map_err ( |_| {
92- consensus:: parse_failed_error ( "BIP-0152 prefilled tx index out of bounds" )
91+ crate :: consensus:: parse_failed_error ( "BIP-0152 prefilled tx index out of bounds" )
9392 } ) ?;
9493 let tx = Transaction :: consensus_decode ( r) ?;
9594 Ok ( Self { idx, tx } )
@@ -100,7 +99,6 @@ impl Decodable for PrefilledTransaction {
10099#[ derive( PartialEq , Eq , Clone , Copy , Hash , Default , PartialOrd , Ord ) ]
101100pub struct ShortId ( [ u8 ; 6 ] ) ;
102101internals:: impl_array_newtype!( ShortId , u8 , 6 ) ;
103- impl_array_newtype_stringify ! ( ShortId , 6 ) ;
104102
105103impl ShortId {
106104 /// Calculates the SipHash24 keys used to calculate short IDs.
@@ -134,6 +132,30 @@ impl ShortId {
134132 }
135133}
136134
135+ impl core:: fmt:: LowerHex for ShortId {
136+ fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
137+ hex:: display:: fmt_hex_exact!( f, 6 , & self . 0 , hex:: Case :: Lower )
138+ }
139+ }
140+
141+ impl core:: fmt:: UpperHex for ShortId {
142+ fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
143+ hex:: display:: fmt_hex_exact!( f, 6 , & self . 0 , hex:: Case :: Upper )
144+ }
145+ }
146+
147+ impl core:: fmt:: Display for ShortId {
148+ fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
149+ core:: fmt:: LowerHex :: fmt ( self , f)
150+ }
151+ }
152+
153+ impl core:: fmt:: Debug for ShortId {
154+ fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
155+ core:: fmt:: LowerHex :: fmt ( self , f)
156+ }
157+ }
158+
137159impl Encodable for ShortId {
138160 #[ inline]
139161 fn consensus_encode < W : Write + ?Sized > ( & self , w : & mut W ) -> Result < usize , io:: Error > {
@@ -177,7 +199,7 @@ impl Decodable for HeaderAndShortIds {
177199 } ;
178200 match header_short_ids. short_ids . len ( ) . checked_add ( header_short_ids. prefilled_txs . len ( ) ) {
179201 Some ( x) if x <= u16:: MAX . into ( ) => Ok ( header_short_ids) ,
180- _ => Err ( consensus:: parse_failed_error ( "indexes overflowed 16 bits" ) ) ,
202+ _ => Err ( crate :: consensus:: parse_failed_error ( "indexes overflowed 16 bits" ) ) ,
181203 }
182204 }
183205}
@@ -313,7 +335,7 @@ impl Decodable for BlockTransactionsRequest {
313335 // transactions that would be allowed in a vector.
314336 let byte_size = nb_indexes
315337 . checked_mul ( mem:: size_of :: < Transaction > ( ) )
316- . ok_or ( consensus:: parse_failed_error ( "invalid length" ) ) ?;
338+ . ok_or ( crate :: consensus:: parse_failed_error ( "invalid length" ) ) ?;
317339 if byte_size > encode:: MAX_VEC_SIZE {
318340 return Err ( encode:: ParseError :: OversizedVectorAllocation {
319341 requested : byte_size,
@@ -328,12 +350,12 @@ impl Decodable for BlockTransactionsRequest {
328350 let differential = r. read_compact_size ( ) ?;
329351 last_index = match last_index. checked_add ( differential) {
330352 Some ( i) => i,
331- None => return Err ( consensus:: parse_failed_error ( "block index overflow" ) ) ,
353+ None => return Err ( crate :: consensus:: parse_failed_error ( "block index overflow" ) ) ,
332354 } ;
333355 indexes. push ( last_index) ;
334356 last_index = match last_index. checked_add ( 1 ) {
335357 Some ( i) => i,
336- None => return Err ( consensus:: parse_failed_error ( "block index overflow" ) ) ,
358+ None => return Err ( crate :: consensus:: parse_failed_error ( "block index overflow" ) ) ,
337359 } ;
338360 }
339361 indexes
@@ -373,7 +395,7 @@ pub struct BlockTransactions {
373395 /// The transactions provided.
374396 pub transactions : Vec < Transaction > ,
375397}
376- internal_macros :: impl_consensus_encoding!( BlockTransactions , block_hash, transactions) ;
398+ crate :: consensus :: impl_consensus_encoding!( BlockTransactions , block_hash, transactions) ;
377399
378400impl BlockTransactions {
379401 /// Constructs a new [`BlockTransactions`] from a [`BlockTransactionsRequest`] and
@@ -446,14 +468,15 @@ impl<'a> Arbitrary<'a> for BlockTransactionsRequest {
446468
447469#[ cfg( test) ]
448470mod test {
471+ use alloc:: vec;
449472 use hex:: FromHex ;
450473
451474 use super :: * ;
452- use crate :: consensus:: encode:: { deserialize, serialize} ;
453- use crate :: locktime:: absolute;
454- use crate :: merkle_tree:: TxMerkleNode ;
455- use crate :: transaction:: OutPointExt ;
456- use crate :: {
475+ use bitcoin :: consensus:: encode:: { deserialize, serialize} ;
476+ use bitcoin :: locktime:: absolute;
477+ use bitcoin :: merkle_tree:: TxMerkleNode ;
478+ use bitcoin :: transaction:: OutPointExt ;
479+ use bitcoin :: {
457480 transaction, Amount , BlockChecked , BlockTime , CompactTarget , OutPoint , ScriptPubKeyBuf ,
458481 ScriptSigBuf , Sequence , TxIn , TxOut , Txid , Witness ,
459482 } ;
0 commit comments