-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1303 from subspace/refactor-subspace-transaction-…
…pool Refactor subspace transaction pool
- Loading branch information
Showing
20 changed files
with
481 additions
and
510 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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,114 @@ | ||
use sc_transaction_pool::error::Result as TxPoolResult; | ||
use sc_transaction_pool_api::error::Error as TxPoolError; | ||
use sc_transaction_pool_api::TransactionSource; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_core::traits::SpawnNamed; | ||
use sp_domains::transaction::{ | ||
InvalidTransactionCode, PreValidationObject, PreValidationObjectApi, | ||
}; | ||
use sp_runtime::generic::BlockId; | ||
use sp_runtime::traits::Block as BlockT; | ||
use std::marker::PhantomData; | ||
use std::sync::Arc; | ||
use subspace_fraud_proof::VerifyFraudProof; | ||
use subspace_transaction_pool::bundle_validator::ValidateBundle; | ||
use subspace_transaction_pool::PreValidateTransaction; | ||
|
||
pub struct PrimaryChainTxPreValidator<Block, Client, Verifier, BundleValidator> { | ||
client: Arc<Client>, | ||
spawner: Box<dyn SpawnNamed>, | ||
fraud_proof_verifier: Verifier, | ||
bundle_validator: BundleValidator, | ||
_phantom_data: PhantomData<Block>, | ||
} | ||
|
||
impl<Block, Client, Verifier, BundleValidator> Clone | ||
for PrimaryChainTxPreValidator<Block, Client, Verifier, BundleValidator> | ||
where | ||
Verifier: Clone, | ||
BundleValidator: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
client: self.client.clone(), | ||
spawner: self.spawner.clone(), | ||
fraud_proof_verifier: self.fraud_proof_verifier.clone(), | ||
bundle_validator: self.bundle_validator.clone(), | ||
_phantom_data: self._phantom_data, | ||
} | ||
} | ||
} | ||
|
||
impl<Block, Client, Verifier, BundleValidator> | ||
PrimaryChainTxPreValidator<Block, Client, Verifier, BundleValidator> | ||
{ | ||
pub fn new( | ||
client: Arc<Client>, | ||
spawner: Box<dyn SpawnNamed>, | ||
fraud_proof_verifier: Verifier, | ||
bundle_validator: BundleValidator, | ||
) -> Self { | ||
Self { | ||
client, | ||
spawner, | ||
fraud_proof_verifier, | ||
bundle_validator, | ||
_phantom_data: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<Block, Client, Verifier, BundleValidator> PreValidateTransaction | ||
for PrimaryChainTxPreValidator<Block, Client, Verifier, BundleValidator> | ||
where | ||
Block: BlockT, | ||
Client: ProvideRuntimeApi<Block> + Send + Sync, | ||
Client::Api: PreValidationObjectApi<Block, domain_runtime_primitives::Hash>, | ||
Verifier: VerifyFraudProof<Block> + Clone + Send + Sync + 'static, | ||
BundleValidator: | ||
ValidateBundle<Block, domain_runtime_primitives::Hash> + Clone + Send + Sync + 'static, | ||
{ | ||
type Block = Block; | ||
async fn pre_validate_transaction( | ||
&self, | ||
at: Block::Hash, | ||
_source: TransactionSource, | ||
uxt: Block::Extrinsic, | ||
) -> TxPoolResult<()> { | ||
let pre_validation_object = self | ||
.client | ||
.runtime_api() | ||
.extract_pre_validation_object(at, uxt.clone()) | ||
.map_err(|err| sc_transaction_pool::error::Error::Blockchain(err.into()))?; | ||
|
||
match pre_validation_object { | ||
PreValidationObject::Null => { | ||
// No pre-validation is required. | ||
} | ||
PreValidationObject::Bundle(bundle) => { | ||
if let Err(err) = self | ||
.bundle_validator | ||
.validate_bundle(&BlockId::Hash(at), &bundle) | ||
{ | ||
tracing::trace!(target: "txpool", error = ?err, "Dropped `submit_bundle` extrinsic"); | ||
return Err(TxPoolError::ImmediatelyDropped.into()); | ||
} | ||
} | ||
PreValidationObject::FraudProof(fraud_proof) => { | ||
subspace_fraud_proof::validate_fraud_proof_in_tx_pool( | ||
&self.spawner, | ||
self.fraud_proof_verifier.clone(), | ||
fraud_proof, | ||
) | ||
.await | ||
.map_err(|err| { | ||
tracing::debug!(target: "txpool", error = ?err, "Invalid fraud proof"); | ||
TxPoolError::InvalidTransaction(InvalidTransactionCode::FraudProof.into()) | ||
})?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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 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.