From 26b093ea8ad909fe1916c815af640131fbecbc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Wed, 3 Feb 2021 12:29:18 +0100 Subject: [PATCH] contracts: Make ChainExtension trait generic over the runtime (#8003) --- substrate/frame/contracts/src/chain_extension.rs | 12 +++++++----- substrate/frame/contracts/src/lib.rs | 2 +- substrate/frame/contracts/src/tests.rs | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/substrate/frame/contracts/src/chain_extension.rs b/substrate/frame/contracts/src/chain_extension.rs index c664b82fe6..ef6e034791 100644 --- a/substrate/frame/contracts/src/chain_extension.rs +++ b/substrate/frame/contracts/src/chain_extension.rs @@ -63,7 +63,7 @@ use sp_std::{ pub use frame_system::Config as SysConfig; pub use pallet_contracts_primitives::ReturnFlags; pub use sp_core::crypto::UncheckedFrom; -pub use crate::exec::Ext; +pub use crate::{Config, exec::Ext}; pub use state::Init as InitState; /// Result that returns a [`DispatchError`] on error. @@ -74,7 +74,7 @@ pub type Result = sp_std::result::Result; /// In order to create a custom chain extension this trait must be implemented and supplied /// to the pallet contracts configuration trait as the associated type of the same name. /// Consult the [module documentation](self) for a general explanation of chain extensions. -pub trait ChainExtension { +pub trait ChainExtension { /// Call the chain extension logic. /// /// This is the only function that needs to be implemented in order to write a @@ -91,8 +91,9 @@ pub trait ChainExtension { /// In case of `Err` the contract execution is immediately suspended and the passed error /// is returned to the caller. Otherwise the value of [`RetVal`] determines the exit /// behaviour. - fn call(func_id: u32, env: Environment) -> Result + fn call(func_id: u32, env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>; /// Determines whether chain extensions are enabled for this chain. @@ -108,9 +109,10 @@ pub trait ChainExtension { } /// Implementation that indicates that no chain extension is available. -impl ChainExtension for () { - fn call(_func_id: u32, mut _env: Environment) -> Result +impl ChainExtension for () { + fn call(_func_id: u32, mut _env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { // Never called since [`Self::enabled()`] is set to `false`. Because we want to diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs index 9c810faad9..96ba7b32e2 100644 --- a/substrate/frame/contracts/src/lib.rs +++ b/substrate/frame/contracts/src/lib.rs @@ -337,7 +337,7 @@ pub trait Config: frame_system::Config { type WeightInfo: WeightInfo; /// Type that allows the runtime authors to add new host functions for a contract to call. - type ChainExtension: chain_extension::ChainExtension; + type ChainExtension: chain_extension::ChainExtension; /// The maximum number of tries that can be queued for deletion. type DeletionQueueDepth: Get; diff --git a/substrate/frame/contracts/src/tests.rs b/substrate/frame/contracts/src/tests.rs index a8bf80213a..448df5b0de 100644 --- a/substrate/frame/contracts/src/tests.rs +++ b/substrate/frame/contracts/src/tests.rs @@ -143,9 +143,10 @@ impl Default for TestExtension { } } -impl ChainExtension for TestExtension { - fn call(func_id: u32, env: Environment) -> ExtensionResult +impl ChainExtension for TestExtension { + fn call(func_id: u32, env: Environment) -> ExtensionResult where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { match func_id {