noble_contracts_primitives/lib.rs
1// This file is part of Tetcore.
2
3// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// 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 at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// 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 and
16// limitations under the License.
17
18//! A crate that hosts a common definitions that are relevant for the noble-contracts.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use bitflags::bitflags;
23use codec::{Decode, Encode};
24use tp_runtime::{DispatchError, RuntimeDebug};
25use tetcore_std::prelude::*;
26
27/// Result type of a `bare_call` call.
28///
29/// The result of a contract execution along with a gas consumed.
30#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
31pub struct ContractExecResult {
32 pub exec_result: ExecResult,
33 pub gas_consumed: u64,
34}
35
36/// Result type of a `get_storage` call.
37pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;
38
39/// Result type of a `rent_projection` call.
40pub type RentProjectionResult<BlockNumber> =
41 Result<RentProjection<BlockNumber>, ContractAccessError>;
42
43/// The possible errors that can happen querying the storage of a contract.
44#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
45pub enum ContractAccessError {
46 /// The given address doesn't point to a contract.
47 DoesntExist,
48 /// The specified contract is a tombstone and thus cannot have any storage.
49 IsTombstone,
50}
51
52#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
53pub enum RentProjection<BlockNumber> {
54 /// Eviction is projected to happen at the specified block number.
55 EvictionAt(BlockNumber),
56 /// No eviction is scheduled.
57 ///
58 /// E.g. Contract accumulated enough funds to offset the rent storage costs.
59 NoEviction,
60}
61
62bitflags! {
63 /// Flags used by a contract to customize exit behaviour.
64 #[derive(Encode, Decode)]
65 pub struct ReturnFlags: u32 {
66 /// If this bit is set all changes made by the contract execution are rolled back.
67 const REVERT = 0x0000_0001;
68 }
69}
70
71/// Output of a contract call or instantiation which ran to completion.
72#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]
73pub struct ExecReturnValue {
74 /// Flags passed along by `seal_return`. Empty when `seal_return` was never called.
75 pub flags: ReturnFlags,
76 /// Buffer passed along by `seal_return`. Empty when `seal_return` was never called.
77 pub data: Vec<u8>,
78}
79
80impl ExecReturnValue {
81 /// We understand the absense of a revert flag as success.
82 pub fn is_success(&self) -> bool {
83 !self.flags.contains(ReturnFlags::REVERT)
84 }
85}
86
87/// Origin of the error.
88///
89/// Call or instantiate both called into other contracts and pass through errors happening
90/// in those to the caller. This enum is for the caller to distinguish whether the error
91/// happened during the execution of the callee or in the current execution context.
92#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]
93pub enum ErrorOrigin {
94 /// Caller error origin.
95 ///
96 /// The error happened in the current exeuction context rather than in the one
97 /// of the contract that is called into.
98 Caller,
99 /// The error happened during execution of the called contract.
100 Callee,
101}
102
103/// Error returned by contract exection.
104#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug)]
105pub struct ExecError {
106 /// The reason why the execution failed.
107 pub error: DispatchError,
108 /// Origin of the error.
109 pub origin: ErrorOrigin,
110}
111
112impl<T: Into<DispatchError>> From<T> for ExecError {
113 fn from(error: T) -> Self {
114 Self {
115 error: error.into(),
116 origin: ErrorOrigin::Caller,
117 }
118 }
119}
120
121/// The result that is returned from contract execution. It either contains the output
122/// buffer or an error describing the reason for failure.
123pub type ExecResult = Result<ExecReturnValue, ExecError>;