Skip to content

Commit

Permalink
Implement simple SignedExtension for reclaiming
Browse files Browse the repository at this point in the history
  • Loading branch information
skunert committed Nov 10, 2023
1 parent be98bf2 commit a1c66e4
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! The actual implementation of the validate block functionality.
use super::{trie_cache, MemoryOptimizedValidationParams};
use super::{trie_cache, trie_recorder, MemoryOptimizedValidationParams};
use cumulus_primitives_core::{
relay_chain::Hash as RHash, ParachainBlockData, PersistedValidationData,
};
Expand All @@ -33,13 +33,15 @@ use sp_core::storage::{ChildInfo, StateVersion};
use sp_externalities::{set_and_run_with_externalities, Externalities};
use sp_io::KillStorageResult;
use sp_runtime::traits::{Block as BlockT, Extrinsic, HashingFor, Header as HeaderT};
use sp_std::prelude::*;
use sp_trie::MemoryDB;
use sp_std::{prelude::*, sync::Arc};
use sp_trie::{MemoryDB, ProofSizeProvider, TrieRecorderProvider};
use trie_recorder::SizeOnlyRecorderProvider;

type TrieBackend<B> = sp_state_machine::TrieBackend<
MemoryDB<HashingFor<B>>,
HashingFor<B>,
trie_cache::CacheProvider<HashingFor<B>>,
SizeOnlyRecorderProvider<HashingFor<B>>,
>;

type Ext<'a, B> = sp_state_machine::Ext<'a, HashingFor<B>, TrieBackend<B>>;
Expand All @@ -48,6 +50,9 @@ fn with_externalities<F: FnOnce(&mut dyn Externalities) -> R, R>(f: F) -> R {
sp_externalities::with_externalities(f).expect("Environmental externalities not set.")
}

/// Recorder instance to be used during this validate_block call.
environmental::environmental!(recorder: trait ProofSizeProvider);

/// Validate the given parachain block.
///
/// This function is doing roughly the following:
Expand Down Expand Up @@ -120,6 +125,7 @@ where

sp_std::mem::drop(storage_proof);

let mut recorder = SizeOnlyRecorderProvider::new();
let cache_provider = trie_cache::CacheProvider::new();
// We use the storage root of the `parent_head` to ensure that it is the correct root.
// This is already being done above while creating the in-memory db, but let's be paranoid!!
Expand All @@ -128,6 +134,7 @@ where
*parent_header.state_root(),
cache_provider,
)
.with_recorder(recorder.clone())
.build();

let _guard = (
Expand Down Expand Up @@ -167,9 +174,11 @@ where
.replace_implementation(host_default_child_storage_next_key),
sp_io::offchain_index::host_set.replace_implementation(host_offchain_index_set),
sp_io::offchain_index::host_clear.replace_implementation(host_offchain_index_clear),
cumulus_primitives_proof_size_hostfunction::storage_proof_size::host_storage_proof_size
.replace_implementation(host_storage_proof_size),
);

run_with_externalities::<B, _, _>(&backend, || {
run_with_externalities_and_recorder::<B, _, _>(&backend, &mut recorder, || {
let relay_chain_proof = crate::RelayChainStateProof::new(
PSC::SelfParaId::get(),
inherent_data.validation_data.relay_parent_storage_root,
Expand All @@ -190,7 +199,7 @@ where
}
});

run_with_externalities::<B, _, _>(&backend, || {
run_with_externalities_and_recorder::<B, _, _>(&backend, &mut recorder, || {
let head_data = HeadData(block.header().encode());

E::execute_block(block);
Expand Down Expand Up @@ -266,14 +275,15 @@ fn validate_validation_data(
}

/// Run the given closure with the externalities set.
fn run_with_externalities<B: BlockT, R, F: FnOnce() -> R>(
fn run_with_externalities_and_recorder<B: BlockT, R, F: FnOnce() -> R>(
backend: &TrieBackend<B>,
recorder: &mut SizeOnlyRecorderProvider<HashingFor<B>>,
execute: F,
) -> R {
let mut overlay = sp_state_machine::OverlayedChanges::default();
let mut ext = Ext::<B>::new(&mut overlay, backend);

set_and_run_with_externalities(&mut ext, || execute())
recorder::using(recorder, || set_and_run_with_externalities(&mut ext, || execute()))
}

fn host_storage_read(key: &[u8], value_out: &mut [u8], value_offset: u32) -> Option<u32> {
Expand Down Expand Up @@ -305,6 +315,10 @@ fn host_storage_clear(key: &[u8]) {
with_externalities(|ext| ext.place_storage(key.to_vec(), None))
}

fn host_storage_proof_size() -> u64 {
recorder::with(|rec| rec.estimate_encoded_size()).expect("Recorder is always set; qed") as _
}

fn host_storage_root(version: StateVersion) -> Vec<u8> {
with_externalities(|ext| ext.storage_root(version))
}
Expand Down
3 changes: 2 additions & 1 deletion cumulus/parachain-template/node/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::net::SocketAddr;

use cumulus_client_service::storage_proof_size::HostFunctions as ReclaimHostFunctions;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
use log::info;
Expand Down Expand Up @@ -183,7 +184,7 @@ pub fn run() -> Result<()> {
match cmd {
BenchmarkCmd::Pallet(cmd) =>
if cfg!(feature = "runtime-benchmarks") {
runner.sync_run(|config| cmd.run::<Block, ()>(config))
runner.sync_run(|config| cmd.run::<Block, ReclaimHostFunctions>(config))
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
Expand Down
5 changes: 4 additions & 1 deletion cumulus/parachain-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
frame_benchmarking::benchmarking::HostFunctions,
);

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
parachain_template_runtime::api::dispatch(method, data)
Expand Down
4 changes: 4 additions & 0 deletions cumulus/parachains/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ pallet-collator-selection = { path = "../../pallets/collator-selection", default
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
cumulus-primitives-utility = { path = "../../primitives/utility", default-features = false }
parachain-info = { package = "staging-parachain-info", path = "../pallets/parachain-info", default-features = false }
cumulus-primitives-proof-size-hostfunction = { path = "../../primitives/proof-size-hostfunction", default-features = false }

[dev-dependencies]
pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false}
sp-io = { path = "../../../substrate/primitives/io", default-features = false}
sp-trie = { path = "../../../substrate/primitives/trie", default-features = false }

[build-dependencies]
substrate-wasm-builder = { path = "../../../substrate/utils/wasm-builder" }
Expand All @@ -67,6 +69,7 @@ std = [
"pallet-balances/std",
"pallet-collator-selection/std",
"pallet-message-queue/std",
"cumulus-primitives-proof-size-hostfunction/std",
"parachain-info/std",
"polkadot-core-primitives/std",
"polkadot-primitives/std",
Expand All @@ -75,6 +78,7 @@ std = [
"sp-consensus-aura/std",
"sp-core/std",
"sp-io/std",
"sp-trie/std",
"sp-runtime/std",
"sp-std/std",
"westend-runtime-constants/std",
Expand Down
1 change: 1 addition & 0 deletions cumulus/parachains/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod kusama;
pub mod message_queue;
pub mod polkadot;
pub mod rococo;
pub mod storage_weight_reclaim_extension;
pub mod westend;
pub mod wococo;
pub mod xcm_config;
Expand Down
Loading

0 comments on commit a1c66e4

Please sign in to comment.