1
+ use crate :: serde:: RandomState ;
1
2
use bitvec:: prelude:: * ;
2
3
use bitvec:: vec:: BitVec ;
3
- use rand:: Rng ;
4
4
/// When deserializing a clvm object, a stack of deserialized child objects
5
5
/// is created, which can be used with back-references. A `ReadCacheLookup` keeps
6
6
/// track of the state of this stack and all child objects under each root
@@ -19,51 +19,9 @@ use rand::Rng;
19
19
///
20
20
/// All hashes correspond to sha256 tree hashes.
21
21
use std:: collections:: { HashMap , HashSet } ;
22
- use std:: hash:: { BuildHasher , Hasher } ;
23
22
24
23
use super :: bytes32:: { hash_blob, hash_blobs, Bytes32 } ;
25
24
26
- #[ derive( Default , Clone , Copy ) ]
27
- pub struct IdentityHash ( u64 , u64 ) ;
28
-
29
- impl IdentityHash {
30
- fn new ( salt : u64 ) -> Self {
31
- Self ( 0 , salt)
32
- }
33
- }
34
-
35
- impl Hasher for IdentityHash {
36
- fn finish ( & self ) -> u64 {
37
- self . 0
38
- }
39
-
40
- fn write ( & mut self , bytes : & [ u8 ] ) {
41
- self . 0 =
42
- u64:: from_le_bytes ( bytes[ 0 ..8 ] . try_into ( ) . expect ( "expected 32 byte hashes" ) ) ^ self . 1 ;
43
- }
44
-
45
- fn write_u64 ( & mut self , _i : u64 ) {
46
- panic ! ( "This hasher only takes bytes" ) ;
47
- }
48
- }
49
-
50
- pub struct RandomState ( u64 ) ;
51
-
52
- impl RandomState {
53
- fn new ( ) -> Self {
54
- let mut rng = rand:: thread_rng ( ) ;
55
- Self ( rng. gen ( ) )
56
- }
57
- }
58
-
59
- impl BuildHasher for RandomState {
60
- type Hasher = IdentityHash ;
61
-
62
- fn build_hasher ( & self ) -> Self :: Hasher {
63
- IdentityHash :: new ( self . 0 )
64
- }
65
- }
66
-
67
25
#[ derive( Debug ) ]
68
26
pub struct ReadCacheLookup {
69
27
root_hash : Bytes32 ,
@@ -91,9 +49,9 @@ impl ReadCacheLookup {
91
49
let read_stack = Vec :: with_capacity ( 1000 ) ;
92
50
// all keys in count and parent_lookup are tree-hashes. There's no need
93
51
// to hash them again for the hash map
94
- let mut count = HashMap :: with_hasher ( RandomState :: new ( ) ) ;
52
+ let mut count = HashMap :: with_hasher ( RandomState :: default ( ) ) ;
95
53
count. insert ( root_hash, 1 ) ;
96
- let parent_lookup = HashMap :: with_hasher ( RandomState :: new ( ) ) ;
54
+ let parent_lookup = HashMap :: with_hasher ( RandomState :: default ( ) ) ;
97
55
Self {
98
56
root_hash,
99
57
read_stack,
@@ -178,8 +136,10 @@ impl ReadCacheLookup {
178
136
179
137
// all the values we put in this hash set are themselves sha256 hashes.
180
138
// There's no point in hashing the hashes
181
- let mut seen_ids =
182
- HashSet :: < & Bytes32 , RandomState > :: with_capacity_and_hasher ( 1000 , RandomState :: new ( ) ) ;
139
+ let mut seen_ids = HashSet :: < & Bytes32 , RandomState > :: with_capacity_and_hasher (
140
+ 1000 ,
141
+ RandomState :: default ( ) ,
142
+ ) ;
183
143
184
144
let max_bytes_for_path_encoding = serialized_length - 2 ; // 1 byte for 0xfe, 1 min byte for savings
185
145
let max_path_length: usize = ( max_bytes_for_path_encoding. saturating_mul ( 8 ) - 1 )
0 commit comments