-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable benchmarking with prepared module
- Loading branch information
Showing
4 changed files
with
169 additions
and
33 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
substrate/frame/contracts/src/benchmarking/host_sandbox.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::{ | ||
benchmarking::{Contract, WasmModule}, | ||
exec::Stack, | ||
storage::meter::Meter, | ||
wasm::Runtime, | ||
BalanceOf, Config, Determinism, GasMeter, Origin, Schedule, TypeInfo, WasmBlob, Weight, | ||
}; | ||
use codec::{Encode, HasCompact}; | ||
use core::fmt::Debug; | ||
use sp_core::Get; | ||
use sp_std::prelude::*; | ||
use wasmi::{Func, Store}; | ||
|
||
type StackExt<'a, T> = Stack<'a, T, WasmBlob<T>>; | ||
|
||
/// A sandbox used for benchmarking host functions. | ||
pub struct Sandbox<T: Config> { | ||
caller: T::AccountId, | ||
dest: T::AccountId, | ||
gas_meter: GasMeter<T>, | ||
storage_meter: Meter<T>, | ||
schedule: Schedule<T>, | ||
} | ||
|
||
impl<T> Sandbox<T> | ||
where | ||
T: Config + pallet_balances::Config, | ||
<BalanceOf<T> as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, | ||
{ | ||
/// Create a new sandbox for the given module. | ||
pub fn new(module: WasmModule<T>) -> &'static mut Self { | ||
let instance = Contract::<T>::new(module.clone(), vec![]).unwrap(); | ||
let caller = instance.caller.clone(); | ||
let dest = instance.account_id.clone(); | ||
|
||
let sandbox = Self { | ||
caller, | ||
dest, | ||
schedule: T::Schedule::get(), | ||
gas_meter: GasMeter::new(Weight::MAX), | ||
storage_meter: Default::default(), | ||
}; | ||
|
||
// TODO: Clean up the leak | ||
Box::leak(Box::new(sandbox)) | ||
} | ||
|
||
/// Prepare a call to the module. | ||
pub fn prepare_call( | ||
&'static mut self, | ||
input: Vec<u8>, | ||
) -> (Func, Store<Runtime<StackExt<'static, T>>>) { | ||
let (ext, module) = Stack::bench_new_call( | ||
self.dest.clone(), | ||
Origin::from_account_id(self.caller.clone()), | ||
&mut self.gas_meter, | ||
&mut self.storage_meter, | ||
&self.schedule, | ||
0u32.into(), | ||
None, | ||
Determinism::Enforced, | ||
); | ||
|
||
// TODO: Clean up the leak | ||
let ext: &mut StackExt<'static, T> = Box::leak(Box::new(ext)); | ||
module.bench_prepare_call(ext, input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters