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

Commit a732a9a

Browse files
authored
contracts: Expose environment types for offchain tooling (#14750)
* Expose environment types for offchain tooling * Use EnvironmentType wrapper * Add type impl to test config --------- Co-authored-by: parity-processbot <>
1 parent 30998d1 commit a732a9a

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ impl pallet_contracts::Config for Runtime {
12651265
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
12661266
#[cfg(feature = "unsafe-debug")]
12671267
type Debug = ();
1268+
type Environment = ();
12681269
}
12691270

12701271
impl pallet_sudo::Config for Runtime {

frame/contracts/src/lib.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub mod weights;
103103
#[cfg(test)]
104104
mod tests;
105105
use crate::{
106-
exec::{AccountIdOf, ErrorOrigin, ExecError, Executable, Key, Stack as ExecStack},
106+
exec::{AccountIdOf, ErrorOrigin, ExecError, Executable, Key, MomentOf, Stack as ExecStack},
107107
gas::GasMeter,
108108
storage::{meter::Meter as StorageMeter, ContractInfo, DeletionQueueManager},
109109
wasm::{CodeInfo, WasmBlob},
@@ -122,9 +122,13 @@ use frame_support::{
122122
ConstU32, Contains, Get, Randomness, Time,
123123
},
124124
weights::Weight,
125-
BoundedVec, RuntimeDebug, RuntimeDebugNoBound,
125+
BoundedVec, DefaultNoBound, RuntimeDebug, RuntimeDebugNoBound,
126+
};
127+
use frame_system::{
128+
ensure_signed,
129+
pallet_prelude::{BlockNumberFor, OriginFor},
130+
EventRecord, Pallet as System,
126131
};
127-
use frame_system::{ensure_signed, pallet_prelude::OriginFor, EventRecord, Pallet as System};
128132
use pallet_contracts_primitives::{
129133
Code, CodeUploadResult, CodeUploadReturnValue, ContractAccessError, ContractExecResult,
130134
ContractInstantiateResult, ContractResult, ExecReturnValue, GetStorageResult,
@@ -179,6 +183,36 @@ const SENTINEL: u32 = u32::MAX;
179183
/// Example: `RUST_LOG=runtime::contracts=debug my_code --dev`
180184
const LOG_TARGET: &str = "runtime::contracts";
181185

186+
/// Wrapper around `PhantomData` to prevent it being filtered by `scale-info`.
187+
///
188+
/// `scale-info` filters out `PhantomData` fields because usually we are only interested
189+
/// in sized types. However, when trying to communicate **types** as opposed to **values**
190+
/// we want to have those zero sized types be included.
191+
#[derive(Encode, Decode, DefaultNoBound, TypeInfo)]
192+
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
193+
pub struct EnvironmentType<T>(PhantomData<T>);
194+
195+
/// List of all runtime configurable types that are used in the communication between
196+
/// `pallet-contracts` and any given contract.
197+
///
198+
/// Since those types are configurable they can vary between
199+
/// chains all using `pallet-contracts`. Hence we need a mechanism to communicate those types
200+
/// in a way that can be consumed by offchain tooling.
201+
///
202+
/// This type only exists in order to appear in the metadata where it can be read by
203+
/// offchain tooling.
204+
#[derive(Encode, Decode, DefaultNoBound, TypeInfo)]
205+
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
206+
#[scale_info(skip_type_params(T))]
207+
pub struct Environment<T: Config> {
208+
account_id: EnvironmentType<AccountIdOf<T>>,
209+
balance: EnvironmentType<BalanceOf<T>>,
210+
hash: EnvironmentType<<T as frame_system::Config>::Hash>,
211+
hasher: EnvironmentType<<T as frame_system::Config>::Hashing>,
212+
timestamp: EnvironmentType<MomentOf<T>>,
213+
block_number: EnvironmentType<BlockNumberFor<T>>,
214+
}
215+
182216
#[frame_support::pallet]
183217
pub mod pallet {
184218
use super::*;
@@ -360,6 +394,13 @@ pub mod pallet {
360394
/// Do **not** use it in a production environment or for benchmarking purposes.
361395
#[cfg(feature = "unsafe-debug")]
362396
type Debug: unsafe_debug::UnsafeDebug<Self>;
397+
398+
/// Type that bundles together all the runtime configurable interface types.
399+
///
400+
/// This is not a real config. We just mention the type here as constant so that
401+
/// its type appears in the metadata. Only valid value is `()`.
402+
#[pallet::constant]
403+
type Environment: Get<Environment<Self>>;
363404
}
364405

365406
#[pallet::hooks]

frame/contracts/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ impl Config for Test {
481481
type MaxDelegateDependencies = MaxDelegateDependencies;
482482
#[cfg(feature = "unsafe-debug")]
483483
type Debug = unsafe_debug::TestDebugger;
484+
type Environment = ();
484485
}
485486

486487
pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);

0 commit comments

Comments
 (0)