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 unreachable!(
70 "contract in non-debug/non-std mode needs to be build with `panic_immediate_abort`"
71 );
72}
73
74// This extern crate definition is required since otherwise rustc
75// is not recognizing its allocator and panic handler definitions.
76#[cfg(not(any(feature = "std", feature = "no-allocator")))]
77extern crate ink_allocator;
78
79mod api;
80mod backend;
81pub mod call;
82mod engine;
83mod error;
84#[doc(hidden)]
85pub mod event;
86pub mod hash;
87
88#[cfg(test)]
89mod tests;
90
91#[cfg(any(feature = "std", test, doc))]
92#[doc(inline)]
93pub use self::engine::off_chain::test_api as test;
94
95use self::backend::{
96 EnvBackend,
97 TypedEnvBackend,
98};
99pub use self::{
100 api::*,
101 error::{
102 Error,
103 Result,
104 },
105 event::{
106 Event,
107 TopicEncoder,
108 },
109 types::{
110 AccountIdGuard,
111 Balance,
112 BlockNumber,
113 CodecAsType,
114 DefaultEnvironment,
115 Environment,
116 FromLittleEndian,
117 Gas,
118 Timestamp,
119 },
120};
121pub use ink_primitives::{
122 contract::{
123 ContractEnv,
124 ContractReference,
125 ContractReverseReference,
126 },
127 reflect,
128 reflect::{
129 DecodeDispatch,
130 DispatchError,
131 },
132 types,
133};
134#[doc(inline)]
135pub use pallet_revive_uapi::{
136 CallFlags,
137 ReturnErrorCode,
138 ReturnFlags,
139};
140
141/// A convenience type alias to the marker type representing the "default" ABI for calls.
142///
143/// # Note
144///
145/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
146/// in the ink! project's manifest file (i.e. `Cargo.toml`).
147#[cfg(not(ink_abi = "sol"))]
148#[doc(hidden)]
149pub type DefaultAbi = ink_primitives::abi::Ink;
150
151/// A convenience type alias to the marker type representing the "default" ABI for calls.
152///
153/// # Note
154///
155/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
156/// in the ink! project's manifest file (i.e. `Cargo.toml`).
157#[cfg(ink_abi = "sol")]
158#[doc(hidden)]
159pub type DefaultAbi = ink_primitives::abi::Sol;