1+ use alloy_eips:: BlockNumHash ;
12use alloy_primitives:: B256 ;
23use bytes:: BufMut ;
3- use derive_more:: { Constructor , From , Into } ;
4+ use derive_more:: { From , Into } ;
45use reth_db:: {
56 table:: { Compress , Decompress } ,
67 DatabaseError ,
@@ -10,16 +11,16 @@ use serde::{Deserialize, Serialize};
1011/// Wrapper for block number and block hash tuple to implement [`Compress`]/[`Decompress`].
1112///
1213/// Used for storing block metadata (number + hash).
13- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize , From , Into , Constructor ) ]
14- pub struct BlockNumberHash ( pub u64 , pub B256 ) ;
14+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Serialize , Deserialize , From , Into ) ]
15+ pub struct BlockNumberHash ( BlockNumHash ) ;
1516
1617impl Compress for BlockNumberHash {
1718 type Compressed = Vec < u8 > ;
1819
1920 fn compress_to_buf < B : BufMut + AsMut < [ u8 ] > > ( & self , buf : & mut B ) {
2021 // Encode block number (8 bytes, big-endian) + hash (32 bytes) = 40 bytes total
21- buf. put_u64 ( self . 0 ) ;
22- buf. put_slice ( self . 1 . as_slice ( ) ) ;
22+ buf. put_u64 ( self . 0 . number ) ;
23+ buf. put_slice ( self . 0 . hash . as_slice ( ) ) ;
2324 }
2425}
2526
@@ -29,11 +30,27 @@ impl Decompress for BlockNumberHash {
2930 return Err ( DatabaseError :: Decode ) ;
3031 }
3132
32- let block_number =
33- u64:: from_be_bytes ( value[ ..8 ] . try_into ( ) . map_err ( |_| DatabaseError :: Decode ) ?) ;
33+ let number = u64:: from_be_bytes ( value[ ..8 ] . try_into ( ) . map_err ( |_| DatabaseError :: Decode ) ?) ;
3434 let hash = B256 :: from_slice ( & value[ 8 ..40 ] ) ;
3535
36- Ok ( Self ( block_number, hash) )
36+ Ok ( Self ( BlockNumHash { number, hash } ) )
37+ }
38+ }
39+
40+ impl BlockNumberHash {
41+ /// Create new instance.
42+ pub const fn new ( number : u64 , hash : B256 ) -> Self {
43+ Self ( BlockNumHash { number, hash } )
44+ }
45+
46+ /// Get the block number.
47+ pub const fn number ( & self ) -> u64 {
48+ self . 0 . number
49+ }
50+
51+ /// Get the block hash.
52+ pub const fn hash ( & self ) -> B256 {
53+ self . 0 . hash
3754 }
3855}
3956
@@ -45,9 +62,9 @@ mod tests {
4562 #[ test]
4663 fn test_block_number_hash_roundtrip ( ) {
4764 let test_cases = vec ! [
48- BlockNumberHash ( 0 , B256 :: ZERO ) ,
49- BlockNumberHash ( 42 , B256 :: repeat_byte( 0xaa ) ) ,
50- BlockNumberHash ( u64 :: MAX , B256 :: repeat_byte( 0xff ) ) ,
65+ BlockNumberHash :: new ( 0 , B256 :: ZERO ) ,
66+ BlockNumberHash :: new ( 42 , B256 :: repeat_byte( 0xaa ) ) ,
67+ BlockNumberHash :: new ( u64 :: MAX , B256 :: repeat_byte( 0xff ) ) ,
5168 ] ;
5269
5370 for original in test_cases {
0 commit comments