contracts: Convert to framev2 macros (#8157)

* contracts: Convert to framev2

* Reduce the API surface of the crate

* Remove unused import

* Merge import block

* Use pallet::metadata to reduce metadata diff

* Remove the explicit "Null" from AccountCounter
This commit is contained in:
Alexander Theißen
2021-02-22 16:18:24 +01:00
committed by GitHub
parent 4f4a0c5b38
commit b2f393945a
15 changed files with 673 additions and 668 deletions
@@ -28,13 +28,13 @@
//! Thus, before executing a contract it should be reinstrument with new schedule.
use crate::{
CodeHash, CodeStorage, PristineCode, Schedule, Config, Error,
wasm::{prepare, PrefabWasmModule}, Module as Contracts, RawEvent,
gas::{Gas, GasMeter, Token},
CodeHash, CodeStorage, PristineCode, Schedule, Config, Error, Weight,
wasm::{prepare, PrefabWasmModule}, Module as Contracts, Event,
gas::{GasMeter, Token},
weights::WeightInfo,
};
use sp_core::crypto::UncheckedFrom;
use frame_support::{StorageMap, dispatch::DispatchError};
use frame_support::dispatch::DispatchError;
#[cfg(feature = "runtime-benchmarks")]
pub use self::private::reinstrument as reinstrument;
@@ -58,7 +58,7 @@ where
Some(module) => increment_64(&mut module.refcount),
None => {
*existing = Some(prefab_module);
Contracts::<T>::deposit_event(RawEvent::CodeStored(code_hash))
Contracts::<T>::deposit_event(Event::CodeStored(code_hash))
}
}
});
@@ -170,7 +170,7 @@ where
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>
{
<PristineCode<T>>::remove(code_hash);
Contracts::<T>::deposit_event(RawEvent::CodeRemoved(code_hash))
Contracts::<T>::deposit_event(Event::CodeRemoved(code_hash))
}
/// Increment the refcount panicking if it should ever overflow (which will not happen).
@@ -196,7 +196,7 @@ struct InstrumentToken(u32);
impl<T: Config> Token<T> for InstrumentToken {
type Metadata = ();
fn calculate_amount(&self, _metadata: &Self::Metadata) -> Gas {
fn calculate_amount(&self, _metadata: &Self::Metadata) -> Weight {
T::WeightInfo::instrument(self.0 / 1024)
}
}
@@ -20,13 +20,11 @@
//!
//! Most likely you should use `define_env` macro.
#[macro_export]
macro_rules! convert_args {
() => (vec![]);
( $( $t:ty ),* ) => ( vec![ $( { use $crate::wasm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
}
#[macro_export]
macro_rules! gen_signature {
( ( $( $params: ty ),* ) ) => (
{
@@ -43,7 +41,6 @@ macro_rules! gen_signature {
);
}
#[macro_export]
macro_rules! gen_signature_dispatch {
(
$needle_name:ident,
@@ -102,7 +99,6 @@ where
f
}
#[macro_export]
macro_rules! unmarshall_then_body_then_marshall {
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
let body = $crate::wasm::env_def::macros::constrain_closure::<
@@ -128,7 +124,6 @@ macro_rules! unmarshall_then_body_then_marshall {
})
}
#[macro_export]
macro_rules! define_func {
( < E: $seal_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
fn $name< E: $seal_ty >(
@@ -152,7 +147,6 @@ macro_rules! define_func {
};
}
#[macro_export]
macro_rules! register_func {
( $reg_cb:ident, < E: $seal_ty:tt > ; ) => {};
@@ -215,9 +209,9 @@ mod tests {
use sp_runtime::traits::Zero;
use sp_sandbox::{ReturnValue, Value};
use crate::{
Weight,
wasm::{Runtime, runtime::TrapReason, tests::MockExt},
exec::Ext,
gas::Gas,
};
struct TestRuntime {
@@ -282,7 +276,7 @@ mod tests {
#[test]
fn macro_define_func() {
define_func!( <E: Ext> seal_gas (_ctx, amount: u32) => {
let amount = Gas::from(amount);
let amount = Weight::from(amount);
if !amount.is_zero() {
Ok(())
} else {
@@ -334,7 +328,7 @@ mod tests {
define_env!(Env, <E: Ext>,
seal_gas( _ctx, amount: u32 ) => {
let amount = Gas::from(amount);
let amount = Weight::from(amount);
if !amount.is_zero() {
Ok(())
} else {
@@ -22,7 +22,7 @@ use sp_sandbox::Value;
use parity_wasm::elements::{FunctionType, ValueType};
#[macro_use]
pub(crate) mod macros;
pub mod macros;
pub trait ConvertibleToWasm: Sized {
const VALUE_TYPE: ValueType;
@@ -67,13 +67,13 @@ impl ConvertibleToWasm for u64 {
}
}
pub(crate) type HostFunc<E> =
pub type HostFunc<E> =
fn(
&mut Runtime<E>,
&[sp_sandbox::Value]
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError>;
pub(crate) trait FunctionImplProvider<E: Ext> {
pub trait FunctionImplProvider<E: Ext> {
fn impls<F: FnMut(&[u8], HostFunc<E>)>(f: &mut F);
}
+5 -3
View File
@@ -38,6 +38,8 @@ use pallet_contracts_primitives::ExecResult;
pub use self::runtime::{ReturnCode, Runtime, RuntimeToken};
#[cfg(feature = "runtime-benchmarks")]
pub use self::code_cache::reinstrument;
#[cfg(test)]
pub use tests::MockExt;
/// A prepared wasm module ready for execution.
///
@@ -237,7 +239,7 @@ mod tests {
use crate::{
CodeHash, BalanceOf, Error, Module as Contracts,
exec::{Ext, StorageKey, AccountIdOf, Executable},
gas::{Gas, GasMeter},
gas::GasMeter,
tests::{Test, Call, ALICE, BOB},
};
use std::collections::HashMap;
@@ -248,7 +250,7 @@ mod tests {
use assert_matches::assert_matches;
use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags, ExecError, ErrorOrigin};
const GAS_LIMIT: Gas = 10_000_000_000;
const GAS_LIMIT: Weight = 10_000_000_000;
#[derive(Debug, PartialEq, Eq)]
struct DispatchEntry(Call);
@@ -1202,7 +1204,7 @@ mod tests {
&mut gas_meter,
).unwrap();
let gas_left = Gas::decode(&mut output.data.as_slice()).unwrap();
let gas_left = Weight::decode(&mut output.data.as_slice()).unwrap();
assert!(gas_left < GAS_LIMIT, "gas_left must be less than initial");
assert!(gas_left > gas_meter.gas_left(), "gas_left must be greater than final");
}
@@ -20,11 +20,11 @@
use crate::{
HostFnWeights, Config, CodeHash, BalanceOf, Error,
exec::{Ext, StorageKey, TopicOf},
gas::{Gas, GasMeter, Token, ChargedAmount},
gas::{GasMeter, Token, ChargedAmount},
wasm::env_def::ConvertibleToWasm,
};
use parity_wasm::elements::ValueType;
use frame_support::{dispatch::DispatchError, ensure, traits::Get};
use frame_support::{dispatch::DispatchError, ensure, traits::Get, weights::Weight};
use sp_std::prelude::*;
use codec::{Decode, DecodeAll, Encode};
use sp_runtime::traits::SaturatedConversion;
@@ -223,7 +223,7 @@ where
{
type Metadata = HostFnWeights<T>;
fn calculate_amount(&self, s: &Self::Metadata) -> Gas {
fn calculate_amount(&self, s: &Self::Metadata) -> Weight {
use self::RuntimeToken::*;
match *self {
MeteringBlock(amount) => s.gas.saturating_add(amount.into()),