revm_bytecode/legacy/
raw.rs

1use super::{analyze_legacy, LegacyAnalyzedBytecode};
2use core::ops::Deref;
3use primitives::Bytes;
4use std::vec::Vec;
5
6/// Used only as intermediate representation for legacy bytecode.
7/// Please check [`LegacyAnalyzedBytecode`] for the main structure that is used in Revm.
8#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct LegacyRawBytecode(pub Bytes);
11
12impl LegacyRawBytecode {
13    /// Converts the raw bytecode into an analyzed bytecode.
14    ///
15    /// It extends the bytecode with 33 zero bytes and analyzes it to find the jumpdests.
16    pub fn into_analyzed(self) -> LegacyAnalyzedBytecode {
17        let len = self.0.len();
18        let mut padded_bytecode = Vec::with_capacity(len + 33);
19        padded_bytecode.extend_from_slice(&self.0);
20        padded_bytecode.resize(len + 33, 0);
21        let jump_table = analyze_legacy(&padded_bytecode);
22        LegacyAnalyzedBytecode::new(padded_bytecode.into(), len, jump_table)
23    }
24}
25
26impl From<Bytes> for LegacyRawBytecode {
27    fn from(bytes: Bytes) -> Self {
28        Self(bytes)
29    }
30}
31
32impl<const N: usize> From<[u8; N]> for LegacyRawBytecode {
33    fn from(bytes: [u8; N]) -> Self {
34        Self(bytes.into())
35    }
36}
37
38impl Deref for LegacyRawBytecode {
39    type Target = Bytes;
40
41    fn deref(&self) -> &Self::Target {
42        &self.0
43    }
44}