Skip to main content

fluentbase_types/
bytecode.rs

1use crate::{Address, B256, RWASM_SIG, RWASM_SIG_LEN, WASM_SIG, WASM_SIG_LEN};
2use core::fmt::Formatter;
3use rwasm::RwasmModule;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
6#[allow(non_camel_case_types)]
7pub enum BytecodeType {
8    EVM,
9    WASM,
10}
11
12impl BytecodeType {
13    pub fn from_slice(input: &[u8]) -> Self {
14        // default WebAssembly signature
15        if input.len() >= WASM_SIG_LEN && input[0..WASM_SIG_LEN] == WASM_SIG {
16            return Self::WASM;
17        }
18        // case for rWASM contracts that are inside genesis
19        if input.len() >= RWASM_SIG_LEN && input[0..RWASM_SIG_LEN] == RWASM_SIG {
20            return Self::WASM;
21        }
22        // all the rest are EVM bytecode
23        Self::EVM
24    }
25}
26
27#[derive(Clone, Debug)]
28pub enum BytecodeOrHash {
29    Bytecode {
30        bytecode: RwasmModule,
31        hash: B256,
32        address: Address,
33    },
34    Hash(B256),
35}
36
37impl core::fmt::Display for BytecodeOrHash {
38    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
39        match self {
40            BytecodeOrHash::Bytecode {
41                address,
42                hash: code_hash,
43                ..
44            } => {
45                write!(f, "bytecode {}::{}", address, code_hash)
46            }
47            BytecodeOrHash::Hash(code_hash) => write!(f, "{}", code_hash),
48        }
49    }
50}
51
52impl From<B256> for BytecodeOrHash {
53    #[inline(always)]
54    fn from(value: B256) -> Self {
55        Self::Hash(value)
56    }
57}
58
59impl Default for BytecodeOrHash {
60    #[inline(always)]
61    fn default() -> Self {
62        Self::Hash(B256::ZERO)
63    }
64}
65
66impl BytecodeOrHash {
67    pub fn code_hash(&self) -> B256 {
68        match self {
69            BytecodeOrHash::Bytecode {
70                hash: code_hash, ..
71            } => *code_hash,
72            BytecodeOrHash::Hash(hash) => *hash,
73        }
74    }
75}