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

Commit f80d23b

Browse files
authored
contracts: Make ChainExtension trait generic over the runtime (#8003)
1 parent 9b40d50 commit f80d23b

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

frame/contracts/src/chain_extension.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use sp_std::{
6363
pub use frame_system::Config as SysConfig;
6464
pub use pallet_contracts_primitives::ReturnFlags;
6565
pub use sp_core::crypto::UncheckedFrom;
66-
pub use crate::exec::Ext;
66+
pub use crate::{Config, exec::Ext};
6767
pub use state::Init as InitState;
6868

6969
/// Result that returns a [`DispatchError`] on error.
@@ -74,7 +74,7 @@ pub type Result<T> = sp_std::result::Result<T, DispatchError>;
7474
/// In order to create a custom chain extension this trait must be implemented and supplied
7575
/// to the pallet contracts configuration trait as the associated type of the same name.
7676
/// Consult the [module documentation](self) for a general explanation of chain extensions.
77-
pub trait ChainExtension {
77+
pub trait ChainExtension<C: Config> {
7878
/// Call the chain extension logic.
7979
///
8080
/// This is the only function that needs to be implemented in order to write a
@@ -91,8 +91,9 @@ pub trait ChainExtension {
9191
/// In case of `Err` the contract execution is immediately suspended and the passed error
9292
/// is returned to the caller. Otherwise the value of [`RetVal`] determines the exit
9393
/// behaviour.
94-
fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal>
94+
fn call<E>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal>
9595
where
96+
E: Ext<T = C>,
9697
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>;
9798

9899
/// Determines whether chain extensions are enabled for this chain.
@@ -108,9 +109,10 @@ pub trait ChainExtension {
108109
}
109110

110111
/// Implementation that indicates that no chain extension is available.
111-
impl ChainExtension for () {
112-
fn call<E: Ext>(_func_id: u32, mut _env: Environment<E, InitState>) -> Result<RetVal>
112+
impl<C: Config> ChainExtension<C> for () {
113+
fn call<E>(_func_id: u32, mut _env: Environment<E, InitState>) -> Result<RetVal>
113114
where
115+
E: Ext<T = C>,
114116
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
115117
{
116118
// Never called since [`Self::enabled()`] is set to `false`. Because we want to

frame/contracts/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub trait Config: frame_system::Config {
337337
type WeightInfo: WeightInfo;
338338

339339
/// Type that allows the runtime authors to add new host functions for a contract to call.
340-
type ChainExtension: chain_extension::ChainExtension;
340+
type ChainExtension: chain_extension::ChainExtension<Self>;
341341

342342
/// The maximum number of tries that can be queued for deletion.
343343
type DeletionQueueDepth: Get<u32>;

frame/contracts/src/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ impl Default for TestExtension {
143143
}
144144
}
145145

146-
impl ChainExtension for TestExtension {
147-
fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> ExtensionResult<RetVal>
146+
impl ChainExtension<Test> for TestExtension {
147+
fn call<E>(func_id: u32, env: Environment<E, InitState>) -> ExtensionResult<RetVal>
148148
where
149+
E: Ext<T = Test>,
149150
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
150151
{
151152
match func_id {

0 commit comments

Comments
 (0)