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

Add OnPostInherent Hook (try 2) #10128

Closed
wants to merge 15 commits into from

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Oct 30, 2021

Fixes paritytech/polkadot-sdk#312
Fixes #5757

This PR introduces a new OnPostInherent hook within the block builder/execution pipeline.

This allows runtime developers to execute code after all inherent extrinsics have been executed, but before any signed extrinsics.

We introduce this hook only for the #[pallet] macro and not for the decl_module! macro. (it wouldnt be so hard to enable this if we wanted... but seems we should encourage people to start giving up decl_module! for new features.)

This should be completely backwards compatible with existing pallets which do not take advantage of this hook.

  • add tests
  • migrate scheduler

@github-actions github-actions bot added the A0-please_review Pull request needs code review. label Oct 30, 2021
@shawntabrizi shawntabrizi added B7-runtimenoteworthy D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited C1-low PR touches the given topic and has a low impact on builders. labels Oct 30, 2021
Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

maybe we should have a test that the client is calling on_post_inherent when building the block (maybe this is waht test-runtime is for)
And that executive is calling on_post_inherent when executing the block.

bin/node-template/runtime/src/lib.rs Outdated Show resolved Hide resolved
client/block-builder/src/lib.rs Outdated Show resolved Hide resolved
frame/support/src/dispatch.rs Show resolved Hide resolved
frame/support/src/traits/hooks.rs Outdated Show resolved Hide resolved
let block_id = &self.block_id;

self.api
.on_post_inherent_with_context(&block_id, ExecutionContext::BlockConstruction)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

what will happen if runtime have not yet implemented this API?

Copy link
Member Author

Choose a reason for hiding this comment

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

Block Production and Block Execution would come to different results.

Copy link
Contributor

@xlc xlc Oct 31, 2021

Choose a reason for hiding this comment

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

I think we need to think bit more about how to upgrade.

New runtime + old client needs to panic, else I think it will cause forking
Old runtime + new client should have the same behavior as old runtime + old client else it is forking

So the upgrade order should be upgrade client, and then runtime. And all the old client shouldn't be able to produce blocks for new runtime. Syncing maybe is fine?

Copy link
Member Author

Choose a reason for hiding this comment

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

syncing should be fine since there will be no implementations of on_post_inherent before this PR, so everything will be a noop.

I agree, this is clearly a client breaking change, and should be handled like any hard change, but also only if the feature is being used.

There is not much danger to release this in the wild as long as the feature is not used.

Copy link
Contributor

@gui1117 gui1117 Nov 1, 2021

Choose a reason for hiding this comment

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

if the client call the on_post_inherent api but the api doesn't exist in the runtime, I don't think it will be no-op. I think it will panic, no ?
there should be a way to only execute on_post_inherent if the api is provided by the runtime (by using version or stuff like this https://paritytech.github.io/substrate/master/sp_api/macro.decl_runtime_apis.html#runtime-api-trait-versioning), this way the client support all kind of runtime.

Thought the client will have to upgrade before the runtime, an old client will produce invalid block.

Copy link
Contributor

Choose a reason for hiding this comment

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

The more concrete solution to me would be:

  • bump the version of BlockBuilder
  • in the client we check the version of BlockBuilder and call into on_post_inherent only if version is superior or equal to the new version

When release a version we need to say that client must upgrade before runtime. or at least before runtime implement on_post_inherent with something not no-op.

@shawntabrizi
Copy link
Member Author

We need to bump version of block builder
Then bump runtime version
Then everything should okay

@gui1117
Copy link
Contributor

gui1117 commented Dec 2, 2021

yeah for me the solution is:

  • bump the version of BlockBuilder
  • in the client we check the version of BlockBuilder and call into on_post_inherent only if version is superior or equal to the new version

When release a version we need to say that client must upgrade before runtime. or at least before runtime implement on_post_inherent with something not no-op.

@JoshOrndorff
Copy link
Contributor

For anyone awaiting this feature, our current work-around is to create a dummy empty-payload inherent just to kick off the computation you want to run.

https://github.com/PureStake/nimbus/blob/ffb03c0614af1d3649fe52423ba99d98035baf74/pallets/author-inherent/src/lib.rs#L110-L127

@ggwpez
Copy link
Member

ggwpez commented Oct 6, 2022

stale

@ggwpez ggwpez closed this Oct 6, 2022
@louismerlin louismerlin removed the D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited label Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. C1-low PR touches the given topic and has a low impact on builders.
Projects
Status: Done
6 participants