RandomxVm

Struct RandomxVm 

Source
pub struct RandomxVm<'a, T: 'a> { /* private fields */ }

Implementations§

Source§

impl RandomxVm<'_, RandomxCache>

Source

pub fn new( flags: RandomxFlags, cache: &RandomxCache, ) -> Result<Self, RandomxError>

Source§

impl RandomxVm<'_, RandomxDataset>

Source

pub fn new_fast( flags: RandomxFlags, dataset: &RandomxDataset, ) -> Result<Self, RandomxError>

Examples found in repository?
examples/multithreaded.rs (line 32)
10fn main() {
11    const NUM_THREADS: u32 = 8;
12    // number of hashes to perform in each thread, not the total.
13    const NUM_HASHES: u32 = 5000;
14
15    let start = Instant::now();
16
17    // Try adding `| RandomxFlags::LARGEPAGES`.
18    let flags = RandomxFlags::default() | RandomxFlags::FULLMEM;
19    let dataset = Arc::new(RandomxDataset::new(flags, b"key", NUM_THREADS as u8).unwrap());
20
21    println!("Dataset initialised in {}ms", start.elapsed().as_millis());
22
23    let mut handles = Vec::new();
24
25    let start = Instant::now();
26
27    for i in 0..NUM_THREADS {
28        let dataset = dataset.clone();
29
30        handles.push(thread::spawn(move || {
31            let mut nonce: u32 = i;
32            let vm = RandomxVm::new_fast(flags, &dataset).unwrap();
33
34            for _ in 0..NUM_HASHES {
35                let _ = vm.hash(&nonce.to_be_bytes());
36
37                // e.g. thread 0 will use nonces 0, 8, 16, ...
38                // and thread 1 will use nonces 1, 9, 17, ...
39                nonce += NUM_THREADS;
40            }
41        }));
42    }
43
44    for handle in handles {
45        let _ = handle.join();
46    }
47
48    println!(
49        "Completed {} hashes in {}ms",
50        NUM_THREADS * NUM_HASHES,
51        start.elapsed().as_millis()
52    );
53}
Source§

impl<T> RandomxVm<'_, T>

Source

pub fn hash(&self, input: &[u8]) -> [u8; 32]

Calculate the RandomX hash of some data.

use randomx_bindings::*;
let flags = RandomxFlags::default();
let cache = RandomxCache::new(flags, "key".as_bytes())?;
let vm = RandomxVm::new(flags, &cache)?;
let hash = vm.hash("input".as_bytes());
Examples found in repository?
examples/multithreaded.rs (line 35)
10fn main() {
11    const NUM_THREADS: u32 = 8;
12    // number of hashes to perform in each thread, not the total.
13    const NUM_HASHES: u32 = 5000;
14
15    let start = Instant::now();
16
17    // Try adding `| RandomxFlags::LARGEPAGES`.
18    let flags = RandomxFlags::default() | RandomxFlags::FULLMEM;
19    let dataset = Arc::new(RandomxDataset::new(flags, b"key", NUM_THREADS as u8).unwrap());
20
21    println!("Dataset initialised in {}ms", start.elapsed().as_millis());
22
23    let mut handles = Vec::new();
24
25    let start = Instant::now();
26
27    for i in 0..NUM_THREADS {
28        let dataset = dataset.clone();
29
30        handles.push(thread::spawn(move || {
31            let mut nonce: u32 = i;
32            let vm = RandomxVm::new_fast(flags, &dataset).unwrap();
33
34            for _ in 0..NUM_HASHES {
35                let _ = vm.hash(&nonce.to_be_bytes());
36
37                // e.g. thread 0 will use nonces 0, 8, 16, ...
38                // and thread 1 will use nonces 1, 9, 17, ...
39                nonce += NUM_THREADS;
40            }
41        }));
42    }
43
44    for handle in handles {
45        let _ = handle.join();
46    }
47
48    println!(
49        "Completed {} hashes in {}ms",
50        NUM_THREADS * NUM_HASHES,
51        start.elapsed().as_millis()
52    );
53}

Trait Implementations§

Source§

impl<T> Drop for RandomxVm<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Send for RandomxVm<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for RandomxVm<'a, T>

§

impl<'a, T> RefUnwindSafe for RandomxVm<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> !Sync for RandomxVm<'a, T>

§

impl<'a, T> Unpin for RandomxVm<'a, T>

§

impl<'a, T> UnwindSafe for RandomxVm<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.