fuel_core_txpool/selection_algorithms/
mod.rs

1use std::collections::HashSet;
2
3use fuel_core_types::fuel_tx::ContractId;
4
5use crate::storage::{
6    RemovedTransactions,
7    StorageData,
8};
9
10pub mod ratio_tip_gas;
11
12/// Constraints that the selection algorithm has to respect.
13pub struct Constraints {
14    /// Minimum gas price that all transaction must support.
15    pub minimal_gas_price: u64,
16    /// Maximum limit of gas that all selected transaction shouldn't exceed.
17    pub max_gas: u64,
18    /// Maximum number of transactions that can be selected.
19    pub maximum_txs: u16,
20    /// Maximum size of the block.
21    pub maximum_block_size: u32,
22    /// List of excluded contracts.
23    pub excluded_contracts: HashSet<ContractId>,
24}
25
26/// The selection algorithm is responsible for selecting the best transactions to include in a block.
27pub trait SelectionAlgorithm {
28    /// The storage type of the selection algorithm.
29    type Storage;
30    /// The index that identifies a transaction in the storage.
31    type StorageIndex;
32    /// Given the constraints, the selection algorithm has to return the best list of transactions to include in a block.
33    fn gather_best_txs(
34        &mut self,
35        constraints: Constraints,
36        storage: &mut Self::Storage,
37    ) -> RemovedTransactions;
38
39    /// Update the selection algorithm with the new transaction that are executable.
40    fn new_executable_transaction(
41        &mut self,
42        storage_id: Self::StorageIndex,
43        store_entry: &StorageData,
44    );
45
46    /// Returns the number of executable transactions
47    fn number_of_executable_transactions(&self) -> usize;
48
49    /// Get less worth transactions iterator
50    fn get_less_worth_txs(&self) -> impl Iterator<Item = &Self::StorageIndex>;
51
52    /// Inform the selection algorithm that a transaction was removed from the pool.
53    fn on_removed_transaction(&mut self, storage_entry: &StorageData);
54}