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

Commit

Permalink
[contracts] Derive useful traits for public types (#14723)
Browse files Browse the repository at this point in the history
* Types in the pallet

* Primitives
  • Loading branch information
pmikolajczyk41 authored Aug 14, 2023
1 parent 48d4313 commit ca379d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
18 changes: 10 additions & 8 deletions frame/contracts/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use bitflags::bitflags;
use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Saturating, Zero},
Expand All @@ -39,7 +39,7 @@ use sp_weights::Weight;
/// It has been extended to include `events` at the end of the struct while not bumping the
/// `ContractsApi` version. Therefore when SCALE decoding a `ContractResult` its trailing data
/// should be ignored to avoid any potential compatibility issues.
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ContractResult<R, Balance, EventRecord> {
/// How much weight was consumed during execution.
pub gas_consumed: Weight,
Expand Down Expand Up @@ -99,7 +99,7 @@ pub type CodeUploadResult<CodeHash, Balance> =
pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;

/// The possible errors that can happen querying the storage of a contract.
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)]
pub enum ContractAccessError {
/// The given address doesn't point to a contract.
DoesntExist,
Expand All @@ -119,7 +119,7 @@ bitflags! {
}

/// Output of a contract call or instantiation which ran to completion.
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ExecReturnValue {
/// Flags passed along by `seal_return`. Empty when `seal_return` was never called.
pub flags: ReturnFlags,
Expand All @@ -135,7 +135,7 @@ impl ExecReturnValue {
}

/// The result of a successful contract instantiation.
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct InstantiateReturnValue<AccountId> {
/// The output of the called constructor.
pub result: ExecReturnValue,
Expand All @@ -144,7 +144,7 @@ pub struct InstantiateReturnValue<AccountId> {
}

/// The result of successfully uploading a contract.
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)]
pub struct CodeUploadReturnValue<CodeHash, Balance> {
/// The key under which the new code is stored.
pub code_hash: CodeHash,
Expand All @@ -153,7 +153,7 @@ pub struct CodeUploadReturnValue<CodeHash, Balance> {
}

/// Reference to an existing code hash or a new wasm module.
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum Code<Hash> {
/// A wasm module as raw bytes.
Upload(Vec<u8>),
Expand All @@ -162,7 +162,9 @@ pub enum Code<Hash> {
}

/// The amount of balance that was either charged or refunded in order to pay for storage.
#[derive(Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, Clone, TypeInfo)]
#[derive(
Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo,
)]
pub enum StorageDeposit<Balance> {
/// The transaction reduced storage consumption.
///
Expand Down
12 changes: 11 additions & 1 deletion frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,17 @@ pub trait Ext: sealing::Sealed {
}

/// Describes the different functions that can be exported by an [`Executable`].
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(
Copy,
Clone,
PartialEq,
Eq,
sp_core::RuntimeDebug,
codec::Decode,
codec::Encode,
codec::MaxEncodedLen,
scale_info::TypeInfo,
)]
pub enum ExportedFunction {
/// The constructor function which is executed on deployment of a contract.
Constructor,
Expand Down
12 changes: 8 additions & 4 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use crate::{
storage::{meter::Meter as StorageMeter, ContractInfo, DeletionQueueManager},
wasm::{CodeInfo, WasmBlob},
};
use codec::{Codec, Decode, Encode, HasCompact};
use codec::{Codec, Decode, Encode, HasCompact, MaxEncodedLen};
use environmental::*;
use frame_support::{
dispatch::{
Expand All @@ -122,7 +122,7 @@ use frame_support::{
ConstU32, Contains, Get, Randomness, Time,
},
weights::Weight,
BoundedVec, RuntimeDebugNoBound,
BoundedVec, RuntimeDebug, RuntimeDebugNoBound,
};
use frame_system::{ensure_signed, pallet_prelude::OriginFor, EventRecord, Pallet as System};
use pallet_contracts_primitives::{
Expand Down Expand Up @@ -1119,7 +1119,9 @@ struct InstantiateInput<T: Config> {
}

/// Determines whether events should be collected during execution.
#[derive(PartialEq)]
#[derive(
Copy, Clone, PartialEq, Eq, RuntimeDebug, Decode, Encode, MaxEncodedLen, scale_info::TypeInfo,
)]
pub enum CollectEvents {
/// Collect events.
///
Expand All @@ -1135,7 +1137,9 @@ pub enum CollectEvents {
}

/// Determines whether debug messages will be collected.
#[derive(PartialEq)]
#[derive(
Copy, Clone, PartialEq, Eq, RuntimeDebug, Decode, Encode, MaxEncodedLen, scale_info::TypeInfo,
)]
pub enum DebugInfo {
/// Collect debug messages.
/// # Note
Expand Down

0 comments on commit ca379d7

Please sign in to comment.