darkfi/validator/
consensus.rs

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
19use std::collections::{BTreeSet, HashMap, HashSet};
20
21use darkfi_sdk::{crypto::MerkleTree, tx::TransactionHash};
22use darkfi_serial::{async_trait, deserialize, SerialDecodable, SerialEncodable};
23use num_bigint::BigUint;
24use sled_overlay::{database::SledDbOverlayStateDiff, sled::IVec};
25use tracing::{debug, info, warn};
26
27use crate::{
28    blockchain::{
29        block_store::{BlockDifficulty, BlockRanks},
30        BlockInfo, Blockchain, BlockchainOverlay, BlockchainOverlayPtr, Header, HeaderHash,
31    },
32    runtime::vm_runtime::GAS_LIMIT,
33    tx::{Transaction, MAX_TX_CALLS},
34    validator::{
35        pow::{PoWModule, RANDOMX_KEY_CHANGE_DELAY, RANDOMX_KEY_CHANGING_HEIGHT},
36        utils::{best_fork_index, block_rank, find_extended_fork_index, worst_fork_index},
37        verification::{verify_proposal, verify_transaction},
38    },
39    zk::VerifyingKey,
40    Error, Result,
41};
42
43/// Gas limit for total block transactions(50 full transactions).
44pub const BLOCK_GAS_LIMIT: u64 = GAS_LIMIT * MAX_TX_CALLS as u64 * 50;
45
46/// This struct represents the information required by the consensus
47/// algorithm.
48pub struct Consensus {
49    /// Canonical (confirmed) blockchain
50    pub blockchain: Blockchain,
51    /// Fork size(length) after which it can be confirmed
52    pub confirmation_threshold: usize,
53    /// Fork chains containing block proposals
54    pub forks: Vec<Fork>,
55    /// Max in-memory forks to maintain.
56    max_forks: usize,
57    /// Canonical blockchain PoW module state
58    pub module: PoWModule,
59}
60
61impl Consensus {
62    /// Generate a new Consensus state.
63    pub fn new(
64        blockchain: Blockchain,
65        confirmation_threshold: usize,
66        max_forks: usize,
67        pow_target: u32,
68        pow_fixed_difficulty: Option<BigUint>,
69    ) -> Result<Self> {
70        let max_forks = if max_forks == 0 { 1 } else { max_forks };
71        let module = PoWModule::new(blockchain.clone(), pow_target, pow_fixed_difficulty, None)?;
72
73        Ok(Self { blockchain, confirmation_threshold, forks: vec![], max_forks, module })
74    }
75
76    /// Try to generate a new empty fork. If the forks bound has been
77    /// reached, try to replace the worst ranking one with the new
78    /// empty fork.
79    pub async fn generate_empty_fork(&mut self) -> Result<()> {
80        debug!(target: "validator::consensus::generate_empty_fork", "Generating new empty fork...");
81        // Check if we already have an empty fork
82        for fork in &self.forks {
83            if fork.proposals.is_empty() {
84                debug!(target: "validator::consensus::generate_empty_fork", "An empty fork already exists.");
85                return Ok(())
86            }
87        }
88        let fork = Fork::new(self.blockchain.clone(), self.module.clone()).await?;
89        self.push_fork(fork);
90        debug!(target: "validator::consensus::generate_empty_fork", "Fork generated!");
91
92        Ok(())
93    }
94
95    /// Auxiliary function to push a fork into the forks vector
96    /// respecting the bounding confirguration. The fork will be
97    /// inserted iff the bound has not be reached or it ranks higher
98    /// than the lowest ranking existing fork.
99    fn push_fork(&mut self, fork: Fork) {
100        // Check if we have reached the bound
101        if self.forks.len() < self.max_forks {
102            self.forks.push(fork);
103            return
104        }
105
106        // Grab worst fork. We don't care about competing forks since
107        // any of them can be replaced. It's safe to unwrap here since
108        // we already checked forks length. `best_fork_index` returns
109        // an error iff we pass an empty forks vector.
110        let index = worst_fork_index(&self.forks).unwrap();
111
112        // Check if the provided one ranks lower
113        if fork.targets_rank < self.forks[index].targets_rank {
114            return
115        }
116
117        // Break tie using their hash distances rank
118        if fork.targets_rank == self.forks[index].targets_rank &&
119            fork.hashes_rank <= self.forks[index].hashes_rank
120        {
121            return
122        }
123
124        // Replace the current worst fork with the provided one
125        self.forks[index] = fork;
126    }
127
128    /// Given a proposal, the node verifys it and finds which fork it
129    /// extends. If the proposal extends the canonical blockchain, a
130    /// new fork chain is created.
131    pub async fn append_proposal(
132        &mut self,
133        proposal: &Proposal,
134        is_new: bool,
135        verify_fees: bool,
136    ) -> Result<()> {
137        debug!(target: "validator::consensus::append_proposal", "Appending proposal {}", proposal.hash);
138
139        // Check if proposal already exists
140        for fork in &self.forks {
141            for p in fork.proposals.iter().rev() {
142                if p == &proposal.hash {
143                    debug!(target: "validator::consensus::append_proposal", "Proposal {} already exists", proposal.hash);
144                    return Err(Error::ProposalAlreadyExists)
145                }
146            }
147        }
148        // Check if proposal is canonical
149        if let Ok(canonical_headers) =
150            self.blockchain.blocks.get_order(&[proposal.block.header.height], true)
151        {
152            if canonical_headers[0].unwrap() == proposal.hash {
153                debug!(target: "validator::consensus::append_proposal", "Proposal {} already exists", proposal.hash);
154                return Err(Error::ProposalAlreadyExists)
155            }
156        }
157
158        // Verify proposal and grab corresponding fork
159        let (mut fork, index) = verify_proposal(self, proposal, is_new, verify_fees).await?;
160
161        // Append proposal to the fork
162        fork.append_proposal(proposal).await?;
163
164        // If a fork index was found, replace fork with the mutated
165        // one, otherwise try to push the new fork.
166        match index {
167            Some(i) => {
168                if i < self.forks.len() &&
169                    self.forks[i].proposals == fork.proposals[..fork.proposals.len() - 1]
170                {
171                    self.forks[i] = fork;
172                } else {
173                    self.push_fork(fork);
174                }
175            }
176            None => {
177                self.push_fork(fork);
178            }
179        }
180
181        info!(target: "validator::consensus::append_proposal", "Appended proposal {} - {}", proposal.hash, proposal.block.header.height);
182
183        Ok(())
184    }
185
186    /// Given a proposal, find the fork chain it extends, and return
187    /// its full clone. If the proposal extends the fork not on its
188    /// tail, a new fork is created and we re-apply the proposals up to
189    /// the extending one. If proposal extends canonical, a new fork is
190    /// created. Additionally, we return the fork index if a new fork
191    /// was not created, so caller can replace the fork.
192    pub async fn find_extended_fork(&self, proposal: &Proposal) -> Result<(Fork, Option<usize>)> {
193        // Check if proposal extends any fork
194        let found = find_extended_fork_index(&self.forks, proposal);
195        if found.is_err() {
196            if let Err(Error::ProposalAlreadyExists) = found {
197                return Err(Error::ProposalAlreadyExists)
198            }
199
200            // Check if proposal extends canonical
201            let (last_height, last_block) = self.blockchain.last()?;
202            if proposal.block.header.previous != last_block ||
203                proposal.block.header.height <= last_height
204            {
205                return Err(Error::ExtendedChainIndexNotFound)
206            }
207
208            // Check if we have an empty fork to use
209            for (f_index, fork) in self.forks.iter().enumerate() {
210                if fork.proposals.is_empty() {
211                    return Ok((self.forks[f_index].full_clone()?, Some(f_index)))
212                }
213            }
214
215            // Generate a new fork extending canonical
216            let fork = Fork::new(self.blockchain.clone(), self.module.clone()).await?;
217            return Ok((fork, None))
218        }
219
220        let (f_index, p_index) = found.unwrap();
221        let original_fork = &self.forks[f_index];
222        // Check if proposal extends fork at last proposal
223        if p_index == (original_fork.proposals.len() - 1) {
224            return Ok((original_fork.full_clone()?, Some(f_index)))
225        }
226
227        // Rebuild fork
228        let mut fork = Fork::new(self.blockchain.clone(), self.module.clone()).await?;
229        fork.proposals = original_fork.proposals[..p_index + 1].to_vec();
230        fork.diffs = original_fork.diffs[..p_index + 1].to_vec();
231
232        // Retrieve proposals blocks from original fork
233        let blocks = &original_fork.overlay.lock().unwrap().get_blocks_by_hash(&fork.proposals)?;
234        for (index, block) in blocks.iter().enumerate() {
235            // Apply block diffs
236            fork.overlay.lock().unwrap().overlay.lock().unwrap().add_diff(&fork.diffs[index])?;
237
238            // Calculate block rank
239            let (next_difficulty, target_distance_sq, hash_distance_sq) =
240                block_rank(&mut fork.module, block)?;
241
242            // Update PoW module
243            fork.module.append(&block.header, &next_difficulty)?;
244
245            // Update fork ranks
246            fork.targets_rank += target_distance_sq;
247            fork.hashes_rank += hash_distance_sq;
248        }
249
250        Ok((fork, None))
251    }
252
253    /// Check if best fork proposals can be confirmed.
254    /// Consensus confirmation logic:
255    /// - If the current best fork has reached greater length than the
256    ///   security threshold, and no other fork exist with same rank,
257    ///   first proposal(s) in that fork can be appended to
258    ///   canonical/confirmed blockchain.
259    ///
260    /// When best fork can be confirmed, first block(s) should be
261    /// appended to canonical, and forks should be rebuilt.
262    pub async fn confirmation(&self) -> Result<Option<usize>> {
263        debug!(target: "validator::consensus::confirmation", "Started confirmation check");
264
265        // Grab best fork index
266        let index = best_fork_index(&self.forks)?;
267
268        // Check its length
269        if self.forks[index].proposals.len() < self.confirmation_threshold {
270            debug!(target: "validator::consensus::confirmation", "Nothing to confirm yet, best fork size: {}", self.forks[index].proposals.len());
271            return Ok(None)
272        }
273
274        // Ensure no other fork exists with same rank
275        for (f_index, fork) in self.forks.iter().enumerate() {
276            // Skip best fork
277            if f_index == index {
278                continue
279            }
280
281            // Skip lower ranking forks
282            if fork.targets_rank != self.forks[index].targets_rank {
283                continue
284            }
285
286            // Check hash distances rank
287            if fork.hashes_rank == self.forks[index].hashes_rank {
288                debug!(target: "validator::consensus::confirmation", "Competing best forks found");
289                return Ok(None)
290            }
291        }
292
293        Ok(Some(index))
294    }
295
296    /// Auxiliary function to find the index of a fork containing the
297    /// provided header hash in its proposals.
298    fn find_fork_by_header(&self, fork_header: &HeaderHash) -> Option<usize> {
299        for (index, fork) in self.forks.iter().enumerate() {
300            for p in fork.proposals.iter().rev() {
301                if p == fork_header {
302                    return Some(index)
303                }
304            }
305        }
306        None
307    }
308
309    /// Auxiliary function to retrieve the fork header hash of provided
310    /// height. The fork is identified by the provided header hash.
311    pub async fn get_fork_header_hash(
312        &self,
313        height: u32,
314        fork_header: &HeaderHash,
315    ) -> Result<Option<HeaderHash>> {
316        // Find the fork containing the provided header
317        let Some(index) = self.find_fork_by_header(fork_header) else { return Ok(None) };
318
319        // Grab header if it exists
320        let header =
321            self.forks[index].overlay.lock().unwrap().blocks.get_order(&[height], false)?[0];
322
323        Ok(header)
324    }
325
326    /// Auxiliary function to retrieve the fork headers of provided
327    /// hashes. The fork is identified by the provided header hash. If
328    /// fork doesn't exists, an empty vector is returned.
329    pub async fn get_fork_headers(
330        &self,
331        headers: &[HeaderHash],
332        fork_header: &HeaderHash,
333    ) -> Result<Vec<Header>> {
334        // Find the fork containing the provided header
335        let Some(index) = self.find_fork_by_header(fork_header) else { return Ok(vec![]) };
336
337        // Grab headers
338        let headers = self.forks[index].overlay.lock().unwrap().get_headers_by_hash(headers)?;
339
340        Ok(headers)
341    }
342
343    /// Auxiliary function to retrieve the fork proposals of provided
344    /// hashes. The fork is identified by the provided header hash. If
345    /// fork doesn't exists, an empty vector is returned.
346    pub async fn get_fork_proposals(
347        &self,
348        headers: &[HeaderHash],
349        fork_header: &HeaderHash,
350    ) -> Result<Vec<Proposal>> {
351        // Find the fork containing the provided header
352        let Some(index) = self.find_fork_by_header(fork_header) else { return Ok(vec![]) };
353
354        // Grab proposals
355        let blocks = self.forks[index].overlay.lock().unwrap().get_blocks_by_hash(headers)?;
356        let mut proposals = Vec::with_capacity(blocks.len());
357        for block in blocks {
358            proposals.push(Proposal::new(block));
359        }
360
361        Ok(proposals)
362    }
363
364    /// Auxiliary function to retrieve a fork proposals, starting from
365    /// provided tip. If provided tip is too far behind, unknown, or
366    /// fork doesn't exists, an empty vector is returned. The fork is
367    /// identified by the optional provided header hash. If its `None`,
368    /// we use our best fork.
369    pub async fn get_fork_proposals_after(
370        &self,
371        tip: HeaderHash,
372        fork_tip: Option<HeaderHash>,
373        limit: u32,
374    ) -> Result<Vec<Proposal>> {
375        // Create return vector
376        let mut proposals = vec![];
377
378        // Grab fork index to use
379        let index = match fork_tip {
380            Some(fork_tip) => {
381                let Some(found) = self.find_fork_by_header(&fork_tip) else { return Ok(proposals) };
382                found
383            }
384            None => best_fork_index(&self.forks)?,
385        };
386
387        // Check tip exists
388        let Ok(existing_tips) =
389            self.forks[index].overlay.lock().unwrap().get_blocks_by_hash(&[tip])
390        else {
391            return Ok(proposals)
392        };
393
394        // Check tip is not far behind
395        let last_block_height = self.forks[index].overlay.lock().unwrap().last()?.0;
396        if last_block_height.saturating_sub(existing_tips[0].header.height) >= limit {
397            return Ok(proposals)
398        }
399
400        // Retrieve all proposals after requested one
401        let headers = self.blockchain.blocks.get_all_after(existing_tips[0].header.height)?;
402        let blocks = self.blockchain.get_blocks_by_hash(&headers)?;
403        for block in blocks {
404            proposals.push(Proposal::new(block));
405        }
406        let blocks = self.forks[index]
407            .overlay
408            .lock()
409            .unwrap()
410            .get_blocks_by_hash(&self.forks[index].proposals)?;
411        for block in blocks {
412            // Add fork proposals after the requested one height
413            if block.header.height > existing_tips[0].header.height {
414                proposals.push(Proposal::new(block));
415            }
416        }
417
418        Ok(proposals)
419    }
420
421    /// Auxiliary function to grab current mining RandomX key,
422    /// based on next block height.
423    /// If no forks exist, returns the canonical key.
424    pub async fn current_mining_randomx_key(&self) -> Result<HeaderHash> {
425        // Grab next block height and current keys.
426        // If no forks exist, use canonical keys
427        let (next_block_height, rx_keys) = if self.forks.is_empty() {
428            let (next_block_height, _) = self.blockchain.last()?;
429            (next_block_height + 1, self.module.darkfi_rx_keys)
430        } else {
431            // Grab best fork and its last proposal
432            let index = best_fork_index(&self.forks)?;
433            let fork = &self.forks[index];
434            let last = fork.last_proposal()?;
435            (last.block.header.height + 1, fork.module.darkfi_rx_keys)
436        };
437
438        // We only use the next key when the next block is the
439        // height changing one.
440        if next_block_height > RANDOMX_KEY_CHANGING_HEIGHT &&
441            next_block_height % RANDOMX_KEY_CHANGING_HEIGHT == RANDOMX_KEY_CHANGE_DELAY
442        {
443            Ok(rx_keys.1.ok_or_else(|| Error::ParseFailed("darkfi_rx_keys.1 unwrap() error"))?)
444        } else {
445            Ok(rx_keys.0)
446        }
447    }
448
449    /// Auxiliary function to grab best current fork full clone.
450    pub async fn best_current_fork(&self) -> Result<Fork> {
451        let index = best_fork_index(&self.forks)?;
452        self.forks[index].full_clone()
453    }
454
455    /// Auxiliary function to retrieve current best fork last header.
456    /// If no forks exist, grab the last header from canonical.
457    pub async fn best_fork_last_header(&self) -> Result<(u32, HeaderHash)> {
458        // Check if node has any forks
459        if self.forks.is_empty() {
460            return self.blockchain.last()
461        }
462
463        // Grab best fork
464        let index = best_fork_index(&self.forks)?;
465        let fork = &self.forks[index];
466
467        // Grab its last header
468        let last = fork.last_proposal()?;
469        Ok((last.block.header.height, last.hash))
470    }
471
472    /// Auxiliary function to purge current forks and reset the ones
473    /// starting with the provided prefix, excluding provided confirmed
474    /// fork. Additionally, remove confirmed transactions from the
475    /// forks mempools. This function assumes that the prefix blocks
476    /// have already been appended to canonical chain from the
477    /// confirmed fork.
478    ///
479    /// Note: Always remember to purge new trees from the database if
480    /// not needed.
481    pub async fn reset_forks(
482        &mut self,
483        prefix: &[HeaderHash],
484        confirmed_fork_index: &usize,
485        confirmed_txs: &[Transaction],
486    ) -> Result<()> {
487        // Find all the forks that start with the provided prefix,
488        // excluding confirmed fork index, and remove their prefixed
489        // proposals, and their corresponding diffs. If the fork is not
490        // starting with the provided prefix, drop it.
491        let excess = prefix.len();
492        let prefix_last_index = excess - 1;
493        let prefix_last = prefix.last().unwrap();
494        let mut keep = vec![true; self.forks.len()];
495        let confirmed_txs_hashes: Vec<TransactionHash> =
496            confirmed_txs.iter().map(|tx| tx.hash()).collect();
497        for (index, fork) in self.forks.iter_mut().enumerate() {
498            if &index == confirmed_fork_index {
499                // Remove confirmed proposals txs from fork's mempool
500                fork.mempool.retain(|tx| !confirmed_txs_hashes.contains(tx));
501                continue
502            }
503
504            // If a fork is empty, has less proposals than the prefix
505            // or it doesn't start with the provided prefix we mark it
506            // for removal. It's sufficient to check the prefix last
507            // as the hashes sequence matching is enforced by it, since
508            // it contains all previous ones.
509            if fork.proposals.is_empty() ||
510                prefix_last_index >= fork.proposals.len() ||
511                &fork.proposals[prefix_last_index] != prefix_last
512            {
513                keep[index] = false;
514                continue
515            }
516
517            // Remove confirmed proposals txs from fork's mempool
518            fork.mempool.retain(|tx| !confirmed_txs_hashes.contains(tx));
519
520            // Remove the commited differences
521            let rest_proposals = fork.proposals.split_off(excess);
522            let rest_diffs = fork.diffs.split_off(excess);
523            let mut diffs = fork.diffs.clone();
524            fork.proposals = rest_proposals;
525            fork.diffs = rest_diffs;
526            for diff in diffs.iter_mut() {
527                fork.overlay.lock().unwrap().overlay.lock().unwrap().remove_diff(diff);
528            }
529        }
530
531        // Drop invalid forks
532        let mut iter = keep.iter();
533        self.forks.retain(|_| *iter.next().unwrap());
534
535        // Remove confirmed proposals txs from the unporposed txs sled
536        // tree.
537        self.blockchain.remove_pending_txs_hashes(&confirmed_txs_hashes)?;
538
539        Ok(())
540    }
541
542    /// Auxiliary function to fully purge current forks and leave only
543    /// a new empty fork.
544    pub async fn purge_forks(&mut self) -> Result<()> {
545        debug!(target: "validator::consensus::purge_forks", "Purging current forks...");
546        self.forks = vec![Fork::new(self.blockchain.clone(), self.module.clone()).await?];
547        debug!(target: "validator::consensus::purge_forks", "Forks purged!");
548
549        Ok(())
550    }
551
552    /// Auxiliary function to reset PoW module.
553    pub async fn reset_pow_module(&mut self) -> Result<()> {
554        debug!(target: "validator::consensus::reset_pow_module", "Resetting PoW module...");
555        self.module = PoWModule::new(
556            self.blockchain.clone(),
557            self.module.target,
558            self.module.fixed_difficulty.clone(),
559            None,
560        )?;
561        debug!(target: "validator::consensus::reset_pow_module", "PoW module reset successfully!");
562
563        Ok(())
564    }
565
566    /// Auxiliary function to check current contracts states
567    /// Monotree(SMT) validity in all active forks and canonical.
568    pub async fn healthcheck(&self) -> Result<()> {
569        // Grab current canonical contracts states monotree root
570        let state_root = self.blockchain.contracts.get_state_monotree_root()?;
571
572        // Check that the root matches last block header state root
573        let last_block_state_root = self.blockchain.last_header()?.state_root;
574        if state_root != last_block_state_root {
575            return Err(Error::ContractsStatesRootError(
576                blake3::Hash::from_bytes(state_root).to_string(),
577                blake3::Hash::from_bytes(last_block_state_root).to_string(),
578            ));
579        }
580
581        // Check each fork health
582        for fork in &self.forks {
583            fork.healthcheck()?;
584        }
585
586        Ok(())
587    }
588
589    /// Auxiliary function to purge all unreferenced contract trees
590    /// from the database.
591    pub async fn purge_unreferenced_trees(
592        &self,
593        referenced_trees: &mut BTreeSet<IVec>,
594    ) -> Result<()> {
595        // Check if we have forks
596        if self.forks.is_empty() {
597            // If no forks exist, build a new one so we retrieve the
598            // native/protected trees references.
599            let fork = Fork::new(self.blockchain.clone(), self.module.clone()).await?;
600            fork.referenced_trees(referenced_trees);
601        } else {
602            // Iterate over current forks to retrieve referenced trees
603            for fork in &self.forks {
604                fork.referenced_trees(referenced_trees);
605            }
606        }
607
608        // Retrieve current database trees
609        let current_trees = self.blockchain.sled_db.tree_names();
610
611        // Iterate over current database trees and drop unreferenced
612        // contracts ones.
613        for tree in current_trees {
614            // Check if its referenced
615            if referenced_trees.contains(&tree) {
616                continue
617            }
618
619            // Check if its a contract tree pointer
620            let Ok(tree) = deserialize::<[u8; 32]>(&tree) else { continue };
621
622            // Drop it
623            debug!(target: "validator::consensus::purge_unreferenced_trees", "Dropping unreferenced tree: {}", blake3::Hash::from(tree));
624            self.blockchain.sled_db.drop_tree(tree)?;
625        }
626
627        Ok(())
628    }
629
630    /// Auxiliary function to purge all unproposed pending
631    /// transactions from the database.
632    pub async fn purge_unproposed_pending_txs(
633        &mut self,
634        mut proposed_txs: HashSet<TransactionHash>,
635    ) -> Result<()> {
636        // Iterate over all forks to find proposed txs
637        for fork in &self.forks {
638            // Grab all current proposals transactions hashes
639            let proposals_txs =
640                fork.overlay.lock().unwrap().get_blocks_txs_hashes(&fork.proposals)?;
641            for tx in proposals_txs {
642                proposed_txs.insert(tx);
643            }
644        }
645
646        // Iterate over all forks again to remove unproposed txs from
647        // their mempools.
648        for fork in self.forks.iter_mut() {
649            fork.mempool.retain(|tx| proposed_txs.contains(tx));
650        }
651
652        // Remove unproposed txs from the pending store
653        let proposed_txs: Vec<TransactionHash> = proposed_txs.into_iter().collect();
654        self.blockchain.reset_pending_txs(&proposed_txs)?;
655
656        Ok(())
657    }
658}
659
660/// This struct represents a block proposal, used for consensus.
661#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
662pub struct Proposal {
663    /// Block hash
664    pub hash: HeaderHash,
665    /// Block data
666    pub block: BlockInfo,
667}
668
669impl Proposal {
670    pub fn new(block: BlockInfo) -> Self {
671        let hash = block.hash();
672        Self { hash, block }
673    }
674}
675
676impl From<Proposal> for BlockInfo {
677    fn from(proposal: Proposal) -> BlockInfo {
678        proposal.block
679    }
680}
681
682/// Struct representing a forked blockchain state.
683///
684/// An overlay over the original blockchain is used, containing all
685/// pending to-write records. Additionally, each fork keeps a vector of
686/// valid pending transactions hashes, in order of receival, and the
687/// proposals hashes sequence, for validations.
688#[derive(Clone)]
689pub struct Fork {
690    /// Canonical (confirmed) blockchain
691    pub blockchain: Blockchain,
692    /// Overlay cache over canonical Blockchain
693    pub overlay: BlockchainOverlayPtr,
694    /// Current PoW module state
695    pub module: PoWModule,
696    /// Fork proposal hashes sequence
697    pub proposals: Vec<HeaderHash>,
698    /// Fork proposal overlay diffs sequence
699    pub diffs: Vec<SledDbOverlayStateDiff>,
700    /// Valid pending transaction hashes
701    pub mempool: Vec<TransactionHash>,
702    /// Current fork mining targets rank, cached for better performance
703    pub targets_rank: BigUint,
704    /// Current fork hashes rank, cached for better performance
705    pub hashes_rank: BigUint,
706}
707
708impl Fork {
709    pub async fn new(blockchain: Blockchain, module: PoWModule) -> Result<Self> {
710        let mempool = blockchain.get_pending_txs()?.iter().map(|tx| tx.hash()).collect();
711        let overlay = BlockchainOverlay::new(&blockchain)?;
712        // Retrieve last block difficulty to access current ranks
713        let last_difficulty = blockchain.last_block_difficulty()?;
714        let targets_rank = last_difficulty.ranks.targets_rank;
715        let hashes_rank = last_difficulty.ranks.hashes_rank;
716        Ok(Self {
717            blockchain,
718            overlay,
719            module,
720            proposals: vec![],
721            diffs: vec![],
722            mempool,
723            targets_rank,
724            hashes_rank,
725        })
726    }
727
728    /// Auxiliary function to append a proposal and update current fork
729    /// rank.
730    pub async fn append_proposal(&mut self, proposal: &Proposal) -> Result<()> {
731        // Calculate block rank
732        let (next_difficulty, target_distance_sq, hash_distance_sq) =
733            block_rank(&mut self.module, &proposal.block)?;
734
735        // Update fork ranks
736        self.targets_rank += target_distance_sq.clone();
737        self.hashes_rank += hash_distance_sq.clone();
738
739        // Generate block difficulty and update PoW module
740        let cumulative_difficulty =
741            self.module.cumulative_difficulty.clone() + next_difficulty.clone();
742        let ranks = BlockRanks::new(
743            target_distance_sq,
744            self.targets_rank.clone(),
745            hash_distance_sq,
746            self.hashes_rank.clone(),
747        );
748        let block_difficulty = BlockDifficulty::new(
749            proposal.block.header.height,
750            proposal.block.header.timestamp,
751            next_difficulty,
752            cumulative_difficulty,
753            ranks,
754        );
755        self.module.append_difficulty(&self.overlay, &proposal.block.header, block_difficulty)?;
756
757        // Push proposal's hash
758        self.proposals.push(proposal.hash);
759
760        // Push proposal overlay diff
761        self.diffs.push(self.overlay.lock().unwrap().overlay.lock().unwrap().diff(&self.diffs)?);
762
763        Ok(())
764    }
765
766    /// Auxiliary function to retrieve last proposal.
767    pub fn last_proposal(&self) -> Result<Proposal> {
768        let block = if let Some(last) = self.proposals.last() {
769            self.overlay.lock().unwrap().get_blocks_by_hash(&[*last])?[0].clone()
770        } else {
771            self.overlay.lock().unwrap().last_block()?
772        };
773
774        Ok(Proposal::new(block))
775    }
776
777    /// Auxiliary function to compute forks' next block height.
778    pub fn get_next_block_height(&self) -> Result<u32> {
779        let proposal = self.last_proposal()?;
780        Ok(proposal.block.header.height + 1)
781    }
782
783    /// Auxiliary function to retrieve unproposed valid transactions,
784    /// along with their total gas used and total paid fees.
785    ///
786    /// Note: Always remember to purge new trees from the database if
787    /// not needed.
788    pub async fn unproposed_txs(
789        &mut self,
790        verifying_block_height: u32,
791        verify_fees: bool,
792    ) -> Result<(Vec<Transaction>, u64, u64)> {
793        // Check if our mempool is not empty
794        if self.mempool.is_empty() {
795            return Ok((vec![], 0, 0))
796        }
797
798        // Transactions Merkle tree
799        let mut tree = MerkleTree::new(1);
800
801        // Total gas accumulators
802        let mut total_gas_used = 0_u64;
803        let mut total_gas_paid = 0_u64;
804
805        // Map of ZK proof verifying keys for the current transaction
806        // batch.
807        let mut vks: HashMap<[u8; 32], HashMap<String, VerifyingKey>> = HashMap::new();
808
809        // Grab all current proposals transactions hashes
810        let proposals_txs = self.overlay.lock().unwrap().get_blocks_txs_hashes(&self.proposals)?;
811
812        // Iterate through all pending transactions in the forks'
813        // mempool.
814        let mut unproposed_txs = vec![];
815        let mut erroneous_txs = vec![];
816        for tx in &self.mempool {
817            // If the hash is contained in the proposals transactions
818            // vec, skip it.
819            if proposals_txs.contains(tx) {
820                continue
821            }
822
823            // Retrieve the actual unproposed transaction
824            let unproposed_tx = match self.blockchain.transactions.get_pending(&[*tx], true) {
825                Ok(txs) => txs[0].clone().unwrap(),
826                Err(e) => {
827                    debug!(target: "validator::consensus::unproposed_txs", "Transaction retrieval failed: {e}");
828                    erroneous_txs.push(*tx);
829                    continue
830                }
831            };
832
833            // Update the verifying keys map
834            for call in &unproposed_tx.calls {
835                vks.entry(call.data.contract_id.to_bytes()).or_default();
836            }
837
838            // Verify the transaction against current state
839            self.overlay.lock().unwrap().checkpoint();
840            let gas_data = match verify_transaction(
841                &self.overlay,
842                verifying_block_height,
843                self.module.target,
844                &unproposed_tx,
845                &mut tree,
846                &mut vks,
847                verify_fees,
848            )
849            .await
850            {
851                Ok(gas_values) => gas_values,
852                Err(e) => {
853                    debug!(target: "validator::consensus::unproposed_txs", "Transaction verification failed: {e}");
854                    self.overlay.lock().unwrap().revert_to_checkpoint();
855                    erroneous_txs.push(*tx);
856                    continue
857                }
858            };
859
860            // Store the gas used by the verified transaction
861            let tx_gas_used = gas_data.total_gas_used();
862
863            // Calculate current accumulated gas usage
864            let accumulated_gas_usage = total_gas_used.saturating_add(tx_gas_used);
865
866            // Check gas limit - if accumulated gas used exceeds it,
867            // break out of loop.
868            if accumulated_gas_usage > BLOCK_GAS_LIMIT {
869                warn!(
870                    target: "validator::consensus::unproposed_txs",
871                    "Retrieving transaction {tx} would exceed configured unproposed transaction gas limit: {accumulated_gas_usage} - {BLOCK_GAS_LIMIT}"
872                );
873                self.overlay.lock().unwrap().revert_to_checkpoint();
874                break
875            }
876
877            // Update accumulated total gas
878            total_gas_used = total_gas_used.saturating_add(tx_gas_used);
879            total_gas_paid = total_gas_paid.saturating_add(gas_data.paid);
880
881            // Push the tx hash into the unproposed transactions vector
882            unproposed_txs.push(unproposed_tx);
883        }
884
885        // Remove erroneous transactions txs from fork's mempool
886        self.mempool.retain(|tx| !erroneous_txs.contains(tx));
887
888        Ok((unproposed_txs, total_gas_used, total_gas_paid))
889    }
890
891    /// Auxiliary function to create a full clone using
892    /// BlockchainOverlay::full_clone. Changes to this copy don't
893    /// affect original fork overlay records, since underlying overlay
894    /// pointer have been updated to the cloned one.
895    pub fn full_clone(&self) -> Result<Self> {
896        let blockchain = self.blockchain.clone();
897        let overlay = self.overlay.lock().unwrap().full_clone()?;
898        let module = self.module.clone();
899        let proposals = self.proposals.clone();
900        let diffs = self.diffs.clone();
901        let mempool = self.mempool.clone();
902        let targets_rank = self.targets_rank.clone();
903        let hashes_rank = self.hashes_rank.clone();
904
905        Ok(Self {
906            blockchain,
907            overlay,
908            module,
909            proposals,
910            diffs,
911            mempool,
912            targets_rank,
913            hashes_rank,
914        })
915    }
916
917    /// Auxiliary function to check current contracts states
918    /// Monotree(SMT) validity.
919    ///
920    /// Note: This should be executed on fresh forks and/or when
921    ///       a fork doesn't contain changes over the last appended
922    //        proposal.
923    pub fn healthcheck(&self) -> Result<()> {
924        // Grab current contracts states monotree root
925        let state_root = self.overlay.lock().unwrap().contracts.get_state_monotree_root()?;
926
927        // Check that the root matches last block header state root
928        let last_block_state_root = self.last_proposal()?.block.header.state_root;
929        if state_root != last_block_state_root {
930            return Err(Error::ContractsStatesRootError(
931                blake3::Hash::from_bytes(state_root).to_string(),
932                blake3::Hash::from_bytes(last_block_state_root).to_string(),
933            ));
934        }
935
936        Ok(())
937    }
938
939    /// Auxiliary function to retrieve all referenced trees from the
940    /// fork overlay and insert them to provided `BTreeSet`.
941    pub fn referenced_trees(&self, trees: &mut BTreeSet<IVec>) {
942        // Grab its current overlay
943        let fork_overlay = self.overlay.lock().unwrap();
944        let overlay = fork_overlay.overlay.lock().unwrap();
945
946        // Retrieve its initial trees
947        for initial_tree in &overlay.state.initial_tree_names {
948            trees.insert(initial_tree.clone());
949        }
950
951        // Retrieve its new trees
952        for new_tree in &overlay.state.new_tree_names {
953            trees.insert(new_tree.clone());
954        }
955
956        // Retrieve its dropped trees
957        for dropped_tree in overlay.state.dropped_trees.keys() {
958            trees.insert(dropped_tree.clone());
959        }
960
961        // Retrieve its protected trees
962        for protected_tree in &overlay.state.protected_tree_names {
963            trees.insert(protected_tree.clone());
964        }
965    }
966}