ink_env/lib.rs
1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The `ink_env` utilities used to interoperate with the contract executor.
16//!
17//! Mainly provides entities to work on a contract's storage
18//! as well as high-level collections on top of those.
19//! Also provides environmental utilities, such as storage allocators,
20//! FFI to interface with FRAME contracts and a primitive blockchain
21//! emulator for simple off-chain testing.
22
23#![doc(
24 html_logo_url = "https://use.ink/img/crate-docs/logo.png",
25 html_favicon_url = "https://use.ink/crate-docs/favicon.png"
26)]
27#![cfg_attr(not(feature = "std"), no_std)]
28#![cfg_attr(docsrs, feature(doc_cfg))]
29#![deny(
30 missing_docs,
31 bad_style,
32 bare_trait_objects,
33 improper_ctypes,
34 non_shorthand_field_patterns,
35 no_mangle_generic_items,
36 overflowing_literals,
37 path_statements,
38 patterns_in_fns_without_body,
39 unconditional_recursion,
40 unused_allocation,
41 unused_comparisons,
42 unused_parens,
43 while_true,
44 trivial_casts,
45 trivial_numeric_casts,
46 unused_extern_crates
47)]
48
49/// The capacity of the static buffer.
50/// Usually set to 16 kB.
51/// Can be modified by setting `INK_STATIC_BUFFER_SIZE` environmental variable.
52#[const_env::from_env("INK_STATIC_BUFFER_SIZE")]
53pub const BUFFER_SIZE: usize = 16384;
54
55#[cfg(target_arch = "riscv64")]
56#[panic_handler]
57fn panic(info: &core::panic::PanicInfo) -> ! {
58 // In case the contract is build in debug-mode, we return the
59 // panic message as a payload by triggering a contract revert.
60 #[cfg(any(feature = "ink-debug", feature = "std"))]
61 self::return_value(
62 ReturnFlags::REVERT,
63 &ink_prelude::format!("{}", info.message()).as_bytes(),
64 );
65
66 // If contract is compiled with `cargo contract --release`, it will
67 // for efficiency reasons be build with `panic_immediate_abort`.
68 // This panic handler will thus never be invoked.
69 #[cfg(not(any(feature = "ink-debug", feature = "std")))]
70 unreachable!(
71 "contract in non-debug/non-std mode needs to be build with `panic_immediate_abort`"
72 );
73}
74
75// This extern crate definition is required since otherwise rustc
76// is not recognizing its allocator and panic handler definitions.
77#[cfg(not(any(feature = "std", feature = "no-allocator")))]
78extern crate ink_allocator;
79
80mod api;
81mod backend;
82pub mod call;
83mod engine;
84mod error;
85#[doc(hidden)]
86pub mod event;
87pub mod hash;
88
89#[cfg(test)]
90mod tests;
91
92#[cfg(any(feature = "std", test, doc))]
93#[doc(inline)]
94pub use self::engine::off_chain::test_api as test;
95
96use self::backend::{
97 EnvBackend,
98 TypedEnvBackend,
99};
100pub use self::{
101 api::*,
102 error::{
103 Error,
104 Result,
105 },
106 event::{
107 Event,
108 TopicEncoder,
109 },
110 types::{
111 AccountIdGuard,
112 Balance,
113 BlockNumber,
114 CodecAsType,
115 DefaultEnvironment,
116 Environment,
117 FromLittleEndian,
118 Gas,
119 Timestamp,
120 },
121};
122pub use ink_primitives::{
123 contract::{
124 ContractEnv,
125 ContractReference,
126 ContractReverseReference,
127 },
128 reflect,
129 reflect::{
130 DecodeDispatch,
131 DispatchError,
132 },
133 types,
134};
135#[doc(inline)]
136pub use pallet_revive_uapi::{
137 CallFlags,
138 ReturnErrorCode,
139 ReturnFlags,
140};
141
142/// A convenience type alias to the marker type representing the "default" ABI for calls.
143///
144/// # Note
145///
146/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
147/// in the ink! project's manifest file (i.e. `Cargo.toml`).
148#[cfg(not(ink_abi = "sol"))]
149#[doc(hidden)]
150pub type DefaultAbi = ink_primitives::abi::Ink;
151
152/// A convenience type alias to the marker type representing the "default" ABI for calls.
153///
154/// # Note
155///
156/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
157/// in the ink! project's manifest file (i.e. `Cargo.toml`).
158#[cfg(ink_abi = "sol")]
159#[doc(hidden)]
160pub type DefaultAbi = ink_primitives::abi::Sol;