@@ -9,7 +9,7 @@ use std::{
9
9
} ;
10
10
11
11
use futures:: { stream:: FuturesOrdered , StreamExt } ;
12
- use randomx_rs:: { RandomXCache , RandomXError , RandomXFlag , RandomXVM as VMInner } ;
12
+ use randomx_rs:: { RandomXCache , RandomXError , RandomXFlag , RandomXVM as VmInner } ;
13
13
use rayon:: prelude:: * ;
14
14
use thread_local:: ThreadLocal ;
15
15
use tower:: ServiceExt ;
@@ -33,37 +33,37 @@ const RX_SEEDS_CACHED: usize = 2;
33
33
34
34
/// A multithreaded randomX VM.
35
35
#[ derive( Debug ) ]
36
- pub struct RandomXVM {
36
+ pub struct RandomXVm {
37
37
/// These RandomX VMs all share the same cache.
38
- vms : ThreadLocal < VMInner > ,
38
+ vms : ThreadLocal < VmInner > ,
39
39
/// The RandomX cache.
40
40
cache : RandomXCache ,
41
41
/// The flags used to start the RandomX VMs.
42
42
flags : RandomXFlag ,
43
43
}
44
44
45
- impl RandomXVM {
45
+ impl RandomXVm {
46
46
/// Create a new multithreaded randomX VM with the provided seed.
47
47
pub fn new ( seed : & [ u8 ; 32 ] ) -> Result < Self , RandomXError > {
48
48
// TODO: allow passing in flags.
49
49
let flags = RandomXFlag :: get_recommended_flags ( ) ;
50
50
51
51
let cache = RandomXCache :: new ( flags, seed. as_slice ( ) ) ?;
52
52
53
- Ok ( RandomXVM {
53
+ Ok ( RandomXVm {
54
54
vms : ThreadLocal :: new ( ) ,
55
55
cache,
56
56
flags,
57
57
} )
58
58
}
59
59
}
60
60
61
- impl RandomX for RandomXVM {
61
+ impl RandomX for RandomXVm {
62
62
type Error = RandomXError ;
63
63
64
64
fn calculate_hash ( & self , buf : & [ u8 ] ) -> Result < [ u8 ; 32 ] , Self :: Error > {
65
65
self . vms
66
- . get_or_try ( || VMInner :: new ( self . flags , Some ( self . cache . clone ( ) ) , None ) ) ?
66
+ . get_or_try ( || VmInner :: new ( self . flags , Some ( self . cache . clone ( ) ) , None ) ) ?
67
67
. calculate_hash ( buf)
68
68
. map ( |out| out. try_into ( ) . unwrap ( ) )
69
69
}
@@ -72,17 +72,17 @@ impl RandomX for RandomXVM {
72
72
/// The randomX VMs cache, keeps the VM needed to calculate the current block's PoW hash (if a VM is needed) and a
73
73
/// couple more around this VM.
74
74
#[ derive( Clone , Debug ) ]
75
- pub struct RandomXVMCache {
75
+ pub struct RandomXVmCache {
76
76
/// The top [`RX_SEEDS_CACHED`] RX seeds.
77
77
pub ( crate ) seeds : VecDeque < ( usize , [ u8 ; 32 ] ) > ,
78
78
/// The VMs for `seeds` (if after hf 12, otherwise this will be empty).
79
- pub ( crate ) vms : HashMap < usize , Arc < RandomXVM > > ,
79
+ pub ( crate ) vms : HashMap < usize , Arc < RandomXVm > > ,
80
80
81
81
/// A single cached VM that was given to us from a part of Cuprate.
82
- pub ( crate ) cached_vm : Option < ( [ u8 ; 32 ] , Arc < RandomXVM > ) > ,
82
+ pub ( crate ) cached_vm : Option < ( [ u8 ; 32 ] , Arc < RandomXVm > ) > ,
83
83
}
84
84
85
- impl RandomXVMCache {
85
+ impl RandomXVmCache {
86
86
#[ instrument( name = "init_rx_vm_cache" , level = "info" , skip( database) ) ]
87
87
pub async fn init_from_chain_height < D : Database + Clone > (
88
88
chain_height : usize ,
@@ -106,7 +106,7 @@ impl RandomXVMCache {
106
106
. map ( |( height, seed) | {
107
107
(
108
108
* height,
109
- Arc :: new ( RandomXVM :: new ( seed) . expect ( "Failed to create RandomX VM!" ) ) ,
109
+ Arc :: new ( RandomXVm :: new ( seed) . expect ( "Failed to create RandomX VM!" ) ) ,
110
110
)
111
111
} )
112
112
. collect ( )
@@ -117,15 +117,15 @@ impl RandomXVMCache {
117
117
HashMap :: new ( )
118
118
} ;
119
119
120
- Ok ( RandomXVMCache {
120
+ Ok ( RandomXVmCache {
121
121
seeds,
122
122
vms,
123
123
cached_vm : None ,
124
124
} )
125
125
}
126
126
127
127
/// Add a randomX VM to the cache, with the seed it was created with.
128
- pub fn add_vm ( & mut self , vm : ( [ u8 ; 32 ] , Arc < RandomXVM > ) ) {
128
+ pub fn add_vm ( & mut self , vm : ( [ u8 ; 32 ] , Arc < RandomXVm > ) ) {
129
129
self . cached_vm . replace ( vm) ;
130
130
}
131
131
@@ -136,7 +136,7 @@ impl RandomXVMCache {
136
136
height : usize ,
137
137
chain : Chain ,
138
138
database : D ,
139
- ) -> Result < Arc < RandomXVM > , ExtendedConsensusError > {
139
+ ) -> Result < Arc < RandomXVm > , ExtendedConsensusError > {
140
140
let seed_height = randomx_seed_height ( height) ;
141
141
142
142
let BlockchainResponse :: BlockHash ( seed_hash) = database
@@ -156,13 +156,13 @@ impl RandomXVMCache {
156
156
}
157
157
}
158
158
159
- let alt_vm = rayon_spawn_async ( move || Arc :: new ( RandomXVM :: new ( & seed_hash) . unwrap ( ) ) ) . await ;
159
+ let alt_vm = rayon_spawn_async ( move || Arc :: new ( RandomXVm :: new ( & seed_hash) . unwrap ( ) ) ) . await ;
160
160
161
161
Ok ( alt_vm)
162
162
}
163
163
164
164
/// Get the main-chain RandomX VMs.
165
- pub async fn get_vms ( & mut self ) -> HashMap < usize , Arc < RandomXVM > > {
165
+ pub async fn get_vms ( & mut self ) -> HashMap < usize , Arc < RandomXVm > > {
166
166
match self . seeds . len ( ) . checked_sub ( self . vms . len ( ) ) {
167
167
// No difference in the amount of seeds to VMs.
168
168
Some ( 0 ) => ( ) ,
@@ -184,7 +184,7 @@ impl RandomXVMCache {
184
184
}
185
185
} ;
186
186
187
- rayon_spawn_async ( move || Arc :: new ( RandomXVM :: new ( & next_seed_hash) . unwrap ( ) ) )
187
+ rayon_spawn_async ( move || Arc :: new ( RandomXVm :: new ( & next_seed_hash) . unwrap ( ) ) )
188
188
. await
189
189
} ;
190
190
@@ -200,7 +200,7 @@ impl RandomXVMCache {
200
200
seeds_clone
201
201
. par_iter ( )
202
202
. map ( |( height, seed) | {
203
- let vm = RandomXVM :: new ( seed) . expect ( "Failed to create RandomX VM!" ) ;
203
+ let vm = RandomXVm :: new ( seed) . expect ( "Failed to create RandomX VM!" ) ;
204
204
let vm = Arc :: new ( vm) ;
205
205
( * height, vm)
206
206
} )
0 commit comments