mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 14:41:11 +00:00
Move create_inherents into the block-builder (#6553)
* Move `create_inherents` into the block-builder This moves the `create_inherents` call into the block-builder. This has the advantage that `create_inherents` will be able to reuse the same context that will be used when applying the extrinsics and we also save one call to `on_initialize`. To make sure that `create_inherents` does not modify any state, we execute it in a transaction that is rolled-back after doing the runtime call. * Feedback and build fix * Update primitives/runtime/src/lib.rs Co-authored-by: Sergei Shulepov <sergei@parity.io> * Update client/block-builder/src/lib.rs Co-authored-by: Sergei Shulepov <sergei@parity.io>
This commit is contained in:
@@ -26,7 +26,6 @@ use codec::Decode;
|
||||
use sp_consensus::{evaluation, Proposal, RecordProof};
|
||||
use sp_inherents::InherentData;
|
||||
use log::{error, info, debug, trace, warn};
|
||||
use sp_core::ExecutionContext;
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, DigestFor, BlakeTwo256},
|
||||
@@ -200,15 +199,7 @@ impl<A, B, Block, C> Proposer<B, Block, C, A>
|
||||
record_proof,
|
||||
)?;
|
||||
|
||||
// We don't check the API versions any further here since the dispatch compatibility
|
||||
// check should be enough.
|
||||
for inherent in self.client.runtime_api()
|
||||
.inherent_extrinsics_with_context(
|
||||
&self.parent_id,
|
||||
ExecutionContext::BlockConstruction,
|
||||
inherent_data
|
||||
)?
|
||||
{
|
||||
for inherent in block_builder.create_inherents(inherent_data)? {
|
||||
match block_builder.push(inherent) {
|
||||
Err(ApplyExtrinsicFailed(Validity(e))) if e.exhausted_resources() =>
|
||||
warn!("⚠️ Dropping non-mandatory inherent from overweight block."),
|
||||
|
||||
@@ -20,6 +20,7 @@ sp-consensus = { version = "0.8.0-rc4", path = "../../primitives/consensus/commo
|
||||
sp-blockchain = { version = "2.0.0-rc4", path = "../../primitives/blockchain" }
|
||||
sp-core = { version = "2.0.0-rc4", path = "../../primitives/core" }
|
||||
sp-block-builder = { version = "2.0.0-rc4", path = "../../primitives/block-builder" }
|
||||
sp-inherents = { version = "2.0.0-rc4", path = "../../primitives/inherents" }
|
||||
sc-client-api = { version = "2.0.0-rc4", path = "../api" }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.1", features = ["derive"] }
|
||||
|
||||
|
||||
@@ -34,7 +34,10 @@ use sp_runtime::{
|
||||
};
|
||||
use sp_blockchain::{ApplyExtrinsicFailed, Error};
|
||||
use sp_core::ExecutionContext;
|
||||
use sp_api::{Core, ApiExt, ApiErrorFor, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof};
|
||||
use sp_api::{
|
||||
Core, ApiExt, ApiErrorFor, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof,
|
||||
TransactionOutcome,
|
||||
};
|
||||
use sp_consensus::RecordProof;
|
||||
|
||||
pub use sp_block_builder::BlockBuilder as BlockBuilderApi;
|
||||
@@ -156,17 +159,22 @@ where
|
||||
let block_id = &self.block_id;
|
||||
let extrinsics = &mut self.extrinsics;
|
||||
|
||||
self.api.map_api_result(|api| {
|
||||
self.api.execute_in_transaction(|api| {
|
||||
match api.apply_extrinsic_with_context(
|
||||
block_id,
|
||||
ExecutionContext::BlockConstruction,
|
||||
xt.clone(),
|
||||
)? {
|
||||
Ok(_) => {
|
||||
) {
|
||||
Ok(Ok(_)) => {
|
||||
extrinsics.push(xt);
|
||||
Ok(())
|
||||
TransactionOutcome::Commit(Ok(()))
|
||||
}
|
||||
Err(tx_validity) => Err(ApplyExtrinsicFailed::Validity(tx_validity).into()),
|
||||
Ok(Err(tx_validity)) => {
|
||||
TransactionOutcome::Rollback(
|
||||
Err(ApplyExtrinsicFailed::Validity(tx_validity).into()),
|
||||
)
|
||||
},
|
||||
Err(e) => TransactionOutcome::Rollback(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -212,6 +220,25 @@ where
|
||||
proof,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create the inherents for the block.
|
||||
///
|
||||
/// Returns the inherents created by the runtime or an error if something failed.
|
||||
pub fn create_inherents(
|
||||
&mut self,
|
||||
inherent_data: sp_inherents::InherentData,
|
||||
) -> Result<Vec<Block::Extrinsic>, ApiErrorFor<A, Block>> {
|
||||
let block_id = self.block_id;
|
||||
self.api.execute_in_transaction(move |api| {
|
||||
// `create_inherents` should not change any state, to ensure this we always rollback
|
||||
// the transaction.
|
||||
TransactionOutcome::Rollback(api.inherent_extrinsics_with_context(
|
||||
&block_id,
|
||||
ExecutionContext::BlockConstruction,
|
||||
inherent_data
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user