Skip to content

Commit

Permalink
Merge branch 'oty-fix-bench-runner' into bko-enable-gha-short-benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur authored Sep 22, 2024
2 parents 0a07fe8 + eeb7037 commit c71e04c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion substrate/primitives/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod ext;
pub mod fuzzing;
#[cfg(feature = "std")]
mod in_memory_backend;
pub(crate) mod overlayed_changes;
pub mod overlayed_changes; // FAIL-CI
#[cfg(feature = "std")]
mod read_only;
mod stats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ pub enum ExecutionMode {

#[derive(Debug, Default, Clone)]
#[cfg_attr(test, derive(PartialEq))]
struct InnerValue<V> {
pub struct InnerValue<V> {

Check failure on line 70 in substrate/primitives/state-machine/src/overlayed_changes/changeset.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

missing documentation for a struct
/// Current value. None if value has been deleted.
value: V,
pub value: V,
/// The set of extrinsic indices where the values has been changed.
extrinsics: Extrinsics,
}
Expand All @@ -80,7 +80,7 @@ struct InnerValue<V> {
pub struct OverlayedEntry<V> {
/// The individual versions of that value.
/// One entry per transactions during that the value was actually written.
transactions: Transactions<V>,
pub transactions: Transactions<V>,
}

impl<V> Default for OverlayedEntry<V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! The overlayed changes to state.

mod changeset;
pub mod changeset; // FAIL-CI
mod offchain;

use self::changeset::OverlayedChangeSet;
Expand Down
46 changes: 27 additions & 19 deletions substrate/utils/frame/benchmarking-cli/src/pallet/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ impl PalletCmd {
return self.output_from_results(&batches)
}

let (genesis_storage, genesis_changes) =
self.genesis_storage::<Hasher, ExtraHostFunctions>(&chain_spec)?;
let mut changes = genesis_changes.clone();
let genesis_storage = self.genesis_storage::<Hasher, ExtraHostFunctions>(&chain_spec)?;

let cache_size = Some(self.database_cache_size as usize);
let state_with_tracking = BenchmarkingState::<Hasher>::new(
Expand Down Expand Up @@ -262,7 +260,7 @@ impl PalletCmd {
Self::exec_state_machine(
StateMachine::new(
state,
&mut changes,
&mut Default::default(),
&executor,
"Benchmark_benchmark_metadata",
&(self.extra).encode(),
Expand Down Expand Up @@ -347,7 +345,6 @@ impl PalletCmd {
for (s, selected_components) in all_components.iter().enumerate() {
// First we run a verification
if !self.no_verify {
let mut changes = genesis_changes.clone();
let state = &state_without_tracking;
// Don't use these results since verification code will add overhead.
let _batch: Vec<BenchmarkBatch> = match Self::exec_state_machine::<
Expand All @@ -357,7 +354,7 @@ impl PalletCmd {
>(
StateMachine::new(
state,
&mut changes,
&mut Default::default(),
&executor,
"Benchmark_dispatch_benchmark",
&(
Expand Down Expand Up @@ -389,7 +386,6 @@ impl PalletCmd {
}
// Do one loop of DB tracking.
{
let mut changes = genesis_changes.clone();
let state = &state_with_tracking;
let batch: Vec<BenchmarkBatch> = match Self::exec_state_machine::<
std::result::Result<Vec<BenchmarkBatch>, String>,
Expand All @@ -398,7 +394,7 @@ impl PalletCmd {
>(
StateMachine::new(
state, // todo remove tracking
&mut changes,
&mut Default::default(),
&executor,
"Benchmark_dispatch_benchmark",
&(
Expand Down Expand Up @@ -432,7 +428,6 @@ impl PalletCmd {
}
// Finally run a bunch of loops to get extrinsic timing information.
for r in 0..self.external_repeat {
let mut changes = genesis_changes.clone();
let state = &state_without_tracking;
let batch = match Self::exec_state_machine::<
std::result::Result<Vec<BenchmarkBatch>, String>,
Expand All @@ -441,7 +436,7 @@ impl PalletCmd {
>(
StateMachine::new(
state, // todo remove tracking
&mut changes,
&mut Default::default(),
&executor,
"Benchmark_dispatch_benchmark",
&(
Expand Down Expand Up @@ -567,16 +562,11 @@ impl PalletCmd {
Ok(benchmarks_to_run)
}

/// Produce a genesis storage and genesis changes.
///
/// It would be easier to only return one type, but there is no easy way to convert them.
// TODO: Re-write `BenchmarkingState` to not be such a clusterfuck and only accept
// `OverlayedChanges` instead of a mix between `OverlayedChanges` and `State`. But this can only
// be done once we deprecated and removed the legacy interface :(
/// Produce a genesis storage.
fn genesis_storage<H: Hash, F: HostFunctions>(
&self,
chain_spec: &Option<Box<dyn ChainSpec>>,
) -> Result<(sp_storage::Storage, OverlayedChanges<H>)> {
) -> Result<sp_storage::Storage> {
Ok(match (self.genesis_builder, self.runtime.is_some()) {
(Some(GenesisBuilder::None), _) => Default::default(),
(Some(GenesisBuilder::Spec), _) | (None, false) => {
Expand All @@ -589,13 +579,30 @@ impl PalletCmd {
.build_storage()
.map_err(|e| format!("{ERROR_CANNOT_BUILD_GENESIS}\nError: {e}"))?;

(storage, Default::default())
storage
},
(Some(GenesisBuilder::Runtime), _) | (None, true) =>
(Default::default(), self.genesis_from_runtime::<H, F>()?),
self.genesis_from_runtime::<H, F>().map(Self::changes_to_storage)?,
})
}

fn changes_to_storage<H: Hash>(changes: OverlayedChanges<H>) -> sp_storage::Storage {
let mut top = BTreeMap::<Vec<u8>, Vec<u8>>::new();

for (k, v) in changes.changes() {
let v = v.transactions[0].clone();
match &v.value {
sp_state_machine::overlayed_changes::changeset::StorageEntry::Set(v) => {
top.insert(k.clone(), v.clone());
},
_ => unreachable!("Only Set is expected"),
}
}

// TODO let child_changes =
sp_storage::Storage { top, children_default: Default::default() }
}

/// Generate the genesis changeset by the runtime API.
fn genesis_from_runtime<H: Hash, F: HostFunctions>(&self) -> Result<OverlayedChanges<H>> {
let state = BenchmarkingState::<H>::new(
Expand Down Expand Up @@ -739,6 +746,7 @@ impl PalletCmd {
) -> Result<FetchedCode<'a, BenchmarkingState<H>, H>> {
if let Some(runtime) = &self.runtime {
log::info!("Loading WASM from {}", runtime.display());

let code = fs::read(runtime)?;
let hash = sp_core::blake2_256(&code).to_vec();
let wrapped_code = WrappedRuntimeCode(Cow::Owned(code));
Expand Down

0 comments on commit c71e04c

Please sign in to comment.