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
+1
View File
@@ -41,6 +41,7 @@ cumulus-test-relay-sproof-builder = { path = "../relay-sproof-builder" }
cumulus-primitives-core = { path = "../../primitives/core" }
cumulus-primitives-proof-size-hostfunction = { path = "../../primitives/proof-size-hostfunction" }
cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" }
cumulus-primitives-storage-weight-reclaim = { path = "../../primitives/storage-weight-reclaim" }
[features]
runtime-benchmarks = [
+12 -8
View File
@@ -151,6 +151,7 @@ pub fn generate_extrinsic_with_pair(
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim::<Runtime>::new(),
);
let function = function.into();
@@ -158,7 +159,7 @@ pub fn generate_extrinsic_with_pair(
let raw_payload = SignedPayload::from_raw(
function.clone(),
extra.clone(),
((), VERSION.spec_version, genesis_block, current_block_hash, (), (), ()),
((), VERSION.spec_version, genesis_block, current_block_hash, (), (), (), ()),
);
let signature = raw_payload.using_encoded(|e| origin.sign(e));
@@ -203,13 +204,16 @@ pub fn validate_block(
let mut ext_ext = ext.ext();
let heap_pages = HeapAllocStrategy::Static { extra_pages: 1024 };
let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder()
.with_execution_method(WasmExecutionMethod::default())
.with_max_runtime_instances(1)
.with_runtime_cache_size(2)
.with_onchain_heap_alloc_strategy(heap_pages)
.with_offchain_heap_alloc_strategy(heap_pages)
.build();
let executor = WasmExecutor::<(
sp_io::SubstrateHostFunctions,
cumulus_primitives_proof_size_hostfunction::storage_proof_size::HostFunctions,
)>::builder()
.with_execution_method(WasmExecutionMethod::default())
.with_max_runtime_instances(1)
.with_runtime_cache_size(2)
.with_onchain_heap_alloc_strategy(heap_pages)
.with_offchain_heap_alloc_strategy(heap_pages)
.build();
executor
.uncached_call(
+2
View File
@@ -39,6 +39,7 @@ sp-version = { path = "../../../substrate/primitives/version", default-features
# Cumulus
cumulus-pallet-parachain-system = { path = "../../pallets/parachain-system", default-features = false, features = ["parameterized-consensus-hook"] }
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
cumulus-primitives-storage-weight-reclaim = { path = "../../primitives/storage-weight-reclaim", default-features = false }
[build-dependencies]
substrate-wasm-builder = { path = "../../../substrate/utils/wasm-builder", optional = true }
@@ -49,6 +50,7 @@ std = [
"codec/std",
"cumulus-pallet-parachain-system/std",
"cumulus-primitives-core/std",
"cumulus-primitives-storage-weight-reclaim/std",
"frame-executive/std",
"frame-support/std",
"frame-system-rpc-runtime-api/std",
+1
View File
@@ -331,6 +331,7 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
+1
View File
@@ -81,6 +81,7 @@ cumulus-relay-chain-minimal-node = { path = "../../client/relay-chain-minimal-no
cumulus-client-pov-recovery = { path = "../../client/pov-recovery" }
cumulus-test-relay-sproof-builder = { path = "../relay-sproof-builder" }
cumulus-pallet-parachain-system = { path = "../../pallets/parachain-system", default-features = false, features = ["parameterized-consensus-hook"] }
cumulus-primitives-storage-weight-reclaim = { path = "../../primitives/storage-weight-reclaim" }
pallet-timestamp = { path = "../../../substrate/frame/timestamp" }
[dev-dependencies]
+3 -2
View File
@@ -112,7 +112,7 @@ pub type AnnounceBlockFn = Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>;
pub struct RuntimeExecutor;
impl sc_executor::NativeExecutionDispatch for RuntimeExecutor {
type ExtendHostFunctions = ();
type ExtendHostFunctions = cumulus_client_service::storage_proof_size::HostFunctions;
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
cumulus_test_runtime::api::dispatch(method, data)
@@ -894,11 +894,12 @@ pub fn construct_extrinsic(
frame_system::CheckNonce::<runtime::Runtime>::from(nonce),
frame_system::CheckWeight::<runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(tip),
cumulus_primitives_storage_weight_reclaim::StorageWeightReclaim::<runtime::Runtime>::new(),
);
let raw_payload = runtime::SignedPayload::from_raw(
function.clone(),
extra.clone(),
((), runtime::VERSION.spec_version, genesis_block, current_block_hash, (), (), ()),
((), runtime::VERSION.spec_version, genesis_block, current_block_hash, (), (), (), ()),
);
let signature = raw_payload.using_encoded(|e| caller.sign(e));
runtime::UncheckedExtrinsic::new_signed(