This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Improve transaction submission #6599
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b25c2f2
Improve transaction submission
bkchr 83a233e
Merge remote-tracking branch 'origin/master' into bkchr-fix-txpool-hi…
bkchr 16c65bf
Make sure we don't submit the same transaction twice from the network…
bkchr 9c4e575
Remove added listener call
bkchr 7638145
Feedback
bkchr 24ff601
Ignore banned on resubmit
bkchr 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
Next
Next commit
Improve transaction submission
Before this pr the transaction pool validated each transaction, even if the transaction was already known to the pool. This pr changes the behavior to first check if we are already aware of a transaction and thus, to only validate them if we don't know them yet. However, there is still the possibility that a given transaction is validated multiple times. This can happen if the transaction is added the first time, but is not yet validated and added to the validated pool. Besides that, this pr fixes the wrong metrics of gossiped transactions in the network. It also moves some metrics to the transaction pool api, to better track when a transaction actually is scheduled for validation.
- Loading branch information
commit b25c2f2de346b2503a295534437c666065a3e03a
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ use std::{ | |
|
|
||
| use crate::{base_pool as base, watcher::Watcher}; | ||
|
|
||
| use futures::{Future, FutureExt}; | ||
| use futures::Future; | ||
| use sp_runtime::{ | ||
| generic::BlockId, | ||
| traits::{self, SaturatedConversion, Block as BlockT}, | ||
|
|
@@ -149,23 +149,27 @@ impl<B: ChainApi> Pool<B> { | |
| } | ||
|
|
||
| /// Imports a bunch of unverified extrinsics to the pool | ||
| pub async fn submit_at<T>( | ||
| pub async fn submit_at( | ||
| &self, | ||
| at: &BlockId<B::Block>, | ||
| source: TransactionSource, | ||
| xts: T, | ||
| force: bool, | ||
| ) -> Result<Vec<Result<ExtrinsicHash<B>, B::Error>>, B::Error> where | ||
| T: IntoIterator<Item=ExtrinsicFor<B>>, | ||
| { | ||
| let validated_pool = self.validated_pool.clone(); | ||
| xts: impl IntoIterator<Item=ExtrinsicFor<B>>, | ||
| ) -> Result<Vec<Result<ExtrinsicHash<B>, B::Error>>, B::Error> { | ||
| let xts = xts.into_iter().map(|xt| (source, xt)); | ||
| self.verify(at, xts, force) | ||
| .map(move |validated_transactions| validated_transactions | ||
| .map(|validated_transactions| validated_pool.submit(validated_transactions | ||
| .into_iter() | ||
| .map(|(_, tx)| tx)))) | ||
| .await | ||
| let validated_transactions = self.verify(at, xts, true).await?; | ||
| Ok(self.validated_pool.submit(validated_transactions.into_iter().map(|(_, tx)| tx))) | ||
| } | ||
|
|
||
| /// Resubmit the given extrinsics to the pool. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment that it ignores the fact the transaction might be banned? I.e. this method is suitable to re-import recently pruned transactions (for instance coming from retracted blocks). |
||
| pub async fn resubmit_at( | ||
| &self, | ||
| at: &BlockId<B::Block>, | ||
| source: TransactionSource, | ||
| xts: impl IntoIterator<Item=ExtrinsicFor<B>>, | ||
| ) -> Result<Vec<Result<ExtrinsicHash<B>, B::Error>>, B::Error> { | ||
| let xts = xts.into_iter().map(|xt| (source, xt)); | ||
| let validated_transactions = self.verify(at, xts, false).await?; | ||
| Ok(self.validated_pool.submit(validated_transactions.into_iter().map(|(_, tx)| tx))) | ||
| } | ||
|
|
||
| /// Imports one unverified extrinsic to the pool | ||
|
|
@@ -175,12 +179,8 @@ impl<B: ChainApi> Pool<B> { | |
| source: TransactionSource, | ||
| xt: ExtrinsicFor<B>, | ||
| ) -> Result<ExtrinsicHash<B>, B::Error> { | ||
| self.submit_at(at, source, std::iter::once(xt), false) | ||
| .map(|import_result| import_result.and_then(|mut import_result| import_result | ||
| .pop() | ||
| .expect("One extrinsic passed; one result returned; qed") | ||
| )) | ||
| .await | ||
| let res = self.submit_at(at, source, std::iter::once(xt)).await?.pop(); | ||
| res.expect("One extrinsic passed; one result returned; qed") | ||
| } | ||
|
|
||
| /// Import a single extrinsic and starts to watch their progress in the pool. | ||
|
|
@@ -191,9 +191,7 @@ impl<B: ChainApi> Pool<B> { | |
| xt: ExtrinsicFor<B>, | ||
| ) -> Result<Watcher<ExtrinsicHash<B>, ExtrinsicHash<B>>, B::Error> { | ||
| let block_number = self.resolve_block_number(at)?; | ||
| let (_, tx) = self.verify_one( | ||
| at, block_number, source, xt, false | ||
| ).await; | ||
| let (_, tx) = self.verify_one(at, block_number, source, xt, true).await; | ||
| self.validated_pool.submit_and_watch(tx) | ||
| } | ||
|
|
||
|
|
@@ -328,7 +326,7 @@ impl<B: ChainApi> Pool<B> { | |
| .into_iter() | ||
| .map(|tx| (tx.source, tx.data.clone())); | ||
|
|
||
| let reverified_transactions = self.verify(at, pruned_transactions, false).await?; | ||
| let reverified_transactions = self.verify(at, pruned_transactions, true).await?; | ||
|
|
||
| log::trace!(target: "txpool", "Pruning at {:?}. Resubmitting transactions.", at); | ||
| // And finally - submit reverified transactions back to the pool | ||
|
|
@@ -358,23 +356,17 @@ impl<B: ChainApi> Pool<B> { | |
| &self, | ||
| at: &BlockId<B::Block>, | ||
| xts: impl IntoIterator<Item=(TransactionSource, ExtrinsicFor<B>)>, | ||
| force: bool, | ||
| check_is_known: bool, | ||
| ) -> Result<HashMap<ExtrinsicHash<B>, ValidatedTransactionFor<B>>, B::Error> { | ||
| // we need a block number to compute tx validity | ||
| let block_number = self.resolve_block_number(at)?; | ||
| let mut result = HashMap::new(); | ||
|
|
||
| for (hash, validated_tx) in | ||
| futures::future::join_all( | ||
| xts.into_iter() | ||
| .map(|(source, xt)| self.verify_one(at, block_number, source, xt, force)) | ||
| ) | ||
| .await | ||
| { | ||
| result.insert(hash, validated_tx); | ||
| } | ||
|
|
||
| Ok(result) | ||
| let res = futures::future::join_all( | ||
| xts.into_iter() | ||
| .map(|(source, xt)| self.verify_one(at, block_number, source, xt, check_is_known)) | ||
| ).await.into_iter().collect::<HashMap<_, _>>(); | ||
|
|
||
| Ok(res) | ||
| } | ||
|
|
||
| /// Returns future that validates single transaction at given block. | ||
|
|
@@ -384,14 +376,14 @@ impl<B: ChainApi> Pool<B> { | |
| block_number: NumberFor<B>, | ||
| source: TransactionSource, | ||
| xt: ExtrinsicFor<B>, | ||
| force: bool, | ||
| check_is_known: bool, | ||
| ) -> (ExtrinsicHash<B>, ValidatedTransactionFor<B>) { | ||
| let (hash, bytes) = self.validated_pool.api().hash_and_length(&xt); | ||
| if !force && self.validated_pool.is_banned(&hash) { | ||
| return ( | ||
| hash.clone(), | ||
| ValidatedTransaction::Invalid(hash, error::Error::TemporarilyBanned.into()), | ||
| ) | ||
|
|
||
| if check_is_known { | ||
| if let Err(err) = self.validated_pool.check_is_known(&hash) { | ||
| return (hash.clone(), ValidatedTransaction::Invalid(hash, err.into())) | ||
| } | ||
| } | ||
|
|
||
| let validation_result = self.validated_pool.api().validate_transaction( | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -137,10 +137,24 @@ impl<B: ChainApi> ValidatedPool<B> { | |
| self.rotator.is_banned(hash) | ||
| } | ||
|
|
||
| /// A fast check before doing any further processing of a transaction, like validation. | ||
| /// | ||
| /// It checks if the transaction is already imported or banned. If so, it returns an error. | ||
| pub fn check_is_known(&self, tx_hash: &ExtrinsicHash<B>) -> Result<(), B::Error> { | ||
| if self.is_banned(tx_hash) { | ||
| Err(error::Error::TemporarilyBanned.into()) | ||
| } else if self.pool.read().is_imported(tx_hash) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not do this check unconditionally? The transaction will fail to be imported anyhow, wouldn't it? |
||
| Err(error::Error::AlreadyImported(Box::new(tx_hash.clone())).into()) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Imports a bunch of pre-validated transactions to the pool. | ||
| pub fn submit<T>(&self, txs: T) -> Vec<Result<ExtrinsicHash<B>, B::Error>> where | ||
| T: IntoIterator<Item=ValidatedTransactionFor<B>> | ||
| { | ||
| pub fn submit( | ||
| &self, | ||
| txs: impl IntoIterator<Item=ValidatedTransactionFor<B>>, | ||
| ) -> Vec<Result<ExtrinsicHash<B>, B::Error>> { | ||
| let results = txs.into_iter() | ||
| .map(|validated_tx| self.submit_one(validated_tx)) | ||
| .collect::<Vec<_>>(); | ||
|
|
@@ -175,6 +189,7 @@ impl<B: ChainApi> ValidatedPool<B> { | |
| }, | ||
| ValidatedTransaction::Invalid(hash, err) => { | ||
| self.rotator.ban(&Instant::now(), std::iter::once(hash)); | ||
| self.listener.write().invalid(&hash, false); | ||
|
||
| Err(err.into()) | ||
| }, | ||
| ValidatedTransaction::Unknown(hash, err) => { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the
boolflag to an enum, I just realised you flipped the meaning and was confused with all thefalse->truechanges. Since it's a super important flag I think it's worth making it more descriptive.