Skip to content

Commit

Permalink
Merge pull request #836 from opentensor/sam-update-procedural-fork-1.16
Browse files Browse the repository at this point in the history
update procedural fork to 1.16
  • Loading branch information
unconst authored Oct 11, 2024
2 parents 1b177dd + ac6e87d commit 76eee08
Show file tree
Hide file tree
Showing 38 changed files with 680 additions and 471 deletions.
103 changes: 54 additions & 49 deletions support/procedural-fork/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ fn ensure_valid_return_type(item_fn: &ItemFn) -> Result<()> {
Ok(())
}

/// Ensure that the passed statements do not contain any forbidden variable names
fn ensure_no_forbidden_variable_names(stmts: &[Stmt]) -> Result<()> {
const FORBIDDEN_VAR_NAMES: [&str; 2] = ["recording", "verify"];
for stmt in stmts {
let Stmt::Local(l) = stmt else { continue };
let Pat::Ident(ident) = &l.pat else { continue };
if FORBIDDEN_VAR_NAMES.contains(&ident.ident.to_string().as_str()) {
return Err(Error::new(
ident.span(),
format!(
"Variables {FORBIDDEN_VAR_NAMES:?} are reserved for benchmarking internals.",
),
));
}
}
Ok(())
}

/// Parses params such as `x: Linear<0, 1>`
fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
let mut params: Vec<ParamDef> = Vec::new();
Expand Down Expand Up @@ -481,9 +499,12 @@ impl BenchmarkDef {
}
};

let setup_stmts = Vec::from(&item_fn.block.stmts[0..i]);
ensure_no_forbidden_variable_names(&setup_stmts)?;

Ok(BenchmarkDef {
params,
setup_stmts: Vec::from(&item_fn.block.stmts[0..i]),
setup_stmts,
call_def,
verify_stmts,
last_stmt,
Expand Down Expand Up @@ -692,18 +713,16 @@ pub fn benchmarks(

fn instance(
&self,
recording: &mut impl #krate::Recording,
components: &[(#krate::BenchmarkParameter, u32)],
verify: bool,
) -> Result<
#krate::__private::Box<dyn FnOnce() -> Result<(), #krate::BenchmarkError>>,
#krate::BenchmarkError,
> {
) -> Result<(), #krate::BenchmarkError> {
match self {
#(
Self::#benchmark_names => {
<#benchmark_names as #krate::BenchmarkingSetup<
#type_use_generics
>>::instance(&#benchmark_names, components, verify)
>>::instance(&#benchmark_names, recording, components, verify)
}
)
*
Expand Down Expand Up @@ -794,17 +813,7 @@ pub fn benchmarks(
#krate::benchmarking::set_whitelist(whitelist.clone());
let mut results: #krate::__private::Vec<#krate::BenchmarkResult> = #krate::__private::Vec::new();

// Always do at least one internal repeat...
for _ in 0 .. internal_repeats.max(1) {
// Always reset the state after the benchmark.
#krate::__private::defer!(#krate::benchmarking::wipe_db());

// Set up the externalities environment for the setup we want to
// benchmark.
let closure_to_benchmark = <
SelectedBenchmark as #krate::BenchmarkingSetup<#type_use_generics>
>::instance(&selected_benchmark, c, verify)?;

let on_before_start = || {
// Set the block number to at least 1 so events are deposited.
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
#frame_system::Pallet::<T>::set_block_number(1u32.into());
Expand All @@ -822,6 +831,12 @@ pub fn benchmarks(

// Reset the read/write counter so we don't count operations in the setup process.
#krate::benchmarking::reset_read_write_count();
};

// Always do at least one internal repeat...
for _ in 0 .. internal_repeats.max(1) {
// Always reset the state after the benchmark.
#krate::__private::defer!(#krate::benchmarking::wipe_db());

// Time the extrinsic logic.
#krate::__private::log::trace!(
Expand All @@ -831,20 +846,12 @@ pub fn benchmarks(
c
);

let start_pov = #krate::benchmarking::proof_size();
let start_extrinsic = #krate::benchmarking::current_time();

closure_to_benchmark()?;

let finish_extrinsic = #krate::benchmarking::current_time();
let end_pov = #krate::benchmarking::proof_size();
let mut recording = #krate::BenchmarkRecording::new(&on_before_start);
<SelectedBenchmark as #krate::BenchmarkingSetup<#type_use_generics>>::instance(&selected_benchmark, &mut recording, c, verify)?;

// Calculate the diff caused by the benchmark.
let elapsed_extrinsic = finish_extrinsic.saturating_sub(start_extrinsic);
let diff_pov = match (start_pov, end_pov) {
(Some(start), Some(end)) => end.saturating_sub(start),
_ => Default::default(),
};
let elapsed_extrinsic = recording.elapsed_extrinsic().expect("elapsed time should be recorded");
let diff_pov = recording.diff_pov().unwrap_or_default();

// Commit the changes to get proper write count
#krate::benchmarking::commit_db();
Expand Down Expand Up @@ -1163,9 +1170,10 @@ fn expand_benchmark(

fn instance(
&self,
recording: &mut impl #krate::Recording,
components: &[(#krate::BenchmarkParameter, u32)],
verify: bool
) -> Result<#krate::__private::Box<dyn FnOnce() -> Result<(), #krate::BenchmarkError>>, #krate::BenchmarkError> {
) -> Result<(), #krate::BenchmarkError> {
#(
// prepare instance #param_names
let #param_names = components.iter()
Expand All @@ -1179,15 +1187,15 @@ fn expand_benchmark(
#setup_stmts
)*
#pre_call
Ok(#krate::__private::Box::new(move || -> Result<(), #krate::BenchmarkError> {
#post_call
if verify {
#(
#verify_stmts
)*
}
#impl_last_stmt
}))
recording.start();
#post_call
recording.stop();
if verify {
#(
#verify_stmts
)*
}
#impl_last_stmt
}
}

Expand All @@ -1205,18 +1213,15 @@ fn expand_benchmark(
// Always reset the state after the benchmark.
#krate::__private::defer!(#krate::benchmarking::wipe_db());

// Set up the benchmark, return execution + verification function.
let closure_to_verify = <
SelectedBenchmark as #krate::BenchmarkingSetup<T, _>
>::instance(&selected_benchmark, &c, true)?;

// Set the block number to at least 1 so events are deposited.
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
#frame_system::Pallet::<T>::set_block_number(1u32.into());
}
let on_before_start = || {
// Set the block number to at least 1 so events are deposited.
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
#frame_system::Pallet::<T>::set_block_number(1u32.into());
}
};

// Run execution + verification
closure_to_verify()
<SelectedBenchmark as #krate::BenchmarkingSetup<T, _>>::test_instance(&selected_benchmark, &c, &on_before_start)
};

if components.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn expand_outer_dispatch(
quote! {
#( #query_call_part_macros )*

/// The aggregated runtime call type.
#[derive(
Clone, PartialEq, Eq,
#scrate::__private::codec::Encode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ pub fn expand_outer_inherent(

trait InherentDataExt {
fn create_extrinsics(&self) ->
#scrate::__private::sp_std::vec::Vec<<#block as #scrate::sp_runtime::traits::Block>::Extrinsic>;
#scrate::__private::Vec<<#block as #scrate::sp_runtime::traits::Block>::Extrinsic>;
fn check_extrinsics(&self, block: &#block) -> #scrate::inherent::CheckInherentsResult;
}

impl InherentDataExt for #scrate::inherent::InherentData {
fn create_extrinsics(&self) ->
#scrate::__private::sp_std::vec::Vec<<#block as #scrate::sp_runtime::traits::Block>::Extrinsic>
#scrate::__private::Vec<<#block as #scrate::sp_runtime::traits::Block>::Extrinsic>
{
use #scrate::inherent::ProvideInherent;

let mut inherents = #scrate::__private::sp_std::vec::Vec::new();
let mut inherents = #scrate::__private::Vec::new();

#(
#pallet_attrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn expand_runtime_metadata(
>();

#scrate::__private::metadata_ir::MetadataIR {
pallets: #scrate::__private::sp_std::vec![ #(#pallets),* ],
pallets: #scrate::__private::vec![ #(#pallets),* ],
extrinsic: #scrate::__private::metadata_ir::ExtrinsicMetadataIR {
ty,
version: <#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata>::VERSION,
Expand Down Expand Up @@ -159,7 +159,7 @@ pub fn expand_runtime_metadata(
})
}

pub fn metadata_versions() -> #scrate::__private::sp_std::vec::Vec<u32> {
pub fn metadata_versions() -> #scrate::__private::Vec<u32> {
#scrate::__private::metadata_ir::supported_versions()
}
}
Expand Down
34 changes: 17 additions & 17 deletions support/procedural-fork/src/construct_runtime/expand/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,25 @@ pub fn expand_outer_origin(
#[derive(Clone)]
pub struct RuntimeOrigin {
pub caller: OriginCaller,
filter: #scrate::__private::sp_std::rc::Rc<Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
filter: #scrate::__private::Rc<#scrate::__private::Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
}

#[cfg(not(feature = "std"))]
impl #scrate::__private::sp_std::fmt::Debug for RuntimeOrigin {
impl core::fmt::Debug for RuntimeOrigin {
fn fmt(
&self,
fmt: &mut #scrate::__private::sp_std::fmt::Formatter,
) -> #scrate::__private::sp_std::result::Result<(), #scrate::__private::sp_std::fmt::Error> {
fmt: &mut core::fmt::Formatter,
) -> core::result::Result<(), core::fmt::Error> {
fmt.write_str("<wasm:stripped>")
}
}

#[cfg(feature = "std")]
impl #scrate::__private::sp_std::fmt::Debug for RuntimeOrigin {
impl core::fmt::Debug for RuntimeOrigin {
fn fmt(
&self,
fmt: &mut #scrate::__private::sp_std::fmt::Formatter,
) -> #scrate::__private::sp_std::result::Result<(), #scrate::__private::sp_std::fmt::Error> {
fmt: &mut core::fmt::Formatter,
) -> core::result::Result<(), core::fmt::Error> {
fmt.debug_struct("Origin")
.field("caller", &self.caller)
.field("filter", &"[function ptr]")
Expand All @@ -144,7 +144,7 @@ pub fn expand_outer_origin(
fn add_filter(&mut self, filter: impl Fn(&Self::Call) -> bool + 'static) {
let f = self.filter.clone();

self.filter = #scrate::__private::sp_std::rc::Rc::new(Box::new(move |call| {
self.filter = #scrate::__private::Rc::new(#scrate::__private::Box::new(move |call| {
f(call) && filter(call)
}));
}
Expand All @@ -155,7 +155,7 @@ pub fn expand_outer_origin(
as #scrate::traits::Contains<<#runtime as #system_path::Config>::RuntimeCall>
>::contains;

self.filter = #scrate::__private::sp_std::rc::Rc::new(Box::new(filter));
self.filter = #scrate::__private::Rc::new(#scrate::__private::Box::new(filter));
}

fn set_caller_from(&mut self, other: impl Into<Self>) {
Expand Down Expand Up @@ -257,7 +257,7 @@ pub fn expand_outer_origin(
impl TryFrom<OriginCaller> for #system_path::Origin<#runtime> {
type Error = OriginCaller;
fn try_from(x: OriginCaller)
-> #scrate::__private::sp_std::result::Result<#system_path::Origin<#runtime>, OriginCaller>
-> core::result::Result<#system_path::Origin<#runtime>, OriginCaller>
{
if let OriginCaller::system(l) = x {
Ok(l)
Expand All @@ -280,7 +280,7 @@ pub fn expand_outer_origin(
fn from(x: OriginCaller) -> Self {
let mut o = RuntimeOrigin {
caller: x,
filter: #scrate::__private::sp_std::rc::Rc::new(Box::new(|_| true)),
filter: #scrate::__private::Rc::new(#scrate::__private::Box::new(|_| true)),
};

#scrate::traits::OriginTrait::reset_filter(&mut o);
Expand All @@ -289,7 +289,7 @@ pub fn expand_outer_origin(
}
}

impl From<RuntimeOrigin> for #scrate::__private::sp_std::result::Result<#system_path::Origin<#runtime>, RuntimeOrigin> {
impl From<RuntimeOrigin> for core::result::Result<#system_path::Origin<#runtime>, RuntimeOrigin> {
/// NOTE: converting to pallet origin loses the origin filter information.
fn from(val: RuntimeOrigin) -> Self {
if let OriginCaller::system(l) = val.caller {
Expand Down Expand Up @@ -357,7 +357,7 @@ fn expand_origin_caller_variant(
}

fn expand_origin_pallet_conversions(
scrate: &TokenStream,
_scrate: &TokenStream,
runtime: &Ident,
pallet: &Pallet,
instance: Option<&Ident>,
Expand Down Expand Up @@ -405,7 +405,7 @@ fn expand_origin_pallet_conversions(
}

#attr
impl From<RuntimeOrigin> for #scrate::__private::sp_std::result::Result<#pallet_origin, RuntimeOrigin> {
impl From<RuntimeOrigin> for core::result::Result<#pallet_origin, RuntimeOrigin> {
/// NOTE: converting to pallet origin loses the origin filter information.
fn from(val: RuntimeOrigin) -> Self {
if let OriginCaller::#variant_name(l) = val.caller {
Expand All @@ -421,7 +421,7 @@ fn expand_origin_pallet_conversions(
type Error = OriginCaller;
fn try_from(
x: OriginCaller,
) -> #scrate::__private::sp_std::result::Result<#pallet_origin, OriginCaller> {
) -> core::result::Result<#pallet_origin, OriginCaller> {
if let OriginCaller::#variant_name(l) = x {
Ok(l)
} else {
Expand All @@ -435,7 +435,7 @@ fn expand_origin_pallet_conversions(
type Error = ();
fn try_from(
x: &'a OriginCaller,
) -> #scrate::__private::sp_std::result::Result<&'a #pallet_origin, ()> {
) -> core::result::Result<&'a #pallet_origin, ()> {
if let OriginCaller::#variant_name(l) = x {
Ok(&l)
} else {
Expand All @@ -449,7 +449,7 @@ fn expand_origin_pallet_conversions(
type Error = ();
fn try_from(
x: &'a RuntimeOrigin,
) -> #scrate::__private::sp_std::result::Result<&'a #pallet_origin, ()> {
) -> core::result::Result<&'a #pallet_origin, ()> {
if let OriginCaller::#variant_name(l) = &x.caller {
Ok(&l)
} else {
Expand Down
Loading

0 comments on commit 76eee08

Please sign in to comment.