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

PoV Reclaim Runtime Side #3002

Merged
merged 32 commits into from
Feb 23, 2024
Merged

PoV Reclaim Runtime Side #3002

merged 32 commits into from
Feb 23, 2024

Conversation

skunert
Copy link
Contributor

@skunert skunert commented Jan 19, 2024

Runtime side for PoV Reclaim

Implementation Overview

  • Hostfunction to fetch the storage proof size has been added to the PVF. It uses the size tracking recorder that was introduced in my previous PR.
  • Mechanisms to use the reclaim HostFunction have been introduced.
      1. A SignedExtension that checks the node-reported proof size before and after application of an extrinsic. Then it reclaims the difference.
      1. A manual helper to make reclaiming easier when manual interaction is required, for example in on_idle or other hooks.
  • In order to utilize the manual reclaiming, I modified WeightMeter to support the reduction of consumed weight, at least for storage proof size.

How to use

To enable the general functionality for a parachain:

  1. Add the SignedExtension to your parachain runtime.
  2. Provide the HostFunction to the node
  3. Enable proof recording during block import

TODO

  • PRDoc

@skunert skunert added T0-node This PR/Issue is related to the topic “node”. T1-FRAME This PR/Issue is related to core FRAME, the framework. T5-host_functions This PR/Issue is related to host functions. T9-cumulus This PR/Issue is related to cumulus. labels Jan 19, 2024
@skunert skunert requested review from a team January 19, 2024 14:08
@skunert skunert requested a review from ggwpez January 30, 2024 12:41
Comment on lines 61 to 62
previous_proof_weight: u64,
previous_reported_proof_size: Option<u64>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: is it on purpose that one of the variables called proof_weight, and another proof_size? I can see it as convention that weight refers to benchmarked "weight" and size refers to actually consumed proof size, but below in post_dispatch() there is a variable called consumed_weight.

Copy link
Contributor Author

@skunert skunert Feb 2, 2024

Choose a reason for hiding this comment

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

I tried to use proof_size as the absolute number that is reported by the host function. Weight as the amount consumed or reported by the runtime facilities.

But I should double check if that makes sense and usage is consistent.

Copy link
Contributor

@dmitry-markin dmitry-markin left a comment

Choose a reason for hiding this comment

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

Looks good to me, but it's a good idea for somebody else to have a closer look at validate_block and that the host function is registered everywhere where needed, as I lack the needed knowledge to check this.

cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
@skunert skunert requested a review from a team as a code owner February 2, 2024 13:19
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Copy link
Member

@davxy davxy left a comment

Choose a reason for hiding this comment

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

Mostly nitpicks

cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
);

run_with_externalities::<B, _, _>(&backend, || {
run_with_externalities_and_recorder::<B, _, _>(&backend, &mut recorder, || {
Copy link
Member

Choose a reason for hiding this comment

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

Dumb question: do we need access to the recorder in this closure as well?
Shouldn't we need access to the recorder only in the scope where we call execute_block only?
(i.e. in the call below)
Or we need it to check_inherents as well?

Another dumb question: why we need two calls to run_with_externalities_and_recorder?
Can't we unify the closures in a single one?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should not need it.

Copy link
Member

Choose a reason for hiding this comment

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

Let me phrase it better, we should NOT use the recorder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean we need to supply the recorder everywhere where host functions are potentially being called. And since the CheckInherent type is user supplied, host functions could be called there.

The two closures are separate because we want to start with a fresh overlay and externalities. But this is a good point, I now reset the recorder too between these two code sections.

cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
cumulus/parachains/common/src/storage_weight_reclaim.rs Outdated Show resolved Hide resolved
@@ -40,7 +40,10 @@ use substrate_prometheus_endpoint::Registry;
pub struct ParachainNativeExecutor;

impl sc_executor::NativeExecutionDispatch for ParachainNativeExecutor {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
type ExtendHostFunctions = (
cumulus_client_service::storage_proof_size::HostFunctions,
Copy link
Member

@davxy davxy Feb 3, 2024

Choose a reason for hiding this comment

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

If we propose this host function in the template. Maybe we should also need to add the StorageWeigthReclaim signed extension to the runtime signed extensions tuple? (i.e. here)

Otherwise what is its usage?

IIUC the StorageWeightReclaimer alone can't work correctly if we don't also return the extra weight to frame_system::BlockWeight ( by means of this signed extension hooks right?
Or I'm missing some other means of doing this? E.g. the user directly altering frame_system::BlockWeight in some other hook

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIUC the StorageWeightReclaimer alone can't work correctly if we don't also return the extra weight to frame_system::BlockWeight ( by means of this signed extension hooks right?
Or I'm missing some other means of doing this? E.g. the user directly altering frame_system::BlockWeight in some other hook.

The StorageWeightReclaimer is a separate mechanism from the SignedExtension. It is designed to be used inside the on_idle hook, where weight management happens manually via WeightMeter. Users need to check whether the operation they plan fits into the remaining weight. In the end, on_idle will return the consumed weight and it will be accounted for after the hook finished here:

let used_weight = <AllPalletsWithSystem as OnIdle<BlockNumberFor<System>>>::on_idle(
block_number,
remaining_weight,
);
<frame_system::Pallet<System>>::register_extra_weight_unchecked(
used_weight,
DispatchClass::Mandatory,
);

This means that the weightreclaimer should only modify the value in the weightmeter, not the storage value itself.

If we propose this host function in the template. Maybe we should also need to add the StorageWeigthReclaim signed extension to the runtime signed extensions tuple? (i.e. here)

Otherwise what is its usage?

This is a good point, I will add it.

@muharem muharem self-requested a review February 4, 2024 01:34
@michalkucharczyk
Copy link
Contributor

Code/concept looking good to me. Just a random thought: did you consider adding signed extensions to one of test runtimes (e.g. substrate-test-runtime or cumulus-test-runtime) to add some client-level testing involving more components?

@skunert skunert added this pull request to the merge queue Feb 23, 2024
Merged via the queue into paritytech:master with commit 3386377 Feb 23, 2024
128 of 131 checks passed
@skunert skunert deleted the reclaim-again branch February 23, 2024 14:40
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
# Runtime side for PoV Reclaim

## Implementation Overview
- Hostfunction to fetch the storage proof size has been added to the
PVF. It uses the size tracking recorder that was introduced in my
previous PR.
- Mechanisms to use the reclaim HostFunction have been introduced.
- 1. A SignedExtension that checks the node-reported proof size before
and after application of an extrinsic. Then it reclaims the difference.
- 2. A manual helper to make reclaiming easier when manual interaction
is required, for example in `on_idle` or other hooks.
- In order to utilize the manual reclaiming, I modified `WeightMeter` to
support the reduction of consumed weight, at least for storage proof
size.

## How to use
To enable the general functionality for a parachain:
1. Add the SignedExtension to your parachain runtime. 
2. Provide the HostFunction to the node
3. Enable proof recording during block import

## TODO
- [x] PRDoc

---------

Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Bastian Köcher <git@kchr.de>
enddynayn added a commit to frequency-chain/frequency that referenced this pull request Jul 26, 2024
- Upgrade Polkadot-sdk to v.1.8.0.
- Update weights to reflect the new version.

Notable Changes:
- [System Callabacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host function](paritytech/polkadot-sdk#3002)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
enddynayn added a commit to frequency-chain/frequency that referenced this pull request Jul 26, 2024
- Upgrade Polkadot-sdk to v.1.8.0.
- Update weights to reflect the new version.

Notable Changes:
- [System Callabacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host function](paritytech/polkadot-sdk#3002)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
enddynayn added a commit to frequency-chain/frequency that referenced this pull request Jul 26, 2024
- Upgrade Polkadot-sdk to v.1.8.0.
- Update weights to reflect the new version.

Notable Changes:
- [System Callabacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host function](paritytech/polkadot-sdk#3002)
- [Rename of storage version function](https://github.com/paritytech/polkadot-sdk/pull/1554/files#diff-01dc4f43df9baa537f30c6b369525715d596a3068944f38458e9f160d5412d58R306)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
enddynayn added a commit to frequency-chain/frequency that referenced this pull request Jul 26, 2024
- Upgrade Polkadot-sdk to v.1.9.0.
- Update weights to reflect the new version.

Notable Changes:
- [System Callabacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host function](paritytech/polkadot-sdk#3002)
- [Rename of storage version function](https://github.com/paritytech/polkadot-sdk/pull/1554/files#diff-01dc4f43df9baa537f30c6b369525715d596a3068944f38458e9f160d5412d58R306)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
enddynayn added a commit to frequency-chain/frequency that referenced this pull request Jul 26, 2024
- Upgrade Polkadot-sdk to v.1.9.0.
- Update weights to reflect the new version.

Notable Changes:
- [System
Callbacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet
pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host
function](paritytech/polkadot-sdk#3002)
- [Rename of storage version
function](https://github.com/paritytech/polkadot-sdk/pull/1554/files#diff-01dc4f43df9baa537f30c6b369525715d596a3068944f38458e9f160d5412d58R306)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
rustadot pushed a commit to rustadot/recurrency that referenced this pull request Sep 5, 2024
- Upgrade Polkadot-sdk to v.1.9.0.
- Update weights to reflect the new version.

Notable Changes:
- [System
Callbacks](paritytech/polkadot-sdk#1781)
- [Remove of pallet
pallet::getter](paritytech/polkadot-sdk#3456)
- [Add storage_proof_size host
function](paritytech/polkadot-sdk#3002)
- [Rename of storage version
function](https://github.com/paritytech/polkadot-sdk/pull/1554/files#diff-01dc4f43df9baa537f30c6b369525715d596a3068944f38458e9f160d5412d58R306)

For more details, please refer to:

[Release
Notes](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.9.0)
AurevoirXavier added a commit to darwinia-network/darwinia that referenced this pull request Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T0-node This PR/Issue is related to the topic “node”. T1-FRAME This PR/Issue is related to core FRAME, the framework. T5-host_functions This PR/Issue is related to host functions. T9-cumulus This PR/Issue is related to cumulus.
Projects
Status: done
Status: Audited
Development

Successfully merging this pull request may close these issues.

9 participants