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 //#[cfg(any(feature = "ink-debug", feature = "std"))]
59 self::return_value(
60 ReturnFlags::REVERT,
61 &ink_prelude::format!("{}", info.message()).as_bytes(),
62 );
63
64 /*
65 #[cfg(not(any(feature = "ink-debug", feature = "std")))]
66 cfg_if::cfg_if! {
67 if #[cfg(target_arch = "riscv64")] {
68 // Safety: The unimp instruction is guaranteed to trap
69 unsafe {
70 core::arch::asm!("unimp");
71 core::hint::unreachable_unchecked();
72 }
73 } else {
74 core::compile_error!("ink! only supports riscv64");
75 }
76 }
77 */
78}
79
80// This extern crate definition is required since otherwise rustc
81// is not recognizing its allocator and panic handler definitions.
82#[cfg(not(any(feature = "std", feature = "no-allocator")))]
83extern crate ink_allocator;
84
85mod api;
86mod backend;
87pub mod call;
88pub mod chain_extension;
89mod engine;
90mod error;
91#[doc(hidden)]
92pub mod event;
93pub mod hash;
94
95#[cfg(test)]
96mod tests;
97
98#[cfg(any(feature = "std", test, doc))]
99#[doc(inline)]
100pub use self::engine::off_chain::test_api as test;
101
102use self::backend::{
103 EnvBackend,
104 TypedEnvBackend,
105};
106pub use self::{
107 api::*,
108 error::{
109 Error,
110 Result,
111 },
112 event::Event,
113 types::{
114 AccountIdGuard,
115 Balance,
116 BlockNumber,
117 CodecAsType,
118 DefaultEnvironment,
119 Environment,
120 FromLittleEndian,
121 Gas,
122 NoChainExtension,
123 Timestamp,
124 },
125};
126#[cfg(feature = "unstable-hostfn")]
127use ink_primitives::Clear;
128pub use ink_primitives::{
129 contract::{
130 ContractEnv,
131 ContractReference,
132 ContractReverseReference,
133 },
134 reflect,
135 reflect::{
136 DecodeDispatch,
137 DispatchError,
138 },
139 types,
140};
141#[doc(inline)]
142pub use pallet_revive_uapi::{
143 CallFlags,
144 ReturnErrorCode,
145 ReturnFlags,
146};