PoV Reclaim Runtime Side (#3002)

# 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>
This commit is contained in:
Sebastian Kunert
2024-02-23 15:09:49 +01:00
committed by GitHub
parent 5fc6d67be0
commit 3386377b0f
25 changed files with 875 additions and 29 deletions
+2 -1
View File
@@ -148,6 +148,7 @@ use sp_io::TestExternalities;
pub mod limits;
#[cfg(test)]
pub(crate) mod mock;
pub mod offchain;
mod extensions;
@@ -847,7 +848,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::whitelist_storage]
#[pallet::getter(fn block_weight)]
pub(super) type BlockWeight<T: Config> = StorageValue<_, ConsumedWeight, ValueQuery>;
pub type BlockWeight<T: Config> = StorageValue<_, ConsumedWeight, ValueQuery>;
/// Total length (in bytes) for all extrinsics put together, for the current block.
#[pallet::storage]
@@ -149,6 +149,11 @@ impl WeightMeter {
pub fn can_consume(&self, w: Weight) -> bool {
self.consumed.checked_add(&w).map_or(false, |t| t.all_lte(self.limit))
}
/// Reclaim the given weight.
pub fn reclaim_proof_size(&mut self, s: u64) {
self.consumed.saturating_reduce(Weight::from_parts(0, s));
}
}
#[cfg(test)]
@@ -277,6 +282,21 @@ mod tests {
assert_eq!(meter.consumed(), Weight::from_parts(5, 10));
}
#[test]
#[cfg(debug_assertions)]
fn reclaim_works() {
let mut meter = WeightMeter::with_limit(Weight::from_parts(5, 10));
meter.consume(Weight::from_parts(5, 10));
assert_eq!(meter.consumed(), Weight::from_parts(5, 10));
meter.reclaim_proof_size(3);
assert_eq!(meter.consumed(), Weight::from_parts(5, 7));
meter.reclaim_proof_size(10);
assert_eq!(meter.consumed(), Weight::from_parts(5, 0));
}
#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Weight counter overflow")]
+9
View File
@@ -72,6 +72,7 @@ pub struct TestClientBuilder<Block: BlockT, ExecutorDispatch, Backend: 'static,
fork_blocks: ForkBlocks<Block>,
bad_blocks: BadBlocks<Block>,
enable_offchain_indexing_api: bool,
enable_import_proof_recording: bool,
no_genesis: bool,
}
@@ -120,6 +121,7 @@ impl<Block: BlockT, ExecutorDispatch, Backend, G: GenesisInit>
bad_blocks: None,
enable_offchain_indexing_api: false,
no_genesis: false,
enable_import_proof_recording: false,
}
}
@@ -165,6 +167,12 @@ impl<Block: BlockT, ExecutorDispatch, Backend, G: GenesisInit>
self
}
/// Enable proof recording on import.
pub fn enable_import_proof_recording(mut self) -> Self {
self.enable_import_proof_recording = true;
self
}
/// Disable writing genesis.
pub fn set_no_genesis(mut self) -> Self {
self.no_genesis = true;
@@ -202,6 +210,7 @@ impl<Block: BlockT, ExecutorDispatch, Backend, G: GenesisInit>
};
let client_config = ClientConfig {
enable_import_proof_recording: self.enable_import_proof_recording,
offchain_indexing_api: self.enable_offchain_indexing_api,
no_genesis: self.no_genesis,
..Default::default()