Skip to content
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

BlockId removal: tx-pool refactor #1678

Merged
merged 14 commits into from
Sep 27, 2023
8 changes: 2 additions & 6 deletions substrate/client/rpc-spec-v2/src/transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::sync::Arc;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_runtime::{generic, traits::Block as BlockT};
use sp_runtime::traits::Block as BlockT;

use codec::Decode;
use futures::{FutureExt, StreamExt, TryFutureExt};
Expand Down Expand Up @@ -110,11 +110,7 @@ where

let submit = self
.pool
.submit_and_watch(
&generic::BlockId::hash(best_block_hash),
TX_SOURCE,
decoded_extrinsic,
)
.submit_and_watch(best_block_hash, TX_SOURCE, decoded_extrinsic)
.map_err(|e| {
e.into_pool_error()
.map(Error::from)
Expand Down
30 changes: 12 additions & 18 deletions substrate/client/rpc/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_keystore::{KeystoreExt, KeystorePtr};
use sp_runtime::{generic, traits::Block as BlockT};
use sp_runtime::traits::Block as BlockT;
use sp_session::SessionKeys;

use self::error::{Error, Result};
Expand Down Expand Up @@ -97,15 +97,12 @@ where
Err(err) => return Err(Error::Client(Box::new(err)).into()),
};
let best_block_hash = self.client.info().best_hash;
self.pool
.submit_one(&generic::BlockId::hash(best_block_hash), TX_SOURCE, xt)
.await
.map_err(|e| {
e.into_pool_error()
.map(|e| Error::Pool(e))
.unwrap_or_else(|e| Error::Verification(Box::new(e)))
.into()
})
self.pool.submit_one(best_block_hash, TX_SOURCE, xt).await.map_err(|e| {
e.into_pool_error()
.map(|e| Error::Pool(e))
.unwrap_or_else(|e| Error::Verification(Box::new(e)))
.into()
})
}

fn insert_key(&self, key_type: String, suri: String, public: Bytes) -> RpcResult<()> {
Expand Down Expand Up @@ -191,14 +188,11 @@ where
},
};

let submit = self
.pool
.submit_and_watch(&generic::BlockId::hash(best_block_hash), TX_SOURCE, dxt)
.map_err(|e| {
e.into_pool_error()
.map(error::Error::from)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)))
});
let submit = self.pool.submit_and_watch(best_block_hash, TX_SOURCE, dxt).map_err(|e| {
e.into_pool_error()
.map(error::Error::from)
.unwrap_or_else(|e| error::Error::Verification(Box::new(e)))
});

let fut = async move {
let stream = match submit.await {
Expand Down
9 changes: 2 additions & 7 deletions substrate/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ use sc_network_sync::SyncingService;
use sc_utils::mpsc::TracingUnboundedReceiver;
use sp_blockchain::HeaderMetadata;
use sp_consensus::SyncOracle;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};

pub use self::{
builder::{
Expand Down Expand Up @@ -481,10 +478,8 @@ where
},
};

let best_block_id = BlockId::hash(self.client.info().best_hash);

let import_future = self.pool.submit_one(
&best_block_id,
self.client.info().best_hash,
sc_transaction_pool_api::TransactionSource::External,
uxt,
);
Expand Down
11 changes: 4 additions & 7 deletions substrate/client/transaction-pool/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ use codec::Codec;
use futures::{Future, Stream};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp_core::offchain::TransactionPoolExt;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Member, NumberFor},
};
use sp_runtime::traits::{Block as BlockT, Member, NumberFor};
use std::{collections::HashMap, hash::Hash, marker::PhantomData, pin::Pin, sync::Arc};

const LOG_TARGET: &str = "txpool::api";
Expand Down Expand Up @@ -202,15 +199,15 @@ pub trait TransactionPool: Send + Sync {
/// Returns a future that imports a bunch of unverified transactions to the pool.
fn submit_at(
&self,
at: &BlockId<Self::Block>,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
xts: Vec<TransactionFor<Self>>,
) -> PoolFuture<Vec<Result<TxHash<Self>, Self::Error>>, Self::Error>;

/// Returns a future that imports one unverified transaction to the pool.
fn submit_one(
&self,
at: &BlockId<Self::Block>,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
xt: TransactionFor<Self>,
) -> PoolFuture<TxHash<Self>, Self::Error>;
Expand All @@ -219,7 +216,7 @@ pub trait TransactionPool: Send + Sync {
/// pool.
fn submit_and_watch(
&self,
at: &BlockId<Self::Block>,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
xt: TransactionFor<Self>,
) -> PoolFuture<Pin<Box<TransactionStatusStreamFor<Self>>>, Self::Error>;
Expand Down
27 changes: 11 additions & 16 deletions substrate/client/transaction-pool/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ where

fn validate_transaction(
&self,
at: &BlockId<Self::Block>,
at: <Self::Block as BlockT>::Hash,
source: TransactionSource,
uxt: graph::ExtrinsicFor<Self>,
) -> Self::ValidationFuture {
let (tx, rx) = oneshot::channel();
let client = self.client.clone();
let at = *at;
let validation_pool = self.validation_pool.clone();
let metrics = self.metrics.clone();

Expand All @@ -151,7 +150,7 @@ where
.await
.send(
async move {
let res = validate_transaction_blocking(&*client, &at, source, uxt);
let res = validate_transaction_blocking(&*client, at, source, uxt);
let _ = tx.send(res);
metrics.report(|m| m.validations_finished.inc());
}
Expand Down Expand Up @@ -209,7 +208,7 @@ where
/// This method will call into the runtime to perform the validation.
fn validate_transaction_blocking<Client, Block>(
client: &Client,
at: &BlockId<Block>,
at: Block::Hash,
source: TransactionSource,
uxt: graph::ExtrinsicFor<FullChainApi<Client, Block>>,
) -> error::Result<TransactionValidity>
Expand All @@ -225,14 +224,10 @@ where
{
sp_tracing::within_span!(sp_tracing::Level::TRACE, "validate_transaction";
{
let block_hash = client.to_hash(at)
.map_err(|e| Error::RuntimeApi(e.to_string()))?
.ok_or_else(|| Error::RuntimeApi(format!("Could not get hash for block `{:?}`.", at)))?;

let runtime_api = client.runtime_api();
let api_version = sp_tracing::within_span! { sp_tracing::Level::TRACE, "check_version";
runtime_api
.api_version::<dyn TaggedTransactionQueue<Block>>(block_hash)
.api_version::<dyn TaggedTransactionQueue<Block>>(at)
.map_err(|e| Error::RuntimeApi(e.to_string()))?
.ok_or_else(|| Error::RuntimeApi(
format!("Could not find `TaggedTransactionQueue` api for block `{:?}`.", at)
Expand All @@ -245,31 +240,31 @@ where
sp_tracing::Level::TRACE, "runtime::validate_transaction";
{
if api_version >= 3 {
runtime_api.validate_transaction(block_hash, source, uxt, block_hash)
runtime_api.validate_transaction(at, source, uxt, at)
.map_err(|e| Error::RuntimeApi(e.to_string()))
} else {
let block_number = client.to_number(at)
let block_number = client.to_number(&BlockId::Hash(at))
.map_err(|e| Error::RuntimeApi(e.to_string()))?
.ok_or_else(||
Error::RuntimeApi(format!("Could not get number for block `{:?}`.", at))
)?;

// The old versions require us to call `initialize_block` before.
runtime_api.initialize_block(block_hash, &sp_runtime::traits::Header::new(
runtime_api.initialize_block(at, &sp_runtime::traits::Header::new(
block_number + sp_runtime::traits::One::one(),
Default::default(),
Default::default(),
block_hash,
at,
Default::default()),
).map_err(|e| Error::RuntimeApi(e.to_string()))?;

if api_version == 2 {
#[allow(deprecated)] // old validate_transaction
runtime_api.validate_transaction_before_version_3(block_hash, source, uxt)
runtime_api.validate_transaction_before_version_3(at, source, uxt)
.map_err(|e| Error::RuntimeApi(e.to_string()))
} else {
#[allow(deprecated)] // old validate_transaction
runtime_api.validate_transaction_before_version_2(block_hash, uxt)
runtime_api.validate_transaction_before_version_2(at, uxt)
.map_err(|e| Error::RuntimeApi(e.to_string()))
}
}
Expand All @@ -294,7 +289,7 @@ where
/// the runtime locally.
pub fn validate_transaction_blocking(
&self,
at: &BlockId<Block>,
at: Block::Hash,
source: TransactionSource,
uxt: graph::ExtrinsicFor<Self>,
) -> error::Result<TransactionValidity> {
Expand Down
Loading