git.delta.rocks / unique-network / refs/commits / e8fdadb89923

difftreelog

source

pallets/contracts/common/src/lib.rs5.5 KiBsourcehistory
1// This file is part of Substrate.23// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.4// SPDX-License-Identifier: Apache-2.056// Licensed under the Apache License, Version 2.0 (the "License");7// you may not use this file except in compliance with the License.8// You may obtain a copy of the License at9//10// 	http://www.apache.org/licenses/LICENSE-2.011//12// Unless required by applicable law or agreed to in writing, software13// distributed under the License is distributed on an "AS IS" BASIS,14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15// See the License for the specific language governing permissions and16// limitations under the License.1718//! A crate that hosts a common definitions that are relevant for the pallet-contracts.1920#![cfg_attr(not(feature = "std"), no_std)]2122use bitflags::bitflags;23use codec::{Decode, Encode};24use sp_core::Bytes;25use sp_runtime::{DispatchError, RuntimeDebug};26use sp_std::prelude::*;2728#[cfg(feature = "std")]29use serde::{Serialize, Deserialize};3031/// Result type of a `bare_call` or `bare_instantiate` call.32///33/// It contains the execution result together with some auxiliary information.34#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]35#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]36#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]37pub struct ContractResult<T> {38	/// How much gas was consumed during execution.39	pub gas_consumed: u64,40	/// An optional debug message. This message is only non-empty when explicitly requested41	/// by the code that calls into the contract.42	///43	/// The contained bytes are valid UTF-8. This is not declared as `String` because44	/// this type is not allowed within the runtime. A client should decode them in order45	/// to present the message to its users.46	///47	/// # Note48	///49	/// The debug message is never generated during on-chain execution. It is reserved for50	/// RPC calls.51	pub debug_message: Bytes,52	/// The execution result of the wasm code.53	pub result: T,54}5556/// Result type of a `bare_call` call.57pub type ContractExecResult = ContractResult<Result<ExecReturnValue, DispatchError>>;5859/// Result type of a `bare_instantiate` call.60pub type ContractInstantiateResult<AccountId, BlockNumber> =61	ContractResult<Result<InstantiateReturnValue<AccountId, BlockNumber>, DispatchError>>;6263/// Result type of a `get_storage` call.64pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;6566/// Result type of a `rent_projection` call.67pub type RentProjectionResult<BlockNumber> =68	Result<RentProjection<BlockNumber>, ContractAccessError>;6970/// The possible errors that can happen querying the storage of a contract.71#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]72pub enum ContractAccessError {73	/// The given address doesn't point to a contract.74	DoesntExist,75	/// The specified contract is a tombstone and thus cannot have any storage.76	IsTombstone,77}7879#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]80#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]81#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]82pub enum RentProjection<BlockNumber> {83	/// Eviction is projected to happen at the specified block number.84	EvictionAt(BlockNumber),85	/// No eviction is scheduled.86	///87	/// E.g. Contract accumulated enough funds to offset the rent storage costs.88	NoEviction,89}9091bitflags! {92	/// Flags used by a contract to customize exit behaviour.93	#[derive(Encode, Decode)]94	#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]95	#[cfg_attr(feature = "std", serde(rename_all = "camelCase", transparent))]96	pub struct ReturnFlags: u32 {97		/// If this bit is set all changes made by the contract execution are rolled back.98		const REVERT = 0x0000_0001;99	}100}101102/// Output of a contract call or instantiation which ran to completion.103#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]104#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]105#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]106pub struct ExecReturnValue {107	/// Flags passed along by `seal_return`. Empty when `seal_return` was never called.108	pub flags: ReturnFlags,109	/// Buffer passed along by `seal_return`. Empty when `seal_return` was never called.110	pub data: Bytes,111}112113impl ExecReturnValue {114	/// We understand the absense of a revert flag as success.115	pub fn is_success(&self) -> bool {116		!self.flags.contains(ReturnFlags::REVERT)117	}118}119120/// The result of a successful contract instantiation.121#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]122#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]123#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]124pub struct InstantiateReturnValue<AccountId, BlockNumber> {125	/// The output of the called constructor.126	pub result: ExecReturnValue,127	/// The account id of the new contract.128	pub account_id: AccountId,129	/// Information about when and if the new project will be evicted.130	///131	/// # Note132	///133	/// `None` if `bare_instantiate` was called with134	/// `compute_projection` set to false. From the perspective of an RPC this means that135	/// the runtime API did not request this value and this feature is therefore unsupported.136	pub rent_projection: Option<RentProjection<BlockNumber>>,137}138139/// Reference to an existing code hash or a new wasm module.140#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]141#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]142#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]143pub enum Code<Hash> {144	/// A wasm module as raw bytes.145	Upload(Bytes),146	/// The code hash of an on-chain wasm blob.147	Existing(Hash),148}