fluentbase_types/lib.rs
1//! Shared primitive types, syscall indices, and constants used across the Fluentbase stack.
2#![cfg_attr(not(feature = "std"), no_std)]
3
4extern crate alloc;
5
6pub mod bincode;
7mod block_fuel;
8mod bytecode;
9mod crypto_api;
10mod curves;
11mod eip2935;
12mod exit_code;
13pub mod fd;
14mod genesis;
15mod import_linker;
16mod native_api;
17#[cfg(target_arch = "wasm32")]
18mod rwasm_context;
19mod sys_func_idx;
20mod syscall;
21pub mod system;
22
23pub use alloy_primitives::*;
24pub use block_fuel::*;
25pub use bytecode::*;
26pub use byteorder;
27pub use crypto_api::*;
28pub use curves::*;
29pub use eip2935::*;
30pub use exit_code::*;
31pub use genesis::*;
32pub use hashbrown::{self, hash_map, hash_set, HashMap, HashSet};
33pub use import_linker::*;
34pub use native_api::*;
35pub use paste;
36#[cfg(target_arch = "wasm32")]
37pub use rwasm_context::{bindings, RwasmContext};
38pub use sys_func_idx::*;
39pub use syscall::*;
40
41pub const KECCAK_EMPTY: B256 =
42 b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
43pub const POSEIDON_EMPTY: B256 =
44 b256!("2098f5fb9e239eab3ceac3f27b81e481dc3124d55ffed523a839ee8446b64864");
45
46/// keccak256 of "Transfer(address,address,uint256)" that notifies
47/// about native transfer of eth
48pub const NATIVE_TRANSFER_KECCAK: B256 =
49 b256!("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");
50pub const NATIVE_TRANSFER_ADDRESS: Address = address!("0000000000000000000000000000000000000000");
51
52pub const SYSTEM_ADDRESS: Address = address!("0xfffffffffffffffffffffffffffffffffffffffe");
53
54pub const STATE_MAIN: u32 = 0;
55pub const STATE_DEPLOY: u32 = 1;
56
57pub const CALL_DEPTH_ROOT: u32 = 0;
58
59/// A relation between fuel and gas,
60/// according to our benchmarks, average WebAssembly instruction is ~20 times faster than average EVM
61/// instruction.
62///
63/// The value can be changed in the future.
64pub const FUEL_DENOM_RATE: u64 = 20;
65
66/// The maximum allowed value for the `x` parameter used in linear gas cost calculation
67/// of builtins.
68/// This limit ensures:
69/// 1. Runtime: all intermediate i32 operations do not overflow (WASM constraint)
70/// 2. Compile-time: final fuel value fits in u32
71///
72/// Formula:
73/// words = (x + 31) / 32
74/// fuel = base_cost + word_cost × words
75///
76/// Derivation:
77/// The bottleneck is I32Mul: words × word_cost ≤ i32::MAX
78///
79/// words ≈ x / 32, so:
80/// (x / 32) × word_cost ≤ 2^31
81/// x ≤ (2^31 × 32) / word_cost_max
82///
83/// Worst case (DEBUG_LOG with FUEL_DENOM_RATE = 20):
84/// - word_cost_max = 16 × 20 = 320
85///
86/// x ≤ (2^31 × 32) / 320 = 214,748,364 bytes (~204 MB)
87///
88/// We use 128 MB as a safe limit within the theoretical maximum:
89pub const FUEL_MAX_LINEAR_X: u32 = 134_217_728; // 128 MB (2^27)
90
91/// The maximum allowed value for the `x` parameter used in quadratic gas cost calculation
92/// of builtins.
93/// This limit ensures:
94/// 1. Runtime: words × words does not overflow i32 (WASM constraint)
95/// 2. Compile-time: final fuel value fits in u32
96///
97/// Formula:
98/// words = (x + 31) / 32
99/// fuel = (word_cost × words + words² / divisor) × FUEL_DENOM_RATE
100///
101/// Derivation:
102/// words × words must not overflow i32:
103/// words² ≤ i32::MAX (2,147,483,647)
104/// words ≤ 46,340
105/// x ≤ 46,340 × 32 = 1,482,880 bytes (~1.4 MB)
106///
107/// We use 1.25 MB as a safe limit within the theoretical maximum:
108pub const FUEL_MAX_QUADRATIC_X: u32 = 1_310_720; // 1.25 MB (2^20 + 2^18)
109
110// Quadratic fuel constants (EVM memory expansion formula)
111pub const QUADRATIC_WORD_FUEL_COST: u32 = 3;
112pub const QUADRATIC_DIVISOR: u32 = 512;
113
114/// A max rWasm call stack limit
115pub const CALL_STACK_LIMIT: u32 = 1024;
116
117/// WASM max code size
118///
119/// This value is temporary for testing purposes, requires recalculation.
120/// The limit is equal to 2Mb.
121pub const WASM_MAX_CODE_SIZE: usize = 0x100000;
122#[cfg(feature = "svm")]
123pub const SVM_MAX_CODE_SIZE: usize = 0x200000;
124
125/// rWasm max code size
126///
127/// This limit is required to limit the number of bytes produced after Wasm binary compilation.
128/// There are several attack vectors on this that produces abnormal amount of instructions.
129pub const RWASM_MAX_CODE_SIZE: usize = 12 * 1024 * 1024;
130
131/// WebAssembly magic bytes
132///
133/// These values are equal to \0ASM
134pub const WASM_MAGIC_BYTES: [u8; 4] = [0x00, 0x61, 0x73, 0x6d];
135/// Solana magic bytes
136#[cfg(feature = "svm")]
137pub const SVM_ELF_MAGIC_BYTES: [u8; 4] = [0x7f, 0x45, 0x4c, 0x46];
138/// ERC20 magic bytes: as char codes for "ERC" and the number 0x20
139pub const UNIVERSAL_TOKEN_MAGIC_BYTES: [u8; 4] = [0x45, 0x52, 0x43, 0x20];
140
141/// EIP-170: Contract code size limit
142///
143/// By default, the limit is `0x6000` (~25kb)
144pub const EVM_MAX_CODE_SIZE: usize = 0x6000;
145
146/// EIP-3860: Limit and meter initcode
147///
148/// Limit of maximum initcode size is `2 * WASM_MAX_CODE_SIZE`.
149pub const EVM_MAX_INITCODE_SIZE: usize = 2 * EVM_MAX_CODE_SIZE;
150
151pub const EIP7702_SIG_LEN: usize = 2;
152/// rWASM binary format signature:
153/// - 0xef 0x00 - EIP-3540 compatible prefix
154/// - 0x52 - rWASM version number (equal to 'R')
155pub const EIP7702_SIG: [u8; EIP7702_SIG_LEN] = [0xef, 0x01];
156
157pub const WASM_SIG_LEN: usize = 4;
158/// WebAssembly signature (\00ASM)
159pub const WASM_SIG: [u8; WASM_SIG_LEN] = [0x00, 0x61, 0x73, 0x6d];
160
161pub const RWASM_SIG_LEN: usize = 2;
162/// rWASM binary format signature:
163/// - 0xef 0x00 - EIP-3540 compatible prefix
164/// - 0x52 - rWASM version number (equal to 'R')
165pub const RWASM_SIG: [u8; RWASM_SIG_LEN] = [0xef, 0x52];
166
167/// A maximum allowed bytes to be copied using EXT_CODE_COPY instruction
168///
169/// Technically we can't store a binary that exceeds rWasm module size (12MiB)
170pub const EXT_CODE_COPY_MAX_COPY_SIZE: usize = RWASM_MAX_CODE_SIZE;