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:
Bastian Köcher
2020-07-02 15:17:14 +02:00
committed by GitHub
parent e1d0f84c67
commit 4f7f312be5
9 changed files with 78 additions and 48 deletions
@@ -253,18 +253,18 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
{
type StateBackend = C::StateBackend;
fn map_api_result<F: FnOnce(&Self) -> std::result::Result<R, E>, R, E>(
fn execute_in_transaction<F: FnOnce(&Self) -> #crate_::TransactionOutcome<R>, R>(
&self,
map_call: F,
) -> std::result::Result<R, E> where Self: Sized {
call: F,
) -> R where Self: Sized {
self.changes.borrow_mut().start_transaction();
*self.commit_on_success.borrow_mut() = false;
let res = map_call(self);
let res = call(self);
*self.commit_on_success.borrow_mut() = true;
self.commit_on_ok(&res);
self.commit_or_rollback(matches!(res, #crate_::TransactionOutcome::Commit(_)));
res
res.into_inner()
}
fn has_api<A: #crate_::RuntimeApiInfo + ?Sized>(
@@ -380,21 +380,21 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
&self.recorder,
);
self.commit_on_ok(&res);
self.commit_or_rollback(res.is_ok());
res
}
fn commit_on_ok<R, E>(&self, res: &std::result::Result<R, E>) {
fn commit_or_rollback(&self, commit: bool) {
let proof = "\
We only close a transaction when we opened one ourself.
Other parts of the runtime that make use of transactions (state-machine)
also balance their transactions. The runtime cannot close client initiated
transactions. qed";
if *self.commit_on_success.borrow() {
if res.is_err() {
self.changes.borrow_mut().rollback_transaction().expect(proof);
} else {
if commit {
self.changes.borrow_mut().commit_transaction().expect(proof);
} else {
self.changes.borrow_mut().rollback_transaction().expect(proof);
}
}
}
@@ -73,11 +73,11 @@ fn implement_common_api_traits(
impl #crate_::ApiExt<#block_type> for #self_ty {
type StateBackend = #crate_::InMemoryBackend<#crate_::HashFor<#block_type>>;
fn map_api_result<F: FnOnce(&Self) -> std::result::Result<R, E>, R, E>(
fn execute_in_transaction<F: FnOnce(&Self) -> #crate_::TransactionOutcome<R>, R>(
&self,
map_call: F,
) -> std::result::Result<R, E> where Self: Sized {
map_call(self)
call: F,
) -> R where Self: Sized {
call(self).into_inner()
}
fn has_api<A: #crate_::RuntimeApiInfo + ?Sized>(