Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Reduce provisioner work #6328

Merged
20 commits merged into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify code
  • Loading branch information
alexgparity committed Nov 26, 2022
commit c49f64b068578f0f069b95d6696d4e5fc7289389
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"

[dependencies]
async-trait = "0.1.57"
futures = "0.3.21"
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
24 changes: 20 additions & 4 deletions node/client/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,27 @@ pub fn benchmark_inherent_data(
parent_header: header,
};

struct BenchmarkProvider(polkadot_primitives::v2::InherentData);
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for BenchmarkProvider {
async fn provide_inherent_data(
&self,
dst_inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
dst_inherent_data.put_data(*b"BENCH_PR", &self.0)
This conversation was marked as resolved.
Show resolved Hide resolved
}

async fn try_handle_error(
&self,
_identifier: &sp_inherents::InherentIdentifier,
_error: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}

futures::executor::block_on(
polkadot_node_core_parachains_inherent::ParachainsInherentDataProvider::from_data(
para_data,
)
.provide_inherent_data(&mut inherent_data),
BenchmarkProvider(para_data).provide_inherent_data(&mut inherent_data),
)?;

Ok(inherent_data)
Expand Down
65 changes: 15 additions & 50 deletions node/core/parachains-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use polkadot_node_subsystem::{
errors::SubsystemError, messages::ProvisionerMessage, overseer::Handle,
};
use polkadot_primitives::v2::{Block, Hash, InherentData as ParachainsInherentData};
use sp_blockchain::HeaderBackend;
use sp_runtime::generic::BlockId;
use std::{sync::Arc, time};

Expand All @@ -39,22 +38,24 @@ pub(crate) const LOG_TARGET: &str = "parachain::parachains-inherent";
const PROVISIONER_TIMEOUT: time::Duration = core::time::Duration::from_millis(2500);

/// Provides the parachains inherent data.
pub struct ParachainsInherentDataProvider {
inherent_data: ParachainsInherentData,
pub struct ParachainsInherentDataProvider<C: sp_blockchain::HeaderBackend<Block>> {
pub client: Arc<C>,
pub overseer: polkadot_overseer::Handle,
pub parent: Hash,
}

impl ParachainsInherentDataProvider {
/// Create a [`Self`] directly from some [`ParachainsInherentData`].
pub fn from_data(inherent_data: ParachainsInherentData) -> Self {
Self { inherent_data }
impl<C: sp_blockchain::HeaderBackend<Block>> ParachainsInherentDataProvider<C> {
/// Create a new [`Self`].
pub fn new(client: Arc<C>, overseer: polkadot_overseer::Handle, parent: Hash) -> Self {
ParachainsInherentDataProvider { client, overseer, parent }
}

/// Create a new instance of the [`ParachainsInherentDataProvider`].
pub async fn create<C: HeaderBackend<Block>>(
pub async fn create(
client: Arc<C>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the logic in this function should be moved into the provide_inherent_data function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so? I like how this is actually a separate function handing all the data gathering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still put it into a separate function. I just don't like the duplication of the InherentDataProvider. This doesn't make any sense to me.

mut overseer: Handle,
parent: Hash,
) -> Result<Self, Error> {
) -> Result<ParachainsInherentData, Error> {
let pid = async {
let (sender, receiver) = futures::channel::oneshot::channel();
gum::trace!(
Expand Down Expand Up @@ -119,64 +120,28 @@ impl ParachainsInherentDataProvider {
},
};

Ok(Self { inherent_data })
}
}

#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for ParachainsInherentDataProvider {
async fn provide_inherent_data(
&self,
dst_inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
dst_inherent_data
.put_data(polkadot_primitives::v2::PARACHAINS_INHERENT_IDENTIFIER, &self.inherent_data)
}

async fn try_handle_error(
&self,
_identifier: &sp_inherents::InherentIdentifier,
_error: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
// Inherent isn't checked and can not return any error
None
}
}

/// Parachains inherent-data provider
//#[derive(Encode, Decode, Clone, PartialEq, RuntimeDebug, TypeInfo)]
pub struct InherentDataProvider<C: sp_blockchain::HeaderBackend<Block>> {
pub client: Arc<C>,
pub overseer: polkadot_overseer::Handle,
pub parent: Hash,
}

impl<C: sp_blockchain::HeaderBackend<Block>> InherentDataProvider<C> {
pub fn new(client: Arc<C>, overseer: polkadot_overseer::Handle, parent: Hash) -> Self {
InherentDataProvider { client, overseer, parent }
Ok(inherent_data)
}
}

#[async_trait::async_trait]
impl<C: sp_blockchain::HeaderBackend<Block>> sp_inherents::InherentDataProvider
for InherentDataProvider<C>
for ParachainsInherentDataProvider<C>
{
async fn provide_inherent_data(
&self,
dst_inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
let parachain = ParachainsInherentDataProvider::create(
let inherent_data = ParachainsInherentDataProvider::create(
self.client.clone(),
self.overseer.clone(),
self.parent,
)
.await
.map_err(|e| sp_inherents::Error::Application(Box::new(e)))?;

dst_inherent_data.put_data(
polkadot_primitives::v2::PARACHAINS_INHERENT_IDENTIFIER,
&parachain.inherent_data,
)
dst_inherent_data
.put_data(polkadot_primitives::v2::PARACHAINS_INHERENT_IDENTIFIER, &inherent_data)
}

async fn try_handle_error(
Expand Down
2 changes: 1 addition & 1 deletion node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ where

async move {
let parachain =
polkadot_node_core_parachains_inherent::InherentDataProvider::new(
polkadot_node_core_parachains_inherent::ParachainsInherentDataProvider::new(
client_clone,
overseer_handle,
parent,
Expand Down