consensus.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::collections::{HashMap, HashSet};
  19. use darkfi_sdk::{crypto::MerkleTree, tx::TransactionHash};
  20. use darkfi_serial::{async_trait, SerialDecodable, SerialEncodable};
  21. use log::{debug, info, warn};
  22. use num_bigint::BigUint;
  23. use sled_overlay::database::SledDbOverlayState;
  24. use smol::lock::RwLock;
  25. use crate::{
  26. blockchain::{
  27. block_store::{BlockDifficulty, BlockRanks},
  28. BlockInfo, Blockchain, BlockchainOverlay, BlockchainOverlayPtr, HeaderHash,
  29. },
  30. tx::Transaction,
  31. validator::{
  32. pow::PoWModule,
  33. utils::{best_fork_index, block_rank, find_extended_fork_index},
  34. verification::{verify_proposal, verify_transaction},
  35. },
  36. zk::VerifyingKey,
  37. Error, Result,
  38. };
  39. // Consensus configuration
  40. /// Average amount of gas consumed during transaction execution, derived by the Gas Analyzer
  41. const GAS_TX_AVG: u64 = 23_822_290;
  42. /// Multiplier used to calculate the gas limit for unproposed transactions
  43. const GAS_LIMIT_MULTIPLIER_UNPROPOSED_TXS: u64 = 50;
  44. /// Gas limit for unproposed transactions
  45. pub const GAS_LIMIT_UNPROPOSED_TXS: u64 = GAS_TX_AVG * GAS_LIMIT_MULTIPLIER_UNPROPOSED_TXS;
  46. /// This struct represents the information required by the consensus algorithm
  47. pub struct Consensus {
  48. /// Canonical (finalized) blockchain
  49. pub blockchain: Blockchain,
  50. /// Fork size(length) after which it can be finalized
  51. pub finalization_threshold: usize,
  52. /// Fork chains containing block proposals
  53. pub forks: RwLock<Vec<Fork>>,
  54. /// Canonical blockchain PoW module state
  55. pub module: RwLock<PoWModule>,
  56. /// Lock to restrict when proposals appends can happen
  57. pub append_lock: RwLock<()>,
  58. }
  59. impl Consensus {
  60. /// Generate a new Consensus state.
  61. pub fn new(
  62. blockchain: Blockchain,
  63. finalization_threshold: usize,
  64. pow_target: u32,
  65. pow_fixed_difficulty: Option<BigUint>,
  66. ) -> Result<Self> {
  67. let forks = RwLock::new(vec![]);
  68. let module =
  69. RwLock::new(PoWModule::new(blockchain.clone(), pow_target, pow_fixed_difficulty)?);
  70. let append_lock = RwLock::new(());
  71. Ok(Self { blockchain, finalization_threshold, forks, module, append_lock })
  72. }
  73. /// Generate a new empty fork.
  74. pub async fn generate_empty_fork(&self) -> Result<()> {
  75. debug!(target: "validator::consensus::generate_empty_fork", "Generating new empty fork...");
  76. let mut forks = self.forks.write().await;
  77. // Check if we already have an empty fork
  78. for fork in forks.iter() {
  79. if fork.proposals.is_empty() {
  80. debug!(target: "validator::consensus::generate_empty_fork", "An empty fork already exists.");
  81. drop(forks);
  82. return Ok(())
  83. }
  84. }
  85. let fork = Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?;
  86. forks.push(fork);
  87. drop(forks);
  88. debug!(target: "validator::consensus::generate_empty_fork", "Fork generated!");
  89. Ok(())
  90. }
  91. /// Given a proposal, the node verifys it and finds which fork it extends.
  92. /// If the proposal extends the canonical blockchain, a new fork chain is created.
  93. pub async fn append_proposal(&self, proposal: &Proposal, verify_fees: bool) -> Result<()> {
  94. debug!(target: "validator::consensus::append_proposal", "Appending proposal {}", proposal.hash);
  95. // Check if proposal already exists
  96. let lock = self.forks.read().await;
  97. for fork in lock.iter() {
  98. for p in fork.proposals.iter().rev() {
  99. if p == &proposal.hash {
  100. drop(lock);
  101. debug!(target: "validator::consensus::append_proposal", "Proposal {} already exists", proposal.hash);
  102. return Err(Error::ProposalAlreadyExists)
  103. }
  104. }
  105. }
  106. drop(lock);
  107. // Verify proposal and grab corresponding fork
  108. let (mut fork, index) = verify_proposal(self, proposal, verify_fees).await?;
  109. // Append proposal to the fork
  110. fork.append_proposal(proposal).await?;
  111. // TODO: to keep memory usage low, we should only append forks that
  112. // are higher ranking than our current best one
  113. // If a fork index was found, replace forks with the mutated one,
  114. // otherwise push the new fork.
  115. let mut lock = self.forks.write().await;
  116. match index {
  117. Some(i) => {
  118. if i < lock.len() && lock[i].proposals == fork.proposals[..fork.proposals.len() - 1]
  119. {
  120. lock[i] = fork;
  121. } else {
  122. lock.push(fork);
  123. }
  124. }
  125. None => {
  126. lock.push(fork);
  127. }
  128. }
  129. drop(lock);
  130. info!(target: "validator::consensus::append_proposal", "Appended proposal {}", proposal.hash);
  131. Ok(())
  132. }
  133. /// Given a proposal, find the fork chain it extends, and return its full clone.
  134. /// If the proposal extends the fork not on its tail, a new fork is created and
  135. /// we re-apply the proposals up to the extending one. If proposal extends canonical,
  136. /// a new fork is created. Additionally, we return the fork index if a new fork
  137. /// was not created, so caller can replace the fork.
  138. pub async fn find_extended_fork(&self, proposal: &Proposal) -> Result<(Fork, Option<usize>)> {
  139. // Grab a lock over current forks
  140. let forks = self.forks.read().await;
  141. // Check if proposal extends any fork
  142. let found = find_extended_fork_index(&forks, proposal);
  143. if found.is_err() {
  144. if let Err(Error::ProposalAlreadyExists) = found {
  145. return Err(Error::ProposalAlreadyExists)
  146. }
  147. // Check if proposal extends canonical
  148. let (last_height, last_block) = self.blockchain.last()?;
  149. if proposal.block.header.previous != last_block ||
  150. proposal.block.header.height <= last_height
  151. {
  152. return Err(Error::ExtendedChainIndexNotFound)
  153. }
  154. // Check if we have an empty fork to use
  155. for (f_index, fork) in forks.iter().enumerate() {
  156. if fork.proposals.is_empty() {
  157. return Ok((forks[f_index].full_clone()?, Some(f_index)))
  158. }
  159. }
  160. // Generate a new fork extending canonical
  161. let fork = Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?;
  162. return Ok((fork, None))
  163. }
  164. let (f_index, p_index) = found.unwrap();
  165. let original_fork = &forks[f_index];
  166. // Check if proposal extends fork at last proposal
  167. if p_index == (original_fork.proposals.len() - 1) {
  168. return Ok((original_fork.full_clone()?, Some(f_index)))
  169. }
  170. // Rebuild fork
  171. let mut fork = Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?;
  172. fork.proposals = original_fork.proposals[..p_index + 1].to_vec();
  173. fork.diffs = original_fork.diffs[..p_index + 1].to_vec();
  174. // Retrieve proposals blocks from original fork
  175. let blocks = &original_fork.overlay.lock().unwrap().get_blocks_by_hash(&fork.proposals)?;
  176. for (index, block) in blocks.iter().enumerate() {
  177. // Apply block diffs
  178. fork.overlay.lock().unwrap().overlay.lock().unwrap().add_diff(&fork.diffs[index]);
  179. // Grab next mine target and difficulty
  180. let (next_target, next_difficulty) = fork.module.next_mine_target_and_difficulty()?;
  181. // Calculate block rank
  182. let (target_distance_sq, hash_distance_sq) = block_rank(block, &next_target);
  183. // Update PoW module
  184. fork.module.append(block.header.timestamp, &next_difficulty);
  185. // Update fork ranks
  186. fork.targets_rank += target_distance_sq;
  187. fork.hashes_rank += hash_distance_sq;
  188. }
  189. // Drop forks lock
  190. drop(forks);
  191. Ok((fork, None))
  192. }
  193. /// Check if best fork proposals can be finalized.
  194. /// Consensus finalization logic:
  195. /// - If the current best fork has reached greater length than the security threshold,
  196. /// and no other fork exist with same rank, first proposal(s) in that fork can be
  197. /// appended to canonical blockchain (finalize).
  198. ///
  199. /// When best fork can be finalized, first block(s) should be appended to canonical,
  200. /// and forks should be rebuilt.
  201. pub async fn finalization(&self) -> Result<Option<usize>> {
  202. debug!(target: "validator::consensus::finalization", "Started finalization check");
  203. // Grab best fork
  204. let forks = self.forks.read().await;
  205. let index = best_fork_index(&forks)?;
  206. let fork = &forks[index];
  207. // Check its length
  208. let length = fork.proposals.len();
  209. if length < self.finalization_threshold {
  210. debug!(target: "validator::consensus::finalization", "Nothing to finalize yet, best fork size: {}", length);
  211. drop(forks);
  212. return Ok(None)
  213. }
  214. // Drop forks lock
  215. drop(forks);
  216. Ok(Some(index))
  217. }
  218. /// Auxiliary function to retrieve a fork proposals.
  219. /// If provided tip is not the canonical(finalized), or fork doesn't exists,
  220. /// an empty vector is returned.
  221. pub async fn get_fork_proposals(
  222. &self,
  223. tip: HeaderHash,
  224. fork_tip: HeaderHash,
  225. ) -> Result<Vec<Proposal>> {
  226. // Tip must be canonical(finalized) blockchain last
  227. if self.blockchain.last()?.1 != tip {
  228. return Ok(vec![])
  229. }
  230. // Grab a lock over current forks
  231. let forks = self.forks.read().await;
  232. // Check if node has any forks
  233. if forks.is_empty() {
  234. drop(forks);
  235. return Ok(vec![])
  236. }
  237. // Find fork by its tip
  238. for fork in forks.iter() {
  239. if fork.proposals.last() == Some(&fork_tip) {
  240. // Grab its proposals
  241. let blocks = fork.overlay.lock().unwrap().get_blocks_by_hash(&fork.proposals)?;
  242. let mut ret = Vec::with_capacity(blocks.len());
  243. for block in blocks {
  244. ret.push(Proposal::new(block));
  245. }
  246. drop(forks);
  247. return Ok(ret)
  248. }
  249. }
  250. // Fork was not found
  251. Ok(vec![])
  252. }
  253. /// Auxiliary function to retrieve current best fork last header.
  254. /// If no forks exist, grab the last header from canonical.
  255. pub async fn best_fork_last_header(&self) -> Result<(u32, HeaderHash)> {
  256. // Grab a lock over current forks
  257. let forks = self.forks.read().await;
  258. // Check if node has any forks
  259. if forks.is_empty() {
  260. drop(forks);
  261. return self.blockchain.last()
  262. }
  263. // Grab best fork
  264. let fork = &forks[best_fork_index(&forks)?];
  265. // Grab its last header
  266. let last = fork.last_proposal()?;
  267. drop(forks);
  268. Ok((last.block.header.height, last.hash))
  269. }
  270. /// Auxiliary function to retrieve current best fork proposals.
  271. /// If provided tip is not the canonical(finalized), or no forks exist,
  272. /// an empty vector is returned.
  273. pub async fn get_best_fork_proposals(&self, tip: HeaderHash) -> Result<Vec<Proposal>> {
  274. // Tip must be canonical(finalized) blockchain last
  275. if self.blockchain.last()?.1 != tip {
  276. return Ok(vec![])
  277. }
  278. // Grab a lock over current forks
  279. let forks = self.forks.read().await;
  280. // Check if node has any forks
  281. if forks.is_empty() {
  282. drop(forks);
  283. return Ok(vec![])
  284. }
  285. // Grab best fork
  286. let fork = &forks[best_fork_index(&forks)?];
  287. // Grab its proposals
  288. let blocks = fork.overlay.lock().unwrap().get_blocks_by_hash(&fork.proposals)?;
  289. let mut ret = Vec::with_capacity(blocks.len());
  290. for block in blocks {
  291. ret.push(Proposal::new(block));
  292. }
  293. Ok(ret)
  294. }
  295. /// Auxiliary function to purge current forks and reset the ones starting
  296. /// with the provided prefix, excluding provided finalized fork.
  297. /// Additionally, remove finalized transactions from the forks mempools,
  298. /// along with the unporposed transactions sled trees.
  299. /// This function assumes that the prefix blocks have already been appended
  300. /// to canonical chain from the finalized fork.
  301. pub async fn reset_forks(
  302. &self,
  303. prefix: &[HeaderHash],
  304. finalized_fork_index: &usize,
  305. finalized_txs: &[Transaction],
  306. ) -> Result<()> {
  307. // Grab a lock over current forks
  308. let mut forks = self.forks.write().await;
  309. // Find all the forks that start with the provided prefix,
  310. // excluding finalized fork index, and remove their prefixed
  311. // proposals, and their corresponding diffs.
  312. // If the fork is not starting with the provided prefix,
  313. // drop it. Additionally, keep track of all the referenced
  314. // trees in overlays that are valid.
  315. let excess = prefix.len();
  316. let prefix_last_index = excess - 1;
  317. let prefix_last = prefix.last().unwrap();
  318. let mut keep = vec![true; forks.len()];
  319. let mut referenced_trees = HashSet::new();
  320. let mut referenced_txs = HashSet::new();
  321. let finalized_txs_hashes: Vec<TransactionHash> =
  322. finalized_txs.iter().map(|tx| tx.hash()).collect();
  323. for (index, fork) in forks.iter_mut().enumerate() {
  324. if &index == finalized_fork_index {
  325. // Store its tree references
  326. let fork_overlay = fork.overlay.lock().unwrap();
  327. let overlay = fork_overlay.overlay.lock().unwrap();
  328. for tree in &overlay.state.initial_tree_names {
  329. referenced_trees.insert(tree.clone());
  330. }
  331. for tree in &overlay.state.new_tree_names {
  332. referenced_trees.insert(tree.clone());
  333. }
  334. for tree in &overlay.state.dropped_tree_names {
  335. referenced_trees.insert(tree.clone());
  336. }
  337. // Remove finalized proposals txs from fork's mempool
  338. fork.mempool.retain(|tx| !finalized_txs_hashes.contains(tx));
  339. // Store its txs references
  340. for tx in &fork.mempool {
  341. referenced_txs.insert(*tx);
  342. }
  343. drop(overlay);
  344. drop(fork_overlay);
  345. continue
  346. }
  347. if fork.proposals.is_empty() ||
  348. prefix_last_index >= fork.proposals.len() ||
  349. &fork.proposals[prefix_last_index] != prefix_last
  350. {
  351. keep[index] = false;
  352. continue
  353. }
  354. // Remove finalized proposals txs from fork's mempool
  355. fork.mempool.retain(|tx| !finalized_txs_hashes.contains(tx));
  356. // Store its txs references
  357. for tx in &fork.mempool {
  358. referenced_txs.insert(*tx);
  359. }
  360. // Remove the commited differences
  361. let rest_proposals = fork.proposals.split_off(excess);
  362. let rest_diffs = fork.diffs.split_off(excess);
  363. let mut diffs = fork.diffs.clone();
  364. fork.proposals = rest_proposals;
  365. fork.diffs = rest_diffs;
  366. for diff in diffs.iter_mut() {
  367. fork.overlay.lock().unwrap().overlay.lock().unwrap().remove_diff(diff);
  368. }
  369. // Store its tree references
  370. let fork_overlay = fork.overlay.lock().unwrap();
  371. let overlay = fork_overlay.overlay.lock().unwrap();
  372. for tree in &overlay.state.initial_tree_names {
  373. referenced_trees.insert(tree.clone());
  374. }
  375. for tree in &overlay.state.new_tree_names {
  376. referenced_trees.insert(tree.clone());
  377. }
  378. for tree in &overlay.state.dropped_tree_names {
  379. referenced_trees.insert(tree.clone());
  380. }
  381. drop(overlay);
  382. drop(fork_overlay);
  383. }
  384. // Find the trees and pending txs that are no longer referenced by valid forks
  385. let mut dropped_trees = HashSet::new();
  386. let mut dropped_txs = HashSet::new();
  387. for (index, fork) in forks.iter_mut().enumerate() {
  388. if keep[index] {
  389. continue
  390. }
  391. for tx in &fork.mempool {
  392. if !referenced_txs.contains(tx) {
  393. dropped_txs.insert(*tx);
  394. }
  395. }
  396. let fork_overlay = fork.overlay.lock().unwrap();
  397. let overlay = fork_overlay.overlay.lock().unwrap();
  398. for tree in &overlay.state.initial_tree_names {
  399. if !referenced_trees.contains(tree) {
  400. dropped_trees.insert(tree.clone());
  401. }
  402. }
  403. for tree in &overlay.state.new_tree_names {
  404. if !referenced_trees.contains(tree) {
  405. dropped_trees.insert(tree.clone());
  406. }
  407. }
  408. for tree in &overlay.state.dropped_tree_names {
  409. if !referenced_trees.contains(tree) {
  410. dropped_trees.insert(tree.clone());
  411. }
  412. }
  413. drop(overlay);
  414. drop(fork_overlay);
  415. }
  416. // Drop unreferenced trees from the database
  417. for tree in dropped_trees {
  418. self.blockchain.sled_db.drop_tree(tree)?;
  419. }
  420. // Drop invalid forks
  421. let mut iter = keep.iter();
  422. forks.retain(|_| *iter.next().unwrap());
  423. // Remove finalized proposals txs from the unporposed txs sled tree
  424. self.blockchain.remove_pending_txs_hashes(&finalized_txs_hashes)?;
  425. // Remove unreferenced txs from the unporposed txs sled tree
  426. self.blockchain.remove_pending_txs_hashes(&Vec::from_iter(dropped_txs))?;
  427. // Drop forks lock
  428. drop(forks);
  429. Ok(())
  430. }
  431. /// Auxiliary function to fully purge current forks and leave only a new empty fork.
  432. pub async fn purge_forks(&self) -> Result<()> {
  433. debug!(target: "validator::consensus::purge_forks", "Purging current forks...");
  434. let mut forks = self.forks.write().await;
  435. *forks = vec![Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?];
  436. drop(forks);
  437. debug!(target: "validator::consensus::purge_forks", "Forks purged!");
  438. Ok(())
  439. }
  440. }
  441. /// This struct represents a block proposal, used for consensus.
  442. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  443. pub struct Proposal {
  444. /// Block hash
  445. pub hash: HeaderHash,
  446. /// Block data
  447. pub block: BlockInfo,
  448. }
  449. impl Proposal {
  450. pub fn new(block: BlockInfo) -> Self {
  451. let hash = block.hash();
  452. Self { hash, block }
  453. }
  454. }
  455. impl From<Proposal> for BlockInfo {
  456. fn from(proposal: Proposal) -> BlockInfo {
  457. proposal.block
  458. }
  459. }
  460. /// This struct represents a forked blockchain state, using an overlay over original
  461. /// blockchain, containing all pending to-write records. Additionally, each fork
  462. /// keeps a vector of valid pending transactions hashes, in order of receival, and
  463. /// the proposals hashes sequence, for validations.
  464. #[derive(Clone)]
  465. pub struct Fork {
  466. /// Canonical (finalized) blockchain
  467. pub blockchain: Blockchain,
  468. /// Overlay cache over canonical Blockchain
  469. pub overlay: BlockchainOverlayPtr,
  470. /// Current PoW module state,
  471. pub module: PoWModule,
  472. /// Fork proposal hashes sequence
  473. pub proposals: Vec<HeaderHash>,
  474. /// Fork proposal overlay diffs sequence
  475. pub diffs: Vec<SledDbOverlayState>,
  476. /// Valid pending transaction hashes
  477. pub mempool: Vec<TransactionHash>,
  478. /// Current fork mining targets rank, cached for better performance
  479. pub targets_rank: BigUint,
  480. /// Current fork hashes rank, cached for better performance
  481. pub hashes_rank: BigUint,
  482. }
  483. impl Fork {
  484. pub async fn new(blockchain: Blockchain, module: PoWModule) -> Result<Self> {
  485. let mempool = blockchain.get_pending_txs()?.iter().map(|tx| tx.hash()).collect();
  486. let overlay = BlockchainOverlay::new(&blockchain)?;
  487. // Retrieve last block difficulty to access current ranks
  488. let last_difficulty = blockchain.last_block_difficulty()?;
  489. let targets_rank = last_difficulty.ranks.targets_rank;
  490. let hashes_rank = last_difficulty.ranks.hashes_rank;
  491. Ok(Self {
  492. blockchain,
  493. overlay,
  494. module,
  495. proposals: vec![],
  496. diffs: vec![],
  497. mempool,
  498. targets_rank,
  499. hashes_rank,
  500. })
  501. }
  502. /// Auxiliary function to append a proposal and update current fork rank.
  503. pub async fn append_proposal(&mut self, proposal: &Proposal) -> Result<()> {
  504. // Grab next mine target and difficulty
  505. let (next_target, next_difficulty) = self.module.next_mine_target_and_difficulty()?;
  506. // Calculate block rank
  507. let (target_distance_sq, hash_distance_sq) = block_rank(&proposal.block, &next_target);
  508. // Update fork ranks
  509. self.targets_rank += target_distance_sq.clone();
  510. self.hashes_rank += hash_distance_sq.clone();
  511. // Generate block difficulty and update PoW module
  512. let cummulative_difficulty =
  513. self.module.cummulative_difficulty.clone() + next_difficulty.clone();
  514. let ranks = BlockRanks::new(
  515. target_distance_sq,
  516. self.targets_rank.clone(),
  517. hash_distance_sq,
  518. self.hashes_rank.clone(),
  519. );
  520. let block_difficulty = BlockDifficulty::new(
  521. proposal.block.header.height,
  522. proposal.block.header.timestamp,
  523. next_difficulty,
  524. cummulative_difficulty,
  525. ranks,
  526. );
  527. self.module.append_difficulty(&self.overlay, block_difficulty)?;
  528. // Push proposal's hash
  529. self.proposals.push(proposal.hash);
  530. // Push proposal overlay diff
  531. self.diffs.push(self.overlay.lock().unwrap().overlay.lock().unwrap().diff(&self.diffs));
  532. Ok(())
  533. }
  534. /// Auxiliary function to retrieve last proposal.
  535. pub fn last_proposal(&self) -> Result<Proposal> {
  536. let block = if let Some(last) = self.proposals.last() {
  537. self.overlay.lock().unwrap().get_blocks_by_hash(&[*last])?[0].clone()
  538. } else {
  539. self.overlay.lock().unwrap().last_block()?
  540. };
  541. Ok(Proposal::new(block))
  542. }
  543. /// Auxiliary function to compute forks' next block height.
  544. pub fn get_next_block_height(&self) -> Result<u32> {
  545. let proposal = self.last_proposal()?;
  546. Ok(proposal.block.header.height + 1)
  547. }
  548. /// Auxiliary function to retrieve unproposed valid transactions,
  549. /// along with their total gas used and total paid fees.
  550. pub async fn unproposed_txs(
  551. &self,
  552. blockchain: &Blockchain,
  553. verifying_block_height: u32,
  554. block_target: u32,
  555. verify_fees: bool,
  556. ) -> Result<(Vec<Transaction>, u64, u64)> {
  557. // Check if our mempool is not empty
  558. if self.mempool.is_empty() {
  559. return Ok((vec![], 0, 0))
  560. }
  561. // Transactions Merkle tree
  562. let mut tree = MerkleTree::new(1);
  563. // Total gas accumulators
  564. let mut total_gas_used = 0;
  565. let mut total_gas_paid = 0;
  566. // Map of ZK proof verifying keys for the current transaction batch
  567. let mut vks: HashMap<[u8; 32], HashMap<String, VerifyingKey>> = HashMap::new();
  568. // Clone forks' overlay
  569. let overlay = self.overlay.lock().unwrap().full_clone()?;
  570. // Grab all current proposals transactions hashes
  571. let proposals_txs = overlay.lock().unwrap().get_blocks_txs_hashes(&self.proposals)?;
  572. // Iterate through all pending transactions in the forks' mempool
  573. let mut unproposed_txs = vec![];
  574. for tx in &self.mempool {
  575. // If the hash is contained in the proposals transactions vec, skip it
  576. if proposals_txs.contains(tx) {
  577. continue
  578. }
  579. // Retrieve the actual unproposed transaction
  580. let unproposed_tx =
  581. blockchain.transactions.get_pending(&[*tx], true)?[0].clone().unwrap();
  582. // Update the verifying keys map
  583. for call in &unproposed_tx.calls {
  584. vks.entry(call.data.contract_id.to_bytes()).or_default();
  585. }
  586. // Verify the transaction against current state
  587. overlay.lock().unwrap().checkpoint();
  588. let (tx_gas_used, tx_gas_paid) = match verify_transaction(
  589. &overlay,
  590. verifying_block_height,
  591. block_target,
  592. &unproposed_tx,
  593. &mut tree,
  594. &mut vks,
  595. verify_fees,
  596. )
  597. .await
  598. {
  599. Ok(gas_values) => gas_values,
  600. Err(e) => {
  601. debug!(target: "validator::consensus::unproposed_txs", "Transaction verification failed: {}", e);
  602. overlay.lock().unwrap().revert_to_checkpoint()?;
  603. continue
  604. }
  605. };
  606. // Calculate current accumulated gas usage
  607. let accumulated_gas_usage = total_gas_used + tx_gas_used;
  608. // Check gas limit - if accumulated gas used exceeds it, break out of loop
  609. if accumulated_gas_usage > GAS_LIMIT_UNPROPOSED_TXS {
  610. warn!(target: "validator::consensus::unproposed_txs", "Retrieving transaction {} would exceed configured unproposed transaction gas limit: {} - {}", tx, accumulated_gas_usage, GAS_LIMIT_UNPROPOSED_TXS);
  611. break
  612. }
  613. // Update accumulated total gas
  614. total_gas_used += tx_gas_used;
  615. total_gas_paid += tx_gas_paid;
  616. // Push the tx hash into the unproposed transactions vector
  617. unproposed_txs.push(unproposed_tx);
  618. }
  619. Ok((unproposed_txs, total_gas_used, total_gas_paid))
  620. }
  621. /// Auxiliary function to create a full clone using BlockchainOverlay::full_clone.
  622. /// Changes to this copy don't affect original fork overlay records, since underlying
  623. /// overlay pointer have been updated to the cloned one.
  624. pub fn full_clone(&self) -> Result<Self> {
  625. let blockchain = self.blockchain.clone();
  626. let overlay = self.overlay.lock().unwrap().full_clone()?;
  627. let module = self.module.clone();
  628. let proposals = self.proposals.clone();
  629. let diffs = self.diffs.clone();
  630. let mempool = self.mempool.clone();
  631. let targets_rank = self.targets_rank.clone();
  632. let hashes_rank = self.hashes_rank.clone();
  633. Ok(Self {
  634. blockchain,
  635. overlay,
  636. module,
  637. proposals,
  638. diffs,
  639. mempool,
  640. targets_rank,
  641. hashes_rank,
  642. })
  643. }
  644. }