pow.rs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. sync::{
  20. atomic::{AtomicBool, AtomicU32, Ordering},
  21. Arc,
  22. },
  23. thread,
  24. time::Instant,
  25. };
  26. use darkfi_sdk::num_traits::{One, Zero};
  27. use num_bigint::BigUint;
  28. use randomx::{RandomXCache, RandomXDataset, RandomXFlags, RandomXVM};
  29. use smol::channel::Receiver;
  30. use tracing::{debug, error};
  31. use crate::{
  32. blockchain::{
  33. block_store::BlockDifficulty,
  34. header_store::{
  35. Header, HeaderHash,
  36. PowData::{DarkFi, Monero},
  37. },
  38. Blockchain, BlockchainOverlayPtr,
  39. },
  40. util::{ringbuffer::RingBuffer, time::Timestamp},
  41. validator::{
  42. utils::{median, MAX_32_BYTES},
  43. RandomXFactory,
  44. },
  45. Error, Result,
  46. };
  47. // Note: We have combined some constants for better performance.
  48. /// Amount of max items(blocks) to use for next difficulty calculation.
  49. /// Must be >= 2 and == BUF_SIZE - DIFFICULTY_LAG.
  50. const DIFFICULTY_WINDOW: usize = 720;
  51. /// Amount of latest blocks to exlude from the calculation.
  52. /// Our ring buffer has length: DIFFICULTY_WINDOW + DIFFICULTY_LAG,
  53. /// but we only use DIFFICULTY_WINDOW items in calculations.
  54. /// Must be == BUF_SIZE - DIFFICULTY_WINDOW.
  55. const _DIFFICULTY_LAG: usize = 15;
  56. /// Ring buffer length.
  57. /// Must be == DIFFICULTY_WINDOW + DIFFICULTY_LAG
  58. const BUF_SIZE: usize = 735;
  59. /// Used to calculate how many items to retain for next difficulty
  60. /// calculation. We are keeping the middle items, meaning cutting
  61. /// both from frond and back of the ring buffer, ending up with max
  62. /// DIFFICULTY_WINDOW - 2*DIFFICULTY_CUT items.
  63. /// (2*DIFFICULTY_CUT <= DIFFICULTY_WINDOW-2) must be true.
  64. const _DIFFICULTY_CUT: usize = 60;
  65. /// Max items to use for next difficulty calculation.
  66. /// Must be DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT
  67. const RETAINED: usize = 600;
  68. /// Already known cutoff start index for this config
  69. const CUT_BEGIN: usize = 60;
  70. /// Already known cutoff end index for this config
  71. const CUT_END: usize = 660;
  72. /// How many most recent blocks to use to verify new blocks' timestamp
  73. const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: usize = 60;
  74. /// Time limit in the future of what blocks can be
  75. const BLOCK_FUTURE_TIME_LIMIT: Timestamp = Timestamp::from_u64(60 * 60 * 2);
  76. /// RandomX VM key changing height
  77. pub const RANDOMX_KEY_CHANGING_HEIGHT: u32 = 2048;
  78. /// RandomX VM key change delay
  79. pub const RANDOMX_KEY_CHANGE_DELAY: u32 = 64;
  80. /// This struct represents the information required by the PoW algorithm
  81. #[derive(Clone)]
  82. pub struct PoWModule {
  83. /// Genesis block timestamp
  84. pub genesis: Timestamp,
  85. /// Target block time, in seconds
  86. pub target: u32,
  87. /// Optional fixed difficulty
  88. pub fixed_difficulty: Option<BigUint>,
  89. /// Latest block timestamps ringbuffer
  90. pub timestamps: RingBuffer<Timestamp, BUF_SIZE>,
  91. /// Latest block cumulative difficulties ringbuffer
  92. pub difficulties: RingBuffer<BigUint, BUF_SIZE>,
  93. /// Total blocks cumulative difficulty
  94. /// Note: we keep this as a struct field for faster
  95. /// access(optimization), since its always same as
  96. /// difficulties buffer last.
  97. pub cumulative_difficulty: BigUint,
  98. /// Native PoW RandomX VMs current and next keys pair
  99. pub darkfi_rx_keys: (HeaderHash, Option<HeaderHash>),
  100. /// RandomXFactory for native PoW (Arc from parent)
  101. pub darkfi_rx_factory: RandomXFactory,
  102. /// RandomXFactory for Monero PoW (Arc from parent)
  103. pub monero_rx_factory: RandomXFactory,
  104. }
  105. impl PoWModule {
  106. // Initialize a new `PowModule` for provided target over provided `Blockchain`.
  107. // Optionally, a fixed difficulty can be set and/or initialize before some height.
  108. pub fn new(
  109. blockchain: Blockchain,
  110. target: u32,
  111. fixed_difficulty: Option<BigUint>,
  112. height: Option<u32>,
  113. ) -> Result<Self> {
  114. // Retrieve genesis block timestamp
  115. let genesis = blockchain.genesis_block()?.header.timestamp;
  116. // Retrieving last BUF_SIZE difficulties from blockchain to build the buffers
  117. let mut timestamps = RingBuffer::<Timestamp, BUF_SIZE>::new();
  118. let mut difficulties = RingBuffer::<BigUint, BUF_SIZE>::new();
  119. let mut cumulative_difficulty = BigUint::zero();
  120. let last_n = match height {
  121. Some(h) => blockchain.blocks.get_difficulties_before(h, BUF_SIZE)?,
  122. None => blockchain.blocks.get_last_n_difficulties(BUF_SIZE)?,
  123. };
  124. for difficulty in last_n {
  125. timestamps.push(difficulty.timestamp);
  126. difficulties.push(difficulty.cumulative_difficulty.clone());
  127. cumulative_difficulty = difficulty.cumulative_difficulty;
  128. }
  129. // If a fixed difficulty has been set, assert its greater than zero
  130. if let Some(diff) = &fixed_difficulty {
  131. assert!(diff > &BigUint::zero());
  132. }
  133. // Retrieve current and next native PoW RandomX VM keys pair,
  134. // and generate the RandomX factories.
  135. let darkfi_rx_keys = blockchain.get_randomx_vm_keys(
  136. &RANDOMX_KEY_CHANGING_HEIGHT,
  137. &RANDOMX_KEY_CHANGE_DELAY,
  138. height,
  139. )?;
  140. let darkfi_rx_factory = RandomXFactory::default();
  141. let monero_rx_factory = RandomXFactory::default();
  142. Ok(Self {
  143. genesis,
  144. target,
  145. fixed_difficulty,
  146. timestamps,
  147. difficulties,
  148. cumulative_difficulty,
  149. darkfi_rx_keys,
  150. darkfi_rx_factory,
  151. monero_rx_factory,
  152. })
  153. }
  154. /// Compute the next mining difficulty, based on current ring buffers.
  155. /// If ring buffers contain 2 or less items, difficulty 1 is returned.
  156. /// If a fixed difficulty has been set, this function will always
  157. /// return that after first 2 difficulties.
  158. pub fn next_difficulty(&self) -> Result<BigUint> {
  159. // Retrieve first DIFFICULTY_WINDOW timestamps from the ring buffer
  160. let mut timestamps: Vec<Timestamp> =
  161. self.timestamps.iter().take(DIFFICULTY_WINDOW).cloned().collect();
  162. // Check we have enough timestamps
  163. let length = timestamps.len();
  164. if length < 2 {
  165. return Ok(BigUint::one())
  166. }
  167. // If a fixed difficulty has been set, return that
  168. if let Some(diff) = &self.fixed_difficulty {
  169. return Ok(diff.clone())
  170. }
  171. // Sort the timestamps vector
  172. timestamps.sort_unstable();
  173. // Grab cutoff indexes
  174. let (cut_begin, cut_end) = self.cutoff(length)?;
  175. // Calculate total time span
  176. let cut_end = cut_end - 1;
  177. let mut time_span = timestamps[cut_end].checked_sub(timestamps[cut_begin])?;
  178. if time_span.inner() == 0 {
  179. time_span = 1.into();
  180. }
  181. // Calculate total work done during this time span
  182. let total_work = &self.difficulties[cut_end] - &self.difficulties[cut_begin];
  183. if total_work <= BigUint::zero() {
  184. return Err(Error::PoWTotalWorkIsZero)
  185. }
  186. // Compute next difficulty
  187. let next_difficulty =
  188. (total_work * self.target + time_span.inner() - BigUint::one()) / time_span.inner();
  189. Ok(next_difficulty)
  190. }
  191. /// Calculate cutoff indexes.
  192. /// If buffers have been filled, we return the
  193. /// already known indexes, for performance.
  194. fn cutoff(&self, length: usize) -> Result<(usize, usize)> {
  195. if length >= DIFFICULTY_WINDOW {
  196. return Ok((CUT_BEGIN, CUT_END))
  197. }
  198. let (cut_begin, cut_end) = if length <= RETAINED {
  199. (0, length)
  200. } else {
  201. let cut_begin = (length - RETAINED).div_ceil(2);
  202. (cut_begin, cut_begin + RETAINED)
  203. };
  204. // Sanity check
  205. if
  206. /* cut_begin < 0 || */
  207. cut_begin + 2 > cut_end || cut_end > length {
  208. return Err(Error::PoWCuttofCalculationError)
  209. }
  210. Ok((cut_begin, cut_end))
  211. }
  212. /// Compute the next mine target.
  213. pub fn next_mine_target(&self) -> Result<BigUint> {
  214. Ok(&*MAX_32_BYTES / &self.next_difficulty()?)
  215. }
  216. /// Compute the next mine target and difficulty.
  217. pub fn next_mine_target_and_difficulty(&self) -> Result<(BigUint, BigUint)> {
  218. let difficulty = self.next_difficulty()?;
  219. let mine_target = &*MAX_32_BYTES / &difficulty;
  220. Ok((mine_target, difficulty))
  221. }
  222. /// Verify provided difficulty corresponds to the next one.
  223. pub fn verify_difficulty(&self, difficulty: &BigUint) -> Result<bool> {
  224. Ok(difficulty == &self.next_difficulty()?)
  225. }
  226. /// Verify provided block timestamp is not far in the future and
  227. /// check its valid acorrding to current timestamps median.
  228. pub fn verify_current_timestamp(&self, timestamp: Timestamp) -> Result<bool> {
  229. if timestamp > Timestamp::current_time().checked_add(BLOCK_FUTURE_TIME_LIMIT)? {
  230. return Ok(false)
  231. }
  232. Ok(self.verify_timestamp_by_median(timestamp))
  233. }
  234. /// Verify provided block timestamp is valid and matches certain criteria.
  235. pub fn verify_timestamp_by_median(&self, timestamp: Timestamp) -> bool {
  236. // Check timestamp is after genesis one
  237. if timestamp <= self.genesis {
  238. return false
  239. }
  240. // If not enough blocks, no proper median yet, return true
  241. if self.timestamps.len() < BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW {
  242. return true
  243. }
  244. // Make sure the timestamp is higher or equal to the median
  245. let timestamps = self
  246. .timestamps
  247. .iter()
  248. .rev()
  249. .take(BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW)
  250. .map(|x| x.inner())
  251. .collect();
  252. timestamp >= median(timestamps).into()
  253. }
  254. /// Verify provided block timestamp and hash.
  255. pub fn verify_current_block(&mut self, header: &Header) -> Result<()> {
  256. // First we verify the block's timestamp
  257. if !self.verify_current_timestamp(header.timestamp)? {
  258. return Err(Error::PoWInvalidTimestamp)
  259. }
  260. // Then we verify the block's hash
  261. self.verify_block_hash(header)
  262. }
  263. /// Verify provided block hash is less than provided mine target.
  264. pub fn verify_block_target(&mut self, header: &Header, target: &BigUint) -> Result<BigUint> {
  265. let verifier_setup = Instant::now();
  266. // Grab verifier output hash based on block PoW data
  267. let (out_hash, verification_time) = match &header.pow_data {
  268. DarkFi => {
  269. // Check which VM key should be used.
  270. // We only use the next key when the next block is the
  271. // height changing one.
  272. let randomx_key = if header.height > RANDOMX_KEY_CHANGING_HEIGHT &&
  273. header.height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY
  274. {
  275. &self
  276. .darkfi_rx_keys
  277. .1
  278. .ok_or_else(|| Error::ParseFailed("darkfi_rx_keys.1 unwrap() error"))?
  279. } else {
  280. &self.darkfi_rx_keys.0
  281. };
  282. let vm = self.darkfi_rx_factory.create(&randomx_key.inner()[..])?;
  283. debug!(
  284. target: "validator::pow::verify_block_target",
  285. "[VERIFIER] DarkFi PoW setup time: {:?}",
  286. verifier_setup.elapsed(),
  287. );
  288. let verification_time = Instant::now();
  289. let out_hash = vm.calculate_hash(&header.to_block_hashing_blob())?;
  290. (BigUint::from_bytes_le(&out_hash), verification_time)
  291. }
  292. Monero(powdata) => {
  293. let vm = self.monero_rx_factory.create(powdata.randomx_key())?;
  294. debug!(
  295. target: "validator::pow::verify_block_target",
  296. "[VERIFIER] Monero PoW setup time: {:?}",
  297. verifier_setup.elapsed(),
  298. );
  299. let verification_time = Instant::now();
  300. let out_hash = vm.calculate_hash(&powdata.to_block_hashing_blob())?;
  301. (BigUint::from_bytes_le(&out_hash), verification_time)
  302. }
  303. };
  304. debug!(target: "validator::pow::verify_block_target", "[VERIFIER] Verification time: {:?}", verification_time.elapsed());
  305. // Verify hash is less than the provided mine target
  306. if out_hash > *target {
  307. return Err(Error::PoWInvalidOutHash)
  308. }
  309. Ok(out_hash)
  310. }
  311. /// Verify provided block corresponds to next mine target.
  312. pub fn verify_block_hash(&mut self, header: &Header) -> Result<()> {
  313. // Grab the next mine target
  314. let target = self.next_mine_target()?;
  315. // Verify hash is less than the expected mine target
  316. let _ = self.verify_block_target(header, &target)?;
  317. Ok(())
  318. }
  319. /// Append provided header timestamp and difficulty to the ring
  320. /// buffers, and check if we need to rotate and/or create the next
  321. /// key RandomX VM in the native PoW factory.
  322. pub fn append(&mut self, header: &Header, difficulty: &BigUint) -> Result<()> {
  323. self.timestamps.push(header.timestamp);
  324. self.cumulative_difficulty += difficulty;
  325. self.difficulties.push(self.cumulative_difficulty.clone());
  326. if header.height < RANDOMX_KEY_CHANGING_HEIGHT {
  327. return Ok(())
  328. }
  329. // Check if need to set the new key
  330. if header.height.is_multiple_of(RANDOMX_KEY_CHANGING_HEIGHT) {
  331. let next_key = header.hash();
  332. let _ = self.darkfi_rx_factory.create(&next_key.inner()[..])?;
  333. self.darkfi_rx_keys.1 = Some(next_key);
  334. return Ok(())
  335. }
  336. // Check if need to rotate keys
  337. if header.height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY {
  338. self.darkfi_rx_keys.0 = self
  339. .darkfi_rx_keys
  340. .1
  341. .ok_or_else(|| Error::ParseFailed("darkfi_rx_keys.1 unwrap() error"))?;
  342. self.darkfi_rx_keys.1 = None;
  343. }
  344. Ok(())
  345. }
  346. /// Append provided block difficulty to the ring buffers and insert
  347. /// it to provided overlay.
  348. pub fn append_difficulty(
  349. &mut self,
  350. overlay: &BlockchainOverlayPtr,
  351. header: &Header,
  352. difficulty: BlockDifficulty,
  353. ) -> Result<()> {
  354. self.append(header, &difficulty.difficulty)?;
  355. overlay.lock().unwrap().blocks.insert_difficulty(&[difficulty])
  356. }
  357. /// Mine provided block, based on next mine target.
  358. /// Note: this is used in tests not in actual mining.
  359. pub fn mine_block(
  360. &self,
  361. header: &mut Header,
  362. threads: usize,
  363. stop_signal: &Receiver<()>,
  364. ) -> Result<()> {
  365. // Grab the RandomX key to use.
  366. // We only use the next key when the next block is the
  367. // height changing one.
  368. let randomx_key = if header.height > RANDOMX_KEY_CHANGING_HEIGHT &&
  369. header.height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY
  370. {
  371. &self
  372. .darkfi_rx_keys
  373. .1
  374. .ok_or_else(|| Error::ParseFailed("darkfi_rx_keys.1 unwrap() error"))?
  375. } else {
  376. &self.darkfi_rx_keys.0
  377. };
  378. // Generate the RandomX VMs for the key
  379. let flags = get_mining_flags(false, false, false);
  380. let vms = generate_mining_vms(flags, randomx_key, threads, stop_signal)?;
  381. // Grab the next mine target
  382. let target = self.next_mine_target()?;
  383. // Mine the block
  384. mine_block(&vms, &target, header, stop_signal)
  385. }
  386. }
  387. impl std::fmt::Display for PoWModule {
  388. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  389. write!(f, "PoWModule:")?;
  390. write!(f, "\ttarget: {}", self.target)?;
  391. write!(f, "\ttimestamps: {:?}", self.timestamps)?;
  392. write!(f, "\tdifficulties: {:?}", self.difficulties)?;
  393. write!(f, "\tcumulative_difficulty: {}", self.cumulative_difficulty)
  394. }
  395. }
  396. /// Auxiliary function to define `RandomXFlags` used in mining.
  397. ///
  398. /// Note: RandomX recommended flags will include `SSSE3` and `AVX2`
  399. /// extensions if CPU supports them.
  400. pub fn get_mining_flags(fast_mode: bool, large_pages: bool, secure: bool) -> RandomXFlags {
  401. let mut flags = RandomXFlags::get_recommended_flags();
  402. if fast_mode {
  403. flags |= RandomXFlags::FULLMEM;
  404. }
  405. if large_pages {
  406. flags |= RandomXFlags::LARGEPAGES;
  407. }
  408. if secure && flags.contains(RandomXFlags::JIT) {
  409. flags |= RandomXFlags::SECURE;
  410. }
  411. flags
  412. }
  413. /// Auxiliary function to initialize a `RandomXDataset` using all
  414. /// available threads.
  415. fn init_dataset(
  416. flags: RandomXFlags,
  417. input: &HeaderHash,
  418. stop_signal: &Receiver<()>,
  419. ) -> Result<RandomXDataset> {
  420. // Allocate cache and dataset
  421. let cache = RandomXCache::new(flags, &input.inner()[..])?;
  422. let dataset_item_count = RandomXDataset::count()?;
  423. let dataset = RandomXDataset::new(flags, cache, dataset_item_count)?;
  424. // Multithreaded dataset init using all available threads
  425. let threads = thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
  426. debug!(target: "validator::pow::init_dataset", "[MINER] Initializing RandomX dataset using {threads} threads...");
  427. let mut handles = Vec::with_capacity(threads);
  428. let threads_u32 = threads as u32;
  429. let per_thread = dataset_item_count / threads_u32;
  430. let remainder = dataset_item_count % threads_u32;
  431. for t in 0..threads_u32 {
  432. // Check if stop signal is received
  433. if stop_signal.is_full() {
  434. debug!(target: "validator::pow::init_dataset", "[MINER] Stop signal received, threads creation loop exiting");
  435. break
  436. }
  437. let dataset = dataset.clone();
  438. let start_item = t * per_thread;
  439. let count = per_thread + if t == threads_u32 - 1 { remainder } else { 0 };
  440. handles.push(thread::spawn(move || {
  441. dataset.init(start_item, count);
  442. }));
  443. }
  444. // Wait for threads to finish setup
  445. for handle in handles {
  446. let _ = handle.join();
  447. }
  448. // Check if stop signal is received
  449. if stop_signal.is_full() {
  450. debug!(target: "validator::pow::init_dataset", "[MINER] Stop signal received, exiting");
  451. return Err(Error::MinerTaskStopped);
  452. }
  453. Ok(dataset)
  454. }
  455. /// Auxiliary function to generate mining VMs for provided RandomX key.
  456. pub fn generate_mining_vms(
  457. flags: RandomXFlags,
  458. input: &HeaderHash,
  459. threads: usize,
  460. stop_signal: &Receiver<()>,
  461. ) -> Result<Vec<Arc<RandomXVM>>> {
  462. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initializing RandomX cache and dataset...");
  463. debug!(target: "validator::pow::generate_mining_vms", "[MINER] PoW input: {input}");
  464. let setup_start = Instant::now();
  465. // Check if fast mode is enabled
  466. let (cache, dataset) = if flags.contains(RandomXFlags::FULLMEM) {
  467. // Initialize dataset
  468. let dataset = init_dataset(flags, input, stop_signal)?;
  469. (None, Some(dataset))
  470. } else {
  471. // Initialize cache for light mode
  472. let cache = RandomXCache::new(flags, &input.inner()[..])?;
  473. (Some(cache), None)
  474. };
  475. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initialized RandomX cache and dataset: {:?}", setup_start.elapsed());
  476. // Single thread mining VM
  477. if threads == 1 {
  478. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initializing RandomX VM...");
  479. let vm_start = Instant::now();
  480. let vm = Arc::new(RandomXVM::new(flags, cache, dataset)?);
  481. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initialized RandomX VM in {:?}", vm_start.elapsed());
  482. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Setup time: {:?}", setup_start.elapsed());
  483. return Ok(vec![vm])
  484. }
  485. // Multi thread mining VMs
  486. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initializing {threads} RandomX VMs...");
  487. let mut vms = Vec::with_capacity(threads);
  488. let threads_u32 = threads as u32;
  489. for t in 0..threads_u32 {
  490. // Check if stop signal is received
  491. if stop_signal.is_full() {
  492. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Stop signal received, exiting");
  493. return Err(Error::MinerTaskStopped);
  494. }
  495. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initializing RandomX VM #{t}...");
  496. let vm_start = Instant::now();
  497. vms.push(Arc::new(RandomXVM::new(flags, cache.clone(), dataset.clone())?));
  498. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Initialized RandomX VM #{t} in {:?}", vm_start.elapsed());
  499. }
  500. debug!(target: "validator::pow::generate_mining_vms", "[MINER] Setup time: {:?}", setup_start.elapsed());
  501. Ok(vms)
  502. }
  503. /// Mine provided header, based on provided PoW module next mine target,
  504. /// using provided RandomX VMs setup.
  505. pub fn mine_block(
  506. vms: &[Arc<RandomXVM>],
  507. target: &BigUint,
  508. header: &mut Header,
  509. stop_signal: &Receiver<()>,
  510. ) -> Result<()> {
  511. debug!(target: "validator::pow::mine_block", "[MINER] Mine target: 0x{target:064x}");
  512. // Check VMs were provided
  513. if vms.is_empty() {
  514. error!(target: "validator::pow::mine_block", "[MINER] No VMs were provided!");
  515. return Err(Error::MinerTaskStopped)
  516. }
  517. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Initializing mining threads...");
  518. let mut handles = Vec::with_capacity(vms.len());
  519. let atomic_nonce = Arc::new(AtomicU32::new(0));
  520. let found_header = Arc::new(AtomicBool::new(false));
  521. let found_nonce = Arc::new(AtomicU32::new(0));
  522. let threads = vms.len() as u32;
  523. let mining_start = Instant::now();
  524. for t in 0..threads {
  525. // Check if stop signal is received
  526. if stop_signal.is_full() {
  527. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Stop signal received, threads creation loop exiting");
  528. break
  529. }
  530. if found_header.load(Ordering::SeqCst) {
  531. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Block header found, threads creation loop exiting");
  532. break
  533. }
  534. let vm = vms[t as usize].clone();
  535. let target = target.clone();
  536. let mut thread_header = header.clone();
  537. let atomic_nonce = atomic_nonce.clone();
  538. let found_header = found_header.clone();
  539. let found_nonce = found_nonce.clone();
  540. let stop_signal = stop_signal.clone();
  541. handles.push(thread::spawn(move || {
  542. let mut last_nonce = atomic_nonce.fetch_add(1, Ordering::SeqCst);
  543. thread_header.nonce = last_nonce;
  544. if let Err(e) = vm.calculate_hash_first(thread_header.hash().inner()) {
  545. error!(target: "validator::pow::randomx_vms_mine", "[MINER] Calculating hash in thread #{t} failed: {e}");
  546. return
  547. };
  548. loop {
  549. // Check if stop signal was received
  550. if stop_signal.is_full() {
  551. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Stop signal received, thread #{t} exiting");
  552. break
  553. }
  554. if found_header.load(Ordering::SeqCst) {
  555. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Block header found, thread #{t} exiting");
  556. break;
  557. }
  558. thread_header.nonce = atomic_nonce.fetch_add(1, Ordering::SeqCst);
  559. let out_hash = match vm.calculate_hash_next(thread_header.hash().inner()) {
  560. Ok(hash) => hash,
  561. Err(e) => {
  562. error!(target: "validator::pow::randomx_vms_mine", "[MINER] Calculating hash in thread #{t} failed: {e}");
  563. break
  564. }
  565. };
  566. let out_hash = BigUint::from_bytes_le(&out_hash);
  567. if out_hash <= target {
  568. found_header.store(true, Ordering::SeqCst);
  569. thread_header.nonce = last_nonce; // Since out hash refers to previous run nonce
  570. found_nonce.store(thread_header.nonce, Ordering::SeqCst);
  571. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Thread #{t} found block header using nonce {}",
  572. thread_header.nonce
  573. );
  574. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Block header hash {}", thread_header.hash());
  575. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] RandomX output: 0x{out_hash:064x}");
  576. break;
  577. }
  578. last_nonce = thread_header.nonce;
  579. }
  580. }));
  581. }
  582. // Wait for threads to finish mining
  583. for handle in handles {
  584. let _ = handle.join();
  585. }
  586. // Check if stop signal is received
  587. if stop_signal.is_full() {
  588. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Stop signal received, exiting");
  589. return Err(Error::MinerTaskStopped);
  590. }
  591. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Completed mining in {:?}", mining_start.elapsed());
  592. header.nonce = found_nonce.load(Ordering::SeqCst);
  593. debug!(target: "validator::pow::randomx_vms_mine", "[MINER] Mined header: {header:?}");
  594. Ok(())
  595. }
  596. #[cfg(test)]
  597. mod tests {
  598. use std::{
  599. io::{BufRead, Cursor},
  600. process::Command,
  601. };
  602. use darkfi_sdk::num_traits::Num;
  603. use num_bigint::BigUint;
  604. use sled_overlay::sled;
  605. use crate::{
  606. blockchain::{header_store::Header, BlockInfo, Blockchain},
  607. Result,
  608. };
  609. use super::PoWModule;
  610. const DEFAULT_TEST_THREADS: usize = 2;
  611. const DEFAULT_TEST_DIFFICULTY_TARGET: u32 = 120;
  612. #[test]
  613. fn test_wide_difficulty() -> Result<()> {
  614. let sled_db = sled::Config::new().temporary(true).open()?;
  615. let blockchain = Blockchain::new(&sled_db)?;
  616. let genesis_block = BlockInfo::default();
  617. blockchain.add_block(&genesis_block)?;
  618. let mut module = PoWModule::new(blockchain, DEFAULT_TEST_DIFFICULTY_TARGET, None, None)?;
  619. let output = Command::new("./script/monero_gen_wide_data.py").output().unwrap();
  620. let reader = Cursor::new(output.stdout);
  621. let mut previous = genesis_block.header;
  622. for (n, line) in reader.lines().enumerate() {
  623. let line = line.unwrap();
  624. let parts: Vec<String> = line.split(' ').map(|x| x.to_string()).collect();
  625. assert!(parts.len() == 2);
  626. let header = Header::new(
  627. previous.hash(),
  628. previous.height + 1,
  629. 0,
  630. parts[0].parse::<u64>().unwrap().into(),
  631. );
  632. let difficulty = BigUint::from_str_radix(&parts[1], 10).unwrap();
  633. let res = module.next_difficulty()?;
  634. if res != difficulty {
  635. eprintln!("Wrong wide difficulty for block {n}");
  636. eprintln!("Expected: {difficulty}");
  637. eprintln!("Found: {res}");
  638. assert!(res == difficulty);
  639. }
  640. module.append(&header, &difficulty)?;
  641. previous = header;
  642. }
  643. Ok(())
  644. }
  645. #[test]
  646. fn test_miner_correctness() -> Result<()> {
  647. // Default setup
  648. let sled_db = sled::Config::new().temporary(true).open()?;
  649. let blockchain = Blockchain::new(&sled_db)?;
  650. let mut genesis_block = BlockInfo::default();
  651. genesis_block.header.timestamp = 0.into();
  652. blockchain.add_block(&genesis_block)?;
  653. let mut module = PoWModule::new(blockchain, DEFAULT_TEST_DIFFICULTY_TARGET, None, None)?;
  654. let (_, recvr) = smol::channel::bounded(1);
  655. // Mine next block
  656. let mut next_block = BlockInfo::default();
  657. next_block.header.previous = genesis_block.hash();
  658. module.mine_block(&mut next_block.header, DEFAULT_TEST_THREADS, &recvr)?;
  659. // Verify it
  660. module.verify_current_block(&next_block.header)?;
  661. Ok(())
  662. }
  663. }