diff --git a/.github/workflows/build_binaries.yml b/.github/workflows/build_binaries.yml index 58a851b540..aba532f474 100644 --- a/.github/workflows/build_binaries.yml +++ b/.github/workflows/build_binaries.yml @@ -42,7 +42,7 @@ concurrency: group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/v') || github.ref != 'refs/heads/development' || github.ref != 'refs/heads/nextnet' || github.ref != 'refs/heads/stagenet' }} -permissions: {} +permissions: { } jobs: matrix-prep: diff --git a/Cargo.lock b/Cargo.lock index 0d9e861b8d..1c6662159d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5588,9 +5588,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smart-default" @@ -6629,18 +6629,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.50" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.50" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", @@ -7630,7 +7630,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.0", + "windows-targets 0.52.6", ] [[package]] @@ -7665,17 +7665,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -7692,9 +7693,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -7710,9 +7711,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -7728,9 +7729,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.0" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -7746,9 +7753,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -7764,9 +7771,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -7782,9 +7789,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -7800,9 +7807,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -7900,9 +7907,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.6.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] diff --git a/base_layer/core/src/proof_of_work/monero_rx/helpers.rs b/base_layer/core/src/proof_of_work/monero_rx/helpers.rs index 5b0dae7ea6..100e09c4b7 100644 --- a/base_layer/core/src/proof_of_work/monero_rx/helpers.rs +++ b/base_layer/core/src/proof_of_work/monero_rx/helpers.rs @@ -65,7 +65,7 @@ pub fn randomx_difficulty( let monero_pow_data = verify_header(header, genesis_block_hash, consensus)?; trace!(target: LOG_TARGET, "Valid Monero data: {}", monero_pow_data); let blockhashing_blob = monero_pow_data.to_blockhashing_blob(); - let vm = randomx_factory.create(monero_pow_data.randomx_key())?; + let vm = randomx_factory.create(monero_pow_data.randomx_key(), None, None)?; get_random_x_difficulty(&blockhashing_blob, &vm).map(|(diff, _)| diff) } @@ -1245,7 +1245,7 @@ mod test { let key = from_hex("2aca6501719a5c7ab7d4acbc7cc5d277b57ad8c27c6830788c2d5a596308e5b1").unwrap(); let rx = RandomXFactory::default(); - let (difficulty, hash) = get_random_x_difficulty(&input, &rx.create(&key).unwrap()).unwrap(); + let (difficulty, hash) = get_random_x_difficulty(&input, &rx.create(&key, None, None).unwrap()).unwrap(); assert_eq!( hash.to_hex(), "f68fbc8cc85bde856cd1323e9f8e6f024483038d728835de2f8c014ff6260000" diff --git a/base_layer/core/src/proof_of_work/monero_rx/merkle_tree.rs b/base_layer/core/src/proof_of_work/monero_rx/merkle_tree.rs index faaeeba537..93184fa968 100644 --- a/base_layer/core/src/proof_of_work/monero_rx/merkle_tree.rs +++ b/base_layer/core/src/proof_of_work/monero_rx/merkle_tree.rs @@ -423,7 +423,7 @@ mod test { fn randomx_hash(input: &[u8], key: &str) -> String { let key = from_hex(key).unwrap(); RandomXFactory::default() - .create(&key) + .create(&key, None, None) .unwrap() .calculate_hash(input) .unwrap() diff --git a/base_layer/core/src/proof_of_work/monero_rx/pow_data.rs b/base_layer/core/src/proof_of_work/monero_rx/pow_data.rs index 14ebf802e0..23b9131967 100644 --- a/base_layer/core/src/proof_of_work/monero_rx/pow_data.rs +++ b/base_layer/core/src/proof_of_work/monero_rx/pow_data.rs @@ -47,7 +47,7 @@ use crate::{ proof_of_work::monero_rx::helpers::create_block_hashing_blob, }; -/// This is a struct to deserialize the data from he pow field into data required for the randomX Monero merged mine +/// This is a struct to deserialize the data from the pow field into data required for the randomX Monero merged mine /// pow. #[derive(Clone, Debug)] pub struct MoneroPowData { diff --git a/base_layer/core/src/proof_of_work/randomx_factory.rs b/base_layer/core/src/proof_of_work/randomx_factory.rs index fb4889d3fa..4f96871f0f 100644 --- a/base_layer/core/src/proof_of_work/randomx_factory.rs +++ b/base_layer/core/src/proof_of_work/randomx_factory.rs @@ -9,7 +9,7 @@ use std::{ }; use log::*; -use randomx_rs::{RandomXCache, RandomXError, RandomXFlag, RandomXVM}; +use randomx_rs::{RandomXCache, RandomXDataset, RandomXError, RandomXFlag, RandomXVM}; const LOG_TARGET: &str = "c::pow::randomx_factory"; @@ -35,32 +35,21 @@ pub struct RandomXVMInstance { } impl RandomXVMInstance { - fn create(key: &[u8], flags: RandomXFlag) -> Result { - let (flags, cache) = match RandomXCache::new(flags, key) { - Ok(cache) => (flags, cache), - Err(err) => { - warn!( - target: LOG_TARGET, - "Error initializing RandomX cache with flags {:?}. {:?}. Fallback to default flags", flags, err - ); - // This is informed by how RandomX falls back on any cache allocation failure - // https://github.com/xmrig/xmrig/blob/02b2b87bb685ab83b132267aa3c2de0766f16b8b/src/crypto/rx/RxCache.cpp#L88 - let flags = RandomXFlag::FLAG_DEFAULT; - let cache = RandomXCache::new(flags, key)?; - (flags, cache) - }, - }; - + fn create( + key: &[u8], + flags: RandomXFlag, + cache: Option, + dataset: Option, + ) -> Result { // Note: Memory required per VM in light mode is 256MB - let vm = RandomXVM::new(flags, Some(cache), None)?; - - // Note: No dataset is initialized here because we want to run in light mode. Only a cache - // is required by the VM for verification, giving it a dataset will only make the VM - // consume more memory than necessary. Dataset is currently an optional value as it may be - // useful at some point in future. // Note: RandomXFlag::FULL_MEM and RandomXFlag::LARGE_PAGES are incompatible with // light mode. These are not set by RandomX automatically even in fast mode. + let cache = match cache { + Some(c) => c, + None => RandomXCache::new(flags, key)?, + }; + let vm = RandomXVM::new(flags, Some(cache), dataset)?; Ok(Self { #[allow(clippy::arc_with_non_send_sync)] @@ -106,15 +95,26 @@ impl RandomXFactory { } } + pub fn new_with_flags(max_vms: usize, flags: RandomXFlag) -> Self { + Self { + inner: Arc::new(RwLock::new(RandomXFactoryInner::new_with_flags(max_vms, flags))), + } + } + /// Create a new RandomX VM instance with the specified key - pub fn create(&self, key: &[u8]) -> Result { + pub fn create( + &self, + key: &[u8], + cache: Option, + dataset: Option, + ) -> Result { let res; { let mut inner = self .inner .write() .map_err(|_| RandomXVMFactoryError::PoisonedLockError)?; - res = inner.create(key)?; + res = inner.create(key, cache, dataset)?; } Ok(res) } @@ -158,8 +158,25 @@ impl RandomXFactoryInner { } } + pub(crate) fn new_with_flags(max_vms: usize, flags: RandomXFlag) -> Self { + debug!( + target: LOG_TARGET, + "RandomX factory started with {} max VMs and recommended flags = {:?}", max_vms, flags + ); + Self { + flags, + vms: Default::default(), + max_vms, + } + } + /// Create a new RandomXVMInstance - pub(crate) fn create(&mut self, key: &[u8]) -> Result { + pub(crate) fn create( + &mut self, + key: &[u8], + cache: Option, + dataset: Option, + ) -> Result { if let Some(entry) = self.vms.get_mut(key) { let vm = entry.1.clone(); entry.0 = Instant::now(); @@ -167,20 +184,12 @@ impl RandomXFactoryInner { } if self.vms.len() >= self.max_vms { - let mut oldest_value = Instant::now(); - let mut oldest_key = None; - for (k, v) in &self.vms { - if v.0 < oldest_value { - oldest_key = Some(k.clone()); - oldest_value = v.0; - } - } - if let Some(k) = oldest_key { - self.vms.remove(&k); + if let Some(oldest_key) = self.vms.iter().min_by_key(|(_, (i, _))| *i).map(|(k, _)| k.clone()) { + self.vms.remove(&oldest_key); } } - let vm = RandomXVMInstance::create(key, self.flags)?; + let vm = RandomXVMInstance::create(key, self.flags, cache, dataset)?; self.vms.insert(Vec::from(key), (Instant::now(), vm.clone())); @@ -216,14 +225,14 @@ mod test { let factory = RandomXFactory::new(2); let key = b"some-key"; - let vm = factory.create(&key[..]).unwrap(); + let vm = factory.create(&key[..], None, None).unwrap(); let preimage = b"hashme"; let hash1 = vm.calculate_hash(&preimage[..]).unwrap(); - let vm = factory.create(&key[..]).unwrap(); + let vm = factory.create(&key[..], None, None).unwrap(); assert_eq!(vm.calculate_hash(&preimage[..]).unwrap(), hash1); let key = b"another-key"; - let vm = factory.create(&key[..]).unwrap(); + let vm = factory.create(&key[..], None, None).unwrap(); assert_ne!(vm.calculate_hash(&preimage[..]).unwrap(), hash1); } @@ -236,7 +245,7 @@ mod test { let factory = factory.clone(); threads.push(tokio::spawn(async move { let key = b"some-key"; - let vm = factory.create(&key[..]).unwrap(); + let vm = factory.create(&key[..], None, None).unwrap(); let preimage = b"hashme"; let _hash = vm.calculate_hash(&preimage[..]).unwrap(); })); diff --git a/common/config/presets/h_collectibles.toml b/common/config/presets/i_collectibles.toml similarity index 100% rename from common/config/presets/h_collectibles.toml rename to common/config/presets/i_collectibles.toml diff --git a/common/config/presets/i_indexer.toml b/common/config/presets/j_indexer.toml similarity index 100% rename from common/config/presets/i_indexer.toml rename to common/config/presets/j_indexer.toml diff --git a/common/config/presets/j_dan_wallet_daemon.toml b/common/config/presets/k_dan_wallet_daemon.toml similarity index 100% rename from common/config/presets/j_dan_wallet_daemon.toml rename to common/config/presets/k_dan_wallet_daemon.toml diff --git a/common/src/configuration/utils.rs b/common/src/configuration/utils.rs index cacd4cc0a6..9b0b300daf 100644 --- a/common/src/configuration/utils.rs +++ b/common/src/configuration/utils.rs @@ -136,9 +136,9 @@ pub fn get_default_config(use_mining_config: bool) -> [&'static str; 12] { include_str!("../../config/presets/g_miner.toml"), include_str!("../../config/presets/f_merge_mining_proxy.toml"), include_str!("../../config/presets/e_validator_node.toml"), - include_str!("../../config/presets/h_collectibles.toml"), - include_str!("../../config/presets/i_indexer.toml"), - include_str!("../../config/presets/j_dan_wallet_daemon.toml"), + include_str!("../../config/presets/i_collectibles.toml"), + include_str!("../../config/presets/j_indexer.toml"), + include_str!("../../config/presets/k_dan_wallet_daemon.toml"), ] }