-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Tx pool output temporary cache #2802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1f49b75
Add saved output cache
AurelienFT 04ec7f0
Change new txs notifier to be notified only on new execution txs.
AurelienFT a259c83
Update changelog
AurelienFT 3f2b749
Merge branch 'master' into tx_pool_output_temporary_cache
AurelienFT cd4d254
Change how we notify about new executable transactions
AurelienFT 57baa14
Fix test
AurelienFT 36b8948
Merge branch 'master' into tx_pool_output_temporary_cache
xgreenx e1f2698
Fix all tests
AurelienFT a2b5d75
Fixed the issues with dependent transactions during block import.
xgreenx dee7e2d
Add test temporary cache
AurelienFT 9d350d6
Change test of saved output to be a loop and test dependent behavior …
AurelienFT b317f8a
update comment and rename test
AurelienFT 00f18db
Merge branch 'master' into tx_pool_output_temporary_cache
xgreenx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add a new cache with outputs extracted from the pool for the duration of the block. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Change new txs notifier to be notified only on executable transactions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| mod collisions; | ||
|
|
||
| use std::{ | ||
| collections::HashMap, | ||
| collections::{ | ||
| HashMap, | ||
| HashSet, | ||
| }, | ||
| iter, | ||
| time::{ | ||
| Instant, | ||
|
|
@@ -14,7 +17,13 @@ use fuel_core_metrics::txpool_metrics::txpool_metrics; | |
| use fuel_core_types::{ | ||
| fuel_tx::{ | ||
| field::BlobId, | ||
| Address, | ||
| AssetId, | ||
| ContractId, | ||
| Output, | ||
| TxId, | ||
| UtxoId, | ||
| Word, | ||
| }, | ||
| services::txpool::{ | ||
| ArcPoolTx, | ||
|
|
@@ -47,16 +56,27 @@ use crate::{ | |
| }, | ||
| }; | ||
|
|
||
| #[cfg(test)] | ||
| use std::collections::HashSet; | ||
|
|
||
| #[derive(Debug, Clone, Copy, Default)] | ||
| pub struct TxPoolStats { | ||
| pub tx_count: u64, | ||
| pub total_size: u64, | ||
| pub total_gas: u64, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)] | ||
| pub(crate) struct SavedCoinOutput { | ||
| pub utxo_id: UtxoId, | ||
| pub to: Address, | ||
| pub amount: Word, | ||
| pub asset_id: AssetId, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord)] | ||
| pub(crate) enum SavedOutput { | ||
| Coin(SavedCoinOutput), | ||
| Contract(ContractId), | ||
| } | ||
|
|
||
| /// The pool is the main component of the txpool service. It is responsible for storing transactions | ||
| /// and allowing the selection of transactions for inclusion in a block. | ||
| pub struct Pool<S, SI, CM, SA> { | ||
|
|
@@ -70,6 +90,8 @@ pub struct Pool<S, SI, CM, SA> { | |
| pub(crate) selection_algorithm: SA, | ||
| /// Mapping from tx_id to storage_id. | ||
| pub(crate) tx_id_to_storage_id: HashMap<TxId, SI>, | ||
| /// All sent outputs when transactions are extracted. Clear when processing a block. | ||
| pub(crate) extracted_outputs: HashSet<SavedOutput>, | ||
| /// Current pool gas stored. | ||
| pub(crate) current_gas: u64, | ||
| /// Current pool size in bytes. | ||
|
|
@@ -93,6 +115,7 @@ impl<S, SI, CM, SA> Pool<S, SI, CM, SA> { | |
| selection_algorithm, | ||
| config, | ||
| tx_id_to_storage_id: HashMap::new(), | ||
| extracted_outputs: HashSet::new(), | ||
| current_gas: 0, | ||
| current_bytes_size: 0, | ||
| pool_stats_sender, | ||
|
|
@@ -119,7 +142,7 @@ where | |
| &mut self, | ||
| tx: ArcPoolTx, | ||
| persistent_storage: &impl TxPoolPersistentStorage, | ||
| ) -> Result<Vec<ArcPoolTx>, InsertionErrorType> { | ||
| ) -> Result<(Vec<ArcPoolTx>, bool), InsertionErrorType> { | ||
| let insertion_result = self.insert_inner(tx, persistent_storage); | ||
| self.register_transaction_counts(); | ||
| insertion_result | ||
|
|
@@ -129,7 +152,7 @@ where | |
| &mut self, | ||
| tx: std::sync::Arc<PoolTransaction>, | ||
| persistent_storage: &impl TxPoolPersistentStorage, | ||
| ) -> Result<Vec<std::sync::Arc<PoolTransaction>>, InsertionErrorType> { | ||
| ) -> Result<(Vec<std::sync::Arc<PoolTransaction>>, bool), InsertionErrorType> { | ||
| let CanStoreTransaction { | ||
| checked_transaction, | ||
| transactions_to_remove, | ||
|
|
@@ -189,7 +212,7 @@ where | |
| .map(|data| data.transaction) | ||
| .collect::<Vec<_>>(); | ||
| self.update_stats(); | ||
| Ok(removed_transactions) | ||
| Ok((removed_transactions, !has_dependencies)) | ||
| } | ||
|
|
||
| fn update_stats(&self) { | ||
|
|
@@ -228,6 +251,7 @@ where | |
| self.storage.validate_inputs( | ||
| &tx, | ||
| persistent_storage, | ||
| &self.extracted_outputs, | ||
| self.config.utxo_validation, | ||
| )?; | ||
|
|
||
|
|
@@ -300,6 +324,43 @@ where | |
| } | ||
| } | ||
|
|
||
| fn populate_saved_outputs_cache(&mut self, best_txs: &[StorageData]) { | ||
| self.extracted_outputs.clear(); | ||
| for tx in best_txs { | ||
| for (idx, output) in tx.transaction.outputs().iter().enumerate() { | ||
| match output { | ||
| Output::Coin { | ||
| to, | ||
| amount, | ||
| asset_id, | ||
| } => { | ||
| self.extracted_outputs.insert(SavedOutput::Coin( | ||
| SavedCoinOutput { | ||
| utxo_id: UtxoId::new( | ||
| tx.transaction.id(), | ||
| u16::try_from(idx) | ||
| .expect("Outputs count is less than u16::MAX"), | ||
| ), | ||
| to: *to, | ||
| amount: *amount, | ||
| asset_id: *asset_id, | ||
| }, | ||
| )); | ||
| } | ||
| Output::ContractCreated { contract_id, .. } => { | ||
| self.extracted_outputs | ||
| .insert(SavedOutput::Contract(*contract_id)); | ||
| } | ||
| Output::Contract { .. } | ||
| | Output::Change { .. } | ||
| | Output::Variable { .. } => { | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: Use block space also (https://github.com/FuelLabs/fuel-core/issues/2133) | ||
| /// Extract transactions for a block. | ||
| /// Returns a list of transactions that were selected for the block | ||
|
|
@@ -313,6 +374,9 @@ where | |
| let best_txs = self | ||
| .selection_algorithm | ||
| .gather_best_txs(constraints, &mut self.storage); | ||
|
|
||
| self.populate_saved_outputs_cache(&best_txs); | ||
|
|
||
| if let Some(start) = maybe_start { | ||
| Self::record_select_transaction_time(start) | ||
| }; | ||
|
|
@@ -350,7 +414,8 @@ where | |
|
|
||
| /// Remove transaction but keep its dependents. | ||
| /// The dependents become executables. | ||
|
||
| pub fn remove_transactions(&mut self, tx_ids: impl Iterator<Item = TxId>) { | ||
| pub fn process_block(&mut self, tx_ids: impl Iterator<Item = TxId>) { | ||
| self.extracted_outputs.clear(); | ||
| for tx_id in tx_ids { | ||
| if let Some(storage_id) = self.tx_id_to_storage_id.remove(&tx_id) { | ||
| let dependents: Vec<S::StorageIndex> = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.