Skip to content

Commit

Permalink
Merge branch 'devnet-ready' into sam-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
keithtensor authored Oct 15, 2024
2 parents bdeb29e + 76eee08 commit 38d3b21
Show file tree
Hide file tree
Showing 41 changed files with 758 additions and 468 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn main() {
};

track_lint(DisallowV1Benchmarks::lint(&parsed_file));
track_lint(ForbidAsPrimitiveConversion::lint(&parsed_file));
track_lint(RequireFreezeStruct::lint(&parsed_file));
track_lint(RequireExplicitPalletIndex::lint(&parsed_file));
});
Expand Down
18 changes: 9 additions & 9 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/bin/bash
set -ex
cd support/macros
cargo publish --token $1
cargo publish $1
cd ../..
cd pallets/commitments
cargo publish --token $1
cargo publish $1
cd ..
cd collective
cargo publish --token $1
cargo publish $1
cd ..
cd registry
cargo publish --token $1
cargo publish $1
cd ..
cd subtensor
cargo publish --token $1
cargo publish $1
cd runtime-api
cargo publish --token $1
cargo publish $1
cd ../..
cd admin-utils
cargo publish --token $1
cargo publish $1
cd ../..
cd runtime
cargo publish --token $1
cargo publish $1
cd ..
cd node
cargo publish --token $1
cargo publish $1
echo "published successfully."
78 changes: 78 additions & 0 deletions support/linting/src/forbid_as_primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use super::*;
use syn::{visit::Visit, ExprMethodCall, File, Ident};

pub struct ForbidAsPrimitiveConversion;

impl Lint for ForbidAsPrimitiveConversion {
fn lint(source: &File) -> Result {
let mut visitor = AsPrimitiveVisitor::default();

visitor.visit_file(source);

if !visitor.errors.is_empty() {
return Err(visitor.errors);
}

Ok(())
}
}

#[derive(Default)]
struct AsPrimitiveVisitor {
errors: Vec<syn::Error>,
}

impl<'ast> Visit<'ast> for AsPrimitiveVisitor {
fn visit_expr_method_call(&mut self, node: &'ast ExprMethodCall) {
if is_as_primitive(&node.method) {
self.errors.push(syn::Error::new(
node.method.span(),
"Using 'as_*()' methods is banned to avoid accidental panics. Use `try_into()` instead.",
));
}

syn::visit::visit_expr_method_call(self, node);
}
}

fn is_as_primitive(ident: &Ident) -> bool {
matches!(
ident.to_string().as_str(),
"as_u32" | "as_u64" | "as_u128" | "as_usize"
)
}

#[cfg(test)]
mod tests {
use super::*;

fn lint(input: &str) -> Result {
let expr: ExprMethodCall = syn::parse_str(input).expect("should only use on a method call");
let mut visitor = AsPrimitiveVisitor::default();
visitor.visit_expr_method_call(&expr);
if !visitor.errors.is_empty() {
return Err(visitor.errors);
}
Ok(())
}

#[test]
fn test_as_primitives() {
let input = r#"x.as_u32()"#;
assert!(lint(input).is_err());
let input = r#"x.as_u64()"#;
assert!(lint(input).is_err());
let input = r#"x.as_u128()"#;
assert!(lint(input).is_err());
let input = r#"x.as_usize()"#;
assert!(lint(input).is_err());
}

#[test]
fn test_non_as_primitives() {
let input = r#"x.as_ref()"#;
assert!(lint(input).is_ok());
let input = r#"x.as_slice()"#;
assert!(lint(input).is_ok());
}
}
2 changes: 2 additions & 0 deletions support/linting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ pub mod lint;
pub use lint::*;

mod disallow_v1_benchmarks;
mod forbid_as_primitive;
mod pallet_index;
mod require_freeze_struct;

pub use disallow_v1_benchmarks::DisallowV1Benchmarks;
pub use forbid_as_primitive::ForbidAsPrimitiveConversion;
pub use pallet_index::RequireExplicitPalletIndex;
pub use require_freeze_struct::RequireFreezeStruct;
103 changes: 54 additions & 49 deletions support/procedural-fork/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,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 @@ -482,9 +500,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 @@ -693,18 +714,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 @@ -795,17 +814,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 @@ -823,6 +832,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 @@ -832,20 +847,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 @@ -1164,9 +1171,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 @@ -1180,15 +1188,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 @@ -1206,18 +1214,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 @@ -70,6 +70,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 @@ -62,17 +62,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 @@ -115,7 +115,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 @@ -160,7 +160,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
Loading

0 comments on commit 38d3b21

Please sign in to comment.