Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[contracts] Adapt storage reading host functions to Weights V2 #12976

Merged
merged 11 commits into from
Jan 18, 2023
14 changes: 7 additions & 7 deletions frame/contracts/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ fn iterate_fields(data: &syn::DataStruct, fmt: impl Fn(&Ident) -> TokenStream2)

fn format_weight(field: &Ident) -> TokenStream2 {
quote_spanned! { field.span() =>
&if self.#field > 1_000_000_000 {
&if self.#field.ref_time() > 1_000_000_000 {
format!(
"{:.1?} ms",
Fixed::saturating_from_rational(self.#field, 1_000_000_000).to_float()
Fixed::saturating_from_rational(self.#field.ref_time(), 1_000_000_000).to_float()
)
} else if self.#field > 1_000_000 {
} else if self.#field.ref_time() > 1_000_000 {
format!(
"{:.1?} µs",
Fixed::saturating_from_rational(self.#field, 1_000_000).to_float()
Fixed::saturating_from_rational(self.#field.ref_time(), 1_000_000).to_float()
)
} else if self.#field > 1_000 {
} else if self.#field.ref_time() > 1_000 {
format!(
"{:.1?} ns",
Fixed::saturating_from_rational(self.#field, 1_000).to_float()
Fixed::saturating_from_rational(self.#field.ref_time(), 1_000).to_float()
)
} else {
format!("{} ps", self.#field)
format!("{} ps", self.#field.ref_time())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ benchmarks! {
// configured `Schedule` during benchmark development.
// It can be outputed using the following command:
// cargo run --manifest-path=bin/node/cli/Cargo.toml --release \
// --features runtime-benchmarks -- benchmark --extra --dev --execution=native \
// --features runtime-benchmarks -- benchmark pallet --extra --dev --execution=native \
// -p pallet_contracts -e print_schedule --no-median-slopes --no-min-squares
#[extra]
print_schedule {
Expand Down
7 changes: 3 additions & 4 deletions frame/contracts/src/chain_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S> {
///
/// Weight is synonymous with gas in substrate.
pub fn charge_weight(&mut self, amount: Weight) -> Result<ChargedAmount> {
self.inner.runtime.charge_gas(RuntimeCosts::ChainExtension(amount.ref_time()))
self.inner.runtime.charge_gas(RuntimeCosts::ChainExtension(amount))
}

/// Adjust a previously charged amount down to its actual amount.
Expand All @@ -237,7 +237,7 @@ impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S> {
pub fn adjust_weight(&mut self, charged: ChargedAmount, actual_weight: Weight) {
self.inner
.runtime
.adjust_gas(charged, RuntimeCosts::ChainExtension(actual_weight.ref_time()))
.adjust_gas(charged, RuntimeCosts::ChainExtension(actual_weight))
}

/// Grants access to the execution environment of the current contract call.
Expand Down Expand Up @@ -408,8 +408,7 @@ impl<'a, 'b, E: Ext, S: BufOut> Environment<'a, 'b, E, S> {
buffer,
allow_skip,
|len| {
weight_per_byte
.map(|w| RuntimeCosts::ChainExtension(w.ref_time().saturating_mul(len.into())))
weight_per_byte.map(|w| RuntimeCosts::ChainExtension(w.saturating_mul(len.into())))
},
)
}
Expand Down
4 changes: 2 additions & 2 deletions frame/contracts/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ impl<T: Config> GasMeter<T> {
/// Returns `OutOfGas` if there is not enough gas or addition of the specified
/// amount of gas has lead to overflow. On success returns `Proceed`.
///
/// NOTE that amount is always consumed, i.e. if there is not enough gas
/// then the counter will be set to zero.
/// NOTE that amount isn't consumed if there is not enough gas. This is considered
/// safe because we always charge gas before performing any resource-spending action.
#[inline]
pub fn charge<Tok: Token<T>>(&mut self, token: Tok) -> Result<ChargedAmount, DispatchError> {
#[cfg(test)]
Expand Down
22 changes: 11 additions & 11 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ pub mod pallet {
{
/// Deprecated version if [`Self::call`] for use in an in-storage `Call`.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::call().saturating_add(<Pallet<T>>::compat_weight(*gas_limit)))]
#[pallet::weight(T::WeightInfo::call().saturating_add(<Pallet<T>>::compat_weight_limit(*gas_limit)))]
#[allow(deprecated)]
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to `call`")]
pub fn call_old_weight(
Expand All @@ -396,7 +396,7 @@ pub mod pallet {
origin,
dest,
value,
<Pallet<T>>::compat_weight(gas_limit),
<Pallet<T>>::compat_weight_limit(gas_limit),
storage_deposit_limit,
data,
)
Expand All @@ -406,7 +406,7 @@ pub mod pallet {
#[pallet::call_index(1)]
#[pallet::weight(
T::WeightInfo::instantiate_with_code(code.len() as u32, data.len() as u32, salt.len() as u32)
.saturating_add(<Pallet<T>>::compat_weight(*gas_limit))
.saturating_add(<Pallet<T>>::compat_weight_limit(*gas_limit))
)]
#[allow(deprecated)]
#[deprecated(
Expand All @@ -424,7 +424,7 @@ pub mod pallet {
Self::instantiate_with_code(
origin,
value,
<Pallet<T>>::compat_weight(gas_limit),
<Pallet<T>>::compat_weight_limit(gas_limit),
storage_deposit_limit,
code,
data,
Expand All @@ -435,7 +435,7 @@ pub mod pallet {
/// Deprecated version if [`Self::instantiate`] for use in an in-storage `Call`.
#[pallet::call_index(2)]
#[pallet::weight(
T::WeightInfo::instantiate(data.len() as u32, salt.len() as u32).saturating_add(<Pallet<T>>::compat_weight(*gas_limit))
T::WeightInfo::instantiate(data.len() as u32, salt.len() as u32).saturating_add(<Pallet<T>>::compat_weight_limit(*gas_limit))
)]
#[allow(deprecated)]
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to `instantiate`")]
Expand All @@ -451,7 +451,7 @@ pub mod pallet {
Self::instantiate(
origin,
value,
<Pallet<T>>::compat_weight(gas_limit),
<Pallet<T>>::compat_weight_limit(gas_limit),
storage_deposit_limit,
code_hash,
data,
Expand Down Expand Up @@ -1216,12 +1216,12 @@ impl<T: Config> Pallet<T> {
<T::Currency as Inspect<AccountIdOf<T>>>::minimum_balance()
}

/// Convert a 1D Weight to a 2D weight.
/// Convert gas_limit from 1D Weight to a 2D Weight.
///
/// Used by backwards compatible extrinsics. We cannot just set the proof to zero
/// or an old `Call` will just fail.
fn compat_weight(gas_limit: OldWeight) -> Weight {
Weight::from(gas_limit).set_proof_size(u64::from(T::MaxCodeLen::get()) * 2)
/// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to
/// zero or an old `Call` will just fail with OutOfGas.
fn compat_weight_limit(gas_limit: OldWeight) -> Weight {
Weight::from_parts(gas_limit.0, u64::from(T::MaxCodeLen::get()) * 2)
}
}

Expand Down
Loading