Skip to content

Commit 76eee08

Browse files
authored
Merge pull request #836 from opentensor/sam-update-procedural-fork-1.16
update procedural fork to 1.16
2 parents 1b177dd + ac6e87d commit 76eee08

38 files changed

+680
-471
lines changed

support/procedural-fork/src/benchmark.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,24 @@ fn ensure_valid_return_type(item_fn: &ItemFn) -> Result<()> {
323323
Ok(())
324324
}
325325

326+
/// Ensure that the passed statements do not contain any forbidden variable names
327+
fn ensure_no_forbidden_variable_names(stmts: &[Stmt]) -> Result<()> {
328+
const FORBIDDEN_VAR_NAMES: [&str; 2] = ["recording", "verify"];
329+
for stmt in stmts {
330+
let Stmt::Local(l) = stmt else { continue };
331+
let Pat::Ident(ident) = &l.pat else { continue };
332+
if FORBIDDEN_VAR_NAMES.contains(&ident.ident.to_string().as_str()) {
333+
return Err(Error::new(
334+
ident.span(),
335+
format!(
336+
"Variables {FORBIDDEN_VAR_NAMES:?} are reserved for benchmarking internals.",
337+
),
338+
));
339+
}
340+
}
341+
Ok(())
342+
}
343+
326344
/// Parses params such as `x: Linear<0, 1>`
327345
fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
328346
let mut params: Vec<ParamDef> = Vec::new();
@@ -481,9 +499,12 @@ impl BenchmarkDef {
481499
}
482500
};
483501

502+
let setup_stmts = Vec::from(&item_fn.block.stmts[0..i]);
503+
ensure_no_forbidden_variable_names(&setup_stmts)?;
504+
484505
Ok(BenchmarkDef {
485506
params,
486-
setup_stmts: Vec::from(&item_fn.block.stmts[0..i]),
507+
setup_stmts,
487508
call_def,
488509
verify_stmts,
489510
last_stmt,
@@ -692,18 +713,16 @@ pub fn benchmarks(
692713

693714
fn instance(
694715
&self,
716+
recording: &mut impl #krate::Recording,
695717
components: &[(#krate::BenchmarkParameter, u32)],
696718
verify: bool,
697-
) -> Result<
698-
#krate::__private::Box<dyn FnOnce() -> Result<(), #krate::BenchmarkError>>,
699-
#krate::BenchmarkError,
700-
> {
719+
) -> Result<(), #krate::BenchmarkError> {
701720
match self {
702721
#(
703722
Self::#benchmark_names => {
704723
<#benchmark_names as #krate::BenchmarkingSetup<
705724
#type_use_generics
706-
>>::instance(&#benchmark_names, components, verify)
725+
>>::instance(&#benchmark_names, recording, components, verify)
707726
}
708727
)
709728
*
@@ -794,17 +813,7 @@ pub fn benchmarks(
794813
#krate::benchmarking::set_whitelist(whitelist.clone());
795814
let mut results: #krate::__private::Vec<#krate::BenchmarkResult> = #krate::__private::Vec::new();
796815

797-
// Always do at least one internal repeat...
798-
for _ in 0 .. internal_repeats.max(1) {
799-
// Always reset the state after the benchmark.
800-
#krate::__private::defer!(#krate::benchmarking::wipe_db());
801-
802-
// Set up the externalities environment for the setup we want to
803-
// benchmark.
804-
let closure_to_benchmark = <
805-
SelectedBenchmark as #krate::BenchmarkingSetup<#type_use_generics>
806-
>::instance(&selected_benchmark, c, verify)?;
807-
816+
let on_before_start = || {
808817
// Set the block number to at least 1 so events are deposited.
809818
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
810819
#frame_system::Pallet::<T>::set_block_number(1u32.into());
@@ -822,6 +831,12 @@ pub fn benchmarks(
822831

823832
// Reset the read/write counter so we don't count operations in the setup process.
824833
#krate::benchmarking::reset_read_write_count();
834+
};
835+
836+
// Always do at least one internal repeat...
837+
for _ in 0 .. internal_repeats.max(1) {
838+
// Always reset the state after the benchmark.
839+
#krate::__private::defer!(#krate::benchmarking::wipe_db());
825840

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

834-
let start_pov = #krate::benchmarking::proof_size();
835-
let start_extrinsic = #krate::benchmarking::current_time();
836-
837-
closure_to_benchmark()?;
838-
839-
let finish_extrinsic = #krate::benchmarking::current_time();
840-
let end_pov = #krate::benchmarking::proof_size();
849+
let mut recording = #krate::BenchmarkRecording::new(&on_before_start);
850+
<SelectedBenchmark as #krate::BenchmarkingSetup<#type_use_generics>>::instance(&selected_benchmark, &mut recording, c, verify)?;
841851

842852
// Calculate the diff caused by the benchmark.
843-
let elapsed_extrinsic = finish_extrinsic.saturating_sub(start_extrinsic);
844-
let diff_pov = match (start_pov, end_pov) {
845-
(Some(start), Some(end)) => end.saturating_sub(start),
846-
_ => Default::default(),
847-
};
853+
let elapsed_extrinsic = recording.elapsed_extrinsic().expect("elapsed time should be recorded");
854+
let diff_pov = recording.diff_pov().unwrap_or_default();
848855

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

11641171
fn instance(
11651172
&self,
1173+
recording: &mut impl #krate::Recording,
11661174
components: &[(#krate::BenchmarkParameter, u32)],
11671175
verify: bool
1168-
) -> Result<#krate::__private::Box<dyn FnOnce() -> Result<(), #krate::BenchmarkError>>, #krate::BenchmarkError> {
1176+
) -> Result<(), #krate::BenchmarkError> {
11691177
#(
11701178
// prepare instance #param_names
11711179
let #param_names = components.iter()
@@ -1179,15 +1187,15 @@ fn expand_benchmark(
11791187
#setup_stmts
11801188
)*
11811189
#pre_call
1182-
Ok(#krate::__private::Box::new(move || -> Result<(), #krate::BenchmarkError> {
1183-
#post_call
1184-
if verify {
1185-
#(
1186-
#verify_stmts
1187-
)*
1188-
}
1189-
#impl_last_stmt
1190-
}))
1190+
recording.start();
1191+
#post_call
1192+
recording.stop();
1193+
if verify {
1194+
#(
1195+
#verify_stmts
1196+
)*
1197+
}
1198+
#impl_last_stmt
11911199
}
11921200
}
11931201

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

1208-
// Set up the benchmark, return execution + verification function.
1209-
let closure_to_verify = <
1210-
SelectedBenchmark as #krate::BenchmarkingSetup<T, _>
1211-
>::instance(&selected_benchmark, &c, true)?;
1212-
1213-
// Set the block number to at least 1 so events are deposited.
1214-
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
1215-
#frame_system::Pallet::<T>::set_block_number(1u32.into());
1216-
}
1216+
let on_before_start = || {
1217+
// Set the block number to at least 1 so events are deposited.
1218+
if #krate::__private::Zero::is_zero(&#frame_system::Pallet::<T>::block_number()) {
1219+
#frame_system::Pallet::<T>::set_block_number(1u32.into());
1220+
}
1221+
};
12171222

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

12221227
if components.is_empty() {

support/procedural-fork/src/construct_runtime/expand/call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub fn expand_outer_dispatch(
6969
quote! {
7070
#( #query_call_part_macros )*
7171

72+
/// The aggregated runtime call type.
7273
#[derive(
7374
Clone, PartialEq, Eq,
7475
#scrate::__private::codec::Encode,

support/procedural-fork/src/construct_runtime/expand/inherent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ pub fn expand_outer_inherent(
6161

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

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

74-
let mut inherents = #scrate::__private::sp_std::vec::Vec::new();
74+
let mut inherents = #scrate::__private::Vec::new();
7575

7676
#(
7777
#pallet_attrs

support/procedural-fork/src/construct_runtime/expand/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub fn expand_runtime_metadata(
114114
>();
115115

116116
#scrate::__private::metadata_ir::MetadataIR {
117-
pallets: #scrate::__private::sp_std::vec![ #(#pallets),* ],
117+
pallets: #scrate::__private::vec![ #(#pallets),* ],
118118
extrinsic: #scrate::__private::metadata_ir::ExtrinsicMetadataIR {
119119
ty,
120120
version: <#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata>::VERSION,
@@ -159,7 +159,7 @@ pub fn expand_runtime_metadata(
159159
})
160160
}
161161

162-
pub fn metadata_versions() -> #scrate::__private::sp_std::vec::Vec<u32> {
162+
pub fn metadata_versions() -> #scrate::__private::Vec<u32> {
163163
#scrate::__private::metadata_ir::supported_versions()
164164
}
165165
}

support/procedural-fork/src/construct_runtime/expand/origin.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,25 @@ pub fn expand_outer_origin(
110110
#[derive(Clone)]
111111
pub struct RuntimeOrigin {
112112
pub caller: OriginCaller,
113-
filter: #scrate::__private::sp_std::rc::Rc<Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
113+
filter: #scrate::__private::Rc<#scrate::__private::Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
114114
}
115115

116116
#[cfg(not(feature = "std"))]
117-
impl #scrate::__private::sp_std::fmt::Debug for RuntimeOrigin {
117+
impl core::fmt::Debug for RuntimeOrigin {
118118
fn fmt(
119119
&self,
120-
fmt: &mut #scrate::__private::sp_std::fmt::Formatter,
121-
) -> #scrate::__private::sp_std::result::Result<(), #scrate::__private::sp_std::fmt::Error> {
120+
fmt: &mut core::fmt::Formatter,
121+
) -> core::result::Result<(), core::fmt::Error> {
122122
fmt.write_str("<wasm:stripped>")
123123
}
124124
}
125125

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

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

158-
self.filter = #scrate::__private::sp_std::rc::Rc::new(Box::new(filter));
158+
self.filter = #scrate::__private::Rc::new(#scrate::__private::Box::new(filter));
159159
}
160160

161161
fn set_caller_from(&mut self, other: impl Into<Self>) {
@@ -257,7 +257,7 @@ pub fn expand_outer_origin(
257257
impl TryFrom<OriginCaller> for #system_path::Origin<#runtime> {
258258
type Error = OriginCaller;
259259
fn try_from(x: OriginCaller)
260-
-> #scrate::__private::sp_std::result::Result<#system_path::Origin<#runtime>, OriginCaller>
260+
-> core::result::Result<#system_path::Origin<#runtime>, OriginCaller>
261261
{
262262
if let OriginCaller::system(l) = x {
263263
Ok(l)
@@ -280,7 +280,7 @@ pub fn expand_outer_origin(
280280
fn from(x: OriginCaller) -> Self {
281281
let mut o = RuntimeOrigin {
282282
caller: x,
283-
filter: #scrate::__private::sp_std::rc::Rc::new(Box::new(|_| true)),
283+
filter: #scrate::__private::Rc::new(#scrate::__private::Box::new(|_| true)),
284284
};
285285

286286
#scrate::traits::OriginTrait::reset_filter(&mut o);
@@ -289,7 +289,7 @@ pub fn expand_outer_origin(
289289
}
290290
}
291291

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

359359
fn expand_origin_pallet_conversions(
360-
scrate: &TokenStream,
360+
_scrate: &TokenStream,
361361
runtime: &Ident,
362362
pallet: &Pallet,
363363
instance: Option<&Ident>,
@@ -405,7 +405,7 @@ fn expand_origin_pallet_conversions(
405405
}
406406

407407
#attr
408-
impl From<RuntimeOrigin> for #scrate::__private::sp_std::result::Result<#pallet_origin, RuntimeOrigin> {
408+
impl From<RuntimeOrigin> for core::result::Result<#pallet_origin, RuntimeOrigin> {
409409
/// NOTE: converting to pallet origin loses the origin filter information.
410410
fn from(val: RuntimeOrigin) -> Self {
411411
if let OriginCaller::#variant_name(l) = val.caller {
@@ -421,7 +421,7 @@ fn expand_origin_pallet_conversions(
421421
type Error = OriginCaller;
422422
fn try_from(
423423
x: OriginCaller,
424-
) -> #scrate::__private::sp_std::result::Result<#pallet_origin, OriginCaller> {
424+
) -> core::result::Result<#pallet_origin, OriginCaller> {
425425
if let OriginCaller::#variant_name(l) = x {
426426
Ok(l)
427427
} else {
@@ -435,7 +435,7 @@ fn expand_origin_pallet_conversions(
435435
type Error = ();
436436
fn try_from(
437437
x: &'a OriginCaller,
438-
) -> #scrate::__private::sp_std::result::Result<&'a #pallet_origin, ()> {
438+
) -> core::result::Result<&'a #pallet_origin, ()> {
439439
if let OriginCaller::#variant_name(l) = x {
440440
Ok(&l)
441441
} else {
@@ -449,7 +449,7 @@ fn expand_origin_pallet_conversions(
449449
type Error = ();
450450
fn try_from(
451451
x: &'a RuntimeOrigin,
452-
) -> #scrate::__private::sp_std::result::Result<&'a #pallet_origin, ()> {
452+
) -> core::result::Result<&'a #pallet_origin, ()> {
453453
if let OriginCaller::#variant_name(l) = &x.caller {
454454
Ok(&l)
455455
} else {

0 commit comments

Comments
 (0)