revm_interpreter/interpreter/ext_bytecode/
serde.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use super::ExtBytecode;
use crate::interpreter::Jumps;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Serialize, Deserialize)]
struct ExtBytecodeSerde {
    base: bytecode::Bytecode,
    program_counter: usize,
}

impl Serialize for ExtBytecode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        ExtBytecodeSerde {
            base: self.base.clone(),
            program_counter: self.pc(),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ExtBytecode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let ExtBytecodeSerde {
            base,
            program_counter,
        } = ExtBytecodeSerde::deserialize(deserializer)?;

        let mut bytecode = Self::new(base);

        if program_counter >= bytecode.base.bytecode().len() {
            panic!("serde pc: {program_counter} is greater than or equal to bytecode len");
        }
        bytecode.absolute_jump(program_counter);
        Ok(bytecode)
    }
}